ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java (file contents):
Revision 1.119 by dl, Mon Aug 13 15:50:31 2012 UTC vs.
Revision 1.324 by dl, Fri Mar 18 16:01:41 2022 UTC

# Line 5 | Line 5
5   */
6  
7   package java.util.concurrent;
8 import java.util.concurrent.atomic.LongAdder;
9 import java.util.concurrent.ForkJoinPool;
10 import java.util.concurrent.ForkJoinTask;
8  
9 < import java.util.Comparator;
9 > import java.io.ObjectStreamField;
10 > import java.io.Serializable;
11 > import java.lang.reflect.ParameterizedType;
12 > import java.lang.reflect.Type;
13 > import java.util.AbstractMap;
14   import java.util.Arrays;
14 import java.util.Map;
15 import java.util.Set;
15   import java.util.Collection;
16 < import java.util.AbstractMap;
18 < import java.util.AbstractSet;
19 < import java.util.AbstractCollection;
20 < import java.util.Hashtable;
16 > import java.util.Enumeration;
17   import java.util.HashMap;
18 + import java.util.Hashtable;
19   import java.util.Iterator;
20 < import java.util.Enumeration;
24 < import java.util.ConcurrentModificationException;
20 > import java.util.Map;
21   import java.util.NoSuchElementException;
22 < import java.util.concurrent.ConcurrentMap;
23 < import java.util.concurrent.ThreadLocalRandom;
28 < import java.util.concurrent.locks.LockSupport;
29 < import java.util.concurrent.locks.AbstractQueuedSynchronizer;
22 > import java.util.Set;
23 > import java.util.Spliterator;
24   import java.util.concurrent.atomic.AtomicReference;
25 <
26 < import java.io.Serializable;
25 > import java.util.concurrent.locks.LockSupport;
26 > import java.util.concurrent.locks.ReentrantLock;
27 > import java.util.function.BiConsumer;
28 > import java.util.function.BiFunction;
29 > import java.util.function.Consumer;
30 > import java.util.function.DoubleBinaryOperator;
31 > import java.util.function.Function;
32 > import java.util.function.IntBinaryOperator;
33 > import java.util.function.LongBinaryOperator;
34 > import java.util.function.Predicate;
35 > import java.util.function.ToDoubleBiFunction;
36 > import java.util.function.ToDoubleFunction;
37 > import java.util.function.ToIntBiFunction;
38 > import java.util.function.ToIntFunction;
39 > import java.util.function.ToLongBiFunction;
40 > import java.util.function.ToLongFunction;
41 > import java.util.stream.Stream;
42 > import jdk.internal.misc.Unsafe;
43  
44   /**
45   * A hash table supporting full concurrency of retrievals and
# Line 43 | Line 53 | import java.io.Serializable;
53   * interoperable with {@code Hashtable} in programs that rely on its
54   * thread safety but not on its synchronization details.
55   *
56 < * <p> Retrieval operations (including {@code get}) generally do not
56 > * <p>Retrieval operations (including {@code get}) generally do not
57   * block, so may overlap with update operations (including {@code put}
58   * and {@code remove}). Retrievals reflect the results of the most
59   * recently <em>completed</em> update operations holding upon their
60 < * onset.  For aggregate operations such as {@code putAll} and {@code
61 < * clear}, concurrent retrievals may reflect insertion or removal of
62 < * only some entries.  Similarly, Iterators and Enumerations return
63 < * elements reflecting the state of the hash table at some point at or
64 < * since the creation of the iterator/enumeration.  They do
65 < * <em>not</em> throw {@link ConcurrentModificationException}.
66 < * However, iterators are designed to be used by only one thread at a
67 < * time.  Bear in mind that the results of aggregate status methods
68 < * including {@code size}, {@code isEmpty}, and {@code containsValue}
69 < * are typically useful only when a map is not undergoing concurrent
70 < * updates in other threads.  Otherwise the results of these methods
71 < * reflect transient states that may be adequate for monitoring
72 < * or estimation purposes, but not for program control.
60 > * onset. (More formally, an update operation for a given key bears a
61 > * <em>happens-before</em> relation with any (non-null) retrieval for
62 > * that key reporting the updated value.)  For aggregate operations
63 > * such as {@code putAll} and {@code clear}, concurrent retrievals may
64 > * reflect insertion or removal of only some entries.  Similarly,
65 > * Iterators, Spliterators and Enumerations return elements reflecting the
66 > * state of the hash table at some point at or since the creation of the
67 > * iterator/enumeration.  They do <em>not</em> throw {@link
68 > * java.util.ConcurrentModificationException ConcurrentModificationException}.
69 > * However, iterators are designed to be used by only one thread at a time.
70 > * Bear in mind that the results of aggregate status methods including
71 > * {@code size}, {@code isEmpty}, and {@code containsValue} are typically
72 > * useful only when a map is not undergoing concurrent updates in other threads.
73 > * Otherwise the results of these methods reflect transient states
74 > * that may be adequate for monitoring or estimation purposes, but not
75 > * for program control.
76   *
77 < * <p> The table is dynamically expanded when there are too many
77 > * <p>The table is dynamically expanded when there are too many
78   * collisions (i.e., keys that have distinct hash codes but fall into
79   * the same slot modulo the table size), with the expected average
80   * effect of maintaining roughly two bins per mapping (corresponding
# Line 80 | Line 93 | import java.io.Serializable;
93   * expected {@code concurrencyLevel} as an additional hint for
94   * internal sizing.  Note that using many keys with exactly the same
95   * {@code hashCode()} is a sure way to slow down performance of any
96 < * hash table.
96 > * hash table. To ameliorate impact, when keys are {@link Comparable},
97 > * this class may use comparison order among keys to help break ties.
98 > *
99 > * <p>A {@link Set} projection of a ConcurrentHashMap may be created
100 > * (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed
101 > * (using {@link #keySet(Object)} when only keys are of interest, and the
102 > * mapped values are (perhaps transiently) not used or all take the
103 > * same mapping value.
104 > *
105 > * <p>A ConcurrentHashMap can be used as a scalable frequency map (a
106 > * form of histogram or multiset) by using {@link
107 > * java.util.concurrent.atomic.LongAdder} values and initializing via
108 > * {@link #computeIfAbsent computeIfAbsent}. For example, to add a count
109 > * to a {@code ConcurrentHashMap<String,LongAdder> freqs}, you can use
110 > * {@code freqs.computeIfAbsent(key, k -> new LongAdder()).increment();}
111   *
112   * <p>This class and its views and iterators implement all of the
113   * <em>optional</em> methods of the {@link Map} and {@link Iterator}
114   * interfaces.
115   *
116 < * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
116 > * <p>Like {@link Hashtable} but unlike {@link HashMap}, this class
117   * does <em>not</em> allow {@code null} to be used as a key or value.
118   *
119 + * <p>ConcurrentHashMaps support a set of sequential and parallel bulk
120 + * operations that, unlike most {@link Stream} methods, are designed
121 + * to be safely, and often sensibly, applied even with maps that are
122 + * being concurrently updated by other threads; for example, when
123 + * computing a snapshot summary of the values in a shared registry.
124 + * There are three kinds of operation, each with four forms, accepting
125 + * functions with keys, values, entries, and (key, value) pairs as
126 + * arguments and/or return values. Because the elements of a
127 + * ConcurrentHashMap are not ordered in any particular way, and may be
128 + * processed in different orders in different parallel executions, the
129 + * correctness of supplied functions should not depend on any
130 + * ordering, or on any other objects or values that may transiently
131 + * change while computation is in progress; and except for forEach
132 + * actions, should ideally be side-effect-free. Bulk operations on
133 + * {@link Map.Entry} objects do not support method {@code setValue}.
134 + *
135 + * <ul>
136 + * <li>forEach: Performs a given action on each element.
137 + * A variant form applies a given transformation on each element
138 + * before performing the action.
139 + *
140 + * <li>search: Returns the first available non-null result of
141 + * applying a given function on each element; skipping further
142 + * search when a result is found.
143 + *
144 + * <li>reduce: Accumulates each element.  The supplied reduction
145 + * function cannot rely on ordering (more formally, it should be
146 + * both associative and commutative).  There are five variants:
147 + *
148 + * <ul>
149 + *
150 + * <li>Plain reductions. (There is not a form of this method for
151 + * (key, value) function arguments since there is no corresponding
152 + * return type.)
153 + *
154 + * <li>Mapped reductions that accumulate the results of a given
155 + * function applied to each element.
156 + *
157 + * <li>Reductions to scalar doubles, longs, and ints, using a
158 + * given basis value.
159 + *
160 + * </ul>
161 + * </ul>
162 + *
163 + * <p>These bulk operations accept a {@code parallelismThreshold}
164 + * argument. Methods proceed sequentially if the current map size is
165 + * estimated to be less than the given threshold. Using a value of
166 + * {@code Long.MAX_VALUE} suppresses all parallelism.  Using a value
167 + * of {@code 1} results in maximal parallelism by partitioning into
168 + * enough subtasks to fully utilize the {@link
169 + * ForkJoinPool#commonPool()} that is used for all parallel
170 + * computations. Normally, you would initially choose one of these
171 + * extreme values, and then measure performance of using in-between
172 + * values that trade off overhead versus throughput.
173 + *
174 + * <p>The concurrency properties of bulk operations follow
175 + * from those of ConcurrentHashMap: Any non-null result returned
176 + * from {@code get(key)} and related access methods bears a
177 + * happens-before relation with the associated insertion or
178 + * update.  The result of any bulk operation reflects the
179 + * composition of these per-element relations (but is not
180 + * necessarily atomic with respect to the map as a whole unless it
181 + * is somehow known to be quiescent).  Conversely, because keys
182 + * and values in the map are never null, null serves as a reliable
183 + * atomic indicator of the current lack of any result.  To
184 + * maintain this property, null serves as an implicit basis for
185 + * all non-scalar reduction operations. For the double, long, and
186 + * int versions, the basis should be one that, when combined with
187 + * any other value, returns that other value (more formally, it
188 + * should be the identity element for the reduction). Most common
189 + * reductions have these properties; for example, computing a sum
190 + * with basis 0 or a minimum with basis MAX_VALUE.
191 + *
192 + * <p>Search and transformation functions provided as arguments
193 + * should similarly return null to indicate the lack of any result
194 + * (in which case it is not used). In the case of mapped
195 + * reductions, this also enables transformations to serve as
196 + * filters, returning null (or, in the case of primitive
197 + * specializations, the identity basis) if the element should not
198 + * be combined. You can create compound transformations and
199 + * filterings by composing them yourself under this "null means
200 + * there is nothing there now" rule before using them in search or
201 + * reduce operations.
202 + *
203 + * <p>Methods accepting and/or returning Entry arguments maintain
204 + * key-value associations. They may be useful for example when
205 + * finding the key for the greatest value. Note that "plain" Entry
206 + * arguments can be supplied using {@code new
207 + * AbstractMap.SimpleEntry(k,v)}.
208 + *
209 + * <p>Bulk operations may complete abruptly, throwing an
210 + * exception encountered in the application of a supplied
211 + * function. Bear in mind when handling such exceptions that other
212 + * concurrently executing functions could also have thrown
213 + * exceptions, or would have done so if the first exception had
214 + * not occurred.
215 + *
216 + * <p>Speedups for parallel compared to sequential forms are common
217 + * but not guaranteed.  Parallel operations involving brief functions
218 + * on small maps may execute more slowly than sequential forms if the
219 + * underlying work to parallelize the computation is more expensive
220 + * than the computation itself.  Similarly, parallelization may not
221 + * lead to much actual parallelism if all processors are busy
222 + * performing unrelated tasks.
223 + *
224 + * <p>All arguments to all task methods must be non-null.
225 + *
226   * <p>This class is a member of the
227 < * <a href="{@docRoot}/../technotes/guides/collections/index.html">
227 > * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
228   * Java Collections Framework</a>.
229   *
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 *
230   * @since 1.5
231   * @author Doug Lea
232   * @param <K> the type of keys maintained by this map
233   * @param <V> the type of mapped values
234   */
235 < public class ConcurrentHashMap<K, V>
236 <    implements ConcurrentMap<K, V>, Serializable {
235 > public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
236 >    implements ConcurrentMap<K,V>, Serializable {
237      private static final long serialVersionUID = 7249069246763182397L;
238  
110    /**
111     * A partitionable iterator. A Spliterator can be traversed
112     * directly, but can also be partitioned (before traversal) by
113     * 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 ConcurrentHashMap<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>
156     */
157    public static interface Spliterator<T> extends Iterator<T> {
158        /**
159         * Returns a Spliterator covering approximately half of the
160         * elements, guaranteed not to overlap with those subsequently
161         * returned by this Spliterator.  After invoking this method,
162         * the current Spliterator will <em>not</em> produce any of
163         * the elements of the returned Spliterator, but the two
164         * Spliterators together will produce all of the elements that
165         * would have been produced by this Spliterator had this
166         * method not been called. The exact number of elements
167         * 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
175         */
176        Spliterator<T> split();
177    }
178
239      /*
240       * Overview:
241       *
# Line 186 | Line 246 | public class ConcurrentHashMap<K, V>
246       * the same or better than java.util.HashMap, and to support high
247       * initial insertion rates on an empty table by many threads.
248       *
249 <     * Each key-value mapping is held in a Node.  Because Node fields
250 <     * can contain special values, they are defined using plain Object
251 <     * types. Similarly in turn, all internal methods that use them
252 <     * work off Object types. And similarly, so do the internal
253 <     * methods of auxiliary iterator and view classes.  All public
254 <     * generic typed methods relay in/out of these internal methods,
255 <     * supplying null-checks and casts as needed. This also allows
256 <     * many of the public methods to be factored into a smaller number
257 <     * of internal methods (although sadly not so for the five
258 <     * variants of put-related operations). The validation-based
259 <     * approach explained below leads to a lot of code sprawl because
260 <     * retry-control precludes factoring into smaller methods.
249 >     * This map usually acts as a binned (bucketed) hash table.  Each
250 >     * key-value mapping is held in a Node.  Most nodes are instances
251 >     * of the basic Node class with hash, key, value, and next
252 >     * fields. However, various subclasses exist: TreeNodes are
253 >     * arranged in balanced trees, not lists.  TreeBins hold the roots
254 >     * of sets of TreeNodes. ForwardingNodes are placed at the heads
255 >     * of bins during resizing. ReservationNodes are used as
256 >     * placeholders while establishing values in computeIfAbsent and
257 >     * related methods.  The types TreeBin, ForwardingNode, and
258 >     * ReservationNode do not hold normal user keys, values, or
259 >     * hashes, and are readily distinguishable during search etc
260 >     * because they have negative hash fields and null key and value
261 >     * fields. (These special nodes are either uncommon or transient,
262 >     * so the impact of carrying around some unused fields is
263 >     * insignificant.)
264       *
265       * The table is lazily initialized to a power-of-two size upon the
266       * first insertion.  Each bin in the table normally contains a
# Line 205 | Line 268 | public class ConcurrentHashMap<K, V>
268       * Table accesses require volatile/atomic reads, writes, and
269       * CASes.  Because there is no other way to arrange this without
270       * adding further indirections, we use intrinsics
271 <     * (sun.misc.Unsafe) operations.  The lists of nodes within bins
272 <     * are always accurately traversable under volatile reads, so long
273 <     * as lookups check hash code and non-nullness of value before
274 <     * checking key equality.
275 <     *
276 <     * 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).
271 >     * (jdk.internal.misc.Unsafe) operations.
272 >     *
273 >     * We use the top (sign) bit of Node hash fields for control
274 >     * purposes -- it is available anyway because of addressing
275 >     * constraints.  Nodes with negative hash fields are specially
276 >     * handled or ignored in map methods.
277       *
278       * Insertion (via put or its variants) of the first node in an
279       * empty bin is performed by just CASing it to the bin.  This is
# Line 231 | Line 282 | public class ConcurrentHashMap<K, V>
282       * delete, and replace) require locks.  We do not want to waste
283       * the space required to associate a distinct lock object with
284       * each bin, so instead use the first node of a bin list itself as
285 <     * a lock. Blocking support for these locks relies on the builtin
286 <     * "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.
285 >     * a lock. Locking support for these locks relies on builtin
286 >     * "synchronized" monitors.
287       *
288       * Using the first node of a list as a lock does not by itself
289       * suffice though: When a node is locked, any update must first
290       * validate that it is still the first node after locking it, and
291       * retry if not. Because new nodes are always appended to lists,
292       * once a node is first in a bin, it remains first until deleted
293 <     * 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.
293 >     * or the bin becomes invalidated (upon resizing).
294       *
295       * The main disadvantage of per-bin locks is that other update
296       * operations on other nodes in a bin list protected by the same
# Line 279 | Line 323 | public class ConcurrentHashMap<K, V>
323       * sometimes deviate significantly from uniform randomness.  This
324       * includes the case when N > (1<<30), so some keys MUST collide.
325       * Similarly for dumb or hostile usages in which multiple keys are
326 <     * designed to have identical hash codes. Also, although we guard
327 <     * against the worst effects of this (see method spread), sets of
328 <     * hashes may differ only in bits that do not impact their bin
329 <     * index for a given power-of-two mask.  So we use a secondary
330 <     * strategy that applies when the number of nodes in a bin exceeds
331 <     * 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
326 >     * designed to have identical hash codes or ones that differs only
327 >     * in masked-out high bits. So we use a secondary strategy that
328 >     * applies when the number of nodes in a bin exceeds a
329 >     * threshold. These TreeBins use a balanced tree to hold nodes (a
330 >     * specialized form of red-black trees), bounding search time to
331 >     * O(log N).  Each search step in a TreeBin is at least twice as
332       * slow as in a regular list, but given that N cannot exceed
333       * (1<<64) (before running out of addresses) this bounds search
334       * steps, lock hold times, etc, to reasonable constants (roughly
# Line 298 | Line 339 | public class ConcurrentHashMap<K, V>
339       * iterators in the same way.
340       *
341       * The table is resized when occupancy exceeds a percentage
342 <     * threshold (nominally, 0.75, but see below).  Only a single
343 <     * thread performs the resize (using field "sizeCtl", to arrange
344 <     * exclusion), but the table otherwise remains usable for reads
345 <     * and updates. Resizing proceeds by transferring bins, one by
346 <     * one, from the table to the next table.  Because we are using
347 <     * power-of-two expansion, the elements from each bin must either
348 <     * stay at same index, or move with a power of two offset. We
349 <     * eliminate unnecessary node creation by catching cases where old
350 <     * nodes can be reused because their next fields won't change.  On
351 <     * average, only about one-sixth of them need cloning when a table
352 <     * doubles. The nodes they replace will be garbage collectable as
353 <     * soon as they are no longer referenced by any reader thread that
354 <     * may be in the midst of concurrently traversing table.  Upon
355 <     * transfer, the old table bin contains only a special forwarding
356 <     * node (with hash field "MOVED") that contains the next table as
357 <     * its key. On encountering a forwarding node, access and update
358 <     * operations restart, using the new table.
359 <     *
360 <     * Each bin transfer requires its bin lock. However, unlike other
361 <     * cases, a transfer can skip a bin if it fails to acquire its
362 <     * lock, and revisit it later (unless it is a TreeBin). Method
363 <     * rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that
364 <     * have been skipped because of failure to acquire a lock, and
365 <     * blocks only if none are available (i.e., only very rarely).
366 <     * The transfer operation must also ensure that all accessible
367 <     * bins in both the old and new table are usable by any traversal.
368 <     * When there are no lock acquisition failures, this is arranged
369 <     * simply by proceeding from the last bin (table.length - 1) up
370 <     * towards the first.  Upon seeing a forwarding node, traversals
371 <     * (see class Iter) arrange to move to the new table
372 <     * without revisiting nodes.  However, when any node is skipped
373 <     * during a transfer, all earlier table bins may have become
374 <     * visible, so are initialized with a reverse-forwarding node back
375 <     * to the old table until the new ones are established. (This
376 <     * sometimes requires transiently locking a forwarding node, which
377 <     * is possible under the above encoding.) These more expensive
378 <     * mechanics trigger only when necessary.
342 >     * threshold (nominally, 0.75, but see below).  Any thread
343 >     * noticing an overfull bin may assist in resizing after the
344 >     * initiating thread allocates and sets up the replacement array.
345 >     * However, rather than stalling, these other threads may proceed
346 >     * with insertions etc.  The use of TreeBins shields us from the
347 >     * worst case effects of overfilling while resizes are in
348 >     * progress.  Resizing proceeds by transferring bins, one by one,
349 >     * from the table to the next table. However, threads claim small
350 >     * blocks of indices to transfer (via field transferIndex) before
351 >     * doing so, reducing contention.  A generation stamp in field
352 >     * sizeCtl ensures that resizings do not overlap. Because we are
353 >     * using power-of-two expansion, the elements from each bin must
354 >     * either stay at same index, or move with a power of two
355 >     * offset. We eliminate unnecessary node creation by catching
356 >     * cases where old nodes can be reused because their next fields
357 >     * won't change.  On average, only about one-sixth of them need
358 >     * cloning when a table doubles. The nodes they replace will be
359 >     * garbage collectible as soon as they are no longer referenced by
360 >     * any reader thread that may be in the midst of concurrently
361 >     * traversing table.  Upon transfer, the old table bin contains
362 >     * only a special forwarding node (with hash field "MOVED") that
363 >     * contains the next table as its key. On encountering a
364 >     * forwarding node, access and update operations restart, using
365 >     * the new table.
366 >     *
367 >     * Each bin transfer requires its bin lock, which can stall
368 >     * waiting for locks while resizing. However, because other
369 >     * threads can join in and help resize rather than contend for
370 >     * locks, average aggregate waits become shorter as resizing
371 >     * progresses.  The transfer operation must also ensure that all
372 >     * accessible bins in both the old and new table are usable by any
373 >     * traversal.  This is arranged in part by proceeding from the
374 >     * last bin (table.length - 1) up towards the first.  Upon seeing
375 >     * a forwarding node, traversals (see class Traverser) arrange to
376 >     * move to the new table without revisiting nodes.  To ensure that
377 >     * no intervening nodes are skipped even when moved out of order,
378 >     * a stack (see class TableStack) is created on first encounter of
379 >     * a forwarding node during a traversal, to maintain its place if
380 >     * later processing the current table. The need for these
381 >     * save/restore mechanics is relatively rare, but when one
382 >     * forwarding node is encountered, typically many more will be.
383 >     * So Traversers use a simple caching scheme to avoid creating so
384 >     * many new TableStack nodes. (Thanks to Peter Levart for
385 >     * suggesting use of a stack here.)
386       *
387       * The traversal scheme also applies to partial traversals of
388       * ranges of bins (via an alternate Traverser constructor)
# Line 349 | Line 397 | public class ConcurrentHashMap<K, V>
397       * These cases attempt to override the initial capacity settings,
398       * but harmlessly fail to take effect in cases of races.
399       *
400 <     * The element count is maintained using a LongAdder, which avoids
401 <     * contention on updates but can encounter cache thrashing if read
402 <     * too frequently during concurrent access. To avoid reading so
403 <     * often, resizing is attempted either when a bin lock is
404 <     * contended, or upon adding to a bin already holding two or more
405 <     * nodes (checked before adding in the xIfAbsent methods, after
406 <     * adding in others). Under uniform hash distributions, the
407 <     * probability of this occurring at threshold is around 13%,
408 <     * meaning that only about 1 in 8 puts check threshold (and after
409 <     * resizing, many fewer do so). But this approximation has high
410 <     * variance for small table sizes, so we check on any collision
411 <     * for sizes <= 64. The bulk putAll operation further reduces
412 <     * contention by only committing count updates upon these size
413 <     * checks.
400 >     * The element count is maintained using a specialization of
401 >     * LongAdder. We need to incorporate a specialization rather than
402 >     * just use a LongAdder in order to access implicit
403 >     * contention-sensing that leads to creation of multiple
404 >     * CounterCells.  The counter mechanics avoid contention on
405 >     * updates but can encounter cache thrashing if read too
406 >     * frequently during concurrent access. To avoid reading so often,
407 >     * resizing under contention is attempted only upon adding to a
408 >     * bin already holding two or more nodes. Under uniform hash
409 >     * distributions, the probability of this occurring at threshold
410 >     * is around 13%, meaning that only about 1 in 8 puts check
411 >     * threshold (and after resizing, many fewer do so).
412 >     *
413 >     * TreeBins use a special form of comparison for search and
414 >     * related operations (which is the main reason we cannot use
415 >     * existing collections such as TreeMaps). TreeBins contain
416 >     * Comparable elements, but may contain others, as well as
417 >     * elements that are Comparable but not necessarily Comparable for
418 >     * the same T, so we cannot invoke compareTo among them. To handle
419 >     * this, the tree is ordered primarily by hash value, then by
420 >     * Comparable.compareTo order if applicable.  On lookup at a node,
421 >     * if elements are not comparable or compare as 0 then both left
422 >     * and right children may need to be searched in the case of tied
423 >     * hash values. (This corresponds to the full list search that
424 >     * would be necessary if all elements were non-Comparable and had
425 >     * tied hashes.) On insertion, to keep a total ordering (or as
426 >     * close as is required here) across rebalancings, we compare
427 >     * classes and identityHashCodes as tie-breakers. The red-black
428 >     * balancing code is updated from pre-jdk-collections
429 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
430 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
431 >     * Algorithms" (CLR).
432 >     *
433 >     * TreeBins also require an additional locking mechanism.  While
434 >     * list traversal is always possible by readers even during
435 >     * updates, tree traversal is not, mainly because of tree-rotations
436 >     * that may change the root node and/or its linkages.  TreeBins
437 >     * include a simple read-write lock mechanism parasitic on the
438 >     * main bin-synchronization strategy: Structural adjustments
439 >     * associated with an insertion or removal are already bin-locked
440 >     * (and so cannot conflict with other writers) but must wait for
441 >     * ongoing readers to finish. Since there can be only one such
442 >     * waiter, we use a simple scheme using a single "waiter" field to
443 >     * block writers.  However, readers need never block.  If the root
444 >     * lock is held, they proceed along the slow traversal path (via
445 >     * next-pointers) until the lock becomes available or the list is
446 >     * exhausted, whichever comes first. These cases are not fast, but
447 >     * maximize aggregate expected throughput.
448       *
449       * Maintaining API and serialization compatibility with previous
450       * versions of this class introduces several oddities. Mainly: We
451 <     * leave untouched but unused constructor arguments refering to
451 >     * leave untouched but unused constructor arguments referring to
452       * concurrencyLevel. We accept a loadFactor constructor argument,
453       * but apply it only to initial table capacity (which is the only
454       * time that we can guarantee to honor it.) We also declare an
455       * unused "Segment" class that is instantiated in minimal form
456       * only when serializing.
457 +     *
458 +     * Also, solely for compatibility with previous versions of this
459 +     * class, it extends AbstractMap, even though all of its methods
460 +     * are overridden, so it is just useless baggage.
461 +     *
462 +     * This file is organized to make things a little easier to follow
463 +     * while reading than they might otherwise: First the main static
464 +     * declarations and utilities, then fields, then main public
465 +     * methods (with a few factorings of multiple public methods into
466 +     * internal ones), then sizing methods, trees, traversers, and
467 +     * bulk operations.
468       */
469  
470      /* ---------------- Constants -------------- */
# Line 413 | Line 506 | public class ConcurrentHashMap<K, V>
506      private static final float LOAD_FACTOR = 0.75f;
507  
508      /**
509 <     * The buffer size for skipped bins during transfers. The
510 <     * value is arbitrary but should be large enough to avoid
511 <     * most locking stalls during resizes.
509 >     * The bin count threshold for using a tree rather than list for a
510 >     * bin.  Bins are converted to trees when adding an element to a
511 >     * bin with at least this many nodes. The value must be greater
512 >     * than 2, and should be at least 8 to mesh with assumptions in
513 >     * tree removal about conversion back to plain bins upon
514 >     * shrinkage.
515       */
516 <    private static final int TRANSFER_BUFFER_SIZE = 32;
516 >    static final int TREEIFY_THRESHOLD = 8;
517  
518      /**
519 <     * The bin count threshold for using a tree rather than list for a
520 <     * bin.  The value reflects the approximate break-even point for
521 <     * using tree-based operations.
519 >     * The bin count threshold for untreeifying a (split) bin during a
520 >     * resize operation. Should be less than TREEIFY_THRESHOLD, and at
521 >     * most 6 to mesh with shrinkage detection under removal.
522       */
523 <    private static final int TREE_THRESHOLD = 8;
523 >    static final int UNTREEIFY_THRESHOLD = 6;
524  
525 <    /*
526 <     * Encodings for special uses of Node hash fields. See above for
527 <     * explanation.
525 >    /**
526 >     * The smallest table capacity for which bins may be treeified.
527 >     * (Otherwise the table is resized if too many nodes in a bin.)
528 >     * The value should be at least 4 * TREEIFY_THRESHOLD to avoid
529 >     * conflicts between resizing and treeification thresholds.
530       */
531 <    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 -------------- */
531 >    static final int MIN_TREEIFY_CAPACITY = 64;
532  
533      /**
534 <     * The array of bins. Lazily initialized upon first insertion.
535 <     * Size is always a power of two. Accessed directly by iterators.
534 >     * Minimum number of rebinnings per transfer step. Ranges are
535 >     * subdivided to allow multiple resizer threads.  This value
536 >     * serves as a lower bound to avoid resizers encountering
537 >     * excessive memory contention.  The value should be at least
538 >     * DEFAULT_CAPACITY.
539       */
540 <    transient volatile Node[] table;
540 >    private static final int MIN_TRANSFER_STRIDE = 16;
541  
542      /**
543 <     * The counter maintaining number of elements.
543 >     * The number of bits used for generation stamp in sizeCtl.
544 >     * Must be at least 6 for 32bit arrays.
545       */
546 <    private transient final LongAdder counter;
546 >    private static final int RESIZE_STAMP_BITS = 16;
547  
548      /**
549 <     * Table initialization and resizing control.  When negative, the
550 <     * 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.
549 >     * The maximum number of threads that can help resize.
550 >     * Must fit in 32 - RESIZE_STAMP_BITS bits.
551       */
552 <    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;
464 <
465 <    /** For serialization compatibility. Null unless serialized; see below */
466 <    private Segment<K,V>[] segments;
552 >    private static final int MAX_RESIZERS = (1 << (32 - RESIZE_STAMP_BITS)) - 1;
553  
554 <    /* ---------------- Table element access -------------- */
554 >    /**
555 >     * The bit shift for recording size stamp in sizeCtl.
556 >     */
557 >    private static final int RESIZE_STAMP_SHIFT = 32 - RESIZE_STAMP_BITS;
558  
559      /*
560 <     * 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.
560 >     * Encodings for Node hash fields. See above for explanation.
561       */
562 <
563 <    static final Node tabAt(Node[] tab, int i) { // used by Iter
564 <        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
565 <    }
566 <
567 <    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
568 <        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
569 <    }
570 <
571 <    private static final void setTabAt(Node[] tab, int i, Node v) {
572 <        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
573 <    }
562 >    static final int MOVED     = -1; // hash for forwarding nodes
563 >    static final int TREEBIN   = -2; // hash for roots of trees
564 >    static final int RESERVED  = -3; // hash for transient reservations
565 >    static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
566 >
567 >    /** Number of CPUS, to place bounds on some sizings */
568 >    static final int NCPU = Runtime.getRuntime().availableProcessors();
569 >
570 >    /**
571 >     * Serialized pseudo-fields, provided only for jdk7 compatibility.
572 >     * @serialField segments Segment[]
573 >     *   The segments, each of which is a specialized hash table.
574 >     * @serialField segmentMask int
575 >     *   Mask value for indexing into segments. The upper bits of a
576 >     *   key's hash code are used to choose the segment.
577 >     * @serialField segmentShift int
578 >     *   Shift value for indexing within segments.
579 >     */
580 >    private static final ObjectStreamField[] serialPersistentFields = {
581 >        new ObjectStreamField("segments", Segment[].class),
582 >        new ObjectStreamField("segmentMask", Integer.TYPE),
583 >        new ObjectStreamField("segmentShift", Integer.TYPE),
584 >    };
585  
586      /* ---------------- Nodes -------------- */
587  
588      /**
589 <     * Key-value entry. Note that this is never exported out as a
590 <     * user-visible Map.Entry (see MapEntry below). Nodes with a hash
591 <     * field of MOVED are special, and do not contain user keys or
592 <     * values.  Otherwise, keys are never null, and null val fields
593 <     * indicate that a node is in the process of being deleted or
594 <     * created. For purposes of read-only access, a key may be read
595 <     * before a val, but can only be used after checking val to be
596 <     * non-null.
597 <     */
598 <    static class Node {
599 <        volatile int hash;
600 <        final Object key;
509 <        volatile Object val;
510 <        volatile Node next;
589 >     * Key-value entry.  This class is never exported out as a
590 >     * user-mutable Map.Entry (i.e., one supporting setValue; see
591 >     * MapEntry below), but can be used for read-only traversals used
592 >     * in bulk tasks.  Subclasses of Node with a negative hash field
593 >     * are special, and contain null keys and values (but are never
594 >     * exported).  Otherwise, keys and vals are never null.
595 >     */
596 >    static class Node<K,V> implements Map.Entry<K,V> {
597 >        final int hash;
598 >        final K key;
599 >        volatile V val;
600 >        volatile Node<K,V> next;
601  
602 <        Node(int hash, Object key, Object val, Node next) {
602 >        Node(int hash, K key, V val) {
603              this.hash = hash;
604              this.key = key;
605              this.val = val;
516            this.next = next;
517        }
518
519        /** CompareAndSet the hash field */
520        final boolean casHash(int cmp, int val) {
521            return UNSAFE.compareAndSwapInt(this, hashOffset, cmp, val);
522        }
523
524        /** The number of spins before blocking for a lock */
525        static final int MAX_SPINS =
526            Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
527
528        /**
529         * Spins a while if LOCKED bit set and this node is the first
530         * of its bin, and then sets WAITING bits on hash field and
531         * blocks (once) if they are still set.  It is OK for this
532         * method to return even if lock is not available upon exit,
533         * which enables these simple single-wait mechanics.
534         *
535         * The corresponding signalling operation is performed within
536         * callers: Upon detecting that WAITING has been set when
537         * unlocking lock (via a failed CAS from non-waiting LOCKED
538         * state), unlockers acquire the sync lock and perform a
539         * notifyAll.
540         */
541        final void tryAwaitLock(Node[] tab, int i) {
542            if (tab != null && i >= 0 && i < tab.length) { // bounds check
543                int r = ThreadLocalRandom.current().nextInt(); // randomize spins
544                int spins = MAX_SPINS, h;
545                while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
546                    if (spins >= 0) {
547                        r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
548                        if (r >= 0 && --spins == 0)
549                            Thread.yield();  // yield before block
550                    }
551                    else if (casHash(h, h | WAITING)) {
552                        synchronized (this) {
553                            if (tabAt(tab, i) == this &&
554                                (hash & WAITING) == WAITING) {
555                                try {
556                                    wait();
557                                } catch (InterruptedException ie) {
558                                    Thread.currentThread().interrupt();
559                                }
560                            }
561                            else
562                                notifyAll(); // possibly won race vs signaller
563                        }
564                        break;
565                    }
566                }
567            }
568        }
569
570        // Unsafe mechanics for casHash
571        private static final sun.misc.Unsafe UNSAFE;
572        private static final long hashOffset;
573
574        static {
575            try {
576                UNSAFE = sun.misc.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;
606          }
602    }
607  
608 <    /**
609 <     * A specialized form of red-black tree for use in bins
610 <     * 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 <            }
608 >        Node(int hash, K key, V val, Node<K,V> next) {
609 >            this(hash, key, val);
610 >            this.next = next;
611          }
612  
613 <        /** From CLR */
614 <        private void rotateRight(TreeNode p) {
615 <            if (p != null) {
616 <                TreeNode l = p.left, pp, lr;
617 <                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 <            }
613 >        public final K getKey()     { return key; }
614 >        public final V getValue()   { return val; }
615 >        public final int hashCode() { return key.hashCode() ^ val.hashCode(); }
616 >        public final String toString() {
617 >            return Helpers.mapEntryToString(key, val);
618          }
619 <
620 <        /**
715 <         * Return 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;
619 >        public final V setValue(V value) {
620 >            throw new UnsupportedOperationException();
621          }
622  
623 <        /**
624 <         * Wrapper for getTreeNode used by CHM.get. Tries to obtain
625 <         * read-lock to call getTreeNode, but during failure to get
626 <         * lock, searches along next links.
627 <         */
628 <        final Object getValue(int h, Object k) {
629 <            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;
623 >        public final boolean equals(Object o) {
624 >            Object k, v, u; Map.Entry<?,?> e;
625 >            return ((o instanceof Map.Entry) &&
626 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
627 >                    (v = e.getValue()) != null &&
628 >                    (k == key || k.equals(key)) &&
629 >                    (v == (u = val) || v.equals(u)));
630          }
631  
632          /**
633 <         * Finds or adds a node.
777 <         * @return null if added
633 >         * Virtualized support for map.get(); overridden in subclasses.
634           */
635 <        @SuppressWarnings("unchecked") // suppress Comparable cast warning
636 <            final TreeNode putTreeNode(int h, Object k, Object v) {
637 <            Class<?> c = k.getClass();
638 <            TreeNode pp = root, p = null;
639 <            int dir = 0;
640 <            while (pp != null) { // find existing node or leaf to insert at
641 <                int ph;  Object pk; Class<?> pc;
642 <                p = pp;
643 <                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;
635 >        Node<K,V> find(int h, Object k) {
636 >            Node<K,V> e = this;
637 >            if (k != null) {
638 >                do {
639 >                    K ek;
640 >                    if (e.hash == h &&
641 >                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
642 >                        return e;
643 >                } while ((e = e.next) != null);
644              }
645              return null;
646          }
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        }
647      }
648  
649 <    /* ---------------- Collision reduction methods -------------- */
649 >    /* ---------------- Static utilities -------------- */
650  
651      /**
652 <     * Spreads higher bits to lower, and also forces top 2 bits to 0.
653 <     * Because the table uses power-of-two masking, sets of hashes
654 <     * that vary only in bits above the current mask will always
655 <     * collide. (Among known examples are sets of Float keys holding
656 <     * consecutive whole numbers in small tables.)  To counter this,
657 <     * we apply a transform that spreads the impact of higher bits
652 >     * Spreads (XORs) higher bits of hash to lower and also forces top
653 >     * bit to 0. Because the table uses power-of-two masking, sets of
654 >     * hashes that vary only in bits above the current mask will
655 >     * always collide. (Among known examples are sets of Float keys
656 >     * holding consecutive whole numbers in small tables.)  So we
657 >     * apply a transform that spreads the impact of higher bits
658       * downward. There is a tradeoff between speed, utility, and
659       * quality of bit-spreading. Because many common sets of hashes
660 <     * are already reasonably distributed across bits (so don't benefit
661 <     * from spreading), and because we use trees to handle large sets
662 <     * of collisions in bins, we don't need excessively high quality.
660 >     * are already reasonably distributed (so don't benefit from
661 >     * spreading), and because we use trees to handle large sets of
662 >     * collisions in bins, we just XOR some shifted bits in the
663 >     * cheapest possible way to reduce systematic lossage, as well as
664 >     * to incorporate impact of the highest bits that would otherwise
665 >     * never be used in index calculations because of table bounds.
666       */
667 <    private static final int spread(int h) {
668 <        h ^= (h >>> 18) ^ (h >>> 12);
1066 <        return (h ^ (h >>> 10)) & HASH_BITS;
667 >    static final int spread(int h) {
668 >        return (h ^ (h >>> 16)) & HASH_BITS;
669      }
670  
671      /**
672 <     * Replaces a list bin with a tree bin. Call only when locked.
673 <     * Fails to replace if the given key is non-comparable or table
1072 <     * is, or needs, resizing.
672 >     * Returns a power of two table size for the given desired capacity.
673 >     * See Hackers Delight, sec 3.2
674       */
675 <    private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
676 <        if ((key instanceof Comparable) &&
677 <            (tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) {
1077 <            TreeBin t = new TreeBin();
1078 <            for (Node e = tabAt(tab, index); e != null; e = e.next)
1079 <                t.putTreeNode(e.hash & HASH_BITS, e.key, e.val);
1080 <            setTabAt(tab, index, new Node(MOVED, t, null, null));
1081 <        }
675 >    private static final int tableSizeFor(int c) {
676 >        int n = -1 >>> Integer.numberOfLeadingZeros(c - 1);
677 >        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
678      }
679  
680 <    /* ---------------- Internal access and update methods -------------- */
681 <
682 <    /** Implementation for get and containsKey */
683 <    private final Object internalGet(Object k) {
684 <        int h = spread(k.hashCode());
685 <        retry: for (Node[] tab = table; tab != null;) {
686 <            Node e, p; Object ek, ev; int eh;      // locals to read fields once
687 <            for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
688 <                if ((eh = e.hash) == MOVED) {
689 <                    if ((ek = e.key) instanceof TreeBin)  // search TreeBin
690 <                        return ((TreeBin)ek).getValue(h, k);
691 <                    else {                        // restart with new table
692 <                        tab = (Node[])ek;
693 <                        continue retry;
694 <                    }
680 >    /**
681 >     * Returns x's Class if it is of the form "class C implements
682 >     * Comparable<C>", else null.
683 >     */
684 >    static Class<?> comparableClassFor(Object x) {
685 >        if (x instanceof Comparable) {
686 >            Class<?> c; Type[] ts, as; ParameterizedType p;
687 >            if ((c = x.getClass()) == String.class) // bypass checks
688 >                return c;
689 >            if ((ts = c.getGenericInterfaces()) != null) {
690 >                for (Type t : ts) {
691 >                    if ((t instanceof ParameterizedType) &&
692 >                        ((p = (ParameterizedType)t).getRawType() ==
693 >                         Comparable.class) &&
694 >                        (as = p.getActualTypeArguments()) != null &&
695 >                        as.length == 1 && as[0] == c) // type arg is c
696 >                        return c;
697                  }
1100                else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1101                         ((ek = e.key) == k || k.equals(ek)))
1102                    return ev;
698              }
1104            break;
699          }
700          return null;
701      }
702  
703      /**
704 <     * Implementation for the four public remove/replace methods:
705 <     * Replaces node value with v, conditional upon match of cv if
1112 <     * non-null.  If resulting value is null, delete.
704 >     * Returns k.compareTo(x) if x matches kc (k's screened comparable
705 >     * class), else 0.
706       */
707 <    private final Object internalReplace(Object k, Object v, Object cv) {
708 <        int h = spread(k.hashCode());
709 <        Object oldVal = null;
710 <        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;
707 >    @SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
708 >    static int compareComparables(Class<?> kc, Object k, Object x) {
709 >        return (x == null || x.getClass() != kc ? 0 :
710 >                ((Comparable)k).compareTo(x));
711      }
712  
713 <    /*
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 <    }
713 >    /* ---------------- Table element access -------------- */
714  
715 <    /** Implementation for computeIfAbsent */
716 <    private final Object internalComputeIfAbsent(K k,
717 <                                                 Fun<? super K, ?> mf) {
718 <        int h = spread(k.hashCode());
719 <        Object val = null;
720 <        int count = 0;
721 <        for (Node[] tab = table;;) {
722 <            Node f; int i, fh; Object fk, fv;
723 <            if (tab == null)
724 <                tab = initTable();
725 <            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
726 <                Node node = new Node(fh = h | LOCKED, k, null, null);
727 <                if (casTabAt(tab, i, null, node)) {
1432 <                    count = 1;
1433 <                    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 <    }
715 >    /*
716 >     * Atomic access methods are used for table elements as well as
717 >     * elements of in-progress next table while resizing.  All uses of
718 >     * the tab arguments must be null checked by callers.  All callers
719 >     * also paranoically precheck that tab's length is not zero (or an
720 >     * equivalent check), thus ensuring that any index argument taking
721 >     * the form of a hash value anded with (length - 1) is a valid
722 >     * index.  Note that, to be correct wrt arbitrary concurrency
723 >     * errors by users, these checks must operate on local variables,
724 >     * which accounts for some odd-looking inline assignments below.
725 >     * Note that calls to setTabAt always occur within locked regions,
726 >     * and so require only release ordering.
727 >     */
728  
1547    /** Implementation for compute */
729      @SuppressWarnings("unchecked")
730 <        private final Object internalCompute(K k, boolean onlyIfPresent,
731 <                                             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;
730 >    static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
731 >        return (Node<K,V>)U.getReferenceAcquire(tab, ((long)i << ASHIFT) + ABASE);
732      }
733  
734 <    private final Object internalMerge(K k, V v,
735 <                                       BiFun<? super V, ? super V, ? extends V> mf) {
736 <        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;
734 >    static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,
735 >                                        Node<K,V> c, Node<K,V> v) {
736 >        return U.compareAndSetReference(tab, ((long)i << ASHIFT) + ABASE, c, v);
737      }
738  
739 <    /** Implementation for putAll */
740 <    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();
739 >    static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
740 >        U.putReferenceRelease(tab, ((long)i << ASHIFT) + ABASE, v);
741      }
742  
743 <    /* ---------------- Table Initialization and Resizing -------------- */
743 >    /* ---------------- Fields -------------- */
744  
745      /**
746 <     * Returns a power of two table size for the given desired capacity.
747 <     * See Hackers Delight, sec 3.2
746 >     * The array of bins. Lazily initialized upon first insertion.
747 >     * Size is always a power of two. Accessed directly by iterators.
748       */
749 <    private static final int tableSizeFor(int c) {
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 <    }
749 >    transient volatile Node<K,V>[] table;
750  
751      /**
752 <     * Initializes table, using the size recorded in sizeCtl.
752 >     * The next table to use; non-null only while resizing.
753       */
754 <    private final Node[] initTable() {
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 <    }
754 >    private transient volatile Node<K,V>[] nextTable;
755  
756      /**
757 <     * If table is too small and not already resizing, creates next
758 <     * table and transfers bins.  Rechecks occupancy after a transfer
759 <     * 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 <    }
1945 <
1946 <    /**
1947 <     * Tries to presize table to accommodate the given number of elements.
1948 <     *
1949 <     * @param size number of elements (doesn't need to be perfectly accurate)
757 >     * Base counter value, used mainly when there is no contention,
758 >     * but also as a fallback during table initialization
759 >     * races. Updated via CAS.
760       */
761 <    private final void tryPresize(int size) {
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 <    }
761 >    private transient volatile long baseCount;
762  
763      /**
764 <     * Splits a normal bin with list headed by e into lo and hi parts;
765 <     * installs in given table.
764 >     * Table initialization and resizing control.  When negative, the
765 >     * table is being initialized or resized: -1 for initialization,
766 >     * else -(1 + the number of active resizing threads).  Otherwise,
767 >     * when table is null, holds the initial table size to use upon
768 >     * creation, or 0 for default. After initialization, holds the
769 >     * next element count value upon which to resize the table.
770       */
771 <    private static 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 <    }
771 >    private transient volatile int sizeCtl;
772  
773      /**
774 <     * Splits a tree bin into lo and hi parts; installs in given table.
774 >     * The next table index (plus one) to split while resizing.
775       */
776 <    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 <    }
776 >    private transient volatile int transferIndex;
777  
778      /**
779 <     * Implementation for clear. Steps through each bin, removing all
2162 <     * nodes.
779 >     * Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
780       */
781 <    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 -------------- */
781 >    private transient volatile int cellsBusy;
782  
783      /**
784 <     * Encapsulates traversal for methods such as containsValue; also
785 <     * serves as a base class for other iterators.
786 <     *
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 ConcurrentHashMap<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(ConcurrentHashMap<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)
2331 <                advance();
2332 <            Node e = last;
2333 <            if (e == null)
2334 <                throw new IllegalStateException();
2335 <            last = null;
2336 <            map.remove(e.key);
2337 <        }
784 >     * Table of counter cells. When non-null, size is a power of 2.
785 >     */
786 >    private transient volatile CounterCell[] counterCells;
787  
788 <        public final boolean hasNext() {
789 <            return nextVal != null || advance() != null;
790 <        }
788 >    // views
789 >    private transient KeySetView<K,V> keySet;
790 >    private transient ValuesView<K,V> values;
791 >    private transient EntrySetView<K,V> entrySet;
792  
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    }
793  
794      /* ---------------- Public operations -------------- */
795  
# Line 2352 | Line 797 | public class ConcurrentHashMap<K, V>
797       * Creates a new, empty map with the default initial table size (16).
798       */
799      public ConcurrentHashMap() {
2355        this.counter = new LongAdder();
800      }
801  
802      /**
# Line 2366 | Line 810 | public class ConcurrentHashMap<K, V>
810       * elements is negative
811       */
812      public ConcurrentHashMap(int initialCapacity) {
813 <        if (initialCapacity < 0)
2370 <            throw new IllegalArgumentException();
2371 <        int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
2372 <                   MAXIMUM_CAPACITY :
2373 <                   tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
2374 <        this.counter = new LongAdder();
2375 <        this.sizeCtl = cap;
813 >        this(initialCapacity, LOAD_FACTOR, 1);
814      }
815  
816      /**
# Line 2381 | Line 819 | public class ConcurrentHashMap<K, V>
819       * @param m the map
820       */
821      public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
2384        this.counter = new LongAdder();
822          this.sizeCtl = DEFAULT_CAPACITY;
823 <        internalPutAll(m);
823 >        putAll(m);
824      }
825  
826      /**
# Line 2407 | Line 844 | public class ConcurrentHashMap<K, V>
844  
845      /**
846       * Creates a new, empty map with an initial table size based on
847 <     * the given number of elements ({@code initialCapacity}), table
848 <     * density ({@code loadFactor}), and number of concurrently
847 >     * the given number of elements ({@code initialCapacity}), initial
848 >     * table density ({@code loadFactor}), and number of concurrently
849       * updating threads ({@code concurrencyLevel}).
850       *
851       * @param initialCapacity the initial capacity. The implementation
# Line 2424 | Line 861 | public class ConcurrentHashMap<K, V>
861       * nonpositive
862       */
863      public ConcurrentHashMap(int initialCapacity,
864 <                               float loadFactor, int concurrencyLevel) {
864 >                             float loadFactor, int concurrencyLevel) {
865          if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
866              throw new IllegalArgumentException();
867          if (initialCapacity < concurrencyLevel)   // Use at least as many bins
# Line 2432 | Line 869 | public class ConcurrentHashMap<K, V>
869          long size = (long)(1.0 + (long)initialCapacity / loadFactor);
870          int cap = (size >= (long)MAXIMUM_CAPACITY) ?
871              MAXIMUM_CAPACITY : tableSizeFor((int)size);
2435        this.counter = new LongAdder();
872          this.sizeCtl = cap;
873      }
874  
875 <    /**
2440 <     * {@inheritDoc}
2441 <     */
2442 <    public boolean isEmpty() {
2443 <        return counter.sum() <= 0L; // ignore transient negative values
2444 <    }
875 >    // Original (since JDK1.2) Map methods
876  
877      /**
878       * {@inheritDoc}
879       */
880      public int size() {
881 <        long n = counter.sum();
881 >        long n = sumCount();
882          return ((n < 0L) ? 0 :
883                  (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
884                  (int)n);
885      }
886  
887      /**
888 <     * 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
888 >     * {@inheritDoc}
889       */
890 <    public long mappingCount() {
891 <        long n = counter.sum();
2467 <        return (n < 0L) ? 0L : n;
890 >    public boolean isEmpty() {
891 >        return sumCount() <= 0L; // ignore transient negative values
892      }
893  
894      /**
# Line 2478 | Line 902 | public class ConcurrentHashMap<K, V>
902       *
903       * @throws NullPointerException if the specified key is null
904       */
905 <    @SuppressWarnings("unchecked")
906 <        public V get(Object key) {
907 <        if (key == null)
908 <            throw new NullPointerException();
909 <        return (V)internalGet(key);
905 >    public V get(Object key) {
906 >        Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
907 >        int h = spread(key.hashCode());
908 >        if ((tab = table) != null && (n = tab.length) > 0 &&
909 >            (e = tabAt(tab, (n - 1) & h)) != null) {
910 >            if ((eh = e.hash) == h) {
911 >                if ((ek = e.key) == key || (ek != null && key.equals(ek)))
912 >                    return e.val;
913 >            }
914 >            else if (eh < 0)
915 >                return (p = e.find(h, key)) != null ? p.val : null;
916 >            while ((e = e.next) != null) {
917 >                if (e.hash == h &&
918 >                    ((ek = e.key) == key || (ek != null && key.equals(ek))))
919 >                    return e.val;
920 >            }
921 >        }
922 >        return null;
923      }
924  
925      /**
926       * Tests if the specified object is a key in this table.
927       *
928 <     * @param  key   possible key
928 >     * @param  key possible key
929       * @return {@code true} if and only if the specified object
930       *         is a key in this table, as determined by the
931       *         {@code equals} method; {@code false} otherwise
932       * @throws NullPointerException if the specified key is null
933       */
934      public boolean containsKey(Object key) {
935 <        if (key == null)
2499 <            throw new NullPointerException();
2500 <        return internalGet(key) != null;
935 >        return get(key) != null;
936      }
937  
938      /**
# Line 2513 | Line 948 | public class ConcurrentHashMap<K, V>
948      public boolean containsValue(Object value) {
949          if (value == null)
950              throw new NullPointerException();
951 <        Object v;
952 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
953 <        while ((v = it.advance()) != null) {
954 <            if (v == value || value.equals(v))
955 <                return true;
951 >        Node<K,V>[] t;
952 >        if ((t = table) != null) {
953 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
954 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
955 >                V v;
956 >                if ((v = p.val) == value || (v != null && value.equals(v)))
957 >                    return true;
958 >            }
959          }
960          return false;
961      }
962  
963      /**
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    /**
964       * Maps the specified key to the specified value in this table.
965       * Neither the key nor the value can be null.
966       *
967 <     * <p> The value can be retrieved by calling the {@code get} method
967 >     * <p>The value can be retrieved by calling the {@code get} method
968       * with a key that is equal to the original key.
969       *
970       * @param key key with which the specified value is to be associated
# Line 2554 | Line 973 | public class ConcurrentHashMap<K, V>
973       *         {@code null} if there was no mapping for {@code key}
974       * @throws NullPointerException if the specified key or value is null
975       */
976 <    @SuppressWarnings("unchecked")
977 <        public V put(K key, V value) {
2559 <        if (key == null || value == null)
2560 <            throw new NullPointerException();
2561 <        return (V)internalPut(key, value);
976 >    public V put(K key, V value) {
977 >        return putVal(key, value, false);
978      }
979  
980 <    /**
981 <     * {@inheritDoc}
982 <     *
983 <     * @return the previous value associated with the specified key,
984 <     *         or {@code null} if there was no mapping for the key
985 <     * @throws NullPointerException if the specified key or value is null
986 <     */
987 <    @SuppressWarnings("unchecked")
988 <        public V putIfAbsent(K key, V value) {
989 <        if (key == null || value == null)
990 <            throw new NullPointerException();
991 <        return (V)internalPutIfAbsent(key, value);
980 >    /** Implementation for put and putIfAbsent */
981 >    final V putVal(K key, V value, boolean onlyIfAbsent) {
982 >        if (key == null || value == null) throw new NullPointerException();
983 >        int hash = spread(key.hashCode());
984 >        int binCount = 0;
985 >        for (Node<K,V>[] tab = table;;) {
986 >            Node<K,V> f; int n, i, fh; K fk; V fv;
987 >            if (tab == null || (n = tab.length) == 0)
988 >                tab = initTable();
989 >            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
990 >                if (casTabAt(tab, i, null, new Node<K,V>(hash, key, value)))
991 >                    break;                   // no lock when adding to empty bin
992 >            }
993 >            else if ((fh = f.hash) == MOVED)
994 >                tab = helpTransfer(tab, f);
995 >            else if (onlyIfAbsent // check first node without acquiring lock
996 >                     && fh == hash
997 >                     && ((fk = f.key) == key || (fk != null && key.equals(fk)))
998 >                     && (fv = f.val) != null)
999 >                return fv;
1000 >            else {
1001 >                V oldVal = null;
1002 >                synchronized (f) {
1003 >                    if (tabAt(tab, i) == f) {
1004 >                        if (fh >= 0) {
1005 >                            binCount = 1;
1006 >                            for (Node<K,V> e = f;; ++binCount) {
1007 >                                K ek;
1008 >                                if (e.hash == hash &&
1009 >                                    ((ek = e.key) == key ||
1010 >                                     (ek != null && key.equals(ek)))) {
1011 >                                    oldVal = e.val;
1012 >                                    if (!onlyIfAbsent)
1013 >                                        e.val = value;
1014 >                                    break;
1015 >                                }
1016 >                                Node<K,V> pred = e;
1017 >                                if ((e = e.next) == null) {
1018 >                                    pred.next = new Node<K,V>(hash, key, value);
1019 >                                    break;
1020 >                                }
1021 >                            }
1022 >                        }
1023 >                        else if (f instanceof TreeBin) {
1024 >                            Node<K,V> p;
1025 >                            binCount = 2;
1026 >                            if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
1027 >                                                           value)) != null) {
1028 >                                oldVal = p.val;
1029 >                                if (!onlyIfAbsent)
1030 >                                    p.val = value;
1031 >                            }
1032 >                        }
1033 >                        else if (f instanceof ReservationNode)
1034 >                            throw new IllegalStateException("Recursive update");
1035 >                    }
1036 >                }
1037 >                if (binCount != 0) {
1038 >                    if (binCount >= TREEIFY_THRESHOLD)
1039 >                        treeifyBin(tab, i);
1040 >                    if (oldVal != null)
1041 >                        return oldVal;
1042 >                    break;
1043 >                }
1044 >            }
1045 >        }
1046 >        addCount(1L, binCount);
1047 >        return null;
1048      }
1049  
1050      /**
# Line 2583 | Line 1055 | public class ConcurrentHashMap<K, V>
1055       * @param m mappings to be stored in this map
1056       */
1057      public void putAll(Map<? extends K, ? extends V> m) {
1058 <        internalPutAll(m);
1059 <    }
1060 <
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
2661 <     *         the specified key, or null if none.
2662 <     * @throws NullPointerException if the specified key or remappingFunction
2663 <     *         is null
2664 <     * @throws IllegalStateException if the computation detectably
2665 <     *         attempts a recursive update to this map that would
2666 <     *         otherwise never complete
2667 <     * @throws RuntimeException or Error if the remappingFunction does so,
2668 <     *         in which case the mapping is unchanged
2669 <     */
2670 <    public V computeIfPresent(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2671 <        if (key == null || remappingFunction == null)
2672 <            throw new NullPointerException();
2673 <        return (V)internalCompute(key, true, remappingFunction);
2674 <    }
2675 <
2676 <    /**
2677 <     * Computes a new mapping value given a key and
2678 <     * its current mapped value (or {@code null} if there is no current
2679 <     * mapping). This is equivalent to
2680 <     *  <pre> {@code
2681 <     *   value = remappingFunction.apply(key, map.get(key));
2682 <     *   if (value != null)
2683 <     *     map.put(key, value);
2684 <     *   else
2685 <     *     map.remove(key);
2686 <     * }</pre>
2687 <     *
2688 <     * except that the action is performed atomically.  If the
2689 <     * function returns {@code null}, the mapping is removed.  If the
2690 <     * function itself throws an (unchecked) exception, the exception
2691 <     * is rethrown to its caller, and the current mapping is left
2692 <     * unchanged.  Some attempted update operations on this map by
2693 <     * other threads may be blocked while computation is in progress,
2694 <     * so the computation should be short and simple, and must not
2695 <     * attempt to update any other mappings of this Map. For example,
2696 <     * to either create or append new messages to a value mapping:
2697 <     *
2698 <     * <pre> {@code
2699 <     * Map<Key, String> map = ...;
2700 <     * final String msg = ...;
2701 <     * map.compute(key, new BiFun<Key, String, String>() {
2702 <     *   public String apply(Key k, String v) {
2703 <     *    return (v == null) ? msg : v + msg;});}}</pre>
2704 <     *
2705 <     * @param key key with which the specified value is to be associated
2706 <     * @param remappingFunction the function to compute a value
2707 <     * @return the new value associated with
2708 <     *         the specified key, or null if none.
2709 <     * @throws NullPointerException if the specified key or remappingFunction
2710 <     *         is null
2711 <     * @throws IllegalStateException if the computation detectably
2712 <     *         attempts a recursive update to this map that would
2713 <     *         otherwise never complete
2714 <     * @throws RuntimeException or Error if the remappingFunction does so,
2715 <     *         in which case the mapping is unchanged
2716 <     */
2717 <    //    @SuppressWarnings("unchecked")
2718 <    public V compute(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2719 <        if (key == null || remappingFunction == null)
2720 <            throw new NullPointerException();
2721 <        return (V)internalCompute(key, false, remappingFunction);
2722 <    }
2723 <
2724 <    /**
2725 <     * If the specified key is not already associated
2726 <     * with a value, associate it with the given value.
2727 <     * Otherwise, replace the value with the results of
2728 <     * the given remapping function. This is equivalent to:
2729 <     *  <pre> {@code
2730 <     *   if (!map.containsKey(key))
2731 <     *     map.put(value);
2732 <     *   else {
2733 <     *     newValue = remappingFunction.apply(map.get(key), value);
2734 <     *     if (value != null)
2735 <     *       map.put(key, value);
2736 <     *     else
2737 <     *       map.remove(key);
2738 <     *   }
2739 <     * }</pre>
2740 <     * except that the action is performed atomically.  If the
2741 <     * function returns {@code null}, the mapping is removed.  If the
2742 <     * function itself throws an (unchecked) exception, the exception
2743 <     * is rethrown to its caller, and the current mapping is left
2744 <     * unchanged.  Some attempted update operations on this map by
2745 <     * other threads may be blocked while computation is in progress,
2746 <     * so the computation should be short and simple, and must not
2747 <     * attempt to update any other mappings of this Map.
2748 <     */
2749 <    //    @SuppressWarnings("unchecked")
2750 <    public V merge(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2751 <        if (key == null || value == null || remappingFunction == null)
2752 <            throw new NullPointerException();
2753 <        return (V)internalMerge(key, value, remappingFunction);
1058 >        tryPresize(m.size());
1059 >        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
1060 >            putVal(e.getKey(), e.getValue(), false);
1061      }
1062  
1063      /**
# Line 2762 | Line 1069 | public class ConcurrentHashMap<K, V>
1069       *         {@code null} if there was no mapping for {@code key}
1070       * @throws NullPointerException if the specified key is null
1071       */
1072 <    @SuppressWarnings("unchecked")
1073 <        public V remove(Object key) {
2767 <        if (key == null)
2768 <            throw new NullPointerException();
2769 <        return (V)internalReplace(key, null, null);
2770 <    }
2771 <
2772 <    /**
2773 <     * {@inheritDoc}
2774 <     *
2775 <     * @throws NullPointerException if the specified key is null
2776 <     */
2777 <    public boolean remove(Object key, Object value) {
2778 <        if (key == null)
2779 <            throw new NullPointerException();
2780 <        if (value == null)
2781 <            return false;
2782 <        return internalReplace(key, null, value) != null;
1072 >    public V remove(Object key) {
1073 >        return replaceNode(key, null, null);
1074      }
1075  
1076      /**
1077 <     * {@inheritDoc}
1078 <     *
1079 <     * @throws NullPointerException if any of the arguments are null
2789 <     */
2790 <    public boolean replace(K key, V oldValue, V newValue) {
2791 <        if (key == null || oldValue == null || newValue == null)
2792 <            throw new NullPointerException();
2793 <        return internalReplace(key, newValue, oldValue) != null;
2794 <    }
2795 <
2796 <    /**
2797 <     * {@inheritDoc}
2798 <     *
2799 <     * @return the previous value associated with the specified key,
2800 <     *         or {@code null} if there was no mapping for the key
2801 <     * @throws NullPointerException if the specified key or value is null
1077 >     * Implementation for the four public remove/replace methods:
1078 >     * Replaces node value with v, conditional upon match of cv if
1079 >     * non-null.  If resulting value is null, delete.
1080       */
1081 <    @SuppressWarnings("unchecked")
1082 <        public V replace(K key, V value) {
1083 <        if (key == null || value == null)
1084 <            throw new NullPointerException();
1085 <        return (V)internalReplace(key, value, null);
1081 >    final V replaceNode(Object key, V value, Object cv) {
1082 >        int hash = spread(key.hashCode());
1083 >        for (Node<K,V>[] tab = table;;) {
1084 >            Node<K,V> f; int n, i, fh;
1085 >            if (tab == null || (n = tab.length) == 0 ||
1086 >                (f = tabAt(tab, i = (n - 1) & hash)) == null)
1087 >                break;
1088 >            else if ((fh = f.hash) == MOVED)
1089 >                tab = helpTransfer(tab, f);
1090 >            else {
1091 >                V oldVal = null;
1092 >                boolean validated = false;
1093 >                synchronized (f) {
1094 >                    if (tabAt(tab, i) == f) {
1095 >                        if (fh >= 0) {
1096 >                            validated = true;
1097 >                            for (Node<K,V> e = f, pred = null;;) {
1098 >                                K ek;
1099 >                                if (e.hash == hash &&
1100 >                                    ((ek = e.key) == key ||
1101 >                                     (ek != null && key.equals(ek)))) {
1102 >                                    V ev = e.val;
1103 >                                    if (cv == null || cv == ev ||
1104 >                                        (ev != null && cv.equals(ev))) {
1105 >                                        oldVal = ev;
1106 >                                        if (value != null)
1107 >                                            e.val = value;
1108 >                                        else if (pred != null)
1109 >                                            pred.next = e.next;
1110 >                                        else
1111 >                                            setTabAt(tab, i, e.next);
1112 >                                    }
1113 >                                    break;
1114 >                                }
1115 >                                pred = e;
1116 >                                if ((e = e.next) == null)
1117 >                                    break;
1118 >                            }
1119 >                        }
1120 >                        else if (f instanceof TreeBin) {
1121 >                            validated = true;
1122 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1123 >                            TreeNode<K,V> r, p;
1124 >                            if ((r = t.root) != null &&
1125 >                                (p = r.findTreeNode(hash, key, null)) != null) {
1126 >                                V pv = p.val;
1127 >                                if (cv == null || cv == pv ||
1128 >                                    (pv != null && cv.equals(pv))) {
1129 >                                    oldVal = pv;
1130 >                                    if (value != null)
1131 >                                        p.val = value;
1132 >                                    else if (t.removeTreeNode(p))
1133 >                                        setTabAt(tab, i, untreeify(t.first));
1134 >                                }
1135 >                            }
1136 >                        }
1137 >                        else if (f instanceof ReservationNode)
1138 >                            throw new IllegalStateException("Recursive update");
1139 >                    }
1140 >                }
1141 >                if (validated) {
1142 >                    if (oldVal != null) {
1143 >                        if (value == null)
1144 >                            addCount(-1L, -1);
1145 >                        return oldVal;
1146 >                    }
1147 >                    break;
1148 >                }
1149 >            }
1150 >        }
1151 >        return null;
1152      }
1153  
1154      /**
1155       * Removes all of the mappings from this map.
1156       */
1157      public void clear() {
1158 <        internalClear();
1158 >        long delta = 0L; // negative number of deletions
1159 >        int i = 0;
1160 >        Node<K,V>[] tab = table;
1161 >        while (tab != null && i < tab.length) {
1162 >            int fh;
1163 >            Node<K,V> f = tabAt(tab, i);
1164 >            if (f == null)
1165 >                ++i;
1166 >            else if ((fh = f.hash) == MOVED) {
1167 >                tab = helpTransfer(tab, f);
1168 >                i = 0; // restart
1169 >            }
1170 >            else {
1171 >                synchronized (f) {
1172 >                    if (tabAt(tab, i) == f) {
1173 >                        Node<K,V> p = (fh >= 0 ? f :
1174 >                                       (f instanceof TreeBin) ?
1175 >                                       ((TreeBin<K,V>)f).first : null);
1176 >                        while (p != null) {
1177 >                            --delta;
1178 >                            p = p.next;
1179 >                        }
1180 >                        setTabAt(tab, i++, null);
1181 >                    }
1182 >                }
1183 >            }
1184 >        }
1185 >        if (delta != 0L)
1186 >            addCount(delta, -1);
1187      }
1188  
1189      /**
1190       * Returns a {@link Set} view of the keys contained in this map.
1191       * The set is backed by the map, so changes to the map are
1192 <     * reflected in the set, and vice-versa.  The set supports element
1192 >     * reflected in the set, and vice-versa. The set supports element
1193       * removal, which removes the corresponding mapping from this map,
1194       * via the {@code Iterator.remove}, {@code Set.remove},
1195       * {@code removeAll}, {@code retainAll}, and {@code clear}
1196       * operations.  It does not support the {@code add} or
1197       * {@code addAll} operations.
1198       *
1199 <     * <p>The view's {@code iterator} is a "weakly consistent" iterator
1200 <     * that will never throw {@link ConcurrentModificationException},
1201 <     * and guarantees to traverse elements as they existed upon
1202 <     * construction of the iterator, and may (but is not guaranteed to)
1203 <     * reflect any modifications subsequent to construction.
1204 <     */
1205 <    public Set<K> keySet() {
1206 <        KeySet<K,V> ks = keySet;
1207 <        return (ks != null) ? ks : (keySet = new KeySet<K,V>(this));
1199 >     * <p>The view's iterators and spliterators are
1200 >     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
1201 >     *
1202 >     * <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT},
1203 >     * {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}.
1204 >     *
1205 >     * @return the set view
1206 >     */
1207 >    public KeySetView<K,V> keySet() {
1208 >        KeySetView<K,V> ks;
1209 >        if ((ks = keySet) != null) return ks;
1210 >        return keySet = new KeySetView<K,V>(this, null);
1211      }
1212  
1213      /**
# Line 2845 | Line 1220 | public class ConcurrentHashMap<K, V>
1220       * {@code retainAll}, and {@code clear} operations.  It does not
1221       * support the {@code add} or {@code addAll} operations.
1222       *
1223 <     * <p>The view's {@code iterator} is a "weakly consistent" iterator
1224 <     * that will never throw {@link ConcurrentModificationException},
1225 <     * and guarantees to traverse elements as they existed upon
1226 <     * construction of the iterator, and may (but is not guaranteed to)
1227 <     * reflect any modifications subsequent to construction.
1223 >     * <p>The view's iterators and spliterators are
1224 >     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
1225 >     *
1226 >     * <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT}
1227 >     * and {@link Spliterator#NONNULL}.
1228 >     *
1229 >     * @return the collection view
1230       */
1231      public Collection<V> values() {
1232 <        Values<K,V> vs = values;
1233 <        return (vs != null) ? vs : (values = new Values<K,V>(this));
1232 >        ValuesView<K,V> vs;
1233 >        if ((vs = values) != null) return vs;
1234 >        return values = new ValuesView<K,V>(this);
1235      }
1236  
1237      /**
# Line 2863 | Line 1241 | public class ConcurrentHashMap<K, V>
1241       * removal, which removes the corresponding mapping from the map,
1242       * via the {@code Iterator.remove}, {@code Set.remove},
1243       * {@code removeAll}, {@code retainAll}, and {@code clear}
1244 <     * operations.  It does not support the {@code add} or
2867 <     * {@code addAll} operations.
2868 <     *
2869 <     * <p>The view's {@code iterator} is a "weakly consistent" iterator
2870 <     * that will never throw {@link ConcurrentModificationException},
2871 <     * and guarantees to traverse elements as they existed upon
2872 <     * construction of the iterator, and may (but is not guaranteed to)
2873 <     * reflect any modifications subsequent to construction.
2874 <     */
2875 <    public Set<Map.Entry<K,V>> entrySet() {
2876 <        EntrySet<K,V> es = entrySet;
2877 <        return (es != null) ? es : (entrySet = new EntrySet<K,V>(this));
2878 <    }
2879 <
2880 <    /**
2881 <     * Returns an enumeration of the keys in this table.
2882 <     *
2883 <     * @return an enumeration of the keys in this table
2884 <     * @see #keySet()
2885 <     */
2886 <    public Enumeration<K> keys() {
2887 <        return new KeyIterator<K,V>(this);
2888 <    }
2889 <
2890 <    /**
2891 <     * Returns an enumeration of the values in this table.
1244 >     * operations.
1245       *
1246 <     * @return an enumeration of the values in this table
1247 <     * @see #values()
2895 <     */
2896 <    public Enumeration<V> elements() {
2897 <        return new ValueIterator<K,V>(this);
2898 <    }
2899 <
2900 <    /**
2901 <     * Returns a partionable iterator of the keys in this map.
2902 <     *
2903 <     * @return a partionable iterator of the keys in this map
2904 <     */
2905 <    public Spliterator<K> keySpliterator() {
2906 <        return new KeyIterator<K,V>(this);
2907 <    }
2908 <
2909 <    /**
2910 <     * Returns a partionable iterator of the values in this map.
1246 >     * <p>The view's iterators and spliterators are
1247 >     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
1248       *
1249 <     * @return a partionable iterator of the values in this map
1250 <     */
2914 <    public Spliterator<V> valueSpliterator() {
2915 <        return new ValueIterator<K,V>(this);
2916 <    }
2917 <
2918 <    /**
2919 <     * Returns a partionable iterator of the entries in this map.
1249 >     * <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT},
1250 >     * {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}.
1251       *
1252 <     * @return a partionable iterator of the entries in this map
1252 >     * @return the set view
1253       */
1254 <    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
1255 <        return new EntryIterator<K,V>(this);
1254 >    public Set<Map.Entry<K,V>> entrySet() {
1255 >        EntrySetView<K,V> es;
1256 >        if ((es = entrySet) != null) return es;
1257 >        return entrySet = new EntrySetView<K,V>(this);
1258      }
1259  
1260      /**
# Line 2933 | Line 1266 | public class ConcurrentHashMap<K, V>
1266       */
1267      public int hashCode() {
1268          int h = 0;
1269 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1270 <        Object v;
1271 <        while ((v = it.advance()) != null) {
1272 <            h += it.nextKey.hashCode() ^ v.hashCode();
1269 >        Node<K,V>[] t;
1270 >        if ((t = table) != null) {
1271 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1272 >            for (Node<K,V> p; (p = it.advance()) != null; )
1273 >                h += p.key.hashCode() ^ p.val.hashCode();
1274          }
1275          return h;
1276      }
# Line 2953 | Line 1287 | public class ConcurrentHashMap<K, V>
1287       * @return a string representation of this map
1288       */
1289      public String toString() {
1290 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1290 >        Node<K,V>[] t;
1291 >        int f = (t = table) == null ? 0 : t.length;
1292 >        Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1293          StringBuilder sb = new StringBuilder();
1294          sb.append('{');
1295 <        Object v;
1296 <        if ((v = it.advance()) != null) {
1295 >        Node<K,V> p;
1296 >        if ((p = it.advance()) != null) {
1297              for (;;) {
1298 <                Object k = it.nextKey;
1298 >                K k = p.key;
1299 >                V v = p.val;
1300                  sb.append(k == this ? "(this Map)" : k);
1301                  sb.append('=');
1302                  sb.append(v == this ? "(this Map)" : v);
1303 <                if ((v = it.advance()) == null)
1303 >                if ((p = it.advance()) == null)
1304                      break;
1305                  sb.append(',').append(' ');
1306              }
# Line 2986 | Line 1323 | public class ConcurrentHashMap<K, V>
1323              if (!(o instanceof Map))
1324                  return false;
1325              Map<?,?> m = (Map<?,?>) o;
1326 <            Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
1327 <            Object val;
1328 <            while ((val = it.advance()) != null) {
1329 <                Object v = m.get(it.nextKey);
1326 >            Node<K,V>[] t;
1327 >            int f = (t = table) == null ? 0 : t.length;
1328 >            Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1329 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1330 >                V val = p.val;
1331 >                Object v = m.get(p.key);
1332                  if (v == null || (v != val && !v.equals(val)))
1333                      return false;
1334              }
# Line 2997 | Line 1336 | public class ConcurrentHashMap<K, V>
1336                  Object mk, mv, v;
1337                  if ((mk = e.getKey()) == null ||
1338                      (mv = e.getValue()) == null ||
1339 <                    (v = internalGet(mk)) == null ||
1339 >                    (v = get(mk)) == null ||
1340                      (mv != v && !mv.equals(v)))
1341                      return false;
1342              }
# Line 3005 | Line 1344 | public class ConcurrentHashMap<K, V>
1344          return true;
1345      }
1346  
1347 <    /* ----------------Iterators -------------- */
1347 >    /**
1348 >     * Stripped-down version of helper class used in previous version,
1349 >     * declared for the sake of serialization compatibility.
1350 >     */
1351 >    static class Segment<K,V> extends ReentrantLock implements Serializable {
1352 >        private static final long serialVersionUID = 2249069246763182397L;
1353 >        final float loadFactor;
1354 >        Segment(float lf) { this.loadFactor = lf; }
1355 >    }
1356  
1357 <    static final class KeyIterator<K,V> extends Traverser<K,V,Object>
1358 <        implements Spliterator<K>, Enumeration<K> {
1359 <        KeyIterator(ConcurrentHashMap<K, V> map) { super(map); }
1360 <        KeyIterator(Traverser<K,V,Object> it, boolean split) {
1361 <            super(it, split);
1362 <        }
1363 <        public KeyIterator<K,V> split() {
1364 <            if (last != null || (next != null && nextVal == null))
1365 <                throw new IllegalStateException();
1366 <            return new KeyIterator<K,V>(this, true);
1357 >    /**
1358 >     * Saves this map to a stream (that is, serializes it).
1359 >     *
1360 >     * @param s the stream
1361 >     * @throws java.io.IOException if an I/O error occurs
1362 >     * @serialData
1363 >     * the serialized fields, followed by the key (Object) and value
1364 >     * (Object) for each key-value mapping, followed by a null pair.
1365 >     * The key-value mappings are emitted in no particular order.
1366 >     */
1367 >    private void writeObject(java.io.ObjectOutputStream s)
1368 >        throws java.io.IOException {
1369 >        // For serialization compatibility
1370 >        // Emulate segment calculation from previous version of this class
1371 >        int sshift = 0;
1372 >        int ssize = 1;
1373 >        while (ssize < DEFAULT_CONCURRENCY_LEVEL) {
1374 >            ++sshift;
1375 >            ssize <<= 1;
1376          }
1377 +        int segmentShift = 32 - sshift;
1378 +        int segmentMask = ssize - 1;
1379          @SuppressWarnings("unchecked")
1380 <            public final K next() {
1381 <            if (nextVal == null && advance() == null)
1382 <                throw new NoSuchElementException();
1383 <            Object k = nextKey;
1384 <            nextVal = null;
1385 <            return (K) k;
1380 >        Segment<K,V>[] segments = (Segment<K,V>[])
1381 >            new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
1382 >        for (int i = 0; i < segments.length; ++i)
1383 >            segments[i] = new Segment<K,V>(LOAD_FACTOR);
1384 >        java.io.ObjectOutputStream.PutField streamFields = s.putFields();
1385 >        streamFields.put("segments", segments);
1386 >        streamFields.put("segmentShift", segmentShift);
1387 >        streamFields.put("segmentMask", segmentMask);
1388 >        s.writeFields();
1389 >
1390 >        Node<K,V>[] t;
1391 >        if ((t = table) != null) {
1392 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1393 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1394 >                s.writeObject(p.key);
1395 >                s.writeObject(p.val);
1396 >            }
1397          }
1398 <
1399 <        public final K nextElement() { return next(); }
1398 >        s.writeObject(null);
1399 >        s.writeObject(null);
1400      }
1401  
1402 <    static final class ValueIterator<K,V> extends Traverser<K,V,Object>
1403 <        implements Spliterator<V>, Enumeration<V> {
1404 <        ValueIterator(ConcurrentHashMap<K, V> map) { super(map); }
1405 <        ValueIterator(Traverser<K,V,Object> it, boolean split) {
1406 <            super(it, split);
1402 >    /**
1403 >     * Reconstitutes this map from a stream (that is, deserializes it).
1404 >     * @param s the stream
1405 >     * @throws ClassNotFoundException if the class of a serialized object
1406 >     *         could not be found
1407 >     * @throws java.io.IOException if an I/O error occurs
1408 >     */
1409 >    private void readObject(java.io.ObjectInputStream s)
1410 >        throws java.io.IOException, ClassNotFoundException {
1411 >        /*
1412 >         * To improve performance in typical cases, we create nodes
1413 >         * while reading, then place in table once size is known.
1414 >         * However, we must also validate uniqueness and deal with
1415 >         * overpopulated bins while doing so, which requires
1416 >         * specialized versions of putVal mechanics.
1417 >         */
1418 >        sizeCtl = -1; // force exclusion for table construction
1419 >        s.defaultReadObject();
1420 >        long size = 0L;
1421 >        Node<K,V> p = null;
1422 >        for (;;) {
1423 >            @SuppressWarnings("unchecked")
1424 >            K k = (K) s.readObject();
1425 >            @SuppressWarnings("unchecked")
1426 >            V v = (V) s.readObject();
1427 >            if (k != null && v != null) {
1428 >                p = new Node<K,V>(spread(k.hashCode()), k, v, p);
1429 >                ++size;
1430 >            }
1431 >            else
1432 >                break;
1433          }
1434 <        public ValueIterator<K,V> split() {
1435 <            if (last != null || (next != null && nextVal == null))
1436 <                throw new IllegalStateException();
1437 <            return new ValueIterator<K,V>(this, true);
1434 >        if (size == 0L)
1435 >            sizeCtl = 0;
1436 >        else {
1437 >            long ts = (long)(1.0 + size / LOAD_FACTOR);
1438 >            int n = (ts >= (long)MAXIMUM_CAPACITY) ?
1439 >                MAXIMUM_CAPACITY : tableSizeFor((int)ts);
1440 >            @SuppressWarnings("unchecked")
1441 >            Node<K,V>[] tab = (Node<K,V>[])new Node<?,?>[n];
1442 >            int mask = n - 1;
1443 >            long added = 0L;
1444 >            while (p != null) {
1445 >                boolean insertAtFront;
1446 >                Node<K,V> next = p.next, first;
1447 >                int h = p.hash, j = h & mask;
1448 >                if ((first = tabAt(tab, j)) == null)
1449 >                    insertAtFront = true;
1450 >                else {
1451 >                    K k = p.key;
1452 >                    if (first.hash < 0) {
1453 >                        TreeBin<K,V> t = (TreeBin<K,V>)first;
1454 >                        if (t.putTreeVal(h, k, p.val) == null)
1455 >                            ++added;
1456 >                        insertAtFront = false;
1457 >                    }
1458 >                    else {
1459 >                        int binCount = 0;
1460 >                        insertAtFront = true;
1461 >                        Node<K,V> q; K qk;
1462 >                        for (q = first; q != null; q = q.next) {
1463 >                            if (q.hash == h &&
1464 >                                ((qk = q.key) == k ||
1465 >                                 (qk != null && k.equals(qk)))) {
1466 >                                insertAtFront = false;
1467 >                                break;
1468 >                            }
1469 >                            ++binCount;
1470 >                        }
1471 >                        if (insertAtFront && binCount >= TREEIFY_THRESHOLD) {
1472 >                            insertAtFront = false;
1473 >                            ++added;
1474 >                            p.next = first;
1475 >                            TreeNode<K,V> hd = null, tl = null;
1476 >                            for (q = p; q != null; q = q.next) {
1477 >                                TreeNode<K,V> t = new TreeNode<K,V>
1478 >                                    (q.hash, q.key, q.val, null, null);
1479 >                                if ((t.prev = tl) == null)
1480 >                                    hd = t;
1481 >                                else
1482 >                                    tl.next = t;
1483 >                                tl = t;
1484 >                            }
1485 >                            setTabAt(tab, j, new TreeBin<K,V>(hd));
1486 >                        }
1487 >                    }
1488 >                }
1489 >                if (insertAtFront) {
1490 >                    ++added;
1491 >                    p.next = first;
1492 >                    setTabAt(tab, j, p);
1493 >                }
1494 >                p = next;
1495 >            }
1496 >            table = tab;
1497 >            sizeCtl = n - (n >>> 2);
1498 >            baseCount = added;
1499          }
1500 +    }
1501  
1502 <        @SuppressWarnings("unchecked")
3046 <            public final V next() {
3047 <            Object v;
3048 <            if ((v = nextVal) == null && (v = advance()) == null)
3049 <                throw new NoSuchElementException();
3050 <            nextVal = null;
3051 <            return (V) v;
3052 <        }
1502 >    // ConcurrentMap methods
1503  
1504 <        public final V nextElement() { return next(); }
1504 >    /**
1505 >     * {@inheritDoc}
1506 >     *
1507 >     * @return the previous value associated with the specified key,
1508 >     *         or {@code null} if there was no mapping for the key
1509 >     * @throws NullPointerException if the specified key or value is null
1510 >     */
1511 >    public V putIfAbsent(K key, V value) {
1512 >        return putVal(key, value, true);
1513      }
1514  
1515 <    static final class EntryIterator<K,V> extends Traverser<K,V,Object>
1516 <        implements Spliterator<Map.Entry<K,V>> {
1517 <        EntryIterator(ConcurrentHashMap<K, V> map) { super(map); }
1518 <        EntryIterator(Traverser<K,V,Object> it, boolean split) {
1519 <            super(it, split);
1520 <        }
1521 <        public EntryIterator<K,V> split() {
1522 <            if (last != null || (next != null && nextVal == null))
1523 <                throw new IllegalStateException();
1524 <            return new EntryIterator<K,V>(this, true);
3067 <        }
1515 >    /**
1516 >     * {@inheritDoc}
1517 >     *
1518 >     * @throws NullPointerException if the specified key is null
1519 >     */
1520 >    public boolean remove(Object key, Object value) {
1521 >        if (key == null)
1522 >            throw new NullPointerException();
1523 >        return value != null && replaceNode(key, null, value) != null;
1524 >    }
1525  
1526 <        @SuppressWarnings("unchecked")
1527 <            public final Map.Entry<K,V> next() {
1528 <            Object v;
1529 <            if ((v = nextVal) == null && (v = advance()) == null)
1530 <                throw new NoSuchElementException();
1531 <            Object k = nextKey;
1532 <            nextVal = null;
1533 <            return new MapEntry<K,V>((K)k, (V)v, map);
1534 <        }
1526 >    /**
1527 >     * {@inheritDoc}
1528 >     *
1529 >     * @throws NullPointerException if any of the arguments are null
1530 >     */
1531 >    public boolean replace(K key, V oldValue, V newValue) {
1532 >        if (key == null || oldValue == null || newValue == null)
1533 >            throw new NullPointerException();
1534 >        return replaceNode(key, newValue, oldValue) != null;
1535      }
1536  
1537      /**
1538 <     * Exported Entry for iterators
1538 >     * {@inheritDoc}
1539 >     *
1540 >     * @return the previous value associated with the specified key,
1541 >     *         or {@code null} if there was no mapping for the key
1542 >     * @throws NullPointerException if the specified key or value is null
1543       */
1544 <    static final class MapEntry<K,V> implements Map.Entry<K, V> {
1545 <        final K key; // non-null
1546 <        V val;       // non-null
1547 <        final ConcurrentHashMap<K, V> map;
1548 <        MapEntry(K key, V val, ConcurrentHashMap<K, V> map) {
3088 <            this.key = key;
3089 <            this.val = val;
3090 <            this.map = map;
3091 <        }
3092 <        public final K getKey()       { return key; }
3093 <        public final V getValue()     { return val; }
3094 <        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
3095 <        public final String toString(){ return key + "=" + val; }
1544 >    public V replace(K key, V value) {
1545 >        if (key == null || value == null)
1546 >            throw new NullPointerException();
1547 >        return replaceNode(key, value, null);
1548 >    }
1549  
1550 <        public final boolean equals(Object o) {
3098 <            Object k, v; Map.Entry<?,?> e;
3099 <            return ((o instanceof Map.Entry) &&
3100 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3101 <                    (v = e.getValue()) != null &&
3102 <                    (k == key || k.equals(key)) &&
3103 <                    (v == val || v.equals(val)));
3104 <        }
1550 >    // Overrides of JDK8+ Map extension method defaults
1551  
1552 <        /**
1553 <         * Sets our entry's value and writes through to the map. The
1554 <         * value to return is somewhat arbitrary here. Since we do not
1555 <         * necessarily track asynchronous changes, the most recent
1556 <         * "previous" value could be different from what we return (or
1557 <         * could even have been removed in which case the put will
1558 <         * re-establish). We do not and cannot guarantee more.
1559 <         */
1560 <        public final V setValue(V value) {
1561 <            if (value == null) throw new NullPointerException();
1562 <            V v = val;
1563 <            val = value;
1564 <            map.put(key, value);
1565 <            return v;
1552 >    /**
1553 >     * Returns the value to which the specified key is mapped, or the
1554 >     * given default value if this map contains no mapping for the
1555 >     * key.
1556 >     *
1557 >     * @param key the key whose associated value is to be returned
1558 >     * @param defaultValue the value to return if this map contains
1559 >     * no mapping for the given key
1560 >     * @return the mapping for the key, if present; else the default value
1561 >     * @throws NullPointerException if the specified key is null
1562 >     */
1563 >    public V getOrDefault(Object key, V defaultValue) {
1564 >        V v;
1565 >        return (v = get(key)) == null ? defaultValue : v;
1566 >    }
1567 >
1568 >    public void forEach(BiConsumer<? super K, ? super V> action) {
1569 >        if (action == null) throw new NullPointerException();
1570 >        Node<K,V>[] t;
1571 >        if ((t = table) != null) {
1572 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1573 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1574 >                action.accept(p.key, p.val);
1575 >            }
1576          }
1577      }
1578  
1579 <    /* ----------------Views -------------- */
1579 >    public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
1580 >        if (function == null) throw new NullPointerException();
1581 >        Node<K,V>[] t;
1582 >        if ((t = table) != null) {
1583 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1584 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1585 >                V oldValue = p.val;
1586 >                for (K key = p.key;;) {
1587 >                    V newValue = function.apply(key, oldValue);
1588 >                    if (newValue == null)
1589 >                        throw new NullPointerException();
1590 >                    if (replaceNode(key, newValue, oldValue) != null ||
1591 >                        (oldValue = get(key)) == null)
1592 >                        break;
1593 >                }
1594 >            }
1595 >        }
1596 >    }
1597  
1598      /**
1599 <     * Base class for views.
1599 >     * Helper method for EntrySetView.removeIf.
1600       */
1601 <    static abstract class CHMView<K, V> {
1602 <        final ConcurrentHashMap<K, V> map;
1603 <        CHMView(ConcurrentHashMap<K, V> map)  { this.map = map; }
1604 <        public final int size()                 { return map.size(); }
1605 <        public final boolean isEmpty()          { return map.isEmpty(); }
1606 <        public final void clear()               { map.clear(); }
1607 <
1608 <        // implementations below rely on concrete classes supplying these
1609 <        abstract public Iterator<?> iterator();
1610 <        abstract public boolean contains(Object o);
1611 <        abstract public boolean remove(Object o);
1612 <
1613 <        private static final String oomeMsg = "Required array size too large";
1601 >    boolean removeEntryIf(Predicate<? super Entry<K,V>> function) {
1602 >        if (function == null) throw new NullPointerException();
1603 >        Node<K,V>[] t;
1604 >        boolean removed = false;
1605 >        if ((t = table) != null) {
1606 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1607 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1608 >                K k = p.key;
1609 >                V v = p.val;
1610 >                Map.Entry<K,V> e = new AbstractMap.SimpleImmutableEntry<>(k, v);
1611 >                if (function.test(e) && replaceNode(k, null, v) != null)
1612 >                    removed = true;
1613 >            }
1614 >        }
1615 >        return removed;
1616 >    }
1617  
1618 <        public final Object[] toArray() {
1619 <            long sz = map.mappingCount();
1620 <            if (sz > (long)(MAX_ARRAY_SIZE))
1621 <                throw new OutOfMemoryError(oomeMsg);
1622 <            int n = (int)sz;
1623 <            Object[] r = new Object[n];
1624 <            int i = 0;
1625 <            Iterator<?> it = iterator();
1626 <            while (it.hasNext()) {
1627 <                if (i == n) {
1628 <                    if (n >= MAX_ARRAY_SIZE)
1629 <                        throw new OutOfMemoryError(oomeMsg);
1630 <                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
1631 <                        n = MAX_ARRAY_SIZE;
3156 <                    else
3157 <                        n += (n >>> 1) + 1;
3158 <                    r = Arrays.copyOf(r, n);
3159 <                }
3160 <                r[i++] = it.next();
1618 >    /**
1619 >     * Helper method for ValuesView.removeIf.
1620 >     */
1621 >    boolean removeValueIf(Predicate<? super V> function) {
1622 >        if (function == null) throw new NullPointerException();
1623 >        Node<K,V>[] t;
1624 >        boolean removed = false;
1625 >        if ((t = table) != null) {
1626 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1627 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1628 >                K k = p.key;
1629 >                V v = p.val;
1630 >                if (function.test(v) && replaceNode(k, null, v) != null)
1631 >                    removed = true;
1632              }
3162            return (i == n) ? r : Arrays.copyOf(r, i);
1633          }
1634 +        return removed;
1635 +    }
1636  
1637 <        @SuppressWarnings("unchecked")
1638 <            public final <T> T[] toArray(T[] a) {
1639 <            long sz = map.mappingCount();
1640 <            if (sz > (long)(MAX_ARRAY_SIZE))
1641 <                throw new OutOfMemoryError(oomeMsg);
1642 <            int m = (int)sz;
1643 <            T[] r = (a.length >= m) ? a :
1644 <                (T[])java.lang.reflect.Array
1645 <                .newInstance(a.getClass().getComponentType(), m);
1646 <            int n = r.length;
1647 <            int i = 0;
1648 <            Iterator<?> it = iterator();
1649 <            while (it.hasNext()) {
1650 <                if (i == n) {
1651 <                    if (n >= MAX_ARRAY_SIZE)
1652 <                        throw new OutOfMemoryError(oomeMsg);
1653 <                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
1654 <                        n = MAX_ARRAY_SIZE;
1655 <                    else
1656 <                        n += (n >>> 1) + 1;
1657 <                    r = Arrays.copyOf(r, n);
1637 >    /**
1638 >     * If the specified key is not already associated with a value,
1639 >     * attempts to compute its value using the given mapping function
1640 >     * and enters it into this map unless {@code null}.  The entire
1641 >     * method invocation is performed atomically.  The supplied
1642 >     * function is invoked exactly once per invocation of this method
1643 >     * if the key is absent, else not at all.  Some attempted update
1644 >     * operations on this map by other threads may be blocked while
1645 >     * computation is in progress, so the computation should be short
1646 >     * and simple.
1647 >     *
1648 >     * <p>The mapping function must not modify this map during computation.
1649 >     *
1650 >     * @param key key with which the specified value is to be associated
1651 >     * @param mappingFunction the function to compute a value
1652 >     * @return the current (existing or computed) value associated with
1653 >     *         the specified key, or null if the computed value is null
1654 >     * @throws NullPointerException if the specified key or mappingFunction
1655 >     *         is null
1656 >     * @throws IllegalStateException if the computation detectably
1657 >     *         attempts a recursive update to this map that would
1658 >     *         otherwise never complete
1659 >     * @throws RuntimeException or Error if the mappingFunction does so,
1660 >     *         in which case the mapping is left unestablished
1661 >     */
1662 >    public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
1663 >        if (key == null || mappingFunction == null)
1664 >            throw new NullPointerException();
1665 >        int h = spread(key.hashCode());
1666 >        V val = null;
1667 >        int binCount = 0;
1668 >        for (Node<K,V>[] tab = table;;) {
1669 >            Node<K,V> f; int n, i, fh; K fk; V fv;
1670 >            if (tab == null || (n = tab.length) == 0)
1671 >                tab = initTable();
1672 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1673 >                Node<K,V> r = new ReservationNode<K,V>();
1674 >                synchronized (r) {
1675 >                    if (casTabAt(tab, i, null, r)) {
1676 >                        binCount = 1;
1677 >                        Node<K,V> node = null;
1678 >                        try {
1679 >                            if ((val = mappingFunction.apply(key)) != null)
1680 >                                node = new Node<K,V>(h, key, val);
1681 >                        } finally {
1682 >                            setTabAt(tab, i, node);
1683 >                        }
1684 >                    }
1685                  }
1686 <                r[i++] = (T)it.next();
1686 >                if (binCount != 0)
1687 >                    break;
1688              }
1689 <            if (a == r && i < n) {
1690 <                r[i] = null; // null-terminate
1691 <                return r;
1689 >            else if ((fh = f.hash) == MOVED)
1690 >                tab = helpTransfer(tab, f);
1691 >            else if (fh == h    // check first node without acquiring lock
1692 >                     && ((fk = f.key) == key || (fk != null && key.equals(fk)))
1693 >                     && (fv = f.val) != null)
1694 >                return fv;
1695 >            else {
1696 >                boolean added = false;
1697 >                synchronized (f) {
1698 >                    if (tabAt(tab, i) == f) {
1699 >                        if (fh >= 0) {
1700 >                            binCount = 1;
1701 >                            for (Node<K,V> e = f;; ++binCount) {
1702 >                                K ek;
1703 >                                if (e.hash == h &&
1704 >                                    ((ek = e.key) == key ||
1705 >                                     (ek != null && key.equals(ek)))) {
1706 >                                    val = e.val;
1707 >                                    break;
1708 >                                }
1709 >                                Node<K,V> pred = e;
1710 >                                if ((e = e.next) == null) {
1711 >                                    if ((val = mappingFunction.apply(key)) != null) {
1712 >                                        if (pred.next != null)
1713 >                                            throw new IllegalStateException("Recursive update");
1714 >                                        added = true;
1715 >                                        pred.next = new Node<K,V>(h, key, val);
1716 >                                    }
1717 >                                    break;
1718 >                                }
1719 >                            }
1720 >                        }
1721 >                        else if (f instanceof TreeBin) {
1722 >                            binCount = 2;
1723 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1724 >                            TreeNode<K,V> r, p;
1725 >                            if ((r = t.root) != null &&
1726 >                                (p = r.findTreeNode(h, key, null)) != null)
1727 >                                val = p.val;
1728 >                            else if ((val = mappingFunction.apply(key)) != null) {
1729 >                                added = true;
1730 >                                t.putTreeVal(h, key, val);
1731 >                            }
1732 >                        }
1733 >                        else if (f instanceof ReservationNode)
1734 >                            throw new IllegalStateException("Recursive update");
1735 >                    }
1736 >                }
1737 >                if (binCount != 0) {
1738 >                    if (binCount >= TREEIFY_THRESHOLD)
1739 >                        treeifyBin(tab, i);
1740 >                    if (!added)
1741 >                        return val;
1742 >                    break;
1743 >                }
1744              }
3193            return (i == n) ? r : Arrays.copyOf(r, i);
3194        }
3195
3196        public final int hashCode() {
3197            int h = 0;
3198            for (Iterator<?> it = iterator(); it.hasNext();)
3199                h += it.next().hashCode();
3200            return h;
1745          }
1746 +        if (val != null)
1747 +            addCount(1L, binCount);
1748 +        return val;
1749 +    }
1750  
1751 <        public final String toString() {
1752 <            StringBuilder sb = new StringBuilder();
1753 <            sb.append('[');
1754 <            Iterator<?> it = iterator();
1755 <            if (it.hasNext()) {
1756 <                for (;;) {
1757 <                    Object e = it.next();
1758 <                    sb.append(e == this ? "(this Collection)" : e);
1759 <                    if (!it.hasNext())
1760 <                        break;
1761 <                    sb.append(',').append(' ');
1751 >    /**
1752 >     * If the value for the specified key is present, attempts to
1753 >     * compute a new mapping given the key and its current mapped
1754 >     * value.  The entire method invocation is performed atomically.
1755 >     * The supplied function is invoked exactly once per invocation of
1756 >     * this method if the key is present, else not at all.  Some
1757 >     * attempted update operations on this map by other threads may be
1758 >     * blocked while computation is in progress, so the computation
1759 >     * should be short and simple.
1760 >     *
1761 >     * <p>The remapping function must not modify this map during computation.
1762 >     *
1763 >     * @param key key with which a value may be associated
1764 >     * @param remappingFunction the function to compute a value
1765 >     * @return the new value associated with the specified key, or null if none
1766 >     * @throws NullPointerException if the specified key or remappingFunction
1767 >     *         is null
1768 >     * @throws IllegalStateException if the computation detectably
1769 >     *         attempts a recursive update to this map that would
1770 >     *         otherwise never complete
1771 >     * @throws RuntimeException or Error if the remappingFunction does so,
1772 >     *         in which case the mapping is unchanged
1773 >     */
1774 >    public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
1775 >        if (key == null || remappingFunction == null)
1776 >            throw new NullPointerException();
1777 >        int h = spread(key.hashCode());
1778 >        V val = null;
1779 >        int delta = 0;
1780 >        int binCount = 0;
1781 >        for (Node<K,V>[] tab = table;;) {
1782 >            Node<K,V> f; int n, i, fh;
1783 >            if (tab == null || (n = tab.length) == 0)
1784 >                tab = initTable();
1785 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null)
1786 >                break;
1787 >            else if ((fh = f.hash) == MOVED)
1788 >                tab = helpTransfer(tab, f);
1789 >            else {
1790 >                synchronized (f) {
1791 >                    if (tabAt(tab, i) == f) {
1792 >                        if (fh >= 0) {
1793 >                            binCount = 1;
1794 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1795 >                                K ek;
1796 >                                if (e.hash == h &&
1797 >                                    ((ek = e.key) == key ||
1798 >                                     (ek != null && key.equals(ek)))) {
1799 >                                    val = remappingFunction.apply(key, e.val);
1800 >                                    if (val != null)
1801 >                                        e.val = val;
1802 >                                    else {
1803 >                                        delta = -1;
1804 >                                        Node<K,V> en = e.next;
1805 >                                        if (pred != null)
1806 >                                            pred.next = en;
1807 >                                        else
1808 >                                            setTabAt(tab, i, en);
1809 >                                    }
1810 >                                    break;
1811 >                                }
1812 >                                pred = e;
1813 >                                if ((e = e.next) == null)
1814 >                                    break;
1815 >                            }
1816 >                        }
1817 >                        else if (f instanceof TreeBin) {
1818 >                            binCount = 2;
1819 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1820 >                            TreeNode<K,V> r, p;
1821 >                            if ((r = t.root) != null &&
1822 >                                (p = r.findTreeNode(h, key, null)) != null) {
1823 >                                val = remappingFunction.apply(key, p.val);
1824 >                                if (val != null)
1825 >                                    p.val = val;
1826 >                                else {
1827 >                                    delta = -1;
1828 >                                    if (t.removeTreeNode(p))
1829 >                                        setTabAt(tab, i, untreeify(t.first));
1830 >                                }
1831 >                            }
1832 >                        }
1833 >                        else if (f instanceof ReservationNode)
1834 >                            throw new IllegalStateException("Recursive update");
1835 >                    }
1836                  }
1837 +                if (binCount != 0)
1838 +                    break;
1839              }
3216            return sb.append(']').toString();
1840          }
1841 +        if (delta != 0)
1842 +            addCount((long)delta, binCount);
1843 +        return val;
1844 +    }
1845  
1846 <        public final boolean containsAll(Collection<?> c) {
1847 <            if (c != this) {
1848 <                for (Iterator<?> it = c.iterator(); it.hasNext();) {
1849 <                    Object e = it.next();
1850 <                    if (e == null || !contains(e))
1851 <                        return false;
1846 >    /**
1847 >     * Attempts to compute a mapping for the specified key and its
1848 >     * current mapped value (or {@code null} if there is no current
1849 >     * mapping). The entire method invocation is performed atomically.
1850 >     * The supplied function is invoked exactly once per invocation of
1851 >     * this method.  Some attempted update operations on this map by
1852 >     * other threads may be blocked while computation is in progress,
1853 >     * so the computation should be short and simple.
1854 >     *
1855 >     * <p>The remapping function must not modify this map during computation.
1856 >     *
1857 >     * @param key key with which the specified value is to be associated
1858 >     * @param remappingFunction the function to compute a value
1859 >     * @return the new value associated with the specified key, or null if none
1860 >     * @throws NullPointerException if the specified key or remappingFunction
1861 >     *         is null
1862 >     * @throws IllegalStateException if the computation detectably
1863 >     *         attempts a recursive update to this map that would
1864 >     *         otherwise never complete
1865 >     * @throws RuntimeException or Error if the remappingFunction does so,
1866 >     *         in which case the mapping is unchanged
1867 >     */
1868 >    public V compute(K key,
1869 >                     BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
1870 >        if (key == null || remappingFunction == null)
1871 >            throw new NullPointerException();
1872 >        int h = spread(key.hashCode());
1873 >        V val = null;
1874 >        int delta = 0;
1875 >        int binCount = 0;
1876 >        for (Node<K,V>[] tab = table;;) {
1877 >            Node<K,V> f; int n, i, fh;
1878 >            if (tab == null || (n = tab.length) == 0)
1879 >                tab = initTable();
1880 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1881 >                Node<K,V> r = new ReservationNode<K,V>();
1882 >                synchronized (r) {
1883 >                    if (casTabAt(tab, i, null, r)) {
1884 >                        binCount = 1;
1885 >                        Node<K,V> node = null;
1886 >                        try {
1887 >                            if ((val = remappingFunction.apply(key, null)) != null) {
1888 >                                delta = 1;
1889 >                                node = new Node<K,V>(h, key, val);
1890 >                            }
1891 >                        } finally {
1892 >                            setTabAt(tab, i, node);
1893 >                        }
1894 >                    }
1895                  }
1896 +                if (binCount != 0)
1897 +                    break;
1898              }
1899 <            return true;
1900 <        }
1901 <
1902 <        public final boolean removeAll(Collection<?> c) {
1903 <            boolean modified = false;
1904 <            for (Iterator<?> it = iterator(); it.hasNext();) {
1905 <                if (c.contains(it.next())) {
1906 <                    it.remove();
1907 <                    modified = true;
1899 >            else if ((fh = f.hash) == MOVED)
1900 >                tab = helpTransfer(tab, f);
1901 >            else {
1902 >                synchronized (f) {
1903 >                    if (tabAt(tab, i) == f) {
1904 >                        if (fh >= 0) {
1905 >                            binCount = 1;
1906 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1907 >                                K ek;
1908 >                                if (e.hash == h &&
1909 >                                    ((ek = e.key) == key ||
1910 >                                     (ek != null && key.equals(ek)))) {
1911 >                                    val = remappingFunction.apply(key, e.val);
1912 >                                    if (val != null)
1913 >                                        e.val = val;
1914 >                                    else {
1915 >                                        delta = -1;
1916 >                                        Node<K,V> en = e.next;
1917 >                                        if (pred != null)
1918 >                                            pred.next = en;
1919 >                                        else
1920 >                                            setTabAt(tab, i, en);
1921 >                                    }
1922 >                                    break;
1923 >                                }
1924 >                                pred = e;
1925 >                                if ((e = e.next) == null) {
1926 >                                    val = remappingFunction.apply(key, null);
1927 >                                    if (val != null) {
1928 >                                        if (pred.next != null)
1929 >                                            throw new IllegalStateException("Recursive update");
1930 >                                        delta = 1;
1931 >                                        pred.next = new Node<K,V>(h, key, val);
1932 >                                    }
1933 >                                    break;
1934 >                                }
1935 >                            }
1936 >                        }
1937 >                        else if (f instanceof TreeBin) {
1938 >                            binCount = 1;
1939 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1940 >                            TreeNode<K,V> r, p;
1941 >                            if ((r = t.root) != null)
1942 >                                p = r.findTreeNode(h, key, null);
1943 >                            else
1944 >                                p = null;
1945 >                            V pv = (p == null) ? null : p.val;
1946 >                            val = remappingFunction.apply(key, pv);
1947 >                            if (val != null) {
1948 >                                if (p != null)
1949 >                                    p.val = val;
1950 >                                else {
1951 >                                    delta = 1;
1952 >                                    t.putTreeVal(h, key, val);
1953 >                                }
1954 >                            }
1955 >                            else if (p != null) {
1956 >                                delta = -1;
1957 >                                if (t.removeTreeNode(p))
1958 >                                    setTabAt(tab, i, untreeify(t.first));
1959 >                            }
1960 >                        }
1961 >                        else if (f instanceof ReservationNode)
1962 >                            throw new IllegalStateException("Recursive update");
1963 >                    }
1964 >                }
1965 >                if (binCount != 0) {
1966 >                    if (binCount >= TREEIFY_THRESHOLD)
1967 >                        treeifyBin(tab, i);
1968 >                    break;
1969                  }
1970              }
3238            return modified;
1971          }
1972 +        if (delta != 0)
1973 +            addCount((long)delta, binCount);
1974 +        return val;
1975 +    }
1976  
1977 <        public final boolean retainAll(Collection<?> c) {
1978 <            boolean modified = false;
1979 <            for (Iterator<?> it = iterator(); it.hasNext();) {
1980 <                if (!c.contains(it.next())) {
1981 <                    it.remove();
1982 <                    modified = true;
1977 >    /**
1978 >     * If the specified key is not already associated with a
1979 >     * (non-null) value, associates it with the given value.
1980 >     * Otherwise, replaces the value with the results of the given
1981 >     * remapping function, or removes if {@code null}. The entire
1982 >     * method invocation is performed atomically.  Some attempted
1983 >     * update operations on this map by other threads may be blocked
1984 >     * while computation is in progress, so the computation should be
1985 >     * short and simple, and must not attempt to update any other
1986 >     * mappings of this Map.
1987 >     *
1988 >     * @param key key with which the specified value is to be associated
1989 >     * @param value the value to use if absent
1990 >     * @param remappingFunction the function to recompute a value if present
1991 >     * @return the new value associated with the specified key, or null if none
1992 >     * @throws NullPointerException if the specified key or the
1993 >     *         remappingFunction is null
1994 >     * @throws RuntimeException or Error if the remappingFunction does so,
1995 >     *         in which case the mapping is unchanged
1996 >     */
1997 >    public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
1998 >        if (key == null || value == null || remappingFunction == null)
1999 >            throw new NullPointerException();
2000 >        int h = spread(key.hashCode());
2001 >        V val = null;
2002 >        int delta = 0;
2003 >        int binCount = 0;
2004 >        for (Node<K,V>[] tab = table;;) {
2005 >            Node<K,V> f; int n, i, fh;
2006 >            if (tab == null || (n = tab.length) == 0)
2007 >                tab = initTable();
2008 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
2009 >                if (casTabAt(tab, i, null, new Node<K,V>(h, key, value))) {
2010 >                    delta = 1;
2011 >                    val = value;
2012 >                    break;
2013 >                }
2014 >            }
2015 >            else if ((fh = f.hash) == MOVED)
2016 >                tab = helpTransfer(tab, f);
2017 >            else {
2018 >                synchronized (f) {
2019 >                    if (tabAt(tab, i) == f) {
2020 >                        if (fh >= 0) {
2021 >                            binCount = 1;
2022 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
2023 >                                K ek;
2024 >                                if (e.hash == h &&
2025 >                                    ((ek = e.key) == key ||
2026 >                                     (ek != null && key.equals(ek)))) {
2027 >                                    val = remappingFunction.apply(e.val, value);
2028 >                                    if (val != null)
2029 >                                        e.val = val;
2030 >                                    else {
2031 >                                        delta = -1;
2032 >                                        Node<K,V> en = e.next;
2033 >                                        if (pred != null)
2034 >                                            pred.next = en;
2035 >                                        else
2036 >                                            setTabAt(tab, i, en);
2037 >                                    }
2038 >                                    break;
2039 >                                }
2040 >                                pred = e;
2041 >                                if ((e = e.next) == null) {
2042 >                                    delta = 1;
2043 >                                    val = value;
2044 >                                    pred.next = new Node<K,V>(h, key, val);
2045 >                                    break;
2046 >                                }
2047 >                            }
2048 >                        }
2049 >                        else if (f instanceof TreeBin) {
2050 >                            binCount = 2;
2051 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
2052 >                            TreeNode<K,V> r = t.root;
2053 >                            TreeNode<K,V> p = (r == null) ? null :
2054 >                                r.findTreeNode(h, key, null);
2055 >                            val = (p == null) ? value :
2056 >                                remappingFunction.apply(p.val, value);
2057 >                            if (val != null) {
2058 >                                if (p != null)
2059 >                                    p.val = val;
2060 >                                else {
2061 >                                    delta = 1;
2062 >                                    t.putTreeVal(h, key, val);
2063 >                                }
2064 >                            }
2065 >                            else if (p != null) {
2066 >                                delta = -1;
2067 >                                if (t.removeTreeNode(p))
2068 >                                    setTabAt(tab, i, untreeify(t.first));
2069 >                            }
2070 >                        }
2071 >                        else if (f instanceof ReservationNode)
2072 >                            throw new IllegalStateException("Recursive update");
2073 >                    }
2074 >                }
2075 >                if (binCount != 0) {
2076 >                    if (binCount >= TREEIFY_THRESHOLD)
2077 >                        treeifyBin(tab, i);
2078 >                    break;
2079                  }
2080              }
3249            return modified;
2081          }
2082 +        if (delta != 0)
2083 +            addCount((long)delta, binCount);
2084 +        return val;
2085 +    }
2086 +
2087 +    // Hashtable legacy methods
2088  
2089 +    /**
2090 +     * Tests if some key maps into the specified value in this table.
2091 +     *
2092 +     * <p>Note that this method is identical in functionality to
2093 +     * {@link #containsValue(Object)}, and exists solely to ensure
2094 +     * full compatibility with class {@link java.util.Hashtable},
2095 +     * which supported this method prior to introduction of the
2096 +     * Java Collections Framework.
2097 +     *
2098 +     * @param  value a value to search for
2099 +     * @return {@code true} if and only if some key maps to the
2100 +     *         {@code value} argument in this table as
2101 +     *         determined by the {@code equals} method;
2102 +     *         {@code false} otherwise
2103 +     * @throws NullPointerException if the specified value is null
2104 +     */
2105 +    public boolean contains(Object value) {
2106 +        return containsValue(value);
2107      }
2108  
2109 <    static final class KeySet<K,V> extends CHMView<K,V> implements Set<K> {
2110 <        KeySet(ConcurrentHashMap<K, V> map)  {
2111 <            super(map);
2112 <        }
2113 <        public final boolean contains(Object o) { return map.containsKey(o); }
2114 <        public final boolean remove(Object o)   { return map.remove(o) != null; }
2115 <        public final Iterator<K> iterator() {
2116 <            return new KeyIterator<K,V>(map);
2117 <        }
2118 <        public final boolean add(K e) {
3264 <            throw new UnsupportedOperationException();
3265 <        }
3266 <        public final boolean addAll(Collection<? extends K> c) {
3267 <            throw new UnsupportedOperationException();
3268 <        }
3269 <        public boolean equals(Object o) {
3270 <            Set<?> c;
3271 <            return ((o instanceof Set) &&
3272 <                    ((c = (Set<?>)o) == this ||
3273 <                     (containsAll(c) && c.containsAll(this))));
3274 <        }
2109 >    /**
2110 >     * Returns an enumeration of the keys in this table.
2111 >     *
2112 >     * @return an enumeration of the keys in this table
2113 >     * @see #keySet()
2114 >     */
2115 >    public Enumeration<K> keys() {
2116 >        Node<K,V>[] t;
2117 >        int f = (t = table) == null ? 0 : t.length;
2118 >        return new KeyIterator<K,V>(t, f, 0, f, this);
2119      }
2120  
2121 +    /**
2122 +     * Returns an enumeration of the values in this table.
2123 +     *
2124 +     * @return an enumeration of the values in this table
2125 +     * @see #values()
2126 +     */
2127 +    public Enumeration<V> elements() {
2128 +        Node<K,V>[] t;
2129 +        int f = (t = table) == null ? 0 : t.length;
2130 +        return new ValueIterator<K,V>(t, f, 0, f, this);
2131 +    }
2132  
2133 <    static final class Values<K,V> extends CHMView<K,V>
2134 <        implements Collection<V> {
2135 <        Values(ConcurrentHashMap<K, V> map)   { super(map); }
2136 <        public final boolean contains(Object o) { return map.containsValue(o); }
2137 <        public final boolean remove(Object o) {
2138 <            if (o != null) {
2139 <                Iterator<V> it = new ValueIterator<K,V>(map);
2140 <                while (it.hasNext()) {
2141 <                    if (o.equals(it.next())) {
2142 <                        it.remove();
2143 <                        return true;
2133 >    // ConcurrentHashMap-only methods
2134 >
2135 >    /**
2136 >     * Returns the number of mappings. This method should be used
2137 >     * instead of {@link #size} because a ConcurrentHashMap may
2138 >     * contain more mappings than can be represented as an int. The
2139 >     * value returned is an estimate; the actual count may differ if
2140 >     * there are concurrent insertions or removals.
2141 >     *
2142 >     * @return the number of mappings
2143 >     * @since 1.8
2144 >     */
2145 >    public long mappingCount() {
2146 >        long n = sumCount();
2147 >        return (n < 0L) ? 0L : n; // ignore transient negative values
2148 >    }
2149 >
2150 >    /**
2151 >     * Creates a new {@link Set} backed by a ConcurrentHashMap
2152 >     * from the given type to {@code Boolean.TRUE}.
2153 >     *
2154 >     * @param <K> the element type of the returned set
2155 >     * @return the new set
2156 >     * @since 1.8
2157 >     */
2158 >    public static <K> KeySetView<K,Boolean> newKeySet() {
2159 >        return new KeySetView<K,Boolean>
2160 >            (new ConcurrentHashMap<K,Boolean>(), Boolean.TRUE);
2161 >    }
2162 >
2163 >    /**
2164 >     * Creates a new {@link Set} backed by a ConcurrentHashMap
2165 >     * from the given type to {@code Boolean.TRUE}.
2166 >     *
2167 >     * @param initialCapacity The implementation performs internal
2168 >     * sizing to accommodate this many elements.
2169 >     * @param <K> the element type of the returned set
2170 >     * @return the new set
2171 >     * @throws IllegalArgumentException if the initial capacity of
2172 >     * elements is negative
2173 >     * @since 1.8
2174 >     */
2175 >    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2176 >        return new KeySetView<K,Boolean>
2177 >            (new ConcurrentHashMap<K,Boolean>(initialCapacity), Boolean.TRUE);
2178 >    }
2179 >
2180 >    /**
2181 >     * Returns a {@link Set} view of the keys in this map, using the
2182 >     * given common mapped value for any additions (i.e., {@link
2183 >     * Collection#add} and {@link Collection#addAll(Collection)}).
2184 >     * This is of course only appropriate if it is acceptable to use
2185 >     * the same value for all additions from this view.
2186 >     *
2187 >     * @param mappedValue the mapped value to use for any additions
2188 >     * @return the set view
2189 >     * @throws NullPointerException if the mappedValue is null
2190 >     */
2191 >    public KeySetView<K,V> keySet(V mappedValue) {
2192 >        if (mappedValue == null)
2193 >            throw new NullPointerException();
2194 >        return new KeySetView<K,V>(this, mappedValue);
2195 >    }
2196 >
2197 >    /* ---------------- Special Nodes -------------- */
2198 >
2199 >    /**
2200 >     * A node inserted at head of bins during transfer operations.
2201 >     */
2202 >    static final class ForwardingNode<K,V> extends Node<K,V> {
2203 >        final Node<K,V>[] nextTable;
2204 >        ForwardingNode(Node<K,V>[] tab) {
2205 >            super(MOVED, null, null);
2206 >            this.nextTable = tab;
2207 >        }
2208 >
2209 >        Node<K,V> find(int h, Object k) {
2210 >            // loop to avoid arbitrarily deep recursion on forwarding nodes
2211 >            outer: for (Node<K,V>[] tab = nextTable;;) {
2212 >                Node<K,V> e; int n;
2213 >                if (k == null || tab == null || (n = tab.length) == 0 ||
2214 >                    (e = tabAt(tab, (n - 1) & h)) == null)
2215 >                    return null;
2216 >                for (;;) {
2217 >                    int eh; K ek;
2218 >                    if ((eh = e.hash) == h &&
2219 >                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
2220 >                        return e;
2221 >                    if (eh < 0) {
2222 >                        if (e instanceof ForwardingNode) {
2223 >                            tab = ((ForwardingNode<K,V>)e).nextTable;
2224 >                            continue outer;
2225 >                        }
2226 >                        else
2227 >                            return e.find(h, k);
2228                      }
2229 +                    if ((e = e.next) == null)
2230 +                        return null;
2231                  }
2232              }
3292            return false;
2233          }
3294        public final Iterator<V> iterator() {
3295            return new ValueIterator<K,V>(map);
3296        }
3297        public final boolean add(V e) {
3298            throw new UnsupportedOperationException();
3299        }
3300        public final boolean addAll(Collection<? extends V> c) {
3301            throw new UnsupportedOperationException();
3302        }
3303
2234      }
2235  
2236 <    static final class EntrySet<K,V> extends CHMView<K,V>
2237 <        implements Set<Map.Entry<K,V>> {
2238 <        EntrySet(ConcurrentHashMap<K, V> map) { super(map); }
2239 <        public final boolean contains(Object o) {
2240 <            Object k, v, r; Map.Entry<?,?> e;
2241 <            return ((o instanceof Map.Entry) &&
3312 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3313 <                    (r = map.get(k)) != null &&
3314 <                    (v = e.getValue()) != null &&
3315 <                    (v == r || v.equals(r)));
3316 <        }
3317 <        public final boolean remove(Object o) {
3318 <            Object k, v; Map.Entry<?,?> e;
3319 <            return ((o instanceof Map.Entry) &&
3320 <                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3321 <                    (v = e.getValue()) != null &&
3322 <                    map.remove(k, v));
3323 <        }
3324 <        public final Iterator<Map.Entry<K,V>> iterator() {
3325 <            return new EntryIterator<K,V>(map);
3326 <        }
3327 <        public final boolean add(Entry<K,V> e) {
3328 <            throw new UnsupportedOperationException();
3329 <        }
3330 <        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
3331 <            throw new UnsupportedOperationException();
2236 >    /**
2237 >     * A place-holder node used in computeIfAbsent and compute.
2238 >     */
2239 >    static final class ReservationNode<K,V> extends Node<K,V> {
2240 >        ReservationNode() {
2241 >            super(RESERVED, null, null);
2242          }
2243 <        public boolean equals(Object o) {
2244 <            Set<?> c;
2245 <            return ((o instanceof Set) &&
3336 <                    ((c = (Set<?>)o) == this ||
3337 <                     (containsAll(c) && c.containsAll(this))));
2243 >
2244 >        Node<K,V> find(int h, Object k) {
2245 >            return null;
2246          }
2247      }
2248  
2249 <    /* ---------------- Serialization Support -------------- */
2249 >    /* ---------------- Table Initialization and Resizing -------------- */
2250  
2251      /**
2252 <     * Stripped-down version of helper class used in previous version,
2253 <     * declared for the sake of serialization compatibility
2252 >     * Returns the stamp bits for resizing a table of size n.
2253 >     * Must be negative when shifted left by RESIZE_STAMP_SHIFT.
2254       */
2255 <    static class Segment<K,V> implements Serializable {
2256 <        private static final long serialVersionUID = 2249069246763182397L;
3349 <        final float loadFactor;
3350 <        Segment(float lf) { this.loadFactor = lf; }
2255 >    static final int resizeStamp(int n) {
2256 >        return Integer.numberOfLeadingZeros(n) | (1 << (RESIZE_STAMP_BITS - 1));
2257      }
2258  
2259      /**
2260 <     * Saves the state of the {@code ConcurrentHashMap} instance to a
3355 <     * stream (i.e., serializes it).
3356 <     * @param s the stream
3357 <     * @serialData
3358 <     * the key (Object) and value (Object)
3359 <     * for each key-value mapping, followed by a null pair.
3360 <     * The key-value mappings are emitted in no particular order.
2260 >     * Initializes table, using the size recorded in sizeCtl.
2261       */
2262 <    @SuppressWarnings("unchecked")
2263 <        private void writeObject(java.io.ObjectOutputStream s)
2264 <        throws java.io.IOException {
2265 <        if (segments == null) { // for serialization compatibility
2266 <            segments = (Segment<K,V>[])
2267 <                new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
2268 <            for (int i = 0; i < segments.length; ++i)
2269 <                segments[i] = new Segment<K,V>(LOAD_FACTOR);
2270 <        }
2271 <        s.defaultWriteObject();
2272 <        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2273 <        Object v;
2274 <        while ((v = it.advance()) != null) {
2275 <            s.writeObject(it.nextKey);
2276 <            s.writeObject(v);
2262 >    private final Node<K,V>[] initTable() {
2263 >        Node<K,V>[] tab; int sc;
2264 >        while ((tab = table) == null || tab.length == 0) {
2265 >            if ((sc = sizeCtl) < 0)
2266 >                Thread.yield(); // lost initialization race; just spin
2267 >            else if (U.compareAndSetInt(this, SIZECTL, sc, -1)) {
2268 >                try {
2269 >                    if ((tab = table) == null || tab.length == 0) {
2270 >                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
2271 >                        @SuppressWarnings("unchecked")
2272 >                        Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
2273 >                        table = tab = nt;
2274 >                        sc = n - (n >>> 2);
2275 >                    }
2276 >                } finally {
2277 >                    sizeCtl = sc;
2278 >                }
2279 >                break;
2280 >            }
2281          }
2282 <        s.writeObject(null);
3379 <        s.writeObject(null);
3380 <        segments = null; // throw away
2282 >        return tab;
2283      }
2284  
2285      /**
2286 <     * Reconstitutes the instance from a stream (that is, deserializes it).
2287 <     * @param s the stream
2286 >     * Adds to count, and if table is too small and not already
2287 >     * resizing, initiates transfer. If already resizing, helps
2288 >     * perform transfer if work is available.  Rechecks occupancy
2289 >     * after a transfer to see if another resize is already needed
2290 >     * because resizings are lagging additions.
2291 >     *
2292 >     * @param x the count to add
2293 >     * @param check if <0, don't check resize, if <= 1 only check if uncontended
2294 >     */
2295 >    private final void addCount(long x, int check) {
2296 >        CounterCell[] cs; long b, s;
2297 >        if ((cs = counterCells) != null ||
2298 >            !U.compareAndSetLong(this, BASECOUNT, b = baseCount, s = b + x)) {
2299 >            CounterCell c; long v; int m;
2300 >            boolean uncontended = true;
2301 >            if (cs == null || (m = cs.length - 1) < 0 ||
2302 >                (c = cs[ThreadLocalRandom.getProbe() & m]) == null ||
2303 >                !(uncontended =
2304 >                  U.compareAndSetLong(c, CELLVALUE, v = c.value, v + x))) {
2305 >                fullAddCount(x, uncontended);
2306 >                return;
2307 >            }
2308 >            if (check <= 1)
2309 >                return;
2310 >            s = sumCount();
2311 >        }
2312 >        if (check >= 0) {
2313 >            Node<K,V>[] tab, nt; int n, sc;
2314 >            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
2315 >                   (n = tab.length) < MAXIMUM_CAPACITY) {
2316 >                int rs = resizeStamp(n) << RESIZE_STAMP_SHIFT;
2317 >                if (sc < 0) {
2318 >                    if (sc == rs + MAX_RESIZERS || sc == rs + 1 ||
2319 >                        (nt = nextTable) == null || transferIndex <= 0)
2320 >                        break;
2321 >                    if (U.compareAndSetInt(this, SIZECTL, sc, sc + 1))
2322 >                        transfer(tab, nt);
2323 >                }
2324 >                else if (U.compareAndSetInt(this, SIZECTL, sc, rs + 2))
2325 >                    transfer(tab, null);
2326 >                s = sumCount();
2327 >            }
2328 >        }
2329 >    }
2330 >
2331 >    /**
2332 >     * Helps transfer if a resize is in progress.
2333       */
2334 <    @SuppressWarnings("unchecked")
2335 <        private void readObject(java.io.ObjectInputStream s)
2336 <        throws java.io.IOException, ClassNotFoundException {
2337 <        s.defaultReadObject();
2338 <        this.segments = null; // unneeded
2339 <        // initialize transient final field
2340 <        UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder());
2334 >    final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
2335 >        Node<K,V>[] nextTab; int sc;
2336 >        if (tab != null && (f instanceof ForwardingNode) &&
2337 >            (nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
2338 >            int rs = resizeStamp(tab.length) << RESIZE_STAMP_SHIFT;
2339 >            while (nextTab == nextTable && table == tab &&
2340 >                   (sc = sizeCtl) < 0) {
2341 >                if (sc == rs + MAX_RESIZERS || sc == rs + 1 ||
2342 >                    transferIndex <= 0)
2343 >                    break;
2344 >                if (U.compareAndSetInt(this, SIZECTL, sc, sc + 1)) {
2345 >                    transfer(tab, nextTab);
2346 >                    break;
2347 >                }
2348 >            }
2349 >            return nextTab;
2350 >        }
2351 >        return table;
2352 >    }
2353  
2354 <        // Create all nodes, then place in table once size is known
2355 <        long size = 0L;
2356 <        Node p = null;
2357 <        for (;;) {
2358 <            K k = (K) s.readObject();
2359 <            V v = (V) s.readObject();
2360 <            if (k != null && v != null) {
2361 <                int h = spread(k.hashCode());
2362 <                p = new Node(h, k, v, p);
2363 <                ++size;
2354 >    /**
2355 >     * Tries to presize table to accommodate the given number of elements.
2356 >     *
2357 >     * @param size number of elements (doesn't need to be perfectly accurate)
2358 >     */
2359 >    private final void tryPresize(int size) {
2360 >        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
2361 >            tableSizeFor(size + (size >>> 1) + 1);
2362 >        int sc;
2363 >        while ((sc = sizeCtl) >= 0) {
2364 >            Node<K,V>[] tab = table; int n;
2365 >            if (tab == null || (n = tab.length) == 0) {
2366 >                n = (sc > c) ? sc : c;
2367 >                if (U.compareAndSetInt(this, SIZECTL, sc, -1)) {
2368 >                    try {
2369 >                        if (table == tab) {
2370 >                            @SuppressWarnings("unchecked")
2371 >                            Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
2372 >                            table = nt;
2373 >                            sc = n - (n >>> 2);
2374 >                        }
2375 >                    } finally {
2376 >                        sizeCtl = sc;
2377 >                    }
2378 >                }
2379              }
2380 <            else
2380 >            else if (c <= sc || n >= MAXIMUM_CAPACITY)
2381                  break;
2382 +            else if (tab == table) {
2383 +                int rs = resizeStamp(n);
2384 +                if (U.compareAndSetInt(this, SIZECTL, sc,
2385 +                                        (rs << RESIZE_STAMP_SHIFT) + 2))
2386 +                    transfer(tab, null);
2387 +            }
2388          }
2389 <        if (p != null) {
2390 <            boolean init = false;
2391 <            int n;
2392 <            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
2393 <                n = MAXIMUM_CAPACITY;
2394 <            else {
2395 <                int sz = (int)size;
2396 <                n = tableSizeFor(sz + (sz >>> 1) + 1);
2389 >    }
2390 >
2391 >    /**
2392 >     * Moves and/or copies the nodes in each bin to new table. See
2393 >     * above for explanation.
2394 >     */
2395 >    private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
2396 >        int n = tab.length, stride;
2397 >        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
2398 >            stride = MIN_TRANSFER_STRIDE; // subdivide range
2399 >        if (nextTab == null) {            // initiating
2400 >            try {
2401 >                @SuppressWarnings("unchecked")
2402 >                Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];
2403 >                nextTab = nt;
2404 >            } catch (Throwable ex) {      // try to cope with OOME
2405 >                sizeCtl = Integer.MAX_VALUE;
2406 >                return;
2407 >            }
2408 >            nextTable = nextTab;
2409 >            transferIndex = n;
2410 >        }
2411 >        int nextn = nextTab.length;
2412 >        ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
2413 >        boolean advance = true;
2414 >        boolean finishing = false; // to ensure sweep before committing nextTab
2415 >        for (int i = 0, bound = 0;;) {
2416 >            Node<K,V> f; int fh;
2417 >            while (advance) {
2418 >                int nextIndex, nextBound;
2419 >                if (--i >= bound || finishing)
2420 >                    advance = false;
2421 >                else if ((nextIndex = transferIndex) <= 0) {
2422 >                    i = -1;
2423 >                    advance = false;
2424 >                }
2425 >                else if (U.compareAndSetInt
2426 >                         (this, TRANSFERINDEX, nextIndex,
2427 >                          nextBound = (nextIndex > stride ?
2428 >                                       nextIndex - stride : 0))) {
2429 >                    bound = nextBound;
2430 >                    i = nextIndex - 1;
2431 >                    advance = false;
2432 >                }
2433 >            }
2434 >            if (i < 0 || i >= n || i + n >= nextn) {
2435 >                int sc;
2436 >                if (finishing) {
2437 >                    nextTable = null;
2438 >                    table = nextTab;
2439 >                    sizeCtl = (n << 1) - (n >>> 1);
2440 >                    return;
2441 >                }
2442 >                if (U.compareAndSetInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {
2443 >                    if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
2444 >                        return;
2445 >                    finishing = advance = true;
2446 >                    i = n; // recheck before commit
2447 >                }
2448              }
2449 <            int sc = sizeCtl;
2450 <            boolean collide = false;
2451 <            if (n > sc &&
2452 <                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2453 <                try {
2454 <                    if (table == null) {
2455 <                        init = true;
2456 <                        Node[] tab = new Node[n];
2457 <                        int mask = n - 1;
2458 <                        while (p != null) {
2459 <                            int j = p.hash & mask;
2460 <                            Node next = p.next;
2461 <                            Node q = p.next = tabAt(tab, j);
2462 <                            setTabAt(tab, j, p);
2463 <                            if (!collide && q != null && q.hash == p.hash)
2464 <                                collide = true;
2465 <                            p = next;
2449 >            else if ((f = tabAt(tab, i)) == null)
2450 >                advance = casTabAt(tab, i, null, fwd);
2451 >            else if ((fh = f.hash) == MOVED)
2452 >                advance = true; // already processed
2453 >            else {
2454 >                synchronized (f) {
2455 >                    if (tabAt(tab, i) == f) {
2456 >                        Node<K,V> ln, hn;
2457 >                        if (fh >= 0) {
2458 >                            int runBit = fh & n;
2459 >                            Node<K,V> lastRun = f;
2460 >                            for (Node<K,V> p = f.next; p != null; p = p.next) {
2461 >                                int b = p.hash & n;
2462 >                                if (b != runBit) {
2463 >                                    runBit = b;
2464 >                                    lastRun = p;
2465 >                                }
2466 >                            }
2467 >                            if (runBit == 0) {
2468 >                                ln = lastRun;
2469 >                                hn = null;
2470 >                            }
2471 >                            else {
2472 >                                hn = lastRun;
2473 >                                ln = null;
2474 >                            }
2475 >                            for (Node<K,V> p = f; p != lastRun; p = p.next) {
2476 >                                int ph = p.hash; K pk = p.key; V pv = p.val;
2477 >                                if ((ph & n) == 0)
2478 >                                    ln = new Node<K,V>(ph, pk, pv, ln);
2479 >                                else
2480 >                                    hn = new Node<K,V>(ph, pk, pv, hn);
2481 >                            }
2482 >                            setTabAt(nextTab, i, ln);
2483 >                            setTabAt(nextTab, i + n, hn);
2484 >                            setTabAt(tab, i, fwd);
2485 >                            advance = true;
2486                          }
2487 <                        table = tab;
2488 <                        counter.add(size);
2489 <                        sc = n - (n >>> 2);
2487 >                        else if (f instanceof TreeBin) {
2488 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
2489 >                            TreeNode<K,V> lo = null, loTail = null;
2490 >                            TreeNode<K,V> hi = null, hiTail = null;
2491 >                            int lc = 0, hc = 0;
2492 >                            for (Node<K,V> e = t.first; e != null; e = e.next) {
2493 >                                int h = e.hash;
2494 >                                TreeNode<K,V> p = new TreeNode<K,V>
2495 >                                    (h, e.key, e.val, null, null);
2496 >                                if ((h & n) == 0) {
2497 >                                    if ((p.prev = loTail) == null)
2498 >                                        lo = p;
2499 >                                    else
2500 >                                        loTail.next = p;
2501 >                                    loTail = p;
2502 >                                    ++lc;
2503 >                                }
2504 >                                else {
2505 >                                    if ((p.prev = hiTail) == null)
2506 >                                        hi = p;
2507 >                                    else
2508 >                                        hiTail.next = p;
2509 >                                    hiTail = p;
2510 >                                    ++hc;
2511 >                                }
2512 >                            }
2513 >                            ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
2514 >                                (hc != 0) ? new TreeBin<K,V>(lo) : t;
2515 >                            hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
2516 >                                (lc != 0) ? new TreeBin<K,V>(hi) : t;
2517 >                            setTabAt(nextTab, i, ln);
2518 >                            setTabAt(nextTab, i + n, hn);
2519 >                            setTabAt(tab, i, fwd);
2520 >                            advance = true;
2521 >                        }
2522 >                        else if (f instanceof ReservationNode)
2523 >                            throw new IllegalStateException("Recursive update");
2524                      }
3440                } finally {
3441                    sizeCtl = sc;
2525                  }
2526 <                if (collide) { // rescan and convert to TreeBins
2527 <                    Node[] tab = table;
2528 <                    for (int i = 0; i < tab.length; ++i) {
2529 <                        int c = 0;
2530 <                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
2531 <                            if (++c > TREE_THRESHOLD &&
2532 <                                (e.key instanceof Comparable)) {
2533 <                                replaceWithTreeBin(tab, i, e.key);
2534 <                                break;
2526 >            }
2527 >        }
2528 >    }
2529 >
2530 >    /* ---------------- Counter support -------------- */
2531 >
2532 >    /**
2533 >     * A padded cell for distributing counts.  Adapted from LongAdder
2534 >     * and Striped64.  See their internal docs for explanation.
2535 >     */
2536 >    @jdk.internal.vm.annotation.Contended static final class CounterCell {
2537 >        volatile long value;
2538 >        CounterCell(long x) { value = x; }
2539 >    }
2540 >
2541 >    final long sumCount() {
2542 >        CounterCell[] cs = counterCells;
2543 >        long sum = baseCount;
2544 >        if (cs != null) {
2545 >            for (CounterCell c : cs)
2546 >                if (c != null)
2547 >                    sum += c.value;
2548 >        }
2549 >        return sum;
2550 >    }
2551 >
2552 >    // See LongAdder version for explanation
2553 >    private final void fullAddCount(long x, boolean wasUncontended) {
2554 >        int h;
2555 >        if ((h = ThreadLocalRandom.getProbe()) == 0) {
2556 >            ThreadLocalRandom.localInit();      // force initialization
2557 >            h = ThreadLocalRandom.getProbe();
2558 >            wasUncontended = true;
2559 >        }
2560 >        boolean collide = false;                // True if last slot nonempty
2561 >        for (;;) {
2562 >            CounterCell[] cs; CounterCell c; int n; long v;
2563 >            if ((cs = counterCells) != null && (n = cs.length) > 0) {
2564 >                if ((c = cs[(n - 1) & h]) == null) {
2565 >                    if (cellsBusy == 0) {            // Try to attach new Cell
2566 >                        CounterCell r = new CounterCell(x); // Optimistic create
2567 >                        if (cellsBusy == 0 &&
2568 >                            U.compareAndSetInt(this, CELLSBUSY, 0, 1)) {
2569 >                            boolean created = false;
2570 >                            try {               // Recheck under lock
2571 >                                CounterCell[] rs; int m, j;
2572 >                                if ((rs = counterCells) != null &&
2573 >                                    (m = rs.length) > 0 &&
2574 >                                    rs[j = (m - 1) & h] == null) {
2575 >                                    rs[j] = r;
2576 >                                    created = true;
2577 >                                }
2578 >                            } finally {
2579 >                                cellsBusy = 0;
2580                              }
2581 +                            if (created)
2582 +                                break;
2583 +                            continue;           // Slot is now non-empty
2584                          }
2585                      }
2586 +                    collide = false;
2587                  }
2588 +                else if (!wasUncontended)       // CAS already known to fail
2589 +                    wasUncontended = true;      // Continue after rehash
2590 +                else if (U.compareAndSetLong(c, CELLVALUE, v = c.value, v + x))
2591 +                    break;
2592 +                else if (counterCells != cs || n >= NCPU)
2593 +                    collide = false;            // At max size or stale
2594 +                else if (!collide)
2595 +                    collide = true;
2596 +                else if (cellsBusy == 0 &&
2597 +                         U.compareAndSetInt(this, CELLSBUSY, 0, 1)) {
2598 +                    try {
2599 +                        if (counterCells == cs) // Expand table unless stale
2600 +                            counterCells = Arrays.copyOf(cs, n << 1);
2601 +                    } finally {
2602 +                        cellsBusy = 0;
2603 +                    }
2604 +                    collide = false;
2605 +                    continue;                   // Retry with expanded table
2606 +                }
2607 +                h = ThreadLocalRandom.advanceProbe(h);
2608              }
2609 <            if (!init) { // Can only happen if unsafely published.
2610 <                while (p != null) {
2611 <                    internalPut(p.key, p.val);
2612 <                    p = p.next;
2609 >            else if (cellsBusy == 0 && counterCells == cs &&
2610 >                     U.compareAndSetInt(this, CELLSBUSY, 0, 1)) {
2611 >                boolean init = false;
2612 >                try {                           // Initialize table
2613 >                    if (counterCells == cs) {
2614 >                        CounterCell[] rs = new CounterCell[2];
2615 >                        rs[h & 1] = new CounterCell(x);
2616 >                        counterCells = rs;
2617 >                        init = true;
2618 >                    }
2619 >                } finally {
2620 >                    cellsBusy = 0;
2621                  }
2622 +                if (init)
2623 +                    break;
2624              }
2625 +            else if (U.compareAndSetLong(this, BASECOUNT, v = baseCount, v + x))
2626 +                break;                          // Fall back on using base
2627          }
2628      }
2629  
2630 +    /* ---------------- Conversion from/to TreeBins -------------- */
2631  
2632 <    // -------------------------------------------------------
2633 <
2634 <    // Sams
2635 <    /** Interface describing a void action of one argument */
2636 <    public interface Action<A> { void apply(A a); }
2637 <    /** Interface describing a void action of two arguments */
2638 <    public interface BiAction<A,B> { void apply(A a, B b); }
2639 <    /** Interface describing a function of one argument */
2640 <    public interface Fun<A,T> { T apply(A a); }
2641 <    /** Interface describing a function of two arguments */
2642 <    public interface BiFun<A,B,T> { T apply(A a, B b); }
2643 <    /** Interface describing a function of no arguments */
2644 <    public interface Generator<T> { T apply(); }
2645 <    /** Interface describing a function mapping its argument to a double */
2646 <    public interface ObjectToDouble<A> { double apply(A a); }
2647 <    /** Interface describing a function mapping its argument to a long */
2648 <    public interface ObjectToLong<A> { long apply(A a); }
2649 <    /** Interface describing a function mapping its argument to an int */
2650 <    public interface ObjectToInt<A> {int apply(A a); }
2651 <    /** Interface describing a function mapping two arguments to a double */
2652 <    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
2653 <    /** Interface describing a function mapping two arguments to a long */
2654 <    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
2655 <    /** Interface describing a function mapping two arguments to an int */
2656 <    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
2657 <    /** Interface describing a function mapping a double to a double */
2658 <    public interface DoubleToDouble { double apply(double a); }
2659 <    /** Interface describing a function mapping a long to a long */
2660 <    public interface LongToLong { long apply(long a); }
3496 <    /** Interface describing a function mapping an int to an int */
3497 <    public interface IntToInt { int apply(int a); }
3498 <    /** Interface describing a function mapping two doubles to a double */
3499 <    public interface DoubleByDoubleToDouble { double apply(double a, double b); }
3500 <    /** Interface describing a function mapping two longs to a long */
3501 <    public interface LongByLongToLong { long apply(long a, long b); }
3502 <    /** Interface describing a function mapping two ints to an int */
3503 <    public interface IntByIntToInt { int apply(int a, int b); }
3504 <
3505 <
3506 <    // -------------------------------------------------------
2632 >    /**
2633 >     * Replaces all linked nodes in bin at given index unless table is
2634 >     * too small, in which case resizes instead.
2635 >     */
2636 >    private final void treeifyBin(Node<K,V>[] tab, int index) {
2637 >        Node<K,V> b; int n;
2638 >        if (tab != null) {
2639 >            if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
2640 >                tryPresize(n << 1);
2641 >            else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
2642 >                synchronized (b) {
2643 >                    if (tabAt(tab, index) == b) {
2644 >                        TreeNode<K,V> hd = null, tl = null;
2645 >                        for (Node<K,V> e = b; e != null; e = e.next) {
2646 >                            TreeNode<K,V> p =
2647 >                                new TreeNode<K,V>(e.hash, e.key, e.val,
2648 >                                                  null, null);
2649 >                            if ((p.prev = tl) == null)
2650 >                                hd = p;
2651 >                            else
2652 >                                tl.next = p;
2653 >                            tl = p;
2654 >                        }
2655 >                        setTabAt(tab, index, new TreeBin<K,V>(hd));
2656 >                    }
2657 >                }
2658 >            }
2659 >        }
2660 >    }
2661  
2662      /**
2663 <     * Returns an extended {@link Parallel} view of this map using the
3510 <     * given executor for bulk parallel operations.
3511 <     *
3512 <     * @param executor the executor
3513 <     * @return a parallel view
2663 >     * Returns a list of non-TreeNodes replacing those in given list.
2664       */
2665 <    public Parallel parallel(ForkJoinPool executor)  {
2666 <        return new Parallel(executor);
2665 >    static <K,V> Node<K,V> untreeify(Node<K,V> b) {
2666 >        Node<K,V> hd = null, tl = null;
2667 >        for (Node<K,V> q = b; q != null; q = q.next) {
2668 >            Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val);
2669 >            if (tl == null)
2670 >                hd = p;
2671 >            else
2672 >                tl.next = p;
2673 >            tl = p;
2674 >        }
2675 >        return hd;
2676      }
2677  
2678 +    /* ---------------- TreeNodes -------------- */
2679 +
2680      /**
2681 <     * An extended view of a ConcurrentHashMap supporting bulk
3521 <     * parallel operations. These operations are designed to be be
3522 <     * safely, and often sensibly, applied even with maps that are
3523 <     * being concurrently updated by other threads; for example, when
3524 <     * computing a snapshot summary of the values in a shared
3525 <     * registry.  There are three kinds of operation, each with four
3526 <     * forms, accepting functions with Keys, Values, Entries, and
3527 <     * (Key, Value) arguments and/or return values. Because the
3528 <     * elements of a ConcurrentHashMap are not ordered in any
3529 <     * particular way, and may be processed in different orders in
3530 <     * different parallel executions, the correctness of supplied
3531 <     * functions should not depend on any ordering, or on any other
3532 <     * objects or values that may transiently change while computation
3533 <     * is in progress; and except for forEach actions, should ideally
3534 <     * be side-effect-free.
3535 <     *
3536 <     * <ul>
3537 <     * <li> forEach: Perform a given action on each element.
3538 <     * A variant form applies a given transformation on each element
3539 <     * before performing the action.</li>
3540 <     *
3541 <     * <li> search: Return the first available non-null result of
3542 <     * applying a given function on each element; skipping further
3543 <     * search when a result is found.</li>
3544 <     *
3545 <     * <li> reduce: Accumulate each element.  The supplied reduction
3546 <     * function cannot rely on ordering (more formally, it should be
3547 <     * both associative and commutative).  There are five variants:
3548 <     *
3549 <     * <ul>
3550 <     *
3551 <     * <li> Plain reductions. (There is not a form of this method for
3552 <     * (key, value) function arguments since there is no corresponding
3553 <     * return type.)</li>
3554 <     *
3555 <     * <li> Mapped reductions that accumulate the results of a given
3556 <     * function applied to each element.</li>
3557 <     *
3558 <     * <li> Reductions to scalar doubles, longs, and ints, using a
3559 <     * given basis value.</li>
3560 <     *
3561 <     * </li>
3562 <     * </ul>
3563 <     * </ul>
3564 <     *
3565 <     * <p>The concurrency properties of the bulk operations follow
3566 <     * from those of ConcurrentHashMap: Any non-null result returned
3567 <     * from {@code get(key)} and related access methods bears a
3568 <     * happens-before relation with the associated insertion or
3569 <     * update.  The result of any bulk operation reflects the
3570 <     * composition of these per-element relations (but is not
3571 <     * necessarily atomic with respect to the map as a whole unless it
3572 <     * is somehow known to be quiescent).  Conversely, because keys
3573 <     * and values in the map are never null, null serves as a reliable
3574 <     * atomic indicator of the current lack of any result.  To
3575 <     * maintain this property, null serves as an implicit basis for
3576 <     * all non-scalar reduction operations. For the double, long, and
3577 <     * int versions, the basis should be one that, when combined with
3578 <     * any other value, returns that other value (more formally, it
3579 <     * should be the identity element for the reduction). Most common
3580 <     * reductions have these properties; for example, computing a sum
3581 <     * with basis 0 or a minimum with basis MAX_VALUE.
3582 <     *
3583 <     * <p>Search and transformation functions provided as arguments
3584 <     * should similarly return null to indicate the lack of any result
3585 <     * (in which case it is not used). In the case of mapped
3586 <     * reductions, this also enables transformations to serve as
3587 <     * filters, returning null (or, in the case of primitive
3588 <     * specializations, the identity basis) if the element should not
3589 <     * be combined. You can create compound transformations and
3590 <     * filterings by composing them yourself under this "null means
3591 <     * there is nothing there now" rule before using them in search or
3592 <     * reduce operations.
3593 <     *
3594 <     * <p>Methods accepting and/or returning Entry arguments maintain
3595 <     * key-value associations. They may be useful for example when
3596 <     * finding the key for the greatest value. Note that "plain" Entry
3597 <     * arguments can be supplied using {@code new
3598 <     * AbstractMap.SimpleEntry(k,v)}.
3599 <     *
3600 <     * <p> Bulk operations may complete abruptly, throwing an
3601 <     * exception encountered in the application of a supplied
3602 <     * function. Bear in mind when handling such exceptions that other
3603 <     * concurrently executing functions could also have thrown
3604 <     * exceptions, or would have done so if the first exception had
3605 <     * not occurred.
3606 <     *
3607 <     * <p>Parallel speedups compared to sequential processing are
3608 <     * common but not guaranteed.  Operations involving brief
3609 <     * functions on small maps may execute more slowly than sequential
3610 <     * loops if the underlying work to parallelize the computation is
3611 <     * more expensive than the computation itself. Similarly,
3612 <     * parallelization may not lead to much actual parallelism if all
3613 <     * processors are busy performing unrelated tasks.
3614 <     *
3615 <     * <p> All arguments to all task methods must be non-null.
3616 <     *
3617 <     * <p><em>jsr166e note: During transition, this class
3618 <     * uses nested functional interfaces with different names but the
3619 <     * same forms as those expected for JDK8.<em>
2681 >     * Nodes for use in TreeBins.
2682       */
2683 <    public class Parallel {
2684 <        final ForkJoinPool fjp;
2683 >    static final class TreeNode<K,V> extends Node<K,V> {
2684 >        TreeNode<K,V> parent;  // red-black tree links
2685 >        TreeNode<K,V> left;
2686 >        TreeNode<K,V> right;
2687 >        TreeNode<K,V> prev;    // needed to unlink next upon deletion
2688 >        boolean red;
2689  
2690 <        /**
2691 <         * Returns an extended view of this map using the given
2692 <         * executor for bulk parallel operations.
2693 <         *
3628 <         * @param executor the executor
3629 <         */
3630 <        public Parallel(ForkJoinPool executor)  {
3631 <            this.fjp = executor;
2690 >        TreeNode(int hash, K key, V val, Node<K,V> next,
2691 >                 TreeNode<K,V> parent) {
2692 >            super(hash, key, val, next);
2693 >            this.parent = parent;
2694          }
2695  
2696 <        /**
2697 <         * Performs the given action for each (key, value).
3636 <         *
3637 <         * @param action the action
3638 <         */
3639 <        public void forEach(BiAction<K,V> action) {
3640 <            fjp.invoke(ForkJoinTasks.forEach
3641 <                       (ConcurrentHashMap.this, action));
2696 >        Node<K,V> find(int h, Object k) {
2697 >            return findTreeNode(h, k, null);
2698          }
2699  
2700          /**
2701 <         * Performs the given action for each non-null transformation
2702 <         * of each (key, value).
3647 <         *
3648 <         * @param transformer a function returning the transformation
3649 <         * for an element, or null of there is no transformation (in
3650 <         * which case the action is not applied).
3651 <         * @param action the action
2701 >         * Returns the TreeNode (or null if not found) for the given key
2702 >         * starting at given root.
2703           */
2704 <        public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
2705 <                                Action<U> action) {
2706 <            fjp.invoke(ForkJoinTasks.forEach
2707 <                       (ConcurrentHashMap.this, transformer, action));
2704 >        final TreeNode<K,V> findTreeNode(int h, Object k, Class<?> kc) {
2705 >            if (k != null) {
2706 >                TreeNode<K,V> p = this;
2707 >                do {
2708 >                    int ph, dir; K pk; TreeNode<K,V> q;
2709 >                    TreeNode<K,V> pl = p.left, pr = p.right;
2710 >                    if ((ph = p.hash) > h)
2711 >                        p = pl;
2712 >                    else if (ph < h)
2713 >                        p = pr;
2714 >                    else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2715 >                        return p;
2716 >                    else if (pl == null)
2717 >                        p = pr;
2718 >                    else if (pr == null)
2719 >                        p = pl;
2720 >                    else if ((kc != null ||
2721 >                              (kc = comparableClassFor(k)) != null) &&
2722 >                             (dir = compareComparables(kc, k, pk)) != 0)
2723 >                        p = (dir < 0) ? pl : pr;
2724 >                    else if ((q = pr.findTreeNode(h, k, kc)) != null)
2725 >                        return q;
2726 >                    else
2727 >                        p = pl;
2728 >                } while (p != null);
2729 >            }
2730 >            return null;
2731          }
2732 +    }
2733  
2734 <        /**
3660 <         * Returns a non-null result from applying the given search
3661 <         * function on each (key, value), or null if none.  Further
3662 <         * element processing is suppressed upon success. However,
3663 <         * this method does not return until other in-progress
3664 <         * parallel invocations of the search function also complete.
3665 <         *
3666 <         * @param searchFunction a function returning a non-null
3667 <         * result on success, else null
3668 <         * @return a non-null result from applying the given search
3669 <         * function on each (key, value), or null if none
3670 <         */
3671 <        public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) {
3672 <            return fjp.invoke(ForkJoinTasks.search
3673 <                              (ConcurrentHashMap.this, searchFunction));
3674 <        }
2734 >    /* ---------------- TreeBins -------------- */
2735  
2736 <        /**
2737 <         * Returns the result of accumulating the given transformation
2738 <         * of all (key, value) pairs using the given reducer to
2739 <         * combine values, or null if none.
2740 <         *
2741 <         * @param transformer a function returning the transformation
2742 <         * for an element, or null of there is no transformation (in
2743 <         * which case it is not combined).
2744 <         * @param reducer a commutative associative combining function
2745 <         * @return the result of accumulating the given transformation
2746 <         * of all (key, value) pairs
2747 <         */
2748 <        public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
2749 <                            BiFun<? super U, ? super U, ? extends U> reducer) {
2750 <            return fjp.invoke(ForkJoinTasks.reduce
2751 <                              (ConcurrentHashMap.this, transformer, reducer));
2736 >    /**
2737 >     * TreeNodes used at the heads of bins. TreeBins do not hold user
2738 >     * keys or values, but instead point to list of TreeNodes and
2739 >     * their root. They also maintain a parasitic read-write lock
2740 >     * forcing writers (who hold bin lock) to wait for readers (who do
2741 >     * not) to complete before tree restructuring operations.
2742 >     */
2743 >    static final class TreeBin<K,V> extends Node<K,V> {
2744 >        TreeNode<K,V> root;
2745 >        volatile TreeNode<K,V> first;
2746 >        volatile Thread waiter;
2747 >        volatile int lockState;
2748 >        // values for lockState
2749 >        static final int WRITER = 1; // set while holding write lock
2750 >        static final int WAITER = 2; // set when waiting for write lock
2751 >        static final int READER = 4; // increment value for setting read lock
2752 >
2753 >        /**
2754 >         * Tie-breaking utility for ordering insertions when equal
2755 >         * hashCodes and non-comparable. We don't require a total
2756 >         * order, just a consistent insertion rule to maintain
2757 >         * equivalence across rebalancings. Tie-breaking further than
2758 >         * necessary simplifies testing a bit.
2759 >         */
2760 >        static int tieBreakOrder(Object a, Object b) {
2761 >            int d;
2762 >            if (a == null || b == null ||
2763 >                (d = a.getClass().getName().
2764 >                 compareTo(b.getClass().getName())) == 0)
2765 >                d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
2766 >                     -1 : 1);
2767 >            return d;
2768          }
2769  
2770          /**
2771 <         * Returns the result of accumulating the given transformation
2772 <         * of all (key, value) pairs using the given reducer to
2773 <         * combine values, and the given basis as an identity value.
2774 <         *
2775 <         * @param transformer a function returning the transformation
2776 <         * for an element
2777 <         * @param basis the identity (initial default value) for the reduction
2778 <         * @param reducer a commutative associative combining function
2779 <         * @return the result of accumulating the given transformation
2780 <         * of all (key, value) pairs
2781 <         */
2782 <        public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
2783 <                                     double basis,
2784 <                                     DoubleByDoubleToDouble reducer) {
2785 <            return fjp.invoke(ForkJoinTasks.reduceToDouble
2786 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
2771 >         * Creates bin with initial set of nodes headed by b.
2772 >         */
2773 >        TreeBin(TreeNode<K,V> b) {
2774 >            super(TREEBIN, null, null);
2775 >            this.first = b;
2776 >            TreeNode<K,V> r = null;
2777 >            for (TreeNode<K,V> x = b, next; x != null; x = next) {
2778 >                next = (TreeNode<K,V>)x.next;
2779 >                x.left = x.right = null;
2780 >                if (r == null) {
2781 >                    x.parent = null;
2782 >                    x.red = false;
2783 >                    r = x;
2784 >                }
2785 >                else {
2786 >                    K k = x.key;
2787 >                    int h = x.hash;
2788 >                    Class<?> kc = null;
2789 >                    for (TreeNode<K,V> p = r;;) {
2790 >                        int dir, ph;
2791 >                        K pk = p.key;
2792 >                        if ((ph = p.hash) > h)
2793 >                            dir = -1;
2794 >                        else if (ph < h)
2795 >                            dir = 1;
2796 >                        else if ((kc == null &&
2797 >                                  (kc = comparableClassFor(k)) == null) ||
2798 >                                 (dir = compareComparables(kc, k, pk)) == 0)
2799 >                            dir = tieBreakOrder(k, pk);
2800 >                        TreeNode<K,V> xp = p;
2801 >                        if ((p = (dir <= 0) ? p.left : p.right) == null) {
2802 >                            x.parent = xp;
2803 >                            if (dir <= 0)
2804 >                                xp.left = x;
2805 >                            else
2806 >                                xp.right = x;
2807 >                            r = balanceInsertion(r, x);
2808 >                            break;
2809 >                        }
2810 >                    }
2811 >                }
2812 >            }
2813 >            this.root = r;
2814 >            assert checkInvariants(root);
2815          }
2816  
2817          /**
2818 <         * Returns the result of accumulating the given transformation
3715 <         * of all (key, value) pairs using the given reducer to
3716 <         * combine values, and the given basis as an identity value.
3717 <         *
3718 <         * @param transformer a function returning the transformation
3719 <         * for an element
3720 <         * @param basis the identity (initial default value) for the reduction
3721 <         * @param reducer a commutative associative combining function
3722 <         * @return the result of accumulating the given transformation
3723 <         * of all (key, value) pairs using the given reducer to
3724 <         * combine values, and the given basis as an identity value.
2818 >         * Acquires write lock for tree restructuring.
2819           */
2820 <        public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
2821 <                                 long basis,
2822 <                                 LongByLongToLong reducer) {
3729 <            return fjp.invoke(ForkJoinTasks.reduceToLong
3730 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
2820 >        private final void lockRoot() {
2821 >            if (!U.compareAndSetInt(this, LOCKSTATE, 0, WRITER))
2822 >                contendedLock(); // offload to separate method
2823          }
2824  
2825          /**
2826 <         * Returns the result of accumulating the given transformation
3735 <         * of all (key, value) pairs using the given reducer to
3736 <         * combine values, and the given basis as an identity value.
3737 <         *
3738 <         * @param transformer a function returning the transformation
3739 <         * for an element
3740 <         * @param basis the identity (initial default value) for the reduction
3741 <         * @param reducer a commutative associative combining function
3742 <         * @return the result of accumulating the given transformation
3743 <         * of all (key, value) pairs
2826 >         * Releases write lock for tree restructuring.
2827           */
2828 <        public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
2829 <                               int basis,
3747 <                               IntByIntToInt reducer) {
3748 <            return fjp.invoke(ForkJoinTasks.reduceToInt
3749 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
2828 >        private final void unlockRoot() {
2829 >            lockState = 0;
2830          }
2831  
2832          /**
2833 <         * Performs the given action for each key
3754 <         *
3755 <         * @param action the action
2833 >         * Possibly blocks awaiting root lock.
2834           */
2835 <        public void forEachKey(Action<K> action) {
2836 <            fjp.invoke(ForkJoinTasks.forEachKey
2837 <                       (ConcurrentHashMap.this, action));
2835 >        private final void contendedLock() {
2836 >            boolean waiting = false;
2837 >            for (int s;;) {
2838 >                if (((s = lockState) & ~WAITER) == 0) {
2839 >                    if (U.compareAndSetInt(this, LOCKSTATE, s, WRITER)) {
2840 >                        if (waiting)
2841 >                            waiter = null;
2842 >                        return;
2843 >                    }
2844 >                }
2845 >                else if ((s & WAITER) == 0) {
2846 >                    if (U.compareAndSetInt(this, LOCKSTATE, s, s | WAITER)) {
2847 >                        waiting = true;
2848 >                        waiter = Thread.currentThread();
2849 >                    }
2850 >                }
2851 >                else if (waiting)
2852 >                    LockSupport.park(this);
2853 >            }
2854          }
2855  
2856          /**
2857 <         * Performs the given action for each non-null transformation
2858 <         * of each key
2859 <         *
3766 <         * @param transformer a function returning the transformation
3767 <         * for an element, or null of there is no transformation (in
3768 <         * which case the action is not applied).
3769 <         * @param action the action
2857 >         * Returns matching node or null if none. Tries to search
2858 >         * using tree comparisons from root, but continues linear
2859 >         * search when lock not available.
2860           */
2861 <        public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
2862 <                                   Action<U> action) {
2863 <            fjp.invoke(ForkJoinTasks.forEachKey
2864 <                       (ConcurrentHashMap.this, transformer, action));
2861 >        final Node<K,V> find(int h, Object k) {
2862 >            if (k != null) {
2863 >                for (Node<K,V> e = first; e != null; ) {
2864 >                    int s; K ek;
2865 >                    if (((s = lockState) & (WAITER|WRITER)) != 0) {
2866 >                        if (e.hash == h &&
2867 >                            ((ek = e.key) == k || (ek != null && k.equals(ek))))
2868 >                            return e;
2869 >                        e = e.next;
2870 >                    }
2871 >                    else if (U.compareAndSetInt(this, LOCKSTATE, s,
2872 >                                                 s + READER)) {
2873 >                        TreeNode<K,V> r, p;
2874 >                        try {
2875 >                            p = ((r = root) == null ? null :
2876 >                                 r.findTreeNode(h, k, null));
2877 >                        } finally {
2878 >                            Thread w;
2879 >                            if (U.getAndAddInt(this, LOCKSTATE, -READER) ==
2880 >                                (READER|WAITER) && (w = waiter) != null)
2881 >                                LockSupport.unpark(w);
2882 >                        }
2883 >                        return p;
2884 >                    }
2885 >                }
2886 >            }
2887 >            return null;
2888          }
2889  
2890          /**
2891 <         * Returns a non-null result from applying the given search
2892 <         * function on each key, or null if none.  Further element
3780 <         * processing is suppressed upon success. However, this method
3781 <         * does not return until other in-progress parallel
3782 <         * invocations of the search function also complete.
3783 <         *
3784 <         * @param searchFunction a function returning a non-null
3785 <         * result on success, else null
3786 <         * @return a non-null result from applying the given search
3787 <         * function on each key, or null if none
2891 >         * Finds or adds a node.
2892 >         * @return null if added
2893           */
2894 <        public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
2895 <            return fjp.invoke(ForkJoinTasks.searchKeys
2896 <                              (ConcurrentHashMap.this, searchFunction));
2894 >        final TreeNode<K,V> putTreeVal(int h, K k, V v) {
2895 >            Class<?> kc = null;
2896 >            boolean searched = false;
2897 >            for (TreeNode<K,V> p = root;;) {
2898 >                int dir, ph; K pk;
2899 >                if (p == null) {
2900 >                    first = root = new TreeNode<K,V>(h, k, v, null, null);
2901 >                    break;
2902 >                }
2903 >                else if ((ph = p.hash) > h)
2904 >                    dir = -1;
2905 >                else if (ph < h)
2906 >                    dir = 1;
2907 >                else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2908 >                    return p;
2909 >                else if ((kc == null &&
2910 >                          (kc = comparableClassFor(k)) == null) ||
2911 >                         (dir = compareComparables(kc, k, pk)) == 0) {
2912 >                    if (!searched) {
2913 >                        TreeNode<K,V> q, ch;
2914 >                        searched = true;
2915 >                        if (((ch = p.left) != null &&
2916 >                             (q = ch.findTreeNode(h, k, kc)) != null) ||
2917 >                            ((ch = p.right) != null &&
2918 >                             (q = ch.findTreeNode(h, k, kc)) != null))
2919 >                            return q;
2920 >                    }
2921 >                    dir = tieBreakOrder(k, pk);
2922 >                }
2923 >
2924 >                TreeNode<K,V> xp = p;
2925 >                if ((p = (dir <= 0) ? p.left : p.right) == null) {
2926 >                    TreeNode<K,V> x, f = first;
2927 >                    first = x = new TreeNode<K,V>(h, k, v, f, xp);
2928 >                    if (f != null)
2929 >                        f.prev = x;
2930 >                    if (dir <= 0)
2931 >                        xp.left = x;
2932 >                    else
2933 >                        xp.right = x;
2934 >                    if (!xp.red)
2935 >                        x.red = true;
2936 >                    else {
2937 >                        lockRoot();
2938 >                        try {
2939 >                            root = balanceInsertion(root, x);
2940 >                        } finally {
2941 >                            unlockRoot();
2942 >                        }
2943 >                    }
2944 >                    break;
2945 >                }
2946 >            }
2947 >            assert checkInvariants(root);
2948 >            return null;
2949          }
2950  
2951          /**
2952 <         * Returns the result of accumulating all keys using the given
2953 <         * reducer to combine values, or null if none.
2952 >         * Removes the given node, that must be present before this
2953 >         * call.  This is messier than typical red-black deletion code
2954 >         * because we cannot swap the contents of an interior node
2955 >         * with a leaf successor that is pinned by "next" pointers
2956 >         * that are accessible independently of lock. So instead we
2957 >         * swap the tree linkages.
2958           *
2959 <         * @param reducer a commutative associative combining function
3799 <         * @return the result of accumulating all keys using the given
3800 <         * reducer to combine values, or null if none
2959 >         * @return true if now too small, so should be untreeified
2960           */
2961 <        public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
2962 <            return fjp.invoke(ForkJoinTasks.reduceKeys
2963 <                              (ConcurrentHashMap.this, reducer));
2964 <        }
2961 >        final boolean removeTreeNode(TreeNode<K,V> p) {
2962 >            TreeNode<K,V> next = (TreeNode<K,V>)p.next;
2963 >            TreeNode<K,V> pred = p.prev;  // unlink traversal pointers
2964 >            TreeNode<K,V> r, rl;
2965 >            if (pred == null)
2966 >                first = next;
2967 >            else
2968 >                pred.next = next;
2969 >            if (next != null)
2970 >                next.prev = pred;
2971 >            if (first == null) {
2972 >                root = null;
2973 >                return true;
2974 >            }
2975 >            if ((r = root) == null || r.right == null || // too small
2976 >                (rl = r.left) == null || rl.left == null)
2977 >                return true;
2978 >            lockRoot();
2979 >            try {
2980 >                TreeNode<K,V> replacement;
2981 >                TreeNode<K,V> pl = p.left;
2982 >                TreeNode<K,V> pr = p.right;
2983 >                if (pl != null && pr != null) {
2984 >                    TreeNode<K,V> s = pr, sl;
2985 >                    while ((sl = s.left) != null) // find successor
2986 >                        s = sl;
2987 >                    boolean c = s.red; s.red = p.red; p.red = c; // swap colors
2988 >                    TreeNode<K,V> sr = s.right;
2989 >                    TreeNode<K,V> pp = p.parent;
2990 >                    if (s == pr) { // p was s's direct parent
2991 >                        p.parent = s;
2992 >                        s.right = p;
2993 >                    }
2994 >                    else {
2995 >                        TreeNode<K,V> sp = s.parent;
2996 >                        if ((p.parent = sp) != null) {
2997 >                            if (s == sp.left)
2998 >                                sp.left = p;
2999 >                            else
3000 >                                sp.right = p;
3001 >                        }
3002 >                        if ((s.right = pr) != null)
3003 >                            pr.parent = s;
3004 >                    }
3005 >                    p.left = null;
3006 >                    if ((p.right = sr) != null)
3007 >                        sr.parent = p;
3008 >                    if ((s.left = pl) != null)
3009 >                        pl.parent = s;
3010 >                    if ((s.parent = pp) == null)
3011 >                        r = s;
3012 >                    else if (p == pp.left)
3013 >                        pp.left = s;
3014 >                    else
3015 >                        pp.right = s;
3016 >                    if (sr != null)
3017 >                        replacement = sr;
3018 >                    else
3019 >                        replacement = p;
3020 >                }
3021 >                else if (pl != null)
3022 >                    replacement = pl;
3023 >                else if (pr != null)
3024 >                    replacement = pr;
3025 >                else
3026 >                    replacement = p;
3027 >                if (replacement != p) {
3028 >                    TreeNode<K,V> pp = replacement.parent = p.parent;
3029 >                    if (pp == null)
3030 >                        r = replacement;
3031 >                    else if (p == pp.left)
3032 >                        pp.left = replacement;
3033 >                    else
3034 >                        pp.right = replacement;
3035 >                    p.left = p.right = p.parent = null;
3036 >                }
3037  
3038 <        /**
3808 <         * Returns the result of accumulating the given transformation
3809 <         * of all keys using the given reducer to combine values, or
3810 <         * null if none.
3811 <         *
3812 <         * @param transformer a function returning the transformation
3813 <         * for an element, or null of there is no transformation (in
3814 <         * which case it is not combined).
3815 <         * @param reducer a commutative associative combining function
3816 <         * @return the result of accumulating the given transformation
3817 <         * of all keys
3818 <         */
3819 <        public <U> U reduceKeys(Fun<? super K, ? extends U> transformer,
3820 <                                BiFun<? super U, ? super U, ? extends U> reducer) {
3821 <            return fjp.invoke(ForkJoinTasks.reduceKeys
3822 <                              (ConcurrentHashMap.this, transformer, reducer));
3823 <        }
3038 >                root = (p.red) ? r : balanceDeletion(r, replacement);
3039  
3040 <        /**
3041 <         * Returns the result of accumulating the given transformation
3042 <         * of all keys using the given reducer to combine values, and
3043 <         * the given basis as an identity value.
3044 <         *
3045 <         * @param transformer a function returning the transformation
3046 <         * for an element
3047 <         * @param basis the identity (initial default value) for the reduction
3048 <         * @param reducer a commutative associative combining function
3049 <         * @return  the result of accumulating the given transformation
3050 <         * of all keys
3051 <         */
3052 <        public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
3053 <                                         double basis,
3054 <                                         DoubleByDoubleToDouble reducer) {
3840 <            return fjp.invoke(ForkJoinTasks.reduceKeysToDouble
3841 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3040 >                if (p == replacement) {  // detach pointers
3041 >                    TreeNode<K,V> pp;
3042 >                    if ((pp = p.parent) != null) {
3043 >                        if (p == pp.left)
3044 >                            pp.left = null;
3045 >                        else if (p == pp.right)
3046 >                            pp.right = null;
3047 >                        p.parent = null;
3048 >                    }
3049 >                }
3050 >            } finally {
3051 >                unlockRoot();
3052 >            }
3053 >            assert checkInvariants(root);
3054 >            return false;
3055          }
3056  
3057 <        /**
3058 <         * Returns the result of accumulating the given transformation
3846 <         * of all keys using the given reducer to combine values, and
3847 <         * the given basis as an identity value.
3848 <         *
3849 <         * @param transformer a function returning the transformation
3850 <         * for an element
3851 <         * @param basis the identity (initial default value) for the reduction
3852 <         * @param reducer a commutative associative combining function
3853 <         * @return the result of accumulating the given transformation
3854 <         * of all keys
3855 <         */
3856 <        public long reduceKeysToLong(ObjectToLong<? super K> transformer,
3857 <                                     long basis,
3858 <                                     LongByLongToLong reducer) {
3859 <            return fjp.invoke(ForkJoinTasks.reduceKeysToLong
3860 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3861 <        }
3057 >        /* ------------------------------------------------------------ */
3058 >        // Red-black tree methods, all adapted from CLR
3059  
3060 <        /**
3061 <         * Returns the result of accumulating the given transformation
3062 <         * of all keys using the given reducer to combine values, and
3063 <         * the given basis as an identity value.
3064 <         *
3065 <         * @param transformer a function returning the transformation
3066 <         * for an element
3067 <         * @param basis the identity (initial default value) for the reduction
3068 <         * @param reducer a commutative associative combining function
3069 <         * @return the result of accumulating the given transformation
3070 <         * of all keys
3071 <         */
3072 <        public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3073 <                                   int basis,
3074 <                                   IntByIntToInt reducer) {
3075 <            return fjp.invoke(ForkJoinTasks.reduceKeysToInt
3879 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3060 >        static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
3061 >                                              TreeNode<K,V> p) {
3062 >            TreeNode<K,V> r, pp, rl;
3063 >            if (p != null && (r = p.right) != null) {
3064 >                if ((rl = p.right = r.left) != null)
3065 >                    rl.parent = p;
3066 >                if ((pp = r.parent = p.parent) == null)
3067 >                    (root = r).red = false;
3068 >                else if (pp.left == p)
3069 >                    pp.left = r;
3070 >                else
3071 >                    pp.right = r;
3072 >                r.left = p;
3073 >                p.parent = r;
3074 >            }
3075 >            return root;
3076          }
3077  
3078 <        /**
3079 <         * Performs the given action for each value
3080 <         *
3081 <         * @param action the action
3082 <         */
3083 <        public void forEachValue(Action<V> action) {
3084 <            fjp.invoke(ForkJoinTasks.forEachValue
3085 <                       (ConcurrentHashMap.this, action));
3078 >        static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root,
3079 >                                               TreeNode<K,V> p) {
3080 >            TreeNode<K,V> l, pp, lr;
3081 >            if (p != null && (l = p.left) != null) {
3082 >                if ((lr = p.left = l.right) != null)
3083 >                    lr.parent = p;
3084 >                if ((pp = l.parent = p.parent) == null)
3085 >                    (root = l).red = false;
3086 >                else if (pp.right == p)
3087 >                    pp.right = l;
3088 >                else
3089 >                    pp.left = l;
3090 >                l.right = p;
3091 >                p.parent = l;
3092 >            }
3093 >            return root;
3094          }
3095  
3096 <        /**
3097 <         * Performs the given action for each non-null transformation
3098 <         * of each value
3099 <         *
3100 <         * @param transformer a function returning the transformation
3101 <         * for an element, or null of there is no transformation (in
3102 <         * which case the action is not applied).
3103 <         */
3104 <        public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
3105 <                                     Action<U> action) {
3106 <            fjp.invoke(ForkJoinTasks.forEachValue
3107 <                       (ConcurrentHashMap.this, transformer, action));
3096 >        static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
3097 >                                                    TreeNode<K,V> x) {
3098 >            x.red = true;
3099 >            for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
3100 >                if ((xp = x.parent) == null) {
3101 >                    x.red = false;
3102 >                    return x;
3103 >                }
3104 >                else if (!xp.red || (xpp = xp.parent) == null)
3105 >                    return root;
3106 >                if (xp == (xppl = xpp.left)) {
3107 >                    if ((xppr = xpp.right) != null && xppr.red) {
3108 >                        xppr.red = false;
3109 >                        xp.red = false;
3110 >                        xpp.red = true;
3111 >                        x = xpp;
3112 >                    }
3113 >                    else {
3114 >                        if (x == xp.right) {
3115 >                            root = rotateLeft(root, x = xp);
3116 >                            xpp = (xp = x.parent) == null ? null : xp.parent;
3117 >                        }
3118 >                        if (xp != null) {
3119 >                            xp.red = false;
3120 >                            if (xpp != null) {
3121 >                                xpp.red = true;
3122 >                                root = rotateRight(root, xpp);
3123 >                            }
3124 >                        }
3125 >                    }
3126 >                }
3127 >                else {
3128 >                    if (xppl != null && xppl.red) {
3129 >                        xppl.red = false;
3130 >                        xp.red = false;
3131 >                        xpp.red = true;
3132 >                        x = xpp;
3133 >                    }
3134 >                    else {
3135 >                        if (x == xp.left) {
3136 >                            root = rotateRight(root, x = xp);
3137 >                            xpp = (xp = x.parent) == null ? null : xp.parent;
3138 >                        }
3139 >                        if (xp != null) {
3140 >                            xp.red = false;
3141 >                            if (xpp != null) {
3142 >                                xpp.red = true;
3143 >                                root = rotateLeft(root, xpp);
3144 >                            }
3145 >                        }
3146 >                    }
3147 >                }
3148 >            }
3149          }
3150  
3151 <        /**
3152 <         * Returns a non-null result from applying the given search
3153 <         * function on each value, or null if none.  Further element
3154 <         * processing is suppressed upon success. However, this method
3155 <         * does not return until other in-progress parallel
3156 <         * invocations of the search function also complete.
3157 <         *
3158 <         * @param searchFunction a function returning a non-null
3159 <         * result on success, else null
3160 <         * @return a non-null result from applying the given search
3161 <         * function on each value, or null if none
3162 <         *
3163 <         */
3164 <        public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
3165 <            return fjp.invoke(ForkJoinTasks.searchValues
3166 <                              (ConcurrentHashMap.this, searchFunction));
3151 >        static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
3152 >                                                   TreeNode<K,V> x) {
3153 >            for (TreeNode<K,V> xp, xpl, xpr;;) {
3154 >                if (x == null || x == root)
3155 >                    return root;
3156 >                else if ((xp = x.parent) == null) {
3157 >                    x.red = false;
3158 >                    return x;
3159 >                }
3160 >                else if (x.red) {
3161 >                    x.red = false;
3162 >                    return root;
3163 >                }
3164 >                else if ((xpl = xp.left) == x) {
3165 >                    if ((xpr = xp.right) != null && xpr.red) {
3166 >                        xpr.red = false;
3167 >                        xp.red = true;
3168 >                        root = rotateLeft(root, xp);
3169 >                        xpr = (xp = x.parent) == null ? null : xp.right;
3170 >                    }
3171 >                    if (xpr == null)
3172 >                        x = xp;
3173 >                    else {
3174 >                        TreeNode<K,V> sl = xpr.left, sr = xpr.right;
3175 >                        if ((sr == null || !sr.red) &&
3176 >                            (sl == null || !sl.red)) {
3177 >                            xpr.red = true;
3178 >                            x = xp;
3179 >                        }
3180 >                        else {
3181 >                            if (sr == null || !sr.red) {
3182 >                                if (sl != null)
3183 >                                    sl.red = false;
3184 >                                xpr.red = true;
3185 >                                root = rotateRight(root, xpr);
3186 >                                xpr = (xp = x.parent) == null ?
3187 >                                    null : xp.right;
3188 >                            }
3189 >                            if (xpr != null) {
3190 >                                xpr.red = (xp == null) ? false : xp.red;
3191 >                                if ((sr = xpr.right) != null)
3192 >                                    sr.red = false;
3193 >                            }
3194 >                            if (xp != null) {
3195 >                                xp.red = false;
3196 >                                root = rotateLeft(root, xp);
3197 >                            }
3198 >                            x = root;
3199 >                        }
3200 >                    }
3201 >                }
3202 >                else { // symmetric
3203 >                    if (xpl != null && xpl.red) {
3204 >                        xpl.red = false;
3205 >                        xp.red = true;
3206 >                        root = rotateRight(root, xp);
3207 >                        xpl = (xp = x.parent) == null ? null : xp.left;
3208 >                    }
3209 >                    if (xpl == null)
3210 >                        x = xp;
3211 >                    else {
3212 >                        TreeNode<K,V> sl = xpl.left, sr = xpl.right;
3213 >                        if ((sl == null || !sl.red) &&
3214 >                            (sr == null || !sr.red)) {
3215 >                            xpl.red = true;
3216 >                            x = xp;
3217 >                        }
3218 >                        else {
3219 >                            if (sl == null || !sl.red) {
3220 >                                if (sr != null)
3221 >                                    sr.red = false;
3222 >                                xpl.red = true;
3223 >                                root = rotateLeft(root, xpl);
3224 >                                xpl = (xp = x.parent) == null ?
3225 >                                    null : xp.left;
3226 >                            }
3227 >                            if (xpl != null) {
3228 >                                xpl.red = (xp == null) ? false : xp.red;
3229 >                                if ((sl = xpl.left) != null)
3230 >                                    sl.red = false;
3231 >                            }
3232 >                            if (xp != null) {
3233 >                                xp.red = false;
3234 >                                root = rotateRight(root, xp);
3235 >                            }
3236 >                            x = root;
3237 >                        }
3238 >                    }
3239 >                }
3240 >            }
3241          }
3242  
3243          /**
3244 <         * Returns the result of accumulating all values using the
3926 <         * given reducer to combine values, or null if none.
3927 <         *
3928 <         * @param reducer a commutative associative combining function
3929 <         * @return  the result of accumulating all values
3244 >         * Checks invariants recursively for the tree of Nodes rooted at t.
3245           */
3246 <        public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
3247 <            return fjp.invoke(ForkJoinTasks.reduceValues
3248 <                              (ConcurrentHashMap.this, reducer));
3246 >        static <K,V> boolean checkInvariants(TreeNode<K,V> t) {
3247 >            TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right,
3248 >                tb = t.prev, tn = (TreeNode<K,V>)t.next;
3249 >            if (tb != null && tb.next != t)
3250 >                return false;
3251 >            if (tn != null && tn.prev != t)
3252 >                return false;
3253 >            if (tp != null && t != tp.left && t != tp.right)
3254 >                return false;
3255 >            if (tl != null && (tl.parent != t || tl.hash > t.hash))
3256 >                return false;
3257 >            if (tr != null && (tr.parent != t || tr.hash < t.hash))
3258 >                return false;
3259 >            if (t.red && tl != null && tl.red && tr != null && tr.red)
3260 >                return false;
3261 >            if (tl != null && !checkInvariants(tl))
3262 >                return false;
3263 >            if (tr != null && !checkInvariants(tr))
3264 >                return false;
3265 >            return true;
3266          }
3267  
3268 <        /**
3269 <         * Returns the result of accumulating the given transformation
3270 <         * of all values using the given reducer to combine values, or
3271 <         * null if none.
3272 <         *
3273 <         * @param transformer a function returning the transformation
3274 <         * for an element, or null of there is no transformation (in
3275 <         * which case it is not combined).
3276 <         * @param reducer a commutative associative combining function
3277 <         * @return the result of accumulating the given transformation
3278 <         * of all values
3279 <         */
3280 <        public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
3281 <                                  BiFun<? super U, ? super U, ? extends U> reducer) {
3282 <            return fjp.invoke(ForkJoinTasks.reduceValues
3283 <                              (ConcurrentHashMap.this, transformer, reducer));
3268 >        private static final long LOCKSTATE
3269 >            = U.objectFieldOffset(TreeBin.class, "lockState");
3270 >    }
3271 >
3272 >    /* ----------------Table Traversal -------------- */
3273 >
3274 >    /**
3275 >     * Records the table, its length, and current traversal index for a
3276 >     * traverser that must process a region of a forwarded table before
3277 >     * proceeding with current table.
3278 >     */
3279 >    static final class TableStack<K,V> {
3280 >        int length;
3281 >        int index;
3282 >        Node<K,V>[] tab;
3283 >        TableStack<K,V> next;
3284 >    }
3285 >
3286 >    /**
3287 >     * Encapsulates traversal for methods such as containsValue; also
3288 >     * serves as a base class for other iterators and spliterators.
3289 >     *
3290 >     * Method advance visits once each still-valid node that was
3291 >     * reachable upon iterator construction. It might miss some that
3292 >     * were added to a bin after the bin was visited, which is OK wrt
3293 >     * consistency guarantees. Maintaining this property in the face
3294 >     * of possible ongoing resizes requires a fair amount of
3295 >     * bookkeeping state that is difficult to optimize away amidst
3296 >     * volatile accesses.  Even so, traversal maintains reasonable
3297 >     * throughput.
3298 >     *
3299 >     * Normally, iteration proceeds bin-by-bin traversing lists.
3300 >     * However, if the table has been resized, then all future steps
3301 >     * must traverse both the bin at the current index as well as at
3302 >     * (index + baseSize); and so on for further resizings. To
3303 >     * paranoically cope with potential sharing by users of iterators
3304 >     * across threads, iteration terminates if a bounds checks fails
3305 >     * for a table read.
3306 >     */
3307 >    static class Traverser<K,V> {
3308 >        Node<K,V>[] tab;        // current table; updated if resized
3309 >        Node<K,V> next;         // the next entry to use
3310 >        TableStack<K,V> stack, spare; // to save/restore on ForwardingNodes
3311 >        int index;              // index of bin to use next
3312 >        int baseIndex;          // current index of initial table
3313 >        int baseLimit;          // index bound for initial table
3314 >        final int baseSize;     // initial table size
3315 >
3316 >        Traverser(Node<K,V>[] tab, int size, int index, int limit) {
3317 >            this.tab = tab;
3318 >            this.baseSize = size;
3319 >            this.baseIndex = this.index = index;
3320 >            this.baseLimit = limit;
3321 >            this.next = null;
3322          }
3323  
3324          /**
3325 <         * Returns the result of accumulating the given transformation
3326 <         * of all values using the given reducer to combine values,
3327 <         * and the given basis as an identity value.
3328 <         *
3329 <         * @param transformer a function returning the transformation
3330 <         * for an element
3331 <         * @param basis the identity (initial default value) for the reduction
3332 <         * @param reducer a commutative associative combining function
3333 <         * @return the result of accumulating the given transformation
3334 <         * of all values
3335 <         */
3336 <        public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
3337 <                                           double basis,
3338 <                                           DoubleByDoubleToDouble reducer) {
3339 <            return fjp.invoke(ForkJoinTasks.reduceValuesToDouble
3340 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3325 >         * Advances if possible, returning next valid node, or null if none.
3326 >         */
3327 >        final Node<K,V> advance() {
3328 >            Node<K,V> e;
3329 >            if ((e = next) != null)
3330 >                e = e.next;
3331 >            for (;;) {
3332 >                Node<K,V>[] t; int i, n;  // must use locals in checks
3333 >                if (e != null)
3334 >                    return next = e;
3335 >                if (baseIndex >= baseLimit || (t = tab) == null ||
3336 >                    (n = t.length) <= (i = index) || i < 0)
3337 >                    return next = null;
3338 >                if ((e = tabAt(t, i)) != null && e.hash < 0) {
3339 >                    if (e instanceof ForwardingNode) {
3340 >                        tab = ((ForwardingNode<K,V>)e).nextTable;
3341 >                        e = null;
3342 >                        pushState(t, i, n);
3343 >                        continue;
3344 >                    }
3345 >                    else if (e instanceof TreeBin)
3346 >                        e = ((TreeBin<K,V>)e).first;
3347 >                    else
3348 >                        e = null;
3349 >                }
3350 >                if (stack != null)
3351 >                    recoverState(n);
3352 >                else if ((index = i + baseSize) >= n)
3353 >                    index = ++baseIndex; // visit upper slots if present
3354 >            }
3355          }
3356  
3357          /**
3358 <         * Returns the result of accumulating the given transformation
3975 <         * of all values using the given reducer to combine values,
3976 <         * and the given basis as an identity value.
3977 <         *
3978 <         * @param transformer a function returning the transformation
3979 <         * for an element
3980 <         * @param basis the identity (initial default value) for the reduction
3981 <         * @param reducer a commutative associative combining function
3982 <         * @return the result of accumulating the given transformation
3983 <         * of all values
3358 >         * Saves traversal state upon encountering a forwarding node.
3359           */
3360 <        public long reduceValuesToLong(ObjectToLong<? super V> transformer,
3361 <                                       long basis,
3362 <                                       LongByLongToLong reducer) {
3363 <            return fjp.invoke(ForkJoinTasks.reduceValuesToLong
3364 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3360 >        private void pushState(Node<K,V>[] t, int i, int n) {
3361 >            TableStack<K,V> s = spare;  // reuse if possible
3362 >            if (s != null)
3363 >                spare = s.next;
3364 >            else
3365 >                s = new TableStack<K,V>();
3366 >            s.tab = t;
3367 >            s.length = n;
3368 >            s.index = i;
3369 >            s.next = stack;
3370 >            stack = s;
3371          }
3372  
3373          /**
3374 <         * Returns the result of accumulating the given transformation
3994 <         * of all values using the given reducer to combine values,
3995 <         * and the given basis as an identity value.
3374 >         * Possibly pops traversal state.
3375           *
3376 <         * @param transformer a function returning the transformation
3998 <         * for an element
3999 <         * @param basis the identity (initial default value) for the reduction
4000 <         * @param reducer a commutative associative combining function
4001 <         * @return the result of accumulating the given transformation
4002 <         * of all values
3376 >         * @param n length of current table
3377           */
3378 <        public int reduceValuesToInt(ObjectToInt<? super V> transformer,
3379 <                                     int basis,
3380 <                                     IntByIntToInt reducer) {
3381 <            return fjp.invoke(ForkJoinTasks.reduceValuesToInt
3382 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3378 >        private void recoverState(int n) {
3379 >            TableStack<K,V> s; int len;
3380 >            while ((s = stack) != null && (index += (len = s.length)) >= n) {
3381 >                n = len;
3382 >                index = s.index;
3383 >                tab = s.tab;
3384 >                s.tab = null;
3385 >                TableStack<K,V> next = s.next;
3386 >                s.next = spare; // save for reuse
3387 >                stack = next;
3388 >                spare = s;
3389 >            }
3390 >            if (s == null && (index += baseSize) >= n)
3391 >                index = ++baseIndex;
3392          }
3393 +    }
3394  
3395 <        /**
3396 <         * Perform the given action for each entry
3397 <         *
3398 <         * @param action the action
3399 <         */
3400 <        public void forEachEntry(Action<Map.Entry<K,V>> action) {
3401 <            fjp.invoke(ForkJoinTasks.forEachEntry
3402 <                       (ConcurrentHashMap.this, action));
3395 >    /**
3396 >     * Base of key, value, and entry Iterators. Adds fields to
3397 >     * Traverser to support iterator.remove.
3398 >     */
3399 >    static class BaseIterator<K,V> extends Traverser<K,V> {
3400 >        final ConcurrentHashMap<K,V> map;
3401 >        Node<K,V> lastReturned;
3402 >        BaseIterator(Node<K,V>[] tab, int size, int index, int limit,
3403 >                    ConcurrentHashMap<K,V> map) {
3404 >            super(tab, size, index, limit);
3405 >            this.map = map;
3406 >            advance();
3407          }
3408  
3409 <        /**
3410 <         * Perform the given action for each non-null transformation
3411 <         * of each entry
3412 <         *
3413 <         * @param transformer a function returning the transformation
3414 <         * for an element, or null of there is no transformation (in
3415 <         * which case the action is not applied).
3416 <         * @param action the action
3417 <         */
4030 <        public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
4031 <                                     Action<U> action) {
4032 <            fjp.invoke(ForkJoinTasks.forEachEntry
4033 <                       (ConcurrentHashMap.this, transformer, action));
3409 >        public final boolean hasNext() { return next != null; }
3410 >        public final boolean hasMoreElements() { return next != null; }
3411 >
3412 >        public final void remove() {
3413 >            Node<K,V> p;
3414 >            if ((p = lastReturned) == null)
3415 >                throw new IllegalStateException();
3416 >            lastReturned = null;
3417 >            map.replaceNode(p.key, null, null);
3418          }
3419 +    }
3420  
3421 <        /**
3422 <         * Returns a non-null result from applying the given search
3423 <         * function on each entry, or null if none.  Further element
3424 <         * processing is suppressed upon success. However, this method
3425 <         * does not return until other in-progress parallel
4041 <         * invocations of the search function also complete.
4042 <         *
4043 <         * @param searchFunction a function returning a non-null
4044 <         * result on success, else null
4045 <         * @return a non-null result from applying the given search
4046 <         * function on each entry, or null if none
4047 <         */
4048 <        public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4049 <            return fjp.invoke(ForkJoinTasks.searchEntries
4050 <                              (ConcurrentHashMap.this, searchFunction));
3421 >    static final class KeyIterator<K,V> extends BaseIterator<K,V>
3422 >        implements Iterator<K>, Enumeration<K> {
3423 >        KeyIterator(Node<K,V>[] tab, int size, int index, int limit,
3424 >                    ConcurrentHashMap<K,V> map) {
3425 >            super(tab, size, index, limit, map);
3426          }
3427  
3428 <        /**
3429 <         * Returns the result of accumulating all entries using the
3430 <         * given reducer to combine values, or null if none.
3431 <         *
3432 <         * @param reducer a commutative associative combining function
3433 <         * @return the result of accumulating all entries
3434 <         */
3435 <        public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4061 <            return fjp.invoke(ForkJoinTasks.reduceEntries
4062 <                              (ConcurrentHashMap.this, reducer));
3428 >        public final K next() {
3429 >            Node<K,V> p;
3430 >            if ((p = next) == null)
3431 >                throw new NoSuchElementException();
3432 >            K k = p.key;
3433 >            lastReturned = p;
3434 >            advance();
3435 >            return k;
3436          }
3437  
3438 <        /**
3439 <         * Returns the result of accumulating the given transformation
3440 <         * of all entries using the given reducer to combine values,
3441 <         * or null if none.
3442 <         *
3443 <         * @param transformer a function returning the transformation
3444 <         * for an element, or null of there is no transformation (in
3445 <         * which case it is not combined).
4073 <         * @param reducer a commutative associative combining function
4074 <         * @return the result of accumulating the given transformation
4075 <         * of all entries
4076 <         */
4077 <        public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer,
4078 <                                   BiFun<? super U, ? super U, ? extends U> reducer) {
4079 <            return fjp.invoke(ForkJoinTasks.reduceEntries
4080 <                              (ConcurrentHashMap.this, transformer, reducer));
3438 >        public final K nextElement() { return next(); }
3439 >    }
3440 >
3441 >    static final class ValueIterator<K,V> extends BaseIterator<K,V>
3442 >        implements Iterator<V>, Enumeration<V> {
3443 >        ValueIterator(Node<K,V>[] tab, int size, int index, int limit,
3444 >                      ConcurrentHashMap<K,V> map) {
3445 >            super(tab, size, index, limit, map);
3446          }
3447  
3448 <        /**
3449 <         * Returns the result of accumulating the given transformation
3450 <         * of all entries using the given reducer to combine values,
3451 <         * and the given basis as an identity value.
3452 <         *
3453 <         * @param transformer a function returning the transformation
3454 <         * for an element
3455 <         * @param basis the identity (initial default value) for the reduction
4091 <         * @param reducer a commutative associative combining function
4092 <         * @return the result of accumulating the given transformation
4093 <         * of all entries
4094 <         */
4095 <        public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
4096 <                                            double basis,
4097 <                                            DoubleByDoubleToDouble reducer) {
4098 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToDouble
4099 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3448 >        public final V next() {
3449 >            Node<K,V> p;
3450 >            if ((p = next) == null)
3451 >                throw new NoSuchElementException();
3452 >            V v = p.val;
3453 >            lastReturned = p;
3454 >            advance();
3455 >            return v;
3456          }
3457  
3458 <        /**
3459 <         * Returns the result of accumulating the given transformation
3460 <         * of all entries using the given reducer to combine values,
3461 <         * and the given basis as an identity value.
3462 <         *
3463 <         * @param transformer a function returning the transformation
3464 <         * for an element
3465 <         * @param basis the identity (initial default value) for the reduction
4110 <         * @param reducer a commutative associative combining function
4111 <         * @return  the result of accumulating the given transformation
4112 <         * of all entries
4113 <         */
4114 <        public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4115 <                                        long basis,
4116 <                                        LongByLongToLong reducer) {
4117 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToLong
4118 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3458 >        public final V nextElement() { return next(); }
3459 >    }
3460 >
3461 >    static final class EntryIterator<K,V> extends BaseIterator<K,V>
3462 >        implements Iterator<Map.Entry<K,V>> {
3463 >        EntryIterator(Node<K,V>[] tab, int size, int index, int limit,
3464 >                      ConcurrentHashMap<K,V> map) {
3465 >            super(tab, size, index, limit, map);
3466          }
3467  
3468 <        /**
3469 <         * Returns the result of accumulating the given transformation
3470 <         * of all entries using the given reducer to combine values,
3471 <         * and the given basis as an identity value.
3472 <         *
3473 <         * @param transformer a function returning the transformation
3474 <         * for an element
3475 <         * @param basis the identity (initial default value) for the reduction
3476 <         * @param reducer a commutative associative combining function
4130 <         * @return the result of accumulating the given transformation
4131 <         * of all entries
4132 <         */
4133 <        public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer,
4134 <                                      int basis,
4135 <                                      IntByIntToInt reducer) {
4136 <            return fjp.invoke(ForkJoinTasks.reduceEntriesToInt
4137 <                              (ConcurrentHashMap.this, transformer, basis, reducer));
3468 >        public final Map.Entry<K,V> next() {
3469 >            Node<K,V> p;
3470 >            if ((p = next) == null)
3471 >                throw new NoSuchElementException();
3472 >            K k = p.key;
3473 >            V v = p.val;
3474 >            lastReturned = p;
3475 >            advance();
3476 >            return new MapEntry<K,V>(k, v, map);
3477          }
3478      }
3479  
4141    // ---------------------------------------------------------------------
4142
3480      /**
3481 <     * Predefined tasks for performing bulk parallel operations on
4145 <     * ConcurrentHashMaps. These tasks follow the forms and rules used
4146 <     * in class {@link Parallel}. Each method has the same name, but
4147 <     * returns a task rather than invoking it. These methods may be
4148 <     * useful in custom applications such as submitting a task without
4149 <     * waiting for completion, or combining with other tasks.
3481 >     * Exported Entry for EntryIterator.
3482       */
3483 <    public static class ForkJoinTasks {
3484 <        private ForkJoinTasks() {}
3483 >    static final class MapEntry<K,V> implements Map.Entry<K,V> {
3484 >        final K key; // non-null
3485 >        V val;       // non-null
3486 >        final ConcurrentHashMap<K,V> map;
3487 >        MapEntry(K key, V val, ConcurrentHashMap<K,V> map) {
3488 >            this.key = key;
3489 >            this.val = val;
3490 >            this.map = map;
3491 >        }
3492 >        public K getKey()        { return key; }
3493 >        public V getValue()      { return val; }
3494 >        public int hashCode()    { return key.hashCode() ^ val.hashCode(); }
3495 >        public String toString() {
3496 >            return Helpers.mapEntryToString(key, val);
3497 >        }
3498  
3499 <        /**
3500 <         * Returns a task that when invoked, performs the given
3501 <         * action for each (key, value)
3502 <         *
3503 <         * @param map the map
3504 <         * @param action the action
3505 <         * @return the task
4161 <         */
4162 <        public static <K,V> ForkJoinTask<Void> forEach
4163 <            (ConcurrentHashMap<K,V> map,
4164 <             BiAction<K,V> action) {
4165 <            if (action == null) throw new NullPointerException();
4166 <            return new ForEachMappingTask<K,V>(map, action);
3499 >        public boolean equals(Object o) {
3500 >            Object k, v; Map.Entry<?,?> e;
3501 >            return ((o instanceof Map.Entry) &&
3502 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3503 >                    (v = e.getValue()) != null &&
3504 >                    (k == key || k.equals(key)) &&
3505 >                    (v == val || v.equals(val)));
3506          }
3507  
3508          /**
3509 <         * Returns a task that when invoked, performs the given
3510 <         * action for each non-null transformation of each (key, value)
3511 <         *
3512 <         * @param map the map
3513 <         * @param transformer a function returning the transformation
3514 <         * for an element, or null of there is no transformation (in
4176 <         * which case the action is not applied).
4177 <         * @param action the action
4178 <         * @return the task
3509 >         * Sets our entry's value and writes through to the map. The
3510 >         * value to return is somewhat arbitrary here. Since we do not
3511 >         * necessarily track asynchronous changes, the most recent
3512 >         * "previous" value could be different from what we return (or
3513 >         * could even have been removed, in which case the put will
3514 >         * re-establish). We do not and cannot guarantee more.
3515           */
3516 <        public static <K,V,U> ForkJoinTask<Void> forEach
3517 <            (ConcurrentHashMap<K,V> map,
3518 <             BiFun<? super K, ? super V, ? extends U> transformer,
3519 <             Action<U> action) {
3520 <            if (transformer == null || action == null)
3521 <                throw new NullPointerException();
4186 <            return new ForEachTransformedMappingTask<K,V,U>
4187 <                (map, transformer, action);
3516 >        public V setValue(V value) {
3517 >            if (value == null) throw new NullPointerException();
3518 >            V v = val;
3519 >            val = value;
3520 >            map.put(key, value);
3521 >            return v;
3522          }
3523 +    }
3524  
3525 <        /**
3526 <         * Returns a task that when invoked, returns a non-null
3527 <         * result from applying the given search function on each
3528 <         * (key, value), or null if none.  Further element processing
3529 <         * is suppressed upon success. However, this method does not
3530 <         * return until other in-progress parallel invocations of the
3531 <         * search function also complete.
3532 <         *
3533 <         * @param map the map
3534 <         * @param searchFunction a function returning a non-null
3535 <         * result on success, else null
3536 <         * @return the task
3537 <         */
3538 <        public static <K,V,U> ForkJoinTask<U> search
4204 <            (ConcurrentHashMap<K,V> map,
4205 <             BiFun<? super K, ? super V, ? extends U> searchFunction) {
4206 <            if (searchFunction == null) throw new NullPointerException();
4207 <            return new SearchMappingsTask<K,V,U>
4208 <                (map, searchFunction,
4209 <                 new AtomicReference<U>());
3525 >    static final class KeySpliterator<K,V> extends Traverser<K,V>
3526 >        implements Spliterator<K> {
3527 >        long est;               // size estimate
3528 >        KeySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3529 >                       long est) {
3530 >            super(tab, size, index, limit);
3531 >            this.est = est;
3532 >        }
3533 >
3534 >        public KeySpliterator<K,V> trySplit() {
3535 >            int i, f, h;
3536 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3537 >                new KeySpliterator<K,V>(tab, baseSize, baseLimit = h,
3538 >                                        f, est >>>= 1);
3539          }
3540  
3541 <        /**
3542 <         * Returns a task that when invoked, returns the result of
3543 <         * accumulating the given transformation of all (key, value) pairs
3544 <         * using the given reducer to combine values, or null if none.
4216 <         *
4217 <         * @param map the map
4218 <         * @param transformer a function returning the transformation
4219 <         * for an element, or null of there is no transformation (in
4220 <         * which case it is not combined).
4221 <         * @param reducer a commutative associative combining function
4222 <         * @return the task
4223 <         */
4224 <        public static <K,V,U> ForkJoinTask<U> reduce
4225 <            (ConcurrentHashMap<K,V> map,
4226 <             BiFun<? super K, ? super V, ? extends U> transformer,
4227 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4228 <            if (transformer == null || reducer == null)
4229 <                throw new NullPointerException();
4230 <            return new MapReduceMappingsTask<K,V,U>
4231 <                (map, transformer, reducer);
3541 >        public void forEachRemaining(Consumer<? super K> action) {
3542 >            if (action == null) throw new NullPointerException();
3543 >            for (Node<K,V> p; (p = advance()) != null;)
3544 >                action.accept(p.key);
3545          }
3546  
3547 <        /**
3548 <         * Returns a task that when invoked, returns the result of
3549 <         * accumulating the given transformation of all (key, value) pairs
3550 <         * using the given reducer to combine values, and the given
3551 <         * basis as an identity value.
3552 <         *
3553 <         * @param map the map
4241 <         * @param transformer a function returning the transformation
4242 <         * for an element
4243 <         * @param basis the identity (initial default value) for the reduction
4244 <         * @param reducer a commutative associative combining function
4245 <         * @return the task
4246 <         */
4247 <        public static <K,V> ForkJoinTask<Double> reduceToDouble
4248 <            (ConcurrentHashMap<K,V> map,
4249 <             ObjectByObjectToDouble<? super K, ? super V> transformer,
4250 <             double basis,
4251 <             DoubleByDoubleToDouble reducer) {
4252 <            if (transformer == null || reducer == null)
4253 <                throw new NullPointerException();
4254 <            return new MapReduceMappingsToDoubleTask<K,V>
4255 <                (map, transformer, basis, reducer);
3547 >        public boolean tryAdvance(Consumer<? super K> action) {
3548 >            if (action == null) throw new NullPointerException();
3549 >            Node<K,V> p;
3550 >            if ((p = advance()) == null)
3551 >                return false;
3552 >            action.accept(p.key);
3553 >            return true;
3554          }
3555  
3556 <        /**
3557 <         * Returns a task that when invoked, returns the result of
3558 <         * accumulating the given transformation of all (key, value) pairs
3559 <         * using the given reducer to combine values, and the given
3560 <         * basis as an identity value.
4263 <         *
4264 <         * @param map the map
4265 <         * @param transformer a function returning the transformation
4266 <         * for an element
4267 <         * @param basis the identity (initial default value) for the reduction
4268 <         * @param reducer a commutative associative combining function
4269 <         * @return the task
4270 <         */
4271 <        public static <K,V> ForkJoinTask<Long> reduceToLong
4272 <            (ConcurrentHashMap<K,V> map,
4273 <             ObjectByObjectToLong<? super K, ? super V> transformer,
4274 <             long basis,
4275 <             LongByLongToLong reducer) {
4276 <            if (transformer == null || reducer == null)
4277 <                throw new NullPointerException();
4278 <            return new MapReduceMappingsToLongTask<K,V>
4279 <                (map, transformer, basis, reducer);
3556 >        public long estimateSize() { return est; }
3557 >
3558 >        public int characteristics() {
3559 >            return Spliterator.DISTINCT | Spliterator.CONCURRENT |
3560 >                Spliterator.NONNULL;
3561          }
3562 +    }
3563  
3564 <        /**
3565 <         * Returns a task that when invoked, returns the result of
3566 <         * accumulating the given transformation of all (key, value) pairs
3567 <         * using the given reducer to combine values, and the given
3568 <         * basis as an identity value.
3569 <         *
3570 <         * @param transformer a function returning the transformation
4289 <         * for an element
4290 <         * @param basis the identity (initial default value) for the reduction
4291 <         * @param reducer a commutative associative combining function
4292 <         * @return the task
4293 <         */
4294 <        public static <K,V> ForkJoinTask<Integer> reduceToInt
4295 <            (ConcurrentHashMap<K,V> map,
4296 <             ObjectByObjectToInt<? super K, ? super V> transformer,
4297 <             int basis,
4298 <             IntByIntToInt reducer) {
4299 <            if (transformer == null || reducer == null)
4300 <                throw new NullPointerException();
4301 <            return new MapReduceMappingsToIntTask<K,V>
4302 <                (map, transformer, basis, reducer);
3564 >    static final class ValueSpliterator<K,V> extends Traverser<K,V>
3565 >        implements Spliterator<V> {
3566 >        long est;               // size estimate
3567 >        ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit,
3568 >                         long est) {
3569 >            super(tab, size, index, limit);
3570 >            this.est = est;
3571          }
3572  
3573 <        /**
3574 <         * Returns a task that when invoked, performs the given action
3575 <         * for each key
3576 <         *
3577 <         * @param map the map
3578 <         * @param action the action
3579 <         * @return the task
3580 <         */
4313 <        public static <K,V> ForkJoinTask<Void> forEachKey
4314 <            (ConcurrentHashMap<K,V> map,
4315 <             Action<K> action) {
3573 >        public ValueSpliterator<K,V> trySplit() {
3574 >            int i, f, h;
3575 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3576 >                new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h,
3577 >                                          f, est >>>= 1);
3578 >        }
3579 >
3580 >        public void forEachRemaining(Consumer<? super V> action) {
3581              if (action == null) throw new NullPointerException();
3582 <            return new ForEachKeyTask<K,V>(map, action);
3582 >            for (Node<K,V> p; (p = advance()) != null;)
3583 >                action.accept(p.val);
3584          }
3585  
3586 <        /**
3587 <         * Returns a task that when invoked, performs the given action
3588 <         * for each non-null transformation of each key
3589 <         *
3590 <         * @param map the map
3591 <         * @param transformer a function returning the transformation
3592 <         * for an element, or null of there is no transformation (in
4327 <         * which case the action is not applied).
4328 <         * @param action the action
4329 <         * @return the task
4330 <         */
4331 <        public static <K,V,U> ForkJoinTask<Void> forEachKey
4332 <            (ConcurrentHashMap<K,V> map,
4333 <             Fun<? super K, ? extends U> transformer,
4334 <             Action<U> action) {
4335 <            if (transformer == null || action == null)
4336 <                throw new NullPointerException();
4337 <            return new ForEachTransformedKeyTask<K,V,U>
4338 <                (map, transformer, action);
3586 >        public boolean tryAdvance(Consumer<? super V> action) {
3587 >            if (action == null) throw new NullPointerException();
3588 >            Node<K,V> p;
3589 >            if ((p = advance()) == null)
3590 >                return false;
3591 >            action.accept(p.val);
3592 >            return true;
3593          }
3594  
3595 <        /**
3596 <         * Returns a task that when invoked, returns a non-null result
3597 <         * from applying the given search function on each key, or
3598 <         * null if none.  Further element processing is suppressed
4345 <         * upon success. However, this method does not return until
4346 <         * other in-progress parallel invocations of the search
4347 <         * function also complete.
4348 <         *
4349 <         * @param map the map
4350 <         * @param searchFunction a function returning a non-null
4351 <         * result on success, else null
4352 <         * @return the task
4353 <         */
4354 <        public static <K,V,U> ForkJoinTask<U> searchKeys
4355 <            (ConcurrentHashMap<K,V> map,
4356 <             Fun<? super K, ? extends U> searchFunction) {
4357 <            if (searchFunction == null) throw new NullPointerException();
4358 <            return new SearchKeysTask<K,V,U>
4359 <                (map, searchFunction,
4360 <                 new AtomicReference<U>());
3595 >        public long estimateSize() { return est; }
3596 >
3597 >        public int characteristics() {
3598 >            return Spliterator.CONCURRENT | Spliterator.NONNULL;
3599          }
3600 +    }
3601  
3602 <        /**
3603 <         * Returns a task that when invoked, returns the result of
3604 <         * accumulating all keys using the given reducer to combine
3605 <         * values, or null if none.
3606 <         *
3607 <         * @param map the map
3608 <         * @param reducer a commutative associative combining function
3609 <         * @return the task
3610 <         */
4372 <        public static <K,V> ForkJoinTask<K> reduceKeys
4373 <            (ConcurrentHashMap<K,V> map,
4374 <             BiFun<? super K, ? super K, ? extends K> reducer) {
4375 <            if (reducer == null) throw new NullPointerException();
4376 <            return new ReduceKeysTask<K,V>
4377 <                (map, reducer);
3602 >    static final class EntrySpliterator<K,V> extends Traverser<K,V>
3603 >        implements Spliterator<Map.Entry<K,V>> {
3604 >        final ConcurrentHashMap<K,V> map; // To export MapEntry
3605 >        long est;               // size estimate
3606 >        EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3607 >                         long est, ConcurrentHashMap<K,V> map) {
3608 >            super(tab, size, index, limit);
3609 >            this.map = map;
3610 >            this.est = est;
3611          }
3612 <        /**
3613 <         * Returns a task that when invoked, returns the result of
3614 <         * accumulating the given transformation of all keys using the given
3615 <         * reducer to combine values, or null if none.
3616 <         *
3617 <         * @param map the map
4385 <         * @param transformer a function returning the transformation
4386 <         * for an element, or null of there is no transformation (in
4387 <         * which case it is not combined).
4388 <         * @param reducer a commutative associative combining function
4389 <         * @return the task
4390 <         */
4391 <        public static <K,V,U> ForkJoinTask<U> reduceKeys
4392 <            (ConcurrentHashMap<K,V> map,
4393 <             Fun<? super K, ? extends U> transformer,
4394 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4395 <            if (transformer == null || reducer == null)
4396 <                throw new NullPointerException();
4397 <            return new MapReduceKeysTask<K,V,U>
4398 <                (map, transformer, reducer);
3612 >
3613 >        public EntrySpliterator<K,V> trySplit() {
3614 >            int i, f, h;
3615 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3616 >                new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h,
3617 >                                          f, est >>>= 1, map);
3618          }
3619  
3620 <        /**
3621 <         * Returns a task that when invoked, returns the result of
3622 <         * accumulating the given transformation of all keys using the given
3623 <         * reducer to combine values, and the given basis as an
4405 <         * identity value.
4406 <         *
4407 <         * @param map the map
4408 <         * @param transformer a function returning the transformation
4409 <         * for an element
4410 <         * @param basis the identity (initial default value) for the reduction
4411 <         * @param reducer a commutative associative combining function
4412 <         * @return the task
4413 <         */
4414 <        public static <K,V> ForkJoinTask<Double> reduceKeysToDouble
4415 <            (ConcurrentHashMap<K,V> map,
4416 <             ObjectToDouble<? super K> transformer,
4417 <             double basis,
4418 <             DoubleByDoubleToDouble reducer) {
4419 <            if (transformer == null || reducer == null)
4420 <                throw new NullPointerException();
4421 <            return new MapReduceKeysToDoubleTask<K,V>
4422 <                (map, transformer, basis, reducer);
3620 >        public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) {
3621 >            if (action == null) throw new NullPointerException();
3622 >            for (Node<K,V> p; (p = advance()) != null; )
3623 >                action.accept(new MapEntry<K,V>(p.key, p.val, map));
3624          }
3625  
3626 <        /**
3627 <         * Returns a task that when invoked, returns the result of
3628 <         * accumulating the given transformation of all keys using the given
3629 <         * reducer to combine values, and the given basis as an
3630 <         * identity value.
3631 <         *
3632 <         * @param map the map
4432 <         * @param transformer a function returning the transformation
4433 <         * for an element
4434 <         * @param basis the identity (initial default value) for the reduction
4435 <         * @param reducer a commutative associative combining function
4436 <         * @return the task
4437 <         */
4438 <        public static <K,V> ForkJoinTask<Long> reduceKeysToLong
4439 <            (ConcurrentHashMap<K,V> map,
4440 <             ObjectToLong<? super K> transformer,
4441 <             long basis,
4442 <             LongByLongToLong reducer) {
4443 <            if (transformer == null || reducer == null)
4444 <                throw new NullPointerException();
4445 <            return new MapReduceKeysToLongTask<K,V>
4446 <                (map, transformer, basis, reducer);
3626 >        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
3627 >            if (action == null) throw new NullPointerException();
3628 >            Node<K,V> p;
3629 >            if ((p = advance()) == null)
3630 >                return false;
3631 >            action.accept(new MapEntry<K,V>(p.key, p.val, map));
3632 >            return true;
3633          }
3634  
3635 <        /**
3636 <         * Returns a task that when invoked, returns the result of
3637 <         * accumulating the given transformation of all keys using the given
3638 <         * reducer to combine values, and the given basis as an
3639 <         * identity value.
4454 <         *
4455 <         * @param map the map
4456 <         * @param transformer a function returning the transformation
4457 <         * for an element
4458 <         * @param basis the identity (initial default value) for the reduction
4459 <         * @param reducer a commutative associative combining function
4460 <         * @return the task
4461 <         */
4462 <        public static <K,V> ForkJoinTask<Integer> reduceKeysToInt
4463 <            (ConcurrentHashMap<K,V> map,
4464 <             ObjectToInt<? super K> transformer,
4465 <             int basis,
4466 <             IntByIntToInt reducer) {
4467 <            if (transformer == null || reducer == null)
4468 <                throw new NullPointerException();
4469 <            return new MapReduceKeysToIntTask<K,V>
4470 <                (map, transformer, basis, reducer);
3635 >        public long estimateSize() { return est; }
3636 >
3637 >        public int characteristics() {
3638 >            return Spliterator.DISTINCT | Spliterator.CONCURRENT |
3639 >                Spliterator.NONNULL;
3640          }
3641 +    }
3642 +
3643 +    // Parallel bulk operations
3644 +
3645 +    /**
3646 +     * Computes initial batch value for bulk tasks. The returned value
3647 +     * is approximately exp2 of the number of times (minus one) to
3648 +     * split task by two before executing leaf action. This value is
3649 +     * faster to compute and more convenient to use as a guide to
3650 +     * splitting than is the depth, since it is used while dividing by
3651 +     * two anyway.
3652 +     */
3653 +    final int batchFor(long b) {
3654 +        long n;
3655 +        if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b)
3656 +            return 0;
3657 +        int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4
3658 +        return (b <= 0L || (n /= b) >= sp) ? sp : (int)n;
3659 +    }
3660 +
3661 +    /**
3662 +     * Performs the given action for each (key, value).
3663 +     *
3664 +     * @param parallelismThreshold the (estimated) number of elements
3665 +     * needed for this operation to be executed in parallel
3666 +     * @param action the action
3667 +     * @since 1.8
3668 +     */
3669 +    public void forEach(long parallelismThreshold,
3670 +                        BiConsumer<? super K,? super V> action) {
3671 +        if (action == null) throw new NullPointerException();
3672 +        new ForEachMappingTask<K,V>
3673 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3674 +             action).invoke();
3675 +    }
3676 +
3677 +    /**
3678 +     * Performs the given action for each non-null transformation
3679 +     * of each (key, value).
3680 +     *
3681 +     * @param parallelismThreshold the (estimated) number of elements
3682 +     * needed for this operation to be executed in parallel
3683 +     * @param transformer a function returning the transformation
3684 +     * for an element, or null if there is no transformation (in
3685 +     * which case the action is not applied)
3686 +     * @param action the action
3687 +     * @param <U> the return type of the transformer
3688 +     * @since 1.8
3689 +     */
3690 +    public <U> void forEach(long parallelismThreshold,
3691 +                            BiFunction<? super K, ? super V, ? extends U> transformer,
3692 +                            Consumer<? super U> action) {
3693 +        if (transformer == null || action == null)
3694 +            throw new NullPointerException();
3695 +        new ForEachTransformedMappingTask<K,V,U>
3696 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3697 +             transformer, action).invoke();
3698 +    }
3699 +
3700 +    /**
3701 +     * Returns a non-null result from applying the given search
3702 +     * function on each (key, value), or null if none.  Upon
3703 +     * success, further element processing is suppressed and the
3704 +     * results of any other parallel invocations of the search
3705 +     * function are ignored.
3706 +     *
3707 +     * @param parallelismThreshold the (estimated) number of elements
3708 +     * needed for this operation to be executed in parallel
3709 +     * @param searchFunction a function returning a non-null
3710 +     * result on success, else null
3711 +     * @param <U> the return type of the search function
3712 +     * @return a non-null result from applying the given search
3713 +     * function on each (key, value), or null if none
3714 +     * @since 1.8
3715 +     */
3716 +    public <U> U search(long parallelismThreshold,
3717 +                        BiFunction<? super K, ? super V, ? extends U> searchFunction) {
3718 +        if (searchFunction == null) throw new NullPointerException();
3719 +        return new SearchMappingsTask<K,V,U>
3720 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3721 +             searchFunction, new AtomicReference<U>()).invoke();
3722 +    }
3723 +
3724 +    /**
3725 +     * Returns the result of accumulating the given transformation
3726 +     * of all (key, value) pairs using the given reducer to
3727 +     * combine values, or null if none.
3728 +     *
3729 +     * @param parallelismThreshold the (estimated) number of elements
3730 +     * needed for this operation to be executed in parallel
3731 +     * @param transformer a function returning the transformation
3732 +     * for an element, or null if there is no transformation (in
3733 +     * which case it is not combined)
3734 +     * @param reducer a commutative associative combining function
3735 +     * @param <U> the return type of the transformer
3736 +     * @return the result of accumulating the given transformation
3737 +     * of all (key, value) pairs
3738 +     * @since 1.8
3739 +     */
3740 +    public <U> U reduce(long parallelismThreshold,
3741 +                        BiFunction<? super K, ? super V, ? extends U> transformer,
3742 +                        BiFunction<? super U, ? super U, ? extends U> reducer) {
3743 +        if (transformer == null || reducer == null)
3744 +            throw new NullPointerException();
3745 +        return new MapReduceMappingsTask<K,V,U>
3746 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3747 +             null, transformer, reducer).invoke();
3748 +    }
3749 +
3750 +    /**
3751 +     * Returns the result of accumulating the given transformation
3752 +     * of all (key, value) pairs using the given reducer to
3753 +     * combine values, and the given basis as an identity value.
3754 +     *
3755 +     * @param parallelismThreshold the (estimated) number of elements
3756 +     * needed for this operation to be executed in parallel
3757 +     * @param transformer a function returning the transformation
3758 +     * for an element
3759 +     * @param basis the identity (initial default value) for the reduction
3760 +     * @param reducer a commutative associative combining function
3761 +     * @return the result of accumulating the given transformation
3762 +     * of all (key, value) pairs
3763 +     * @since 1.8
3764 +     */
3765 +    public double reduceToDouble(long parallelismThreshold,
3766 +                                 ToDoubleBiFunction<? super K, ? super V> transformer,
3767 +                                 double basis,
3768 +                                 DoubleBinaryOperator reducer) {
3769 +        if (transformer == null || reducer == null)
3770 +            throw new NullPointerException();
3771 +        return new MapReduceMappingsToDoubleTask<K,V>
3772 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3773 +             null, transformer, basis, reducer).invoke();
3774 +    }
3775 +
3776 +    /**
3777 +     * Returns the result of accumulating the given transformation
3778 +     * of all (key, value) pairs using the given reducer to
3779 +     * combine values, and the given basis as an identity value.
3780 +     *
3781 +     * @param parallelismThreshold the (estimated) number of elements
3782 +     * needed for this operation to be executed in parallel
3783 +     * @param transformer a function returning the transformation
3784 +     * for an element
3785 +     * @param basis the identity (initial default value) for the reduction
3786 +     * @param reducer a commutative associative combining function
3787 +     * @return the result of accumulating the given transformation
3788 +     * of all (key, value) pairs
3789 +     * @since 1.8
3790 +     */
3791 +    public long reduceToLong(long parallelismThreshold,
3792 +                             ToLongBiFunction<? super K, ? super V> transformer,
3793 +                             long basis,
3794 +                             LongBinaryOperator reducer) {
3795 +        if (transformer == null || reducer == null)
3796 +            throw new NullPointerException();
3797 +        return new MapReduceMappingsToLongTask<K,V>
3798 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3799 +             null, transformer, basis, reducer).invoke();
3800 +    }
3801 +
3802 +    /**
3803 +     * Returns the result of accumulating the given transformation
3804 +     * of all (key, value) pairs using the given reducer to
3805 +     * combine values, and the given basis as an identity value.
3806 +     *
3807 +     * @param parallelismThreshold the (estimated) number of elements
3808 +     * needed for this operation to be executed in parallel
3809 +     * @param transformer a function returning the transformation
3810 +     * for an element
3811 +     * @param basis the identity (initial default value) for the reduction
3812 +     * @param reducer a commutative associative combining function
3813 +     * @return the result of accumulating the given transformation
3814 +     * of all (key, value) pairs
3815 +     * @since 1.8
3816 +     */
3817 +    public int reduceToInt(long parallelismThreshold,
3818 +                           ToIntBiFunction<? super K, ? super V> transformer,
3819 +                           int basis,
3820 +                           IntBinaryOperator reducer) {
3821 +        if (transformer == null || reducer == null)
3822 +            throw new NullPointerException();
3823 +        return new MapReduceMappingsToIntTask<K,V>
3824 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3825 +             null, transformer, basis, reducer).invoke();
3826 +    }
3827 +
3828 +    /**
3829 +     * Performs the given action for each key.
3830 +     *
3831 +     * @param parallelismThreshold the (estimated) number of elements
3832 +     * needed for this operation to be executed in parallel
3833 +     * @param action the action
3834 +     * @since 1.8
3835 +     */
3836 +    public void forEachKey(long parallelismThreshold,
3837 +                           Consumer<? super K> action) {
3838 +        if (action == null) throw new NullPointerException();
3839 +        new ForEachKeyTask<K,V>
3840 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3841 +             action).invoke();
3842 +    }
3843 +
3844 +    /**
3845 +     * Performs the given action for each non-null transformation
3846 +     * of each key.
3847 +     *
3848 +     * @param parallelismThreshold the (estimated) number of elements
3849 +     * needed for this operation to be executed in parallel
3850 +     * @param transformer a function returning the transformation
3851 +     * for an element, or null if there is no transformation (in
3852 +     * which case the action is not applied)
3853 +     * @param action the action
3854 +     * @param <U> the return type of the transformer
3855 +     * @since 1.8
3856 +     */
3857 +    public <U> void forEachKey(long parallelismThreshold,
3858 +                               Function<? super K, ? extends U> transformer,
3859 +                               Consumer<? super U> action) {
3860 +        if (transformer == null || action == null)
3861 +            throw new NullPointerException();
3862 +        new ForEachTransformedKeyTask<K,V,U>
3863 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3864 +             transformer, action).invoke();
3865 +    }
3866 +
3867 +    /**
3868 +     * Returns a non-null result from applying the given search
3869 +     * function on each key, or null if none. Upon success,
3870 +     * further element processing is suppressed and the results of
3871 +     * any other parallel invocations of the search function are
3872 +     * ignored.
3873 +     *
3874 +     * @param parallelismThreshold the (estimated) number of elements
3875 +     * needed for this operation to be executed in parallel
3876 +     * @param searchFunction a function returning a non-null
3877 +     * result on success, else null
3878 +     * @param <U> the return type of the search function
3879 +     * @return a non-null result from applying the given search
3880 +     * function on each key, or null if none
3881 +     * @since 1.8
3882 +     */
3883 +    public <U> U searchKeys(long parallelismThreshold,
3884 +                            Function<? super K, ? extends U> searchFunction) {
3885 +        if (searchFunction == null) throw new NullPointerException();
3886 +        return new SearchKeysTask<K,V,U>
3887 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3888 +             searchFunction, new AtomicReference<U>()).invoke();
3889 +    }
3890 +
3891 +    /**
3892 +     * Returns the result of accumulating all keys using the given
3893 +     * reducer to combine values, or null if none.
3894 +     *
3895 +     * @param parallelismThreshold the (estimated) number of elements
3896 +     * needed for this operation to be executed in parallel
3897 +     * @param reducer a commutative associative combining function
3898 +     * @return the result of accumulating all keys using the given
3899 +     * reducer to combine values, or null if none
3900 +     * @since 1.8
3901 +     */
3902 +    public K reduceKeys(long parallelismThreshold,
3903 +                        BiFunction<? super K, ? super K, ? extends K> reducer) {
3904 +        if (reducer == null) throw new NullPointerException();
3905 +        return new ReduceKeysTask<K,V>
3906 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3907 +             null, reducer).invoke();
3908 +    }
3909 +
3910 +    /**
3911 +     * Returns the result of accumulating the given transformation
3912 +     * of all keys using the given reducer to combine values, or
3913 +     * null if none.
3914 +     *
3915 +     * @param parallelismThreshold the (estimated) number of elements
3916 +     * needed for this operation to be executed in parallel
3917 +     * @param transformer a function returning the transformation
3918 +     * for an element, or null if there is no transformation (in
3919 +     * which case it is not combined)
3920 +     * @param reducer a commutative associative combining function
3921 +     * @param <U> the return type of the transformer
3922 +     * @return the result of accumulating the given transformation
3923 +     * of all keys
3924 +     * @since 1.8
3925 +     */
3926 +    public <U> U reduceKeys(long parallelismThreshold,
3927 +                            Function<? super K, ? extends U> transformer,
3928 +         BiFunction<? super U, ? super U, ? extends U> reducer) {
3929 +        if (transformer == null || reducer == null)
3930 +            throw new NullPointerException();
3931 +        return new MapReduceKeysTask<K,V,U>
3932 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3933 +             null, transformer, reducer).invoke();
3934 +    }
3935 +
3936 +    /**
3937 +     * Returns the result of accumulating the given transformation
3938 +     * of all keys using the given reducer to combine values, and
3939 +     * the given basis as an identity value.
3940 +     *
3941 +     * @param parallelismThreshold the (estimated) number of elements
3942 +     * needed for this operation to be executed in parallel
3943 +     * @param transformer a function returning the transformation
3944 +     * for an element
3945 +     * @param basis the identity (initial default value) for the reduction
3946 +     * @param reducer a commutative associative combining function
3947 +     * @return the result of accumulating the given transformation
3948 +     * of all keys
3949 +     * @since 1.8
3950 +     */
3951 +    public double reduceKeysToDouble(long parallelismThreshold,
3952 +                                     ToDoubleFunction<? super K> transformer,
3953 +                                     double basis,
3954 +                                     DoubleBinaryOperator reducer) {
3955 +        if (transformer == null || reducer == null)
3956 +            throw new NullPointerException();
3957 +        return new MapReduceKeysToDoubleTask<K,V>
3958 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3959 +             null, transformer, basis, reducer).invoke();
3960 +    }
3961 +
3962 +    /**
3963 +     * Returns the result of accumulating the given transformation
3964 +     * of all keys using the given reducer to combine values, and
3965 +     * the given basis as an identity value.
3966 +     *
3967 +     * @param parallelismThreshold the (estimated) number of elements
3968 +     * needed for this operation to be executed in parallel
3969 +     * @param transformer a function returning the transformation
3970 +     * for an element
3971 +     * @param basis the identity (initial default value) for the reduction
3972 +     * @param reducer a commutative associative combining function
3973 +     * @return the result of accumulating the given transformation
3974 +     * of all keys
3975 +     * @since 1.8
3976 +     */
3977 +    public long reduceKeysToLong(long parallelismThreshold,
3978 +                                 ToLongFunction<? super K> transformer,
3979 +                                 long basis,
3980 +                                 LongBinaryOperator reducer) {
3981 +        if (transformer == null || reducer == null)
3982 +            throw new NullPointerException();
3983 +        return new MapReduceKeysToLongTask<K,V>
3984 +            (null, batchFor(parallelismThreshold), 0, 0, table,
3985 +             null, transformer, basis, reducer).invoke();
3986 +    }
3987 +
3988 +    /**
3989 +     * Returns the result of accumulating the given transformation
3990 +     * of all keys using the given reducer to combine values, and
3991 +     * the given basis as an identity value.
3992 +     *
3993 +     * @param parallelismThreshold the (estimated) number of elements
3994 +     * needed for this operation to be executed in parallel
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 keys
4001 +     * @since 1.8
4002 +     */
4003 +    public int reduceKeysToInt(long parallelismThreshold,
4004 +                               ToIntFunction<? super K> transformer,
4005 +                               int basis,
4006 +                               IntBinaryOperator reducer) {
4007 +        if (transformer == null || reducer == null)
4008 +            throw new NullPointerException();
4009 +        return new MapReduceKeysToIntTask<K,V>
4010 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4011 +             null, transformer, basis, reducer).invoke();
4012 +    }
4013 +
4014 +    /**
4015 +     * Performs the given action for each value.
4016 +     *
4017 +     * @param parallelismThreshold the (estimated) number of elements
4018 +     * needed for this operation to be executed in parallel
4019 +     * @param action the action
4020 +     * @since 1.8
4021 +     */
4022 +    public void forEachValue(long parallelismThreshold,
4023 +                             Consumer<? super V> action) {
4024 +        if (action == null)
4025 +            throw new NullPointerException();
4026 +        new ForEachValueTask<K,V>
4027 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4028 +             action).invoke();
4029 +    }
4030 +
4031 +    /**
4032 +     * Performs the given action for each non-null transformation
4033 +     * of each value.
4034 +     *
4035 +     * @param parallelismThreshold the (estimated) number of elements
4036 +     * needed for this operation to be executed in parallel
4037 +     * @param transformer a function returning the transformation
4038 +     * for an element, or null if there is no transformation (in
4039 +     * which case the action is not applied)
4040 +     * @param action the action
4041 +     * @param <U> the return type of the transformer
4042 +     * @since 1.8
4043 +     */
4044 +    public <U> void forEachValue(long parallelismThreshold,
4045 +                                 Function<? super V, ? extends U> transformer,
4046 +                                 Consumer<? super U> action) {
4047 +        if (transformer == null || action == null)
4048 +            throw new NullPointerException();
4049 +        new ForEachTransformedValueTask<K,V,U>
4050 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4051 +             transformer, action).invoke();
4052 +    }
4053 +
4054 +    /**
4055 +     * Returns a non-null result from applying the given search
4056 +     * function on each value, or null if none.  Upon success,
4057 +     * further element processing is suppressed and the results of
4058 +     * any other parallel invocations of the search function are
4059 +     * ignored.
4060 +     *
4061 +     * @param parallelismThreshold the (estimated) number of elements
4062 +     * needed for this operation to be executed in parallel
4063 +     * @param searchFunction a function returning a non-null
4064 +     * result on success, else null
4065 +     * @param <U> the return type of the search function
4066 +     * @return a non-null result from applying the given search
4067 +     * function on each value, or null if none
4068 +     * @since 1.8
4069 +     */
4070 +    public <U> U searchValues(long parallelismThreshold,
4071 +                              Function<? super V, ? extends U> searchFunction) {
4072 +        if (searchFunction == null) throw new NullPointerException();
4073 +        return new SearchValuesTask<K,V,U>
4074 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4075 +             searchFunction, new AtomicReference<U>()).invoke();
4076 +    }
4077 +
4078 +    /**
4079 +     * Returns the result of accumulating all values using the
4080 +     * given reducer to combine values, or null if none.
4081 +     *
4082 +     * @param parallelismThreshold the (estimated) number of elements
4083 +     * needed for this operation to be executed in parallel
4084 +     * @param reducer a commutative associative combining function
4085 +     * @return the result of accumulating all values
4086 +     * @since 1.8
4087 +     */
4088 +    public V reduceValues(long parallelismThreshold,
4089 +                          BiFunction<? super V, ? super V, ? extends V> reducer) {
4090 +        if (reducer == null) throw new NullPointerException();
4091 +        return new ReduceValuesTask<K,V>
4092 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4093 +             null, reducer).invoke();
4094 +    }
4095 +
4096 +    /**
4097 +     * Returns the result of accumulating the given transformation
4098 +     * of all values using the given reducer to combine values, or
4099 +     * null if none.
4100 +     *
4101 +     * @param parallelismThreshold the (estimated) number of elements
4102 +     * needed for this operation to be executed in parallel
4103 +     * @param transformer a function returning the transformation
4104 +     * for an element, or null if there is no transformation (in
4105 +     * which case it is not combined)
4106 +     * @param reducer a commutative associative combining function
4107 +     * @param <U> the return type of the transformer
4108 +     * @return the result of accumulating the given transformation
4109 +     * of all values
4110 +     * @since 1.8
4111 +     */
4112 +    public <U> U reduceValues(long parallelismThreshold,
4113 +                              Function<? super V, ? extends U> transformer,
4114 +                              BiFunction<? super U, ? super U, ? extends U> reducer) {
4115 +        if (transformer == null || reducer == null)
4116 +            throw new NullPointerException();
4117 +        return new MapReduceValuesTask<K,V,U>
4118 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4119 +             null, transformer, reducer).invoke();
4120 +    }
4121 +
4122 +    /**
4123 +     * Returns the result of accumulating the given transformation
4124 +     * of all values using the given reducer to combine values,
4125 +     * and the given basis as an identity value.
4126 +     *
4127 +     * @param parallelismThreshold the (estimated) number of elements
4128 +     * needed for this operation to be executed in parallel
4129 +     * @param transformer a function returning the transformation
4130 +     * for an element
4131 +     * @param basis the identity (initial default value) for the reduction
4132 +     * @param reducer a commutative associative combining function
4133 +     * @return the result of accumulating the given transformation
4134 +     * of all values
4135 +     * @since 1.8
4136 +     */
4137 +    public double reduceValuesToDouble(long parallelismThreshold,
4138 +                                       ToDoubleFunction<? super V> transformer,
4139 +                                       double basis,
4140 +                                       DoubleBinaryOperator reducer) {
4141 +        if (transformer == null || reducer == null)
4142 +            throw new NullPointerException();
4143 +        return new MapReduceValuesToDoubleTask<K,V>
4144 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4145 +             null, transformer, basis, reducer).invoke();
4146 +    }
4147 +
4148 +    /**
4149 +     * Returns the result of accumulating the given transformation
4150 +     * of all values using the given reducer to combine values,
4151 +     * and the given basis as an identity value.
4152 +     *
4153 +     * @param parallelismThreshold the (estimated) number of elements
4154 +     * needed for this operation to be executed in parallel
4155 +     * @param transformer a function returning the transformation
4156 +     * for an element
4157 +     * @param basis the identity (initial default value) for the reduction
4158 +     * @param reducer a commutative associative combining function
4159 +     * @return the result of accumulating the given transformation
4160 +     * of all values
4161 +     * @since 1.8
4162 +     */
4163 +    public long reduceValuesToLong(long parallelismThreshold,
4164 +                                   ToLongFunction<? super V> transformer,
4165 +                                   long basis,
4166 +                                   LongBinaryOperator reducer) {
4167 +        if (transformer == null || reducer == null)
4168 +            throw new NullPointerException();
4169 +        return new MapReduceValuesToLongTask<K,V>
4170 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4171 +             null, transformer, basis, reducer).invoke();
4172 +    }
4173 +
4174 +    /**
4175 +     * Returns the result of accumulating the given transformation
4176 +     * of all values using the given reducer to combine values,
4177 +     * and the given basis as an identity value.
4178 +     *
4179 +     * @param parallelismThreshold the (estimated) number of elements
4180 +     * needed for this operation to be executed in parallel
4181 +     * @param transformer a function returning the transformation
4182 +     * for an element
4183 +     * @param basis the identity (initial default value) for the reduction
4184 +     * @param reducer a commutative associative combining function
4185 +     * @return the result of accumulating the given transformation
4186 +     * of all values
4187 +     * @since 1.8
4188 +     */
4189 +    public int reduceValuesToInt(long parallelismThreshold,
4190 +                                 ToIntFunction<? super V> transformer,
4191 +                                 int basis,
4192 +                                 IntBinaryOperator reducer) {
4193 +        if (transformer == null || reducer == null)
4194 +            throw new NullPointerException();
4195 +        return new MapReduceValuesToIntTask<K,V>
4196 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4197 +             null, transformer, basis, reducer).invoke();
4198 +    }
4199 +
4200 +    /**
4201 +     * Performs the given action for each entry.
4202 +     *
4203 +     * @param parallelismThreshold the (estimated) number of elements
4204 +     * needed for this operation to be executed in parallel
4205 +     * @param action the action
4206 +     * @since 1.8
4207 +     */
4208 +    public void forEachEntry(long parallelismThreshold,
4209 +                             Consumer<? super Map.Entry<K,V>> action) {
4210 +        if (action == null) throw new NullPointerException();
4211 +        new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table,
4212 +                                  action).invoke();
4213 +    }
4214 +
4215 +    /**
4216 +     * Performs the given action for each non-null transformation
4217 +     * of each entry.
4218 +     *
4219 +     * @param parallelismThreshold the (estimated) number of elements
4220 +     * needed for this operation to be executed in parallel
4221 +     * @param transformer a function returning the transformation
4222 +     * for an element, or null if there is no transformation (in
4223 +     * which case the action is not applied)
4224 +     * @param action the action
4225 +     * @param <U> the return type of the transformer
4226 +     * @since 1.8
4227 +     */
4228 +    public <U> void forEachEntry(long parallelismThreshold,
4229 +                                 Function<Map.Entry<K,V>, ? extends U> transformer,
4230 +                                 Consumer<? super U> action) {
4231 +        if (transformer == null || action == null)
4232 +            throw new NullPointerException();
4233 +        new ForEachTransformedEntryTask<K,V,U>
4234 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4235 +             transformer, action).invoke();
4236 +    }
4237 +
4238 +    /**
4239 +     * Returns a non-null result from applying the given search
4240 +     * function on each entry, or null if none.  Upon success,
4241 +     * further element processing is suppressed and the results of
4242 +     * any other parallel invocations of the search function are
4243 +     * ignored.
4244 +     *
4245 +     * @param parallelismThreshold the (estimated) number of elements
4246 +     * needed for this operation to be executed in parallel
4247 +     * @param searchFunction a function returning a non-null
4248 +     * result on success, else null
4249 +     * @param <U> the return type of the search function
4250 +     * @return a non-null result from applying the given search
4251 +     * function on each entry, or null if none
4252 +     * @since 1.8
4253 +     */
4254 +    public <U> U searchEntries(long parallelismThreshold,
4255 +                               Function<Map.Entry<K,V>, ? extends U> searchFunction) {
4256 +        if (searchFunction == null) throw new NullPointerException();
4257 +        return new SearchEntriesTask<K,V,U>
4258 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4259 +             searchFunction, new AtomicReference<U>()).invoke();
4260 +    }
4261 +
4262 +    /**
4263 +     * Returns the result of accumulating all entries using the
4264 +     * given reducer to combine values, or null if none.
4265 +     *
4266 +     * @param parallelismThreshold the (estimated) number of elements
4267 +     * needed for this operation to be executed in parallel
4268 +     * @param reducer a commutative associative combining function
4269 +     * @return the result of accumulating all entries
4270 +     * @since 1.8
4271 +     */
4272 +    public Map.Entry<K,V> reduceEntries(long parallelismThreshold,
4273 +                                        BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4274 +        if (reducer == null) throw new NullPointerException();
4275 +        return new ReduceEntriesTask<K,V>
4276 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4277 +             null, reducer).invoke();
4278 +    }
4279 +
4280 +    /**
4281 +     * Returns the result of accumulating the given transformation
4282 +     * of all entries using the given reducer to combine values,
4283 +     * or null if none.
4284 +     *
4285 +     * @param parallelismThreshold the (estimated) number of elements
4286 +     * needed for this operation to be executed in parallel
4287 +     * @param transformer a function returning the transformation
4288 +     * for an element, or null if there is no transformation (in
4289 +     * which case it is not combined)
4290 +     * @param reducer a commutative associative combining function
4291 +     * @param <U> the return type of the transformer
4292 +     * @return the result of accumulating the given transformation
4293 +     * of all entries
4294 +     * @since 1.8
4295 +     */
4296 +    public <U> U reduceEntries(long parallelismThreshold,
4297 +                               Function<Map.Entry<K,V>, ? extends U> transformer,
4298 +                               BiFunction<? super U, ? super U, ? extends U> reducer) {
4299 +        if (transformer == null || reducer == null)
4300 +            throw new NullPointerException();
4301 +        return new MapReduceEntriesTask<K,V,U>
4302 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4303 +             null, transformer, reducer).invoke();
4304 +    }
4305 +
4306 +    /**
4307 +     * Returns the result of accumulating the given transformation
4308 +     * of all entries using the given reducer to combine values,
4309 +     * and the given basis as an identity value.
4310 +     *
4311 +     * @param parallelismThreshold the (estimated) number of elements
4312 +     * needed for this operation to be executed in parallel
4313 +     * @param transformer a function returning the transformation
4314 +     * for an element
4315 +     * @param basis the identity (initial default value) for the reduction
4316 +     * @param reducer a commutative associative combining function
4317 +     * @return the result of accumulating the given transformation
4318 +     * of all entries
4319 +     * @since 1.8
4320 +     */
4321 +    public double reduceEntriesToDouble(long parallelismThreshold,
4322 +                                        ToDoubleFunction<Map.Entry<K,V>> transformer,
4323 +                                        double basis,
4324 +                                        DoubleBinaryOperator reducer) {
4325 +        if (transformer == null || reducer == null)
4326 +            throw new NullPointerException();
4327 +        return new MapReduceEntriesToDoubleTask<K,V>
4328 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4329 +             null, transformer, basis, reducer).invoke();
4330 +    }
4331 +
4332 +    /**
4333 +     * Returns the result of accumulating the given transformation
4334 +     * of all entries using the given reducer to combine values,
4335 +     * and the given basis as an identity value.
4336 +     *
4337 +     * @param parallelismThreshold the (estimated) number of elements
4338 +     * needed for this operation to be executed in parallel
4339 +     * @param transformer a function returning the transformation
4340 +     * for an element
4341 +     * @param basis the identity (initial default value) for the reduction
4342 +     * @param reducer a commutative associative combining function
4343 +     * @return the result of accumulating the given transformation
4344 +     * of all entries
4345 +     * @since 1.8
4346 +     */
4347 +    public long reduceEntriesToLong(long parallelismThreshold,
4348 +                                    ToLongFunction<Map.Entry<K,V>> transformer,
4349 +                                    long basis,
4350 +                                    LongBinaryOperator reducer) {
4351 +        if (transformer == null || reducer == null)
4352 +            throw new NullPointerException();
4353 +        return new MapReduceEntriesToLongTask<K,V>
4354 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4355 +             null, transformer, basis, reducer).invoke();
4356 +    }
4357 +
4358 +    /**
4359 +     * Returns the result of accumulating the given transformation
4360 +     * of all entries using the given reducer to combine values,
4361 +     * and the given basis as an identity value.
4362 +     *
4363 +     * @param parallelismThreshold the (estimated) number of elements
4364 +     * needed for this operation to be executed in parallel
4365 +     * @param transformer a function returning the transformation
4366 +     * for an element
4367 +     * @param basis the identity (initial default value) for the reduction
4368 +     * @param reducer a commutative associative combining function
4369 +     * @return the result of accumulating the given transformation
4370 +     * of all entries
4371 +     * @since 1.8
4372 +     */
4373 +    public int reduceEntriesToInt(long parallelismThreshold,
4374 +                                  ToIntFunction<Map.Entry<K,V>> transformer,
4375 +                                  int basis,
4376 +                                  IntBinaryOperator reducer) {
4377 +        if (transformer == null || reducer == null)
4378 +            throw new NullPointerException();
4379 +        return new MapReduceEntriesToIntTask<K,V>
4380 +            (null, batchFor(parallelismThreshold), 0, 0, table,
4381 +             null, transformer, basis, reducer).invoke();
4382 +    }
4383 +
4384 +
4385 +    /* ----------------Views -------------- */
4386 +
4387 +    /**
4388 +     * Base class for views.
4389 +     */
4390 +    abstract static class CollectionView<K,V,E>
4391 +        implements Collection<E>, java.io.Serializable {
4392 +        private static final long serialVersionUID = 7249069246763182397L;
4393 +        final ConcurrentHashMap<K,V> map;
4394 +        CollectionView(ConcurrentHashMap<K,V> map)  { this.map = map; }
4395  
4396          /**
4397 <         * Returns a task that when invoked, performs the given action
4475 <         * for each value
4397 >         * Returns the map backing this view.
4398           *
4399 <         * @param map the map
4478 <         * @param action the action
4399 >         * @return the map backing this view
4400           */
4401 <        public static <K,V> ForkJoinTask<Void> forEachValue
4481 <            (ConcurrentHashMap<K,V> map,
4482 <             Action<V> action) {
4483 <            if (action == null) throw new NullPointerException();
4484 <            return new ForEachValueTask<K,V>(map, action);
4485 <        }
4401 >        public ConcurrentHashMap<K,V> getMap() { return map; }
4402  
4403          /**
4404 <         * Returns a task that when invoked, performs the given action
4405 <         * for each non-null transformation of each value
4490 <         *
4491 <         * @param map the map
4492 <         * @param transformer a function returning the transformation
4493 <         * for an element, or null of there is no transformation (in
4494 <         * which case the action is not applied).
4495 <         * @param action the action
4404 >         * Removes all of the elements from this view, by removing all
4405 >         * the mappings from the map backing this view.
4406           */
4407 <        public static <K,V,U> ForkJoinTask<Void> forEachValue
4408 <            (ConcurrentHashMap<K,V> map,
4409 <             Fun<? super V, ? extends U> transformer,
4500 <             Action<U> action) {
4501 <            if (transformer == null || action == null)
4502 <                throw new NullPointerException();
4503 <            return new ForEachTransformedValueTask<K,V,U>
4504 <                (map, transformer, action);
4505 <        }
4407 >        public final void clear()      { map.clear(); }
4408 >        public final int size()        { return map.size(); }
4409 >        public final boolean isEmpty() { return map.isEmpty(); }
4410  
4411 +        // implementations below rely on concrete classes supplying these
4412 +        // abstract methods
4413          /**
4414 <         * Returns a task that when invoked, returns a non-null result
4509 <         * from applying the given search function on each value, or
4510 <         * null if none.  Further element processing is suppressed
4511 <         * upon success. However, this method does not return until
4512 <         * other in-progress parallel invocations of the search
4513 <         * function also complete.
4414 >         * Returns an iterator over the elements in this collection.
4415           *
4416 <         * @param map the map
4417 <         * @param searchFunction a function returning a non-null
4517 <         * result on success, else null
4518 <         * @return the task
4416 >         * <p>The returned iterator is
4417 >         * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
4418           *
4419 +         * @return an iterator over the elements in this collection
4420           */
4421 <        public static <K,V,U> ForkJoinTask<U> searchValues
4422 <            (ConcurrentHashMap<K,V> map,
4423 <             Fun<? super V, ? extends U> searchFunction) {
4524 <            if (searchFunction == null) throw new NullPointerException();
4525 <            return new SearchValuesTask<K,V,U>
4526 <                (map, searchFunction,
4527 <                 new AtomicReference<U>());
4528 <        }
4421 >        public abstract Iterator<E> iterator();
4422 >        public abstract boolean contains(Object o);
4423 >        public abstract boolean remove(Object o);
4424  
4425 <        /**
4426 <         * Returns a task that when invoked, returns the result of
4427 <         * accumulating all values using the given reducer to combine
4428 <         * values, or null if none.
4429 <         *
4430 <         * @param map the map
4431 <         * @param reducer a commutative associative combining function
4432 <         * @return the task
4433 <         */
4434 <        public static <K,V> ForkJoinTask<V> reduceValues
4435 <            (ConcurrentHashMap<K,V> map,
4436 <             BiFun<? super V, ? super V, ? extends V> reducer) {
4437 <            if (reducer == null) throw new NullPointerException();
4438 <            return new ReduceValuesTask<K,V>
4439 <                (map, reducer);
4425 >        private static final String OOME_MSG = "Required array size too large";
4426 >
4427 >        public final Object[] toArray() {
4428 >            long sz = map.mappingCount();
4429 >            if (sz > MAX_ARRAY_SIZE)
4430 >                throw new OutOfMemoryError(OOME_MSG);
4431 >            int n = (int)sz;
4432 >            Object[] r = new Object[n];
4433 >            int i = 0;
4434 >            for (E e : this) {
4435 >                if (i == n) {
4436 >                    if (n >= MAX_ARRAY_SIZE)
4437 >                        throw new OutOfMemoryError(OOME_MSG);
4438 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4439 >                        n = MAX_ARRAY_SIZE;
4440 >                    else
4441 >                        n += (n >>> 1) + 1;
4442 >                    r = Arrays.copyOf(r, n);
4443 >                }
4444 >                r[i++] = e;
4445 >            }
4446 >            return (i == n) ? r : Arrays.copyOf(r, i);
4447          }
4448  
4449 <        /**
4450 <         * Returns a task that when invoked, returns the result of
4451 <         * accumulating the given transformation of all values using the
4452 <         * given reducer to combine values, or null if none.
4453 <         *
4454 <         * @param map the map
4455 <         * @param transformer a function returning the transformation
4456 <         * for an element, or null of there is no transformation (in
4457 <         * which case it is not combined).
4458 <         * @param reducer a commutative associative combining function
4459 <         * @return the task
4460 <         */
4461 <        public static <K,V,U> ForkJoinTask<U> reduceValues
4462 <            (ConcurrentHashMap<K,V> map,
4463 <             Fun<? super V, ? extends U> transformer,
4464 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4465 <            if (transformer == null || reducer == null)
4466 <                throw new NullPointerException();
4467 <            return new MapReduceValuesTask<K,V,U>
4468 <                (map, transformer, reducer);
4449 >        @SuppressWarnings("unchecked")
4450 >        public final <T> T[] toArray(T[] a) {
4451 >            long sz = map.mappingCount();
4452 >            if (sz > MAX_ARRAY_SIZE)
4453 >                throw new OutOfMemoryError(OOME_MSG);
4454 >            int m = (int)sz;
4455 >            T[] r = (a.length >= m) ? a :
4456 >                (T[])java.lang.reflect.Array
4457 >                .newInstance(a.getClass().getComponentType(), m);
4458 >            int n = r.length;
4459 >            int i = 0;
4460 >            for (E e : this) {
4461 >                if (i == n) {
4462 >                    if (n >= MAX_ARRAY_SIZE)
4463 >                        throw new OutOfMemoryError(OOME_MSG);
4464 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4465 >                        n = MAX_ARRAY_SIZE;
4466 >                    else
4467 >                        n += (n >>> 1) + 1;
4468 >                    r = Arrays.copyOf(r, n);
4469 >                }
4470 >                r[i++] = (T)e;
4471 >            }
4472 >            if (a == r && i < n) {
4473 >                r[i] = null; // null-terminate
4474 >                return r;
4475 >            }
4476 >            return (i == n) ? r : Arrays.copyOf(r, i);
4477          }
4478  
4479          /**
4480 <         * Returns a task that when invoked, returns the result of
4481 <         * accumulating the given transformation of all values using the
4482 <         * given reducer to combine values, and the given basis as an
4483 <         * identity value.
4480 >         * Returns a string representation of this collection.
4481 >         * The string representation consists of the string representations
4482 >         * of the collection's elements in the order they are returned by
4483 >         * its iterator, enclosed in square brackets ({@code "[]"}).
4484 >         * Adjacent elements are separated by the characters {@code ", "}
4485 >         * (comma and space).  Elements are converted to strings as by
4486 >         * {@link String#valueOf(Object)}.
4487           *
4488 <         * @param map the map
4576 <         * @param transformer a function returning the transformation
4577 <         * for an element
4578 <         * @param basis the identity (initial default value) for the reduction
4579 <         * @param reducer a commutative associative combining function
4580 <         * @return the task
4488 >         * @return a string representation of this collection
4489           */
4490 <        public static <K,V> ForkJoinTask<Double> reduceValuesToDouble
4491 <            (ConcurrentHashMap<K,V> map,
4492 <             ObjectToDouble<? super V> transformer,
4493 <             double basis,
4494 <             DoubleByDoubleToDouble reducer) {
4495 <            if (transformer == null || reducer == null)
4496 <                throw new NullPointerException();
4497 <            return new MapReduceValuesToDoubleTask<K,V>
4498 <                (map, transformer, basis, reducer);
4490 >        public final String toString() {
4491 >            StringBuilder sb = new StringBuilder();
4492 >            sb.append('[');
4493 >            Iterator<E> it = iterator();
4494 >            if (it.hasNext()) {
4495 >                for (;;) {
4496 >                    Object e = it.next();
4497 >                    sb.append(e == this ? "(this Collection)" : e);
4498 >                    if (!it.hasNext())
4499 >                        break;
4500 >                    sb.append(',').append(' ');
4501 >                }
4502 >            }
4503 >            return sb.append(']').toString();
4504          }
4505  
4506 <        /**
4507 <         * Returns a task that when invoked, returns the result of
4508 <         * accumulating the given transformation of all values using the
4509 <         * given reducer to combine values, and the given basis as an
4510 <         * identity value.
4511 <         *
4512 <         * @param map the map
4513 <         * @param transformer a function returning the transformation
4601 <         * for an element
4602 <         * @param basis the identity (initial default value) for the reduction
4603 <         * @param reducer a commutative associative combining function
4604 <         * @return the task
4605 <         */
4606 <        public static <K,V> ForkJoinTask<Long> reduceValuesToLong
4607 <            (ConcurrentHashMap<K,V> map,
4608 <             ObjectToLong<? super V> transformer,
4609 <             long basis,
4610 <             LongByLongToLong reducer) {
4611 <            if (transformer == null || reducer == null)
4612 <                throw new NullPointerException();
4613 <            return new MapReduceValuesToLongTask<K,V>
4614 <                (map, transformer, basis, reducer);
4506 >        public final boolean containsAll(Collection<?> c) {
4507 >            if (c != this) {
4508 >                for (Object e : c) {
4509 >                    if (e == null || !contains(e))
4510 >                        return false;
4511 >                }
4512 >            }
4513 >            return true;
4514          }
4515  
4516 <        /**
4517 <         * Returns a task that when invoked, returns the result of
4518 <         * accumulating the given transformation of all values using the
4519 <         * given reducer to combine values, and the given basis as an
4520 <         * identity value.
4521 <         *
4522 <         * @param map the map
4523 <         * @param transformer a function returning the transformation
4524 <         * for an element
4525 <         * @param basis the identity (initial default value) for the reduction
4526 <         * @param reducer a commutative associative combining function
4527 <         * @return the task
4528 <         */
4529 <        public static <K,V> ForkJoinTask<Integer> reduceValuesToInt
4530 <            (ConcurrentHashMap<K,V> map,
4531 <             ObjectToInt<? super V> transformer,
4532 <             int basis,
4533 <             IntByIntToInt reducer) {
4534 <            if (transformer == null || reducer == null)
4535 <                throw new NullPointerException();
4637 <            return new MapReduceValuesToIntTask<K,V>
4638 <                (map, transformer, basis, reducer);
4516 >        public boolean removeAll(Collection<?> c) {
4517 >            if (c == null) throw new NullPointerException();
4518 >            boolean modified = false;
4519 >            // Use (c instanceof Set) as a hint that lookup in c is as
4520 >            // efficient as this view
4521 >            Node<K,V>[] t;
4522 >            if ((t = map.table) == null) {
4523 >                return false;
4524 >            } else if (c instanceof Set<?> && c.size() > t.length) {
4525 >                for (Iterator<?> it = iterator(); it.hasNext(); ) {
4526 >                    if (c.contains(it.next())) {
4527 >                        it.remove();
4528 >                        modified = true;
4529 >                    }
4530 >                }
4531 >            } else {
4532 >                for (Object e : c)
4533 >                    modified |= remove(e);
4534 >            }
4535 >            return modified;
4536          }
4537  
4538 <        /**
4539 <         * Returns a task that when invoked, perform the given action
4540 <         * for each entry
4541 <         *
4542 <         * @param map the map
4543 <         * @param action the action
4544 <         */
4545 <        public static <K,V> ForkJoinTask<Void> forEachEntry
4546 <            (ConcurrentHashMap<K,V> map,
4547 <             Action<Map.Entry<K,V>> action) {
4651 <            if (action == null) throw new NullPointerException();
4652 <            return new ForEachEntryTask<K,V>(map, action);
4538 >        public final boolean retainAll(Collection<?> c) {
4539 >            if (c == null) throw new NullPointerException();
4540 >            boolean modified = false;
4541 >            for (Iterator<E> it = iterator(); it.hasNext();) {
4542 >                if (!c.contains(it.next())) {
4543 >                    it.remove();
4544 >                    modified = true;
4545 >                }
4546 >            }
4547 >            return modified;
4548          }
4549  
4550 <        /**
4551 <         * Returns a task that when invoked, perform the given action
4552 <         * for each non-null transformation of each entry
4553 <         *
4554 <         * @param map the map
4555 <         * @param transformer a function returning the transformation
4556 <         * for an element, or null of there is no transformation (in
4557 <         * which case the action is not applied).
4558 <         * @param action the action
4559 <         */
4560 <        public static <K,V,U> ForkJoinTask<Void> forEachEntry
4561 <            (ConcurrentHashMap<K,V> map,
4562 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4563 <             Action<U> action) {
4564 <            if (transformer == null || action == null)
4565 <                throw new NullPointerException();
4566 <            return new ForEachTransformedEntryTask<K,V,U>
4567 <                (map, transformer, action);
4550 >    }
4551 >
4552 >    /**
4553 >     * A view of a ConcurrentHashMap as a {@link Set} of keys, in
4554 >     * which additions may optionally be enabled by mapping to a
4555 >     * common value.  This class cannot be directly instantiated.
4556 >     * See {@link #keySet() keySet()},
4557 >     * {@link #keySet(Object) keySet(V)},
4558 >     * {@link #newKeySet() newKeySet()},
4559 >     * {@link #newKeySet(int) newKeySet(int)}.
4560 >     *
4561 >     * @since 1.8
4562 >     */
4563 >    public static class KeySetView<K,V> extends CollectionView<K,V,K>
4564 >        implements Set<K>, java.io.Serializable {
4565 >        private static final long serialVersionUID = 7249069246763182397L;
4566 >        @SuppressWarnings("serial") // Conditionally serializable
4567 >        private final V value;
4568 >        KeySetView(ConcurrentHashMap<K,V> map, V value) {  // non-public
4569 >            super(map);
4570 >            this.value = value;
4571          }
4572  
4573          /**
4574 <         * Returns a task that when invoked, returns a non-null result
4575 <         * from applying the given search function on each entry, or
4678 <         * null if none.  Further element processing is suppressed
4679 <         * upon success. However, this method does not return until
4680 <         * other in-progress parallel invocations of the search
4681 <         * function also complete.
4682 <         *
4683 <         * @param map the map
4684 <         * @param searchFunction a function returning a non-null
4685 <         * result on success, else null
4686 <         * @return the task
4574 >         * Returns the default mapped value for additions,
4575 >         * or {@code null} if additions are not supported.
4576           *
4577 +         * @return the default mapped value for additions, or {@code null}
4578 +         * if not supported
4579           */
4580 <        public static <K,V,U> ForkJoinTask<U> searchEntries
4690 <            (ConcurrentHashMap<K,V> map,
4691 <             Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4692 <            if (searchFunction == null) throw new NullPointerException();
4693 <            return new SearchEntriesTask<K,V,U>
4694 <                (map, searchFunction,
4695 <                 new AtomicReference<U>());
4696 <        }
4580 >        public V getMappedValue() { return value; }
4581  
4582          /**
4583 <         * Returns a task that when invoked, returns the result of
4584 <         * accumulating all entries using the given reducer to combine
4701 <         * values, or null if none.
4702 <         *
4703 <         * @param map the map
4704 <         * @param reducer a commutative associative combining function
4705 <         * @return the task
4583 >         * {@inheritDoc}
4584 >         * @throws NullPointerException if the specified key is null
4585           */
4586 <        public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries
4708 <            (ConcurrentHashMap<K,V> map,
4709 <             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4710 <            if (reducer == null) throw new NullPointerException();
4711 <            return new ReduceEntriesTask<K,V>
4712 <                (map, reducer);
4713 <        }
4586 >        public boolean contains(Object o) { return map.containsKey(o); }
4587  
4588          /**
4589 <         * Returns a task that when invoked, returns the result of
4590 <         * accumulating the given transformation of all entries using the
4591 <         * given reducer to combine values, or null if none.
4589 >         * Removes the key from this map view, by removing the key (and its
4590 >         * corresponding value) from the backing map.  This method does
4591 >         * nothing if the key is not in the map.
4592           *
4593 <         * @param map the map
4594 <         * @param transformer a function returning the transformation
4595 <         * for an element, or null of there is no transformation (in
4723 <         * which case it is not combined).
4724 <         * @param reducer a commutative associative combining function
4725 <         * @return the task
4593 >         * @param  o the key to be removed from the backing map
4594 >         * @return {@code true} if the backing map contained the specified key
4595 >         * @throws NullPointerException if the specified key is null
4596           */
4597 <        public static <K,V,U> ForkJoinTask<U> reduceEntries
4728 <            (ConcurrentHashMap<K,V> map,
4729 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
4730 <             BiFun<? super U, ? super U, ? extends U> reducer) {
4731 <            if (transformer == null || reducer == null)
4732 <                throw new NullPointerException();
4733 <            return new MapReduceEntriesTask<K,V,U>
4734 <                (map, transformer, reducer);
4735 <        }
4597 >        public boolean remove(Object o) { return map.remove(o) != null; }
4598  
4599          /**
4600 <         * Returns a task that when invoked, returns the result of
4739 <         * accumulating the given transformation of all entries using the
4740 <         * given reducer to combine values, and the given basis as an
4741 <         * identity value.
4742 <         *
4743 <         * @param map the map
4744 <         * @param transformer a function returning the transformation
4745 <         * for an element
4746 <         * @param basis the identity (initial default value) for the reduction
4747 <         * @param reducer a commutative associative combining function
4748 <         * @return the task
4600 >         * @return an iterator over the keys of the backing map
4601           */
4602 <        public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble
4603 <            (ConcurrentHashMap<K,V> map,
4604 <             ObjectToDouble<Map.Entry<K,V>> transformer,
4605 <             double basis,
4606 <             DoubleByDoubleToDouble reducer) {
4755 <            if (transformer == null || reducer == null)
4756 <                throw new NullPointerException();
4757 <            return new MapReduceEntriesToDoubleTask<K,V>
4758 <                (map, transformer, basis, reducer);
4602 >        public Iterator<K> iterator() {
4603 >            Node<K,V>[] t;
4604 >            ConcurrentHashMap<K,V> m = map;
4605 >            int f = (t = m.table) == null ? 0 : t.length;
4606 >            return new KeyIterator<K,V>(t, f, 0, f, m);
4607          }
4608  
4609          /**
4610 <         * Returns a task that when invoked, returns the result of
4611 <         * accumulating the given transformation of all entries using the
4764 <         * given reducer to combine values, and the given basis as an
4765 <         * identity value.
4610 >         * Adds the specified key to this set view by mapping the key to
4611 >         * the default mapped value in the backing map, if defined.
4612           *
4613 <         * @param map the map
4614 <         * @param transformer a function returning the transformation
4615 <         * for an element
4616 <         * @param basis the identity (initial default value) for the reduction
4617 <         * @param reducer a commutative associative combining function
4772 <         * @return the task
4613 >         * @param e key to be added
4614 >         * @return {@code true} if this set changed as a result of the call
4615 >         * @throws NullPointerException if the specified key is null
4616 >         * @throws UnsupportedOperationException if no default mapped value
4617 >         * for additions was provided
4618           */
4619 <        public static <K,V> ForkJoinTask<Long> reduceEntriesToLong
4620 <            (ConcurrentHashMap<K,V> map,
4621 <             ObjectToLong<Map.Entry<K,V>> transformer,
4622 <             long basis,
4623 <             LongByLongToLong reducer) {
4779 <            if (transformer == null || reducer == null)
4780 <                throw new NullPointerException();
4781 <            return new MapReduceEntriesToLongTask<K,V>
4782 <                (map, transformer, basis, reducer);
4619 >        public boolean add(K e) {
4620 >            V v;
4621 >            if ((v = value) == null)
4622 >                throw new UnsupportedOperationException();
4623 >            return map.putVal(e, v, true) == null;
4624          }
4625  
4626          /**
4627 <         * Returns a task that when invoked, returns the result of
4628 <         * accumulating the given transformation of all entries using the
4788 <         * given reducer to combine values, and the given basis as an
4789 <         * identity value.
4627 >         * Adds all of the elements in the specified collection to this set,
4628 >         * as if by calling {@link #add} on each one.
4629           *
4630 <         * @param map the map
4631 <         * @param transformer a function returning the transformation
4632 <         * for an element
4633 <         * @param basis the identity (initial default value) for the reduction
4634 <         * @param reducer a commutative associative combining function
4635 <         * @return the task
4630 >         * @param c the elements to be inserted into this set
4631 >         * @return {@code true} if this set changed as a result of the call
4632 >         * @throws NullPointerException if the collection or any of its
4633 >         * elements are {@code null}
4634 >         * @throws UnsupportedOperationException if no default mapped value
4635 >         * for additions was provided
4636           */
4637 <        public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt
4638 <            (ConcurrentHashMap<K,V> map,
4639 <             ObjectToInt<Map.Entry<K,V>> transformer,
4640 <             int basis,
4641 <             IntByIntToInt reducer) {
4642 <            if (transformer == null || reducer == null)
4643 <                throw new NullPointerException();
4644 <            return new MapReduceEntriesToIntTask<K,V>
4645 <                (map, transformer, basis, reducer);
4637 >        public boolean addAll(Collection<? extends K> c) {
4638 >            boolean added = false;
4639 >            V v;
4640 >            if ((v = value) == null)
4641 >                throw new UnsupportedOperationException();
4642 >            for (K e : c) {
4643 >                if (map.putVal(e, v, true) == null)
4644 >                    added = true;
4645 >            }
4646 >            return added;
4647          }
4808    }
4648  
4649 <    // -------------------------------------------------------
4649 >        public int hashCode() {
4650 >            int h = 0;
4651 >            for (K e : this)
4652 >                h += e.hashCode();
4653 >            return h;
4654 >        }
4655  
4656 <    /**
4657 <     * Base for FJ tasks for bulk operations. This adds a variant of
4658 <     * CountedCompleters and some split and merge bookeeping to
4659 <     * iterator functionality. The forEach and reduce methods are
4660 <     * similar to those illustrated in CountedCompleter documentation,
4661 <     * except that bottom-up reduction completions perform them within
4818 <     * their compute methods. The search methods are like forEach
4819 <     * except they continually poll for success and exit early.  Also,
4820 <     * exceptions are handled in a simpler manner, by just trying to
4821 <     * complete root task exceptionally.
4822 <     */
4823 <    static abstract class BulkTask<K,V,R> extends Traverser<K,V,R> {
4824 <        final BulkTask<K,V,?> parent;  // completion target
4825 <        int batch;                     // split control
4826 <        int pending;                   // completion control
4656 >        public boolean equals(Object o) {
4657 >            Set<?> c;
4658 >            return ((o instanceof Set) &&
4659 >                    ((c = (Set<?>)o) == this ||
4660 >                     (containsAll(c) && c.containsAll(this))));
4661 >        }
4662  
4663 <        /** Constructor for root tasks */
4664 <        BulkTask(ConcurrentHashMap<K,V> map) {
4665 <            super(map);
4666 <            this.parent = null;
4667 <            this.batch = -1; // force call to batch() on execution
4663 >        public Spliterator<K> spliterator() {
4664 >            Node<K,V>[] t;
4665 >            ConcurrentHashMap<K,V> m = map;
4666 >            long n = m.sumCount();
4667 >            int f = (t = m.table) == null ? 0 : t.length;
4668 >            return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4669          }
4670  
4671 <        /** Constructor for subtasks */
4672 <        BulkTask(BulkTask<K,V,?> parent, int batch, boolean split) {
4673 <            super(parent, split);
4674 <            this.parent = parent;
4675 <            this.batch = batch;
4671 >        public void forEach(Consumer<? super K> action) {
4672 >            if (action == null) throw new NullPointerException();
4673 >            Node<K,V>[] t;
4674 >            if ((t = map.table) != null) {
4675 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4676 >                for (Node<K,V> p; (p = it.advance()) != null; )
4677 >                    action.accept(p.key);
4678 >            }
4679          }
4680 +    }
4681  
4682 <        // FJ methods
4682 >    /**
4683 >     * A view of a ConcurrentHashMap as a {@link Collection} of
4684 >     * values, in which additions are disabled. This class cannot be
4685 >     * directly instantiated. See {@link #values()}.
4686 >     */
4687 >    static final class ValuesView<K,V> extends CollectionView<K,V,V>
4688 >        implements Collection<V>, java.io.Serializable {
4689 >        private static final long serialVersionUID = 2249069246763182397L;
4690 >        ValuesView(ConcurrentHashMap<K,V> map) { super(map); }
4691 >        public final boolean contains(Object o) {
4692 >            return map.containsValue(o);
4693 >        }
4694  
4695 <        /**
4696 <         * Propagate completion. Note that all reduce actions
4697 <         * bypass this method to combine while completing.
4698 <         */
4699 <        final void tryComplete() {
4700 <            BulkTask<K,V,?> a = this, s = a;
4850 <            for (int c;;) {
4851 <                if ((c = a.pending) == 0) {
4852 <                    if ((a = (s = a).parent) == null) {
4853 <                        s.quietlyComplete();
4854 <                        break;
4695 >        public final boolean remove(Object o) {
4696 >            if (o != null) {
4697 >                for (Iterator<V> it = iterator(); it.hasNext();) {
4698 >                    if (o.equals(it.next())) {
4699 >                        it.remove();
4700 >                        return true;
4701                      }
4702                  }
4857                else if (U.compareAndSwapInt(a, PENDING, c, c - 1))
4858                    break;
4703              }
4704 +            return false;
4705          }
4706  
4707 <        /**
4708 <         * Force root task to throw exception unless already complete.
4709 <         */
4710 <        final void tryAbortComputation(Throwable ex) {
4711 <            for (BulkTask<K,V,?> a = this;;) {
4712 <                BulkTask<K,V,?> p = a.parent;
4713 <                if (p == null) {
4714 <                    a.completeExceptionally(ex);
4715 <                    break;
4707 >        public final Iterator<V> iterator() {
4708 >            ConcurrentHashMap<K,V> m = map;
4709 >            Node<K,V>[] t;
4710 >            int f = (t = m.table) == null ? 0 : t.length;
4711 >            return new ValueIterator<K,V>(t, f, 0, f, m);
4712 >        }
4713 >
4714 >        public final boolean add(V e) {
4715 >            throw new UnsupportedOperationException();
4716 >        }
4717 >        public final boolean addAll(Collection<? extends V> c) {
4718 >            throw new UnsupportedOperationException();
4719 >        }
4720 >
4721 >        @Override public boolean removeAll(Collection<?> c) {
4722 >            if (c == null) throw new NullPointerException();
4723 >            boolean modified = false;
4724 >            for (Iterator<V> it = iterator(); it.hasNext();) {
4725 >                if (c.contains(it.next())) {
4726 >                    it.remove();
4727 >                    modified = true;
4728                  }
4872                a = p;
4729              }
4730 +            return modified;
4731          }
4732  
4733 <        public final boolean exec() {
4734 <            try {
4735 <                compute();
4736 <            }
4737 <            catch(Throwable ex) {
4738 <                tryAbortComputation(ex);
4733 >        public boolean removeIf(Predicate<? super V> filter) {
4734 >            return map.removeValueIf(filter);
4735 >        }
4736 >
4737 >        public Spliterator<V> spliterator() {
4738 >            Node<K,V>[] t;
4739 >            ConcurrentHashMap<K,V> m = map;
4740 >            long n = m.sumCount();
4741 >            int f = (t = m.table) == null ? 0 : t.length;
4742 >            return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4743 >        }
4744 >
4745 >        public void forEach(Consumer<? super V> action) {
4746 >            if (action == null) throw new NullPointerException();
4747 >            Node<K,V>[] t;
4748 >            if ((t = map.table) != null) {
4749 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4750 >                for (Node<K,V> p; (p = it.advance()) != null; )
4751 >                    action.accept(p.val);
4752              }
4883            return false;
4753          }
4754 +    }
4755  
4756 <        public abstract void compute();
4756 >    /**
4757 >     * A view of a ConcurrentHashMap as a {@link Set} of (key, value)
4758 >     * entries.  This class cannot be directly instantiated. See
4759 >     * {@link #entrySet()}.
4760 >     */
4761 >    static final class EntrySetView<K,V> extends CollectionView<K,V,Map.Entry<K,V>>
4762 >        implements Set<Map.Entry<K,V>>, java.io.Serializable {
4763 >        private static final long serialVersionUID = 2249069246763182397L;
4764 >        EntrySetView(ConcurrentHashMap<K,V> map) { super(map); }
4765  
4766 <        // utilities
4766 >        public boolean contains(Object o) {
4767 >            Object k, v, r; Map.Entry<?,?> e;
4768 >            return ((o instanceof Map.Entry) &&
4769 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4770 >                    (r = map.get(k)) != null &&
4771 >                    (v = e.getValue()) != null &&
4772 >                    (v == r || v.equals(r)));
4773 >        }
4774  
4775 <        /** CompareAndSet pending count */
4776 <        final boolean casPending(int cmp, int val) {
4777 <            return U.compareAndSwapInt(this, PENDING, cmp, val);
4775 >        public boolean remove(Object o) {
4776 >            Object k, v; Map.Entry<?,?> e;
4777 >            return ((o instanceof Map.Entry) &&
4778 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4779 >                    (v = e.getValue()) != null &&
4780 >                    map.remove(k, v));
4781          }
4782  
4783          /**
4784 <         * Return approx exp2 of the number of times (minus one) to
4897 <         * split task by two before executing leaf action. This value
4898 <         * is faster to compute and more convenient to use as a guide
4899 <         * to splitting than is the depth, since it is used while
4900 <         * dividing by two anyway.
4784 >         * @return an iterator over the entries of the backing map
4785           */
4786 <        final int batch() {
4787 <            int b = batch;
4788 <            if (b < 0) {
4789 <                long n = map.counter.sum();
4790 <                int sp = getPool().getParallelism() << 3; // slack of 8
4791 <                b = batch = (n <= 0L)? 0 : (n < (long)sp) ? (int)n : sp;
4786 >        public Iterator<Map.Entry<K,V>> iterator() {
4787 >            ConcurrentHashMap<K,V> m = map;
4788 >            Node<K,V>[] t;
4789 >            int f = (t = m.table) == null ? 0 : t.length;
4790 >            return new EntryIterator<K,V>(t, f, 0, f, m);
4791 >        }
4792 >
4793 >        public boolean add(Entry<K,V> e) {
4794 >            return map.putVal(e.getKey(), e.getValue(), false) == null;
4795 >        }
4796 >
4797 >        public boolean addAll(Collection<? extends Entry<K,V>> c) {
4798 >            boolean added = false;
4799 >            for (Entry<K,V> e : c) {
4800 >                if (add(e))
4801 >                    added = true;
4802              }
4803 <            return b;
4803 >            return added;
4804          }
4805  
4806 <        /**
4807 <         * Error message for hoisted null checks of functions
4808 <         */
4809 <        static final String NullFunctionMessage =
4810 <            "Unexpected null function";
4806 >        public boolean removeIf(Predicate<? super Entry<K,V>> filter) {
4807 >            return map.removeEntryIf(filter);
4808 >        }
4809 >
4810 >        public final int hashCode() {
4811 >            int h = 0;
4812 >            Node<K,V>[] t;
4813 >            if ((t = map.table) != null) {
4814 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4815 >                for (Node<K,V> p; (p = it.advance()) != null; ) {
4816 >                    h += p.hashCode();
4817 >                }
4818 >            }
4819 >            return h;
4820 >        }
4821 >
4822 >        public final boolean equals(Object o) {
4823 >            Set<?> c;
4824 >            return ((o instanceof Set) &&
4825 >                    ((c = (Set<?>)o) == this ||
4826 >                     (containsAll(c) && c.containsAll(this))));
4827 >        }
4828 >
4829 >        public Spliterator<Map.Entry<K,V>> spliterator() {
4830 >            Node<K,V>[] t;
4831 >            ConcurrentHashMap<K,V> m = map;
4832 >            long n = m.sumCount();
4833 >            int f = (t = m.table) == null ? 0 : t.length;
4834 >            return new EntrySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n, m);
4835 >        }
4836 >
4837 >        public void forEach(Consumer<? super Map.Entry<K,V>> action) {
4838 >            if (action == null) throw new NullPointerException();
4839 >            Node<K,V>[] t;
4840 >            if ((t = map.table) != null) {
4841 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4842 >                for (Node<K,V> p; (p = it.advance()) != null; )
4843 >                    action.accept(new MapEntry<K,V>(p.key, p.val, map));
4844 >            }
4845 >        }
4846 >
4847 >    }
4848 >
4849 >    // -------------------------------------------------------
4850 >
4851 >    /**
4852 >     * Base class for bulk tasks. Repeats some fields and code from
4853 >     * class Traverser, because we need to subclass CountedCompleter.
4854 >     */
4855 >    @SuppressWarnings("serial")
4856 >    abstract static class BulkTask<K,V,R> extends CountedCompleter<R> {
4857 >        Node<K,V>[] tab;        // same as Traverser
4858 >        Node<K,V> next;
4859 >        TableStack<K,V> stack, spare;
4860 >        int index;
4861 >        int baseIndex;
4862 >        int baseLimit;
4863 >        final int baseSize;
4864 >        int batch;              // split control
4865 >
4866 >        BulkTask(BulkTask<K,V,?> par, int b, int i, int f, Node<K,V>[] t) {
4867 >            super(par);
4868 >            this.batch = b;
4869 >            this.index = this.baseIndex = i;
4870 >            if ((this.tab = t) == null)
4871 >                this.baseSize = this.baseLimit = 0;
4872 >            else if (par == null)
4873 >                this.baseSize = this.baseLimit = t.length;
4874 >            else {
4875 >                this.baseLimit = f;
4876 >                this.baseSize = par.baseSize;
4877 >            }
4878 >        }
4879  
4880          /**
4881 <         * Return exportable snapshot entry
4881 >         * Same as Traverser version.
4882           */
4883 <        static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
4884 <            return new AbstractMap.SimpleEntry(k, v);
4883 >        final Node<K,V> advance() {
4884 >            Node<K,V> e;
4885 >            if ((e = next) != null)
4886 >                e = e.next;
4887 >            for (;;) {
4888 >                Node<K,V>[] t; int i, n;
4889 >                if (e != null)
4890 >                    return next = e;
4891 >                if (baseIndex >= baseLimit || (t = tab) == null ||
4892 >                    (n = t.length) <= (i = index) || i < 0)
4893 >                    return next = null;
4894 >                if ((e = tabAt(t, i)) != null && e.hash < 0) {
4895 >                    if (e instanceof ForwardingNode) {
4896 >                        tab = ((ForwardingNode<K,V>)e).nextTable;
4897 >                        e = null;
4898 >                        pushState(t, i, n);
4899 >                        continue;
4900 >                    }
4901 >                    else if (e instanceof TreeBin)
4902 >                        e = ((TreeBin<K,V>)e).first;
4903 >                    else
4904 >                        e = null;
4905 >                }
4906 >                if (stack != null)
4907 >                    recoverState(n);
4908 >                else if ((index = i + baseSize) >= n)
4909 >                    index = ++baseIndex;
4910 >            }
4911          }
4912  
4913 <        // Unsafe mechanics
4914 <        private static final sun.misc.Unsafe U;
4915 <        private static final long PENDING;
4916 <        static {
4917 <            try {
4918 <                U = sun.misc.Unsafe.getUnsafe();
4919 <                PENDING = U.objectFieldOffset
4920 <                    (BulkTask.class.getDeclaredField("pending"));
4921 <            } catch (Exception e) {
4922 <                throw new Error(e);
4913 >        private void pushState(Node<K,V>[] t, int i, int n) {
4914 >            TableStack<K,V> s = spare;
4915 >            if (s != null)
4916 >                spare = s.next;
4917 >            else
4918 >                s = new TableStack<K,V>();
4919 >            s.tab = t;
4920 >            s.length = n;
4921 >            s.index = i;
4922 >            s.next = stack;
4923 >            stack = s;
4924 >        }
4925 >
4926 >        private void recoverState(int n) {
4927 >            TableStack<K,V> s; int len;
4928 >            while ((s = stack) != null && (index += (len = s.length)) >= n) {
4929 >                n = len;
4930 >                index = s.index;
4931 >                tab = s.tab;
4932 >                s.tab = null;
4933 >                TableStack<K,V> next = s.next;
4934 >                s.next = spare; // save for reuse
4935 >                stack = next;
4936 >                spare = s;
4937              }
4938 +            if (s == null && (index += baseSize) >= n)
4939 +                index = ++baseIndex;
4940          }
4941      }
4942  
4943      /*
4944       * Task classes. Coded in a regular but ugly format/style to
4945       * simplify checks that each variant differs in the right way from
4946 <     * others.
4946 >     * others. The null screenings exist because compilers cannot tell
4947 >     * that we've already null-checked task arguments, so we force
4948 >     * simplest hoisted bypass to help avoid convoluted traps.
4949       */
4950 <
4950 >    @SuppressWarnings("serial")
4951      static final class ForEachKeyTask<K,V>
4952          extends BulkTask<K,V,Void> {
4953 <        final Action<K> action;
4948 <        ForEachKeyTask
4949 <            (ConcurrentHashMap<K,V> m,
4950 <             Action<K> action) {
4951 <            super(m);
4952 <            this.action = action;
4953 <        }
4953 >        final Consumer<? super K> action;
4954          ForEachKeyTask
4955 <            (BulkTask<K,V,?> p, int b, boolean split,
4956 <             Action<K> action) {
4957 <            super(p, b, split);
4955 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4956 >             Consumer<? super K> action) {
4957 >            super(p, b, i, f, t);
4958              this.action = action;
4959          }
4960          public final void compute() {
4961 <            final Action<K> action = this.action;
4962 <            if (action == null)
4963 <                throw new Error(NullFunctionMessage);
4964 <            int b = batch(), c;
4965 <            while (b > 1 && baseIndex != baseLimit) {
4966 <                do {} while (!casPending(c = pending, c+1));
4967 <                new ForEachKeyTask<K,V>(this, b >>>= 1, true, action).fork();
4968 <            }
4969 <            while (advance() != null)
4970 <                action.apply((K)nextKey);
4971 <            tryComplete();
4961 >            final Consumer<? super K> action;
4962 >            if ((action = this.action) != null) {
4963 >                for (int i = baseIndex, f, h; batch > 0 &&
4964 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4965 >                    addToPendingCount(1);
4966 >                    new ForEachKeyTask<K,V>
4967 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4968 >                         action).fork();
4969 >                }
4970 >                for (Node<K,V> p; (p = advance()) != null;)
4971 >                    action.accept(p.key);
4972 >                propagateCompletion();
4973 >            }
4974          }
4975      }
4976  
4977 +    @SuppressWarnings("serial")
4978      static final class ForEachValueTask<K,V>
4979          extends BulkTask<K,V,Void> {
4980 <        final Action<V> action;
4978 <        ForEachValueTask
4979 <            (ConcurrentHashMap<K,V> m,
4980 <             Action<V> action) {
4981 <            super(m);
4982 <            this.action = action;
4983 <        }
4980 >        final Consumer<? super V> action;
4981          ForEachValueTask
4982 <            (BulkTask<K,V,?> p, int b, boolean split,
4983 <             Action<V> action) {
4984 <            super(p, b, split);
4982 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4983 >             Consumer<? super V> action) {
4984 >            super(p, b, i, f, t);
4985              this.action = action;
4986          }
4987          public final void compute() {
4988 <            final Action<V> action = this.action;
4989 <            if (action == null)
4990 <                throw new Error(NullFunctionMessage);
4991 <            int b = batch(), c;
4992 <            while (b > 1 && baseIndex != baseLimit) {
4993 <                do {} while (!casPending(c = pending, c+1));
4994 <                new ForEachValueTask<K,V>(this, b >>>= 1, true, action).fork();
4995 <            }
4996 <            Object v;
4997 <            while ((v = advance()) != null)
4998 <                action.apply((V)v);
4999 <            tryComplete();
4988 >            final Consumer<? super V> action;
4989 >            if ((action = this.action) != null) {
4990 >                for (int i = baseIndex, f, h; batch > 0 &&
4991 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4992 >                    addToPendingCount(1);
4993 >                    new ForEachValueTask<K,V>
4994 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4995 >                         action).fork();
4996 >                }
4997 >                for (Node<K,V> p; (p = advance()) != null;)
4998 >                    action.accept(p.val);
4999 >                propagateCompletion();
5000 >            }
5001          }
5002      }
5003  
5004 +    @SuppressWarnings("serial")
5005      static final class ForEachEntryTask<K,V>
5006          extends BulkTask<K,V,Void> {
5007 <        final Action<Entry<K,V>> action;
5007 >        final Consumer<? super Entry<K,V>> action;
5008          ForEachEntryTask
5009 <            (ConcurrentHashMap<K,V> m,
5010 <             Action<Entry<K,V>> action) {
5011 <            super(m);
5013 <            this.action = action;
5014 <        }
5015 <        ForEachEntryTask
5016 <            (BulkTask<K,V,?> p, int b, boolean split,
5017 <             Action<Entry<K,V>> action) {
5018 <            super(p, b, split);
5009 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5010 >             Consumer<? super Entry<K,V>> action) {
5011 >            super(p, b, i, f, t);
5012              this.action = action;
5013          }
5014          public final void compute() {
5015 <            final Action<Entry<K,V>> action = this.action;
5016 <            if (action == null)
5017 <                throw new Error(NullFunctionMessage);
5018 <            int b = batch(), c;
5019 <            while (b > 1 && baseIndex != baseLimit) {
5020 <                do {} while (!casPending(c = pending, c+1));
5021 <                new ForEachEntryTask<K,V>(this, b >>>= 1, true, action).fork();
5022 <            }
5023 <            Object v;
5024 <            while ((v = advance()) != null)
5025 <                action.apply(entryFor((K)nextKey, (V)v));
5026 <            tryComplete();
5015 >            final Consumer<? super Entry<K,V>> action;
5016 >            if ((action = this.action) != null) {
5017 >                for (int i = baseIndex, f, h; batch > 0 &&
5018 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5019 >                    addToPendingCount(1);
5020 >                    new ForEachEntryTask<K,V>
5021 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5022 >                         action).fork();
5023 >                }
5024 >                for (Node<K,V> p; (p = advance()) != null; )
5025 >                    action.accept(p);
5026 >                propagateCompletion();
5027 >            }
5028          }
5029      }
5030  
5031 +    @SuppressWarnings("serial")
5032      static final class ForEachMappingTask<K,V>
5033          extends BulkTask<K,V,Void> {
5034 <        final BiAction<K,V> action;
5040 <        ForEachMappingTask
5041 <            (ConcurrentHashMap<K,V> m,
5042 <             BiAction<K,V> action) {
5043 <            super(m);
5044 <            this.action = action;
5045 <        }
5034 >        final BiConsumer<? super K, ? super V> action;
5035          ForEachMappingTask
5036 <            (BulkTask<K,V,?> p, int b, boolean split,
5037 <             BiAction<K,V> action) {
5038 <            super(p, b, split);
5036 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5037 >             BiConsumer<? super K,? super V> action) {
5038 >            super(p, b, i, f, t);
5039              this.action = action;
5040          }
5052
5041          public final void compute() {
5042 <            final BiAction<K,V> action = this.action;
5043 <            if (action == null)
5044 <                throw new Error(NullFunctionMessage);
5045 <            int b = batch(), c;
5046 <            while (b > 1 && baseIndex != baseLimit) {
5047 <                do {} while (!casPending(c = pending, c+1));
5048 <                new ForEachMappingTask<K,V>(this, b >>>= 1, true,
5049 <                                            action).fork();
5050 <            }
5051 <            Object v;
5052 <            while ((v = advance()) != null)
5053 <                action.apply((K)nextKey, (V)v);
5054 <            tryComplete();
5042 >            final BiConsumer<? super K, ? super V> action;
5043 >            if ((action = this.action) != null) {
5044 >                for (int i = baseIndex, f, h; batch > 0 &&
5045 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5046 >                    addToPendingCount(1);
5047 >                    new ForEachMappingTask<K,V>
5048 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5049 >                         action).fork();
5050 >                }
5051 >                for (Node<K,V> p; (p = advance()) != null; )
5052 >                    action.accept(p.key, p.val);
5053 >                propagateCompletion();
5054 >            }
5055          }
5056      }
5057  
5058 +    @SuppressWarnings("serial")
5059      static final class ForEachTransformedKeyTask<K,V,U>
5060          extends BulkTask<K,V,Void> {
5061 <        final Fun<? super K, ? extends U> transformer;
5062 <        final Action<U> action;
5074 <        ForEachTransformedKeyTask
5075 <            (ConcurrentHashMap<K,V> m,
5076 <             Fun<? super K, ? extends U> transformer,
5077 <             Action<U> action) {
5078 <            super(m);
5079 <            this.transformer = transformer;
5080 <            this.action = action;
5081 <
5082 <        }
5061 >        final Function<? super K, ? extends U> transformer;
5062 >        final Consumer<? super U> action;
5063          ForEachTransformedKeyTask
5064 <            (BulkTask<K,V,?> p, int b, boolean split,
5065 <             Fun<? super K, ? extends U> transformer,
5066 <             Action<U> action) {
5067 <            super(p, b, split);
5088 <            this.transformer = transformer;
5089 <            this.action = action;
5064 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5065 >             Function<? super K, ? extends U> transformer, Consumer<? super U> action) {
5066 >            super(p, b, i, f, t);
5067 >            this.transformer = transformer; this.action = action;
5068          }
5069          public final void compute() {
5070 <            final Fun<? super K, ? extends U> transformer =
5071 <                this.transformer;
5072 <            final Action<U> action = this.action;
5073 <            if (transformer == null || action == null)
5074 <                throw new Error(NullFunctionMessage);
5075 <            int b = batch(), c;
5076 <            while (b > 1 && baseIndex != baseLimit) {
5077 <                do {} while (!casPending(c = pending, c+1));
5078 <                new ForEachTransformedKeyTask<K,V,U>
5079 <                    (this, b >>>= 1, true, transformer, action).fork();
5080 <            }
5081 <            U u;
5082 <            while (advance() != null) {
5083 <                if ((u = transformer.apply((K)nextKey)) != null)
5084 <                    action.apply(u);
5070 >            final Function<? super K, ? extends U> transformer;
5071 >            final Consumer<? super U> action;
5072 >            if ((transformer = this.transformer) != null &&
5073 >                (action = this.action) != null) {
5074 >                for (int i = baseIndex, f, h; batch > 0 &&
5075 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5076 >                    addToPendingCount(1);
5077 >                    new ForEachTransformedKeyTask<K,V,U>
5078 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5079 >                         transformer, action).fork();
5080 >                }
5081 >                for (Node<K,V> p; (p = advance()) != null; ) {
5082 >                    U u;
5083 >                    if ((u = transformer.apply(p.key)) != null)
5084 >                        action.accept(u);
5085 >                }
5086 >                propagateCompletion();
5087              }
5108            tryComplete();
5088          }
5089      }
5090  
5091 +    @SuppressWarnings("serial")
5092      static final class ForEachTransformedValueTask<K,V,U>
5093          extends BulkTask<K,V,Void> {
5094 <        final Fun<? super V, ? extends U> transformer;
5095 <        final Action<U> action;
5094 >        final Function<? super V, ? extends U> transformer;
5095 >        final Consumer<? super U> action;
5096          ForEachTransformedValueTask
5097 <            (ConcurrentHashMap<K,V> m,
5098 <             Fun<? super V, ? extends U> transformer,
5099 <             Action<U> action) {
5100 <            super(m);
5121 <            this.transformer = transformer;
5122 <            this.action = action;
5123 <
5124 <        }
5125 <        ForEachTransformedValueTask
5126 <            (BulkTask<K,V,?> p, int b, boolean split,
5127 <             Fun<? super V, ? extends U> transformer,
5128 <             Action<U> action) {
5129 <            super(p, b, split);
5130 <            this.transformer = transformer;
5131 <            this.action = action;
5097 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5098 >             Function<? super V, ? extends U> transformer, Consumer<? super U> action) {
5099 >            super(p, b, i, f, t);
5100 >            this.transformer = transformer; this.action = action;
5101          }
5102          public final void compute() {
5103 <            final Fun<? super V, ? extends U> transformer =
5104 <                this.transformer;
5105 <            final Action<U> action = this.action;
5106 <            if (transformer == null || action == null)
5107 <                throw new Error(NullFunctionMessage);
5108 <            int b = batch(), c;
5109 <            while (b > 1 && baseIndex != baseLimit) {
5110 <                do {} while (!casPending(c = pending, c+1));
5111 <                new ForEachTransformedValueTask<K,V,U>
5112 <                    (this, b >>>= 1, true, transformer, action).fork();
5113 <            }
5114 <            Object v; U u;
5115 <            while ((v = advance()) != null) {
5116 <                if ((u = transformer.apply((V)v)) != null)
5117 <                    action.apply(u);
5103 >            final Function<? super V, ? extends U> transformer;
5104 >            final Consumer<? super U> action;
5105 >            if ((transformer = this.transformer) != null &&
5106 >                (action = this.action) != null) {
5107 >                for (int i = baseIndex, f, h; batch > 0 &&
5108 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5109 >                    addToPendingCount(1);
5110 >                    new ForEachTransformedValueTask<K,V,U>
5111 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5112 >                         transformer, action).fork();
5113 >                }
5114 >                for (Node<K,V> p; (p = advance()) != null; ) {
5115 >                    U u;
5116 >                    if ((u = transformer.apply(p.val)) != null)
5117 >                        action.accept(u);
5118 >                }
5119 >                propagateCompletion();
5120              }
5150            tryComplete();
5121          }
5122      }
5123  
5124 +    @SuppressWarnings("serial")
5125      static final class ForEachTransformedEntryTask<K,V,U>
5126          extends BulkTask<K,V,Void> {
5127 <        final Fun<Map.Entry<K,V>, ? extends U> transformer;
5128 <        final Action<U> action;
5127 >        final Function<Map.Entry<K,V>, ? extends U> transformer;
5128 >        final Consumer<? super U> action;
5129          ForEachTransformedEntryTask
5130 <            (ConcurrentHashMap<K,V> m,
5131 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5132 <             Action<U> action) {
5133 <            super(m);
5163 <            this.transformer = transformer;
5164 <            this.action = action;
5165 <
5166 <        }
5167 <        ForEachTransformedEntryTask
5168 <            (BulkTask<K,V,?> p, int b, boolean split,
5169 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5170 <             Action<U> action) {
5171 <            super(p, b, split);
5172 <            this.transformer = transformer;
5173 <            this.action = action;
5130 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5131 >             Function<Map.Entry<K,V>, ? extends U> transformer, Consumer<? super U> action) {
5132 >            super(p, b, i, f, t);
5133 >            this.transformer = transformer; this.action = action;
5134          }
5135          public final void compute() {
5136 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5137 <                this.transformer;
5138 <            final Action<U> action = this.action;
5139 <            if (transformer == null || action == null)
5140 <                throw new Error(NullFunctionMessage);
5141 <            int b = batch(), c;
5142 <            while (b > 1 && baseIndex != baseLimit) {
5143 <                do {} while (!casPending(c = pending, c+1));
5144 <                new ForEachTransformedEntryTask<K,V,U>
5145 <                    (this, b >>>= 1, true, transformer, action).fork();
5146 <            }
5147 <            Object v; U u;
5148 <            while ((v = advance()) != null) {
5149 <                if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5150 <                    action.apply(u);
5136 >            final Function<Map.Entry<K,V>, ? extends U> transformer;
5137 >            final Consumer<? super U> action;
5138 >            if ((transformer = this.transformer) != null &&
5139 >                (action = this.action) != null) {
5140 >                for (int i = baseIndex, f, h; batch > 0 &&
5141 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5142 >                    addToPendingCount(1);
5143 >                    new ForEachTransformedEntryTask<K,V,U>
5144 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5145 >                         transformer, action).fork();
5146 >                }
5147 >                for (Node<K,V> p; (p = advance()) != null; ) {
5148 >                    U u;
5149 >                    if ((u = transformer.apply(p)) != null)
5150 >                        action.accept(u);
5151 >                }
5152 >                propagateCompletion();
5153              }
5192            tryComplete();
5154          }
5155      }
5156  
5157 +    @SuppressWarnings("serial")
5158      static final class ForEachTransformedMappingTask<K,V,U>
5159          extends BulkTask<K,V,Void> {
5160 <        final BiFun<? super K, ? super V, ? extends U> transformer;
5161 <        final Action<U> action;
5160 >        final BiFunction<? super K, ? super V, ? extends U> transformer;
5161 >        final Consumer<? super U> action;
5162          ForEachTransformedMappingTask
5163 <            (ConcurrentHashMap<K,V> m,
5164 <             BiFun<? super K, ? super V, ? extends U> transformer,
5165 <             Action<U> action) {
5166 <            super(m);
5167 <            this.transformer = transformer;
5206 <            this.action = action;
5207 <
5208 <        }
5209 <        ForEachTransformedMappingTask
5210 <            (BulkTask<K,V,?> p, int b, boolean split,
5211 <             BiFun<? super K, ? super V, ? extends U> transformer,
5212 <             Action<U> action) {
5213 <            super(p, b, split);
5214 <            this.transformer = transformer;
5215 <            this.action = action;
5163 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5164 >             BiFunction<? super K, ? super V, ? extends U> transformer,
5165 >             Consumer<? super U> action) {
5166 >            super(p, b, i, f, t);
5167 >            this.transformer = transformer; this.action = action;
5168          }
5169          public final void compute() {
5170 <            final BiFun<? super K, ? super V, ? extends U> transformer =
5171 <                this.transformer;
5172 <            final Action<U> action = this.action;
5173 <            if (transformer == null || action == null)
5174 <                throw new Error(NullFunctionMessage);
5175 <            int b = batch(), c;
5176 <            while (b > 1 && baseIndex != baseLimit) {
5177 <                do {} while (!casPending(c = pending, c+1));
5178 <                new ForEachTransformedMappingTask<K,V,U>
5179 <                    (this, b >>>= 1, true, transformer, action).fork();
5180 <            }
5181 <            Object v; U u;
5182 <            while ((v = advance()) != null) {
5183 <                if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5184 <                    action.apply(u);
5170 >            final BiFunction<? super K, ? super V, ? extends U> transformer;
5171 >            final Consumer<? super U> action;
5172 >            if ((transformer = this.transformer) != null &&
5173 >                (action = this.action) != null) {
5174 >                for (int i = baseIndex, f, h; batch > 0 &&
5175 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5176 >                    addToPendingCount(1);
5177 >                    new ForEachTransformedMappingTask<K,V,U>
5178 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5179 >                         transformer, action).fork();
5180 >                }
5181 >                for (Node<K,V> p; (p = advance()) != null; ) {
5182 >                    U u;
5183 >                    if ((u = transformer.apply(p.key, p.val)) != null)
5184 >                        action.accept(u);
5185 >                }
5186 >                propagateCompletion();
5187              }
5234            tryComplete();
5188          }
5189      }
5190  
5191 +    @SuppressWarnings("serial")
5192      static final class SearchKeysTask<K,V,U>
5193          extends BulkTask<K,V,U> {
5194 <        final Fun<? super K, ? extends U> searchFunction;
5194 >        final Function<? super K, ? extends U> searchFunction;
5195          final AtomicReference<U> result;
5196          SearchKeysTask
5197 <            (ConcurrentHashMap<K,V> m,
5198 <             Fun<? super K, ? extends U> searchFunction,
5197 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5198 >             Function<? super K, ? extends U> searchFunction,
5199               AtomicReference<U> result) {
5200 <            super(m);
5247 <            this.searchFunction = searchFunction; this.result = result;
5248 <        }
5249 <        SearchKeysTask
5250 <            (BulkTask<K,V,?> p, int b, boolean split,
5251 <             Fun<? super K, ? extends U> searchFunction,
5252 <             AtomicReference<U> result) {
5253 <            super(p, b, split);
5200 >            super(p, b, i, f, t);
5201              this.searchFunction = searchFunction; this.result = result;
5202          }
5203 +        public final U getRawResult() { return result.get(); }
5204          public final void compute() {
5205 <            AtomicReference<U> result = this.result;
5206 <            final Fun<? super K, ? extends U> searchFunction =
5207 <                this.searchFunction;
5208 <            if (searchFunction == null || result == null)
5209 <                throw new Error(NullFunctionMessage);
5210 <            int b = batch(), c;
5211 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5212 <                do {} while (!casPending(c = pending, c+1));
5213 <                new SearchKeysTask<K,V,U>(this, b >>>= 1, true,
5214 <                                          searchFunction, result).fork();
5215 <            }
5216 <            U u;
5217 <            while (result.get() == null && advance() != null) {
5218 <                if ((u = searchFunction.apply((K)nextKey)) != null) {
5219 <                    result.compareAndSet(null, u);
5220 <                    break;
5205 >            final Function<? super K, ? extends U> searchFunction;
5206 >            final AtomicReference<U> result;
5207 >            if ((searchFunction = this.searchFunction) != null &&
5208 >                (result = this.result) != null) {
5209 >                for (int i = baseIndex, f, h; batch > 0 &&
5210 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5211 >                    if (result.get() != null)
5212 >                        return;
5213 >                    addToPendingCount(1);
5214 >                    new SearchKeysTask<K,V,U>
5215 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5216 >                         searchFunction, result).fork();
5217 >                }
5218 >                while (result.get() == null) {
5219 >                    U u;
5220 >                    Node<K,V> p;
5221 >                    if ((p = advance()) == null) {
5222 >                        propagateCompletion();
5223 >                        break;
5224 >                    }
5225 >                    if ((u = searchFunction.apply(p.key)) != null) {
5226 >                        if (result.compareAndSet(null, u))
5227 >                            quietlyCompleteRoot();
5228 >                        break;
5229 >                    }
5230                  }
5231              }
5275            tryComplete();
5232          }
5277        public final U getRawResult() { return result.get(); }
5233      }
5234  
5235 +    @SuppressWarnings("serial")
5236      static final class SearchValuesTask<K,V,U>
5237          extends BulkTask<K,V,U> {
5238 <        final Fun<? super V, ? extends U> searchFunction;
5238 >        final Function<? super V, ? extends U> searchFunction;
5239          final AtomicReference<U> result;
5240          SearchValuesTask
5241 <            (ConcurrentHashMap<K,V> m,
5242 <             Fun<? super V, ? extends U> searchFunction,
5241 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5242 >             Function<? super V, ? extends U> searchFunction,
5243               AtomicReference<U> result) {
5244 <            super(m);
5289 <            this.searchFunction = searchFunction; this.result = result;
5290 <        }
5291 <        SearchValuesTask
5292 <            (BulkTask<K,V,?> p, int b, boolean split,
5293 <             Fun<? super V, ? extends U> searchFunction,
5294 <             AtomicReference<U> result) {
5295 <            super(p, b, split);
5244 >            super(p, b, i, f, t);
5245              this.searchFunction = searchFunction; this.result = result;
5246          }
5247 +        public final U getRawResult() { return result.get(); }
5248          public final void compute() {
5249 <            AtomicReference<U> result = this.result;
5250 <            final Fun<? super V, ? extends U> searchFunction =
5251 <                this.searchFunction;
5252 <            if (searchFunction == null || result == null)
5253 <                throw new Error(NullFunctionMessage);
5254 <            int b = batch(), c;
5255 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5256 <                do {} while (!casPending(c = pending, c+1));
5257 <                new SearchValuesTask<K,V,U>(this, b >>>= 1, true,
5258 <                                            searchFunction, result).fork();
5259 <            }
5260 <            Object v; U u;
5261 <            while (result.get() == null && (v = advance()) != null) {
5262 <                if ((u = searchFunction.apply((V)v)) != null) {
5263 <                    result.compareAndSet(null, u);
5264 <                    break;
5249 >            final Function<? super V, ? extends U> searchFunction;
5250 >            final AtomicReference<U> result;
5251 >            if ((searchFunction = this.searchFunction) != null &&
5252 >                (result = this.result) != null) {
5253 >                for (int i = baseIndex, f, h; batch > 0 &&
5254 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5255 >                    if (result.get() != null)
5256 >                        return;
5257 >                    addToPendingCount(1);
5258 >                    new SearchValuesTask<K,V,U>
5259 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5260 >                         searchFunction, result).fork();
5261 >                }
5262 >                while (result.get() == null) {
5263 >                    U u;
5264 >                    Node<K,V> p;
5265 >                    if ((p = advance()) == null) {
5266 >                        propagateCompletion();
5267 >                        break;
5268 >                    }
5269 >                    if ((u = searchFunction.apply(p.val)) != null) {
5270 >                        if (result.compareAndSet(null, u))
5271 >                            quietlyCompleteRoot();
5272 >                        break;
5273 >                    }
5274                  }
5275              }
5317            tryComplete();
5276          }
5319        public final U getRawResult() { return result.get(); }
5277      }
5278  
5279 +    @SuppressWarnings("serial")
5280      static final class SearchEntriesTask<K,V,U>
5281          extends BulkTask<K,V,U> {
5282 <        final Fun<Entry<K,V>, ? extends U> searchFunction;
5282 >        final Function<Entry<K,V>, ? extends U> searchFunction;
5283          final AtomicReference<U> result;
5284          SearchEntriesTask
5285 <            (ConcurrentHashMap<K,V> m,
5286 <             Fun<Entry<K,V>, ? extends U> searchFunction,
5285 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5286 >             Function<Entry<K,V>, ? extends U> searchFunction,
5287               AtomicReference<U> result) {
5288 <            super(m);
5331 <            this.searchFunction = searchFunction; this.result = result;
5332 <        }
5333 <        SearchEntriesTask
5334 <            (BulkTask<K,V,?> p, int b, boolean split,
5335 <             Fun<Entry<K,V>, ? extends U> searchFunction,
5336 <             AtomicReference<U> result) {
5337 <            super(p, b, split);
5288 >            super(p, b, i, f, t);
5289              this.searchFunction = searchFunction; this.result = result;
5290          }
5291 +        public final U getRawResult() { return result.get(); }
5292          public final void compute() {
5293 <            AtomicReference<U> result = this.result;
5294 <            final Fun<Entry<K,V>, ? extends U> searchFunction =
5295 <                this.searchFunction;
5296 <            if (searchFunction == null || result == null)
5297 <                throw new Error(NullFunctionMessage);
5298 <            int b = batch(), c;
5299 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5300 <                do {} while (!casPending(c = pending, c+1));
5301 <                new SearchEntriesTask<K,V,U>(this, b >>>= 1, true,
5302 <                                             searchFunction, result).fork();
5303 <            }
5304 <            Object v; U u;
5305 <            while (result.get() == null && (v = advance()) != null) {
5306 <                if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
5307 <                    result.compareAndSet(null, u);
5308 <                    break;
5293 >            final Function<Entry<K,V>, ? extends U> searchFunction;
5294 >            final AtomicReference<U> result;
5295 >            if ((searchFunction = this.searchFunction) != null &&
5296 >                (result = this.result) != null) {
5297 >                for (int i = baseIndex, f, h; batch > 0 &&
5298 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5299 >                    if (result.get() != null)
5300 >                        return;
5301 >                    addToPendingCount(1);
5302 >                    new SearchEntriesTask<K,V,U>
5303 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5304 >                         searchFunction, result).fork();
5305 >                }
5306 >                while (result.get() == null) {
5307 >                    U u;
5308 >                    Node<K,V> p;
5309 >                    if ((p = advance()) == null) {
5310 >                        propagateCompletion();
5311 >                        break;
5312 >                    }
5313 >                    if ((u = searchFunction.apply(p)) != null) {
5314 >                        if (result.compareAndSet(null, u))
5315 >                            quietlyCompleteRoot();
5316 >                        return;
5317 >                    }
5318                  }
5319              }
5359            tryComplete();
5320          }
5361        public final U getRawResult() { return result.get(); }
5321      }
5322  
5323 +    @SuppressWarnings("serial")
5324      static final class SearchMappingsTask<K,V,U>
5325          extends BulkTask<K,V,U> {
5326 <        final BiFun<? super K, ? super V, ? extends U> searchFunction;
5326 >        final BiFunction<? super K, ? super V, ? extends U> searchFunction;
5327          final AtomicReference<U> result;
5328          SearchMappingsTask
5329 <            (ConcurrentHashMap<K,V> m,
5330 <             BiFun<? super K, ? super V, ? extends U> searchFunction,
5371 <             AtomicReference<U> result) {
5372 <            super(m);
5373 <            this.searchFunction = searchFunction; this.result = result;
5374 <        }
5375 <        SearchMappingsTask
5376 <            (BulkTask<K,V,?> p, int b, boolean split,
5377 <             BiFun<? super K, ? super V, ? extends U> searchFunction,
5329 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5330 >             BiFunction<? super K, ? super V, ? extends U> searchFunction,
5331               AtomicReference<U> result) {
5332 <            super(p, b, split);
5332 >            super(p, b, i, f, t);
5333              this.searchFunction = searchFunction; this.result = result;
5334          }
5335 +        public final U getRawResult() { return result.get(); }
5336          public final void compute() {
5337 <            AtomicReference<U> result = this.result;
5338 <            final BiFun<? super K, ? super V, ? extends U> searchFunction =
5339 <                this.searchFunction;
5340 <            if (searchFunction == null || result == null)
5341 <                throw new Error(NullFunctionMessage);
5342 <            int b = batch(), c;
5343 <            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5344 <                do {} while (!casPending(c = pending, c+1));
5345 <                new SearchMappingsTask<K,V,U>(this, b >>>= 1, true,
5346 <                                              searchFunction, result).fork();
5347 <            }
5348 <            Object v; U u;
5349 <            while (result.get() == null && (v = advance()) != null) {
5350 <                if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5351 <                    result.compareAndSet(null, u);
5352 <                    break;
5337 >            final BiFunction<? super K, ? super V, ? extends U> searchFunction;
5338 >            final AtomicReference<U> result;
5339 >            if ((searchFunction = this.searchFunction) != null &&
5340 >                (result = this.result) != null) {
5341 >                for (int i = baseIndex, f, h; batch > 0 &&
5342 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5343 >                    if (result.get() != null)
5344 >                        return;
5345 >                    addToPendingCount(1);
5346 >                    new SearchMappingsTask<K,V,U>
5347 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5348 >                         searchFunction, result).fork();
5349 >                }
5350 >                while (result.get() == null) {
5351 >                    U u;
5352 >                    Node<K,V> p;
5353 >                    if ((p = advance()) == null) {
5354 >                        propagateCompletion();
5355 >                        break;
5356 >                    }
5357 >                    if ((u = searchFunction.apply(p.key, p.val)) != null) {
5358 >                        if (result.compareAndSet(null, u))
5359 >                            quietlyCompleteRoot();
5360 >                        break;
5361 >                    }
5362                  }
5363              }
5401            tryComplete();
5364          }
5403        public final U getRawResult() { return result.get(); }
5365      }
5366  
5367 +    @SuppressWarnings("serial")
5368      static final class ReduceKeysTask<K,V>
5369          extends BulkTask<K,V,K> {
5370 <        final BiFun<? super K, ? super K, ? extends K> reducer;
5370 >        final BiFunction<? super K, ? super K, ? extends K> reducer;
5371          K result;
5372 <        ReduceKeysTask<K,V> sibling;
5372 >        ReduceKeysTask<K,V> rights, nextRight;
5373          ReduceKeysTask
5374 <            (ConcurrentHashMap<K,V> m,
5375 <             BiFun<? super K, ? super K, ? extends K> reducer) {
5376 <            super(m);
5374 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5375 >             ReduceKeysTask<K,V> nextRight,
5376 >             BiFunction<? super K, ? super K, ? extends K> reducer) {
5377 >            super(p, b, i, f, t); this.nextRight = nextRight;
5378              this.reducer = reducer;
5379          }
5380 <        ReduceKeysTask
5418 <            (BulkTask<K,V,?> p, int b, boolean split,
5419 <             BiFun<? super K, ? super K, ? extends K> reducer) {
5420 <            super(p, b, split);
5421 <            this.reducer = reducer;
5422 <        }
5423 <
5380 >        public final K getRawResult() { return result; }
5381          public final void compute() {
5382 <            ReduceKeysTask<K,V> t = this;
5383 <            final BiFun<? super K, ? super K, ? extends K> reducer =
5384 <                this.reducer;
5385 <            if (reducer == null)
5386 <                throw new Error(NullFunctionMessage);
5387 <            int b = batch();
5388 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5389 <                b >>>= 1;
5390 <                t.pending = 1;
5391 <                ReduceKeysTask<K,V> rt =
5392 <                    new ReduceKeysTask<K,V>
5393 <                    (t, b, true, reducer);
5394 <                t = new ReduceKeysTask<K,V>
5395 <                    (t, b, false, reducer);
5396 <                t.sibling = rt;
5397 <                rt.sibling = t;
5398 <                rt.fork();
5399 <            }
5400 <            K r = null;
5401 <            while (t.advance() != null) {
5402 <                K u = (K)t.nextKey;
5403 <                r = (r == null) ? u : reducer.apply(r, u);
5404 <            }
5405 <            t.result = r;
5406 <            for (;;) {
5407 <                int c; BulkTask<K,V,?> par; ReduceKeysTask<K,V> s, p; K u;
5408 <                if ((par = t.parent) == null ||
5409 <                    !(par instanceof ReduceKeysTask)) {
5453 <                    t.quietlyComplete();
5454 <                    break;
5455 <                }
5456 <                else if ((c = (p = (ReduceKeysTask<K,V>)par).pending) == 0) {
5457 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5458 <                        r = (r == null) ? u : reducer.apply(r, u);
5459 <                    (t = p).result = r;
5382 >            final BiFunction<? super K, ? super K, ? extends K> reducer;
5383 >            if ((reducer = this.reducer) != null) {
5384 >                for (int i = baseIndex, f, h; batch > 0 &&
5385 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5386 >                    addToPendingCount(1);
5387 >                    (rights = new ReduceKeysTask<K,V>
5388 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5389 >                      rights, reducer)).fork();
5390 >                }
5391 >                K r = null;
5392 >                for (Node<K,V> p; (p = advance()) != null; ) {
5393 >                    K u = p.key;
5394 >                    r = (r == null) ? u : u == null ? r : reducer.apply(r, u);
5395 >                }
5396 >                result = r;
5397 >                CountedCompleter<?> c;
5398 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5399 >                    @SuppressWarnings("unchecked")
5400 >                    ReduceKeysTask<K,V>
5401 >                        t = (ReduceKeysTask<K,V>)c,
5402 >                        s = t.rights;
5403 >                    while (s != null) {
5404 >                        K tr, sr;
5405 >                        if ((sr = s.result) != null)
5406 >                            t.result = (((tr = t.result) == null) ? sr :
5407 >                                        reducer.apply(tr, sr));
5408 >                        s = t.rights = s.nextRight;
5409 >                    }
5410                  }
5461                else if (p.casPending(c, 0))
5462                    break;
5411              }
5412          }
5465        public final K getRawResult() { return result; }
5413      }
5414  
5415 +    @SuppressWarnings("serial")
5416      static final class ReduceValuesTask<K,V>
5417          extends BulkTask<K,V,V> {
5418 <        final BiFun<? super V, ? super V, ? extends V> reducer;
5418 >        final BiFunction<? super V, ? super V, ? extends V> reducer;
5419          V result;
5420 <        ReduceValuesTask<K,V> sibling;
5420 >        ReduceValuesTask<K,V> rights, nextRight;
5421          ReduceValuesTask
5422 <            (ConcurrentHashMap<K,V> m,
5423 <             BiFun<? super V, ? super V, ? extends V> reducer) {
5424 <            super(m);
5422 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5423 >             ReduceValuesTask<K,V> nextRight,
5424 >             BiFunction<? super V, ? super V, ? extends V> reducer) {
5425 >            super(p, b, i, f, t); this.nextRight = nextRight;
5426              this.reducer = reducer;
5427          }
5428 <        ReduceValuesTask
5480 <            (BulkTask<K,V,?> p, int b, boolean split,
5481 <             BiFun<? super V, ? super V, ? extends V> reducer) {
5482 <            super(p, b, split);
5483 <            this.reducer = reducer;
5484 <        }
5485 <
5428 >        public final V getRawResult() { return result; }
5429          public final void compute() {
5430 <            ReduceValuesTask<K,V> t = this;
5431 <            final BiFun<? super V, ? super V, ? extends V> reducer =
5432 <                this.reducer;
5433 <            if (reducer == null)
5434 <                throw new Error(NullFunctionMessage);
5435 <            int b = batch();
5436 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5437 <                b >>>= 1;
5438 <                t.pending = 1;
5439 <                ReduceValuesTask<K,V> rt =
5440 <                    new ReduceValuesTask<K,V>
5441 <                    (t, b, true, reducer);
5442 <                t = new ReduceValuesTask<K,V>
5443 <                    (t, b, false, reducer);
5444 <                t.sibling = rt;
5445 <                rt.sibling = t;
5446 <                rt.fork();
5447 <            }
5448 <            V r = null;
5449 <            Object v;
5450 <            while ((v = t.advance()) != null) {
5451 <                V u = (V)v;
5452 <                r = (r == null) ? u : reducer.apply(r, u);
5453 <            }
5454 <            t.result = r;
5455 <            for (;;) {
5456 <                int c; BulkTask<K,V,?> par; ReduceValuesTask<K,V> s, p; V u;
5457 <                if ((par = t.parent) == null ||
5515 <                    !(par instanceof ReduceValuesTask)) {
5516 <                    t.quietlyComplete();
5517 <                    break;
5518 <                }
5519 <                else if ((c = (p = (ReduceValuesTask<K,V>)par).pending) == 0) {
5520 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5521 <                        r = (r == null) ? u : reducer.apply(r, u);
5522 <                    (t = p).result = r;
5430 >            final BiFunction<? super V, ? super V, ? extends V> reducer;
5431 >            if ((reducer = this.reducer) != null) {
5432 >                for (int i = baseIndex, f, h; batch > 0 &&
5433 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5434 >                    addToPendingCount(1);
5435 >                    (rights = new ReduceValuesTask<K,V>
5436 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5437 >                      rights, reducer)).fork();
5438 >                }
5439 >                V r = null;
5440 >                for (Node<K,V> p; (p = advance()) != null; ) {
5441 >                    V v = p.val;
5442 >                    r = (r == null) ? v : reducer.apply(r, v);
5443 >                }
5444 >                result = r;
5445 >                CountedCompleter<?> c;
5446 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5447 >                    @SuppressWarnings("unchecked")
5448 >                    ReduceValuesTask<K,V>
5449 >                        t = (ReduceValuesTask<K,V>)c,
5450 >                        s = t.rights;
5451 >                    while (s != null) {
5452 >                        V tr, sr;
5453 >                        if ((sr = s.result) != null)
5454 >                            t.result = (((tr = t.result) == null) ? sr :
5455 >                                        reducer.apply(tr, sr));
5456 >                        s = t.rights = s.nextRight;
5457 >                    }
5458                  }
5524                else if (p.casPending(c, 0))
5525                    break;
5459              }
5460          }
5528        public final V getRawResult() { return result; }
5461      }
5462  
5463 +    @SuppressWarnings("serial")
5464      static final class ReduceEntriesTask<K,V>
5465          extends BulkTask<K,V,Map.Entry<K,V>> {
5466 <        final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5466 >        final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5467          Map.Entry<K,V> result;
5468 <        ReduceEntriesTask<K,V> sibling;
5468 >        ReduceEntriesTask<K,V> rights, nextRight;
5469          ReduceEntriesTask
5470 <            (ConcurrentHashMap<K,V> m,
5471 <             BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5472 <            super(m);
5470 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5471 >             ReduceEntriesTask<K,V> nextRight,
5472 >             BiFunction<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5473 >            super(p, b, i, f, t); this.nextRight = nextRight;
5474              this.reducer = reducer;
5475          }
5476 <        ReduceEntriesTask
5543 <            (BulkTask<K,V,?> p, int b, boolean split,
5544 <             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5545 <            super(p, b, split);
5546 <            this.reducer = reducer;
5547 <        }
5548 <
5476 >        public final Map.Entry<K,V> getRawResult() { return result; }
5477          public final void compute() {
5478 <            ReduceEntriesTask<K,V> t = this;
5479 <            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
5480 <                this.reducer;
5481 <            if (reducer == null)
5482 <                throw new Error(NullFunctionMessage);
5483 <            int b = batch();
5484 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5485 <                b >>>= 1;
5486 <                t.pending = 1;
5487 <                ReduceEntriesTask<K,V> rt =
5488 <                    new ReduceEntriesTask<K,V>
5489 <                    (t, b, true, reducer);
5490 <                t = new ReduceEntriesTask<K,V>
5491 <                    (t, b, false, reducer);
5492 <                t.sibling = rt;
5493 <                rt.sibling = t;
5494 <                rt.fork();
5495 <            }
5496 <            Map.Entry<K,V> r = null;
5497 <            Object v;
5498 <            while ((v = t.advance()) != null) {
5499 <                Map.Entry<K,V> u = entryFor((K)t.nextKey, (V)v);
5500 <                r = (r == null) ? u : reducer.apply(r, u);
5501 <            }
5502 <            t.result = r;
5503 <            for (;;) {
5576 <                int c; BulkTask<K,V,?> par; ReduceEntriesTask<K,V> s, p;
5577 <                Map.Entry<K,V> u;
5578 <                if ((par = t.parent) == null ||
5579 <                    !(par instanceof ReduceEntriesTask)) {
5580 <                    t.quietlyComplete();
5581 <                    break;
5582 <                }
5583 <                else if ((c = (p = (ReduceEntriesTask<K,V>)par).pending) == 0) {
5584 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5585 <                        r = (r == null) ? u : reducer.apply(r, u);
5586 <                    (t = p).result = r;
5478 >            final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5479 >            if ((reducer = this.reducer) != null) {
5480 >                for (int i = baseIndex, f, h; batch > 0 &&
5481 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5482 >                    addToPendingCount(1);
5483 >                    (rights = new ReduceEntriesTask<K,V>
5484 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5485 >                      rights, reducer)).fork();
5486 >                }
5487 >                Map.Entry<K,V> r = null;
5488 >                for (Node<K,V> p; (p = advance()) != null; )
5489 >                    r = (r == null) ? p : reducer.apply(r, p);
5490 >                result = r;
5491 >                CountedCompleter<?> c;
5492 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5493 >                    @SuppressWarnings("unchecked")
5494 >                    ReduceEntriesTask<K,V>
5495 >                        t = (ReduceEntriesTask<K,V>)c,
5496 >                        s = t.rights;
5497 >                    while (s != null) {
5498 >                        Map.Entry<K,V> tr, sr;
5499 >                        if ((sr = s.result) != null)
5500 >                            t.result = (((tr = t.result) == null) ? sr :
5501 >                                        reducer.apply(tr, sr));
5502 >                        s = t.rights = s.nextRight;
5503 >                    }
5504                  }
5588                else if (p.casPending(c, 0))
5589                    break;
5505              }
5506          }
5592        public final Map.Entry<K,V> getRawResult() { return result; }
5507      }
5508  
5509 +    @SuppressWarnings("serial")
5510      static final class MapReduceKeysTask<K,V,U>
5511          extends BulkTask<K,V,U> {
5512 <        final Fun<? super K, ? extends U> transformer;
5513 <        final BiFun<? super U, ? super U, ? extends U> reducer;
5512 >        final Function<? super K, ? extends U> transformer;
5513 >        final BiFunction<? super U, ? super U, ? extends U> reducer;
5514          U result;
5515 <        MapReduceKeysTask<K,V,U> sibling;
5515 >        MapReduceKeysTask<K,V,U> rights, nextRight;
5516          MapReduceKeysTask
5517 <            (ConcurrentHashMap<K,V> m,
5518 <             Fun<? super K, ? extends U> transformer,
5519 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5520 <            super(m);
5521 <            this.transformer = transformer;
5607 <            this.reducer = reducer;
5608 <        }
5609 <        MapReduceKeysTask
5610 <            (BulkTask<K,V,?> p, int b, boolean split,
5611 <             Fun<? super K, ? extends U> transformer,
5612 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5613 <            super(p, b, split);
5517 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5518 >             MapReduceKeysTask<K,V,U> nextRight,
5519 >             Function<? super K, ? extends U> transformer,
5520 >             BiFunction<? super U, ? super U, ? extends U> reducer) {
5521 >            super(p, b, i, f, t); this.nextRight = nextRight;
5522              this.transformer = transformer;
5523              this.reducer = reducer;
5524          }
5525 +        public final U getRawResult() { return result; }
5526          public final void compute() {
5527 <            MapReduceKeysTask<K,V,U> t = this;
5528 <            final Fun<? super K, ? extends U> transformer =
5529 <                this.transformer;
5530 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5531 <                this.reducer;
5532 <            if (transformer == null || reducer == null)
5533 <                throw new Error(NullFunctionMessage);
5534 <            int b = batch();
5535 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5536 <                b >>>= 1;
5537 <                t.pending = 1;
5538 <                MapReduceKeysTask<K,V,U> rt =
5539 <                    new MapReduceKeysTask<K,V,U>
5540 <                    (t, b, true, transformer, reducer);
5541 <                t = new MapReduceKeysTask<K,V,U>
5633 <                    (t, b, false, transformer, reducer);
5634 <                t.sibling = rt;
5635 <                rt.sibling = t;
5636 <                rt.fork();
5637 <            }
5638 <            U r = null, u;
5639 <            while (t.advance() != null) {
5640 <                if ((u = transformer.apply((K)t.nextKey)) != null)
5641 <                    r = (r == null) ? u : reducer.apply(r, u);
5642 <            }
5643 <            t.result = r;
5644 <            for (;;) {
5645 <                int c; BulkTask<K,V,?> par; MapReduceKeysTask<K,V,U> s, p;
5646 <                if ((par = t.parent) == null ||
5647 <                    !(par instanceof MapReduceKeysTask)) {
5648 <                    t.quietlyComplete();
5649 <                    break;
5650 <                }
5651 <                else if ((c = (p = (MapReduceKeysTask<K,V,U>)par).pending) == 0) {
5652 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5527 >            final Function<? super K, ? extends U> transformer;
5528 >            final BiFunction<? super U, ? super U, ? extends U> reducer;
5529 >            if ((transformer = this.transformer) != null &&
5530 >                (reducer = this.reducer) != null) {
5531 >                for (int i = baseIndex, f, h; batch > 0 &&
5532 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5533 >                    addToPendingCount(1);
5534 >                    (rights = new MapReduceKeysTask<K,V,U>
5535 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5536 >                      rights, transformer, reducer)).fork();
5537 >                }
5538 >                U r = null;
5539 >                for (Node<K,V> p; (p = advance()) != null; ) {
5540 >                    U u;
5541 >                    if ((u = transformer.apply(p.key)) != null)
5542                          r = (r == null) ? u : reducer.apply(r, u);
5654                    (t = p).result = r;
5543                  }
5544 <                else if (p.casPending(c, 0))
5545 <                    break;
5544 >                result = r;
5545 >                CountedCompleter<?> c;
5546 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5547 >                    @SuppressWarnings("unchecked")
5548 >                    MapReduceKeysTask<K,V,U>
5549 >                        t = (MapReduceKeysTask<K,V,U>)c,
5550 >                        s = t.rights;
5551 >                    while (s != null) {
5552 >                        U tr, sr;
5553 >                        if ((sr = s.result) != null)
5554 >                            t.result = (((tr = t.result) == null) ? sr :
5555 >                                        reducer.apply(tr, sr));
5556 >                        s = t.rights = s.nextRight;
5557 >                    }
5558 >                }
5559              }
5560          }
5660        public final U getRawResult() { return result; }
5561      }
5562  
5563 +    @SuppressWarnings("serial")
5564      static final class MapReduceValuesTask<K,V,U>
5565          extends BulkTask<K,V,U> {
5566 <        final Fun<? super V, ? extends U> transformer;
5567 <        final BiFun<? super U, ? super U, ? extends U> reducer;
5566 >        final Function<? super V, ? extends U> transformer;
5567 >        final BiFunction<? super U, ? super U, ? extends U> reducer;
5568          U result;
5569 <        MapReduceValuesTask<K,V,U> sibling;
5569 >        MapReduceValuesTask<K,V,U> rights, nextRight;
5570          MapReduceValuesTask
5571 <            (ConcurrentHashMap<K,V> m,
5572 <             Fun<? super V, ? extends U> transformer,
5573 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5574 <            super(m);
5575 <            this.transformer = transformer;
5675 <            this.reducer = reducer;
5676 <        }
5677 <        MapReduceValuesTask
5678 <            (BulkTask<K,V,?> p, int b, boolean split,
5679 <             Fun<? super V, ? extends U> transformer,
5680 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5681 <            super(p, b, split);
5571 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5572 >             MapReduceValuesTask<K,V,U> nextRight,
5573 >             Function<? super V, ? extends U> transformer,
5574 >             BiFunction<? super U, ? super U, ? extends U> reducer) {
5575 >            super(p, b, i, f, t); this.nextRight = nextRight;
5576              this.transformer = transformer;
5577              this.reducer = reducer;
5578          }
5579 +        public final U getRawResult() { return result; }
5580          public final void compute() {
5581 <            MapReduceValuesTask<K,V,U> t = this;
5582 <            final Fun<? super V, ? extends U> transformer =
5583 <                this.transformer;
5584 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5585 <                this.reducer;
5586 <            if (transformer == null || reducer == null)
5587 <                throw new Error(NullFunctionMessage);
5588 <            int b = batch();
5589 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5590 <                b >>>= 1;
5591 <                t.pending = 1;
5592 <                MapReduceValuesTask<K,V,U> rt =
5593 <                    new MapReduceValuesTask<K,V,U>
5594 <                    (t, b, true, transformer, reducer);
5595 <                t = new MapReduceValuesTask<K,V,U>
5701 <                    (t, b, false, transformer, reducer);
5702 <                t.sibling = rt;
5703 <                rt.sibling = t;
5704 <                rt.fork();
5705 <            }
5706 <            U r = null, u;
5707 <            Object v;
5708 <            while ((v = t.advance()) != null) {
5709 <                if ((u = transformer.apply((V)v)) != null)
5710 <                    r = (r == null) ? u : reducer.apply(r, u);
5711 <            }
5712 <            t.result = r;
5713 <            for (;;) {
5714 <                int c; BulkTask<K,V,?> par; MapReduceValuesTask<K,V,U> s, p;
5715 <                if ((par = t.parent) == null ||
5716 <                    !(par instanceof MapReduceValuesTask)) {
5717 <                    t.quietlyComplete();
5718 <                    break;
5719 <                }
5720 <                else if ((c = (p = (MapReduceValuesTask<K,V,U>)par).pending) == 0) {
5721 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5581 >            final Function<? super V, ? extends U> transformer;
5582 >            final BiFunction<? super U, ? super U, ? extends U> reducer;
5583 >            if ((transformer = this.transformer) != null &&
5584 >                (reducer = this.reducer) != null) {
5585 >                for (int i = baseIndex, f, h; batch > 0 &&
5586 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5587 >                    addToPendingCount(1);
5588 >                    (rights = new MapReduceValuesTask<K,V,U>
5589 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5590 >                      rights, transformer, reducer)).fork();
5591 >                }
5592 >                U r = null;
5593 >                for (Node<K,V> p; (p = advance()) != null; ) {
5594 >                    U u;
5595 >                    if ((u = transformer.apply(p.val)) != null)
5596                          r = (r == null) ? u : reducer.apply(r, u);
5723                    (t = p).result = r;
5597                  }
5598 <                else if (p.casPending(c, 0))
5599 <                    break;
5598 >                result = r;
5599 >                CountedCompleter<?> c;
5600 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5601 >                    @SuppressWarnings("unchecked")
5602 >                    MapReduceValuesTask<K,V,U>
5603 >                        t = (MapReduceValuesTask<K,V,U>)c,
5604 >                        s = t.rights;
5605 >                    while (s != null) {
5606 >                        U tr, sr;
5607 >                        if ((sr = s.result) != null)
5608 >                            t.result = (((tr = t.result) == null) ? sr :
5609 >                                        reducer.apply(tr, sr));
5610 >                        s = t.rights = s.nextRight;
5611 >                    }
5612 >                }
5613              }
5614          }
5729        public final U getRawResult() { return result; }
5615      }
5616  
5617 +    @SuppressWarnings("serial")
5618      static final class MapReduceEntriesTask<K,V,U>
5619          extends BulkTask<K,V,U> {
5620 <        final Fun<Map.Entry<K,V>, ? extends U> transformer;
5621 <        final BiFun<? super U, ? super U, ? extends U> reducer;
5620 >        final Function<Map.Entry<K,V>, ? extends U> transformer;
5621 >        final BiFunction<? super U, ? super U, ? extends U> reducer;
5622          U result;
5623 <        MapReduceEntriesTask<K,V,U> sibling;
5623 >        MapReduceEntriesTask<K,V,U> rights, nextRight;
5624          MapReduceEntriesTask
5625 <            (ConcurrentHashMap<K,V> m,
5626 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5627 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5628 <            super(m);
5629 <            this.transformer = transformer;
5744 <            this.reducer = reducer;
5745 <        }
5746 <        MapReduceEntriesTask
5747 <            (BulkTask<K,V,?> p, int b, boolean split,
5748 <             Fun<Map.Entry<K,V>, ? extends U> transformer,
5749 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5750 <            super(p, b, split);
5625 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5626 >             MapReduceEntriesTask<K,V,U> nextRight,
5627 >             Function<Map.Entry<K,V>, ? extends U> transformer,
5628 >             BiFunction<? super U, ? super U, ? extends U> reducer) {
5629 >            super(p, b, i, f, t); this.nextRight = nextRight;
5630              this.transformer = transformer;
5631              this.reducer = reducer;
5632          }
5633 +        public final U getRawResult() { return result; }
5634          public final void compute() {
5635 <            MapReduceEntriesTask<K,V,U> t = this;
5636 <            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5637 <                this.transformer;
5638 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5639 <                this.reducer;
5640 <            if (transformer == null || reducer == null)
5641 <                throw new Error(NullFunctionMessage);
5642 <            int b = batch();
5643 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5644 <                b >>>= 1;
5645 <                t.pending = 1;
5646 <                MapReduceEntriesTask<K,V,U> rt =
5647 <                    new MapReduceEntriesTask<K,V,U>
5648 <                    (t, b, true, transformer, reducer);
5649 <                t = new MapReduceEntriesTask<K,V,U>
5770 <                    (t, b, false, transformer, reducer);
5771 <                t.sibling = rt;
5772 <                rt.sibling = t;
5773 <                rt.fork();
5774 <            }
5775 <            U r = null, u;
5776 <            Object v;
5777 <            while ((v = t.advance()) != null) {
5778 <                if ((u = transformer.apply(entryFor((K)t.nextKey, (V)v))) != null)
5779 <                    r = (r == null) ? u : reducer.apply(r, u);
5780 <            }
5781 <            t.result = r;
5782 <            for (;;) {
5783 <                int c; BulkTask<K,V,?> par; MapReduceEntriesTask<K,V,U> s, p;
5784 <                if ((par = t.parent) == null ||
5785 <                    !(par instanceof MapReduceEntriesTask)) {
5786 <                    t.quietlyComplete();
5787 <                    break;
5788 <                }
5789 <                else if ((c = (p = (MapReduceEntriesTask<K,V,U>)par).pending) == 0) {
5790 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5635 >            final Function<Map.Entry<K,V>, ? extends U> transformer;
5636 >            final BiFunction<? super U, ? super U, ? extends U> reducer;
5637 >            if ((transformer = this.transformer) != null &&
5638 >                (reducer = this.reducer) != null) {
5639 >                for (int i = baseIndex, f, h; batch > 0 &&
5640 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5641 >                    addToPendingCount(1);
5642 >                    (rights = new MapReduceEntriesTask<K,V,U>
5643 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5644 >                      rights, transformer, reducer)).fork();
5645 >                }
5646 >                U r = null;
5647 >                for (Node<K,V> p; (p = advance()) != null; ) {
5648 >                    U u;
5649 >                    if ((u = transformer.apply(p)) != null)
5650                          r = (r == null) ? u : reducer.apply(r, u);
5792                    (t = p).result = r;
5651                  }
5652 <                else if (p.casPending(c, 0))
5653 <                    break;
5652 >                result = r;
5653 >                CountedCompleter<?> c;
5654 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5655 >                    @SuppressWarnings("unchecked")
5656 >                    MapReduceEntriesTask<K,V,U>
5657 >                        t = (MapReduceEntriesTask<K,V,U>)c,
5658 >                        s = t.rights;
5659 >                    while (s != null) {
5660 >                        U tr, sr;
5661 >                        if ((sr = s.result) != null)
5662 >                            t.result = (((tr = t.result) == null) ? sr :
5663 >                                        reducer.apply(tr, sr));
5664 >                        s = t.rights = s.nextRight;
5665 >                    }
5666 >                }
5667              }
5668          }
5798        public final U getRawResult() { return result; }
5669      }
5670  
5671 +    @SuppressWarnings("serial")
5672      static final class MapReduceMappingsTask<K,V,U>
5673          extends BulkTask<K,V,U> {
5674 <        final BiFun<? super K, ? super V, ? extends U> transformer;
5675 <        final BiFun<? super U, ? super U, ? extends U> reducer;
5674 >        final BiFunction<? super K, ? super V, ? extends U> transformer;
5675 >        final BiFunction<? super U, ? super U, ? extends U> reducer;
5676          U result;
5677 <        MapReduceMappingsTask<K,V,U> sibling;
5807 <        MapReduceMappingsTask
5808 <            (ConcurrentHashMap<K,V> m,
5809 <             BiFun<? super K, ? super V, ? extends U> transformer,
5810 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5811 <            super(m);
5812 <            this.transformer = transformer;
5813 <            this.reducer = reducer;
5814 <        }
5677 >        MapReduceMappingsTask<K,V,U> rights, nextRight;
5678          MapReduceMappingsTask
5679 <            (BulkTask<K,V,?> p, int b, boolean split,
5680 <             BiFun<? super K, ? super V, ? extends U> transformer,
5681 <             BiFun<? super U, ? super U, ? extends U> reducer) {
5682 <            super(p, b, split);
5679 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5680 >             MapReduceMappingsTask<K,V,U> nextRight,
5681 >             BiFunction<? super K, ? super V, ? extends U> transformer,
5682 >             BiFunction<? super U, ? super U, ? extends U> reducer) {
5683 >            super(p, b, i, f, t); this.nextRight = nextRight;
5684              this.transformer = transformer;
5685              this.reducer = reducer;
5686          }
5687 +        public final U getRawResult() { return result; }
5688          public final void compute() {
5689 <            MapReduceMappingsTask<K,V,U> t = this;
5690 <            final BiFun<? super K, ? super V, ? extends U> transformer =
5691 <                this.transformer;
5692 <            final BiFun<? super U, ? super U, ? extends U> reducer =
5693 <                this.reducer;
5694 <            if (transformer == null || reducer == null)
5695 <                throw new Error(NullFunctionMessage);
5696 <            int b = batch();
5697 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5698 <                b >>>= 1;
5699 <                t.pending = 1;
5700 <                MapReduceMappingsTask<K,V,U> rt =
5701 <                    new MapReduceMappingsTask<K,V,U>
5702 <                    (t, b, true, transformer, reducer);
5703 <                t = new MapReduceMappingsTask<K,V,U>
5839 <                    (t, b, false, transformer, reducer);
5840 <                t.sibling = rt;
5841 <                rt.sibling = t;
5842 <                rt.fork();
5843 <            }
5844 <            U r = null, u;
5845 <            Object v;
5846 <            while ((v = t.advance()) != null) {
5847 <                if ((u = transformer.apply((K)t.nextKey, (V)v)) != null)
5848 <                    r = (r == null) ? u : reducer.apply(r, u);
5849 <            }
5850 <            for (;;) {
5851 <                int c; BulkTask<K,V,?> par; MapReduceMappingsTask<K,V,U> s, p;
5852 <                if ((par = t.parent) == null ||
5853 <                    !(par instanceof MapReduceMappingsTask)) {
5854 <                    t.quietlyComplete();
5855 <                    break;
5856 <                }
5857 <                else if ((c = (p = (MapReduceMappingsTask<K,V,U>)par).pending) == 0) {
5858 <                    if ((s = t.sibling) != null && (u = s.result) != null)
5689 >            final BiFunction<? super K, ? super V, ? extends U> transformer;
5690 >            final BiFunction<? super U, ? super U, ? extends U> reducer;
5691 >            if ((transformer = this.transformer) != null &&
5692 >                (reducer = this.reducer) != null) {
5693 >                for (int i = baseIndex, f, h; batch > 0 &&
5694 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5695 >                    addToPendingCount(1);
5696 >                    (rights = new MapReduceMappingsTask<K,V,U>
5697 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5698 >                      rights, transformer, reducer)).fork();
5699 >                }
5700 >                U r = null;
5701 >                for (Node<K,V> p; (p = advance()) != null; ) {
5702 >                    U u;
5703 >                    if ((u = transformer.apply(p.key, p.val)) != null)
5704                          r = (r == null) ? u : reducer.apply(r, u);
5860                    (t = p).result = r;
5705                  }
5706 <                else if (p.casPending(c, 0))
5707 <                    break;
5706 >                result = r;
5707 >                CountedCompleter<?> c;
5708 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5709 >                    @SuppressWarnings("unchecked")
5710 >                    MapReduceMappingsTask<K,V,U>
5711 >                        t = (MapReduceMappingsTask<K,V,U>)c,
5712 >                        s = t.rights;
5713 >                    while (s != null) {
5714 >                        U tr, sr;
5715 >                        if ((sr = s.result) != null)
5716 >                            t.result = (((tr = t.result) == null) ? sr :
5717 >                                        reducer.apply(tr, sr));
5718 >                        s = t.rights = s.nextRight;
5719 >                    }
5720 >                }
5721              }
5722          }
5866        public final U getRawResult() { return result; }
5723      }
5724  
5725 +    @SuppressWarnings("serial")
5726      static final class MapReduceKeysToDoubleTask<K,V>
5727          extends BulkTask<K,V,Double> {
5728 <        final ObjectToDouble<? super K> transformer;
5729 <        final DoubleByDoubleToDouble reducer;
5728 >        final ToDoubleFunction<? super K> transformer;
5729 >        final DoubleBinaryOperator reducer;
5730          final double basis;
5731          double result;
5732 <        MapReduceKeysToDoubleTask<K,V> sibling;
5732 >        MapReduceKeysToDoubleTask<K,V> rights, nextRight;
5733          MapReduceKeysToDoubleTask
5734 <            (ConcurrentHashMap<K,V> m,
5735 <             ObjectToDouble<? super K> transformer,
5734 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5735 >             MapReduceKeysToDoubleTask<K,V> nextRight,
5736 >             ToDoubleFunction<? super K> transformer,
5737               double basis,
5738 <             DoubleByDoubleToDouble reducer) {
5739 <            super(m);
5882 <            this.transformer = transformer;
5883 <            this.basis = basis; this.reducer = reducer;
5884 <        }
5885 <        MapReduceKeysToDoubleTask
5886 <            (BulkTask<K,V,?> p, int b, boolean split,
5887 <             ObjectToDouble<? super K> transformer,
5888 <             double basis,
5889 <             DoubleByDoubleToDouble reducer) {
5890 <            super(p, b, split);
5738 >             DoubleBinaryOperator reducer) {
5739 >            super(p, b, i, f, t); this.nextRight = nextRight;
5740              this.transformer = transformer;
5741              this.basis = basis; this.reducer = reducer;
5742          }
5743 +        public final Double getRawResult() { return result; }
5744          public final void compute() {
5745 <            MapReduceKeysToDoubleTask<K,V> t = this;
5746 <            final ObjectToDouble<? super K> transformer =
5747 <                this.transformer;
5748 <            final DoubleByDoubleToDouble reducer = this.reducer;
5749 <            if (transformer == null || reducer == null)
5750 <                throw new Error(NullFunctionMessage);
5751 <            final double id = this.basis;
5752 <            int b = batch();
5753 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5754 <                b >>>= 1;
5755 <                t.pending = 1;
5756 <                MapReduceKeysToDoubleTask<K,V> rt =
5757 <                    new MapReduceKeysToDoubleTask<K,V>
5758 <                    (t, b, true, transformer, id, reducer);
5759 <                t = new MapReduceKeysToDoubleTask<K,V>
5760 <                    (t, b, false, transformer, id, reducer);
5761 <                t.sibling = rt;
5762 <                rt.sibling = t;
5763 <                rt.fork();
5764 <            }
5765 <            double r = id;
5766 <            while (t.advance() != null)
5767 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
5768 <            t.result = r;
5769 <            for (;;) {
5920 <                int c; BulkTask<K,V,?> par; MapReduceKeysToDoubleTask<K,V> s, p;
5921 <                if ((par = t.parent) == null ||
5922 <                    !(par instanceof MapReduceKeysToDoubleTask)) {
5923 <                    t.quietlyComplete();
5924 <                    break;
5925 <                }
5926 <                else if ((c = (p = (MapReduceKeysToDoubleTask<K,V>)par).pending) == 0) {
5927 <                    if ((s = t.sibling) != null)
5928 <                        r = reducer.apply(r, s.result);
5929 <                    (t = p).result = r;
5745 >            final ToDoubleFunction<? super K> transformer;
5746 >            final DoubleBinaryOperator reducer;
5747 >            if ((transformer = this.transformer) != null &&
5748 >                (reducer = this.reducer) != null) {
5749 >                double r = this.basis;
5750 >                for (int i = baseIndex, f, h; batch > 0 &&
5751 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5752 >                    addToPendingCount(1);
5753 >                    (rights = new MapReduceKeysToDoubleTask<K,V>
5754 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5755 >                      rights, transformer, r, reducer)).fork();
5756 >                }
5757 >                for (Node<K,V> p; (p = advance()) != null; )
5758 >                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key));
5759 >                result = r;
5760 >                CountedCompleter<?> c;
5761 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5762 >                    @SuppressWarnings("unchecked")
5763 >                    MapReduceKeysToDoubleTask<K,V>
5764 >                        t = (MapReduceKeysToDoubleTask<K,V>)c,
5765 >                        s = t.rights;
5766 >                    while (s != null) {
5767 >                        t.result = reducer.applyAsDouble(t.result, s.result);
5768 >                        s = t.rights = s.nextRight;
5769 >                    }
5770                  }
5931                else if (p.casPending(c, 0))
5932                    break;
5771              }
5772          }
5935        public final Double getRawResult() { return result; }
5773      }
5774  
5775 +    @SuppressWarnings("serial")
5776      static final class MapReduceValuesToDoubleTask<K,V>
5777          extends BulkTask<K,V,Double> {
5778 <        final ObjectToDouble<? super V> transformer;
5779 <        final DoubleByDoubleToDouble reducer;
5778 >        final ToDoubleFunction<? super V> transformer;
5779 >        final DoubleBinaryOperator reducer;
5780          final double basis;
5781          double result;
5782 <        MapReduceValuesToDoubleTask<K,V> sibling;
5782 >        MapReduceValuesToDoubleTask<K,V> rights, nextRight;
5783          MapReduceValuesToDoubleTask
5784 <            (ConcurrentHashMap<K,V> m,
5785 <             ObjectToDouble<? super V> transformer,
5784 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5785 >             MapReduceValuesToDoubleTask<K,V> nextRight,
5786 >             ToDoubleFunction<? super V> transformer,
5787               double basis,
5788 <             DoubleByDoubleToDouble reducer) {
5789 <            super(m);
5951 <            this.transformer = transformer;
5952 <            this.basis = basis; this.reducer = reducer;
5953 <        }
5954 <        MapReduceValuesToDoubleTask
5955 <            (BulkTask<K,V,?> p, int b, boolean split,
5956 <             ObjectToDouble<? super V> transformer,
5957 <             double basis,
5958 <             DoubleByDoubleToDouble reducer) {
5959 <            super(p, b, split);
5788 >             DoubleBinaryOperator reducer) {
5789 >            super(p, b, i, f, t); this.nextRight = nextRight;
5790              this.transformer = transformer;
5791              this.basis = basis; this.reducer = reducer;
5792          }
5793 +        public final Double getRawResult() { return result; }
5794          public final void compute() {
5795 <            MapReduceValuesToDoubleTask<K,V> t = this;
5796 <            final ObjectToDouble<? super V> transformer =
5797 <                this.transformer;
5798 <            final DoubleByDoubleToDouble reducer = this.reducer;
5799 <            if (transformer == null || reducer == null)
5800 <                throw new Error(NullFunctionMessage);
5801 <            final double id = this.basis;
5802 <            int b = batch();
5803 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5804 <                b >>>= 1;
5805 <                t.pending = 1;
5806 <                MapReduceValuesToDoubleTask<K,V> rt =
5807 <                    new MapReduceValuesToDoubleTask<K,V>
5808 <                    (t, b, true, transformer, id, reducer);
5809 <                t = new MapReduceValuesToDoubleTask<K,V>
5810 <                    (t, b, false, transformer, id, reducer);
5811 <                t.sibling = rt;
5812 <                rt.sibling = t;
5813 <                rt.fork();
5814 <            }
5815 <            double r = id;
5816 <            Object v;
5817 <            while ((v = t.advance()) != null)
5818 <                r = reducer.apply(r, transformer.apply((V)v));
5819 <            t.result = r;
5989 <            for (;;) {
5990 <                int c; BulkTask<K,V,?> par; MapReduceValuesToDoubleTask<K,V> s, p;
5991 <                if ((par = t.parent) == null ||
5992 <                    !(par instanceof MapReduceValuesToDoubleTask)) {
5993 <                    t.quietlyComplete();
5994 <                    break;
5995 <                }
5996 <                else if ((c = (p = (MapReduceValuesToDoubleTask<K,V>)par).pending) == 0) {
5997 <                    if ((s = t.sibling) != null)
5998 <                        r = reducer.apply(r, s.result);
5999 <                    (t = p).result = r;
5795 >            final ToDoubleFunction<? super V> transformer;
5796 >            final DoubleBinaryOperator reducer;
5797 >            if ((transformer = this.transformer) != null &&
5798 >                (reducer = this.reducer) != null) {
5799 >                double r = this.basis;
5800 >                for (int i = baseIndex, f, h; batch > 0 &&
5801 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5802 >                    addToPendingCount(1);
5803 >                    (rights = new MapReduceValuesToDoubleTask<K,V>
5804 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5805 >                      rights, transformer, r, reducer)).fork();
5806 >                }
5807 >                for (Node<K,V> p; (p = advance()) != null; )
5808 >                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.val));
5809 >                result = r;
5810 >                CountedCompleter<?> c;
5811 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5812 >                    @SuppressWarnings("unchecked")
5813 >                    MapReduceValuesToDoubleTask<K,V>
5814 >                        t = (MapReduceValuesToDoubleTask<K,V>)c,
5815 >                        s = t.rights;
5816 >                    while (s != null) {
5817 >                        t.result = reducer.applyAsDouble(t.result, s.result);
5818 >                        s = t.rights = s.nextRight;
5819 >                    }
5820                  }
6001                else if (p.casPending(c, 0))
6002                    break;
5821              }
5822          }
6005        public final Double getRawResult() { return result; }
5823      }
5824  
5825 +    @SuppressWarnings("serial")
5826      static final class MapReduceEntriesToDoubleTask<K,V>
5827          extends BulkTask<K,V,Double> {
5828 <        final ObjectToDouble<Map.Entry<K,V>> transformer;
5829 <        final DoubleByDoubleToDouble reducer;
5828 >        final ToDoubleFunction<Map.Entry<K,V>> transformer;
5829 >        final DoubleBinaryOperator reducer;
5830          final double basis;
5831          double result;
5832 <        MapReduceEntriesToDoubleTask<K,V> sibling;
6015 <        MapReduceEntriesToDoubleTask
6016 <            (ConcurrentHashMap<K,V> m,
6017 <             ObjectToDouble<Map.Entry<K,V>> transformer,
6018 <             double basis,
6019 <             DoubleByDoubleToDouble reducer) {
6020 <            super(m);
6021 <            this.transformer = transformer;
6022 <            this.basis = basis; this.reducer = reducer;
6023 <        }
5832 >        MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
5833          MapReduceEntriesToDoubleTask
5834 <            (BulkTask<K,V,?> p, int b, boolean split,
5835 <             ObjectToDouble<Map.Entry<K,V>> transformer,
5834 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5835 >             MapReduceEntriesToDoubleTask<K,V> nextRight,
5836 >             ToDoubleFunction<Map.Entry<K,V>> transformer,
5837               double basis,
5838 <             DoubleByDoubleToDouble reducer) {
5839 <            super(p, b, split);
5838 >             DoubleBinaryOperator reducer) {
5839 >            super(p, b, i, f, t); this.nextRight = nextRight;
5840              this.transformer = transformer;
5841              this.basis = basis; this.reducer = reducer;
5842          }
5843 +        public final Double getRawResult() { return result; }
5844          public final void compute() {
5845 <            MapReduceEntriesToDoubleTask<K,V> t = this;
5846 <            final ObjectToDouble<Map.Entry<K,V>> transformer =
5847 <                this.transformer;
5848 <            final DoubleByDoubleToDouble reducer = this.reducer;
5849 <            if (transformer == null || reducer == null)
5850 <                throw new Error(NullFunctionMessage);
5851 <            final double id = this.basis;
5852 <            int b = batch();
5853 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5854 <                b >>>= 1;
5855 <                t.pending = 1;
5856 <                MapReduceEntriesToDoubleTask<K,V> rt =
5857 <                    new MapReduceEntriesToDoubleTask<K,V>
5858 <                    (t, b, true, transformer, id, reducer);
5859 <                t = new MapReduceEntriesToDoubleTask<K,V>
5860 <                    (t, b, false, transformer, id, reducer);
5861 <                t.sibling = rt;
5862 <                rt.sibling = t;
5863 <                rt.fork();
5864 <            }
5865 <            double r = id;
5866 <            Object v;
5867 <            while ((v = t.advance()) != null)
5868 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
5869 <            t.result = r;
6059 <            for (;;) {
6060 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToDoubleTask<K,V> s, p;
6061 <                if ((par = t.parent) == null ||
6062 <                    !(par instanceof MapReduceEntriesToDoubleTask)) {
6063 <                    t.quietlyComplete();
6064 <                    break;
6065 <                }
6066 <                else if ((c = (p = (MapReduceEntriesToDoubleTask<K,V>)par).pending) == 0) {
6067 <                    if ((s = t.sibling) != null)
6068 <                        r = reducer.apply(r, s.result);
6069 <                    (t = p).result = r;
5845 >            final ToDoubleFunction<Map.Entry<K,V>> transformer;
5846 >            final DoubleBinaryOperator reducer;
5847 >            if ((transformer = this.transformer) != null &&
5848 >                (reducer = this.reducer) != null) {
5849 >                double r = this.basis;
5850 >                for (int i = baseIndex, f, h; batch > 0 &&
5851 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5852 >                    addToPendingCount(1);
5853 >                    (rights = new MapReduceEntriesToDoubleTask<K,V>
5854 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5855 >                      rights, transformer, r, reducer)).fork();
5856 >                }
5857 >                for (Node<K,V> p; (p = advance()) != null; )
5858 >                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p));
5859 >                result = r;
5860 >                CountedCompleter<?> c;
5861 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5862 >                    @SuppressWarnings("unchecked")
5863 >                    MapReduceEntriesToDoubleTask<K,V>
5864 >                        t = (MapReduceEntriesToDoubleTask<K,V>)c,
5865 >                        s = t.rights;
5866 >                    while (s != null) {
5867 >                        t.result = reducer.applyAsDouble(t.result, s.result);
5868 >                        s = t.rights = s.nextRight;
5869 >                    }
5870                  }
6071                else if (p.casPending(c, 0))
6072                    break;
5871              }
5872          }
6075        public final Double getRawResult() { return result; }
5873      }
5874  
5875 +    @SuppressWarnings("serial")
5876      static final class MapReduceMappingsToDoubleTask<K,V>
5877          extends BulkTask<K,V,Double> {
5878 <        final ObjectByObjectToDouble<? super K, ? super V> transformer;
5879 <        final DoubleByDoubleToDouble reducer;
5878 >        final ToDoubleBiFunction<? super K, ? super V> transformer;
5879 >        final DoubleBinaryOperator reducer;
5880          final double basis;
5881          double result;
5882 <        MapReduceMappingsToDoubleTask<K,V> sibling;
5882 >        MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
5883          MapReduceMappingsToDoubleTask
5884 <            (ConcurrentHashMap<K,V> m,
5885 <             ObjectByObjectToDouble<? super K, ? super V> transformer,
5884 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5885 >             MapReduceMappingsToDoubleTask<K,V> nextRight,
5886 >             ToDoubleBiFunction<? super K, ? super V> transformer,
5887               double basis,
5888 <             DoubleByDoubleToDouble reducer) {
5889 <            super(m);
6091 <            this.transformer = transformer;
6092 <            this.basis = basis; this.reducer = reducer;
6093 <        }
6094 <        MapReduceMappingsToDoubleTask
6095 <            (BulkTask<K,V,?> p, int b, boolean split,
6096 <             ObjectByObjectToDouble<? super K, ? super V> transformer,
6097 <             double basis,
6098 <             DoubleByDoubleToDouble reducer) {
6099 <            super(p, b, split);
5888 >             DoubleBinaryOperator reducer) {
5889 >            super(p, b, i, f, t); this.nextRight = nextRight;
5890              this.transformer = transformer;
5891              this.basis = basis; this.reducer = reducer;
5892          }
5893 +        public final Double getRawResult() { return result; }
5894          public final void compute() {
5895 <            MapReduceMappingsToDoubleTask<K,V> t = this;
5896 <            final ObjectByObjectToDouble<? super K, ? super V> transformer =
5897 <                this.transformer;
5898 <            final DoubleByDoubleToDouble reducer = this.reducer;
5899 <            if (transformer == null || reducer == null)
5900 <                throw new Error(NullFunctionMessage);
5901 <            final double id = this.basis;
5902 <            int b = batch();
5903 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5904 <                b >>>= 1;
5905 <                t.pending = 1;
5906 <                MapReduceMappingsToDoubleTask<K,V> rt =
5907 <                    new MapReduceMappingsToDoubleTask<K,V>
5908 <                    (t, b, true, transformer, id, reducer);
5909 <                t = new MapReduceMappingsToDoubleTask<K,V>
5910 <                    (t, b, false, transformer, id, reducer);
5911 <                t.sibling = rt;
5912 <                rt.sibling = t;
5913 <                rt.fork();
5914 <            }
5915 <            double r = id;
5916 <            Object v;
5917 <            while ((v = t.advance()) != null)
5918 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
5919 <            t.result = r;
6129 <            for (;;) {
6130 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToDoubleTask<K,V> s, p;
6131 <                if ((par = t.parent) == null ||
6132 <                    !(par instanceof MapReduceMappingsToDoubleTask)) {
6133 <                    t.quietlyComplete();
6134 <                    break;
6135 <                }
6136 <                else if ((c = (p = (MapReduceMappingsToDoubleTask<K,V>)par).pending) == 0) {
6137 <                    if ((s = t.sibling) != null)
6138 <                        r = reducer.apply(r, s.result);
6139 <                    (t = p).result = r;
5895 >            final ToDoubleBiFunction<? super K, ? super V> transformer;
5896 >            final DoubleBinaryOperator reducer;
5897 >            if ((transformer = this.transformer) != null &&
5898 >                (reducer = this.reducer) != null) {
5899 >                double r = this.basis;
5900 >                for (int i = baseIndex, f, h; batch > 0 &&
5901 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5902 >                    addToPendingCount(1);
5903 >                    (rights = new MapReduceMappingsToDoubleTask<K,V>
5904 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5905 >                      rights, transformer, r, reducer)).fork();
5906 >                }
5907 >                for (Node<K,V> p; (p = advance()) != null; )
5908 >                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val));
5909 >                result = r;
5910 >                CountedCompleter<?> c;
5911 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5912 >                    @SuppressWarnings("unchecked")
5913 >                    MapReduceMappingsToDoubleTask<K,V>
5914 >                        t = (MapReduceMappingsToDoubleTask<K,V>)c,
5915 >                        s = t.rights;
5916 >                    while (s != null) {
5917 >                        t.result = reducer.applyAsDouble(t.result, s.result);
5918 >                        s = t.rights = s.nextRight;
5919 >                    }
5920                  }
6141                else if (p.casPending(c, 0))
6142                    break;
5921              }
5922          }
6145        public final Double getRawResult() { return result; }
5923      }
5924  
5925 +    @SuppressWarnings("serial")
5926      static final class MapReduceKeysToLongTask<K,V>
5927          extends BulkTask<K,V,Long> {
5928 <        final ObjectToLong<? super K> transformer;
5929 <        final LongByLongToLong reducer;
5928 >        final ToLongFunction<? super K> transformer;
5929 >        final LongBinaryOperator reducer;
5930          final long basis;
5931          long result;
5932 <        MapReduceKeysToLongTask<K,V> sibling;
5932 >        MapReduceKeysToLongTask<K,V> rights, nextRight;
5933          MapReduceKeysToLongTask
5934 <            (ConcurrentHashMap<K,V> m,
5935 <             ObjectToLong<? super K> transformer,
5934 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5935 >             MapReduceKeysToLongTask<K,V> nextRight,
5936 >             ToLongFunction<? super K> transformer,
5937               long basis,
5938 <             LongByLongToLong reducer) {
5939 <            super(m);
6161 <            this.transformer = transformer;
6162 <            this.basis = basis; this.reducer = reducer;
6163 <        }
6164 <        MapReduceKeysToLongTask
6165 <            (BulkTask<K,V,?> p, int b, boolean split,
6166 <             ObjectToLong<? super K> transformer,
6167 <             long basis,
6168 <             LongByLongToLong reducer) {
6169 <            super(p, b, split);
5938 >             LongBinaryOperator reducer) {
5939 >            super(p, b, i, f, t); this.nextRight = nextRight;
5940              this.transformer = transformer;
5941              this.basis = basis; this.reducer = reducer;
5942          }
5943 +        public final Long getRawResult() { return result; }
5944          public final void compute() {
5945 <            MapReduceKeysToLongTask<K,V> t = this;
5946 <            final ObjectToLong<? super K> transformer =
5947 <                this.transformer;
5948 <            final LongByLongToLong reducer = this.reducer;
5949 <            if (transformer == null || reducer == null)
5950 <                throw new Error(NullFunctionMessage);
5951 <            final long id = this.basis;
5952 <            int b = batch();
5953 <            while (b > 1 && t.baseIndex != t.baseLimit) {
5954 <                b >>>= 1;
5955 <                t.pending = 1;
5956 <                MapReduceKeysToLongTask<K,V> rt =
5957 <                    new MapReduceKeysToLongTask<K,V>
5958 <                    (t, b, true, transformer, id, reducer);
5959 <                t = new MapReduceKeysToLongTask<K,V>
5960 <                    (t, b, false, transformer, id, reducer);
5961 <                t.sibling = rt;
5962 <                rt.sibling = t;
5963 <                rt.fork();
5964 <            }
5965 <            long r = id;
5966 <            while (t.advance() != null)
5967 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
5968 <            t.result = r;
5969 <            for (;;) {
6199 <                int c; BulkTask<K,V,?> par; MapReduceKeysToLongTask<K,V> s, p;
6200 <                if ((par = t.parent) == null ||
6201 <                    !(par instanceof MapReduceKeysToLongTask)) {
6202 <                    t.quietlyComplete();
6203 <                    break;
6204 <                }
6205 <                else if ((c = (p = (MapReduceKeysToLongTask<K,V>)par).pending) == 0) {
6206 <                    if ((s = t.sibling) != null)
6207 <                        r = reducer.apply(r, s.result);
6208 <                    (t = p).result = r;
5945 >            final ToLongFunction<? super K> transformer;
5946 >            final LongBinaryOperator reducer;
5947 >            if ((transformer = this.transformer) != null &&
5948 >                (reducer = this.reducer) != null) {
5949 >                long r = this.basis;
5950 >                for (int i = baseIndex, f, h; batch > 0 &&
5951 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5952 >                    addToPendingCount(1);
5953 >                    (rights = new MapReduceKeysToLongTask<K,V>
5954 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
5955 >                      rights, transformer, r, reducer)).fork();
5956 >                }
5957 >                for (Node<K,V> p; (p = advance()) != null; )
5958 >                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.key));
5959 >                result = r;
5960 >                CountedCompleter<?> c;
5961 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5962 >                    @SuppressWarnings("unchecked")
5963 >                    MapReduceKeysToLongTask<K,V>
5964 >                        t = (MapReduceKeysToLongTask<K,V>)c,
5965 >                        s = t.rights;
5966 >                    while (s != null) {
5967 >                        t.result = reducer.applyAsLong(t.result, s.result);
5968 >                        s = t.rights = s.nextRight;
5969 >                    }
5970                  }
6210                else if (p.casPending(c, 0))
6211                    break;
5971              }
5972          }
6214        public final Long getRawResult() { return result; }
5973      }
5974  
5975 +    @SuppressWarnings("serial")
5976      static final class MapReduceValuesToLongTask<K,V>
5977          extends BulkTask<K,V,Long> {
5978 <        final ObjectToLong<? super V> transformer;
5979 <        final LongByLongToLong reducer;
5978 >        final ToLongFunction<? super V> transformer;
5979 >        final LongBinaryOperator reducer;
5980          final long basis;
5981          long result;
5982 <        MapReduceValuesToLongTask<K,V> sibling;
5982 >        MapReduceValuesToLongTask<K,V> rights, nextRight;
5983          MapReduceValuesToLongTask
5984 <            (ConcurrentHashMap<K,V> m,
5985 <             ObjectToLong<? super V> transformer,
5984 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5985 >             MapReduceValuesToLongTask<K,V> nextRight,
5986 >             ToLongFunction<? super V> transformer,
5987               long basis,
5988 <             LongByLongToLong reducer) {
5989 <            super(m);
6230 <            this.transformer = transformer;
6231 <            this.basis = basis; this.reducer = reducer;
6232 <        }
6233 <        MapReduceValuesToLongTask
6234 <            (BulkTask<K,V,?> p, int b, boolean split,
6235 <             ObjectToLong<? super V> transformer,
6236 <             long basis,
6237 <             LongByLongToLong reducer) {
6238 <            super(p, b, split);
5988 >             LongBinaryOperator reducer) {
5989 >            super(p, b, i, f, t); this.nextRight = nextRight;
5990              this.transformer = transformer;
5991              this.basis = basis; this.reducer = reducer;
5992          }
5993 +        public final Long getRawResult() { return result; }
5994          public final void compute() {
5995 <            MapReduceValuesToLongTask<K,V> t = this;
5996 <            final ObjectToLong<? super V> transformer =
5997 <                this.transformer;
5998 <            final LongByLongToLong reducer = this.reducer;
5999 <            if (transformer == null || reducer == null)
6000 <                throw new Error(NullFunctionMessage);
6001 <            final long id = this.basis;
6002 <            int b = batch();
6003 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6004 <                b >>>= 1;
6005 <                t.pending = 1;
6006 <                MapReduceValuesToLongTask<K,V> rt =
6007 <                    new MapReduceValuesToLongTask<K,V>
6008 <                    (t, b, true, transformer, id, reducer);
6009 <                t = new MapReduceValuesToLongTask<K,V>
6010 <                    (t, b, false, transformer, id, reducer);
6011 <                t.sibling = rt;
6012 <                rt.sibling = t;
6013 <                rt.fork();
6014 <            }
6015 <            long r = id;
6016 <            Object v;
6017 <            while ((v = t.advance()) != null)
6018 <                r = reducer.apply(r, transformer.apply((V)v));
6019 <            t.result = r;
6268 <            for (;;) {
6269 <                int c; BulkTask<K,V,?> par; MapReduceValuesToLongTask<K,V> s, p;
6270 <                if ((par = t.parent) == null ||
6271 <                    !(par instanceof MapReduceValuesToLongTask)) {
6272 <                    t.quietlyComplete();
6273 <                    break;
6274 <                }
6275 <                else if ((c = (p = (MapReduceValuesToLongTask<K,V>)par).pending) == 0) {
6276 <                    if ((s = t.sibling) != null)
6277 <                        r = reducer.apply(r, s.result);
6278 <                    (t = p).result = r;
5995 >            final ToLongFunction<? super V> transformer;
5996 >            final LongBinaryOperator reducer;
5997 >            if ((transformer = this.transformer) != null &&
5998 >                (reducer = this.reducer) != null) {
5999 >                long r = this.basis;
6000 >                for (int i = baseIndex, f, h; batch > 0 &&
6001 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6002 >                    addToPendingCount(1);
6003 >                    (rights = new MapReduceValuesToLongTask<K,V>
6004 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
6005 >                      rights, transformer, r, reducer)).fork();
6006 >                }
6007 >                for (Node<K,V> p; (p = advance()) != null; )
6008 >                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.val));
6009 >                result = r;
6010 >                CountedCompleter<?> c;
6011 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6012 >                    @SuppressWarnings("unchecked")
6013 >                    MapReduceValuesToLongTask<K,V>
6014 >                        t = (MapReduceValuesToLongTask<K,V>)c,
6015 >                        s = t.rights;
6016 >                    while (s != null) {
6017 >                        t.result = reducer.applyAsLong(t.result, s.result);
6018 >                        s = t.rights = s.nextRight;
6019 >                    }
6020                  }
6280                else if (p.casPending(c, 0))
6281                    break;
6021              }
6022          }
6284        public final Long getRawResult() { return result; }
6023      }
6024  
6025 +    @SuppressWarnings("serial")
6026      static final class MapReduceEntriesToLongTask<K,V>
6027          extends BulkTask<K,V,Long> {
6028 <        final ObjectToLong<Map.Entry<K,V>> transformer;
6029 <        final LongByLongToLong reducer;
6028 >        final ToLongFunction<Map.Entry<K,V>> transformer;
6029 >        final LongBinaryOperator reducer;
6030          final long basis;
6031          long result;
6032 <        MapReduceEntriesToLongTask<K,V> sibling;
6032 >        MapReduceEntriesToLongTask<K,V> rights, nextRight;
6033          MapReduceEntriesToLongTask
6034 <            (ConcurrentHashMap<K,V> m,
6035 <             ObjectToLong<Map.Entry<K,V>> transformer,
6034 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6035 >             MapReduceEntriesToLongTask<K,V> nextRight,
6036 >             ToLongFunction<Map.Entry<K,V>> transformer,
6037               long basis,
6038 <             LongByLongToLong reducer) {
6039 <            super(m);
6300 <            this.transformer = transformer;
6301 <            this.basis = basis; this.reducer = reducer;
6302 <        }
6303 <        MapReduceEntriesToLongTask
6304 <            (BulkTask<K,V,?> p, int b, boolean split,
6305 <             ObjectToLong<Map.Entry<K,V>> transformer,
6306 <             long basis,
6307 <             LongByLongToLong reducer) {
6308 <            super(p, b, split);
6038 >             LongBinaryOperator reducer) {
6039 >            super(p, b, i, f, t); this.nextRight = nextRight;
6040              this.transformer = transformer;
6041              this.basis = basis; this.reducer = reducer;
6042          }
6043 +        public final Long getRawResult() { return result; }
6044          public final void compute() {
6045 <            MapReduceEntriesToLongTask<K,V> t = this;
6046 <            final ObjectToLong<Map.Entry<K,V>> transformer =
6047 <                this.transformer;
6048 <            final LongByLongToLong reducer = this.reducer;
6049 <            if (transformer == null || reducer == null)
6050 <                throw new Error(NullFunctionMessage);
6051 <            final long id = this.basis;
6052 <            int b = batch();
6053 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6054 <                b >>>= 1;
6055 <                t.pending = 1;
6056 <                MapReduceEntriesToLongTask<K,V> rt =
6057 <                    new MapReduceEntriesToLongTask<K,V>
6058 <                    (t, b, true, transformer, id, reducer);
6059 <                t = new MapReduceEntriesToLongTask<K,V>
6060 <                    (t, b, false, transformer, id, reducer);
6061 <                t.sibling = rt;
6062 <                rt.sibling = t;
6063 <                rt.fork();
6064 <            }
6065 <            long r = id;
6066 <            Object v;
6067 <            while ((v = t.advance()) != null)
6068 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6069 <            t.result = r;
6338 <            for (;;) {
6339 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToLongTask<K,V> s, p;
6340 <                if ((par = t.parent) == null ||
6341 <                    !(par instanceof MapReduceEntriesToLongTask)) {
6342 <                    t.quietlyComplete();
6343 <                    break;
6344 <                }
6345 <                else if ((c = (p = (MapReduceEntriesToLongTask<K,V>)par).pending) == 0) {
6346 <                    if ((s = t.sibling) != null)
6347 <                        r = reducer.apply(r, s.result);
6348 <                    (t = p).result = r;
6045 >            final ToLongFunction<Map.Entry<K,V>> transformer;
6046 >            final LongBinaryOperator reducer;
6047 >            if ((transformer = this.transformer) != null &&
6048 >                (reducer = this.reducer) != null) {
6049 >                long r = this.basis;
6050 >                for (int i = baseIndex, f, h; batch > 0 &&
6051 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6052 >                    addToPendingCount(1);
6053 >                    (rights = new MapReduceEntriesToLongTask<K,V>
6054 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
6055 >                      rights, transformer, r, reducer)).fork();
6056 >                }
6057 >                for (Node<K,V> p; (p = advance()) != null; )
6058 >                    r = reducer.applyAsLong(r, transformer.applyAsLong(p));
6059 >                result = r;
6060 >                CountedCompleter<?> c;
6061 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6062 >                    @SuppressWarnings("unchecked")
6063 >                    MapReduceEntriesToLongTask<K,V>
6064 >                        t = (MapReduceEntriesToLongTask<K,V>)c,
6065 >                        s = t.rights;
6066 >                    while (s != null) {
6067 >                        t.result = reducer.applyAsLong(t.result, s.result);
6068 >                        s = t.rights = s.nextRight;
6069 >                    }
6070                  }
6350                else if (p.casPending(c, 0))
6351                    break;
6071              }
6072          }
6354        public final Long getRawResult() { return result; }
6073      }
6074  
6075 +    @SuppressWarnings("serial")
6076      static final class MapReduceMappingsToLongTask<K,V>
6077          extends BulkTask<K,V,Long> {
6078 <        final ObjectByObjectToLong<? super K, ? super V> transformer;
6079 <        final LongByLongToLong reducer;
6078 >        final ToLongBiFunction<? super K, ? super V> transformer;
6079 >        final LongBinaryOperator reducer;
6080          final long basis;
6081          long result;
6082 <        MapReduceMappingsToLongTask<K,V> sibling;
6364 <        MapReduceMappingsToLongTask
6365 <            (ConcurrentHashMap<K,V> m,
6366 <             ObjectByObjectToLong<? super K, ? super V> transformer,
6367 <             long basis,
6368 <             LongByLongToLong reducer) {
6369 <            super(m);
6370 <            this.transformer = transformer;
6371 <            this.basis = basis; this.reducer = reducer;
6372 <        }
6082 >        MapReduceMappingsToLongTask<K,V> rights, nextRight;
6083          MapReduceMappingsToLongTask
6084 <            (BulkTask<K,V,?> p, int b, boolean split,
6085 <             ObjectByObjectToLong<? super K, ? super V> transformer,
6084 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6085 >             MapReduceMappingsToLongTask<K,V> nextRight,
6086 >             ToLongBiFunction<? super K, ? super V> transformer,
6087               long basis,
6088 <             LongByLongToLong reducer) {
6089 <            super(p, b, split);
6088 >             LongBinaryOperator reducer) {
6089 >            super(p, b, i, f, t); this.nextRight = nextRight;
6090              this.transformer = transformer;
6091              this.basis = basis; this.reducer = reducer;
6092          }
6093 +        public final Long getRawResult() { return result; }
6094          public final void compute() {
6095 <            MapReduceMappingsToLongTask<K,V> t = this;
6096 <            final ObjectByObjectToLong<? super K, ? super V> transformer =
6097 <                this.transformer;
6098 <            final LongByLongToLong reducer = this.reducer;
6099 <            if (transformer == null || reducer == null)
6100 <                throw new Error(NullFunctionMessage);
6101 <            final long id = this.basis;
6102 <            int b = batch();
6103 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6104 <                b >>>= 1;
6105 <                t.pending = 1;
6106 <                MapReduceMappingsToLongTask<K,V> rt =
6107 <                    new MapReduceMappingsToLongTask<K,V>
6108 <                    (t, b, true, transformer, id, reducer);
6109 <                t = new MapReduceMappingsToLongTask<K,V>
6110 <                    (t, b, false, transformer, id, reducer);
6111 <                t.sibling = rt;
6112 <                rt.sibling = t;
6113 <                rt.fork();
6114 <            }
6115 <            long r = id;
6116 <            Object v;
6117 <            while ((v = t.advance()) != null)
6118 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6119 <            t.result = r;
6408 <            for (;;) {
6409 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToLongTask<K,V> s, p;
6410 <                if ((par = t.parent) == null ||
6411 <                    !(par instanceof MapReduceMappingsToLongTask)) {
6412 <                    t.quietlyComplete();
6413 <                    break;
6414 <                }
6415 <                else if ((c = (p = (MapReduceMappingsToLongTask<K,V>)par).pending) == 0) {
6416 <                    if ((s = t.sibling) != null)
6417 <                        r = reducer.apply(r, s.result);
6418 <                    (t = p).result = r;
6095 >            final ToLongBiFunction<? super K, ? super V> transformer;
6096 >            final LongBinaryOperator reducer;
6097 >            if ((transformer = this.transformer) != null &&
6098 >                (reducer = this.reducer) != null) {
6099 >                long r = this.basis;
6100 >                for (int i = baseIndex, f, h; batch > 0 &&
6101 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6102 >                    addToPendingCount(1);
6103 >                    (rights = new MapReduceMappingsToLongTask<K,V>
6104 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
6105 >                      rights, transformer, r, reducer)).fork();
6106 >                }
6107 >                for (Node<K,V> p; (p = advance()) != null; )
6108 >                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.key, p.val));
6109 >                result = r;
6110 >                CountedCompleter<?> c;
6111 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6112 >                    @SuppressWarnings("unchecked")
6113 >                    MapReduceMappingsToLongTask<K,V>
6114 >                        t = (MapReduceMappingsToLongTask<K,V>)c,
6115 >                        s = t.rights;
6116 >                    while (s != null) {
6117 >                        t.result = reducer.applyAsLong(t.result, s.result);
6118 >                        s = t.rights = s.nextRight;
6119 >                    }
6120                  }
6420                else if (p.casPending(c, 0))
6421                    break;
6121              }
6122          }
6424        public final Long getRawResult() { return result; }
6123      }
6124  
6125 +    @SuppressWarnings("serial")
6126      static final class MapReduceKeysToIntTask<K,V>
6127          extends BulkTask<K,V,Integer> {
6128 <        final ObjectToInt<? super K> transformer;
6129 <        final IntByIntToInt reducer;
6128 >        final ToIntFunction<? super K> transformer;
6129 >        final IntBinaryOperator reducer;
6130          final int basis;
6131          int result;
6132 <        MapReduceKeysToIntTask<K,V> sibling;
6434 <        MapReduceKeysToIntTask
6435 <            (ConcurrentHashMap<K,V> m,
6436 <             ObjectToInt<? super K> transformer,
6437 <             int basis,
6438 <             IntByIntToInt reducer) {
6439 <            super(m);
6440 <            this.transformer = transformer;
6441 <            this.basis = basis; this.reducer = reducer;
6442 <        }
6132 >        MapReduceKeysToIntTask<K,V> rights, nextRight;
6133          MapReduceKeysToIntTask
6134 <            (BulkTask<K,V,?> p, int b, boolean split,
6135 <             ObjectToInt<? super K> transformer,
6134 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6135 >             MapReduceKeysToIntTask<K,V> nextRight,
6136 >             ToIntFunction<? super K> transformer,
6137               int basis,
6138 <             IntByIntToInt reducer) {
6139 <            super(p, b, split);
6138 >             IntBinaryOperator reducer) {
6139 >            super(p, b, i, f, t); this.nextRight = nextRight;
6140              this.transformer = transformer;
6141              this.basis = basis; this.reducer = reducer;
6142          }
6143 +        public final Integer getRawResult() { return result; }
6144          public final void compute() {
6145 <            MapReduceKeysToIntTask<K,V> t = this;
6146 <            final ObjectToInt<? super K> transformer =
6147 <                this.transformer;
6148 <            final IntByIntToInt reducer = this.reducer;
6149 <            if (transformer == null || reducer == null)
6150 <                throw new Error(NullFunctionMessage);
6151 <            final int id = this.basis;
6152 <            int b = batch();
6153 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6154 <                b >>>= 1;
6155 <                t.pending = 1;
6156 <                MapReduceKeysToIntTask<K,V> rt =
6157 <                    new MapReduceKeysToIntTask<K,V>
6158 <                    (t, b, true, transformer, id, reducer);
6159 <                t = new MapReduceKeysToIntTask<K,V>
6160 <                    (t, b, false, transformer, id, reducer);
6161 <                t.sibling = rt;
6162 <                rt.sibling = t;
6163 <                rt.fork();
6164 <            }
6165 <            int r = id;
6166 <            while (t.advance() != null)
6167 <                r = reducer.apply(r, transformer.apply((K)t.nextKey));
6168 <            t.result = r;
6169 <            for (;;) {
6478 <                int c; BulkTask<K,V,?> par; MapReduceKeysToIntTask<K,V> s, p;
6479 <                if ((par = t.parent) == null ||
6480 <                    !(par instanceof MapReduceKeysToIntTask)) {
6481 <                    t.quietlyComplete();
6482 <                    break;
6483 <                }
6484 <                else if ((c = (p = (MapReduceKeysToIntTask<K,V>)par).pending) == 0) {
6485 <                    if ((s = t.sibling) != null)
6486 <                        r = reducer.apply(r, s.result);
6487 <                    (t = p).result = r;
6145 >            final ToIntFunction<? super K> transformer;
6146 >            final IntBinaryOperator reducer;
6147 >            if ((transformer = this.transformer) != null &&
6148 >                (reducer = this.reducer) != null) {
6149 >                int r = this.basis;
6150 >                for (int i = baseIndex, f, h; batch > 0 &&
6151 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6152 >                    addToPendingCount(1);
6153 >                    (rights = new MapReduceKeysToIntTask<K,V>
6154 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
6155 >                      rights, transformer, r, reducer)).fork();
6156 >                }
6157 >                for (Node<K,V> p; (p = advance()) != null; )
6158 >                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.key));
6159 >                result = r;
6160 >                CountedCompleter<?> c;
6161 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6162 >                    @SuppressWarnings("unchecked")
6163 >                    MapReduceKeysToIntTask<K,V>
6164 >                        t = (MapReduceKeysToIntTask<K,V>)c,
6165 >                        s = t.rights;
6166 >                    while (s != null) {
6167 >                        t.result = reducer.applyAsInt(t.result, s.result);
6168 >                        s = t.rights = s.nextRight;
6169 >                    }
6170                  }
6489                else if (p.casPending(c, 0))
6490                    break;
6171              }
6172          }
6493        public final Integer getRawResult() { return result; }
6173      }
6174  
6175 +    @SuppressWarnings("serial")
6176      static final class MapReduceValuesToIntTask<K,V>
6177          extends BulkTask<K,V,Integer> {
6178 <        final ObjectToInt<? super V> transformer;
6179 <        final IntByIntToInt reducer;
6178 >        final ToIntFunction<? super V> transformer;
6179 >        final IntBinaryOperator reducer;
6180          final int basis;
6181          int result;
6182 <        MapReduceValuesToIntTask<K,V> sibling;
6182 >        MapReduceValuesToIntTask<K,V> rights, nextRight;
6183          MapReduceValuesToIntTask
6184 <            (ConcurrentHashMap<K,V> m,
6185 <             ObjectToInt<? super V> transformer,
6184 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6185 >             MapReduceValuesToIntTask<K,V> nextRight,
6186 >             ToIntFunction<? super V> transformer,
6187               int basis,
6188 <             IntByIntToInt reducer) {
6189 <            super(m);
6509 <            this.transformer = transformer;
6510 <            this.basis = basis; this.reducer = reducer;
6511 <        }
6512 <        MapReduceValuesToIntTask
6513 <            (BulkTask<K,V,?> p, int b, boolean split,
6514 <             ObjectToInt<? super V> transformer,
6515 <             int basis,
6516 <             IntByIntToInt reducer) {
6517 <            super(p, b, split);
6188 >             IntBinaryOperator reducer) {
6189 >            super(p, b, i, f, t); this.nextRight = nextRight;
6190              this.transformer = transformer;
6191              this.basis = basis; this.reducer = reducer;
6192          }
6193 +        public final Integer getRawResult() { return result; }
6194          public final void compute() {
6195 <            MapReduceValuesToIntTask<K,V> t = this;
6196 <            final ObjectToInt<? super V> transformer =
6197 <                this.transformer;
6198 <            final IntByIntToInt reducer = this.reducer;
6199 <            if (transformer == null || reducer == null)
6200 <                throw new Error(NullFunctionMessage);
6201 <            final int id = this.basis;
6202 <            int b = batch();
6203 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6204 <                b >>>= 1;
6205 <                t.pending = 1;
6206 <                MapReduceValuesToIntTask<K,V> rt =
6207 <                    new MapReduceValuesToIntTask<K,V>
6208 <                    (t, b, true, transformer, id, reducer);
6209 <                t = new MapReduceValuesToIntTask<K,V>
6210 <                    (t, b, false, transformer, id, reducer);
6211 <                t.sibling = rt;
6212 <                rt.sibling = t;
6213 <                rt.fork();
6214 <            }
6215 <            int r = id;
6216 <            Object v;
6217 <            while ((v = t.advance()) != null)
6218 <                r = reducer.apply(r, transformer.apply((V)v));
6219 <            t.result = r;
6547 <            for (;;) {
6548 <                int c; BulkTask<K,V,?> par; MapReduceValuesToIntTask<K,V> s, p;
6549 <                if ((par = t.parent) == null ||
6550 <                    !(par instanceof MapReduceValuesToIntTask)) {
6551 <                    t.quietlyComplete();
6552 <                    break;
6553 <                }
6554 <                else if ((c = (p = (MapReduceValuesToIntTask<K,V>)par).pending) == 0) {
6555 <                    if ((s = t.sibling) != null)
6556 <                        r = reducer.apply(r, s.result);
6557 <                    (t = p).result = r;
6195 >            final ToIntFunction<? super V> transformer;
6196 >            final IntBinaryOperator reducer;
6197 >            if ((transformer = this.transformer) != null &&
6198 >                (reducer = this.reducer) != null) {
6199 >                int r = this.basis;
6200 >                for (int i = baseIndex, f, h; batch > 0 &&
6201 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6202 >                    addToPendingCount(1);
6203 >                    (rights = new MapReduceValuesToIntTask<K,V>
6204 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
6205 >                      rights, transformer, r, reducer)).fork();
6206 >                }
6207 >                for (Node<K,V> p; (p = advance()) != null; )
6208 >                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.val));
6209 >                result = r;
6210 >                CountedCompleter<?> c;
6211 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6212 >                    @SuppressWarnings("unchecked")
6213 >                    MapReduceValuesToIntTask<K,V>
6214 >                        t = (MapReduceValuesToIntTask<K,V>)c,
6215 >                        s = t.rights;
6216 >                    while (s != null) {
6217 >                        t.result = reducer.applyAsInt(t.result, s.result);
6218 >                        s = t.rights = s.nextRight;
6219 >                    }
6220                  }
6559                else if (p.casPending(c, 0))
6560                    break;
6221              }
6222          }
6563        public final Integer getRawResult() { return result; }
6223      }
6224  
6225 +    @SuppressWarnings("serial")
6226      static final class MapReduceEntriesToIntTask<K,V>
6227          extends BulkTask<K,V,Integer> {
6228 <        final ObjectToInt<Map.Entry<K,V>> transformer;
6229 <        final IntByIntToInt reducer;
6228 >        final ToIntFunction<Map.Entry<K,V>> transformer;
6229 >        final IntBinaryOperator reducer;
6230          final int basis;
6231          int result;
6232 <        MapReduceEntriesToIntTask<K,V> sibling;
6232 >        MapReduceEntriesToIntTask<K,V> rights, nextRight;
6233          MapReduceEntriesToIntTask
6234 <            (ConcurrentHashMap<K,V> m,
6235 <             ObjectToInt<Map.Entry<K,V>> transformer,
6234 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6235 >             MapReduceEntriesToIntTask<K,V> nextRight,
6236 >             ToIntFunction<Map.Entry<K,V>> transformer,
6237               int basis,
6238 <             IntByIntToInt reducer) {
6239 <            super(m);
6579 <            this.transformer = transformer;
6580 <            this.basis = basis; this.reducer = reducer;
6581 <        }
6582 <        MapReduceEntriesToIntTask
6583 <            (BulkTask<K,V,?> p, int b, boolean split,
6584 <             ObjectToInt<Map.Entry<K,V>> transformer,
6585 <             int basis,
6586 <             IntByIntToInt reducer) {
6587 <            super(p, b, split);
6238 >             IntBinaryOperator reducer) {
6239 >            super(p, b, i, f, t); this.nextRight = nextRight;
6240              this.transformer = transformer;
6241              this.basis = basis; this.reducer = reducer;
6242          }
6243 +        public final Integer getRawResult() { return result; }
6244          public final void compute() {
6245 <            MapReduceEntriesToIntTask<K,V> t = this;
6246 <            final ObjectToInt<Map.Entry<K,V>> transformer =
6247 <                this.transformer;
6248 <            final IntByIntToInt reducer = this.reducer;
6249 <            if (transformer == null || reducer == null)
6250 <                throw new Error(NullFunctionMessage);
6251 <            final int id = this.basis;
6252 <            int b = batch();
6253 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6254 <                b >>>= 1;
6255 <                t.pending = 1;
6256 <                MapReduceEntriesToIntTask<K,V> rt =
6257 <                    new MapReduceEntriesToIntTask<K,V>
6258 <                    (t, b, true, transformer, id, reducer);
6259 <                t = new MapReduceEntriesToIntTask<K,V>
6260 <                    (t, b, false, transformer, id, reducer);
6261 <                t.sibling = rt;
6262 <                rt.sibling = t;
6263 <                rt.fork();
6264 <            }
6265 <            int r = id;
6266 <            Object v;
6267 <            while ((v = t.advance()) != null)
6268 <                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6269 <            t.result = r;
6617 <            for (;;) {
6618 <                int c; BulkTask<K,V,?> par; MapReduceEntriesToIntTask<K,V> s, p;
6619 <                if ((par = t.parent) == null ||
6620 <                    !(par instanceof MapReduceEntriesToIntTask)) {
6621 <                    t.quietlyComplete();
6622 <                    break;
6623 <                }
6624 <                else if ((c = (p = (MapReduceEntriesToIntTask<K,V>)par).pending) == 0) {
6625 <                    if ((s = t.sibling) != null)
6626 <                        r = reducer.apply(r, s.result);
6627 <                    (t = p).result = r;
6245 >            final ToIntFunction<Map.Entry<K,V>> transformer;
6246 >            final IntBinaryOperator reducer;
6247 >            if ((transformer = this.transformer) != null &&
6248 >                (reducer = this.reducer) != null) {
6249 >                int r = this.basis;
6250 >                for (int i = baseIndex, f, h; batch > 0 &&
6251 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6252 >                    addToPendingCount(1);
6253 >                    (rights = new MapReduceEntriesToIntTask<K,V>
6254 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
6255 >                      rights, transformer, r, reducer)).fork();
6256 >                }
6257 >                for (Node<K,V> p; (p = advance()) != null; )
6258 >                    r = reducer.applyAsInt(r, transformer.applyAsInt(p));
6259 >                result = r;
6260 >                CountedCompleter<?> c;
6261 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6262 >                    @SuppressWarnings("unchecked")
6263 >                    MapReduceEntriesToIntTask<K,V>
6264 >                        t = (MapReduceEntriesToIntTask<K,V>)c,
6265 >                        s = t.rights;
6266 >                    while (s != null) {
6267 >                        t.result = reducer.applyAsInt(t.result, s.result);
6268 >                        s = t.rights = s.nextRight;
6269 >                    }
6270                  }
6629                else if (p.casPending(c, 0))
6630                    break;
6271              }
6272          }
6633        public final Integer getRawResult() { return result; }
6273      }
6274  
6275 +    @SuppressWarnings("serial")
6276      static final class MapReduceMappingsToIntTask<K,V>
6277          extends BulkTask<K,V,Integer> {
6278 <        final ObjectByObjectToInt<? super K, ? super V> transformer;
6279 <        final IntByIntToInt reducer;
6278 >        final ToIntBiFunction<? super K, ? super V> transformer;
6279 >        final IntBinaryOperator reducer;
6280          final int basis;
6281          int result;
6282 <        MapReduceMappingsToIntTask<K,V> sibling;
6643 <        MapReduceMappingsToIntTask
6644 <            (ConcurrentHashMap<K,V> m,
6645 <             ObjectByObjectToInt<? super K, ? super V> transformer,
6646 <             int basis,
6647 <             IntByIntToInt reducer) {
6648 <            super(m);
6649 <            this.transformer = transformer;
6650 <            this.basis = basis; this.reducer = reducer;
6651 <        }
6282 >        MapReduceMappingsToIntTask<K,V> rights, nextRight;
6283          MapReduceMappingsToIntTask
6284 <            (BulkTask<K,V,?> p, int b, boolean split,
6285 <             ObjectByObjectToInt<? super K, ? super V> transformer,
6284 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6285 >             MapReduceMappingsToIntTask<K,V> nextRight,
6286 >             ToIntBiFunction<? super K, ? super V> transformer,
6287               int basis,
6288 <             IntByIntToInt reducer) {
6289 <            super(p, b, split);
6288 >             IntBinaryOperator reducer) {
6289 >            super(p, b, i, f, t); this.nextRight = nextRight;
6290              this.transformer = transformer;
6291              this.basis = basis; this.reducer = reducer;
6292          }
6293 +        public final Integer getRawResult() { return result; }
6294          public final void compute() {
6295 <            MapReduceMappingsToIntTask<K,V> t = this;
6296 <            final ObjectByObjectToInt<? super K, ? super V> transformer =
6297 <                this.transformer;
6298 <            final IntByIntToInt reducer = this.reducer;
6299 <            if (transformer == null || reducer == null)
6300 <                throw new Error(NullFunctionMessage);
6301 <            final int id = this.basis;
6302 <            int b = batch();
6303 <            while (b > 1 && t.baseIndex != t.baseLimit) {
6304 <                b >>>= 1;
6305 <                t.pending = 1;
6306 <                MapReduceMappingsToIntTask<K,V> rt =
6307 <                    new MapReduceMappingsToIntTask<K,V>
6308 <                    (t, b, true, transformer, id, reducer);
6309 <                t = new MapReduceMappingsToIntTask<K,V>
6310 <                    (t, b, false, transformer, id, reducer);
6311 <                t.sibling = rt;
6312 <                rt.sibling = t;
6313 <                rt.fork();
6314 <            }
6315 <            int r = id;
6316 <            Object v;
6317 <            while ((v = t.advance()) != null)
6318 <                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6319 <            t.result = r;
6687 <            for (;;) {
6688 <                int c; BulkTask<K,V,?> par; MapReduceMappingsToIntTask<K,V> s, p;
6689 <                if ((par = t.parent) == null ||
6690 <                    !(par instanceof MapReduceMappingsToIntTask)) {
6691 <                    t.quietlyComplete();
6692 <                    break;
6693 <                }
6694 <                else if ((c = (p = (MapReduceMappingsToIntTask<K,V>)par).pending) == 0) {
6695 <                    if ((s = t.sibling) != null)
6696 <                        r = reducer.apply(r, s.result);
6697 <                    (t = p).result = r;
6295 >            final ToIntBiFunction<? super K, ? super V> transformer;
6296 >            final IntBinaryOperator reducer;
6297 >            if ((transformer = this.transformer) != null &&
6298 >                (reducer = this.reducer) != null) {
6299 >                int r = this.basis;
6300 >                for (int i = baseIndex, f, h; batch > 0 &&
6301 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6302 >                    addToPendingCount(1);
6303 >                    (rights = new MapReduceMappingsToIntTask<K,V>
6304 >                     (this, batch >>>= 1, baseLimit = h, f, tab,
6305 >                      rights, transformer, r, reducer)).fork();
6306 >                }
6307 >                for (Node<K,V> p; (p = advance()) != null; )
6308 >                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.key, p.val));
6309 >                result = r;
6310 >                CountedCompleter<?> c;
6311 >                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6312 >                    @SuppressWarnings("unchecked")
6313 >                    MapReduceMappingsToIntTask<K,V>
6314 >                        t = (MapReduceMappingsToIntTask<K,V>)c,
6315 >                        s = t.rights;
6316 >                    while (s != null) {
6317 >                        t.result = reducer.applyAsInt(t.result, s.result);
6318 >                        s = t.rights = s.nextRight;
6319 >                    }
6320                  }
6699                else if (p.casPending(c, 0))
6700                    break;
6321              }
6322          }
6703        public final Integer getRawResult() { return result; }
6323      }
6324  
6706
6325      // Unsafe mechanics
6326 <    private static final sun.misc.Unsafe UNSAFE;
6327 <    private static final long counterOffset;
6328 <    private static final long sizeCtlOffset;
6329 <    private static final long ABASE;
6326 >    private static final Unsafe U = Unsafe.getUnsafe();
6327 >    private static final long SIZECTL
6328 >        = U.objectFieldOffset(ConcurrentHashMap.class, "sizeCtl");
6329 >    private static final long TRANSFERINDEX
6330 >        = U.objectFieldOffset(ConcurrentHashMap.class, "transferIndex");
6331 >    private static final long BASECOUNT
6332 >        = U.objectFieldOffset(ConcurrentHashMap.class, "baseCount");
6333 >    private static final long CELLSBUSY
6334 >        = U.objectFieldOffset(ConcurrentHashMap.class, "cellsBusy");
6335 >    private static final long CELLVALUE
6336 >        = U.objectFieldOffset(CounterCell.class, "value");
6337 >    private static final int ABASE = U.arrayBaseOffset(Node[].class);
6338      private static final int ASHIFT;
6339  
6340      static {
6341 <        int ss;
6342 <        try {
6343 <            UNSAFE =  sun.misc.Unsafe.getUnsafe();
6344 <            Class<?> k = ConcurrentHashMap.class;
6345 <            counterOffset = UNSAFE.objectFieldOffset
6346 <                (k.getDeclaredField("counter"));
6347 <            sizeCtlOffset = UNSAFE.objectFieldOffset
6348 <                (k.getDeclaredField("sizeCtl"));
6723 <            Class<?> sc = Node[].class;
6724 <            ABASE = UNSAFE.arrayBaseOffset(sc);
6725 <            ss = UNSAFE.arrayIndexScale(sc);
6726 <        } catch (Exception e) {
6727 <            throw new Error(e);
6728 <        }
6729 <        if ((ss & (ss-1)) != 0)
6730 <            throw new Error("data type scale not a power of two");
6731 <        ASHIFT = 31 - Integer.numberOfLeadingZeros(ss);
6732 <    }
6341 >        int scale = U.arrayIndexScale(Node[].class);
6342 >        if ((scale & (scale - 1)) != 0)
6343 >            throw new ExceptionInInitializerError("array index scale not a power of two");
6344 >        ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
6345 >
6346 >        // Reduce the risk of rare disastrous classloading in first call to
6347 >        // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
6348 >        Class<?> ensureLoaded = LockSupport.class;
6349  
6350 +        // Eager class load observed to help JIT during startup
6351 +        ensureLoaded = ReservationNode.class;
6352 +    }
6353   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines