ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/ConcurrentHashMapV8.java
(Generate patch)

Comparing jsr166/src/jsr166e/ConcurrentHashMapV8.java (file contents):
Revision 1.7 by jsr166, Tue Aug 30 13:41:59 2011 UTC vs.
Revision 1.52 by dl, Mon Aug 13 15:52:33 2012 UTC

# Line 6 | Line 6
6  
7   package jsr166e;
8   import jsr166e.LongAdder;
9 + import jsr166e.ForkJoinPool;
10 + import jsr166e.ForkJoinTask;
11 +
12 + import java.util.Comparator;
13 + import java.util.Arrays;
14   import java.util.Map;
15   import java.util.Set;
16   import java.util.Collection;
# Line 19 | Line 24 | import java.util.Enumeration;
24   import java.util.ConcurrentModificationException;
25   import java.util.NoSuchElementException;
26   import java.util.concurrent.ConcurrentMap;
27 + import java.util.concurrent.ThreadLocalRandom;
28 + import java.util.concurrent.locks.LockSupport;
29 + import java.util.concurrent.locks.AbstractQueuedSynchronizer;
30 + import java.util.concurrent.atomic.AtomicReference;
31 +
32   import java.io.Serializable;
33  
34   /**
# Line 49 | Line 59 | import java.io.Serializable;
59   * are typically useful only when a map is not undergoing concurrent
60   * updates in other threads.  Otherwise the results of these methods
61   * reflect transient states that may be adequate for monitoring
62 < * purposes, but not for program control.
62 > * or estimation purposes, but not for program control.
63   *
64 < * <p> Resizing this or any other kind of hash table is a relatively
65 < * slow operation, so, when possible, it is a good idea to provide
66 < * estimates of expected table sizes in constructors. Also, for
67 < * compatibility with previous versions of this class, constructors
68 < * may optionally specify an expected {@code concurrencyLevel} as an
69 < * additional hint for internal sizing.
64 > * <p> The table is dynamically expanded when there are too many
65 > * collisions (i.e., keys that have distinct hash codes but fall into
66 > * the same slot modulo the table size), with the expected average
67 > * effect of maintaining roughly two bins per mapping (corresponding
68 > * to a 0.75 load factor threshold for resizing). There may be much
69 > * variance around this average as mappings are added and removed, but
70 > * overall, this maintains a commonly accepted time/space tradeoff for
71 > * hash tables.  However, resizing this or any other kind of hash
72 > * table may be a relatively slow operation. When possible, it is a
73 > * good idea to provide a size estimate as an optional {@code
74 > * initialCapacity} constructor argument. An additional optional
75 > * {@code loadFactor} constructor argument provides a further means of
76 > * customizing initial table capacity by specifying the table density
77 > * to be used in calculating the amount of space to allocate for the
78 > * given number of elements.  Also, for compatibility with previous
79 > * versions of this class, constructors may optionally specify an
80 > * expected {@code concurrencyLevel} as an additional hint for
81 > * internal sizing.  Note that using many keys with exactly the same
82 > * {@code hashCode()} is a sure way to slow down performance of any
83 > * hash table.
84   *
85   * <p>This class and its views and iterators implement all of the
86   * <em>optional</em> methods of the {@link Map} and {@link Iterator}
# Line 70 | Line 94 | import java.io.Serializable;
94   * Java Collections Framework</a>.
95   *
96   * <p><em>jsr166e note: This class is a candidate replacement for
97 < * java.util.concurrent.ConcurrentHashMap.<em>
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   *
101   * @since 1.5
102   * @author Doug Lea
# Line 78 | Line 104 | import java.io.Serializable;
104   * @param <V> the type of mapped values
105   */
106   public class ConcurrentHashMapV8<K, V>
107 <        implements ConcurrentMap<K, V>, Serializable {
107 >    implements ConcurrentMap<K, V>, Serializable {
108      private static final long serialVersionUID = 7249069246763182397L;
109  
110      /**
111 <     * A function computing a mapping from the given key to a value,
112 <     *  or <code>null</code> if there is no mapping. This is a
113 <     * place-holder for an upcoming JDK8 interface.
114 <     */
115 <    public static interface MappingFunction<K, V> {
116 <        /**
117 <         * Returns a value for the given key, or null if there is no
118 <         * mapping. If this function throws an (unchecked) exception,
119 <         * the exception is rethrown to its caller, and no mapping is
120 <         * recorded.  Because this function is invoked within
121 <         * atomicity control, the computation should be short and
122 <         * simple. The most common usage is to construct a new object
123 <         * serving as an initial mapped value.
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 ConcurrentHashMapV8<String, Long> m = ...
130 >     * // split as if have 8 * parallelism, for load balance
131 >     * int n = m.size();
132 >     * int p = aForkJoinPool.getParallelism() * 8;
133 >     * int split = (n < p)? n : p;
134 >     * long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null));
135 >     * // ...
136 >     * static class SumValues extends RecursiveTask<Long> {
137 >     *   final Spliterator<Long> s;
138 >     *   final int split;             // split while > 1
139 >     *   final SumValues nextJoin;    // records forked subtasks to join
140 >     *   SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
141 >     *     this.s = s; this.depth = depth; this.nextJoin = nextJoin;
142 >     *   }
143 >     *   public Long compute() {
144 >     *     long sum = 0;
145 >     *     SumValues subtasks = null; // fork subtasks
146 >     *     for (int s = split >>> 1; s > 0; s >>>= 1)
147 >     *       (subtasks = new SumValues(s.split(), s, subtasks)).fork();
148 >     *     while (s.hasNext())        // directly process remaining elements
149 >     *       sum += s.next();
150 >     *     for (SumValues t = subtasks; t != null; t = t.nextJoin)
151 >     *       sum += t.join();         // collect subtask results
152 >     *     return sum;
153 >     *   }
154 >     * }
155 >     * }</pre>
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 <         * @param key the (non-null) key
172 <         * @return a value, or null if none
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 <        V map(K key);
176 >        Spliterator<T> split();
177      }
178  
179      /*
# Line 108 | Line 182 | public class ConcurrentHashMapV8<K, V>
182       * The primary design goal of this hash table is to maintain
183       * concurrent readability (typically method get(), but also
184       * iterators and related methods) while minimizing update
185 <     * contention.
185 >     * contention. Secondary goals are to keep space consumption about
186 >     * the same or better than java.util.HashMap, and to support high
187 >     * initial insertion rates on an empty table by many threads.
188       *
189       * Each key-value mapping is held in a Node.  Because Node fields
190       * can contain special values, they are defined using plain Object
191       * types. Similarly in turn, all internal methods that use them
192 <     * work off Object types. All public generic-typed methods relay
193 <     * in/out of these internal methods, supplying casts as needed.
192 >     * work off Object types. And similarly, so do the internal
193 >     * methods of auxiliary iterator and view classes.  All public
194 >     * generic typed methods relay in/out of these internal methods,
195 >     * supplying null-checks and casts as needed. This also allows
196 >     * many of the public methods to be factored into a smaller number
197 >     * of internal methods (although sadly not so for the five
198 >     * variants of put-related operations). The validation-based
199 >     * approach explained below leads to a lot of code sprawl because
200 >     * retry-control precludes factoring into smaller methods.
201       *
202       * The table is lazily initialized to a power-of-two size upon the
203 <     * first insertion.  Each bin in the table contains a (typically
204 <     * short) list of Nodes.  Table accesses require volatile/atomic
205 <     * reads, writes, and CASes.  Because there is no other way to
206 <     * arrange this without adding further indirections, we use
207 <     * intrinsics (sun.misc.Unsafe) operations.  The lists of nodes
208 <     * within bins are always accurately traversable under volatile
209 <     * reads, so long as lookups check hash code and non-nullness of
210 <     * key and value before checking key equality. (All valid hash
211 <     * codes are nonnegative. Negative values are reserved for special
212 <     * forwarding nodes; see below.)
213 <     *
214 <     * A bin may be locked during update (insert, delete, and replace)
215 <     * operations.  We do not want to waste the space required to
216 <     * associate a distinct lock object with each bin, so instead use
217 <     * the first node of a bin list itself as a lock, using builtin
218 <     * "synchronized" locks. These save space and we can live with
219 <     * only plain block-structured lock/unlock operations. Using the
220 <     * first node of a list as a lock does not by itself suffice
221 <     * though: When a node is locked, any update must first validate
222 <     * that it is still the first node, and retry if not. (Because new
223 <     * nodes are always appended to lists, once a node is first in a
224 <     * bin, it remains first until deleted or the bin becomes
225 <     * invalidated.)  However, update operations can and usually do
226 <     * still traverse the bin until the point of update, which helps
227 <     * reduce cache misses on retries.  This is a converse of sorts to
228 <     * the lazy locking technique described by Herlihy & Shavit. If
229 <     * there is no existing node during a put operation, then one can
230 <     * be CAS'ed in (without need for lock except in computeIfAbsent);
231 <     * the CAS serves as validation. This is on average the most
232 <     * common case for put operations -- under random hash codes, the
233 <     * distribution of nodes in bins follows a Poisson distribution
234 <     * (see http://en.wikipedia.org/wiki/Poisson_distribution) with a
235 <     * parameter of 0.5 on average under the default loadFactor of
236 <     * 0.75.  The expected number of locks covering different elements
237 <     * (i.e., bins with 2 or more nodes) is approximately 10% at
238 <     * steady state under default settings.  Lock contention
239 <     * probability for two threads accessing arbitrary distinct
240 <     * elements is, roughly, 1 / (8 * #elements).
241 <     *
242 <     * The table is resized when occupancy exceeds a threshold.  Only
243 <     * a single thread performs the resize (using field "resizing", to
244 <     * arrange exclusion), but the table otherwise remains usable for
245 <     * both reads and updates. Resizing proceeds by transferring bins,
246 <     * one by one, from the table to the next table.  Upon transfer,
247 <     * the old table bin contains only a special forwarding node (with
248 <     * negative hash code ("MOVED")) that contains the next table as
203 >     * first insertion.  Each bin in the table normally contains a
204 >     * list of Nodes (most often, the list has only zero or one Node).
205 >     * Table accesses require volatile/atomic reads, writes, and
206 >     * CASes.  Because there is no other way to arrange this without
207 >     * adding further indirections, we use intrinsics
208 >     * (sun.misc.Unsafe) operations.  The lists of nodes within bins
209 >     * are always accurately traversable under volatile reads, so long
210 >     * as lookups check hash code and non-nullness of value before
211 >     * checking key equality.
212 >     *
213 >     * 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).
226 >     *
227 >     * Insertion (via put or its variants) of the first node in an
228 >     * empty bin is performed by just CASing it to the bin.  This is
229 >     * by far the most common case for put operations under most
230 >     * key/hash distributions.  Other update operations (insert,
231 >     * delete, and replace) require locks.  We do not want to waste
232 >     * the space required to associate a distinct lock object with
233 >     * each bin, so instead use the first node of a bin list itself as
234 >     * a lock. Blocking support for these locks relies on the builtin
235 >     * "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.
240 >     *
241 >     * Using the first node of a list as a lock does not by itself
242 >     * suffice though: When a node is locked, any update must first
243 >     * validate that it is still the first node after locking it, and
244 >     * retry if not. Because new nodes are always appended to lists,
245 >     * once a node is first in a bin, it remains first until deleted
246 >     * 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.
250 >     *
251 >     * The main disadvantage of per-bin locks is that other update
252 >     * operations on other nodes in a bin list protected by the same
253 >     * lock can stall, for example when user equals() or mapping
254 >     * functions take a long time.  However, statistically, under
255 >     * random hash codes, this is not a common problem.  Ideally, the
256 >     * frequency of nodes in bins follows a Poisson distribution
257 >     * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
258 >     * parameter of about 0.5 on average, given the resizing threshold
259 >     * of 0.75, although with a large variance because of resizing
260 >     * granularity. Ignoring variance, the expected occurrences of
261 >     * list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The
262 >     * first values are:
263 >     *
264 >     * 0:    0.60653066
265 >     * 1:    0.30326533
266 >     * 2:    0.07581633
267 >     * 3:    0.01263606
268 >     * 4:    0.00157952
269 >     * 5:    0.00015795
270 >     * 6:    0.00001316
271 >     * 7:    0.00000094
272 >     * 8:    0.00000006
273 >     * more: less than 1 in ten million
274 >     *
275 >     * Lock contention probability for two threads accessing distinct
276 >     * elements is roughly 1 / (8 * #elements) under random hashes.
277 >     *
278 >     * Actual hash code distributions encountered in practice
279 >     * sometimes deviate significantly from uniform randomness.  This
280 >     * includes the case when N > (1<<30), so some keys MUST collide.
281 >     * Similarly for dumb or hostile usages in which multiple keys are
282 >     * designed to have identical hash codes. Also, although we guard
283 >     * against the worst effects of this (see method spread), sets of
284 >     * hashes may differ only in bits that do not impact their bin
285 >     * index for a given power-of-two mask.  So we use a secondary
286 >     * strategy that applies when the number of nodes in a bin exceeds
287 >     * 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
291 >     * slow as in a regular list, but given that N cannot exceed
292 >     * (1<<64) (before running out of addresses) this bounds search
293 >     * steps, lock hold times, etc, to reasonable constants (roughly
294 >     * 100 nodes inspected per operation worst case) so long as keys
295 >     * are Comparable (which is very common -- String, Long, etc).
296 >     * TreeBin nodes (TreeNodes) also maintain the same "next"
297 >     * traversal pointers as regular nodes, so can be traversed in
298 >     * iterators in the same way.
299 >     *
300 >     * The table is resized when occupancy exceeds a percentage
301 >     * threshold (nominally, 0.75, but see below).  Only a single
302 >     * thread performs the resize (using field "sizeCtl", to arrange
303 >     * exclusion), but the table otherwise remains usable for reads
304 >     * and updates. Resizing proceeds by transferring bins, one by
305 >     * one, from the table to the next table.  Because we are using
306 >     * power-of-two expansion, the elements from each bin must either
307 >     * stay at same index, or move with a power of two offset. We
308 >     * eliminate unnecessary node creation by catching cases where old
309 >     * nodes can be reused because their next fields won't change.  On
310 >     * average, only about one-sixth of them need cloning when a table
311 >     * doubles. The nodes they replace will be garbage collectable as
312 >     * soon as they are no longer referenced by any reader thread that
313 >     * may be in the midst of concurrently traversing table.  Upon
314 >     * transfer, the old table bin contains only a special forwarding
315 >     * node (with hash field "MOVED") that contains the next table as
316       * its key. On encountering a forwarding node, access and update
317 <     * operations restart, using the new table. To ensure concurrent
318 <     * readability of traversals, transfers must proceed from the last
319 <     * bin (table.length - 1) up towards the first.  Any traversal
320 <     * starting from the first bin can then arrange to move to the new
321 <     * table for the rest of the traversal without revisiting nodes.
322 <     * This constrains bin transfers to a particular order, and so can
323 <     * block indefinitely waiting for the next lock, and other threads
324 <     * cannot help with the transfer. However, expected stalls are
325 <     * infrequent enough to not warrant the additional overhead and
326 <     * complexity of access and iteration schemes that could admit
327 <     * out-of-order or concurrent bin transfers.
328 <     *
329 <     * A similar traversal scheme (not yet implemented) can apply to
330 <     * partial traversals during partitioned aggregate operations.
331 <     * Also, read-only operations give up if ever forwarded to a null
332 <     * table, which provides support for shutdown-style clearing,
333 <     * which is also not currently implemented.
317 >     * operations restart, using the new table.
318 >     *
319 >     * Each bin transfer requires its bin lock. However, unlike other
320 >     * cases, a transfer can skip a bin if it fails to acquire its
321 >     * lock, and revisit it later (unless it is a TreeBin). Method
322 >     * rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that
323 >     * have been skipped because of failure to acquire a lock, and
324 >     * blocks only if none are available (i.e., only very rarely).
325 >     * The transfer operation must also ensure that all accessible
326 >     * bins in both the old and new table are usable by any traversal.
327 >     * When there are no lock acquisition failures, this is arranged
328 >     * simply by proceeding from the last bin (table.length - 1) up
329 >     * towards the first.  Upon seeing a forwarding node, traversals
330 >     * (see class Iter) arrange to move to the new table
331 >     * without revisiting nodes.  However, when any node is skipped
332 >     * during a transfer, all earlier table bins may have become
333 >     * visible, so are initialized with a reverse-forwarding node back
334 >     * to the old table until the new ones are established. (This
335 >     * sometimes requires transiently locking a forwarding node, which
336 >     * is possible under the above encoding.) These more expensive
337 >     * mechanics trigger only when necessary.
338 >     *
339 >     * The traversal scheme also applies to partial traversals of
340 >     * ranges of bins (via an alternate Traverser constructor)
341 >     * to support partitioned aggregate operations.  Also, read-only
342 >     * operations give up if ever forwarded to a null table, which
343 >     * provides support for shutdown-style clearing, which is also not
344 >     * currently implemented.
345 >     *
346 >     * Lazy table initialization minimizes footprint until first use,
347 >     * and also avoids resizings when the first operation is from a
348 >     * putAll, constructor with map argument, or deserialization.
349 >     * These cases attempt to override the initial capacity settings,
350 >     * but harmlessly fail to take effect in cases of races.
351       *
352       * The element count is maintained using a LongAdder, which avoids
353       * contention on updates but can encounter cache thrashing if read
354 <     * too frequently during concurrent updates. To avoid reading so
355 <     * often, resizing is normally attempted only upon adding to a bin
356 <     * already holding two or more nodes. Under the default threshold
357 <     * (0.75), and uniform hash distributions, the probability of this
358 <     * occurring at threshold is around 13%, meaning that only about 1
359 <     * in 8 puts check threshold (and after resizing, many fewer do
360 <     * so). But this approximation has high variance for small table
361 <     * sizes, so we check on any collision for sizes <= 64.  Further,
362 <     * to increase the probability that a resize occurs soon enough, we
363 <     * offset the threshold (see THRESHOLD_OFFSET) by the expected
364 <     * number of puts between checks. This is currently set to 8, in
365 <     * accord with the default load factor. In practice, this is
366 <     * rarely overridden, and in any case is close enough to other
367 <     * plausible values not to waste dynamic probability computation
368 <     * for more precision.
354 >     * too frequently during concurrent access. To avoid reading so
355 >     * often, resizing is attempted either when a bin lock is
356 >     * contended, or upon adding to a bin already holding two or more
357 >     * nodes (checked before adding in the xIfAbsent methods, after
358 >     * adding in others). Under uniform hash distributions, the
359 >     * probability of this occurring at threshold is around 13%,
360 >     * meaning that only about 1 in 8 puts check threshold (and after
361 >     * resizing, many fewer do so). But this approximation has high
362 >     * variance for small table sizes, so we check on any collision
363 >     * for sizes <= 64. The bulk putAll operation further reduces
364 >     * contention by only committing count updates upon these size
365 >     * checks.
366 >     *
367 >     * Maintaining API and serialization compatibility with previous
368 >     * versions of this class introduces several oddities. Mainly: We
369 >     * leave untouched but unused constructor arguments refering to
370 >     * concurrencyLevel. We accept a loadFactor constructor argument,
371 >     * but apply it only to initial table capacity (which is the only
372 >     * time that we can guarantee to honor it.) We also declare an
373 >     * unused "Segment" class that is instantiated in minimal form
374 >     * only when serializing.
375       */
376  
377      /* ---------------- Constants -------------- */
378  
379      /**
380 <     * The smallest allowed table capacity.  Must be a power of 2, at
381 <     * least 2.
380 >     * The largest possible table capacity.  This value must be
381 >     * exactly 1<<30 to stay within Java array allocation and indexing
382 >     * bounds for power of two table sizes, and is further required
383 >     * because the top two bits of 32bit hash fields are used for
384 >     * control purposes.
385       */
386 <    static final int MINIMUM_CAPACITY = 2;
386 >    private static final int MAXIMUM_CAPACITY = 1 << 30;
387  
388      /**
389 <     * The largest allowed table capacity.  Must be a power of 2, at
390 <     * most 1<<30.
389 >     * The default initial table capacity.  Must be a power of 2
390 >     * (i.e., at least 1) and at most MAXIMUM_CAPACITY.
391       */
392 <    static final int MAXIMUM_CAPACITY = 1 << 30;
392 >    private static final int DEFAULT_CAPACITY = 16;
393  
394      /**
395 <     * The default initial table capacity.  Must be a power of 2, at
396 <     * least MINIMUM_CAPACITY and at most MAXIMUM_CAPACITY
395 >     * The largest possible (non-power of two) array size.
396 >     * Needed by toArray and related methods.
397       */
398 <    static final int DEFAULT_CAPACITY = 16;
398 >    static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
399  
400      /**
401 <     * The default load factor for this table, used when not otherwise
402 <     * specified in a constructor.
401 >     * The default concurrency level for this table. Unused but
402 >     * defined for compatibility with previous versions of this class.
403       */
404 <    static final float DEFAULT_LOAD_FACTOR = 0.75f;
404 >    private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
405  
406      /**
407 <     * The default concurrency level for this table. Unused, but
408 <     * defined for compatibility with previous versions of this class.
407 >     * The load factor for this table. Overrides of this value in
408 >     * constructors affect only the initial table capacity.  The
409 >     * actual floating point value isn't normally used -- it is
410 >     * simpler to use expressions such as {@code n - (n >>> 2)} for
411 >     * the associated resizing threshold.
412       */
413 <    static final int DEFAULT_CONCURRENCY_LEVEL = 16;
413 >    private static final float LOAD_FACTOR = 0.75f;
414  
415      /**
416 <     * The count value to offset thresholds to compensate for checking
417 <     * for resizing only when inserting into bins with two or more
418 <     * elements. See above for explanation.
416 >     * The buffer size for skipped bins during transfers. The
417 >     * value is arbitrary but should be large enough to avoid
418 >     * most locking stalls during resizes.
419       */
420 <    static final int THRESHOLD_OFFSET = 8;
420 >    private static final int TRANSFER_BUFFER_SIZE = 32;
421  
422      /**
423 <     * Special node hash value indicating to use table in node.key
424 <     * Must be negative.
423 >     * The bin count threshold for using a tree rather than list for a
424 >     * bin.  The value reflects the approximate break-even point for
425 >     * using tree-based operations.
426       */
427 <    static final int MOVED = -1;
427 >    private static final int TREE_THRESHOLD = 8;
428 >
429 >    /*
430 >     * Encodings for special uses of Node hash fields. See above for
431 >     * explanation.
432 >     */
433 >    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 -------------- */
439  
440      /**
441       * The array of bins. Lazily initialized upon first insertion.
442 <     * Size is always a power of two. Accessed directly by inner
254 <     * classes.
442 >     * Size is always a power of two. Accessed directly by iterators.
443       */
444      transient volatile Node[] table;
445  
446 <    /** The counter maintaining number of elements. */
446 >    /**
447 >     * The counter maintaining number of elements.
448 >     */
449      private transient final LongAdder counter;
450 <    /** Nonzero when table is being initialized or resized. Updated via CAS. */
451 <    private transient volatile int resizing;
452 <    /** The target load factor for the table. */
453 <    private transient float loadFactor;
454 <    /** The next element count value upon which to resize the table. */
455 <    private transient int threshold;
456 <    /** The initial capacity of the table. */
457 <    private transient int initCap;
450 >
451 >    /**
452 >     * Table initialization and resizing control.  When negative, the
453 >     * 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.
457 >     */
458 >    private transient volatile int sizeCtl;
459  
460      // views
461 <    transient Set<K> keySet;
462 <    transient Set<Map.Entry<K,V>> entrySet;
463 <    transient Collection<V> values;
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 <    Segment<K,V>[] segments;
466 >    private Segment<K,V>[] segments;
467  
468 <    /**
469 <     * Applies a supplemental hash function to a given hashCode, which
470 <     * defends against poor quality hash functions.  The result must
471 <     * be non-negative, and for reasonable performance must have good
472 <     * avalanche properties; i.e., that each bit of the argument
473 <     * affects each bit (except sign bit) of the result.
468 >    /* ---------------- Table element access -------------- */
469 >
470 >    /*
471 >     * 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.
480       */
481 <    private static final int spread(int h) {
482 <        // Apply base step of MurmurHash; see http://code.google.com/p/smhasher/
483 <        h ^= h >>> 16;
484 <        h *= 0x85ebca6b;
485 <        h ^= h >>> 13;
486 <        h *= 0xc2b2ae35;
487 <        return (h >>> 16) ^ (h & 0x7fffffff); // mask out sign bit
481 >
482 >    static final Node tabAt(Node[] tab, int i) { // used by Iter
483 >        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
484 >    }
485 >
486 >    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
487 >        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
488      }
489  
490 +    private static final void setTabAt(Node[] tab, int i, Node v) {
491 +        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
492 +    }
493 +
494 +    /* ---------------- Nodes -------------- */
495 +
496      /**
497       * Key-value entry. Note that this is never exported out as a
498 <     * user-visible Map.Entry.
498 >     * user-visible Map.Entry (see MapEntry below). Nodes with a hash
499 >     * field of MOVED are special, and do not contain user keys or
500 >     * values.  Otherwise, keys are never null, and null val fields
501 >     * indicate that a node is in the process of being deleted or
502 >     * created. For purposes of read-only access, a key may be read
503 >     * before a val, but can only be used after checking val to be
504 >     * non-null.
505       */
506 <    static final class Node {
507 <        final int hash;
506 >    static class Node {
507 >        volatile int hash;
508          final Object key;
509          volatile Object val;
510          volatile Node next;
# Line 306 | Line 515 | public class ConcurrentHashMapV8<K, V>
515              this.val = val;
516              this.next = next;
517          }
309    }
518  
519 <    /*
520 <     * Volatile access methods are used for table elements as well as
521 <     * elements of in-progress next table while resizing.  Uses in
522 <     * access and update methods are null checked by callers, and
315 <     * implicitly bounds-checked, relying on the invariants that tab
316 <     * arrays have non-zero size, and all indices are masked with
317 <     * (tab.length - 1) which is never negative and always less than
318 <     * length. The "relaxed" non-volatile forms are used only during
319 <     * table initialization. The only other usage is in
320 <     * HashIterator.advance, which performs explicit checks.
321 <     */
519 >        /** CompareAndSet the hash field */
520 >        final boolean casHash(int cmp, int val) {
521 >            return UNSAFE.compareAndSwapInt(this, hashOffset, cmp, val);
522 >        }
523  
524 <    static final Node tabAt(Node[] tab, int i) { // used in HashIterator
525 <        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
526 <    }
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 <    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
529 <        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
530 <    }
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 <    private static final void setTabAt(Node[] tab, int i, Node v) {
571 <        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
572 <    }
570 >        // Unsafe mechanics for casHash
571 >        private static final sun.misc.Unsafe UNSAFE;
572 >        private static final long hashOffset;
573  
574 <    private static final Node relaxedTabAt(Node[] tab, int i) {
575 <        return (Node)UNSAFE.getObject(tab, ((long)i<<ASHIFT)+ABASE);
574 >        static {
575 >            try {
576 >                UNSAFE = getUnsafe();
577 >                Class<?> k = Node.class;
578 >                hashOffset = UNSAFE.objectFieldOffset
579 >                    (k.getDeclaredField("hash"));
580 >            } catch (Exception e) {
581 >                throw new Error(e);
582 >            }
583 >        }
584      }
585  
586 <    private static final void relaxedSetTabAt(Node[] tab, int i, Node v) {
587 <        UNSAFE.putObject(tab, ((long)i<<ASHIFT)+ABASE, v);
586 >    /* ---------------- TreeBins -------------- */
587 >
588 >    /**
589 >     * Nodes for use in TreeBins
590 >     */
591 >    static final class TreeNode extends Node {
592 >        TreeNode parent;  // red-black tree links
593 >        TreeNode left;
594 >        TreeNode right;
595 >        TreeNode prev;    // needed to unlink next upon deletion
596 >        boolean red;
597 >
598 >        TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) {
599 >            super(hash, key, val, next);
600 >            this.parent = parent;
601 >        }
602      }
603  
604 <    /* ---------------- Access and update operations -------------- */
604 >    /**
605 >     * A specialized form of red-black tree for use in bins
606 >     * whose size exceeds a threshold.
607 >     *
608 >     * TreeBins use a special form of comparison for search and
609 >     * related operations (which is the main reason we cannot use
610 >     * existing collections such as TreeMaps). TreeBins contain
611 >     * Comparable elements, but may contain others, as well as
612 >     * elements that are Comparable but not necessarily Comparable<T>
613 >     * for the same T, so we cannot invoke compareTo among them. To
614 >     * handle this, the tree is ordered primarily by hash value, then
615 >     * by getClass().getName() order, and then by Comparator order
616 >     * among elements of the same class.  On lookup at a node, if
617 >     * elements are not comparable or compare as 0, both left and
618 >     * right children may need to be searched in the case of tied hash
619 >     * values. (This corresponds to the full list search that would be
620 >     * necessary if all elements were non-Comparable and had tied
621 >     * hashes.)  The red-black balancing code is updated from
622 >     * pre-jdk-collections
623 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
624 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
625 >     * Algorithms" (CLR).
626 >     *
627 >     * TreeBins also maintain a separate locking discipline than
628 >     * regular bins. Because they are forwarded via special MOVED
629 >     * nodes at bin heads (which can never change once established),
630 >     * we cannot use those nodes as locks. Instead, TreeBin
631 >     * extends AbstractQueuedSynchronizer to support a simple form of
632 >     * read-write lock. For update operations and table validation,
633 >     * the exclusive form of lock behaves in the same way as bin-head
634 >     * locks. However, lookups use shared read-lock mechanics to allow
635 >     * multiple readers in the absence of writers.  Additionally,
636 >     * these lookups do not ever block: While the lock is not
637 >     * available, they proceed along the slow traversal path (via
638 >     * next-pointers) until the lock becomes available or the list is
639 >     * exhausted, whichever comes first. (These cases are not fast,
640 >     * but maximize aggregate expected throughput.)  The AQS mechanics
641 >     * for doing this are straightforward.  The lock state is held as
642 >     * AQS getState().  Read counts are negative; the write count (1)
643 >     * is positive.  There are no signalling preferences among readers
644 >     * and writers. Since we don't need to export full Lock API, we
645 >     * just override the minimal AQS methods and use them directly.
646 >     */
647 >    static final class TreeBin extends AbstractQueuedSynchronizer {
648 >        private static final long serialVersionUID = 2249069246763182397L;
649 >        transient TreeNode root;  // root of tree
650 >        transient TreeNode first; // head of next-pointer list
651  
652 <    /** Implementation for get and containsKey **/
653 <    private final Object internalGet(Object k) {
654 <        int h = spread(k.hashCode());
655 <        Node[] tab = table;
656 <        retry: while (tab != null) {
657 <            Node e = tabAt(tab, (tab.length - 1) & h);
658 <            while (e != null) {
659 <                int eh = e.hash;
660 <                if (eh == h) {
661 <                    Object ek = e.key, ev = e.val;
662 <                    if (ev != null && ek != null && (k == ek || k.equals(ek)))
663 <                        return ev;
664 <                }
665 <                else if (eh < 0) { // bin was moved during resize
666 <                    tab = (Node[])e.key;
667 <                    continue retry;
652 >        /* AQS overrides */
653 >        public final boolean isHeldExclusively() { return getState() > 0; }
654 >        public final boolean tryAcquire(int ignore) {
655 >            if (compareAndSetState(0, 1)) {
656 >                setExclusiveOwnerThread(Thread.currentThread());
657 >                return true;
658 >            }
659 >            return false;
660 >        }
661 >        public final boolean tryRelease(int ignore) {
662 >            setExclusiveOwnerThread(null);
663 >            setState(0);
664 >            return true;
665 >        }
666 >        public final int tryAcquireShared(int ignore) {
667 >            for (int c;;) {
668 >                if ((c = getState()) > 0)
669 >                    return -1;
670 >                if (compareAndSetState(c, c -1))
671 >                    return 1;
672 >            }
673 >        }
674 >        public final boolean tryReleaseShared(int ignore) {
675 >            int c;
676 >            do {} while (!compareAndSetState(c = getState(), c + 1));
677 >            return c == -1;
678 >        }
679 >
680 >        /** From CLR */
681 >        private void rotateLeft(TreeNode p) {
682 >            if (p != null) {
683 >                TreeNode r = p.right, pp, rl;
684 >                if ((rl = p.right = r.left) != null)
685 >                    rl.parent = p;
686 >                if ((pp = r.parent = p.parent) == null)
687 >                    root = r;
688 >                else if (pp.left == p)
689 >                    pp.left = r;
690 >                else
691 >                    pp.right = r;
692 >                r.left = p;
693 >                p.parent = r;
694 >            }
695 >        }
696 >
697 >        /** From CLR */
698 >        private void rotateRight(TreeNode p) {
699 >            if (p != null) {
700 >                TreeNode l = p.left, pp, lr;
701 >                if ((lr = p.left = l.right) != null)
702 >                    lr.parent = p;
703 >                if ((pp = l.parent = p.parent) == null)
704 >                    root = l;
705 >                else if (pp.right == p)
706 >                    pp.right = l;
707 >                else
708 >                    pp.left = l;
709 >                l.right = p;
710 >                p.parent = l;
711 >            }
712 >        }
713 >
714 >        /**
715 >         * 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 <                e = e.next;
741 >                else
742 >                    dir = (h < ph) ? -1 : 1;
743 >                p = (dir > 0) ? p.right : p.left;
744              }
745 <            break;
745 >            return null;
746          }
366        return null;
367    }
747  
748 <    /** Implementation for put and putIfAbsent **/
749 <    private final Object internalPut(Object k, Object v, boolean replace) {
750 <        int h = spread(k.hashCode());
751 <        Object oldVal = null;  // the previous value or null if none
752 <        Node[] tab = table;
753 <        for (;;) {
754 <            Node e; int i;
755 <            if (tab == null)
756 <                tab = grow(0);
757 <            else if ((e = tabAt(tab, i = (tab.length - 1) & h)) == null) {
758 <                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
748 >        /**
749 >         * Wrapper for getTreeNode used by CHM.get. Tries to obtain
750 >         * read-lock to call getTreeNode, but during failure to get
751 >         * lock, searches along next links.
752 >         */
753 >        final Object getValue(int h, Object k) {
754 >            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;
773 +        }
774 +
775 +        /**
776 +         * Finds or adds a node.
777 +         * @return null if added
778 +         */
779 +        @SuppressWarnings("unchecked") // suppress Comparable cast warning
780 +            final TreeNode putTreeNode(int h, Object k, Object v) {
781 +            Class<?> c = k.getClass();
782 +            TreeNode pp = root, p = null;
783 +            int dir = 0;
784 +            while (pp != null) { // find existing node or leaf to insert at
785 +                int ph;  Object pk; Class<?> pc;
786 +                p = pp;
787 +                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;
874 +            }
875 +            return null;
876 +        }
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              }
382            else if (e.hash < 0)
383                tab = (Node[])e.key;
943              else {
944 <                boolean validated = false;
945 <                boolean checkSize = false;
946 <                synchronized (e) {
947 <                    Node first = e;
948 <                    for (;;) {
949 <                        Object ek, ev;
950 <                        if ((ev = e.val) == null)
951 <                            break;
952 <                        if (e.hash == h && (ek = e.key) != null &&
953 <                            (k == ek || k.equals(ek))) {
954 <                            if (tabAt(tab, i) == first) {
955 <                                validated = true;
956 <                                oldVal = ev;
957 <                                if (replace)
958 <                                    e.val = v;
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                              }
401                            break;
997                          }
998 <                        Node last = e;
999 <                        if ((e = e.next) == null) {
1000 <                            if (tabAt(tab, i) == first) {
1001 <                                validated = true;
1002 <                                last.next = new Node(h, k, v, null);
1003 <                                if (last != first || tab.length <= 64)
1004 <                                    checkSize = true;
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                              }
411                            break;
1035                          }
1036                      }
1037                  }
1038 <                if (validated) {
1039 <                    if (checkSize && tab.length < MAXIMUM_CAPACITY &&
1040 <                        resizing == 0 && counter.sum() >= threshold)
1041 <                        grow(0);
1042 <                    break;
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 >        }
1047 >    }
1048 >
1049 >    /* ---------------- Collision reduction methods -------------- */
1050 >
1051 >    /**
1052 >     * Spreads higher bits to lower, and also forces top 2 bits to 0.
1053 >     * Because the table uses power-of-two masking, sets of hashes
1054 >     * that vary only in bits above the current mask will always
1055 >     * collide. (Among known examples are sets of Float keys holding
1056 >     * consecutive whole numbers in small tables.)  To counter this,
1057 >     * we apply a transform that spreads the impact of higher bits
1058 >     * downward. There is a tradeoff between speed, utility, and
1059 >     * quality of bit-spreading. Because many common sets of hashes
1060 >     * are already reasonably distributed across bits (so don't benefit
1061 >     * from spreading), and because we use trees to handle large sets
1062 >     * of collisions in bins, we don't need excessively high quality.
1063 >     */
1064 >    private static final int spread(int h) {
1065 >        h ^= (h >>> 18) ^ (h >>> 12);
1066 >        return (h ^ (h >>> 10)) & HASH_BITS;
1067 >    }
1068 >
1069 >    /**
1070 >     * Replaces a list bin with a tree bin. Call only when locked.
1071 >     * Fails to replace if the given key is non-comparable or table
1072 >     * is, or needs, resizing.
1073 >     */
1074 >    private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
1075 >        if ((key instanceof Comparable) &&
1076 >            (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 >        }
1082 >    }
1083 >
1084 >    /* ---------------- Internal access and update methods -------------- */
1085 >
1086 >    /** Implementation for get and containsKey */
1087 >    private final Object internalGet(Object k) {
1088 >        int h = spread(k.hashCode());
1089 >        retry: for (Node[] tab = table; tab != null;) {
1090 >            Node e, p; Object ek, ev; int eh;      // locals to read fields once
1091 >            for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
1092 >                if ((eh = e.hash) == MOVED) {
1093 >                    if ((ek = e.key) instanceof TreeBin)  // search TreeBin
1094 >                        return ((TreeBin)ek).getValue(h, k);
1095 >                    else {                        // restart with new table
1096 >                        tab = (Node[])ek;
1097 >                        continue retry;
1098 >                    }
1099                  }
1100 +                else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1101 +                         ((ek = e.key) == k || k.equals(ek)))
1102 +                    return ev;
1103              }
1104 +            break;
1105          }
1106 <        if (oldVal == null)
424 <            counter.increment();
425 <        return oldVal;
1106 >        return null;
1107      }
1108  
1109      /**
1110 <     * Covers the four public remove/replace methods: Replaces node
1111 <     * value with v, conditional upon match of cv if non-null.  If
1112 <     * resulting value is null, delete.
1110 >     * Implementation for the four public remove/replace methods:
1111 >     * Replaces node value with v, conditional upon match of cv if
1112 >     * non-null.  If resulting value is null, delete.
1113       */
1114      private final Object internalReplace(Object k, Object v, Object cv) {
1115          int h = spread(k.hashCode());
1116          Object oldVal = null;
1117 <        Node e; int i;
1118 <        Node[] tab = table;
1119 <        while (tab != null &&
1120 <               (e = tabAt(tab, i = (tab.length - 1) & h)) != null) {
1121 <            if (e.hash < 0)
1122 <                tab = (Node[])e.key;
1123 <            else {
1117 >        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 <                synchronized (e) {
1165 <                    Node pred = null;
1166 <                    Node first = e;
1167 <                    for (;;) {
1168 <                        Object ek, ev;
1169 <                        if ((ev = e.val) == null)
1170 <                            break;
1171 <                        if (e.hash == h && (ek = e.key) != null &&
453 <                            (k == ek || k.equals(ek))) {
454 <                            if (tabAt(tab, i) == first) {
455 <                                validated = true;
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) {
# Line 464 | Line 1180 | public class ConcurrentHashMapV8<K, V>
1180                                              setTabAt(tab, i, en);
1181                                      }
1182                                  }
1183 +                                break;
1184                              }
1185 <                            break;
1186 <                        }
1187 <                        pred = e;
471 <                        if ((e = e.next) == null) {
472 <                            if (tabAt(tab, i) == first)
473 <                                validated = true;
474 <                            break;
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.decrement();
1198 >                        counter.add(-1L);
1199                      break;
1200                  }
1201              }
# Line 485 | Line 1203 | public class ConcurrentHashMapV8<K, V>
1203          return oldVal;
1204      }
1205  
1206 <    /** Implementation for computeIfAbsent and compute */
1207 <    @SuppressWarnings("unchecked")
1208 <    private final V internalCompute(K k,
1209 <                                    MappingFunction<? super K, ? extends V> f,
1210 <                                    boolean replace) {
1206 >    /*
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 <        V val = null;
1236 <        boolean added = false;
1237 <        boolean validated = false;
497 <        Node[] tab = table;
498 <        do {
499 <            Node e; int i;
1235 >        int count = 0;
1236 >        for (Node[] tab = table;;) {
1237 >            int i; Node f; int fh; Object fk;
1238              if (tab == null)
1239 <                tab = grow(0);
1240 <            else if ((e = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1241 <                Node node = new Node(h, k, null, null);
1242 <                synchronized (node) {
1243 <                    if (casTabAt(tab, i, null, node)) {
1244 <                        validated = true;
1245 <                        try {
1246 <                            val = f.map(k);
1247 <                            if (val != null) {
1248 <                                node.val = val;
1249 <                                added = true;
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                              }
513                        } finally {
514                            if (!added)
515                                setTabAt(tab, i, null);
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 (e.hash < 0)
1275 <                tab = (Node[])e.key;
1276 <            else if (Thread.holdsLock(e))
1277 <                throw new IllegalStateException("Recursive map computation");
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 <                boolean checkSize = false;
1359 <                synchronized (e) {
1360 <                    Node first = e;
528 <                    for (;;) {
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 ((ev = e.val) == null)
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 <                        if (e.hash == h && (ek = e.key) != null &&
1369 <                            (k == ek || k.equals(ek))) {
1370 <                            if (tabAt(tab, i) == first) {
1371 <                                validated = true;
1372 <                                if (replace && (ev = f.map(k)) != null)
1373 <                                    e.val = ev;
1374 <                                val = (V)ev;
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                              }
540                            break;
1396                          }
1397 <                        Node last = e;
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 >    }
1418 >
1419 >    /** Implementation for computeIfAbsent */
1420 >    private final Object internalComputeIfAbsent(K k,
1421 >                                                 Fun<? super K, ?> mf) {
1422 >        int h = spread(k.hashCode());
1423 >        Object val = null;
1424 >        int count = 0;
1425 >        for (Node[] tab = table;;) {
1426 >            Node f; int i, fh; Object fk, fv;
1427 >            if (tab == null)
1428 >                tab = initTable();
1429 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1430 >                Node node = new Node(fh = h | LOCKED, k, null, null);
1431 >                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 <                            if (tabAt(tab, i) == first) {
1490 <                                validated = true;
1491 <                                if ((val = f.map(k)) != null) {
1492 <                                    last.next = new Node(h, k, val, null);
1493 <                                    added = true;
1494 <                                    if (last != first || tab.length <= 64)
1495 <                                        checkSize = true;
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 <                            break;
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 <                if (checkSize && tab.length < MAXIMUM_CAPACITY &&
1538 <                    resizing == 0 && counter.sum() >= threshold)
1539 <                    grow(0);
1540 <            }
1541 <        } while (!validated);
1542 <        if (added)
1543 <            counter.increment();
1537 >            }
1538 >        }
1539 >        if (val != null) {
1540 >            counter.add(1L);
1541 >            if (count > 1)
1542 >                checkForResize();
1543 >        }
1544          return val;
1545      }
1546  
1547 <    /*
1548 <     * Reclassifies nodes in each bin to new table.  Because we are
1549 <     * using power-of-two expansion, the elements from each bin must
1550 <     * either stay at same index, or move with a power of two
1551 <     * offset. We eliminate unnecessary node creation by catching
1552 <     * cases where old nodes can be reused because their next fields
1553 <     * won't change.  Statistically, at the default threshold, only
1554 <     * about one-sixth of them need cloning when a table doubles. The
1555 <     * nodes they replace will be garbage collectable as soon as they
1556 <     * are no longer referenced by any reader thread that may be in
1557 <     * the midst of concurrently traversing table.
1558 <     *
1559 <     * Transfers are done from the bottom up to preserve iterator
1560 <     * traversability. On each step, the old bin is locked,
1561 <     * moved/copied, and then replaced with a forwarding node.
1562 <     */
1563 <    private static final void transfer(Node[] tab, Node[] nextTab) {
1564 <        int n = tab.length;
1565 <        int mask = nextTab.length - 1;
1566 <        Node fwd = new Node(MOVED, nextTab, null, null);
1567 <        for (int i = n - 1; i >= 0; --i) {
1568 <            for (Node e;;) {
1569 <                if ((e = tabAt(tab, i)) == null) {
1570 <                    if (casTabAt(tab, i, e, fwd))
1547 >    /** Implementation for compute */
1548 >    @SuppressWarnings("unchecked")
1549 >        private final Object internalCompute(K k, boolean onlyIfPresent,
1550 >                                             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 <                    boolean validated = false;
1613 <                    synchronized (e) {
1614 <                        int idx = e.hash & mask;
1615 <                        Node lastRun = e;
1616 <                        for (Node p = e.next; p != null; p = p.next) {
1617 <                            int j = p.hash & mask;
1618 <                            if (j != idx) {
1619 <                                idx = j;
1620 <                                lastRun = p;
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 <                        if (tabAt(tab, i) == e) {
1652 <                            validated = true;
1653 <                            relaxedSetTabAt(nextTab, idx, lastRun);
1654 <                            for (Node p = e; p != lastRun; p = p.next) {
1655 <                                int h = p.hash;
1656 <                                int j = h & mask;
1657 <                                Node r = relaxedTabAt(nextTab, j);
1658 <                                relaxedSetTabAt(nextTab, j,
1659 <                                                new Node(h, p.key, p.val, r));
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;
1671 >    }
1672 >
1673 >    private final Object internalMerge(K k, V v,
1674 >                                       BiFun<? super V, ? super V, ? extends V> mf) {
1675 >        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                              }
615                            setTabAt(tab, i, fwd);
1712                          }
1713 +                    } finally {
1714 +                        t.release(0);
1715                      }
1716 <                    if (validated)
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;
1778      }
1779  
1780 <    /**
1781 <     * If not already resizing, initializes or creates next table and
1782 <     * transfers bins. Rechecks occupancy after a transfer to see if
1783 <     * another resize is already needed because resizings are lagging
1784 <     * additions.
1785 <     *
1786 <     * @param sizeHint overridden capacity target (nonzero only from putAll)
1787 <     * @return current table
1788 <     */
1789 <    private final Node[] grow(int sizeHint) {
1790 <        if (resizing == 0 &&
1791 <            UNSAFE.compareAndSwapInt(this, resizingOffset, 0, 1)) {
1792 <            try {
1793 <                for (;;) {
1794 <                    int cap, n;
1795 <                    Node[] tab = table;
1796 <                    if (tab == null) {
1797 <                        int c = initCap;
1798 <                        if (c < sizeHint)
1799 <                            c = sizeHint;
1800 <                        if (c == DEFAULT_CAPACITY)
1801 <                            cap = c;
1802 <                        else if (c >= MAXIMUM_CAPACITY)
1803 <                            cap = MAXIMUM_CAPACITY;
1804 <                        else {
1805 <                            cap = MINIMUM_CAPACITY;
1806 <                            while (cap < c)
1807 <                                cap <<= 1;
1780 >    /** Implementation for putAll */
1781 >    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                      }
655                    else if ((n = tab.length) < MAXIMUM_CAPACITY &&
656                             (sizeHint <= 0 || n < sizeHint))
657                        cap = n << 1;
658                    else
659                        break;
660                    threshold = (int)(cap * loadFactor) - THRESHOLD_OFFSET;
661                    Node[] nextTab = new Node[cap];
662                    if (tab != null)
663                        transfer(tab, nextTab);
664                    table = nextTab;
665                    if (tab == null || cap >= MAXIMUM_CAPACITY ||
666                        (sizeHint > 0 && cap >= sizeHint) ||
667                        counter.sum() < threshold)
668                        break;
1873                  }
670            } finally {
671                resizing = 0;
1874              }
1875 +        } finally {
1876 +            if (delta != 0)
1877 +                counter.add(delta);
1878          }
1879 <        else if (table == null)
1880 <            Thread.yield(); // lost initialization race; just spin
676 <        return table;
1879 >        if (npe)
1880 >            throw new NullPointerException();
1881      }
1882  
1883 +    /* ---------------- Table Initialization and Resizing -------------- */
1884 +
1885      /**
1886 <     * Implementation for putAll and constructor with Map
1887 <     * argument. Tries to first override initial capacity or grow
682 <     * based on map size to pre-allocate table space.
1886 >     * Returns a power of two table size for the given desired capacity.
1887 >     * See Hackers Delight, sec 3.2
1888       */
1889 <    private final void internalPutAll(Map<? extends K, ? extends V> m) {
1890 <        int s = m.size();
1891 <        grow((s >= (MAXIMUM_CAPACITY >>> 1)) ? s : s + (s >>> 1));
1892 <        for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
1893 <            Object k = e.getKey();
1894 <            Object v = e.getValue();
1895 <            if (k == null || v == null)
1896 <                throw new NullPointerException();
1897 <            internalPut(k, v, true);
1889 >    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 >    }
1898 >
1899 >    /**
1900 >     * Initializes table, using the size recorded in sizeCtl.
1901 >     */
1902 >    private final Node[] initTable() {
1903 >        Node[] tab; int sc;
1904 >        while ((tab = table) == null) {
1905 >            if ((sc = sizeCtl) < 0)
1906 >                Thread.yield(); // lost initialization race; just spin
1907 >            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
1908 >                try {
1909 >                    if ((tab = table) == null) {
1910 >                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
1911 >                        tab = table = new Node[n];
1912 >                        sc = n - (n >>> 2);
1913 >                    }
1914 >                } finally {
1915 >                    sizeCtl = sc;
1916 >                }
1917 >                break;
1918 >            }
1919          }
1920 +        return tab;
1921      }
1922  
1923      /**
1924 <     * Implementation for clear. Steps through each bin, removing all nodes.
1924 >     * If table is too small and not already resizing, creates next
1925 >     * table and transfers bins.  Rechecks occupancy after a transfer
1926 >     * to see if another resize is already needed because resizings
1927 >     * are lagging additions.
1928       */
1929 <    private final void internalClear() {
1930 <        long deletions = 0L;
1931 <        int i = 0;
1932 <        Node[] tab = table;
1933 <        while (tab != null && i < tab.length) {
1934 <            Node e = tabAt(tab, i);
1935 <            if (e == null)
1936 <                ++i;
1937 <            else if (e.hash < 0)
1938 <                tab = (Node[])e.key;
1939 <            else {
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)
1950 >     */
1951 >    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 <                synchronized (e) {
2043 <                    if (tabAt(tab, i) == e) {
2042 >                try {              // split to lo and hi lists; copying as needed
2043 >                    if (tabAt(tab, i) == f) {
2044                          validated = true;
2045 <                        do {
2046 <                            if (e.val != null) {
716 <                                e.val = null;
717 <                                ++deletions;
718 <                            }
719 <                        } while ((e = e.next) != null);
720 <                        setTabAt(tab, i, null);
2045 >                        splitBin(nextTab, i, f);
2046 >                        setTabAt(tab, i, fwd);
2047                      }
2048 <                }
2049 <                if (validated) {
2050 <                    ++i;
2051 <                    if (deletions > THRESHOLD_OFFSET) { // bound lag in counts
726 <                        counter.add(-deletions);
727 <                        deletions = 0L;
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          }
732        if (deletions != 0L)
733            counter.add(-deletions);
2088      }
2089  
2090      /**
2091 <     * Base class for key, value, and entry iterators, plus internal
2092 <     * implementations of public traversal-based methods, to avoid
739 <     * duplicating traversal code.
2091 >     * Splits a normal bin with list headed by e into lo and hi parts;
2092 >     * installs in given table.
2093       */
2094 <    class HashIterator {
2095 <        private Node next;          // the next entry to return
2096 <        private Node[] tab;         // current table; updated if resized
2097 <        private Node lastReturned;  // the last entry returned, for remove
2098 <        private Object nextVal;     // cached value of next
2099 <        private int index;          // index of bin to use next
2100 <        private int baseIndex;      // current index of initial table
2101 <        private final int baseSize; // initial table size
2094 >    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 >    }
2120  
2121 <        HashIterator() {
2122 <            Node[] t = tab = table;
2123 <            if (t == null)
2124 <                baseSize = 0;
2121 >    /**
2122 >     * Splits a tree bin into lo and hi parts; installs in given table.
2123 >     */
2124 >    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 <                baseSize = t.length;
2138 <                advance(null);
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 +    }
2159  
2160 <        public final boolean hasNext()         { return next != null; }
2161 <        public final boolean hasMoreElements() { return next != null; }
2162 <
2163 <        /**
2164 <         * Advances next.  Normally, iteration proceeds bin-by-bin
2165 <         * traversing lists.  However, if the table has been resized,
2166 <         * then all future steps must traverse both the bin at the
2167 <         * current index as well as at (index + baseSize); and so on
2168 <         * for further resizings. To paranoically cope with potential
2169 <         * (improper) sharing of iterators across threads, table reads
2170 <         * are bounds-checked.
2171 <         */
2172 <        final void advance(Node e) {
2173 <            for (;;) {
2174 <                Node[] t; int i; // for bounds checks
2175 <                if (e != null) {
2176 <                    Object ek = e.key, ev = e.val;
2177 <                    if (ev != null && ek != null) {
2178 <                        nextVal = ev;
2179 <                        next = e;
2180 <                        break;
2160 >    /**
2161 >     * Implementation for clear. Steps through each bin, removing all
2162 >     * nodes.
2163 >     */
2164 >    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                      }
782                    e = e.next;
2190                  }
2191 <                else if (baseIndex < baseSize && (t = tab) != null &&
2192 <                         t.length > (i = index) && i >= 0) {
2193 <                    if ((e = tabAt(t, i)) != null && e.hash < 0) {
2194 <                        tab = (Node[])e.key;
2195 <                        e = null;
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                      }
790                    else if (i + baseSize < t.length)
791                        index += baseSize;    // visit forwarded upper slots
792                    else
793                        index = ++baseIndex;
794                }
795                else {
796                    next = null;
797                    break;
2214                  }
2215              }
2216          }
2217 +        if (delta != 0)
2218 +            counter.add(delta);
2219 +    }
2220  
2221 <        final Object nextKey() {
803 <            Node e = next;
804 <            if (e == null)
805 <                throw new NoSuchElementException();
806 <            Object k = e.key;
807 <            advance((lastReturned = e).next);
808 <            return k;
809 <        }
2221 >    /* ----------------Table Traversal -------------- */
2222  
2223 <        final Object nextValue() {
2224 <            Node e = next;
2225 <            if (e == null)
2226 <                throw new NoSuchElementException();
2227 <            Object v = nextVal;
2228 <            advance((lastReturned = e).next);
2229 <            return v;
2223 >    /**
2224 >     * Encapsulates traversal for methods such as containsValue; also
2225 >     * serves as a base class for other iterators.
2226 >     *
2227 >     * At each step, the iterator snapshots the key ("nextKey") and
2228 >     * value ("nextVal") of a valid node (i.e., one that, at point of
2229 >     * snapshot, has a non-null user value). Because val fields can
2230 >     * change (including to null, indicating deletion), field nextVal
2231 >     * might not be accurate at point of use, but still maintains the
2232 >     * weak consistency property of holding a value that was once
2233 >     * valid.
2234 >     *
2235 >     * Internal traversals directly access these fields, as in:
2236 >     * {@code while (it.advance() != null) { process(it.nextKey); }}
2237 >     *
2238 >     * Exported iterators must track whether the iterator has advanced
2239 >     * (in hasNext vs next) (by setting/checking/nulling field
2240 >     * nextVal), and then extract key, value, or key-value pairs as
2241 >     * return values of next().
2242 >     *
2243 >     * The iterator visits once each still-valid node that was
2244 >     * reachable upon iterator construction. It might miss some that
2245 >     * were added to a bin after the bin was visited, which is OK wrt
2246 >     * consistency guarantees. Maintaining this property in the face
2247 >     * of possible ongoing resizes requires a fair amount of
2248 >     * bookkeeping state that is difficult to optimize away amidst
2249 >     * volatile accesses.  Even so, traversal maintains reasonable
2250 >     * throughput.
2251 >     *
2252 >     * Normally, iteration proceeds bin-by-bin traversing lists.
2253 >     * However, if the table has been resized, then all future steps
2254 >     * must traverse both the bin at the current index as well as at
2255 >     * (index + baseSize); and so on for further resizings. To
2256 >     * paranoically cope with potential sharing by users of iterators
2257 >     * across threads, iteration terminates if a bounds checks fails
2258 >     * for a table read.
2259 >     *
2260 >     * This class extends ForkJoinTask to streamline parallel
2261 >     * iteration in bulk operations (see BulkTask). This adds only an
2262 >     * int of space overhead, which is close enough to negligible in
2263 >     * cases where it is not needed to not worry about it.
2264 >     */
2265 >    static class Traverser<K,V,R> extends ForkJoinTask<R> {
2266 >        final ConcurrentHashMapV8<K, V> map;
2267 >        Node next;           // the next entry to use
2268 >        Node last;           // the last entry used
2269 >        Object nextKey;      // cached key field of next
2270 >        Object nextVal;      // cached val field of next
2271 >        Node[] tab;          // current table; updated if resized
2272 >        int index;           // index of bin to use next
2273 >        int baseIndex;       // current index of initial table
2274 >        int baseLimit;       // index bound for initial table
2275 >        final int baseSize;  // initial table size
2276 >
2277 >        /** Creates iterator for all entries in the table. */
2278 >        Traverser(ConcurrentHashMapV8<K, V> map) {
2279 >            this.tab = (this.map = map).table;
2280 >            baseLimit = baseSize = (tab == null) ? 0 : tab.length;
2281 >        }
2282 >
2283 >        /** Creates iterator for split() methods */
2284 >        Traverser(Traverser<K,V,?> it, boolean split) {
2285 >            this.map = it.map;
2286 >            this.tab = it.tab;
2287 >            this.baseSize = it.baseSize;
2288 >            int lo = it.baseIndex;
2289 >            int hi = this.baseLimit = it.baseLimit;
2290 >            int i;
2291 >            if (split) // adjust parent
2292 >                i = it.baseLimit = (lo + hi + 1) >>> 1;
2293 >            else       // clone parent
2294 >                i = lo;
2295 >            this.index = this.baseIndex = i;
2296          }
2297  
2298 <        final WriteThroughEntry nextEntry() {
2299 <            Node e = next;
2300 <            if (e == null)
2301 <                throw new NoSuchElementException();
2302 <            WriteThroughEntry entry =
2303 <                new WriteThroughEntry(e.key, nextVal);
2304 <            advance((lastReturned = e).next);
2305 <            return entry;
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 (lastReturned == null)
2330 >            if (nextVal == null)
2331 >                advance();
2332 >            Node e = last;
2333 >            if (e == null)
2334                  throw new IllegalStateException();
2335 <            ConcurrentHashMapV8.this.remove(lastReturned.key);
2336 <            lastReturned = null;
2335 >            last = null;
2336 >            map.remove(e.key);
2337          }
2338  
2339 <        /** Helper for serialization */
2340 <        final void writeEntries(java.io.ObjectOutputStream s)
839 <            throws java.io.IOException {
840 <            Node e;
841 <            while ((e = next) != null) {
842 <                s.writeObject(e.key);
843 <                s.writeObject(nextVal);
844 <                advance(e.next);
845 <            }
2339 >        public final boolean hasNext() {
2340 >            return nextVal != null || advance() != null;
2341          }
2342  
2343 <        /** Helper for containsValue */
2344 <        final boolean containsVal(Object value) {
2345 <            if (value != null) {
2346 <                Node e;
852 <                while ((e = next) != null) {
853 <                    Object v = nextVal;
854 <                    if (value == v || value.equals(v))
855 <                        return true;
856 <                    advance(e.next);
857 <                }
858 <            }
859 <            return false;
860 <        }
861 <
862 <        /** Helper for Map.hashCode */
863 <        final int mapHashCode() {
864 <            int h = 0;
865 <            Node e;
866 <            while ((e = next) != null) {
867 <                h += e.key.hashCode() ^ nextVal.hashCode();
868 <                advance(e.next);
869 <            }
870 <            return h;
871 <        }
872 <
873 <        /** Helper for Map.toString */
874 <        final String mapToString() {
875 <            Node e = next;
876 <            if (e == null)
877 <                return "{}";
878 <            StringBuilder sb = new StringBuilder();
879 <            sb.append('{');
880 <            for (;;) {
881 <                sb.append(e.key   == this ? "(this Map)" : e.key);
882 <                sb.append('=');
883 <                sb.append(nextVal == this ? "(this Map)" : nextVal);
884 <                advance(e.next);
885 <                if ((e = next) != null)
886 <                    sb.append(',').append(' ');
887 <                else
888 <                    return sb.append('}').toString();
889 <            }
890 <        }
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      }
2348  
2349      /* ---------------- Public operations -------------- */
2350  
2351      /**
2352 <     * Creates a new, empty map with the specified initial
897 <     * capacity, load factor and concurrency level.
898 <     *
899 <     * @param initialCapacity the initial capacity. The implementation
900 <     * performs internal sizing to accommodate this many elements.
901 <     * @param loadFactor  the load factor threshold, used to control resizing.
902 <     * Resizing may be performed when the average number of elements per
903 <     * bin exceeds this threshold.
904 <     * @param concurrencyLevel the estimated number of concurrently
905 <     * updating threads. The implementation may use this value as
906 <     * a sizing hint.
907 <     * @throws IllegalArgumentException if the initial capacity is
908 <     * negative or the load factor or concurrencyLevel are
909 <     * nonpositive.
2352 >     * Creates a new, empty map with the default initial table size (16).
2353       */
2354 <    public ConcurrentHashMapV8(int initialCapacity,
912 <                             float loadFactor, int concurrencyLevel) {
913 <        if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0)
914 <            throw new IllegalArgumentException();
915 <        this.initCap = initialCapacity;
916 <        this.loadFactor = loadFactor;
2354 >    public ConcurrentHashMapV8() {
2355          this.counter = new LongAdder();
2356      }
2357  
2358      /**
2359 <     * Creates a new, empty map with the specified initial capacity
2360 <     * and load factor and with the default concurrencyLevel (16).
2359 >     * Creates a new, empty map with an initial table size
2360 >     * accommodating the specified number of elements without the need
2361 >     * to dynamically resize.
2362       *
2363       * @param initialCapacity The implementation performs internal
2364       * sizing to accommodate this many elements.
926     * @param loadFactor  the load factor threshold, used to control resizing.
927     * Resizing may be performed when the average number of elements per
928     * bin exceeds this threshold.
2365       * @throws IllegalArgumentException if the initial capacity of
2366 <     * elements is negative or the load factor is nonpositive
931 <     *
932 <     * @since 1.6
2366 >     * elements is negative
2367       */
2368 <    public ConcurrentHashMapV8(int initialCapacity, float loadFactor) {
2369 <        this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL);
2368 >    public ConcurrentHashMapV8(int initialCapacity) {
2369 >        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;
2376      }
2377  
2378      /**
2379 <     * Creates a new, empty map with the specified initial capacity,
940 <     * and with default load factor (0.75) and concurrencyLevel (16).
2379 >     * Creates a new map with the same mappings as the given map.
2380       *
2381 <     * @param initialCapacity the initial capacity. The implementation
943 <     * performs internal sizing to accommodate this many elements.
944 <     * @throws IllegalArgumentException if the initial capacity of
945 <     * elements is negative.
2381 >     * @param m the map
2382       */
2383 <    public ConcurrentHashMapV8(int initialCapacity) {
2384 <        this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
2383 >    public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
2384 >        this.counter = new LongAdder();
2385 >        this.sizeCtl = DEFAULT_CAPACITY;
2386 >        internalPutAll(m);
2387      }
2388  
2389      /**
2390 <     * Creates a new, empty map with a default initial capacity (16),
2391 <     * load factor (0.75) and concurrencyLevel (16).
2390 >     * Creates a new, empty map with an initial table size based on
2391 >     * the given number of elements ({@code initialCapacity}) and
2392 >     * initial table density ({@code loadFactor}).
2393 >     *
2394 >     * @param initialCapacity the initial capacity. The implementation
2395 >     * performs internal sizing to accommodate this many elements,
2396 >     * given the specified load factor.
2397 >     * @param loadFactor the load factor (table density) for
2398 >     * establishing the initial table size
2399 >     * @throws IllegalArgumentException if the initial capacity of
2400 >     * elements is negative or the load factor is nonpositive
2401 >     *
2402 >     * @since 1.6
2403       */
2404 <    public ConcurrentHashMapV8() {
2405 <        this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
2404 >    public ConcurrentHashMapV8(int initialCapacity, float loadFactor) {
2405 >        this(initialCapacity, loadFactor, 1);
2406      }
2407  
2408      /**
2409 <     * Creates a new map with the same mappings as the given map.
2410 <     * The map is created with a capacity of 1.5 times the number
2411 <     * of mappings in the given map or 16 (whichever is greater),
2412 <     * and a default load factor (0.75) and concurrencyLevel (16).
2409 >     * Creates a new, empty map with an initial table size based on
2410 >     * the given number of elements ({@code initialCapacity}), table
2411 >     * density ({@code loadFactor}), and number of concurrently
2412 >     * updating threads ({@code concurrencyLevel}).
2413       *
2414 <     * @param m the map
2414 >     * @param initialCapacity the initial capacity. The implementation
2415 >     * performs internal sizing to accommodate this many elements,
2416 >     * given the specified load factor.
2417 >     * @param loadFactor the load factor (table density) for
2418 >     * establishing the initial table size
2419 >     * @param concurrencyLevel the estimated number of concurrently
2420 >     * updating threads. The implementation may use this value as
2421 >     * a sizing hint.
2422 >     * @throws IllegalArgumentException if the initial capacity is
2423 >     * negative or the load factor or concurrencyLevel are
2424 >     * nonpositive
2425       */
2426 <    public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
2427 <        this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
2428 <        if (m == null)
2429 <            throw new NullPointerException();
2430 <        internalPutAll(m);
2426 >    public ConcurrentHashMapV8(int initialCapacity,
2427 >                               float loadFactor, int concurrencyLevel) {
2428 >        if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
2429 >            throw new IllegalArgumentException();
2430 >        if (initialCapacity < concurrencyLevel)   // Use at least as many bins
2431 >            initialCapacity = concurrencyLevel;   // as estimated threads
2432 >        long size = (long)(1.0 + (long)initialCapacity / loadFactor);
2433 >        int cap = (size >= (long)MAXIMUM_CAPACITY) ?
2434 >            MAXIMUM_CAPACITY : tableSizeFor((int)size);
2435 >        this.counter = new LongAdder();
2436 >        this.sizeCtl = cap;
2437      }
2438  
2439      /**
2440 <     * Returns {@code true} if this map contains no key-value mappings.
976 <     *
977 <     * @return {@code true} if this map contains no key-value mappings
2440 >     * {@inheritDoc}
2441       */
2442      public boolean isEmpty() {
2443          return counter.sum() <= 0L; // ignore transient negative values
2444      }
2445  
2446      /**
2447 <     * Returns the number of key-value mappings in this map.  If the
985 <     * map contains more than {@code Integer.MAX_VALUE} elements, returns
986 <     * {@code Integer.MAX_VALUE}.
987 <     *
988 <     * @return the number of key-value mappings in this map
2447 >     * {@inheritDoc}
2448       */
2449      public int size() {
2450          long n = counter.sum();
2451 <        return ((n >>> 31) == 0) ? (int)n : (n < 0L) ? 0 : Integer.MAX_VALUE;
2451 >        return ((n < 0L) ? 0 :
2452 >                (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
2453 >                (int)n);
2454 >    }
2455 >
2456 >    /**
2457 >     * 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
2464 >     */
2465 >    public long mappingCount() {
2466 >        long n = counter.sum();
2467 >        return (n < 0L) ? 0L : n;
2468      }
2469  
2470      /**
# Line 1004 | Line 2479 | public class ConcurrentHashMapV8<K, V>
2479       * @throws NullPointerException if the specified key is null
2480       */
2481      @SuppressWarnings("unchecked")
2482 <    public V get(Object key) {
2482 >        public V get(Object key) {
2483          if (key == null)
2484              throw new NullPointerException();
2485          return (V)internalGet(key);
# Line 1016 | Line 2491 | public class ConcurrentHashMapV8<K, V>
2491       * @param  key   possible key
2492       * @return {@code true} if and only if the specified object
2493       *         is a key in this table, as determined by the
2494 <     *         {@code equals} method; {@code false} otherwise.
2494 >     *         {@code equals} method; {@code false} otherwise
2495       * @throws NullPointerException if the specified key is null
2496       */
2497      public boolean containsKey(Object key) {
# Line 1027 | Line 2502 | public class ConcurrentHashMapV8<K, V>
2502  
2503      /**
2504       * Returns {@code true} if this map maps one or more keys to the
2505 <     * specified value. Note: This method requires a full internal
2506 <     * traversal of the hash table, and so is much slower than
1032 <     * method {@code containsKey}.
2505 >     * specified value. Note: This method may require a full traversal
2506 >     * of the map, and is much slower than method {@code containsKey}.
2507       *
2508       * @param value value whose presence in this map is to be tested
2509       * @return {@code true} if this map maps one or more keys to the
# Line 1039 | Line 2513 | public class ConcurrentHashMapV8<K, V>
2513      public boolean containsValue(Object value) {
2514          if (value == null)
2515              throw new NullPointerException();
2516 <        return new HashIterator().containsVal(value);
2516 >        Object v;
2517 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2518 >        while ((v = it.advance()) != null) {
2519 >            if (v == value || value.equals(v))
2520 >                return true;
2521 >        }
2522 >        return false;
2523      }
2524  
2525      /**
# Line 1075 | Line 2555 | public class ConcurrentHashMapV8<K, V>
2555       * @throws NullPointerException if the specified key or value is null
2556       */
2557      @SuppressWarnings("unchecked")
2558 <    public V put(K key, V value) {
2558 >        public V put(K key, V value) {
2559          if (key == null || value == null)
2560              throw new NullPointerException();
2561 <        return (V)internalPut(key, value, true);
2561 >        return (V)internalPut(key, value);
2562      }
2563  
2564      /**
# Line 1089 | Line 2569 | public class ConcurrentHashMapV8<K, V>
2569       * @throws NullPointerException if the specified key or value is null
2570       */
2571      @SuppressWarnings("unchecked")
2572 <    public V putIfAbsent(K key, V value) {
2572 >        public V putIfAbsent(K key, V value) {
2573          if (key == null || value == null)
2574              throw new NullPointerException();
2575 <        return (V)internalPut(key, value, false);
2575 >        return (V)internalPutIfAbsent(key, value);
2576      }
2577  
2578      /**
# Line 1103 | Line 2583 | public class ConcurrentHashMapV8<K, V>
2583       * @param m mappings to be stored in this map
2584       */
2585      public void putAll(Map<? extends K, ? extends V> m) {
1106        if (m == null)
1107            throw new NullPointerException();
2586          internalPutAll(m);
2587      }
2588  
2589      /**
2590       * If the specified key is not already associated with a value,
2591 <     * computes its value using the given mappingFunction, and if
2592 <     * non-null, enters it into the map.  This is equivalent to
2593 <     *
2594 <     * <pre>
2595 <     *   if (map.containsKey(key))
2596 <     *       return map.get(key);
2597 <     *   value = mappingFunction.map(key);
2598 <     *   if (value != null)
2599 <     *      map.put(key, value);
2600 <     *   return value;
2601 <     * </pre>
2602 <     *
2603 <     * except that the action is performed atomically.  Some attempted
2604 <     * update operations on this map by other threads may be blocked
2605 <     * while computation is in progress, so the computation should be
2606 <     * short and simple, and must not attempt to update any other
2607 <     * mappings of this Map. The most appropriate usage is to
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 <     * <pre>{@code
2612 <     * map.computeIfAbsent(key, new MappingFunction<K, V>() {
2613 <     *   public V map(K k) { return new Value(f(k)); }};
2614 <     * }</pre>
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 {@code null} if the computation
1141 <     *         returned {@code null}.
2619 >     *         the specified key, or null if the computed value is null.
2620       * @throws NullPointerException if the specified key or mappingFunction
2621 <     *         is null,
2621 >     *         is null
2622       * @throws IllegalStateException if the computation detectably
2623       *         attempts a recursive update to this map that would
2624 <     *         otherwise never complete.
2624 >     *         otherwise never complete
2625       * @throws RuntimeException or Error if the mappingFunction does so,
2626 <     *         in which case the mapping is left unestablished.
2626 >     *         in which case the mapping is left unestablished
2627       */
2628 <    public V computeIfAbsent(K key, MappingFunction<? super K, ? extends V> mappingFunction) {
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 internalCompute(key, mappingFunction, false);
2632 >        return (V)internalComputeIfAbsent(key, mappingFunction);
2633      }
2634  
2635      /**
2636 <     * Computes the value associated with the given key using the given
2637 <     * mappingFunction, and if non-null, enters it into the map.  This
2638 <     * is equivalent to
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 <     * <pre>
2649 <     *   value = mappingFunction.map(key);
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);
2683 >     *     map.put(key, value);
2684       *   else
2685 <     *      value = map.get(key);
2686 <     *   return value;
2687 <     * </pre>
2688 <     *
2689 <     * except that the action is performed atomically.  Some attempted
2690 <     * update operations on this map by other threads may be blocked
2691 <     * while computation is in progress, so the computation should be
2692 <     * short and simple, and must not attempt to update any other
2693 <     * mappings of this Map.
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 mappingFunction the function to compute a value
2707 <     * @return the current value associated with
2708 <     *         the specified key, or {@code null} if the computation
2709 <     *         returned {@code null} and the value was not otherwise present.
2710 <     * @throws NullPointerException if the specified key or mappingFunction
1182 <     *         is null,
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 mappingFunction does so,
2715 <     *         in which case the mapping is unchanged.
2716 <     */
2717 <    public V compute(K key, MappingFunction<? super K, ? extends V> mappingFunction) {
2718 <        if (key == null || mappingFunction == null)
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 internalCompute(key, mappingFunction, true);
2753 >        return (V)internalMerge(key, value, remappingFunction);
2754      }
2755  
2756      /**
# Line 1202 | Line 2763 | public class ConcurrentHashMapV8<K, V>
2763       * @throws NullPointerException if the specified key is null
2764       */
2765      @SuppressWarnings("unchecked")
2766 <    public V remove(Object key) {
2766 >        public V remove(Object key) {
2767          if (key == null)
2768              throw new NullPointerException();
2769          return (V)internalReplace(key, null, null);
# Line 1240 | Line 2801 | public class ConcurrentHashMapV8<K, V>
2801       * @throws NullPointerException if the specified key or value is null
2802       */
2803      @SuppressWarnings("unchecked")
2804 <    public V replace(K key, V value) {
2804 >        public V replace(K key, V value) {
2805          if (key == null || value == null)
2806              throw new NullPointerException();
2807          return (V)internalReplace(key, value, null);
# Line 1270 | Line 2831 | public class ConcurrentHashMapV8<K, V>
2831       * reflect any modifications subsequent to construction.
2832       */
2833      public Set<K> keySet() {
2834 <        Set<K> ks = keySet;
2835 <        return (ks != null) ? ks : (keySet = new KeySet());
2834 >        KeySet<K,V> ks = keySet;
2835 >        return (ks != null) ? ks : (keySet = new KeySet<K,V>(this));
2836      }
2837  
2838      /**
# Line 1291 | Line 2852 | public class ConcurrentHashMapV8<K, V>
2852       * reflect any modifications subsequent to construction.
2853       */
2854      public Collection<V> values() {
2855 <        Collection<V> vs = values;
2856 <        return (vs != null) ? vs : (values = new Values());
2855 >        Values<K,V> vs = values;
2856 >        return (vs != null) ? vs : (values = new Values<K,V>(this));
2857      }
2858  
2859      /**
# Line 1312 | Line 2873 | public class ConcurrentHashMapV8<K, V>
2873       * reflect any modifications subsequent to construction.
2874       */
2875      public Set<Map.Entry<K,V>> entrySet() {
2876 <        Set<Map.Entry<K,V>> es = entrySet;
2877 <        return (es != null) ? es : (entrySet = new EntrySet());
2876 >        EntrySet<K,V> es = entrySet;
2877 >        return (es != null) ? es : (entrySet = new EntrySet<K,V>(this));
2878      }
2879  
2880      /**
# Line 1323 | Line 2884 | public class ConcurrentHashMapV8<K, V>
2884       * @see #keySet()
2885       */
2886      public Enumeration<K> keys() {
2887 <        return new KeyIterator();
2887 >        return new KeyIterator<K,V>(this);
2888      }
2889  
2890      /**
# Line 1333 | Line 2894 | public class ConcurrentHashMapV8<K, V>
2894       * @see #values()
2895       */
2896      public Enumeration<V> elements() {
2897 <        return new ValueIterator();
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.
2911 >     *
2912 >     * @return a partionable iterator of the values in this map
2913 >     */
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.
2920 >     *
2921 >     * @return a partionable iterator of the entries in this map
2922 >     */
2923 >    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
2924 >        return new EntryIterator<K,V>(this);
2925      }
2926  
2927      /**
# Line 1344 | Line 2932 | public class ConcurrentHashMapV8<K, V>
2932       * @return the hash code value for this map
2933       */
2934      public int hashCode() {
2935 <        return new HashIterator().mapHashCode();
2935 >        int h = 0;
2936 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2937 >        Object v;
2938 >        while ((v = it.advance()) != null) {
2939 >            h += it.nextKey.hashCode() ^ v.hashCode();
2940 >        }
2941 >        return h;
2942      }
2943  
2944      /**
# Line 1359 | Line 2953 | public class ConcurrentHashMapV8<K, V>
2953       * @return a string representation of this map
2954       */
2955      public String toString() {
2956 <        return new HashIterator().mapToString();
2956 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2957 >        StringBuilder sb = new StringBuilder();
2958 >        sb.append('{');
2959 >        Object v;
2960 >        if ((v = it.advance()) != null) {
2961 >            for (;;) {
2962 >                Object k = it.nextKey;
2963 >                sb.append(k == this ? "(this Map)" : k);
2964 >                sb.append('=');
2965 >                sb.append(v == this ? "(this Map)" : v);
2966 >                if ((v = it.advance()) == null)
2967 >                    break;
2968 >                sb.append(',').append(' ');
2969 >            }
2970 >        }
2971 >        return sb.append('}').toString();
2972      }
2973  
2974      /**
# Line 1373 | Line 2982 | public class ConcurrentHashMapV8<K, V>
2982       * @return {@code true} if the specified object is equal to this map
2983       */
2984      public boolean equals(Object o) {
2985 <        if (o == this)
2986 <            return true;
2987 <        if (!(o instanceof Map))
2988 <            return false;
2989 <        Map<?,?> m = (Map<?,?>) o;
2990 <        try {
2991 <            for (Map.Entry<K,V> e : this.entrySet())
2992 <                if (! e.getValue().equals(m.get(e.getKey())))
2985 >        if (o != this) {
2986 >            if (!(o instanceof Map))
2987 >                return false;
2988 >            Map<?,?> m = (Map<?,?>) o;
2989 >            Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2990 >            Object val;
2991 >            while ((val = it.advance()) != null) {
2992 >                Object v = m.get(it.nextKey);
2993 >                if (v == null || (v != val && !v.equals(val)))
2994                      return false;
2995 +            }
2996              for (Map.Entry<?,?> e : m.entrySet()) {
2997 <                Object k = e.getKey();
2998 <                Object v = e.getValue();
2999 <                if (k == null || v == null || !v.equals(get(k)))
2997 >                Object mk, mv, v;
2998 >                if ((mk = e.getKey()) == null ||
2999 >                    (mv = e.getValue()) == null ||
3000 >                    (v = internalGet(mk)) == null ||
3001 >                    (mv != v && !mv.equals(v)))
3002                      return false;
3003              }
3004 <            return true;
3005 <        } catch (ClassCastException unused) {
3006 <            return false;
3007 <        } catch (NullPointerException unused) {
3008 <            return false;
3004 >        }
3005 >        return true;
3006 >    }
3007 >
3008 >    /* ----------------Iterators -------------- */
3009 >
3010 >    static final class KeyIterator<K,V> extends Traverser<K,V,Object>
3011 >        implements Spliterator<K>, Enumeration<K> {
3012 >        KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3013 >        KeyIterator(Traverser<K,V,Object> it, boolean split) {
3014 >            super(it, split);
3015 >        }
3016 >        public KeyIterator<K,V> split() {
3017 >            if (last != null || (next != null && nextVal == null))
3018 >                throw new IllegalStateException();
3019 >            return new KeyIterator<K,V>(this, true);
3020 >        }
3021 >        @SuppressWarnings("unchecked")
3022 >            public final K next() {
3023 >            if (nextVal == null && advance() == null)
3024 >                throw new NoSuchElementException();
3025 >            Object k = nextKey;
3026 >            nextVal = null;
3027 >            return (K) k;
3028 >        }
3029 >
3030 >        public final K nextElement() { return next(); }
3031 >    }
3032 >
3033 >    static final class ValueIterator<K,V> extends Traverser<K,V,Object>
3034 >        implements Spliterator<V>, Enumeration<V> {
3035 >        ValueIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3036 >        ValueIterator(Traverser<K,V,Object> it, boolean split) {
3037 >            super(it, split);
3038 >        }
3039 >        public ValueIterator<K,V> split() {
3040 >            if (last != null || (next != null && nextVal == null))
3041 >                throw new IllegalStateException();
3042 >            return new ValueIterator<K,V>(this, true);
3043 >        }
3044 >
3045 >        @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 >        }
3053 >
3054 >        public final V nextElement() { return next(); }
3055 >    }
3056 >
3057 >    static final class EntryIterator<K,V> extends Traverser<K,V,Object>
3058 >        implements Spliterator<Map.Entry<K,V>> {
3059 >        EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3060 >        EntryIterator(Traverser<K,V,Object> it, boolean split) {
3061 >            super(it, split);
3062 >        }
3063 >        public EntryIterator<K,V> split() {
3064 >            if (last != null || (next != null && nextVal == null))
3065 >                throw new IllegalStateException();
3066 >            return new EntryIterator<K,V>(this, true);
3067 >        }
3068 >
3069 >        @SuppressWarnings("unchecked")
3070 >            public final Map.Entry<K,V> next() {
3071 >            Object v;
3072 >            if ((v = nextVal) == null && (v = advance()) == null)
3073 >                throw new NoSuchElementException();
3074 >            Object k = nextKey;
3075 >            nextVal = null;
3076 >            return new MapEntry<K,V>((K)k, (V)v, map);
3077          }
3078      }
3079  
3080      /**
3081 <     * Custom Entry class used by EntryIterator.next(), that relays
1401 <     * setValue changes to the underlying map.
3081 >     * Exported Entry for iterators
3082       */
3083 <    final class WriteThroughEntry extends AbstractMap.SimpleEntry<K,V> {
3084 <        @SuppressWarnings("unchecked")
3085 <        WriteThroughEntry(Object k, Object v) {
3086 <            super((K)k, (V)v);
3083 >    static final class MapEntry<K,V> implements Map.Entry<K, V> {
3084 >        final K key; // non-null
3085 >        V val;       // non-null
3086 >        final ConcurrentHashMapV8<K, V> map;
3087 >        MapEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
3088 >            this.key = key;
3089 >            this.val = val;
3090 >            this.map = map;
3091 >        }
3092 >        public final K getKey()       { return key; }
3093 >        public final V getValue()     { return val; }
3094 >        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
3095 >        public final String toString(){ return key + "=" + val; }
3096 >
3097 >        public final boolean equals(Object o) {
3098 >            Object k, v; Map.Entry<?,?> e;
3099 >            return ((o instanceof Map.Entry) &&
3100 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3101 >                    (v = e.getValue()) != null &&
3102 >                    (k == key || k.equals(key)) &&
3103 >                    (v == val || v.equals(val)));
3104          }
3105  
3106          /**
3107           * Sets our entry's value and writes through to the map. The
3108 <         * value to return is somewhat arbitrary here. Since a
3109 <         * WriteThroughEntry does not necessarily track asynchronous
3110 <         * changes, the most recent "previous" value could be
3111 <         * different from what we return (or could even have been
3112 <         * removed in which case the put will re-establish). We do not
1416 <         * and cannot guarantee more.
3108 >         * value to return is somewhat arbitrary here. Since we do not
3109 >         * necessarily track asynchronous changes, the most recent
3110 >         * "previous" value could be different from what we return (or
3111 >         * could even have been removed in which case the put will
3112 >         * re-establish). We do not and cannot guarantee more.
3113           */
3114 <        public V setValue(V value) {
3114 >        public final V setValue(V value) {
3115              if (value == null) throw new NullPointerException();
3116 <            V v = super.setValue(value);
3117 <            ConcurrentHashMapV8.this.put(getKey(), value);
3116 >            V v = val;
3117 >            val = value;
3118 >            map.put(key, value);
3119              return v;
3120          }
3121      }
3122  
3123 <    final class KeyIterator extends HashIterator
1427 <        implements Iterator<K>, Enumeration<K> {
1428 <        @SuppressWarnings("unchecked")
1429 <        public final K next()        { return (K)super.nextKey(); }
1430 <        @SuppressWarnings("unchecked")
1431 <        public final K nextElement() { return (K)super.nextKey(); }
1432 <    }
3123 >    /* ----------------Views -------------- */
3124  
3125 <    final class ValueIterator extends HashIterator
3126 <        implements Iterator<V>, Enumeration<V> {
3127 <        @SuppressWarnings("unchecked")
3128 <        public final V next()        { return (V)super.nextValue(); }
3129 <        @SuppressWarnings("unchecked")
3130 <        public final V nextElement() { return (V)super.nextValue(); }
3131 <    }
3132 <
3133 <    final class EntryIterator extends HashIterator
3134 <        implements Iterator<Entry<K,V>> {
3135 <        public final Map.Entry<K,V> next() { return super.nextEntry(); }
3136 <    }
3125 >    /**
3126 >     * Base class for views.
3127 >     */
3128 >    static abstract class CHMView<K, V> {
3129 >        final ConcurrentHashMapV8<K, V> map;
3130 >        CHMView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
3131 >        public final int size()                 { return map.size(); }
3132 >        public final boolean isEmpty()          { return map.isEmpty(); }
3133 >        public final void clear()               { map.clear(); }
3134 >
3135 >        // implementations below rely on concrete classes supplying these
3136 >        abstract public Iterator<?> iterator();
3137 >        abstract public boolean contains(Object o);
3138 >        abstract public boolean remove(Object o);
3139 >
3140 >        private static final String oomeMsg = "Required array size too large";
3141 >
3142 >        public final Object[] toArray() {
3143 >            long sz = map.mappingCount();
3144 >            if (sz > (long)(MAX_ARRAY_SIZE))
3145 >                throw new OutOfMemoryError(oomeMsg);
3146 >            int n = (int)sz;
3147 >            Object[] r = new Object[n];
3148 >            int i = 0;
3149 >            Iterator<?> it = iterator();
3150 >            while (it.hasNext()) {
3151 >                if (i == n) {
3152 >                    if (n >= MAX_ARRAY_SIZE)
3153 >                        throw new OutOfMemoryError(oomeMsg);
3154 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3155 >                        n = MAX_ARRAY_SIZE;
3156 >                    else
3157 >                        n += (n >>> 1) + 1;
3158 >                    r = Arrays.copyOf(r, n);
3159 >                }
3160 >                r[i++] = it.next();
3161 >            }
3162 >            return (i == n) ? r : Arrays.copyOf(r, i);
3163 >        }
3164  
3165 <    final class KeySet extends AbstractSet<K> {
3166 <        public int size() {
3167 <            return ConcurrentHashMapV8.this.size();
3165 >        @SuppressWarnings("unchecked")
3166 >            public final <T> T[] toArray(T[] a) {
3167 >            long sz = map.mappingCount();
3168 >            if (sz > (long)(MAX_ARRAY_SIZE))
3169 >                throw new OutOfMemoryError(oomeMsg);
3170 >            int m = (int)sz;
3171 >            T[] r = (a.length >= m) ? a :
3172 >                (T[])java.lang.reflect.Array
3173 >                .newInstance(a.getClass().getComponentType(), m);
3174 >            int n = r.length;
3175 >            int i = 0;
3176 >            Iterator<?> it = iterator();
3177 >            while (it.hasNext()) {
3178 >                if (i == n) {
3179 >                    if (n >= MAX_ARRAY_SIZE)
3180 >                        throw new OutOfMemoryError(oomeMsg);
3181 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3182 >                        n = MAX_ARRAY_SIZE;
3183 >                    else
3184 >                        n += (n >>> 1) + 1;
3185 >                    r = Arrays.copyOf(r, n);
3186 >                }
3187 >                r[i++] = (T)it.next();
3188 >            }
3189 >            if (a == r && i < n) {
3190 >                r[i] = null; // null-terminate
3191 >                return r;
3192 >            }
3193 >            return (i == n) ? r : Arrays.copyOf(r, i);
3194          }
3195 <        public boolean isEmpty() {
3196 <            return ConcurrentHashMapV8.this.isEmpty();
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;
3201          }
3202 <        public void clear() {
3203 <            ConcurrentHashMapV8.this.clear();
3202 >
3203 >        public final String toString() {
3204 >            StringBuilder sb = new StringBuilder();
3205 >            sb.append('[');
3206 >            Iterator<?> it = iterator();
3207 >            if (it.hasNext()) {
3208 >                for (;;) {
3209 >                    Object e = it.next();
3210 >                    sb.append(e == this ? "(this Collection)" : e);
3211 >                    if (!it.hasNext())
3212 >                        break;
3213 >                    sb.append(',').append(' ');
3214 >                }
3215 >            }
3216 >            return sb.append(']').toString();
3217          }
3218 <        public Iterator<K> iterator() {
3219 <            return new KeyIterator();
3218 >
3219 >        public final boolean containsAll(Collection<?> c) {
3220 >            if (c != this) {
3221 >                for (Iterator<?> it = c.iterator(); it.hasNext();) {
3222 >                    Object e = it.next();
3223 >                    if (e == null || !contains(e))
3224 >                        return false;
3225 >                }
3226 >            }
3227 >            return true;
3228          }
3229 <        public boolean contains(Object o) {
3230 <            return ConcurrentHashMapV8.this.containsKey(o);
3229 >
3230 >        public final boolean removeAll(Collection<?> c) {
3231 >            boolean modified = false;
3232 >            for (Iterator<?> it = iterator(); it.hasNext();) {
3233 >                if (c.contains(it.next())) {
3234 >                    it.remove();
3235 >                    modified = true;
3236 >                }
3237 >            }
3238 >            return modified;
3239          }
3240 <        public boolean remove(Object o) {
3241 <            return ConcurrentHashMapV8.this.remove(o) != null;
3240 >
3241 >        public final boolean retainAll(Collection<?> c) {
3242 >            boolean modified = false;
3243 >            for (Iterator<?> it = iterator(); it.hasNext();) {
3244 >                if (!c.contains(it.next())) {
3245 >                    it.remove();
3246 >                    modified = true;
3247 >                }
3248 >            }
3249 >            return modified;
3250          }
3251 +
3252      }
3253  
3254 <    final class Values extends AbstractCollection<V> {
3255 <        public int size() {
3256 <            return ConcurrentHashMapV8.this.size();
3254 >    static final class KeySet<K,V> extends CHMView<K,V> implements Set<K> {
3255 >        KeySet(ConcurrentHashMapV8<K, V> map)  {
3256 >            super(map);
3257          }
3258 <        public boolean isEmpty() {
3259 <            return ConcurrentHashMapV8.this.isEmpty();
3258 >        public final boolean contains(Object o) { return map.containsKey(o); }
3259 >        public final boolean remove(Object o)   { return map.remove(o) != null; }
3260 >        public final Iterator<K> iterator() {
3261 >            return new KeyIterator<K,V>(map);
3262          }
3263 <        public void clear() {
3264 <            ConcurrentHashMapV8.this.clear();
3263 >        public final boolean add(K e) {
3264 >            throw new UnsupportedOperationException();
3265          }
3266 <        public Iterator<V> iterator() {
3267 <            return new ValueIterator();
3266 >        public final boolean addAll(Collection<? extends K> c) {
3267 >            throw new UnsupportedOperationException();
3268          }
3269 <        public boolean contains(Object o) {
3270 <            return ConcurrentHashMapV8.this.containsValue(o);
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          }
3275      }
3276  
3277 <    final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
3278 <        public int size() {
3279 <            return ConcurrentHashMapV8.this.size();
3280 <        }
3281 <        public boolean isEmpty() {
3282 <            return ConcurrentHashMapV8.this.isEmpty();
3277 >
3278 >    static final class Values<K,V> extends CHMView<K,V>
3279 >        implements Collection<V> {
3280 >        Values(ConcurrentHashMapV8<K, V> map)   { super(map); }
3281 >        public final boolean contains(Object o) { return map.containsValue(o); }
3282 >        public final boolean remove(Object o) {
3283 >            if (o != null) {
3284 >                Iterator<V> it = new ValueIterator<K,V>(map);
3285 >                while (it.hasNext()) {
3286 >                    if (o.equals(it.next())) {
3287 >                        it.remove();
3288 >                        return true;
3289 >                    }
3290 >                }
3291 >            }
3292 >            return false;
3293          }
3294 <        public void clear() {
3295 <            ConcurrentHashMapV8.this.clear();
3294 >        public final Iterator<V> iterator() {
3295 >            return new ValueIterator<K,V>(map);
3296          }
3297 <        public Iterator<Map.Entry<K,V>> iterator() {
3298 <            return new EntryIterator();
3297 >        public final boolean add(V e) {
3298 >            throw new UnsupportedOperationException();
3299          }
3300 <        public boolean contains(Object o) {
3301 <            if (!(o instanceof Map.Entry))
1501 <                return false;
1502 <            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
1503 <            V v = ConcurrentHashMapV8.this.get(e.getKey());
1504 <            return v != null && v.equals(e.getValue());
3300 >        public final boolean addAll(Collection<? extends V> c) {
3301 >            throw new UnsupportedOperationException();
3302          }
3303 <        public boolean remove(Object o) {
3304 <            if (!(o instanceof Map.Entry))
3305 <                return false;
3306 <            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
3307 <            return ConcurrentHashMapV8.this.remove(e.getKey(), e.getValue());
3303 >
3304 >    }
3305 >
3306 >    static final class EntrySet<K,V> extends CHMView<K,V>
3307 >        implements Set<Map.Entry<K,V>> {
3308 >        EntrySet(ConcurrentHashMapV8<K, V> map) { super(map); }
3309 >        public final boolean contains(Object o) {
3310 >            Object k, v, r; Map.Entry<?,?> e;
3311 >            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();
3332 >        }
3333 >        public boolean equals(Object o) {
3334 >            Set<?> c;
3335 >            return ((o instanceof Set) &&
3336 >                    ((c = (Set<?>)o) == this ||
3337 >                     (containsAll(c) && c.containsAll(this))));
3338          }
3339      }
3340  
3341      /* ---------------- Serialization Support -------------- */
3342  
3343      /**
3344 <     * Helper class used in previous version, declared for the sake of
3345 <     * serialization compatibility
3344 >     * Stripped-down version of helper class used in previous version,
3345 >     * declared for the sake of serialization compatibility
3346       */
3347 <    static class Segment<K,V> extends java.util.concurrent.locks.ReentrantLock
1521 <        implements Serializable {
3347 >    static class Segment<K,V> implements Serializable {
3348          private static final long serialVersionUID = 2249069246763182397L;
3349          final float loadFactor;
3350          Segment(float lf) { this.loadFactor = lf; }
# Line 1534 | Line 3360 | public class ConcurrentHashMapV8<K, V>
3360       * The key-value mappings are emitted in no particular order.
3361       */
3362      @SuppressWarnings("unchecked")
3363 <    private void writeObject(java.io.ObjectOutputStream s)
3364 <            throws java.io.IOException {
3363 >        private void writeObject(java.io.ObjectOutputStream s)
3364 >        throws java.io.IOException {
3365          if (segments == null) { // for serialization compatibility
3366              segments = (Segment<K,V>[])
3367                  new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
3368              for (int i = 0; i < segments.length; ++i)
3369 <                segments[i] = new Segment<K,V>(loadFactor);
3369 >                segments[i] = new Segment<K,V>(LOAD_FACTOR);
3370          }
3371          s.defaultWriteObject();
3372 <        new HashIterator().writeEntries(s);
3372 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3373 >        Object v;
3374 >        while ((v = it.advance()) != null) {
3375 >            s.writeObject(it.nextKey);
3376 >            s.writeObject(v);
3377 >        }
3378          s.writeObject(null);
3379          s.writeObject(null);
3380          segments = null; // throw away
3381      }
3382  
3383      /**
3384 <     * Reconstitutes the  instance from a
1554 <     * stream (i.e., deserializes it).
3384 >     * Reconstitutes the instance from a stream (that is, deserializes it).
3385       * @param s the stream
3386       */
3387      @SuppressWarnings("unchecked")
3388 <    private void readObject(java.io.ObjectInputStream s)
3389 <            throws java.io.IOException, ClassNotFoundException {
3388 >        private void readObject(java.io.ObjectInputStream s)
3389 >        throws java.io.IOException, ClassNotFoundException {
3390          s.defaultReadObject();
1561        // find load factor in a segment, if one exists
1562        if (segments != null && segments.length != 0)
1563            this.loadFactor = segments[0].loadFactor;
1564        else
1565            this.loadFactor = DEFAULT_LOAD_FACTOR;
1566        this.initCap = DEFAULT_CAPACITY;
1567        LongAdder ct = new LongAdder(); // force final field write
1568        UNSAFE.putObjectVolatile(this, counterOffset, ct);
3391          this.segments = null; // unneeded
3392 +        // initialize transient final field
3393 +        UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder());
3394  
3395 <        // Read the keys and values, and put the mappings in the table
3395 >        // Create all nodes, then place in table once size is known
3396 >        long size = 0L;
3397 >        Node p = null;
3398          for (;;) {
3399 <            K key = (K) s.readObject();
3400 <            V value = (V) s.readObject();
3401 <            if (key == null)
3399 >            K k = (K) s.readObject();
3400 >            V v = (V) s.readObject();
3401 >            if (k != null && v != null) {
3402 >                int h = spread(k.hashCode());
3403 >                p = new Node(h, k, v, p);
3404 >                ++size;
3405 >            }
3406 >            else
3407                  break;
3408 <            put(key, value);
3408 >        }
3409 >        if (p != null) {
3410 >            boolean init = false;
3411 >            int n;
3412 >            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
3413 >                n = MAXIMUM_CAPACITY;
3414 >            else {
3415 >                int sz = (int)size;
3416 >                n = tableSizeFor(sz + (sz >>> 1) + 1);
3417 >            }
3418 >            int sc = sizeCtl;
3419 >            boolean collide = false;
3420 >            if (n > sc &&
3421 >                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
3422 >                try {
3423 >                    if (table == null) {
3424 >                        init = true;
3425 >                        Node[] tab = new Node[n];
3426 >                        int mask = n - 1;
3427 >                        while (p != null) {
3428 >                            int j = p.hash & mask;
3429 >                            Node next = p.next;
3430 >                            Node q = p.next = tabAt(tab, j);
3431 >                            setTabAt(tab, j, p);
3432 >                            if (!collide && q != null && q.hash == p.hash)
3433 >                                collide = true;
3434 >                            p = next;
3435 >                        }
3436 >                        table = tab;
3437 >                        counter.add(size);
3438 >                        sc = n - (n >>> 2);
3439 >                    }
3440 >                } finally {
3441 >                    sizeCtl = sc;
3442 >                }
3443 >                if (collide) { // rescan and convert to TreeBins
3444 >                    Node[] tab = table;
3445 >                    for (int i = 0; i < tab.length; ++i) {
3446 >                        int c = 0;
3447 >                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
3448 >                            if (++c > TREE_THRESHOLD &&
3449 >                                (e.key instanceof Comparable)) {
3450 >                                replaceWithTreeBin(tab, i, e.key);
3451 >                                break;
3452 >                            }
3453 >                        }
3454 >                    }
3455 >                }
3456 >            }
3457 >            if (!init) { // Can only happen if unsafely published.
3458 >                while (p != null) {
3459 >                    internalPut(p.key, p.val);
3460 >                    p = p.next;
3461 >                }
3462 >            }
3463 >        }
3464 >    }
3465 >
3466 >
3467 >    // -------------------------------------------------------
3468 >
3469 >    // Sams
3470 >    /** Interface describing a void action of one argument */
3471 >    public interface Action<A> { void apply(A a); }
3472 >    /** Interface describing a void action of two arguments */
3473 >    public interface BiAction<A,B> { void apply(A a, B b); }
3474 >    /** Interface describing a function of one argument */
3475 >    public interface Fun<A,T> { T apply(A a); }
3476 >    /** Interface describing a function of two arguments */
3477 >    public interface BiFun<A,B,T> { T apply(A a, B b); }
3478 >    /** Interface describing a function of no arguments */
3479 >    public interface Generator<T> { T apply(); }
3480 >    /** Interface describing a function mapping its argument to a double */
3481 >    public interface ObjectToDouble<A> { double apply(A a); }
3482 >    /** Interface describing a function mapping its argument to a long */
3483 >    public interface ObjectToLong<A> { long apply(A a); }
3484 >    /** Interface describing a function mapping its argument to an int */
3485 >    public interface ObjectToInt<A> {int apply(A a); }
3486 >    /** Interface describing a function mapping two arguments to a double */
3487 >    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
3488 >    /** Interface describing a function mapping two arguments to a long */
3489 >    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
3490 >    /** Interface describing a function mapping two arguments to an int */
3491 >    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
3492 >    /** Interface describing a function mapping a double to a double */
3493 >    public interface DoubleToDouble { double apply(double a); }
3494 >    /** Interface describing a function mapping a long to a long */
3495 >    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 >    // -------------------------------------------------------
3507 >
3508 >    /**
3509 >     * 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
3514 >     */
3515 >    public Parallel parallel(ForkJoinPool executor)  {
3516 >        return new Parallel(executor);
3517 >    }
3518 >
3519 >    /**
3520 >     * 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>
3620 >     */
3621 >    public class Parallel {
3622 >        final ForkJoinPool fjp;
3623 >
3624 >        /**
3625 >         * Returns an extended view of this map using the given
3626 >         * executor for bulk parallel operations.
3627 >         *
3628 >         * @param executor the executor
3629 >         */
3630 >        public Parallel(ForkJoinPool executor)  {
3631 >            this.fjp = executor;
3632 >        }
3633 >
3634 >        /**
3635 >         * 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 >                       (ConcurrentHashMapV8.this, action));
3642 >        }
3643 >
3644 >        /**
3645 >         * Performs the given action for each non-null transformation
3646 >         * 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
3652 >         */
3653 >        public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
3654 >                                Action<U> action) {
3655 >            fjp.invoke(ForkJoinTasks.forEach
3656 >                       (ConcurrentHashMapV8.this, transformer, action));
3657 >        }
3658 >
3659 >        /**
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 >                              (ConcurrentHashMapV8.this, searchFunction));
3674 >        }
3675 >
3676 >        /**
3677 >         * Returns the result of accumulating the given transformation
3678 >         * of all (key, value) pairs using the given reducer to
3679 >         * combine values, or null if none.
3680 >         *
3681 >         * @param transformer a function returning the transformation
3682 >         * for an element, or null of there is no transformation (in
3683 >         * which case it is not combined).
3684 >         * @param reducer a commutative associative combining function
3685 >         * @return the result of accumulating the given transformation
3686 >         * of all (key, value) pairs
3687 >         */
3688 >        public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
3689 >                            BiFun<? super U, ? super U, ? extends U> reducer) {
3690 >            return fjp.invoke(ForkJoinTasks.reduce
3691 >                              (ConcurrentHashMapV8.this, transformer, reducer));
3692 >        }
3693 >
3694 >        /**
3695 >         * Returns the result of accumulating the given transformation
3696 >         * of all (key, value) pairs using the given reducer to
3697 >         * combine values, and the given basis as an identity value.
3698 >         *
3699 >         * @param transformer a function returning the transformation
3700 >         * for an element
3701 >         * @param basis the identity (initial default value) for the reduction
3702 >         * @param reducer a commutative associative combining function
3703 >         * @return the result of accumulating the given transformation
3704 >         * of all (key, value) pairs
3705 >         */
3706 >        public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
3707 >                                     double basis,
3708 >                                     DoubleByDoubleToDouble reducer) {
3709 >            return fjp.invoke(ForkJoinTasks.reduceToDouble
3710 >                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3711 >        }
3712 >
3713 >        /**
3714 >         * 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.
3725 >         */
3726 >        public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
3727 >                                 long basis,
3728 >                                 LongByLongToLong reducer) {
3729 >            return fjp.invoke(ForkJoinTasks.reduceToLong
3730 >                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3731 >        }
3732 >
3733 >        /**
3734 >         * 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
3744 >         */
3745 >        public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
3746 >                               int basis,
3747 >                               IntByIntToInt reducer) {
3748 >            return fjp.invoke(ForkJoinTasks.reduceToInt
3749 >                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3750 >        }
3751 >
3752 >        /**
3753 >         * Performs the given action for each key
3754 >         *
3755 >         * @param action the action
3756 >         */
3757 >        public void forEachKey(Action<K> action) {
3758 >            fjp.invoke(ForkJoinTasks.forEachKey
3759 >                       (ConcurrentHashMapV8.this, action));
3760 >        }
3761 >
3762 >        /**
3763 >         * Performs the given action for each non-null transformation
3764 >         * of each key
3765 >         *
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
3770 >         */
3771 >        public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
3772 >                                   Action<U> action) {
3773 >            fjp.invoke(ForkJoinTasks.forEachKey
3774 >                       (ConcurrentHashMapV8.this, transformer, action));
3775 >        }
3776 >
3777 >        /**
3778 >         * Returns a non-null result from applying the given search
3779 >         * 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
3788 >         */
3789 >        public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
3790 >            return fjp.invoke(ForkJoinTasks.searchKeys
3791 >                              (ConcurrentHashMapV8.this, searchFunction));
3792 >        }
3793 >
3794 >        /**
3795 >         * Returns the result of accumulating all keys using the given
3796 >         * reducer to combine values, or null if none.
3797 >         *
3798 >         * @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
3801 >         */
3802 >        public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
3803 >            return fjp.invoke(ForkJoinTasks.reduceKeys
3804 >                              (ConcurrentHashMapV8.this, reducer));
3805 >        }
3806 >
3807 >        /**
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 >                              (ConcurrentHashMapV8.this, transformer, reducer));
3823 >        }
3824 >
3825 >        /**
3826 >         * Returns the result of accumulating the given transformation
3827 >         * of all keys using the given reducer to combine values, and
3828 >         * the given basis as an identity value.
3829 >         *
3830 >         * @param transformer a function returning the transformation
3831 >         * for an element
3832 >         * @param basis the identity (initial default value) for the reduction
3833 >         * @param reducer a commutative associative combining function
3834 >         * @return  the result of accumulating the given transformation
3835 >         * of all keys
3836 >         */
3837 >        public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
3838 >                                         double basis,
3839 >                                         DoubleByDoubleToDouble reducer) {
3840 >            return fjp.invoke(ForkJoinTasks.reduceKeysToDouble
3841 >                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3842 >        }
3843 >
3844 >        /**
3845 >         * 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 >                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3861 >        }
3862 >
3863 >        /**
3864 >         * Returns the result of accumulating the given transformation
3865 >         * of all keys using the given reducer to combine values, and
3866 >         * the given basis as an identity value.
3867 >         *
3868 >         * @param transformer a function returning the transformation
3869 >         * for an element
3870 >         * @param basis the identity (initial default value) for the reduction
3871 >         * @param reducer a commutative associative combining function
3872 >         * @return the result of accumulating the given transformation
3873 >         * of all keys
3874 >         */
3875 >        public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3876 >                                   int basis,
3877 >                                   IntByIntToInt reducer) {
3878 >            return fjp.invoke(ForkJoinTasks.reduceKeysToInt
3879 >                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3880 >        }
3881 >
3882 >        /**
3883 >         * Performs the given action for each value
3884 >         *
3885 >         * @param action the action
3886 >         */
3887 >        public void forEachValue(Action<V> action) {
3888 >            fjp.invoke(ForkJoinTasks.forEachValue
3889 >                       (ConcurrentHashMapV8.this, action));
3890 >        }
3891 >
3892 >        /**
3893 >         * Performs the given action for each non-null transformation
3894 >         * of each value
3895 >         *
3896 >         * @param transformer a function returning the transformation
3897 >         * for an element, or null of there is no transformation (in
3898 >         * which case the action is not applied).
3899 >         */
3900 >        public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
3901 >                                     Action<U> action) {
3902 >            fjp.invoke(ForkJoinTasks.forEachValue
3903 >                       (ConcurrentHashMapV8.this, transformer, action));
3904 >        }
3905 >
3906 >        /**
3907 >         * Returns a non-null result from applying the given search
3908 >         * function on each value, or null if none.  Further element
3909 >         * processing is suppressed upon success. However, this method
3910 >         * does not return until other in-progress parallel
3911 >         * invocations of the search function also complete.
3912 >         *
3913 >         * @param searchFunction a function returning a non-null
3914 >         * result on success, else null
3915 >         * @return a non-null result from applying the given search
3916 >         * function on each value, or null if none
3917 >         *
3918 >         */
3919 >        public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
3920 >            return fjp.invoke(ForkJoinTasks.searchValues
3921 >                              (ConcurrentHashMapV8.this, searchFunction));
3922 >        }
3923 >
3924 >        /**
3925 >         * 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
3930 >         */
3931 >        public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
3932 >            return fjp.invoke(ForkJoinTasks.reduceValues
3933 >                              (ConcurrentHashMapV8.this, reducer));
3934 >        }
3935 >
3936 >        /**
3937 >         * Returns the result of accumulating the given transformation
3938 >         * of all values using the given reducer to combine values, or
3939 >         * null if none.
3940 >         *
3941 >         * @param transformer a function returning the transformation
3942 >         * for an element, or null of there is no transformation (in
3943 >         * which case it is not combined).
3944 >         * @param reducer a commutative associative combining function
3945 >         * @return the result of accumulating the given transformation
3946 >         * of all values
3947 >         */
3948 >        public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
3949 >                                  BiFun<? super U, ? super U, ? extends U> reducer) {
3950 >            return fjp.invoke(ForkJoinTasks.reduceValues
3951 >                              (ConcurrentHashMapV8.this, transformer, reducer));
3952 >        }
3953 >
3954 >        /**
3955 >         * Returns the result of accumulating the given transformation
3956 >         * of all values using the given reducer to combine values,
3957 >         * and the given basis as an identity value.
3958 >         *
3959 >         * @param transformer a function returning the transformation
3960 >         * for an element
3961 >         * @param basis the identity (initial default value) for the reduction
3962 >         * @param reducer a commutative associative combining function
3963 >         * @return the result of accumulating the given transformation
3964 >         * of all values
3965 >         */
3966 >        public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
3967 >                                           double basis,
3968 >                                           DoubleByDoubleToDouble reducer) {
3969 >            return fjp.invoke(ForkJoinTasks.reduceValuesToDouble
3970 >                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3971 >        }
3972 >
3973 >        /**
3974 >         * 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
3984 >         */
3985 >        public long reduceValuesToLong(ObjectToLong<? super V> transformer,
3986 >                                       long basis,
3987 >                                       LongByLongToLong reducer) {
3988 >            return fjp.invoke(ForkJoinTasks.reduceValuesToLong
3989 >                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3990 >        }
3991 >
3992 >        /**
3993 >         * 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.
3996 >         *
3997 >         * @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
4003 >         */
4004 >        public int reduceValuesToInt(ObjectToInt<? super V> transformer,
4005 >                                     int basis,
4006 >                                     IntByIntToInt reducer) {
4007 >            return fjp.invoke(ForkJoinTasks.reduceValuesToInt
4008 >                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4009 >        }
4010 >
4011 >        /**
4012 >         * Perform the given action for each entry
4013 >         *
4014 >         * @param action the action
4015 >         */
4016 >        public void forEachEntry(Action<Map.Entry<K,V>> action) {
4017 >            fjp.invoke(ForkJoinTasks.forEachEntry
4018 >                       (ConcurrentHashMapV8.this, action));
4019 >        }
4020 >
4021 >        /**
4022 >         * Perform the given action for each non-null transformation
4023 >         * of each entry
4024 >         *
4025 >         * @param transformer a function returning the transformation
4026 >         * for an element, or null of there is no transformation (in
4027 >         * which case the action is not applied).
4028 >         * @param action the action
4029 >         */
4030 >        public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
4031 >                                     Action<U> action) {
4032 >            fjp.invoke(ForkJoinTasks.forEachEntry
4033 >                       (ConcurrentHashMapV8.this, transformer, action));
4034 >        }
4035 >
4036 >        /**
4037 >         * Returns a non-null result from applying the given search
4038 >         * function on each entry, or null if none.  Further element
4039 >         * processing is suppressed upon success. However, this method
4040 >         * 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 >                              (ConcurrentHashMapV8.this, searchFunction));
4051 >        }
4052 >
4053 >        /**
4054 >         * Returns the result of accumulating all entries using the
4055 >         * given reducer to combine values, or null if none.
4056 >         *
4057 >         * @param reducer a commutative associative combining function
4058 >         * @return the result of accumulating all entries
4059 >         */
4060 >        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 >                              (ConcurrentHashMapV8.this, reducer));
4063 >        }
4064 >
4065 >        /**
4066 >         * Returns the result of accumulating the given transformation
4067 >         * of all entries using the given reducer to combine values,
4068 >         * or null if none.
4069 >         *
4070 >         * @param transformer a function returning the transformation
4071 >         * for an element, or null of there is no transformation (in
4072 >         * 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 >                              (ConcurrentHashMapV8.this, transformer, reducer));
4081 >        }
4082 >
4083 >        /**
4084 >         * Returns the result of accumulating the given transformation
4085 >         * of all entries using the given reducer to combine values,
4086 >         * and the given basis as an identity value.
4087 >         *
4088 >         * @param transformer a function returning the transformation
4089 >         * for an element
4090 >         * @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 >                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4100 >        }
4101 >
4102 >        /**
4103 >         * Returns the result of accumulating the given transformation
4104 >         * of all entries using the given reducer to combine values,
4105 >         * and the given basis as an identity value.
4106 >         *
4107 >         * @param transformer a function returning the transformation
4108 >         * for an element
4109 >         * @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 >                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4119 >        }
4120 >
4121 >        /**
4122 >         * Returns the result of accumulating the given transformation
4123 >         * of all entries using the given reducer to combine values,
4124 >         * and the given basis as an identity value.
4125 >         *
4126 >         * @param transformer a function returning the transformation
4127 >         * for an element
4128 >         * @param basis the identity (initial default value) for the reduction
4129 >         * @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 >                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4138 >        }
4139 >    }
4140 >
4141 >    // ---------------------------------------------------------------------
4142 >
4143 >    /**
4144 >     * 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.
4150 >     */
4151 >    public static class ForkJoinTasks {
4152 >        private ForkJoinTasks() {}
4153 >
4154 >        /**
4155 >         * Returns a task that when invoked, performs the given
4156 >         * action for each (key, value)
4157 >         *
4158 >         * @param map the map
4159 >         * @param action the action
4160 >         * @return the task
4161 >         */
4162 >        public static <K,V> ForkJoinTask<Void> forEach
4163 >            (ConcurrentHashMapV8<K,V> map,
4164 >             BiAction<K,V> action) {
4165 >            if (action == null) throw new NullPointerException();
4166 >            return new ForEachMappingTask<K,V>(map, action);
4167 >        }
4168 >
4169 >        /**
4170 >         * Returns a task that when invoked, performs the given
4171 >         * action for each non-null transformation of each (key, value)
4172 >         *
4173 >         * @param map the map
4174 >         * @param transformer a function returning the transformation
4175 >         * 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
4179 >         */
4180 >        public static <K,V,U> ForkJoinTask<Void> forEach
4181 >            (ConcurrentHashMapV8<K,V> map,
4182 >             BiFun<? super K, ? super V, ? extends U> transformer,
4183 >             Action<U> action) {
4184 >            if (transformer == null || action == null)
4185 >                throw new NullPointerException();
4186 >            return new ForEachTransformedMappingTask<K,V,U>
4187 >                (map, transformer, action);
4188 >        }
4189 >
4190 >        /**
4191 >         * Returns a task that when invoked, returns a non-null
4192 >         * result from applying the given search function on each
4193 >         * (key, value), or null if none.  Further element processing
4194 >         * is suppressed upon success. However, this method does not
4195 >         * return until other in-progress parallel invocations of the
4196 >         * search function also complete.
4197 >         *
4198 >         * @param map the map
4199 >         * @param searchFunction a function returning a non-null
4200 >         * result on success, else null
4201 >         * @return the task
4202 >         */
4203 >        public static <K,V,U> ForkJoinTask<U> search
4204 >            (ConcurrentHashMapV8<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>());
4210 >        }
4211 >
4212 >        /**
4213 >         * Returns a task that when invoked, returns the result of
4214 >         * accumulating the given transformation of all (key, value) pairs
4215 >         * 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 >            (ConcurrentHashMapV8<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);
4232 >        }
4233 >
4234 >        /**
4235 >         * Returns a task that when invoked, returns the result of
4236 >         * accumulating the given transformation of all (key, value) pairs
4237 >         * using the given reducer to combine values, and the given
4238 >         * basis as an identity value.
4239 >         *
4240 >         * @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 >            (ConcurrentHashMapV8<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);
4256 >        }
4257 >
4258 >        /**
4259 >         * Returns a task that when invoked, returns the result of
4260 >         * accumulating the given transformation of all (key, value) pairs
4261 >         * using the given reducer to combine values, and the given
4262 >         * 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 >            (ConcurrentHashMapV8<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);
4280 >        }
4281 >
4282 >        /**
4283 >         * Returns a task that when invoked, returns the result of
4284 >         * accumulating the given transformation of all (key, value) pairs
4285 >         * using the given reducer to combine values, and the given
4286 >         * basis as an identity value.
4287 >         *
4288 >         * @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 >            (ConcurrentHashMapV8<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);
4303 >        }
4304 >
4305 >        /**
4306 >         * Returns a task that when invoked, performs the given action
4307 >         * for each key
4308 >         *
4309 >         * @param map the map
4310 >         * @param action the action
4311 >         * @return the task
4312 >         */
4313 >        public static <K,V> ForkJoinTask<Void> forEachKey
4314 >            (ConcurrentHashMapV8<K,V> map,
4315 >             Action<K> action) {
4316 >            if (action == null) throw new NullPointerException();
4317 >            return new ForEachKeyTask<K,V>(map, action);
4318 >        }
4319 >
4320 >        /**
4321 >         * Returns a task that when invoked, performs the given action
4322 >         * for each non-null transformation of each key
4323 >         *
4324 >         * @param map the map
4325 >         * @param transformer a function returning the transformation
4326 >         * 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 >            (ConcurrentHashMapV8<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);
4339 >        }
4340 >
4341 >        /**
4342 >         * Returns a task that when invoked, returns a non-null result
4343 >         * from applying the given search function on each key, or
4344 >         * 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 >            (ConcurrentHashMapV8<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>());
4361 >        }
4362 >
4363 >        /**
4364 >         * Returns a task that when invoked, returns the result of
4365 >         * accumulating all keys using the given reducer to combine
4366 >         * values, or null if none.
4367 >         *
4368 >         * @param map the map
4369 >         * @param reducer a commutative associative combining function
4370 >         * @return the task
4371 >         */
4372 >        public static <K,V> ForkJoinTask<K> reduceKeys
4373 >            (ConcurrentHashMapV8<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);
4378 >        }
4379 >        /**
4380 >         * Returns a task that when invoked, returns the result of
4381 >         * accumulating the given transformation of all keys using the given
4382 >         * reducer to combine values, or null if none.
4383 >         *
4384 >         * @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 >            (ConcurrentHashMapV8<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);
4399 >        }
4400 >
4401 >        /**
4402 >         * Returns a task that when invoked, returns the result of
4403 >         * accumulating the given transformation of all keys using the given
4404 >         * 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 >            (ConcurrentHashMapV8<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);
4423 >        }
4424 >
4425 >        /**
4426 >         * Returns a task that when invoked, returns the result of
4427 >         * accumulating the given transformation of all keys using the given
4428 >         * reducer to combine values, and the given basis as an
4429 >         * identity value.
4430 >         *
4431 >         * @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 >            (ConcurrentHashMapV8<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);
4447 >        }
4448 >
4449 >        /**
4450 >         * Returns a task that when invoked, returns the result of
4451 >         * accumulating the given transformation of all keys using the given
4452 >         * reducer to combine values, and the given basis as an
4453 >         * 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 >            (ConcurrentHashMapV8<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);
4471 >        }
4472 >
4473 >        /**
4474 >         * Returns a task that when invoked, performs the given action
4475 >         * for each value
4476 >         *
4477 >         * @param map the map
4478 >         * @param action the action
4479 >         */
4480 >        public static <K,V> ForkJoinTask<Void> forEachValue
4481 >            (ConcurrentHashMapV8<K,V> map,
4482 >             Action<V> action) {
4483 >            if (action == null) throw new NullPointerException();
4484 >            return new ForEachValueTask<K,V>(map, action);
4485 >        }
4486 >
4487 >        /**
4488 >         * Returns a task that when invoked, performs the given action
4489 >         * 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
4496 >         */
4497 >        public static <K,V,U> ForkJoinTask<Void> forEachValue
4498 >            (ConcurrentHashMapV8<K,V> map,
4499 >             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 >        }
4506 >
4507 >        /**
4508 >         * 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.
4514 >         *
4515 >         * @param map the map
4516 >         * @param searchFunction a function returning a non-null
4517 >         * result on success, else null
4518 >         * @return the task
4519 >         *
4520 >         */
4521 >        public static <K,V,U> ForkJoinTask<U> searchValues
4522 >            (ConcurrentHashMapV8<K,V> map,
4523 >             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 >        }
4529 >
4530 >        /**
4531 >         * Returns a task that when invoked, returns the result of
4532 >         * accumulating all values using the given reducer to combine
4533 >         * values, or null if none.
4534 >         *
4535 >         * @param map the map
4536 >         * @param reducer a commutative associative combining function
4537 >         * @return the task
4538 >         */
4539 >        public static <K,V> ForkJoinTask<V> reduceValues
4540 >            (ConcurrentHashMapV8<K,V> map,
4541 >             BiFun<? super V, ? super V, ? extends V> reducer) {
4542 >            if (reducer == null) throw new NullPointerException();
4543 >            return new ReduceValuesTask<K,V>
4544 >                (map, reducer);
4545 >        }
4546 >
4547 >        /**
4548 >         * Returns a task that when invoked, returns the result of
4549 >         * accumulating the given transformation of all values using the
4550 >         * given reducer to combine values, or null if none.
4551 >         *
4552 >         * @param map the map
4553 >         * @param transformer a function returning the transformation
4554 >         * for an element, or null of there is no transformation (in
4555 >         * which case it is not combined).
4556 >         * @param reducer a commutative associative combining function
4557 >         * @return the task
4558 >         */
4559 >        public static <K,V,U> ForkJoinTask<U> reduceValues
4560 >            (ConcurrentHashMapV8<K,V> map,
4561 >             Fun<? super V, ? extends U> transformer,
4562 >             BiFun<? super U, ? super U, ? extends U> reducer) {
4563 >            if (transformer == null || reducer == null)
4564 >                throw new NullPointerException();
4565 >            return new MapReduceValuesTask<K,V,U>
4566 >                (map, transformer, reducer);
4567 >        }
4568 >
4569 >        /**
4570 >         * Returns a task that when invoked, returns the result of
4571 >         * accumulating the given transformation of all values using the
4572 >         * given reducer to combine values, and the given basis as an
4573 >         * identity value.
4574 >         *
4575 >         * @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
4581 >         */
4582 >        public static <K,V> ForkJoinTask<Double> reduceValuesToDouble
4583 >            (ConcurrentHashMapV8<K,V> map,
4584 >             ObjectToDouble<? super V> transformer,
4585 >             double basis,
4586 >             DoubleByDoubleToDouble reducer) {
4587 >            if (transformer == null || reducer == null)
4588 >                throw new NullPointerException();
4589 >            return new MapReduceValuesToDoubleTask<K,V>
4590 >                (map, transformer, basis, reducer);
4591 >        }
4592 >
4593 >        /**
4594 >         * Returns a task that when invoked, returns the result of
4595 >         * accumulating the given transformation of all values using the
4596 >         * given reducer to combine values, and the given basis as an
4597 >         * identity value.
4598 >         *
4599 >         * @param map the map
4600 >         * @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 >            (ConcurrentHashMapV8<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);
4615 >        }
4616 >
4617 >        /**
4618 >         * Returns a task that when invoked, returns the result of
4619 >         * accumulating the given transformation of all values using the
4620 >         * given reducer to combine values, and the given basis as an
4621 >         * identity value.
4622 >         *
4623 >         * @param map the map
4624 >         * @param transformer a function returning the transformation
4625 >         * for an element
4626 >         * @param basis the identity (initial default value) for the reduction
4627 >         * @param reducer a commutative associative combining function
4628 >         * @return the task
4629 >         */
4630 >        public static <K,V> ForkJoinTask<Integer> reduceValuesToInt
4631 >            (ConcurrentHashMapV8<K,V> map,
4632 >             ObjectToInt<? super V> transformer,
4633 >             int basis,
4634 >             IntByIntToInt reducer) {
4635 >            if (transformer == null || reducer == null)
4636 >                throw new NullPointerException();
4637 >            return new MapReduceValuesToIntTask<K,V>
4638 >                (map, transformer, basis, reducer);
4639 >        }
4640 >
4641 >        /**
4642 >         * Returns a task that when invoked, perform the given action
4643 >         * for each entry
4644 >         *
4645 >         * @param map the map
4646 >         * @param action the action
4647 >         */
4648 >        public static <K,V> ForkJoinTask<Void> forEachEntry
4649 >            (ConcurrentHashMapV8<K,V> map,
4650 >             Action<Map.Entry<K,V>> action) {
4651 >            if (action == null) throw new NullPointerException();
4652 >            return new ForEachEntryTask<K,V>(map, action);
4653 >        }
4654 >
4655 >        /**
4656 >         * Returns a task that when invoked, perform the given action
4657 >         * for each non-null transformation of each entry
4658 >         *
4659 >         * @param map the map
4660 >         * @param transformer a function returning the transformation
4661 >         * for an element, or null of there is no transformation (in
4662 >         * which case the action is not applied).
4663 >         * @param action the action
4664 >         */
4665 >        public static <K,V,U> ForkJoinTask<Void> forEachEntry
4666 >            (ConcurrentHashMapV8<K,V> map,
4667 >             Fun<Map.Entry<K,V>, ? extends U> transformer,
4668 >             Action<U> action) {
4669 >            if (transformer == null || action == null)
4670 >                throw new NullPointerException();
4671 >            return new ForEachTransformedEntryTask<K,V,U>
4672 >                (map, transformer, action);
4673 >        }
4674 >
4675 >        /**
4676 >         * Returns a task that when invoked, returns a non-null result
4677 >         * 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
4687 >         *
4688 >         */
4689 >        public static <K,V,U> ForkJoinTask<U> searchEntries
4690 >            (ConcurrentHashMapV8<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 >        }
4697 >
4698 >        /**
4699 >         * Returns a task that when invoked, returns the result of
4700 >         * 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
4706 >         */
4707 >        public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries
4708 >            (ConcurrentHashMapV8<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 >        }
4714 >
4715 >        /**
4716 >         * Returns a task that when invoked, returns the result of
4717 >         * accumulating the given transformation of all entries using the
4718 >         * given reducer to combine values, or null if none.
4719 >         *
4720 >         * @param map the map
4721 >         * @param transformer a function returning the transformation
4722 >         * 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
4726 >         */
4727 >        public static <K,V,U> ForkJoinTask<U> reduceEntries
4728 >            (ConcurrentHashMapV8<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 >        }
4736 >
4737 >        /**
4738 >         * 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
4749 >         */
4750 >        public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble
4751 >            (ConcurrentHashMapV8<K,V> map,
4752 >             ObjectToDouble<Map.Entry<K,V>> transformer,
4753 >             double basis,
4754 >             DoubleByDoubleToDouble reducer) {
4755 >            if (transformer == null || reducer == null)
4756 >                throw new NullPointerException();
4757 >            return new MapReduceEntriesToDoubleTask<K,V>
4758 >                (map, transformer, basis, reducer);
4759 >        }
4760 >
4761 >        /**
4762 >         * Returns a task that when invoked, returns the result of
4763 >         * accumulating the given transformation of all entries using the
4764 >         * given reducer to combine values, and the given basis as an
4765 >         * identity value.
4766 >         *
4767 >         * @param map the map
4768 >         * @param transformer a function returning the transformation
4769 >         * for an element
4770 >         * @param basis the identity (initial default value) for the reduction
4771 >         * @param reducer a commutative associative combining function
4772 >         * @return the task
4773 >         */
4774 >        public static <K,V> ForkJoinTask<Long> reduceEntriesToLong
4775 >            (ConcurrentHashMapV8<K,V> map,
4776 >             ObjectToLong<Map.Entry<K,V>> transformer,
4777 >             long basis,
4778 >             LongByLongToLong reducer) {
4779 >            if (transformer == null || reducer == null)
4780 >                throw new NullPointerException();
4781 >            return new MapReduceEntriesToLongTask<K,V>
4782 >                (map, transformer, basis, reducer);
4783 >        }
4784 >
4785 >        /**
4786 >         * Returns a task that when invoked, returns the result of
4787 >         * accumulating the given transformation of all entries using the
4788 >         * given reducer to combine values, and the given basis as an
4789 >         * identity value.
4790 >         *
4791 >         * @param map the map
4792 >         * @param transformer a function returning the transformation
4793 >         * for an element
4794 >         * @param basis the identity (initial default value) for the reduction
4795 >         * @param reducer a commutative associative combining function
4796 >         * @return the task
4797 >         */
4798 >        public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt
4799 >            (ConcurrentHashMapV8<K,V> map,
4800 >             ObjectToInt<Map.Entry<K,V>> transformer,
4801 >             int basis,
4802 >             IntByIntToInt reducer) {
4803 >            if (transformer == null || reducer == null)
4804 >                throw new NullPointerException();
4805 >            return new MapReduceEntriesToIntTask<K,V>
4806 >                (map, transformer, basis, reducer);
4807          }
4808      }
4809  
4810 +    // -------------------------------------------------------
4811 +
4812 +    /**
4813 +     * Base for FJ tasks for bulk operations. This adds a variant of
4814 +     * CountedCompleters and some split and merge bookeeping to
4815 +     * iterator functionality. The forEach and reduce methods are
4816 +     * similar to those illustrated in CountedCompleter documentation,
4817 +     * 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
4827 +
4828 +        /** Constructor for root tasks */
4829 +        BulkTask(ConcurrentHashMapV8<K,V> map) {
4830 +            super(map);
4831 +            this.parent = null;
4832 +            this.batch = -1; // force call to batch() on execution
4833 +        }
4834 +
4835 +        /** Constructor for subtasks */
4836 +        BulkTask(BulkTask<K,V,?> parent, int batch, boolean split) {
4837 +            super(parent, split);
4838 +            this.parent = parent;
4839 +            this.batch = batch;
4840 +        }
4841 +
4842 +        // FJ methods
4843 +
4844 +        /**
4845 +         * Propagate completion. Note that all reduce actions
4846 +         * bypass this method to combine while completing.
4847 +         */
4848 +        final void tryComplete() {
4849 +            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;
4855 +                    }
4856 +                }
4857 +                else if (U.compareAndSwapInt(a, PENDING, c, c - 1))
4858 +                    break;
4859 +            }
4860 +        }
4861 +
4862 +        /**
4863 +         * Force root task to throw exception unless already complete.
4864 +         */
4865 +        final void tryAbortComputation(Throwable ex) {
4866 +            for (BulkTask<K,V,?> a = this;;) {
4867 +                BulkTask<K,V,?> p = a.parent;
4868 +                if (p == null) {
4869 +                    a.completeExceptionally(ex);
4870 +                    break;
4871 +                }
4872 +                a = p;
4873 +            }
4874 +        }
4875 +
4876 +        public final boolean exec() {
4877 +            try {
4878 +                compute();
4879 +            }
4880 +            catch(Throwable ex) {
4881 +                tryAbortComputation(ex);
4882 +            }
4883 +            return false;
4884 +        }
4885 +
4886 +        public abstract void compute();
4887 +
4888 +        // utilities
4889 +
4890 +        /** CompareAndSet pending count */
4891 +        final boolean casPending(int cmp, int val) {
4892 +            return U.compareAndSwapInt(this, PENDING, cmp, val);
4893 +        }
4894 +
4895 +        /**
4896 +         * 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.
4901 +         */
4902 +        final int batch() {
4903 +            int b = batch;
4904 +            if (b < 0) {
4905 +                long n = map.counter.sum();
4906 +                int sp = getPool().getParallelism() << 3; // slack of 8
4907 +                b = batch = (n <= 0L)? 0 : (n < (long)sp) ? (int)n : sp;
4908 +            }
4909 +            return b;
4910 +        }
4911 +
4912 +        /**
4913 +         * Error message for hoisted null checks of functions
4914 +         */
4915 +        static final String NullFunctionMessage =
4916 +            "Unexpected null function";
4917 +
4918 +        /**
4919 +         * Return exportable snapshot entry
4920 +         */
4921 +        static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
4922 +            return new AbstractMap.SimpleEntry(k, v);
4923 +        }
4924 +
4925 +        // Unsafe mechanics
4926 +        private static final sun.misc.Unsafe U;
4927 +        private static final long PENDING;
4928 +        static {
4929 +            try {
4930 +                U = sun.misc.Unsafe.getUnsafe();
4931 +                PENDING = U.objectFieldOffset
4932 +                    (BulkTask.class.getDeclaredField("pending"));
4933 +            } catch (Exception e) {
4934 +                throw new Error(e);
4935 +            }
4936 +        }
4937 +    }
4938 +
4939 +    /*
4940 +     * Task classes. Coded in a regular but ugly format/style to
4941 +     * simplify checks that each variant differs in the right way from
4942 +     * others.
4943 +     */
4944 +
4945 +    static final class ForEachKeyTask<K,V>
4946 +        extends BulkTask<K,V,Void> {
4947 +        final Action<K> action;
4948 +        ForEachKeyTask
4949 +            (ConcurrentHashMapV8<K,V> m,
4950 +             Action<K> action) {
4951 +            super(m);
4952 +            this.action = action;
4953 +        }
4954 +        ForEachKeyTask
4955 +            (BulkTask<K,V,?> p, int b, boolean split,
4956 +             Action<K> action) {
4957 +            super(p, b, split);
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();
4972 +        }
4973 +    }
4974 +
4975 +    static final class ForEachValueTask<K,V>
4976 +        extends BulkTask<K,V,Void> {
4977 +        final Action<V> action;
4978 +        ForEachValueTask
4979 +            (ConcurrentHashMapV8<K,V> m,
4980 +             Action<V> action) {
4981 +            super(m);
4982 +            this.action = action;
4983 +        }
4984 +        ForEachValueTask
4985 +            (BulkTask<K,V,?> p, int b, boolean split,
4986 +             Action<V> action) {
4987 +            super(p, b, split);
4988 +            this.action = action;
4989 +        }
4990 +        public final void compute() {
4991 +            final Action<V> action = this.action;
4992 +            if (action == null)
4993 +                throw new Error(NullFunctionMessage);
4994 +            int b = batch(), c;
4995 +            while (b > 1 && baseIndex != baseLimit) {
4996 +                do {} while (!casPending(c = pending, c+1));
4997 +                new ForEachValueTask<K,V>(this, b >>>= 1, true, action).fork();
4998 +            }
4999 +            Object v;
5000 +            while ((v = advance()) != null)
5001 +                action.apply((V)v);
5002 +            tryComplete();
5003 +        }
5004 +    }
5005 +
5006 +    static final class ForEachEntryTask<K,V>
5007 +        extends BulkTask<K,V,Void> {
5008 +        final Action<Entry<K,V>> action;
5009 +        ForEachEntryTask
5010 +            (ConcurrentHashMapV8<K,V> m,
5011 +             Action<Entry<K,V>> action) {
5012 +            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);
5019 +            this.action = action;
5020 +        }
5021 +        public final void compute() {
5022 +            final Action<Entry<K,V>> action = this.action;
5023 +            if (action == null)
5024 +                throw new Error(NullFunctionMessage);
5025 +            int b = batch(), c;
5026 +            while (b > 1 && baseIndex != baseLimit) {
5027 +                do {} while (!casPending(c = pending, c+1));
5028 +                new ForEachEntryTask<K,V>(this, b >>>= 1, true, action).fork();
5029 +            }
5030 +            Object v;
5031 +            while ((v = advance()) != null)
5032 +                action.apply(entryFor((K)nextKey, (V)v));
5033 +            tryComplete();
5034 +        }
5035 +    }
5036 +
5037 +    static final class ForEachMappingTask<K,V>
5038 +        extends BulkTask<K,V,Void> {
5039 +        final BiAction<K,V> action;
5040 +        ForEachMappingTask
5041 +            (ConcurrentHashMapV8<K,V> m,
5042 +             BiAction<K,V> action) {
5043 +            super(m);
5044 +            this.action = action;
5045 +        }
5046 +        ForEachMappingTask
5047 +            (BulkTask<K,V,?> p, int b, boolean split,
5048 +             BiAction<K,V> action) {
5049 +            super(p, b, split);
5050 +            this.action = action;
5051 +        }
5052 +
5053 +        public final void compute() {
5054 +            final BiAction<K,V> action = this.action;
5055 +            if (action == null)
5056 +                throw new Error(NullFunctionMessage);
5057 +            int b = batch(), c;
5058 +            while (b > 1 && baseIndex != baseLimit) {
5059 +                do {} while (!casPending(c = pending, c+1));
5060 +                new ForEachMappingTask<K,V>(this, b >>>= 1, true,
5061 +                                            action).fork();
5062 +            }
5063 +            Object v;
5064 +            while ((v = advance()) != null)
5065 +                action.apply((K)nextKey, (V)v);
5066 +            tryComplete();
5067 +        }
5068 +    }
5069 +
5070 +    static final class ForEachTransformedKeyTask<K,V,U>
5071 +        extends BulkTask<K,V,Void> {
5072 +        final Fun<? super K, ? extends U> transformer;
5073 +        final Action<U> action;
5074 +        ForEachTransformedKeyTask
5075 +            (ConcurrentHashMapV8<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 +        }
5083 +        ForEachTransformedKeyTask
5084 +            (BulkTask<K,V,?> p, int b, boolean split,
5085 +             Fun<? super K, ? extends U> transformer,
5086 +             Action<U> action) {
5087 +            super(p, b, split);
5088 +            this.transformer = transformer;
5089 +            this.action = action;
5090 +        }
5091 +        public final void compute() {
5092 +            final Fun<? super K, ? extends U> transformer =
5093 +                this.transformer;
5094 +            final Action<U> action = this.action;
5095 +            if (transformer == null || action == null)
5096 +                throw new Error(NullFunctionMessage);
5097 +            int b = batch(), c;
5098 +            while (b > 1 && baseIndex != baseLimit) {
5099 +                do {} while (!casPending(c = pending, c+1));
5100 +                new ForEachTransformedKeyTask<K,V,U>
5101 +                    (this, b >>>= 1, true, transformer, action).fork();
5102 +            }
5103 +            U u;
5104 +            while (advance() != null) {
5105 +                if ((u = transformer.apply((K)nextKey)) != null)
5106 +                    action.apply(u);
5107 +            }
5108 +            tryComplete();
5109 +        }
5110 +    }
5111 +
5112 +    static final class ForEachTransformedValueTask<K,V,U>
5113 +        extends BulkTask<K,V,Void> {
5114 +        final Fun<? super V, ? extends U> transformer;
5115 +        final Action<U> action;
5116 +        ForEachTransformedValueTask
5117 +            (ConcurrentHashMapV8<K,V> m,
5118 +             Fun<? super V, ? extends U> transformer,
5119 +             Action<U> action) {
5120 +            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;
5132 +        }
5133 +        public final void compute() {
5134 +            final Fun<? super V, ? extends U> transformer =
5135 +                this.transformer;
5136 +            final Action<U> action = this.action;
5137 +            if (transformer == null || action == null)
5138 +                throw new Error(NullFunctionMessage);
5139 +            int b = batch(), c;
5140 +            while (b > 1 && baseIndex != baseLimit) {
5141 +                do {} while (!casPending(c = pending, c+1));
5142 +                new ForEachTransformedValueTask<K,V,U>
5143 +                    (this, b >>>= 1, true, transformer, action).fork();
5144 +            }
5145 +            Object v; U u;
5146 +            while ((v = advance()) != null) {
5147 +                if ((u = transformer.apply((V)v)) != null)
5148 +                    action.apply(u);
5149 +            }
5150 +            tryComplete();
5151 +        }
5152 +    }
5153 +
5154 +    static final class ForEachTransformedEntryTask<K,V,U>
5155 +        extends BulkTask<K,V,Void> {
5156 +        final Fun<Map.Entry<K,V>, ? extends U> transformer;
5157 +        final Action<U> action;
5158 +        ForEachTransformedEntryTask
5159 +            (ConcurrentHashMapV8<K,V> m,
5160 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
5161 +             Action<U> action) {
5162 +            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;
5174 +        }
5175 +        public final void compute() {
5176 +            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5177 +                this.transformer;
5178 +            final Action<U> action = this.action;
5179 +            if (transformer == null || action == null)
5180 +                throw new Error(NullFunctionMessage);
5181 +            int b = batch(), c;
5182 +            while (b > 1 && baseIndex != baseLimit) {
5183 +                do {} while (!casPending(c = pending, c+1));
5184 +                new ForEachTransformedEntryTask<K,V,U>
5185 +                    (this, b >>>= 1, true, transformer, action).fork();
5186 +            }
5187 +            Object v; U u;
5188 +            while ((v = advance()) != null) {
5189 +                if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5190 +                    action.apply(u);
5191 +            }
5192 +            tryComplete();
5193 +        }
5194 +    }
5195 +
5196 +    static final class ForEachTransformedMappingTask<K,V,U>
5197 +        extends BulkTask<K,V,Void> {
5198 +        final BiFun<? super K, ? super V, ? extends U> transformer;
5199 +        final Action<U> action;
5200 +        ForEachTransformedMappingTask
5201 +            (ConcurrentHashMapV8<K,V> m,
5202 +             BiFun<? super K, ? super V, ? extends U> transformer,
5203 +             Action<U> action) {
5204 +            super(m);
5205 +            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;
5216 +        }
5217 +        public final void compute() {
5218 +            final BiFun<? super K, ? super V, ? extends U> transformer =
5219 +                this.transformer;
5220 +            final Action<U> action = this.action;
5221 +            if (transformer == null || action == null)
5222 +                throw new Error(NullFunctionMessage);
5223 +            int b = batch(), c;
5224 +            while (b > 1 && baseIndex != baseLimit) {
5225 +                do {} while (!casPending(c = pending, c+1));
5226 +                new ForEachTransformedMappingTask<K,V,U>
5227 +                    (this, b >>>= 1, true, transformer, action).fork();
5228 +            }
5229 +            Object v; U u;
5230 +            while ((v = advance()) != null) {
5231 +                if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5232 +                    action.apply(u);
5233 +            }
5234 +            tryComplete();
5235 +        }
5236 +    }
5237 +
5238 +    static final class SearchKeysTask<K,V,U>
5239 +        extends BulkTask<K,V,U> {
5240 +        final Fun<? super K, ? extends U> searchFunction;
5241 +        final AtomicReference<U> result;
5242 +        SearchKeysTask
5243 +            (ConcurrentHashMapV8<K,V> m,
5244 +             Fun<? super K, ? extends U> searchFunction,
5245 +             AtomicReference<U> result) {
5246 +            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);
5254 +            this.searchFunction = searchFunction; this.result = result;
5255 +        }
5256 +        public final void compute() {
5257 +            AtomicReference<U> result = this.result;
5258 +            final Fun<? super K, ? extends U> searchFunction =
5259 +                this.searchFunction;
5260 +            if (searchFunction == null || result == null)
5261 +                throw new Error(NullFunctionMessage);
5262 +            int b = batch(), c;
5263 +            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5264 +                do {} while (!casPending(c = pending, c+1));
5265 +                new SearchKeysTask<K,V,U>(this, b >>>= 1, true,
5266 +                                          searchFunction, result).fork();
5267 +            }
5268 +            U u;
5269 +            while (result.get() == null && advance() != null) {
5270 +                if ((u = searchFunction.apply((K)nextKey)) != null) {
5271 +                    result.compareAndSet(null, u);
5272 +                    break;
5273 +                }
5274 +            }
5275 +            tryComplete();
5276 +        }
5277 +        public final U getRawResult() { return result.get(); }
5278 +    }
5279 +
5280 +    static final class SearchValuesTask<K,V,U>
5281 +        extends BulkTask<K,V,U> {
5282 +        final Fun<? super V, ? extends U> searchFunction;
5283 +        final AtomicReference<U> result;
5284 +        SearchValuesTask
5285 +            (ConcurrentHashMapV8<K,V> m,
5286 +             Fun<? super V, ? extends U> searchFunction,
5287 +             AtomicReference<U> result) {
5288 +            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);
5296 +            this.searchFunction = searchFunction; this.result = result;
5297 +        }
5298 +        public final void compute() {
5299 +            AtomicReference<U> result = this.result;
5300 +            final Fun<? super V, ? extends U> searchFunction =
5301 +                this.searchFunction;
5302 +            if (searchFunction == null || result == null)
5303 +                throw new Error(NullFunctionMessage);
5304 +            int b = batch(), c;
5305 +            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5306 +                do {} while (!casPending(c = pending, c+1));
5307 +                new SearchValuesTask<K,V,U>(this, b >>>= 1, true,
5308 +                                            searchFunction, result).fork();
5309 +            }
5310 +            Object v; U u;
5311 +            while (result.get() == null && (v = advance()) != null) {
5312 +                if ((u = searchFunction.apply((V)v)) != null) {
5313 +                    result.compareAndSet(null, u);
5314 +                    break;
5315 +                }
5316 +            }
5317 +            tryComplete();
5318 +        }
5319 +        public final U getRawResult() { return result.get(); }
5320 +    }
5321 +
5322 +    static final class SearchEntriesTask<K,V,U>
5323 +        extends BulkTask<K,V,U> {
5324 +        final Fun<Entry<K,V>, ? extends U> searchFunction;
5325 +        final AtomicReference<U> result;
5326 +        SearchEntriesTask
5327 +            (ConcurrentHashMapV8<K,V> m,
5328 +             Fun<Entry<K,V>, ? extends U> searchFunction,
5329 +             AtomicReference<U> result) {
5330 +            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);
5338 +            this.searchFunction = searchFunction; this.result = result;
5339 +        }
5340 +        public final void compute() {
5341 +            AtomicReference<U> result = this.result;
5342 +            final Fun<Entry<K,V>, ? extends U> searchFunction =
5343 +                this.searchFunction;
5344 +            if (searchFunction == null || result == null)
5345 +                throw new Error(NullFunctionMessage);
5346 +            int b = batch(), c;
5347 +            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5348 +                do {} while (!casPending(c = pending, c+1));
5349 +                new SearchEntriesTask<K,V,U>(this, b >>>= 1, true,
5350 +                                             searchFunction, result).fork();
5351 +            }
5352 +            Object v; U u;
5353 +            while (result.get() == null && (v = advance()) != null) {
5354 +                if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
5355 +                    result.compareAndSet(null, u);
5356 +                    break;
5357 +                }
5358 +            }
5359 +            tryComplete();
5360 +        }
5361 +        public final U getRawResult() { return result.get(); }
5362 +    }
5363 +
5364 +    static final class SearchMappingsTask<K,V,U>
5365 +        extends BulkTask<K,V,U> {
5366 +        final BiFun<? super K, ? super V, ? extends U> searchFunction;
5367 +        final AtomicReference<U> result;
5368 +        SearchMappingsTask
5369 +            (ConcurrentHashMapV8<K,V> m,
5370 +             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,
5378 +             AtomicReference<U> result) {
5379 +            super(p, b, split);
5380 +            this.searchFunction = searchFunction; this.result = result;
5381 +        }
5382 +        public final void compute() {
5383 +            AtomicReference<U> result = this.result;
5384 +            final BiFun<? super K, ? super V, ? extends U> searchFunction =
5385 +                this.searchFunction;
5386 +            if (searchFunction == null || result == null)
5387 +                throw new Error(NullFunctionMessage);
5388 +            int b = batch(), c;
5389 +            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5390 +                do {} while (!casPending(c = pending, c+1));
5391 +                new SearchMappingsTask<K,V,U>(this, b >>>= 1, true,
5392 +                                              searchFunction, result).fork();
5393 +            }
5394 +            Object v; U u;
5395 +            while (result.get() == null && (v = advance()) != null) {
5396 +                if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5397 +                    result.compareAndSet(null, u);
5398 +                    break;
5399 +                }
5400 +            }
5401 +            tryComplete();
5402 +        }
5403 +        public final U getRawResult() { return result.get(); }
5404 +    }
5405 +
5406 +    static final class ReduceKeysTask<K,V>
5407 +        extends BulkTask<K,V,K> {
5408 +        final BiFun<? super K, ? super K, ? extends K> reducer;
5409 +        K result;
5410 +        ReduceKeysTask<K,V> sibling;
5411 +        ReduceKeysTask
5412 +            (ConcurrentHashMapV8<K,V> m,
5413 +             BiFun<? super K, ? super K, ? extends K> reducer) {
5414 +            super(m);
5415 +            this.reducer = reducer;
5416 +        }
5417 +        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 +
5424 +        public final void compute() {
5425 +            ReduceKeysTask<K,V> t = this;
5426 +            final BiFun<? super K, ? super K, ? extends K> reducer =
5427 +                this.reducer;
5428 +            if (reducer == null)
5429 +                throw new Error(NullFunctionMessage);
5430 +            int b = batch();
5431 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5432 +                b >>>= 1;
5433 +                t.pending = 1;
5434 +                ReduceKeysTask<K,V> rt =
5435 +                    new ReduceKeysTask<K,V>
5436 +                    (t, b, true, reducer);
5437 +                t = new ReduceKeysTask<K,V>
5438 +                    (t, b, false, reducer);
5439 +                t.sibling = rt;
5440 +                rt.sibling = t;
5441 +                rt.fork();
5442 +            }
5443 +            K r = null;
5444 +            while (t.advance() != null) {
5445 +                K u = (K)t.nextKey;
5446 +                r = (r == null) ? u : reducer.apply(r, u);
5447 +            }
5448 +            t.result = r;
5449 +            for (;;) {
5450 +                int c; BulkTask<K,V,?> par; ReduceKeysTask<K,V> s, p; K u;
5451 +                if ((par = t.parent) == null ||
5452 +                    !(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;
5460 +                }
5461 +                else if (p.casPending(c, 0))
5462 +                    break;
5463 +            }
5464 +        }
5465 +        public final K getRawResult() { return result; }
5466 +    }
5467 +
5468 +    static final class ReduceValuesTask<K,V>
5469 +        extends BulkTask<K,V,V> {
5470 +        final BiFun<? super V, ? super V, ? extends V> reducer;
5471 +        V result;
5472 +        ReduceValuesTask<K,V> sibling;
5473 +        ReduceValuesTask
5474 +            (ConcurrentHashMapV8<K,V> m,
5475 +             BiFun<? super V, ? super V, ? extends V> reducer) {
5476 +            super(m);
5477 +            this.reducer = reducer;
5478 +        }
5479 +        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 +
5486 +        public final void compute() {
5487 +            ReduceValuesTask<K,V> t = this;
5488 +            final BiFun<? super V, ? super V, ? extends V> reducer =
5489 +                this.reducer;
5490 +            if (reducer == null)
5491 +                throw new Error(NullFunctionMessage);
5492 +            int b = batch();
5493 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5494 +                b >>>= 1;
5495 +                t.pending = 1;
5496 +                ReduceValuesTask<K,V> rt =
5497 +                    new ReduceValuesTask<K,V>
5498 +                    (t, b, true, reducer);
5499 +                t = new ReduceValuesTask<K,V>
5500 +                    (t, b, false, reducer);
5501 +                t.sibling = rt;
5502 +                rt.sibling = t;
5503 +                rt.fork();
5504 +            }
5505 +            V r = null;
5506 +            Object v;
5507 +            while ((v = t.advance()) != null) {
5508 +                V u = (V)v;
5509 +                r = (r == null) ? u : reducer.apply(r, u);
5510 +            }
5511 +            t.result = r;
5512 +            for (;;) {
5513 +                int c; BulkTask<K,V,?> par; ReduceValuesTask<K,V> s, p; V u;
5514 +                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;
5523 +                }
5524 +                else if (p.casPending(c, 0))
5525 +                    break;
5526 +            }
5527 +        }
5528 +        public final V getRawResult() { return result; }
5529 +    }
5530 +
5531 +    static final class ReduceEntriesTask<K,V>
5532 +        extends BulkTask<K,V,Map.Entry<K,V>> {
5533 +        final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5534 +        Map.Entry<K,V> result;
5535 +        ReduceEntriesTask<K,V> sibling;
5536 +        ReduceEntriesTask
5537 +            (ConcurrentHashMapV8<K,V> m,
5538 +             BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5539 +            super(m);
5540 +            this.reducer = reducer;
5541 +        }
5542 +        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 +
5549 +        public final void compute() {
5550 +            ReduceEntriesTask<K,V> t = this;
5551 +            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
5552 +                this.reducer;
5553 +            if (reducer == null)
5554 +                throw new Error(NullFunctionMessage);
5555 +            int b = batch();
5556 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5557 +                b >>>= 1;
5558 +                t.pending = 1;
5559 +                ReduceEntriesTask<K,V> rt =
5560 +                    new ReduceEntriesTask<K,V>
5561 +                    (t, b, true, reducer);
5562 +                t = new ReduceEntriesTask<K,V>
5563 +                    (t, b, false, reducer);
5564 +                t.sibling = rt;
5565 +                rt.sibling = t;
5566 +                rt.fork();
5567 +            }
5568 +            Map.Entry<K,V> r = null;
5569 +            Object v;
5570 +            while ((v = t.advance()) != null) {
5571 +                Map.Entry<K,V> u = entryFor((K)t.nextKey, (V)v);
5572 +                r = (r == null) ? u : reducer.apply(r, u);
5573 +            }
5574 +            t.result = r;
5575 +            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;
5587 +                }
5588 +                else if (p.casPending(c, 0))
5589 +                    break;
5590 +            }
5591 +        }
5592 +        public final Map.Entry<K,V> getRawResult() { return result; }
5593 +    }
5594 +
5595 +    static final class MapReduceKeysTask<K,V,U>
5596 +        extends BulkTask<K,V,U> {
5597 +        final Fun<? super K, ? extends U> transformer;
5598 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5599 +        U result;
5600 +        MapReduceKeysTask<K,V,U> sibling;
5601 +        MapReduceKeysTask
5602 +            (ConcurrentHashMapV8<K,V> m,
5603 +             Fun<? super K, ? extends U> transformer,
5604 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5605 +            super(m);
5606 +            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);
5614 +            this.transformer = transformer;
5615 +            this.reducer = reducer;
5616 +        }
5617 +        public final void compute() {
5618 +            MapReduceKeysTask<K,V,U> t = this;
5619 +            final Fun<? super K, ? extends U> transformer =
5620 +                this.transformer;
5621 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5622 +                this.reducer;
5623 +            if (transformer == null || reducer == null)
5624 +                throw new Error(NullFunctionMessage);
5625 +            int b = batch();
5626 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5627 +                b >>>= 1;
5628 +                t.pending = 1;
5629 +                MapReduceKeysTask<K,V,U> rt =
5630 +                    new MapReduceKeysTask<K,V,U>
5631 +                    (t, b, true, transformer, reducer);
5632 +                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)
5653 +                        r = (r == null) ? u : reducer.apply(r, u);
5654 +                    (t = p).result = r;
5655 +                }
5656 +                else if (p.casPending(c, 0))
5657 +                    break;
5658 +            }
5659 +        }
5660 +        public final U getRawResult() { return result; }
5661 +    }
5662 +
5663 +    static final class MapReduceValuesTask<K,V,U>
5664 +        extends BulkTask<K,V,U> {
5665 +        final Fun<? super V, ? extends U> transformer;
5666 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5667 +        U result;
5668 +        MapReduceValuesTask<K,V,U> sibling;
5669 +        MapReduceValuesTask
5670 +            (ConcurrentHashMapV8<K,V> m,
5671 +             Fun<? super V, ? extends U> transformer,
5672 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5673 +            super(m);
5674 +            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);
5682 +            this.transformer = transformer;
5683 +            this.reducer = reducer;
5684 +        }
5685 +        public final void compute() {
5686 +            MapReduceValuesTask<K,V,U> t = this;
5687 +            final Fun<? super V, ? extends U> transformer =
5688 +                this.transformer;
5689 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5690 +                this.reducer;
5691 +            if (transformer == null || reducer == null)
5692 +                throw new Error(NullFunctionMessage);
5693 +            int b = batch();
5694 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5695 +                b >>>= 1;
5696 +                t.pending = 1;
5697 +                MapReduceValuesTask<K,V,U> rt =
5698 +                    new MapReduceValuesTask<K,V,U>
5699 +                    (t, b, true, transformer, reducer);
5700 +                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)
5722 +                        r = (r == null) ? u : reducer.apply(r, u);
5723 +                    (t = p).result = r;
5724 +                }
5725 +                else if (p.casPending(c, 0))
5726 +                    break;
5727 +            }
5728 +        }
5729 +        public final U getRawResult() { return result; }
5730 +    }
5731 +
5732 +    static final class MapReduceEntriesTask<K,V,U>
5733 +        extends BulkTask<K,V,U> {
5734 +        final Fun<Map.Entry<K,V>, ? extends U> transformer;
5735 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5736 +        U result;
5737 +        MapReduceEntriesTask<K,V,U> sibling;
5738 +        MapReduceEntriesTask
5739 +            (ConcurrentHashMapV8<K,V> m,
5740 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
5741 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5742 +            super(m);
5743 +            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);
5751 +            this.transformer = transformer;
5752 +            this.reducer = reducer;
5753 +        }
5754 +        public final void compute() {
5755 +            MapReduceEntriesTask<K,V,U> t = this;
5756 +            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5757 +                this.transformer;
5758 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5759 +                this.reducer;
5760 +            if (transformer == null || reducer == null)
5761 +                throw new Error(NullFunctionMessage);
5762 +            int b = batch();
5763 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5764 +                b >>>= 1;
5765 +                t.pending = 1;
5766 +                MapReduceEntriesTask<K,V,U> rt =
5767 +                    new MapReduceEntriesTask<K,V,U>
5768 +                    (t, b, true, transformer, reducer);
5769 +                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)
5791 +                        r = (r == null) ? u : reducer.apply(r, u);
5792 +                    (t = p).result = r;
5793 +                }
5794 +                else if (p.casPending(c, 0))
5795 +                    break;
5796 +            }
5797 +        }
5798 +        public final U getRawResult() { return result; }
5799 +    }
5800 +
5801 +    static final class MapReduceMappingsTask<K,V,U>
5802 +        extends BulkTask<K,V,U> {
5803 +        final BiFun<? super K, ? super V, ? extends U> transformer;
5804 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5805 +        U result;
5806 +        MapReduceMappingsTask<K,V,U> sibling;
5807 +        MapReduceMappingsTask
5808 +            (ConcurrentHashMapV8<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 +        }
5815 +        MapReduceMappingsTask
5816 +            (BulkTask<K,V,?> p, int b, boolean split,
5817 +             BiFun<? super K, ? super V, ? extends U> transformer,
5818 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5819 +            super(p, b, split);
5820 +            this.transformer = transformer;
5821 +            this.reducer = reducer;
5822 +        }
5823 +        public final void compute() {
5824 +            MapReduceMappingsTask<K,V,U> t = this;
5825 +            final BiFun<? super K, ? super V, ? extends U> transformer =
5826 +                this.transformer;
5827 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5828 +                this.reducer;
5829 +            if (transformer == null || reducer == null)
5830 +                throw new Error(NullFunctionMessage);
5831 +            int b = batch();
5832 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5833 +                b >>>= 1;
5834 +                t.pending = 1;
5835 +                MapReduceMappingsTask<K,V,U> rt =
5836 +                    new MapReduceMappingsTask<K,V,U>
5837 +                    (t, b, true, transformer, reducer);
5838 +                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)
5859 +                        r = (r == null) ? u : reducer.apply(r, u);
5860 +                    (t = p).result = r;
5861 +                }
5862 +                else if (p.casPending(c, 0))
5863 +                    break;
5864 +            }
5865 +        }
5866 +        public final U getRawResult() { return result; }
5867 +    }
5868 +
5869 +    static final class MapReduceKeysToDoubleTask<K,V>
5870 +        extends BulkTask<K,V,Double> {
5871 +        final ObjectToDouble<? super K> transformer;
5872 +        final DoubleByDoubleToDouble reducer;
5873 +        final double basis;
5874 +        double result;
5875 +        MapReduceKeysToDoubleTask<K,V> sibling;
5876 +        MapReduceKeysToDoubleTask
5877 +            (ConcurrentHashMapV8<K,V> m,
5878 +             ObjectToDouble<? super K> transformer,
5879 +             double basis,
5880 +             DoubleByDoubleToDouble reducer) {
5881 +            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);
5891 +            this.transformer = transformer;
5892 +            this.basis = basis; this.reducer = reducer;
5893 +        }
5894 +        public final void compute() {
5895 +            MapReduceKeysToDoubleTask<K,V> t = this;
5896 +            final ObjectToDouble<? super K> 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 +                MapReduceKeysToDoubleTask<K,V> rt =
5907 +                    new MapReduceKeysToDoubleTask<K,V>
5908 +                    (t, b, true, transformer, id, reducer);
5909 +                t = new MapReduceKeysToDoubleTask<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 +            while (t.advance() != null)
5917 +                r = reducer.apply(r, transformer.apply((K)t.nextKey));
5918 +            t.result = r;
5919 +            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;
5930 +                }
5931 +                else if (p.casPending(c, 0))
5932 +                    break;
5933 +            }
5934 +        }
5935 +        public final Double getRawResult() { return result; }
5936 +    }
5937 +
5938 +    static final class MapReduceValuesToDoubleTask<K,V>
5939 +        extends BulkTask<K,V,Double> {
5940 +        final ObjectToDouble<? super V> transformer;
5941 +        final DoubleByDoubleToDouble reducer;
5942 +        final double basis;
5943 +        double result;
5944 +        MapReduceValuesToDoubleTask<K,V> sibling;
5945 +        MapReduceValuesToDoubleTask
5946 +            (ConcurrentHashMapV8<K,V> m,
5947 +             ObjectToDouble<? super V> transformer,
5948 +             double basis,
5949 +             DoubleByDoubleToDouble reducer) {
5950 +            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);
5960 +            this.transformer = transformer;
5961 +            this.basis = basis; this.reducer = reducer;
5962 +        }
5963 +        public final void compute() {
5964 +            MapReduceValuesToDoubleTask<K,V> t = this;
5965 +            final ObjectToDouble<? super V> transformer =
5966 +                this.transformer;
5967 +            final DoubleByDoubleToDouble reducer = this.reducer;
5968 +            if (transformer == null || reducer == null)
5969 +                throw new Error(NullFunctionMessage);
5970 +            final double id = this.basis;
5971 +            int b = batch();
5972 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5973 +                b >>>= 1;
5974 +                t.pending = 1;
5975 +                MapReduceValuesToDoubleTask<K,V> rt =
5976 +                    new MapReduceValuesToDoubleTask<K,V>
5977 +                    (t, b, true, transformer, id, reducer);
5978 +                t = new MapReduceValuesToDoubleTask<K,V>
5979 +                    (t, b, false, transformer, id, reducer);
5980 +                t.sibling = rt;
5981 +                rt.sibling = t;
5982 +                rt.fork();
5983 +            }
5984 +            double r = id;
5985 +            Object v;
5986 +            while ((v = t.advance()) != null)
5987 +                r = reducer.apply(r, transformer.apply((V)v));
5988 +            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;
6000 +                }
6001 +                else if (p.casPending(c, 0))
6002 +                    break;
6003 +            }
6004 +        }
6005 +        public final Double getRawResult() { return result; }
6006 +    }
6007 +
6008 +    static final class MapReduceEntriesToDoubleTask<K,V>
6009 +        extends BulkTask<K,V,Double> {
6010 +        final ObjectToDouble<Map.Entry<K,V>> transformer;
6011 +        final DoubleByDoubleToDouble reducer;
6012 +        final double basis;
6013 +        double result;
6014 +        MapReduceEntriesToDoubleTask<K,V> sibling;
6015 +        MapReduceEntriesToDoubleTask
6016 +            (ConcurrentHashMapV8<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 +        }
6024 +        MapReduceEntriesToDoubleTask
6025 +            (BulkTask<K,V,?> p, int b, boolean split,
6026 +             ObjectToDouble<Map.Entry<K,V>> transformer,
6027 +             double basis,
6028 +             DoubleByDoubleToDouble reducer) {
6029 +            super(p, b, split);
6030 +            this.transformer = transformer;
6031 +            this.basis = basis; this.reducer = reducer;
6032 +        }
6033 +        public final void compute() {
6034 +            MapReduceEntriesToDoubleTask<K,V> t = this;
6035 +            final ObjectToDouble<Map.Entry<K,V>> transformer =
6036 +                this.transformer;
6037 +            final DoubleByDoubleToDouble reducer = this.reducer;
6038 +            if (transformer == null || reducer == null)
6039 +                throw new Error(NullFunctionMessage);
6040 +            final double id = this.basis;
6041 +            int b = batch();
6042 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6043 +                b >>>= 1;
6044 +                t.pending = 1;
6045 +                MapReduceEntriesToDoubleTask<K,V> rt =
6046 +                    new MapReduceEntriesToDoubleTask<K,V>
6047 +                    (t, b, true, transformer, id, reducer);
6048 +                t = new MapReduceEntriesToDoubleTask<K,V>
6049 +                    (t, b, false, transformer, id, reducer);
6050 +                t.sibling = rt;
6051 +                rt.sibling = t;
6052 +                rt.fork();
6053 +            }
6054 +            double r = id;
6055 +            Object v;
6056 +            while ((v = t.advance()) != null)
6057 +                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6058 +            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;
6070 +                }
6071 +                else if (p.casPending(c, 0))
6072 +                    break;
6073 +            }
6074 +        }
6075 +        public final Double getRawResult() { return result; }
6076 +    }
6077 +
6078 +    static final class MapReduceMappingsToDoubleTask<K,V>
6079 +        extends BulkTask<K,V,Double> {
6080 +        final ObjectByObjectToDouble<? super K, ? super V> transformer;
6081 +        final DoubleByDoubleToDouble reducer;
6082 +        final double basis;
6083 +        double result;
6084 +        MapReduceMappingsToDoubleTask<K,V> sibling;
6085 +        MapReduceMappingsToDoubleTask
6086 +            (ConcurrentHashMapV8<K,V> m,
6087 +             ObjectByObjectToDouble<? super K, ? super V> transformer,
6088 +             double basis,
6089 +             DoubleByDoubleToDouble reducer) {
6090 +            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);
6100 +            this.transformer = transformer;
6101 +            this.basis = basis; this.reducer = reducer;
6102 +        }
6103 +        public final void compute() {
6104 +            MapReduceMappingsToDoubleTask<K,V> t = this;
6105 +            final ObjectByObjectToDouble<? super K, ? super V> transformer =
6106 +                this.transformer;
6107 +            final DoubleByDoubleToDouble reducer = this.reducer;
6108 +            if (transformer == null || reducer == null)
6109 +                throw new Error(NullFunctionMessage);
6110 +            final double id = this.basis;
6111 +            int b = batch();
6112 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6113 +                b >>>= 1;
6114 +                t.pending = 1;
6115 +                MapReduceMappingsToDoubleTask<K,V> rt =
6116 +                    new MapReduceMappingsToDoubleTask<K,V>
6117 +                    (t, b, true, transformer, id, reducer);
6118 +                t = new MapReduceMappingsToDoubleTask<K,V>
6119 +                    (t, b, false, transformer, id, reducer);
6120 +                t.sibling = rt;
6121 +                rt.sibling = t;
6122 +                rt.fork();
6123 +            }
6124 +            double r = id;
6125 +            Object v;
6126 +            while ((v = t.advance()) != null)
6127 +                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6128 +            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;
6140 +                }
6141 +                else if (p.casPending(c, 0))
6142 +                    break;
6143 +            }
6144 +        }
6145 +        public final Double getRawResult() { return result; }
6146 +    }
6147 +
6148 +    static final class MapReduceKeysToLongTask<K,V>
6149 +        extends BulkTask<K,V,Long> {
6150 +        final ObjectToLong<? super K> transformer;
6151 +        final LongByLongToLong reducer;
6152 +        final long basis;
6153 +        long result;
6154 +        MapReduceKeysToLongTask<K,V> sibling;
6155 +        MapReduceKeysToLongTask
6156 +            (ConcurrentHashMapV8<K,V> m,
6157 +             ObjectToLong<? super K> transformer,
6158 +             long basis,
6159 +             LongByLongToLong reducer) {
6160 +            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);
6170 +            this.transformer = transformer;
6171 +            this.basis = basis; this.reducer = reducer;
6172 +        }
6173 +        public final void compute() {
6174 +            MapReduceKeysToLongTask<K,V> t = this;
6175 +            final ObjectToLong<? super K> transformer =
6176 +                this.transformer;
6177 +            final LongByLongToLong reducer = this.reducer;
6178 +            if (transformer == null || reducer == null)
6179 +                throw new Error(NullFunctionMessage);
6180 +            final long id = this.basis;
6181 +            int b = batch();
6182 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6183 +                b >>>= 1;
6184 +                t.pending = 1;
6185 +                MapReduceKeysToLongTask<K,V> rt =
6186 +                    new MapReduceKeysToLongTask<K,V>
6187 +                    (t, b, true, transformer, id, reducer);
6188 +                t = new MapReduceKeysToLongTask<K,V>
6189 +                    (t, b, false, transformer, id, reducer);
6190 +                t.sibling = rt;
6191 +                rt.sibling = t;
6192 +                rt.fork();
6193 +            }
6194 +            long r = id;
6195 +            while (t.advance() != null)
6196 +                r = reducer.apply(r, transformer.apply((K)t.nextKey));
6197 +            t.result = r;
6198 +            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;
6209 +                }
6210 +                else if (p.casPending(c, 0))
6211 +                    break;
6212 +            }
6213 +        }
6214 +        public final Long getRawResult() { return result; }
6215 +    }
6216 +
6217 +    static final class MapReduceValuesToLongTask<K,V>
6218 +        extends BulkTask<K,V,Long> {
6219 +        final ObjectToLong<? super V> transformer;
6220 +        final LongByLongToLong reducer;
6221 +        final long basis;
6222 +        long result;
6223 +        MapReduceValuesToLongTask<K,V> sibling;
6224 +        MapReduceValuesToLongTask
6225 +            (ConcurrentHashMapV8<K,V> m,
6226 +             ObjectToLong<? super V> transformer,
6227 +             long basis,
6228 +             LongByLongToLong reducer) {
6229 +            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);
6239 +            this.transformer = transformer;
6240 +            this.basis = basis; this.reducer = reducer;
6241 +        }
6242 +        public final void compute() {
6243 +            MapReduceValuesToLongTask<K,V> t = this;
6244 +            final ObjectToLong<? super V> transformer =
6245 +                this.transformer;
6246 +            final LongByLongToLong reducer = this.reducer;
6247 +            if (transformer == null || reducer == null)
6248 +                throw new Error(NullFunctionMessage);
6249 +            final long id = this.basis;
6250 +            int b = batch();
6251 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6252 +                b >>>= 1;
6253 +                t.pending = 1;
6254 +                MapReduceValuesToLongTask<K,V> rt =
6255 +                    new MapReduceValuesToLongTask<K,V>
6256 +                    (t, b, true, transformer, id, reducer);
6257 +                t = new MapReduceValuesToLongTask<K,V>
6258 +                    (t, b, false, transformer, id, reducer);
6259 +                t.sibling = rt;
6260 +                rt.sibling = t;
6261 +                rt.fork();
6262 +            }
6263 +            long r = id;
6264 +            Object v;
6265 +            while ((v = t.advance()) != null)
6266 +                r = reducer.apply(r, transformer.apply((V)v));
6267 +            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;
6279 +                }
6280 +                else if (p.casPending(c, 0))
6281 +                    break;
6282 +            }
6283 +        }
6284 +        public final Long getRawResult() { return result; }
6285 +    }
6286 +
6287 +    static final class MapReduceEntriesToLongTask<K,V>
6288 +        extends BulkTask<K,V,Long> {
6289 +        final ObjectToLong<Map.Entry<K,V>> transformer;
6290 +        final LongByLongToLong reducer;
6291 +        final long basis;
6292 +        long result;
6293 +        MapReduceEntriesToLongTask<K,V> sibling;
6294 +        MapReduceEntriesToLongTask
6295 +            (ConcurrentHashMapV8<K,V> m,
6296 +             ObjectToLong<Map.Entry<K,V>> transformer,
6297 +             long basis,
6298 +             LongByLongToLong reducer) {
6299 +            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);
6309 +            this.transformer = transformer;
6310 +            this.basis = basis; this.reducer = reducer;
6311 +        }
6312 +        public final void compute() {
6313 +            MapReduceEntriesToLongTask<K,V> t = this;
6314 +            final ObjectToLong<Map.Entry<K,V>> transformer =
6315 +                this.transformer;
6316 +            final LongByLongToLong reducer = this.reducer;
6317 +            if (transformer == null || reducer == null)
6318 +                throw new Error(NullFunctionMessage);
6319 +            final long id = this.basis;
6320 +            int b = batch();
6321 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6322 +                b >>>= 1;
6323 +                t.pending = 1;
6324 +                MapReduceEntriesToLongTask<K,V> rt =
6325 +                    new MapReduceEntriesToLongTask<K,V>
6326 +                    (t, b, true, transformer, id, reducer);
6327 +                t = new MapReduceEntriesToLongTask<K,V>
6328 +                    (t, b, false, transformer, id, reducer);
6329 +                t.sibling = rt;
6330 +                rt.sibling = t;
6331 +                rt.fork();
6332 +            }
6333 +            long r = id;
6334 +            Object v;
6335 +            while ((v = t.advance()) != null)
6336 +                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6337 +            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;
6349 +                }
6350 +                else if (p.casPending(c, 0))
6351 +                    break;
6352 +            }
6353 +        }
6354 +        public final Long getRawResult() { return result; }
6355 +    }
6356 +
6357 +    static final class MapReduceMappingsToLongTask<K,V>
6358 +        extends BulkTask<K,V,Long> {
6359 +        final ObjectByObjectToLong<? super K, ? super V> transformer;
6360 +        final LongByLongToLong reducer;
6361 +        final long basis;
6362 +        long result;
6363 +        MapReduceMappingsToLongTask<K,V> sibling;
6364 +        MapReduceMappingsToLongTask
6365 +            (ConcurrentHashMapV8<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 +        }
6373 +        MapReduceMappingsToLongTask
6374 +            (BulkTask<K,V,?> p, int b, boolean split,
6375 +             ObjectByObjectToLong<? super K, ? super V> transformer,
6376 +             long basis,
6377 +             LongByLongToLong reducer) {
6378 +            super(p, b, split);
6379 +            this.transformer = transformer;
6380 +            this.basis = basis; this.reducer = reducer;
6381 +        }
6382 +        public final void compute() {
6383 +            MapReduceMappingsToLongTask<K,V> t = this;
6384 +            final ObjectByObjectToLong<? super K, ? super V> transformer =
6385 +                this.transformer;
6386 +            final LongByLongToLong reducer = this.reducer;
6387 +            if (transformer == null || reducer == null)
6388 +                throw new Error(NullFunctionMessage);
6389 +            final long id = this.basis;
6390 +            int b = batch();
6391 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6392 +                b >>>= 1;
6393 +                t.pending = 1;
6394 +                MapReduceMappingsToLongTask<K,V> rt =
6395 +                    new MapReduceMappingsToLongTask<K,V>
6396 +                    (t, b, true, transformer, id, reducer);
6397 +                t = new MapReduceMappingsToLongTask<K,V>
6398 +                    (t, b, false, transformer, id, reducer);
6399 +                t.sibling = rt;
6400 +                rt.sibling = t;
6401 +                rt.fork();
6402 +            }
6403 +            long r = id;
6404 +            Object v;
6405 +            while ((v = t.advance()) != null)
6406 +                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6407 +            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;
6419 +                }
6420 +                else if (p.casPending(c, 0))
6421 +                    break;
6422 +            }
6423 +        }
6424 +        public final Long getRawResult() { return result; }
6425 +    }
6426 +
6427 +    static final class MapReduceKeysToIntTask<K,V>
6428 +        extends BulkTask<K,V,Integer> {
6429 +        final ObjectToInt<? super K> transformer;
6430 +        final IntByIntToInt reducer;
6431 +        final int basis;
6432 +        int result;
6433 +        MapReduceKeysToIntTask<K,V> sibling;
6434 +        MapReduceKeysToIntTask
6435 +            (ConcurrentHashMapV8<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 +        }
6443 +        MapReduceKeysToIntTask
6444 +            (BulkTask<K,V,?> p, int b, boolean split,
6445 +             ObjectToInt<? super K> transformer,
6446 +             int basis,
6447 +             IntByIntToInt reducer) {
6448 +            super(p, b, split);
6449 +            this.transformer = transformer;
6450 +            this.basis = basis; this.reducer = reducer;
6451 +        }
6452 +        public final void compute() {
6453 +            MapReduceKeysToIntTask<K,V> t = this;
6454 +            final ObjectToInt<? super K> transformer =
6455 +                this.transformer;
6456 +            final IntByIntToInt reducer = this.reducer;
6457 +            if (transformer == null || reducer == null)
6458 +                throw new Error(NullFunctionMessage);
6459 +            final int id = this.basis;
6460 +            int b = batch();
6461 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6462 +                b >>>= 1;
6463 +                t.pending = 1;
6464 +                MapReduceKeysToIntTask<K,V> rt =
6465 +                    new MapReduceKeysToIntTask<K,V>
6466 +                    (t, b, true, transformer, id, reducer);
6467 +                t = new MapReduceKeysToIntTask<K,V>
6468 +                    (t, b, false, transformer, id, reducer);
6469 +                t.sibling = rt;
6470 +                rt.sibling = t;
6471 +                rt.fork();
6472 +            }
6473 +            int r = id;
6474 +            while (t.advance() != null)
6475 +                r = reducer.apply(r, transformer.apply((K)t.nextKey));
6476 +            t.result = r;
6477 +            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;
6488 +                }
6489 +                else if (p.casPending(c, 0))
6490 +                    break;
6491 +            }
6492 +        }
6493 +        public final Integer getRawResult() { return result; }
6494 +    }
6495 +
6496 +    static final class MapReduceValuesToIntTask<K,V>
6497 +        extends BulkTask<K,V,Integer> {
6498 +        final ObjectToInt<? super V> transformer;
6499 +        final IntByIntToInt reducer;
6500 +        final int basis;
6501 +        int result;
6502 +        MapReduceValuesToIntTask<K,V> sibling;
6503 +        MapReduceValuesToIntTask
6504 +            (ConcurrentHashMapV8<K,V> m,
6505 +             ObjectToInt<? super V> transformer,
6506 +             int basis,
6507 +             IntByIntToInt reducer) {
6508 +            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);
6518 +            this.transformer = transformer;
6519 +            this.basis = basis; this.reducer = reducer;
6520 +        }
6521 +        public final void compute() {
6522 +            MapReduceValuesToIntTask<K,V> t = this;
6523 +            final ObjectToInt<? super V> transformer =
6524 +                this.transformer;
6525 +            final IntByIntToInt reducer = this.reducer;
6526 +            if (transformer == null || reducer == null)
6527 +                throw new Error(NullFunctionMessage);
6528 +            final int id = this.basis;
6529 +            int b = batch();
6530 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6531 +                b >>>= 1;
6532 +                t.pending = 1;
6533 +                MapReduceValuesToIntTask<K,V> rt =
6534 +                    new MapReduceValuesToIntTask<K,V>
6535 +                    (t, b, true, transformer, id, reducer);
6536 +                t = new MapReduceValuesToIntTask<K,V>
6537 +                    (t, b, false, transformer, id, reducer);
6538 +                t.sibling = rt;
6539 +                rt.sibling = t;
6540 +                rt.fork();
6541 +            }
6542 +            int r = id;
6543 +            Object v;
6544 +            while ((v = t.advance()) != null)
6545 +                r = reducer.apply(r, transformer.apply((V)v));
6546 +            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;
6558 +                }
6559 +                else if (p.casPending(c, 0))
6560 +                    break;
6561 +            }
6562 +        }
6563 +        public final Integer getRawResult() { return result; }
6564 +    }
6565 +
6566 +    static final class MapReduceEntriesToIntTask<K,V>
6567 +        extends BulkTask<K,V,Integer> {
6568 +        final ObjectToInt<Map.Entry<K,V>> transformer;
6569 +        final IntByIntToInt reducer;
6570 +        final int basis;
6571 +        int result;
6572 +        MapReduceEntriesToIntTask<K,V> sibling;
6573 +        MapReduceEntriesToIntTask
6574 +            (ConcurrentHashMapV8<K,V> m,
6575 +             ObjectToInt<Map.Entry<K,V>> transformer,
6576 +             int basis,
6577 +             IntByIntToInt reducer) {
6578 +            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);
6588 +            this.transformer = transformer;
6589 +            this.basis = basis; this.reducer = reducer;
6590 +        }
6591 +        public final void compute() {
6592 +            MapReduceEntriesToIntTask<K,V> t = this;
6593 +            final ObjectToInt<Map.Entry<K,V>> transformer =
6594 +                this.transformer;
6595 +            final IntByIntToInt reducer = this.reducer;
6596 +            if (transformer == null || reducer == null)
6597 +                throw new Error(NullFunctionMessage);
6598 +            final int id = this.basis;
6599 +            int b = batch();
6600 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6601 +                b >>>= 1;
6602 +                t.pending = 1;
6603 +                MapReduceEntriesToIntTask<K,V> rt =
6604 +                    new MapReduceEntriesToIntTask<K,V>
6605 +                    (t, b, true, transformer, id, reducer);
6606 +                t = new MapReduceEntriesToIntTask<K,V>
6607 +                    (t, b, false, transformer, id, reducer);
6608 +                t.sibling = rt;
6609 +                rt.sibling = t;
6610 +                rt.fork();
6611 +            }
6612 +            int r = id;
6613 +            Object v;
6614 +            while ((v = t.advance()) != null)
6615 +                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6616 +            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;
6628 +                }
6629 +                else if (p.casPending(c, 0))
6630 +                    break;
6631 +            }
6632 +        }
6633 +        public final Integer getRawResult() { return result; }
6634 +    }
6635 +
6636 +    static final class MapReduceMappingsToIntTask<K,V>
6637 +        extends BulkTask<K,V,Integer> {
6638 +        final ObjectByObjectToInt<? super K, ? super V> transformer;
6639 +        final IntByIntToInt reducer;
6640 +        final int basis;
6641 +        int result;
6642 +        MapReduceMappingsToIntTask<K,V> sibling;
6643 +        MapReduceMappingsToIntTask
6644 +            (ConcurrentHashMapV8<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 +        }
6652 +        MapReduceMappingsToIntTask
6653 +            (BulkTask<K,V,?> p, int b, boolean split,
6654 +             ObjectByObjectToInt<? super K, ? super V> transformer,
6655 +             int basis,
6656 +             IntByIntToInt reducer) {
6657 +            super(p, b, split);
6658 +            this.transformer = transformer;
6659 +            this.basis = basis; this.reducer = reducer;
6660 +        }
6661 +        public final void compute() {
6662 +            MapReduceMappingsToIntTask<K,V> t = this;
6663 +            final ObjectByObjectToInt<? super K, ? super V> transformer =
6664 +                this.transformer;
6665 +            final IntByIntToInt reducer = this.reducer;
6666 +            if (transformer == null || reducer == null)
6667 +                throw new Error(NullFunctionMessage);
6668 +            final int id = this.basis;
6669 +            int b = batch();
6670 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6671 +                b >>>= 1;
6672 +                t.pending = 1;
6673 +                MapReduceMappingsToIntTask<K,V> rt =
6674 +                    new MapReduceMappingsToIntTask<K,V>
6675 +                    (t, b, true, transformer, id, reducer);
6676 +                t = new MapReduceMappingsToIntTask<K,V>
6677 +                    (t, b, false, transformer, id, reducer);
6678 +                t.sibling = rt;
6679 +                rt.sibling = t;
6680 +                rt.fork();
6681 +            }
6682 +            int r = id;
6683 +            Object v;
6684 +            while ((v = t.advance()) != null)
6685 +                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6686 +            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;
6698 +                }
6699 +                else if (p.casPending(c, 0))
6700 +                    break;
6701 +            }
6702 +        }
6703 +        public final Integer getRawResult() { return result; }
6704 +    }
6705 +
6706 +
6707      // Unsafe mechanics
6708      private static final sun.misc.Unsafe UNSAFE;
6709      private static final long counterOffset;
6710 <    private static final long resizingOffset;
6710 >    private static final long sizeCtlOffset;
6711      private static final long ABASE;
6712      private static final int ASHIFT;
6713  
# Line 1592 | Line 6718 | public class ConcurrentHashMapV8<K, V>
6718              Class<?> k = ConcurrentHashMapV8.class;
6719              counterOffset = UNSAFE.objectFieldOffset
6720                  (k.getDeclaredField("counter"));
6721 <            resizingOffset = UNSAFE.objectFieldOffset
6722 <                (k.getDeclaredField("resizing"));
6721 >            sizeCtlOffset = UNSAFE.objectFieldOffset
6722 >                (k.getDeclaredField("sizeCtl"));
6723              Class<?> sc = Node[].class;
6724              ABASE = UNSAFE.arrayBaseOffset(sc);
6725              ss = UNSAFE.arrayIndexScale(sc);
# Line 1632 | Line 6758 | public class ConcurrentHashMapV8<K, V>
6758              }
6759          }
6760      }
1635
6761   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines