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.3 by jsr166, Tue Aug 30 07:18:46 2011 UTC vs.
Revision 1.71 by dl, Tue Oct 30 14:23:03 2012 UTC

# Line 5 | Line 5
5   */
6  
7   package jsr166e;
8 < import jsr166e.LongAdder;
8 >
9 > import java.util.Comparator;
10 > import java.util.Arrays;
11   import java.util.Map;
12   import java.util.Set;
13   import java.util.Collection;
# Line 19 | Line 21 | import java.util.Enumeration;
21   import java.util.ConcurrentModificationException;
22   import java.util.NoSuchElementException;
23   import java.util.concurrent.ConcurrentMap;
24 + import java.util.concurrent.ThreadLocalRandom;
25 + import java.util.concurrent.locks.LockSupport;
26 + import java.util.concurrent.locks.AbstractQueuedSynchronizer;
27 + import java.util.concurrent.atomic.AtomicReference;
28 +
29   import java.io.Serializable;
30  
31   /**
# Line 37 | Line 44 | import java.io.Serializable;
44   * block, so may overlap with update operations (including {@code put}
45   * and {@code remove}). Retrievals reflect the results of the most
46   * recently <em>completed</em> update operations holding upon their
47 < * onset.  For aggregate operations such as {@code putAll} and {@code
48 < * clear}, concurrent retrievals may reflect insertion or removal of
49 < * only some entries.  Similarly, Iterators and Enumerations return
50 < * elements reflecting the state of the hash table at some point at or
51 < * since the creation of the iterator/enumeration.  They do
52 < * <em>not</em> throw {@link ConcurrentModificationException}.
53 < * However, iterators are designed to be used by only one thread at a
54 < * time.  Bear in mind that the results of aggregate status methods
55 < * including {@code size}, {@code isEmpty}, and {@code containsValue}
56 < * are typically useful only when a map is not undergoing concurrent
57 < * updates in other threads.  Otherwise the results of these methods
58 < * reflect transient states that may be adequate for monitoring
59 < * purposes, but not for program control.
60 < *
61 < * <p> Resizing this or any other kind of hash table is a relatively
62 < * slow operation, so, when possible, it is a good idea to provide
63 < * estimates of expected table sizes in constructors. Also, for
64 < * compatability with previous versions of this class, constructors
65 < * may optionally specify an expected {@code concurrencyLevel} as an
66 < * additional hint for internal sizing.
47 > * onset. (More formally, an update operation for a given key bears a
48 > * <em>happens-before</em> relation with any (non-null) retrieval for
49 > * that key reporting the updated value.)  For aggregate operations
50 > * such as {@code putAll} and {@code clear}, concurrent retrievals may
51 > * reflect insertion or removal of only some entries.  Similarly,
52 > * Iterators and Enumerations return elements reflecting the state of
53 > * the hash table at some point at or since the creation of the
54 > * iterator/enumeration.  They do <em>not</em> throw {@link
55 > * ConcurrentModificationException}.  However, iterators are designed
56 > * to be used by only one thread at a time.  Bear in mind that the
57 > * results of aggregate status methods including {@code size}, {@code
58 > * isEmpty}, and {@code containsValue} are typically useful only when
59 > * a map is not undergoing concurrent updates in other threads.
60 > * Otherwise the results of these methods reflect transient states
61 > * that may be adequate for monitoring or estimation purposes, but not
62 > * for program control.
63 > *
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> A {@link Set} projection of a ConcurrentHashMapV8 may be created
86 > * (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed
87 > * (using {@link #keySet(Object)} when only keys are of interest, and the
88 > * mapped values are (perhaps transiently) not used or all take the
89 > * same mapping value.
90 > *
91 > * <p> A ConcurrentHashMapV8 can be used as scalable frequency map (a
92 > * form of histogram or multiset) by using {@link LongAdder} values
93 > * and initializing via {@link #computeIfAbsent}. For example, to add
94 > * a count to a {@code ConcurrentHashMapV8<String,LongAdder> freqs}, you
95 > * can use {@code freqs.computeIfAbsent(k -> new
96 > * LongAdder()).increment();}
97   *
98   * <p>This class and its views and iterators implement all of the
99   * <em>optional</em> methods of the {@link Map} and {@link Iterator}
# Line 65 | Line 102 | import java.io.Serializable;
102   * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
103   * does <em>not</em> allow {@code null} to be used as a key or value.
104   *
105 + * <p>ConcurrentHashMapV8s support parallel operations using the {@link
106 + * ForkJoinPool#commonPool}. (Task that may be used in other contexts
107 + * are available in class {@link ForkJoinTasks}). These operations are
108 + * designed to be safely, and often sensibly, applied even with maps
109 + * that are being concurrently updated by other threads; for example,
110 + * when computing a snapshot summary of the values in a shared
111 + * registry.  There are three kinds of operation, each with four
112 + * forms, accepting functions with Keys, Values, Entries, and (Key,
113 + * Value) arguments and/or return values. Because the elements of a
114 + * ConcurrentHashMapV8 are not ordered in any particular way, and may be
115 + * processed in different orders in different parallel executions, the
116 + * correctness of supplied functions should not depend on any
117 + * ordering, or on any other objects or values that may transiently
118 + * change while computation is in progress; and except for forEach
119 + * actions, should ideally be side-effect-free.
120 + *
121 + * <ul>
122 + * <li> forEach: Perform a given action on each element.
123 + * A variant form applies a given transformation on each element
124 + * before performing the action.</li>
125 + *
126 + * <li> search: Return the first available non-null result of
127 + * applying a given function on each element; skipping further
128 + * search when a result is found.</li>
129 + *
130 + * <li> reduce: Accumulate each element.  The supplied reduction
131 + * function cannot rely on ordering (more formally, it should be
132 + * both associative and commutative).  There are five variants:
133 + *
134 + * <ul>
135 + *
136 + * <li> Plain reductions. (There is not a form of this method for
137 + * (key, value) function arguments since there is no corresponding
138 + * return type.)</li>
139 + *
140 + * <li> Mapped reductions that accumulate the results of a given
141 + * function applied to each element.</li>
142 + *
143 + * <li> Reductions to scalar doubles, longs, and ints, using a
144 + * given basis value.</li>
145 + *
146 + * </li>
147 + * </ul>
148 + * </ul>
149 + *
150 + * <p>The concurrency properties of bulk operations follow
151 + * from those of ConcurrentHashMapV8: Any non-null result returned
152 + * from {@code get(key)} and related access methods bears a
153 + * happens-before relation with the associated insertion or
154 + * update.  The result of any bulk operation reflects the
155 + * composition of these per-element relations (but is not
156 + * necessarily atomic with respect to the map as a whole unless it
157 + * is somehow known to be quiescent).  Conversely, because keys
158 + * and values in the map are never null, null serves as a reliable
159 + * atomic indicator of the current lack of any result.  To
160 + * maintain this property, null serves as an implicit basis for
161 + * all non-scalar reduction operations. For the double, long, and
162 + * int versions, the basis should be one that, when combined with
163 + * any other value, returns that other value (more formally, it
164 + * should be the identity element for the reduction). Most common
165 + * reductions have these properties; for example, computing a sum
166 + * with basis 0 or a minimum with basis MAX_VALUE.
167 + *
168 + * <p>Search and transformation functions provided as arguments
169 + * should similarly return null to indicate the lack of any result
170 + * (in which case it is not used). In the case of mapped
171 + * reductions, this also enables transformations to serve as
172 + * filters, returning null (or, in the case of primitive
173 + * specializations, the identity basis) if the element should not
174 + * be combined. You can create compound transformations and
175 + * filterings by composing them yourself under this "null means
176 + * there is nothing there now" rule before using them in search or
177 + * reduce operations.
178 + *
179 + * <p>Methods accepting and/or returning Entry arguments maintain
180 + * key-value associations. They may be useful for example when
181 + * finding the key for the greatest value. Note that "plain" Entry
182 + * arguments can be supplied using {@code new
183 + * AbstractMap.SimpleEntry(k,v)}.
184 + *
185 + * <p> Bulk operations may complete abruptly, throwing an
186 + * exception encountered in the application of a supplied
187 + * function. Bear in mind when handling such exceptions that other
188 + * concurrently executing functions could also have thrown
189 + * exceptions, or would have done so if the first exception had
190 + * not occurred.
191 + *
192 + * <p>Parallel speedups for bulk operations compared to sequential
193 + * processing are common but not guaranteed.  Operations involving
194 + * brief functions on small maps may execute more slowly than
195 + * sequential loops if the underlying work to parallelize the
196 + * computation is more expensive than the computation
197 + * itself. Similarly, parallelization may not lead to much actual
198 + * parallelism if all processors are busy performing unrelated tasks.
199 + *
200 + * <p> All arguments to all task methods must be non-null.
201 + *
202 + * <p><em>jsr166e note: During transition, this class
203 + * uses nested functional interfaces with different names but the
204 + * same forms as those expected for JDK8.<em>
205 + *
206   * <p>This class is a member of the
207   * <a href="{@docRoot}/../technotes/guides/collections/index.html">
208   * Java Collections Framework</a>.
209   *
72 * <p><em>jsr166e note: This class is a candidate replacement for
73 * java.util.concurrent.ConcurrentHashMap.<em>
74 *
210   * @since 1.5
211   * @author Doug Lea
212   * @param <K> the type of keys maintained by this map
213   * @param <V> the type of mapped values
214   */
215   public class ConcurrentHashMapV8<K, V>
216 <        implements ConcurrentMap<K, V>, Serializable {
216 >    implements ConcurrentMap<K, V>, Serializable {
217      private static final long serialVersionUID = 7249069246763182397L;
218  
219      /**
220 <     * A function computing a mapping from the given key to a value,
221 <     *  or <code>null</code> if there is no mapping. This is a
222 <     * place-holder for an upcoming JDK8 interface.
223 <     */
224 <    public static interface MappingFunction<K, V> {
225 <        /**
226 <         * Returns a value for the given key, or null if there is no
227 <         * mapping. If this function throws an (unchecked) exception,
228 <         * the exception is rethrown to its caller, and no mapping is
229 <         * recorded.  Because this function is invoked within
230 <         * atomicity control, the computation should be short and
231 <         * simple. The most common usage is to construct a new object
232 <         * serving as an initial mapped value.
220 >     * A partitionable iterator. A Spliterator can be traversed
221 >     * directly, but can also be partitioned (before traversal) by
222 >     * creating another Spliterator that covers a non-overlapping
223 >     * portion of the elements, and so may be amenable to parallel
224 >     * execution.
225 >     *
226 >     * <p> This interface exports a subset of expected JDK8
227 >     * functionality.
228 >     *
229 >     * <p>Sample usage: Here is one (of the several) ways to compute
230 >     * the sum of the values held in a map using the ForkJoin
231 >     * framework. As illustrated here, Spliterators are well suited to
232 >     * designs in which a task repeatedly splits off half its work
233 >     * into forked subtasks until small enough to process directly,
234 >     * and then joins these subtasks. Variants of this style can also
235 >     * be used in completion-based designs.
236 >     *
237 >     * <pre>
238 >     * {@code ConcurrentHashMapV8<String, Long> m = ...
239 >     * // split as if have 8 * parallelism, for load balance
240 >     * int n = m.size();
241 >     * int p = aForkJoinPool.getParallelism() * 8;
242 >     * int split = (n < p)? n : p;
243 >     * long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null));
244 >     * // ...
245 >     * static class SumValues extends RecursiveTask<Long> {
246 >     *   final Spliterator<Long> s;
247 >     *   final int split;             // split while > 1
248 >     *   final SumValues nextJoin;    // records forked subtasks to join
249 >     *   SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
250 >     *     this.s = s; this.depth = depth; this.nextJoin = nextJoin;
251 >     *   }
252 >     *   public Long compute() {
253 >     *     long sum = 0;
254 >     *     SumValues subtasks = null; // fork subtasks
255 >     *     for (int s = split >>> 1; s > 0; s >>>= 1)
256 >     *       (subtasks = new SumValues(s.split(), s, subtasks)).fork();
257 >     *     while (s.hasNext())        // directly process remaining elements
258 >     *       sum += s.next();
259 >     *     for (SumValues t = subtasks; t != null; t = t.nextJoin)
260 >     *       sum += t.join();         // collect subtask results
261 >     *     return sum;
262 >     *   }
263 >     * }
264 >     * }</pre>
265 >     */
266 >    public static interface Spliterator<T> extends Iterator<T> {
267 >        /**
268 >         * Returns a Spliterator covering approximately half of the
269 >         * elements, guaranteed not to overlap with those subsequently
270 >         * returned by this Spliterator.  After invoking this method,
271 >         * the current Spliterator will <em>not</em> produce any of
272 >         * the elements of the returned Spliterator, but the two
273 >         * Spliterators together will produce all of the elements that
274 >         * would have been produced by this Spliterator had this
275 >         * method not been called. The exact number of elements
276 >         * produced by the returned Spliterator is not guaranteed, and
277 >         * may be zero (i.e., with {@code hasNext()} reporting {@code
278 >         * false}) if this Spliterator cannot be further split.
279 >         *
280 >         * @return a Spliterator covering approximately half of the
281 >         * elements
282 >         * @throws IllegalStateException if this Spliterator has
283 >         * already commenced traversing elements
284 >         */
285 >        Spliterator<T> split();
286 >    }
287 >
288 >    /**
289 >     * A view of a ConcurrentHashMapV8 as a {@link Set} of keys, in
290 >     * which additions may optionally be enabled by mapping to a
291 >     * common value.  This class cannot be directly instantiated. See
292 >     * {@link #keySet}, {@link #keySet(Object)}, {@link #newKeySet()},
293 >     * {@link #newKeySet(int)}.
294 >     *
295 >     * <p>The view's {@code iterator} is a "weakly consistent" iterator
296 >     * that will never throw {@link ConcurrentModificationException},
297 >     * and guarantees to traverse elements as they existed upon
298 >     * construction of the iterator, and may (but is not guaranteed to)
299 >     * reflect any modifications subsequent to construction.
300 >     */
301 >    public static class KeySetView<K,V> extends CHMView<K,V> implements Set<K>, java.io.Serializable {
302 >        private static final long serialVersionUID = 7249069246763182397L;
303 >        private final V value;
304 >        KeySetView(ConcurrentHashMapV8<K, V> map, V value) {  // non-public
305 >            super(map);
306 >            this.value = value;
307 >        }
308 >
309 >        /**
310 >         * Returns the map backing this view.
311 >         *
312 >         * @return the map backing this view
313 >         */
314 >        public ConcurrentHashMapV8<K,V> getMap() { return map; }
315 >
316 >        /**
317 >         * Returns the default mapped value for additions,
318 >         * or {@code null} if additions are not supported.
319           *
320 <         * @param key the (non-null) key
321 <         * @return a value, or null if none
320 >         * @return the default mapped value for additions, or {@code null}
321 >         * if not supported.
322           */
323 <        V map(K key);
323 >        public V getMappedValue() { return value; }
324 >
325 >        // implement Set API
326 >
327 >        public boolean contains(Object o) { return map.containsKey(o); }
328 >        public boolean remove(Object o)   { return map.remove(o) != null; }
329 >        public Iterator<K> iterator()     { return new KeyIterator<K,V>(map); }
330 >        public boolean add(K e) {
331 >            V v;
332 >            if ((v = value) == null)
333 >                throw new UnsupportedOperationException();
334 >            if (e == null)
335 >                throw new NullPointerException();
336 >            return map.internalPutIfAbsent(e, v) == null;
337 >        }
338 >        public boolean addAll(Collection<? extends K> c) {
339 >            boolean added = false;
340 >            V v;
341 >            if ((v = value) == null)
342 >                throw new UnsupportedOperationException();
343 >            for (K e : c) {
344 >                if (e == null)
345 >                    throw new NullPointerException();
346 >                if (map.internalPutIfAbsent(e, v) == null)
347 >                    added = true;
348 >            }
349 >            return added;
350 >        }
351 >        public boolean equals(Object o) {
352 >            Set<?> c;
353 >            return ((o instanceof Set) &&
354 >                    ((c = (Set<?>)o) == this ||
355 >                     (containsAll(c) && c.containsAll(this))));
356 >        }
357      }
358  
359      /*
# Line 108 | Line 362 | public class ConcurrentHashMapV8<K, V>
362       * The primary design goal of this hash table is to maintain
363       * concurrent readability (typically method get(), but also
364       * iterators and related methods) while minimizing update
365 <     * contention.
365 >     * contention. Secondary goals are to keep space consumption about
366 >     * the same or better than java.util.HashMap, and to support high
367 >     * initial insertion rates on an empty table by many threads.
368       *
369       * Each key-value mapping is held in a Node.  Because Node fields
370       * can contain special values, they are defined using plain Object
371       * types. Similarly in turn, all internal methods that use them
372 <     * work off Object types. All public generic-typed methods relay
373 <     * in/out of these internal methods, supplying casts as needed.
372 >     * work off Object types. And similarly, so do the internal
373 >     * methods of auxiliary iterator and view classes.  All public
374 >     * generic typed methods relay in/out of these internal methods,
375 >     * supplying null-checks and casts as needed. This also allows
376 >     * many of the public methods to be factored into a smaller number
377 >     * of internal methods (although sadly not so for the five
378 >     * variants of put-related operations). The validation-based
379 >     * approach explained below leads to a lot of code sprawl because
380 >     * retry-control precludes factoring into smaller methods.
381       *
382       * The table is lazily initialized to a power-of-two size upon the
383 <     * first insertion.  Each bin in the table contains a (typically
384 <     * short) list of Nodes.  Table accesses require volatile/atomic
385 <     * reads, writes, and CASes.  Because there is no other way to
386 <     * arrange this without adding further indirections, we use
387 <     * intrinsics (sun.misc.Unsafe) operations.  The lists of nodes
388 <     * within bins are always accurately traversable under volatile
389 <     * reads, so long as lookups check hash code and non-nullness of
390 <     * key and value before checking key equality. (All valid hash
391 <     * codes are nonnegative. Negative values are reserved for special
392 <     * forwarding nodes; see below.)
393 <     *
394 <     * A bin may be locked during update (insert, delete, and replace)
395 <     * operations.  We do not want to waste the space required to
396 <     * associate a distinct lock object with each bin, so instead use
397 <     * the first node of a bin list itself as a lock, using builtin
398 <     * "synchronized" locks. These save space and we can live with
399 <     * only plain block-structured lock/unlock operations. Using the
400 <     * first node of a list as a lock does not by itself suffice
401 <     * though: When a node is locked, any update must first validate
402 <     * that it is still the first node, and retry if not. (Because new
403 <     * nodes are always appended to lists, once a node is first in a
404 <     * bin, it remains first until deleted or the bin becomes
405 <     * invalidated.)  However, update operations can and usually do
406 <     * still traverse the bin until the point of update, which helps
407 <     * reduce cache misses on retries.  This is a converse of sorts to
408 <     * the lazy locking technique described by Herlihy & Shavit. If
409 <     * there is no existing node during a put operation, then one can
410 <     * be CAS'ed in (without need for lock except in computeIfAbsent);
411 <     * the CAS serves as validation. This is on average the most
412 <     * common case for put operations. The expected number of locks
413 <     * covering different elements (i.e., bins with 2 or more nodes)
414 <     * is approximately 10% at steady state under default settings.
415 <     * Lock contention probability for two threads accessing arbitrary
416 <     * distinct elements is thus less than 1% even for small tables.
417 <     *
418 <     * The table is resized when occupancy exceeds a threshold.  Only
419 <     * a single thread performs the resize (using field "resizing", to
420 <     * arrange exclusion), but the table otherwise remains usable for
421 <     * both reads and updates. Resizing proceeds by transferring bins,
422 <     * one by one, from the table to the next table.  Upon transfer,
423 <     * the old table bin contains only a special forwarding node (with
424 <     * negative hash code ("MOVED")) that contains the next table as
383 >     * first insertion.  Each bin in the table normally contains a
384 >     * list of Nodes (most often, the list has only zero or one Node).
385 >     * Table accesses require volatile/atomic reads, writes, and
386 >     * CASes.  Because there is no other way to arrange this without
387 >     * adding further indirections, we use intrinsics
388 >     * (sun.misc.Unsafe) operations.  The lists of nodes within bins
389 >     * are always accurately traversable under volatile reads, so long
390 >     * as lookups check hash code and non-nullness of value before
391 >     * checking key equality.
392 >     *
393 >     * We use the top two bits of Node hash fields for control
394 >     * purposes -- they are available anyway because of addressing
395 >     * constraints.  As explained further below, these top bits are
396 >     * used as follows:
397 >     *  00 - Normal
398 >     *  01 - Locked
399 >     *  11 - Locked and may have a thread waiting for lock
400 >     *  10 - Node is a forwarding node
401 >     *
402 >     * The lower 30 bits of each Node's hash field contain a
403 >     * transformation of the key's hash code, except for forwarding
404 >     * nodes, for which the lower bits are zero (and so always have
405 >     * hash field == MOVED).
406 >     *
407 >     * Insertion (via put or its variants) of the first node in an
408 >     * empty bin is performed by just CASing it to the bin.  This is
409 >     * by far the most common case for put operations under most
410 >     * key/hash distributions.  Other update operations (insert,
411 >     * delete, and replace) require locks.  We do not want to waste
412 >     * the space required to associate a distinct lock object with
413 >     * each bin, so instead use the first node of a bin list itself as
414 >     * a lock. Blocking support for these locks relies on the builtin
415 >     * "synchronized" monitors.  However, we also need a tryLock
416 >     * construction, so we overlay these by using bits of the Node
417 >     * hash field for lock control (see above), and so normally use
418 >     * builtin monitors only for blocking and signalling using
419 >     * wait/notifyAll constructions. See Node.tryAwaitLock.
420 >     *
421 >     * Using the first node of a list as a lock does not by itself
422 >     * suffice though: When a node is locked, any update must first
423 >     * validate that it is still the first node after locking it, and
424 >     * retry if not. Because new nodes are always appended to lists,
425 >     * once a node is first in a bin, it remains first until deleted
426 >     * or the bin becomes invalidated (upon resizing).  However,
427 >     * operations that only conditionally update may inspect nodes
428 >     * until the point of update. This is a converse of sorts to the
429 >     * lazy locking technique described by Herlihy & Shavit.
430 >     *
431 >     * The main disadvantage of per-bin locks is that other update
432 >     * operations on other nodes in a bin list protected by the same
433 >     * lock can stall, for example when user equals() or mapping
434 >     * functions take a long time.  However, statistically, under
435 >     * random hash codes, this is not a common problem.  Ideally, the
436 >     * frequency of nodes in bins follows a Poisson distribution
437 >     * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
438 >     * parameter of about 0.5 on average, given the resizing threshold
439 >     * of 0.75, although with a large variance because of resizing
440 >     * granularity. Ignoring variance, the expected occurrences of
441 >     * list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The
442 >     * first values are:
443 >     *
444 >     * 0:    0.60653066
445 >     * 1:    0.30326533
446 >     * 2:    0.07581633
447 >     * 3:    0.01263606
448 >     * 4:    0.00157952
449 >     * 5:    0.00015795
450 >     * 6:    0.00001316
451 >     * 7:    0.00000094
452 >     * 8:    0.00000006
453 >     * more: less than 1 in ten million
454 >     *
455 >     * Lock contention probability for two threads accessing distinct
456 >     * elements is roughly 1 / (8 * #elements) under random hashes.
457 >     *
458 >     * Actual hash code distributions encountered in practice
459 >     * sometimes deviate significantly from uniform randomness.  This
460 >     * includes the case when N > (1<<30), so some keys MUST collide.
461 >     * Similarly for dumb or hostile usages in which multiple keys are
462 >     * designed to have identical hash codes. Also, although we guard
463 >     * against the worst effects of this (see method spread), sets of
464 >     * hashes may differ only in bits that do not impact their bin
465 >     * index for a given power-of-two mask.  So we use a secondary
466 >     * strategy that applies when the number of nodes in a bin exceeds
467 >     * a threshold, and at least one of the keys implements
468 >     * Comparable.  These TreeBins use a balanced tree to hold nodes
469 >     * (a specialized form of red-black trees), bounding search time
470 >     * to O(log N).  Each search step in a TreeBin is around twice as
471 >     * slow as in a regular list, but given that N cannot exceed
472 >     * (1<<64) (before running out of addresses) this bounds search
473 >     * steps, lock hold times, etc, to reasonable constants (roughly
474 >     * 100 nodes inspected per operation worst case) so long as keys
475 >     * are Comparable (which is very common -- String, Long, etc).
476 >     * TreeBin nodes (TreeNodes) also maintain the same "next"
477 >     * traversal pointers as regular nodes, so can be traversed in
478 >     * iterators in the same way.
479 >     *
480 >     * The table is resized when occupancy exceeds a percentage
481 >     * threshold (nominally, 0.75, but see below).  Only a single
482 >     * thread performs the resize (using field "sizeCtl", to arrange
483 >     * exclusion), but the table otherwise remains usable for reads
484 >     * and updates. Resizing proceeds by transferring bins, one by
485 >     * one, from the table to the next table.  Because we are using
486 >     * power-of-two expansion, the elements from each bin must either
487 >     * stay at same index, or move with a power of two offset. We
488 >     * eliminate unnecessary node creation by catching cases where old
489 >     * nodes can be reused because their next fields won't change.  On
490 >     * average, only about one-sixth of them need cloning when a table
491 >     * doubles. The nodes they replace will be garbage collectable as
492 >     * soon as they are no longer referenced by any reader thread that
493 >     * may be in the midst of concurrently traversing table.  Upon
494 >     * transfer, the old table bin contains only a special forwarding
495 >     * node (with hash field "MOVED") that contains the next table as
496       * its key. On encountering a forwarding node, access and update
497 <     * operations restart, using the new table. To ensure concurrent
498 <     * readability of traversals, transfers must proceed from the last
499 <     * bin (table.length - 1) up towards the first.  Any traversal
500 <     * starting from the first bin can then arrange to move to the new
501 <     * table for the rest of the traversal without revisiting nodes.
502 <     * This constrains bin transfers to a particular order, and so can
503 <     * block indefinitely waiting for the next lock, and other threads
504 <     * cannot help with the transfer. However, expected stalls are
505 <     * infrequent enough to not warrant the additional overhead and
506 <     * complexity of access and iteration schemes that could admit
507 <     * out-of-order or concurrent bin transfers.
508 <     *
509 <     * A similar traversal scheme (not yet implemented) can apply to
510 <     * partial traversals during partitioned aggregate operations.
511 <     * Also, read-only operations give up if ever forwarded to a null
512 <     * table, which provides support for shutdown-style clearing,
513 <     * which is also not currently implemented.
497 >     * operations restart, using the new table.
498 >     *
499 >     * Each bin transfer requires its bin lock. However, unlike other
500 >     * cases, a transfer can skip a bin if it fails to acquire its
501 >     * lock, and revisit it later (unless it is a TreeBin). Method
502 >     * rebuild maintains a buffer of TRANSFER_BUFFER_SIZE bins that
503 >     * have been skipped because of failure to acquire a lock, and
504 >     * blocks only if none are available (i.e., only very rarely).
505 >     * The transfer operation must also ensure that all accessible
506 >     * bins in both the old and new table are usable by any traversal.
507 >     * When there are no lock acquisition failures, this is arranged
508 >     * simply by proceeding from the last bin (table.length - 1) up
509 >     * towards the first.  Upon seeing a forwarding node, traversals
510 >     * (see class Iter) arrange to move to the new table
511 >     * without revisiting nodes.  However, when any node is skipped
512 >     * during a transfer, all earlier table bins may have become
513 >     * visible, so are initialized with a reverse-forwarding node back
514 >     * to the old table until the new ones are established. (This
515 >     * sometimes requires transiently locking a forwarding node, which
516 >     * is possible under the above encoding.) These more expensive
517 >     * mechanics trigger only when necessary.
518 >     *
519 >     * The traversal scheme also applies to partial traversals of
520 >     * ranges of bins (via an alternate Traverser constructor)
521 >     * to support partitioned aggregate operations.  Also, read-only
522 >     * operations give up if ever forwarded to a null table, which
523 >     * provides support for shutdown-style clearing, which is also not
524 >     * currently implemented.
525 >     *
526 >     * Lazy table initialization minimizes footprint until first use,
527 >     * and also avoids resizings when the first operation is from a
528 >     * putAll, constructor with map argument, or deserialization.
529 >     * These cases attempt to override the initial capacity settings,
530 >     * but harmlessly fail to take effect in cases of races.
531       *
532       * The element count is maintained using a LongAdder, which avoids
533       * contention on updates but can encounter cache thrashing if read
534 <     * too frequently during concurrent updates. To avoid reading so
535 <     * often, resizing is normally attempted only upon adding to a bin
536 <     * already holding two or more nodes. Under the default threshold
537 <     * (0.75), and uniform hash distributions, the probability of this
538 <     * occurring at threshold is around 13%, meaning that only about 1
539 <     * in 8 puts check threshold (and after resizing, many fewer do
540 <     * so). But this approximation has high variance for small table
541 <     * sizes, so we check on any collision for sizes <= 64.  Further,
542 <     * to increase the probablity that a resize occurs soon enough, we
543 <     * offset the threshold (see THRESHOLD_OFFSET) by the expected
544 <     * number of puts between checks. This is currently set to 8, in
545 <     * accord with the default load factor. In practice, this is
546 <     * rarely overridden, and in any case is close enough to other
547 <     * plausible values not to waste dynamic probablity computation
548 <     * for more precision.
534 >     * too frequently during concurrent access. To avoid reading so
535 >     * often, resizing is attempted either when a bin lock is
536 >     * contended, or upon adding to a bin already holding two or more
537 >     * nodes (checked before adding in the xIfAbsent methods, after
538 >     * adding in others). Under uniform hash distributions, the
539 >     * probability of this occurring at threshold is around 13%,
540 >     * meaning that only about 1 in 8 puts check threshold (and after
541 >     * resizing, many fewer do so). But this approximation has high
542 >     * variance for small table sizes, so we check on any collision
543 >     * for sizes <= 64. The bulk putAll operation further reduces
544 >     * contention by only committing count updates upon these size
545 >     * checks.
546 >     *
547 >     * Maintaining API and serialization compatibility with previous
548 >     * versions of this class introduces several oddities. Mainly: We
549 >     * leave untouched but unused constructor arguments refering to
550 >     * concurrencyLevel. We accept a loadFactor constructor argument,
551 >     * but apply it only to initial table capacity (which is the only
552 >     * time that we can guarantee to honor it.) We also declare an
553 >     * unused "Segment" class that is instantiated in minimal form
554 >     * only when serializing.
555       */
556  
557      /* ---------------- Constants -------------- */
558  
559      /**
560 <     * The smallest allowed table capacity.  Must be a power of 2, at
561 <     * least 2.
560 >     * The largest possible table capacity.  This value must be
561 >     * exactly 1<<30 to stay within Java array allocation and indexing
562 >     * bounds for power of two table sizes, and is further required
563 >     * because the top two bits of 32bit hash fields are used for
564 >     * control purposes.
565       */
566 <    static final int MINIMUM_CAPACITY = 2;
566 >    private static final int MAXIMUM_CAPACITY = 1 << 30;
567  
568      /**
569 <     * The largest allowed table capacity.  Must be a power of 2, at
570 <     * most 1<<30.
569 >     * The default initial table capacity.  Must be a power of 2
570 >     * (i.e., at least 1) and at most MAXIMUM_CAPACITY.
571       */
572 <    static final int MAXIMUM_CAPACITY = 1 << 30;
572 >    private static final int DEFAULT_CAPACITY = 16;
573  
574      /**
575 <     * The default initial table capacity.  Must be a power of 2, at
576 <     * least MINIMUM_CAPACITY and at most MAXIMUM_CAPACITY
575 >     * The largest possible (non-power of two) array size.
576 >     * Needed by toArray and related methods.
577       */
578 <    static final int DEFAULT_CAPACITY = 16;
578 >    static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
579  
580      /**
581 <     * The default load factor for this table, used when not otherwise
582 <     * specified in a constructor.
581 >     * The default concurrency level for this table. Unused but
582 >     * defined for compatibility with previous versions of this class.
583       */
584 <    static final float DEFAULT_LOAD_FACTOR = 0.75f;
584 >    private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
585  
586      /**
587 <     * The default concurrency level for this table. Unused, but
588 <     * defined for compatibility with previous versions of this class.
587 >     * The load factor for this table. Overrides of this value in
588 >     * constructors affect only the initial table capacity.  The
589 >     * actual floating point value isn't normally used -- it is
590 >     * simpler to use expressions such as {@code n - (n >>> 2)} for
591 >     * the associated resizing threshold.
592       */
593 <    static final int DEFAULT_CONCURRENCY_LEVEL = 16;
593 >    private static final float LOAD_FACTOR = 0.75f;
594  
595      /**
596 <     * The count value to offset thesholds to compensate for checking
597 <     * for resizing only when inserting into bins with two or more
598 <     * elements. See above for explanation.
596 >     * The buffer size for skipped bins during transfers. The
597 >     * value is arbitrary but should be large enough to avoid
598 >     * most locking stalls during resizes.
599       */
600 <    static final int THRESHOLD_OFFSET = 8;
600 >    private static final int TRANSFER_BUFFER_SIZE = 32;
601  
602      /**
603 <     * Special node hash value indicating to use table in node.key
604 <     * Must be negative.
603 >     * The bin count threshold for using a tree rather than list for a
604 >     * bin.  The value reflects the approximate break-even point for
605 >     * using tree-based operations.
606       */
607 <    static final int MOVED = -1;
607 >    private static final int TREE_THRESHOLD = 8;
608 >
609 >    /*
610 >     * Encodings for special uses of Node hash fields. See above for
611 >     * explanation.
612 >     */
613 >    static final int MOVED     = 0x80000000; // hash field for forwarding nodes
614 >    static final int LOCKED    = 0x40000000; // set/tested only as a bit
615 >    static final int WAITING   = 0xc0000000; // both bits set/tested together
616 >    static final int HASH_BITS = 0x3fffffff; // usable bits of normal node hash
617  
618      /* ---------------- Fields -------------- */
619  
620      /**
621       * The array of bins. Lazily initialized upon first insertion.
622 <     * Size is always a power of two. Accessed directly by inner
250 <     * classes.
622 >     * Size is always a power of two. Accessed directly by iterators.
623       */
624      transient volatile Node[] table;
625  
626 <    /** The counter maintaining number of elements. */
626 >    /**
627 >     * The counter maintaining number of elements.
628 >     */
629      private transient final LongAdder counter;
630 <    /** Nonzero when table is being initialized or resized. Updated via CAS. */
631 <    private transient volatile int resizing;
632 <    /** The target load factor for the table. */
633 <    private transient float loadFactor;
634 <    /** The next element count value upon which to resize the table. */
635 <    private transient int threshold;
636 <    /** The initial capacity of the table. */
637 <    private transient int initCap;
630 >
631 >    /**
632 >     * Table initialization and resizing control.  When negative, the
633 >     * table is being initialized or resized. Otherwise, when table is
634 >     * null, holds the initial table size to use upon creation, or 0
635 >     * for default. After initialization, holds the next element count
636 >     * value upon which to resize the table.
637 >     */
638 >    private transient volatile int sizeCtl;
639  
640      // views
641 <    transient Set<K> keySet;
642 <    transient Set<Map.Entry<K,V>> entrySet;
643 <    transient Collection<V> values;
269 <
270 <    /** For serialization compatability. Null unless serialized; see below */
271 <    Segment<K,V>[] segments;
641 >    private transient KeySetView<K,V> keySet;
642 >    private transient Values<K,V> values;
643 >    private transient EntrySet<K,V> entrySet;
644  
645 <    /**
646 <     * Applies a supplemental hash function to a given hashCode, which
647 <     * defends against poor quality hash functions.  The result must
648 <     * be non-negative, and for reasonable performance must have good
649 <     * avalanche properties; i.e., that each bit of the argument
650 <     * affects each bit (except sign bit) of the result.
645 >    /** For serialization compatibility. Null unless serialized; see below */
646 >    private Segment<K,V>[] segments;
647 >
648 >    /* ---------------- Table element access -------------- */
649 >
650 >    /*
651 >     * Volatile access methods are used for table elements as well as
652 >     * elements of in-progress next table while resizing.  Uses are
653 >     * null checked by callers, and implicitly bounds-checked, relying
654 >     * on the invariants that tab arrays have non-zero size, and all
655 >     * indices are masked with (tab.length - 1) which is never
656 >     * negative and always less than length. Note that, to be correct
657 >     * wrt arbitrary concurrency errors by users, bounds checks must
658 >     * operate on local variables, which accounts for some odd-looking
659 >     * inline assignments below.
660       */
661 <    private static final int spread(int h) {
662 <        // Apply base step of MurmurHash; see http://code.google.com/p/smhasher/
663 <        h ^= h >>> 16;
664 <        h *= 0x85ebca6b;
665 <        h ^= h >>> 13;
666 <        h *= 0xc2b2ae35;
667 <        return (h >>> 16) ^ (h & 0x7fffffff); // mask out sign bit
661 >
662 >    static final Node tabAt(Node[] tab, int i) { // used by Iter
663 >        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
664 >    }
665 >
666 >    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
667 >        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
668      }
669  
670 +    private static final void setTabAt(Node[] tab, int i, Node v) {
671 +        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
672 +    }
673 +
674 +    /* ---------------- Nodes -------------- */
675 +
676      /**
677       * Key-value entry. Note that this is never exported out as a
678 <     * user-visible Map.Entry.
678 >     * user-visible Map.Entry (see MapEntry below). Nodes with a hash
679 >     * field of MOVED are special, and do not contain user keys or
680 >     * values.  Otherwise, keys are never null, and null val fields
681 >     * indicate that a node is in the process of being deleted or
682 >     * created. For purposes of read-only access, a key may be read
683 >     * before a val, but can only be used after checking val to be
684 >     * non-null.
685       */
686 <    static final class Node {
687 <        final int hash;
686 >    static class Node {
687 >        volatile int hash;
688          final Object key;
689          volatile Object val;
690          volatile Node next;
# Line 302 | Line 695 | public class ConcurrentHashMapV8<K, V>
695              this.val = val;
696              this.next = next;
697          }
305    }
698  
699 <    /*
700 <     * Volatile access nethods are used for table elements as well as
701 <     * elements of in-progress next table while resizing.  Uses in
702 <     * access and update methods are null checked by callers, and
311 <     * implicitly bounds-checked, relying on the invariants that tab
312 <     * arrays have non-zero size, and all indices are masked with
313 <     * (tab.length - 1) which is never negative and always less than
314 <     * length. The "relaxed" non-volatile forms are used only during
315 <     * table initialization. The only other usage is in
316 <     * HashIterator.advance, which performs explicit checks.
317 <     */
699 >        /** CompareAndSet the hash field */
700 >        final boolean casHash(int cmp, int val) {
701 >            return UNSAFE.compareAndSwapInt(this, hashOffset, cmp, val);
702 >        }
703  
704 <    static final Node tabAt(Node[] tab, int i) { // used in HashIterator
705 <        return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
706 <    }
704 >        /** The number of spins before blocking for a lock */
705 >        static final int MAX_SPINS =
706 >            Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
707  
708 <    private static final boolean casTabAt(Node[] tab, int i, Node c, Node v) {
709 <        return UNSAFE.compareAndSwapObject(tab, ((long)i<<ASHIFT)+ABASE, c, v);
710 <    }
708 >        /**
709 >         * Spins a while if LOCKED bit set and this node is the first
710 >         * of its bin, and then sets WAITING bits on hash field and
711 >         * blocks (once) if they are still set.  It is OK for this
712 >         * method to return even if lock is not available upon exit,
713 >         * which enables these simple single-wait mechanics.
714 >         *
715 >         * The corresponding signalling operation is performed within
716 >         * callers: Upon detecting that WAITING has been set when
717 >         * unlocking lock (via a failed CAS from non-waiting LOCKED
718 >         * state), unlockers acquire the sync lock and perform a
719 >         * notifyAll.
720 >         *
721 >         * The initial sanity check on tab and bounds is not currently
722 >         * necessary in the only usages of this method, but enables
723 >         * use in other future contexts.
724 >         */
725 >        final void tryAwaitLock(Node[] tab, int i) {
726 >            if (tab != null && i >= 0 && i < tab.length) { // sanity check
727 >                int r = ThreadLocalRandom.current().nextInt(); // randomize spins
728 >                int spins = MAX_SPINS, h;
729 >                while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
730 >                    if (spins >= 0) {
731 >                        r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
732 >                        if (r >= 0 && --spins == 0)
733 >                            Thread.yield();  // yield before block
734 >                    }
735 >                    else if (casHash(h, h | WAITING)) {
736 >                        synchronized (this) {
737 >                            if (tabAt(tab, i) == this &&
738 >                                (hash & WAITING) == WAITING) {
739 >                                try {
740 >                                    wait();
741 >                                } catch (InterruptedException ie) {
742 >                                    Thread.currentThread().interrupt();
743 >                                }
744 >                            }
745 >                            else
746 >                                notifyAll(); // possibly won race vs signaller
747 >                        }
748 >                        break;
749 >                    }
750 >                }
751 >            }
752 >        }
753  
754 <    private static final void setTabAt(Node[] tab, int i, Node v) {
755 <        UNSAFE.putObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE, v);
756 <    }
754 >        // Unsafe mechanics for casHash
755 >        private static final sun.misc.Unsafe UNSAFE;
756 >        private static final long hashOffset;
757  
758 <    private static final Node relaxedTabAt(Node[] tab, int i) {
759 <        return (Node)UNSAFE.getObject(tab, ((long)i<<ASHIFT)+ABASE);
758 >        static {
759 >            try {
760 >                UNSAFE = getUnsafe();
761 >                Class<?> k = Node.class;
762 >                hashOffset = UNSAFE.objectFieldOffset
763 >                    (k.getDeclaredField("hash"));
764 >            } catch (Exception e) {
765 >                throw new Error(e);
766 >            }
767 >        }
768      }
769  
770 <    private static final void relaxedSetTabAt(Node[] tab, int i, Node v) {
771 <        UNSAFE.putObject(tab, ((long)i<<ASHIFT)+ABASE, v);
770 >    /* ---------------- TreeBins -------------- */
771 >
772 >    /**
773 >     * Nodes for use in TreeBins
774 >     */
775 >    static final class TreeNode extends Node {
776 >        TreeNode parent;  // red-black tree links
777 >        TreeNode left;
778 >        TreeNode right;
779 >        TreeNode prev;    // needed to unlink next upon deletion
780 >        boolean red;
781 >
782 >        TreeNode(int hash, Object key, Object val, Node next, TreeNode parent) {
783 >            super(hash, key, val, next);
784 >            this.parent = parent;
785 >        }
786      }
787  
788 <    /* ---------------- Access and update operations -------------- */
788 >    /**
789 >     * A specialized form of red-black tree for use in bins
790 >     * whose size exceeds a threshold.
791 >     *
792 >     * TreeBins use a special form of comparison for search and
793 >     * related operations (which is the main reason we cannot use
794 >     * existing collections such as TreeMaps). TreeBins contain
795 >     * Comparable elements, but may contain others, as well as
796 >     * elements that are Comparable but not necessarily Comparable<T>
797 >     * for the same T, so we cannot invoke compareTo among them. To
798 >     * handle this, the tree is ordered primarily by hash value, then
799 >     * by getClass().getName() order, and then by Comparator order
800 >     * among elements of the same class.  On lookup at a node, if
801 >     * elements are not comparable or compare as 0, both left and
802 >     * right children may need to be searched in the case of tied hash
803 >     * values. (This corresponds to the full list search that would be
804 >     * necessary if all elements were non-Comparable and had tied
805 >     * hashes.)  The red-black balancing code is updated from
806 >     * pre-jdk-collections
807 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
808 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
809 >     * Algorithms" (CLR).
810 >     *
811 >     * TreeBins also maintain a separate locking discipline than
812 >     * regular bins. Because they are forwarded via special MOVED
813 >     * nodes at bin heads (which can never change once established),
814 >     * we cannot use those nodes as locks. Instead, TreeBin
815 >     * extends AbstractQueuedSynchronizer to support a simple form of
816 >     * read-write lock. For update operations and table validation,
817 >     * the exclusive form of lock behaves in the same way as bin-head
818 >     * locks. However, lookups use shared read-lock mechanics to allow
819 >     * multiple readers in the absence of writers.  Additionally,
820 >     * these lookups do not ever block: While the lock is not
821 >     * available, they proceed along the slow traversal path (via
822 >     * next-pointers) until the lock becomes available or the list is
823 >     * exhausted, whichever comes first. (These cases are not fast,
824 >     * but maximize aggregate expected throughput.)  The AQS mechanics
825 >     * for doing this are straightforward.  The lock state is held as
826 >     * AQS getState().  Read counts are negative; the write count (1)
827 >     * is positive.  There are no signalling preferences among readers
828 >     * and writers. Since we don't need to export full Lock API, we
829 >     * just override the minimal AQS methods and use them directly.
830 >     */
831 >    static final class TreeBin extends AbstractQueuedSynchronizer {
832 >        private static final long serialVersionUID = 2249069246763182397L;
833 >        transient TreeNode root;  // root of tree
834 >        transient TreeNode first; // head of next-pointer list
835  
836 <    /** Implementation for get and containsKey **/
837 <   private final Object internalGet(Object k) {
838 <        int h = spread(k.hashCode());
839 <        Node[] tab = table;
840 <        retry: while (tab != null) {
841 <            Node e = tabAt(tab, (tab.length - 1) & h);
842 <            while (e != null) {
843 <                int eh = e.hash;
844 <                if (eh == h) {
845 <                    Object ek = e.key, ev = e.val;
846 <                    if (ev != null && ek != null && (k == ek || k.equals(ek)))
847 <                        return ev;
848 <                }
849 <                else if (eh < 0) { // bin was moved during resize
850 <                    tab = (Node[])e.key;
851 <                    continue retry;
836 >        /* AQS overrides */
837 >        public final boolean isHeldExclusively() { return getState() > 0; }
838 >        public final boolean tryAcquire(int ignore) {
839 >            if (compareAndSetState(0, 1)) {
840 >                setExclusiveOwnerThread(Thread.currentThread());
841 >                return true;
842 >            }
843 >            return false;
844 >        }
845 >        public final boolean tryRelease(int ignore) {
846 >            setExclusiveOwnerThread(null);
847 >            setState(0);
848 >            return true;
849 >        }
850 >        public final int tryAcquireShared(int ignore) {
851 >            for (int c;;) {
852 >                if ((c = getState()) > 0)
853 >                    return -1;
854 >                if (compareAndSetState(c, c -1))
855 >                    return 1;
856 >            }
857 >        }
858 >        public final boolean tryReleaseShared(int ignore) {
859 >            int c;
860 >            do {} while (!compareAndSetState(c = getState(), c + 1));
861 >            return c == -1;
862 >        }
863 >
864 >        /** From CLR */
865 >        private void rotateLeft(TreeNode p) {
866 >            if (p != null) {
867 >                TreeNode r = p.right, pp, rl;
868 >                if ((rl = p.right = r.left) != null)
869 >                    rl.parent = p;
870 >                if ((pp = r.parent = p.parent) == null)
871 >                    root = r;
872 >                else if (pp.left == p)
873 >                    pp.left = r;
874 >                else
875 >                    pp.right = r;
876 >                r.left = p;
877 >                p.parent = r;
878 >            }
879 >        }
880 >
881 >        /** From CLR */
882 >        private void rotateRight(TreeNode p) {
883 >            if (p != null) {
884 >                TreeNode l = p.left, pp, lr;
885 >                if ((lr = p.left = l.right) != null)
886 >                    lr.parent = p;
887 >                if ((pp = l.parent = p.parent) == null)
888 >                    root = l;
889 >                else if (pp.right == p)
890 >                    pp.right = l;
891 >                else
892 >                    pp.left = l;
893 >                l.right = p;
894 >                p.parent = l;
895 >            }
896 >        }
897 >
898 >        /**
899 >         * Returns the TreeNode (or null if not found) for the given key
900 >         * starting at given root.
901 >         */
902 >        @SuppressWarnings("unchecked") final TreeNode getTreeNode
903 >            (int h, Object k, TreeNode p) {
904 >            Class<?> c = k.getClass();
905 >            while (p != null) {
906 >                int dir, ph;  Object pk; Class<?> pc;
907 >                if ((ph = p.hash) == h) {
908 >                    if ((pk = p.key) == k || k.equals(pk))
909 >                        return p;
910 >                    if (c != (pc = pk.getClass()) ||
911 >                        !(k instanceof Comparable) ||
912 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
913 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
914 >                        TreeNode r = null, s = null, pl, pr;
915 >                        if (dir >= 0) {
916 >                            if ((pl = p.left) != null && h <= pl.hash)
917 >                                s = pl;
918 >                        }
919 >                        else if ((pr = p.right) != null && h >= pr.hash)
920 >                            s = pr;
921 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
922 >                            return r;
923 >                    }
924                  }
925 <                e = e.next;
925 >                else
926 >                    dir = (h < ph) ? -1 : 1;
927 >                p = (dir > 0) ? p.right : p.left;
928              }
929 <            break;
929 >            return null;
930          }
362        return null;
363    }
931  
932 <    /** Implementation for put and putIfAbsent **/
933 <    private final Object internalPut(Object k, Object v, boolean replace) {
934 <        int h = spread(k.hashCode());
935 <        Object oldVal = null;  // the previous value or null if none
936 <        Node[] tab = table;
937 <        for (;;) {
938 <            Node e; int i;
939 <            if (tab == null)
940 <                tab = grow(0);
941 <            else if ((e = tabAt(tab, i = (tab.length - 1) & h)) == null) {
942 <                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
932 >        /**
933 >         * Wrapper for getTreeNode used by CHM.get. Tries to obtain
934 >         * read-lock to call getTreeNode, but during failure to get
935 >         * lock, searches along next links.
936 >         */
937 >        final Object getValue(int h, Object k) {
938 >            Node r = null;
939 >            int c = getState(); // Must read lock state first
940 >            for (Node e = first; e != null; e = e.next) {
941 >                if (c <= 0 && compareAndSetState(c, c - 1)) {
942 >                    try {
943 >                        r = getTreeNode(h, k, root);
944 >                    } finally {
945 >                        releaseShared(0);
946 >                    }
947 >                    break;
948 >                }
949 >                else if ((e.hash & HASH_BITS) == h && k.equals(e.key)) {
950 >                    r = e;
951                      break;
952 +                }
953 +                else
954 +                    c = getState();
955 +            }
956 +            return r == null ? null : r.val;
957 +        }
958 +
959 +        /**
960 +         * Finds or adds a node.
961 +         * @return null if added
962 +         */
963 +        @SuppressWarnings("unchecked") final TreeNode putTreeNode
964 +            (int h, Object k, Object v) {
965 +            Class<?> c = k.getClass();
966 +            TreeNode pp = root, p = null;
967 +            int dir = 0;
968 +            while (pp != null) { // find existing node or leaf to insert at
969 +                int ph;  Object pk; Class<?> pc;
970 +                p = pp;
971 +                if ((ph = p.hash) == h) {
972 +                    if ((pk = p.key) == k || k.equals(pk))
973 +                        return p;
974 +                    if (c != (pc = pk.getClass()) ||
975 +                        !(k instanceof Comparable) ||
976 +                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
977 +                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
978 +                        TreeNode r = null, s = null, pl, pr;
979 +                        if (dir >= 0) {
980 +                            if ((pl = p.left) != null && h <= pl.hash)
981 +                                s = pl;
982 +                        }
983 +                        else if ((pr = p.right) != null && h >= pr.hash)
984 +                            s = pr;
985 +                        if (s != null && (r = getTreeNode(h, k, s)) != null)
986 +                            return r;
987 +                    }
988 +                }
989 +                else
990 +                    dir = (h < ph) ? -1 : 1;
991 +                pp = (dir > 0) ? p.right : p.left;
992 +            }
993 +
994 +            TreeNode f = first;
995 +            TreeNode x = first = new TreeNode(h, k, v, f, p);
996 +            if (p == null)
997 +                root = x;
998 +            else { // attach and rebalance; adapted from CLR
999 +                TreeNode xp, xpp;
1000 +                if (f != null)
1001 +                    f.prev = x;
1002 +                if (dir <= 0)
1003 +                    p.left = x;
1004 +                else
1005 +                    p.right = x;
1006 +                x.red = true;
1007 +                while (x != null && (xp = x.parent) != null && xp.red &&
1008 +                       (xpp = xp.parent) != null) {
1009 +                    TreeNode xppl = xpp.left;
1010 +                    if (xp == xppl) {
1011 +                        TreeNode y = xpp.right;
1012 +                        if (y != null && y.red) {
1013 +                            y.red = false;
1014 +                            xp.red = false;
1015 +                            xpp.red = true;
1016 +                            x = xpp;
1017 +                        }
1018 +                        else {
1019 +                            if (x == xp.right) {
1020 +                                rotateLeft(x = xp);
1021 +                                xpp = (xp = x.parent) == null ? null : xp.parent;
1022 +                            }
1023 +                            if (xp != null) {
1024 +                                xp.red = false;
1025 +                                if (xpp != null) {
1026 +                                    xpp.red = true;
1027 +                                    rotateRight(xpp);
1028 +                                }
1029 +                            }
1030 +                        }
1031 +                    }
1032 +                    else {
1033 +                        TreeNode y = xppl;
1034 +                        if (y != null && y.red) {
1035 +                            y.red = false;
1036 +                            xp.red = false;
1037 +                            xpp.red = true;
1038 +                            x = xpp;
1039 +                        }
1040 +                        else {
1041 +                            if (x == xp.left) {
1042 +                                rotateRight(x = xp);
1043 +                                xpp = (xp = x.parent) == null ? null : xp.parent;
1044 +                            }
1045 +                            if (xp != null) {
1046 +                                xp.red = false;
1047 +                                if (xpp != null) {
1048 +                                    xpp.red = true;
1049 +                                    rotateLeft(xpp);
1050 +                                }
1051 +                            }
1052 +                        }
1053 +                    }
1054 +                }
1055 +                TreeNode r = root;
1056 +                if (r != null && r.red)
1057 +                    r.red = false;
1058 +            }
1059 +            return null;
1060 +        }
1061 +
1062 +        /**
1063 +         * Removes the given node, that must be present before this
1064 +         * call.  This is messier than typical red-black deletion code
1065 +         * because we cannot swap the contents of an interior node
1066 +         * with a leaf successor that is pinned by "next" pointers
1067 +         * that are accessible independently of lock. So instead we
1068 +         * swap the tree linkages.
1069 +         */
1070 +        final void deleteTreeNode(TreeNode p) {
1071 +            TreeNode next = (TreeNode)p.next; // unlink traversal pointers
1072 +            TreeNode pred = p.prev;
1073 +            if (pred == null)
1074 +                first = next;
1075 +            else
1076 +                pred.next = next;
1077 +            if (next != null)
1078 +                next.prev = pred;
1079 +            TreeNode replacement;
1080 +            TreeNode pl = p.left;
1081 +            TreeNode pr = p.right;
1082 +            if (pl != null && pr != null) {
1083 +                TreeNode s = pr, sl;
1084 +                while ((sl = s.left) != null) // find successor
1085 +                    s = sl;
1086 +                boolean c = s.red; s.red = p.red; p.red = c; // swap colors
1087 +                TreeNode sr = s.right;
1088 +                TreeNode pp = p.parent;
1089 +                if (s == pr) { // p was s's direct parent
1090 +                    p.parent = s;
1091 +                    s.right = p;
1092 +                }
1093 +                else {
1094 +                    TreeNode sp = s.parent;
1095 +                    if ((p.parent = sp) != null) {
1096 +                        if (s == sp.left)
1097 +                            sp.left = p;
1098 +                        else
1099 +                            sp.right = p;
1100 +                    }
1101 +                    if ((s.right = pr) != null)
1102 +                        pr.parent = s;
1103 +                }
1104 +                p.left = null;
1105 +                if ((p.right = sr) != null)
1106 +                    sr.parent = p;
1107 +                if ((s.left = pl) != null)
1108 +                    pl.parent = s;
1109 +                if ((s.parent = pp) == null)
1110 +                    root = s;
1111 +                else if (p == pp.left)
1112 +                    pp.left = s;
1113 +                else
1114 +                    pp.right = s;
1115 +                replacement = sr;
1116 +            }
1117 +            else
1118 +                replacement = (pl != null) ? pl : pr;
1119 +            TreeNode pp = p.parent;
1120 +            if (replacement == null) {
1121 +                if (pp == null) {
1122 +                    root = null;
1123 +                    return;
1124 +                }
1125 +                replacement = p;
1126              }
378            else if (e.hash < 0)
379                tab = (Node[])e.key;
1127              else {
1128 <                boolean validated = false;
1129 <                boolean checkSize = false;
1130 <                synchronized(e) {
1131 <                    Node first = e;
1132 <                    for (;;) {
1133 <                        Object ek, ev;
1134 <                        if ((ev = e.val) == null)
1135 <                            break;
1136 <                        if (e.hash == h && (ek = e.key) != null &&
1137 <                            (k == ek || k.equals(ek))) {
1138 <                            if (tabAt(tab, i) == first) {
1139 <                                validated = true;
1140 <                                oldVal = ev;
1141 <                                if (replace)
1142 <                                    e.val = v;
1128 >                replacement.parent = pp;
1129 >                if (pp == null)
1130 >                    root = replacement;
1131 >                else if (p == pp.left)
1132 >                    pp.left = replacement;
1133 >                else
1134 >                    pp.right = replacement;
1135 >                p.left = p.right = p.parent = null;
1136 >            }
1137 >            if (!p.red) { // rebalance, from CLR
1138 >                TreeNode x = replacement;
1139 >                while (x != null) {
1140 >                    TreeNode xp, xpl;
1141 >                    if (x.red || (xp = x.parent) == null) {
1142 >                        x.red = false;
1143 >                        break;
1144 >                    }
1145 >                    if (x == (xpl = xp.left)) {
1146 >                        TreeNode sib = xp.right;
1147 >                        if (sib != null && sib.red) {
1148 >                            sib.red = false;
1149 >                            xp.red = true;
1150 >                            rotateLeft(xp);
1151 >                            sib = (xp = x.parent) == null ? null : xp.right;
1152 >                        }
1153 >                        if (sib == null)
1154 >                            x = xp;
1155 >                        else {
1156 >                            TreeNode sl = sib.left, sr = sib.right;
1157 >                            if ((sr == null || !sr.red) &&
1158 >                                (sl == null || !sl.red)) {
1159 >                                sib.red = true;
1160 >                                x = xp;
1161 >                            }
1162 >                            else {
1163 >                                if (sr == null || !sr.red) {
1164 >                                    if (sl != null)
1165 >                                        sl.red = false;
1166 >                                    sib.red = true;
1167 >                                    rotateRight(sib);
1168 >                                    sib = (xp = x.parent) == null ? null : xp.right;
1169 >                                }
1170 >                                if (sib != null) {
1171 >                                    sib.red = (xp == null) ? false : xp.red;
1172 >                                    if ((sr = sib.right) != null)
1173 >                                        sr.red = false;
1174 >                                }
1175 >                                if (xp != null) {
1176 >                                    xp.red = false;
1177 >                                    rotateLeft(xp);
1178 >                                }
1179 >                                x = root;
1180                              }
397                            break;
1181                          }
1182 <                        Node last = e;
1183 <                        if ((e = e.next) == null) {
1184 <                            if (tabAt(tab, i) == first) {
1185 <                                validated = true;
1186 <                                last.next = new Node(h, k, v, null);
1187 <                                if (last != first || tab.length <= 64)
1188 <                                    checkSize = true;
1182 >                    }
1183 >                    else { // symmetric
1184 >                        TreeNode sib = xpl;
1185 >                        if (sib != null && sib.red) {
1186 >                            sib.red = false;
1187 >                            xp.red = true;
1188 >                            rotateRight(xp);
1189 >                            sib = (xp = x.parent) == null ? null : xp.left;
1190 >                        }
1191 >                        if (sib == null)
1192 >                            x = xp;
1193 >                        else {
1194 >                            TreeNode sl = sib.left, sr = sib.right;
1195 >                            if ((sl == null || !sl.red) &&
1196 >                                (sr == null || !sr.red)) {
1197 >                                sib.red = true;
1198 >                                x = xp;
1199 >                            }
1200 >                            else {
1201 >                                if (sl == null || !sl.red) {
1202 >                                    if (sr != null)
1203 >                                        sr.red = false;
1204 >                                    sib.red = true;
1205 >                                    rotateLeft(sib);
1206 >                                    sib = (xp = x.parent) == null ? null : xp.left;
1207 >                                }
1208 >                                if (sib != null) {
1209 >                                    sib.red = (xp == null) ? false : xp.red;
1210 >                                    if ((sl = sib.left) != null)
1211 >                                        sl.red = false;
1212 >                                }
1213 >                                if (xp != null) {
1214 >                                    xp.red = false;
1215 >                                    rotateRight(xp);
1216 >                                }
1217 >                                x = root;
1218                              }
407                            break;
1219                          }
1220                      }
1221                  }
1222 <                if (validated) {
1223 <                    if (checkSize && tab.length < MAXIMUM_CAPACITY &&
1224 <                        resizing == 0 && counter.sum() >= threshold)
1225 <                        grow(0);
1226 <                    break;
1222 >            }
1223 >            if (p == replacement && (pp = p.parent) != null) {
1224 >                if (p == pp.left) // detach pointers
1225 >                    pp.left = null;
1226 >                else if (p == pp.right)
1227 >                    pp.right = null;
1228 >                p.parent = null;
1229 >            }
1230 >        }
1231 >    }
1232 >
1233 >    /* ---------------- Collision reduction methods -------------- */
1234 >
1235 >    /**
1236 >     * Spreads higher bits to lower, and also forces top 2 bits to 0.
1237 >     * Because the table uses power-of-two masking, sets of hashes
1238 >     * that vary only in bits above the current mask will always
1239 >     * collide. (Among known examples are sets of Float keys holding
1240 >     * consecutive whole numbers in small tables.)  To counter this,
1241 >     * we apply a transform that spreads the impact of higher bits
1242 >     * downward. There is a tradeoff between speed, utility, and
1243 >     * quality of bit-spreading. Because many common sets of hashes
1244 >     * are already reasonably distributed across bits (so don't benefit
1245 >     * from spreading), and because we use trees to handle large sets
1246 >     * of collisions in bins, we don't need excessively high quality.
1247 >     */
1248 >    private static final int spread(int h) {
1249 >        h ^= (h >>> 18) ^ (h >>> 12);
1250 >        return (h ^ (h >>> 10)) & HASH_BITS;
1251 >    }
1252 >
1253 >    /**
1254 >     * Replaces a list bin with a tree bin. Call only when locked.
1255 >     * Fails to replace if the given key is non-comparable or table
1256 >     * is, or needs, resizing.
1257 >     */
1258 >    private final void replaceWithTreeBin(Node[] tab, int index, Object key) {
1259 >        if ((key instanceof Comparable) &&
1260 >            (tab.length >= MAXIMUM_CAPACITY || counter.sum() < (long)sizeCtl)) {
1261 >            TreeBin t = new TreeBin();
1262 >            for (Node e = tabAt(tab, index); e != null; e = e.next)
1263 >                t.putTreeNode(e.hash & HASH_BITS, e.key, e.val);
1264 >            setTabAt(tab, index, new Node(MOVED, t, null, null));
1265 >        }
1266 >    }
1267 >
1268 >    /* ---------------- Internal access and update methods -------------- */
1269 >
1270 >    /** Implementation for get and containsKey */
1271 >    private final Object internalGet(Object k) {
1272 >        int h = spread(k.hashCode());
1273 >        retry: for (Node[] tab = table; tab != null;) {
1274 >            Node e, p; Object ek, ev; int eh;      // locals to read fields once
1275 >            for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) {
1276 >                if ((eh = e.hash) == MOVED) {
1277 >                    if ((ek = e.key) instanceof TreeBin)  // search TreeBin
1278 >                        return ((TreeBin)ek).getValue(h, k);
1279 >                    else {                        // restart with new table
1280 >                        tab = (Node[])ek;
1281 >                        continue retry;
1282 >                    }
1283                  }
1284 +                else if ((eh & HASH_BITS) == h && (ev = e.val) != null &&
1285 +                         ((ek = e.key) == k || k.equals(ek)))
1286 +                    return ev;
1287              }
1288 +            break;
1289          }
1290 <        if (oldVal == null)
420 <            counter.increment();
421 <        return oldVal;
1290 >        return null;
1291      }
1292  
1293      /**
1294 <     * Covers the four public remove/replace methods: Replaces node
1295 <     * value with v, conditional upon match of cv if non-null.  If
1296 <     * resulting value is null, delete.
1294 >     * Implementation for the four public remove/replace methods:
1295 >     * Replaces node value with v, conditional upon match of cv if
1296 >     * non-null.  If resulting value is null, delete.
1297       */
1298      private final Object internalReplace(Object k, Object v, Object cv) {
1299          int h = spread(k.hashCode());
1300          Object oldVal = null;
1301 <        Node e; int i;
1302 <        Node[] tab = table;
1303 <        while (tab != null &&
1304 <               (e = tabAt(tab, i = (tab.length - 1) & h)) != null) {
1305 <            if (e.hash < 0)
1306 <                tab = (Node[])e.key;
1307 <            else {
1301 >        for (Node[] tab = table;;) {
1302 >            Node f; int i, fh; Object fk;
1303 >            if (tab == null ||
1304 >                (f = tabAt(tab, i = (tab.length - 1) & h)) == null)
1305 >                break;
1306 >            else if ((fh = f.hash) == MOVED) {
1307 >                if ((fk = f.key) instanceof TreeBin) {
1308 >                    TreeBin t = (TreeBin)fk;
1309 >                    boolean validated = false;
1310 >                    boolean deleted = false;
1311 >                    t.acquire(0);
1312 >                    try {
1313 >                        if (tabAt(tab, i) == f) {
1314 >                            validated = true;
1315 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1316 >                            if (p != null) {
1317 >                                Object pv = p.val;
1318 >                                if (cv == null || cv == pv || cv.equals(pv)) {
1319 >                                    oldVal = pv;
1320 >                                    if ((p.val = v) == null) {
1321 >                                        deleted = true;
1322 >                                        t.deleteTreeNode(p);
1323 >                                    }
1324 >                                }
1325 >                            }
1326 >                        }
1327 >                    } finally {
1328 >                        t.release(0);
1329 >                    }
1330 >                    if (validated) {
1331 >                        if (deleted)
1332 >                            counter.add(-1L);
1333 >                        break;
1334 >                    }
1335 >                }
1336 >                else
1337 >                    tab = (Node[])fk;
1338 >            }
1339 >            else if ((fh & HASH_BITS) != h && f.next == null) // precheck
1340 >                break;                          // rules out possible existence
1341 >            else if ((fh & LOCKED) != 0) {
1342 >                checkForResize();               // try resizing if can't get lock
1343 >                f.tryAwaitLock(tab, i);
1344 >            }
1345 >            else if (f.casHash(fh, fh | LOCKED)) {
1346                  boolean validated = false;
1347                  boolean deleted = false;
1348 <                synchronized(e) {
1349 <                    Node pred = null;
1350 <                    Node first = e;
1351 <                    for (;;) {
1352 <                        Object ek, ev;
1353 <                        if ((ev = e.val) == null)
1354 <                            break;
1355 <                        if (e.hash == h && (ek = e.key) != null &&
449 <                            (k == ek || k.equals(ek))) {
450 <                            if (tabAt(tab, i) == first) {
451 <                                validated = true;
1348 >                try {
1349 >                    if (tabAt(tab, i) == f) {
1350 >                        validated = true;
1351 >                        for (Node e = f, pred = null;;) {
1352 >                            Object ek, ev;
1353 >                            if ((e.hash & HASH_BITS) == h &&
1354 >                                ((ev = e.val) != null) &&
1355 >                                ((ek = e.key) == k || k.equals(ek))) {
1356                                  if (cv == null || cv == ev || cv.equals(ev)) {
1357                                      oldVal = ev;
1358                                      if ((e.val = v) == null) {
# Line 460 | Line 1364 | public class ConcurrentHashMapV8<K, V>
1364                                              setTabAt(tab, i, en);
1365                                      }
1366                                  }
1367 +                                break;
1368                              }
1369 <                            break;
1370 <                        }
1371 <                        pred = e;
467 <                        if ((e = e.next) == null) {
468 <                            if (tabAt(tab, i) == first)
469 <                                validated = true;
470 <                            break;
1369 >                            pred = e;
1370 >                            if ((e = e.next) == null)
1371 >                                break;
1372                          }
1373                      }
1374 +                } finally {
1375 +                    if (!f.casHash(fh | LOCKED, fh)) {
1376 +                        f.hash = fh;
1377 +                        synchronized (f) { f.notifyAll(); };
1378 +                    }
1379                  }
1380                  if (validated) {
1381                      if (deleted)
1382 <                        counter.decrement();
1382 >                        counter.add(-1L);
1383                      break;
1384                  }
1385              }
# Line 481 | Line 1387 | public class ConcurrentHashMapV8<K, V>
1387          return oldVal;
1388      }
1389  
1390 <    /** Implementation for computeIfAbsent and compute */
1391 <    @SuppressWarnings("unchecked")
1392 <    private final V internalCompute(K k,
1393 <                                    MappingFunction<? super K, ? extends V> f,
1394 <                                    boolean replace) {
1390 >    /*
1391 >     * Internal versions of the six insertion methods, each a
1392 >     * little more complicated than the last. All have
1393 >     * the same basic structure as the first (internalPut):
1394 >     *  1. If table uninitialized, create
1395 >     *  2. If bin empty, try to CAS new node
1396 >     *  3. If bin stale, use new table
1397 >     *  4. if bin converted to TreeBin, validate and relay to TreeBin methods
1398 >     *  5. Lock and validate; if valid, scan and add or update
1399 >     *
1400 >     * The others interweave other checks and/or alternative actions:
1401 >     *  * Plain put checks for and performs resize after insertion.
1402 >     *  * putIfAbsent prescans for mapping without lock (and fails to add
1403 >     *    if present), which also makes pre-emptive resize checks worthwhile.
1404 >     *  * computeIfAbsent extends form used in putIfAbsent with additional
1405 >     *    mechanics to deal with, calls, potential exceptions and null
1406 >     *    returns from function call.
1407 >     *  * compute uses the same function-call mechanics, but without
1408 >     *    the prescans
1409 >     *  * merge acts as putIfAbsent in the absent case, but invokes the
1410 >     *    update function if present
1411 >     *  * putAll attempts to pre-allocate enough table space
1412 >     *    and more lazily performs count updates and checks.
1413 >     *
1414 >     * Someday when details settle down a bit more, it might be worth
1415 >     * some factoring to reduce sprawl.
1416 >     */
1417 >
1418 >    /** Implementation for put */
1419 >    private final Object internalPut(Object k, Object v) {
1420          int h = spread(k.hashCode());
1421 <        V val = null;
1422 <        boolean added = false;
1423 <        boolean validated = false;
493 <        Node[] tab = table;
494 <        do {
495 <            Node e; int i;
1421 >        int count = 0;
1422 >        for (Node[] tab = table;;) {
1423 >            int i; Node f; int fh; Object fk;
1424              if (tab == null)
1425 <                tab = grow(0);
1426 <            else if ((e = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1427 <                Node node = new Node(h, k, null, null);
1428 <                synchronized(node) {
1429 <                    if (casTabAt(tab, i, null, node)) {
1430 <                        validated = true;
1431 <                        try {
1432 <                            val = f.map(k);
1433 <                            if (val != null) {
1434 <                                node.val = val;
1435 <                                added = true;
1425 >                tab = initTable();
1426 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1427 >                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1428 >                    break;                   // no lock when adding to empty bin
1429 >            }
1430 >            else if ((fh = f.hash) == MOVED) {
1431 >                if ((fk = f.key) instanceof TreeBin) {
1432 >                    TreeBin t = (TreeBin)fk;
1433 >                    Object oldVal = null;
1434 >                    t.acquire(0);
1435 >                    try {
1436 >                        if (tabAt(tab, i) == f) {
1437 >                            count = 2;
1438 >                            TreeNode p = t.putTreeNode(h, k, v);
1439 >                            if (p != null) {
1440 >                                oldVal = p.val;
1441 >                                p.val = v;
1442                              }
509                        } finally {
510                            if (!added)
511                                setTabAt(tab, i, null);
1443                          }
1444 +                    } finally {
1445 +                        t.release(0);
1446 +                    }
1447 +                    if (count != 0) {
1448 +                        if (oldVal != null)
1449 +                            return oldVal;
1450 +                        break;
1451                      }
1452                  }
1453 +                else
1454 +                    tab = (Node[])fk;
1455 +            }
1456 +            else if ((fh & LOCKED) != 0) {
1457 +                checkForResize();
1458 +                f.tryAwaitLock(tab, i);
1459              }
1460 <            else if (e.hash < 0)
1461 <                tab = (Node[])e.key;
1460 >            else if (f.casHash(fh, fh | LOCKED)) {
1461 >                Object oldVal = null;
1462 >                try {                        // needed in case equals() throws
1463 >                    if (tabAt(tab, i) == f) {
1464 >                        count = 1;
1465 >                        for (Node e = f;; ++count) {
1466 >                            Object ek, ev;
1467 >                            if ((e.hash & HASH_BITS) == h &&
1468 >                                (ev = e.val) != null &&
1469 >                                ((ek = e.key) == k || k.equals(ek))) {
1470 >                                oldVal = ev;
1471 >                                e.val = v;
1472 >                                break;
1473 >                            }
1474 >                            Node last = e;
1475 >                            if ((e = e.next) == null) {
1476 >                                last.next = new Node(h, k, v, null);
1477 >                                if (count >= TREE_THRESHOLD)
1478 >                                    replaceWithTreeBin(tab, i, k);
1479 >                                break;
1480 >                            }
1481 >                        }
1482 >                    }
1483 >                } finally {                  // unlock and signal if needed
1484 >                    if (!f.casHash(fh | LOCKED, fh)) {
1485 >                        f.hash = fh;
1486 >                        synchronized (f) { f.notifyAll(); };
1487 >                    }
1488 >                }
1489 >                if (count != 0) {
1490 >                    if (oldVal != null)
1491 >                        return oldVal;
1492 >                    if (tab.length <= 64)
1493 >                        count = 2;
1494 >                    break;
1495 >                }
1496 >            }
1497 >        }
1498 >        counter.add(1L);
1499 >        if (count > 1)
1500 >            checkForResize();
1501 >        return null;
1502 >    }
1503 >
1504 >    /** Implementation for putIfAbsent */
1505 >    private final Object internalPutIfAbsent(Object k, Object v) {
1506 >        int h = spread(k.hashCode());
1507 >        int count = 0;
1508 >        for (Node[] tab = table;;) {
1509 >            int i; Node f; int fh; Object fk, fv;
1510 >            if (tab == null)
1511 >                tab = initTable();
1512 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1513 >                if (casTabAt(tab, i, null, new Node(h, k, v, null)))
1514 >                    break;
1515 >            }
1516 >            else if ((fh = f.hash) == MOVED) {
1517 >                if ((fk = f.key) instanceof TreeBin) {
1518 >                    TreeBin t = (TreeBin)fk;
1519 >                    Object oldVal = null;
1520 >                    t.acquire(0);
1521 >                    try {
1522 >                        if (tabAt(tab, i) == f) {
1523 >                            count = 2;
1524 >                            TreeNode p = t.putTreeNode(h, k, v);
1525 >                            if (p != null)
1526 >                                oldVal = p.val;
1527 >                        }
1528 >                    } finally {
1529 >                        t.release(0);
1530 >                    }
1531 >                    if (count != 0) {
1532 >                        if (oldVal != null)
1533 >                            return oldVal;
1534 >                        break;
1535 >                    }
1536 >                }
1537 >                else
1538 >                    tab = (Node[])fk;
1539 >            }
1540 >            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1541 >                     ((fk = f.key) == k || k.equals(fk)))
1542 >                return fv;
1543              else {
1544 <                boolean checkSize = false;
1545 <                synchronized(e) {
1546 <                    Node first = e;
522 <                    for (;;) {
1544 >                Node g = f.next;
1545 >                if (g != null) { // at least 2 nodes -- search and maybe resize
1546 >                    for (Node e = g;;) {
1547                          Object ek, ev;
1548 <                        if ((ev = e.val) == null)
1548 >                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1549 >                            ((ek = e.key) == k || k.equals(ek)))
1550 >                            return ev;
1551 >                        if ((e = e.next) == null) {
1552 >                            checkForResize();
1553                              break;
1554 <                        if (e.hash == h && (ek = e.key) != null &&
1555 <                            (k == ek || k.equals(ek))) {
1556 <                            if (tabAt(tab, i) == first) {
1557 <                                validated = true;
1558 <                                if (replace && (ev = f.map(k)) != null)
1559 <                                    e.val = ev;
1560 <                                val = (V)ev;
1554 >                        }
1555 >                    }
1556 >                }
1557 >                if (((fh = f.hash) & LOCKED) != 0) {
1558 >                    checkForResize();
1559 >                    f.tryAwaitLock(tab, i);
1560 >                }
1561 >                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1562 >                    Object oldVal = null;
1563 >                    try {
1564 >                        if (tabAt(tab, i) == f) {
1565 >                            count = 1;
1566 >                            for (Node e = f;; ++count) {
1567 >                                Object ek, ev;
1568 >                                if ((e.hash & HASH_BITS) == h &&
1569 >                                    (ev = e.val) != null &&
1570 >                                    ((ek = e.key) == k || k.equals(ek))) {
1571 >                                    oldVal = ev;
1572 >                                    break;
1573 >                                }
1574 >                                Node last = e;
1575 >                                if ((e = e.next) == null) {
1576 >                                    last.next = new Node(h, k, v, null);
1577 >                                    if (count >= TREE_THRESHOLD)
1578 >                                        replaceWithTreeBin(tab, i, k);
1579 >                                    break;
1580 >                                }
1581                              }
534                            break;
1582                          }
1583 <                        Node last = e;
1583 >                    } finally {
1584 >                        if (!f.casHash(fh | LOCKED, fh)) {
1585 >                            f.hash = fh;
1586 >                            synchronized (f) { f.notifyAll(); };
1587 >                        }
1588 >                    }
1589 >                    if (count != 0) {
1590 >                        if (oldVal != null)
1591 >                            return oldVal;
1592 >                        if (tab.length <= 64)
1593 >                            count = 2;
1594 >                        break;
1595 >                    }
1596 >                }
1597 >            }
1598 >        }
1599 >        counter.add(1L);
1600 >        if (count > 1)
1601 >            checkForResize();
1602 >        return null;
1603 >    }
1604 >
1605 >    /** Implementation for computeIfAbsent */
1606 >    private final Object internalComputeIfAbsent(K k,
1607 >                                                 Fun<? super K, ?> mf) {
1608 >        int h = spread(k.hashCode());
1609 >        Object val = null;
1610 >        int count = 0;
1611 >        for (Node[] tab = table;;) {
1612 >            Node f; int i, fh; Object fk, fv;
1613 >            if (tab == null)
1614 >                tab = initTable();
1615 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1616 >                Node node = new Node(fh = h | LOCKED, k, null, null);
1617 >                if (casTabAt(tab, i, null, node)) {
1618 >                    count = 1;
1619 >                    try {
1620 >                        if ((val = mf.apply(k)) != null)
1621 >                            node.val = val;
1622 >                    } finally {
1623 >                        if (val == null)
1624 >                            setTabAt(tab, i, null);
1625 >                        if (!node.casHash(fh, h)) {
1626 >                            node.hash = h;
1627 >                            synchronized (node) { node.notifyAll(); };
1628 >                        }
1629 >                    }
1630 >                }
1631 >                if (count != 0)
1632 >                    break;
1633 >            }
1634 >            else if ((fh = f.hash) == MOVED) {
1635 >                if ((fk = f.key) instanceof TreeBin) {
1636 >                    TreeBin t = (TreeBin)fk;
1637 >                    boolean added = false;
1638 >                    t.acquire(0);
1639 >                    try {
1640 >                        if (tabAt(tab, i) == f) {
1641 >                            count = 1;
1642 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1643 >                            if (p != null)
1644 >                                val = p.val;
1645 >                            else if ((val = mf.apply(k)) != null) {
1646 >                                added = true;
1647 >                                count = 2;
1648 >                                t.putTreeNode(h, k, val);
1649 >                            }
1650 >                        }
1651 >                    } finally {
1652 >                        t.release(0);
1653 >                    }
1654 >                    if (count != 0) {
1655 >                        if (!added)
1656 >                            return val;
1657 >                        break;
1658 >                    }
1659 >                }
1660 >                else
1661 >                    tab = (Node[])fk;
1662 >            }
1663 >            else if ((fh & HASH_BITS) == h && (fv = f.val) != null &&
1664 >                     ((fk = f.key) == k || k.equals(fk)))
1665 >                return fv;
1666 >            else {
1667 >                Node g = f.next;
1668 >                if (g != null) {
1669 >                    for (Node e = g;;) {
1670 >                        Object ek, ev;
1671 >                        if ((e.hash & HASH_BITS) == h && (ev = e.val) != null &&
1672 >                            ((ek = e.key) == k || k.equals(ek)))
1673 >                            return ev;
1674                          if ((e = e.next) == null) {
1675 <                            if (tabAt(tab, i) == first) {
1676 <                                validated = true;
1677 <                                if ((val = f.map(k)) != null) {
1678 <                                    last.next = new Node(h, k, val, null);
1679 <                                    added = true;
1680 <                                    if (last != first || tab.length <= 64)
1681 <                                        checkSize = true;
1675 >                            checkForResize();
1676 >                            break;
1677 >                        }
1678 >                    }
1679 >                }
1680 >                if (((fh = f.hash) & LOCKED) != 0) {
1681 >                    checkForResize();
1682 >                    f.tryAwaitLock(tab, i);
1683 >                }
1684 >                else if (tabAt(tab, i) == f && f.casHash(fh, fh | LOCKED)) {
1685 >                    boolean added = false;
1686 >                    try {
1687 >                        if (tabAt(tab, i) == f) {
1688 >                            count = 1;
1689 >                            for (Node e = f;; ++count) {
1690 >                                Object ek, ev;
1691 >                                if ((e.hash & HASH_BITS) == h &&
1692 >                                    (ev = e.val) != null &&
1693 >                                    ((ek = e.key) == k || k.equals(ek))) {
1694 >                                    val = ev;
1695 >                                    break;
1696 >                                }
1697 >                                Node last = e;
1698 >                                if ((e = e.next) == null) {
1699 >                                    if ((val = mf.apply(k)) != null) {
1700 >                                        added = true;
1701 >                                        last.next = new Node(h, k, val, null);
1702 >                                        if (count >= TREE_THRESHOLD)
1703 >                                            replaceWithTreeBin(tab, i, k);
1704 >                                    }
1705 >                                    break;
1706                                  }
1707                              }
1708 <                            break;
1708 >                        }
1709 >                    } finally {
1710 >                        if (!f.casHash(fh | LOCKED, fh)) {
1711 >                            f.hash = fh;
1712 >                            synchronized (f) { f.notifyAll(); };
1713                          }
1714                      }
1715 +                    if (count != 0) {
1716 +                        if (!added)
1717 +                            return val;
1718 +                        if (tab.length <= 64)
1719 +                            count = 2;
1720 +                        break;
1721 +                    }
1722                  }
551                if (checkSize && tab.length < MAXIMUM_CAPACITY &&
552                    resizing == 0 && counter.sum() >= threshold)
553                    grow(0);
1723              }
1724 <        } while (!validated);
1725 <        if (added)
1726 <            counter.increment();
1724 >        }
1725 >        if (val != null) {
1726 >            counter.add(1L);
1727 >            if (count > 1)
1728 >                checkForResize();
1729 >        }
1730          return val;
1731      }
1732  
1733 <    /*
1734 <     * Reclassifies nodes in each bin to new table.  Because we are
1735 <     * using power-of-two expansion, the elements from each bin must
1736 <     * either stay at same index, or move with a power of two
1737 <     * offset. We eliminate unnecessary node creation by catching
1738 <     * cases where old nodes can be reused because their next fields
1739 <     * won't change.  Statistically, at the default threshold, only
1740 <     * about one-sixth of them need cloning when a table doubles. The
1741 <     * nodes they replace will be garbage collectable as soon as they
1742 <     * are no longer referenced by any reader thread that may be in
1743 <     * the midst of concurrently traversing table.
1744 <     *
1745 <     * Transfers are done from the bottom up to preserve iterator
1746 <     * traversability. On each step, the old bin is locked,
1747 <     * moved/copied, and then replaced with a forwarding node.
1748 <     */
1749 <    private static final void transfer(Node[] tab, Node[] nextTab) {
1750 <        int n = tab.length;
1751 <        int mask = nextTab.length - 1;
1752 <        Node fwd = new Node(MOVED, nextTab, null, null);
1753 <        for (int i = n - 1; i >= 0; --i) {
1754 <            for (Node e;;) {
1755 <                if ((e = tabAt(tab, i)) == null) {
1756 <                    if (casTabAt(tab, i, e, fwd))
1733 >    /** Implementation for compute */
1734 >    @SuppressWarnings("unchecked") private final Object internalCompute
1735 >        (K k, boolean onlyIfPresent, BiFun<? super K, ? super V, ? extends V> mf) {
1736 >        int h = spread(k.hashCode());
1737 >        Object val = null;
1738 >        int delta = 0;
1739 >        int count = 0;
1740 >        for (Node[] tab = table;;) {
1741 >            Node f; int i, fh; Object fk;
1742 >            if (tab == null)
1743 >                tab = initTable();
1744 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1745 >                if (onlyIfPresent)
1746 >                    break;
1747 >                Node node = new Node(fh = h | LOCKED, k, null, null);
1748 >                if (casTabAt(tab, i, null, node)) {
1749 >                    try {
1750 >                        count = 1;
1751 >                        if ((val = mf.apply(k, null)) != null) {
1752 >                            node.val = val;
1753 >                            delta = 1;
1754 >                        }
1755 >                    } finally {
1756 >                        if (delta == 0)
1757 >                            setTabAt(tab, i, null);
1758 >                        if (!node.casHash(fh, h)) {
1759 >                            node.hash = h;
1760 >                            synchronized (node) { node.notifyAll(); };
1761 >                        }
1762 >                    }
1763 >                }
1764 >                if (count != 0)
1765 >                    break;
1766 >            }
1767 >            else if ((fh = f.hash) == MOVED) {
1768 >                if ((fk = f.key) instanceof TreeBin) {
1769 >                    TreeBin t = (TreeBin)fk;
1770 >                    t.acquire(0);
1771 >                    try {
1772 >                        if (tabAt(tab, i) == f) {
1773 >                            count = 1;
1774 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1775 >                            Object pv = (p == null) ? null : p.val;
1776 >                            if ((val = mf.apply(k, (V)pv)) != null) {
1777 >                                if (p != null)
1778 >                                    p.val = val;
1779 >                                else {
1780 >                                    count = 2;
1781 >                                    delta = 1;
1782 >                                    t.putTreeNode(h, k, val);
1783 >                                }
1784 >                            }
1785 >                            else if (p != null) {
1786 >                                delta = -1;
1787 >                                t.deleteTreeNode(p);
1788 >                            }
1789 >                        }
1790 >                    } finally {
1791 >                        t.release(0);
1792 >                    }
1793 >                    if (count != 0)
1794                          break;
1795                  }
1796 <                else {
1797 <                    boolean validated = false;
1798 <                    synchronized(e) {
1799 <                        int idx = e.hash & mask;
1800 <                        Node lastRun = e;
1801 <                        for (Node p = e.next; p != null; p = p.next) {
1802 <                            int j = p.hash & mask;
1803 <                            if (j != idx) {
1804 <                                idx = j;
1805 <                                lastRun = p;
1796 >                else
1797 >                    tab = (Node[])fk;
1798 >            }
1799 >            else if ((fh & LOCKED) != 0) {
1800 >                checkForResize();
1801 >                f.tryAwaitLock(tab, i);
1802 >            }
1803 >            else if (f.casHash(fh, fh | LOCKED)) {
1804 >                try {
1805 >                    if (tabAt(tab, i) == f) {
1806 >                        count = 1;
1807 >                        for (Node e = f, pred = null;; ++count) {
1808 >                            Object ek, ev;
1809 >                            if ((e.hash & HASH_BITS) == h &&
1810 >                                (ev = e.val) != null &&
1811 >                                ((ek = e.key) == k || k.equals(ek))) {
1812 >                                val = mf.apply(k, (V)ev);
1813 >                                if (val != null)
1814 >                                    e.val = val;
1815 >                                else {
1816 >                                    delta = -1;
1817 >                                    Node en = e.next;
1818 >                                    if (pred != null)
1819 >                                        pred.next = en;
1820 >                                    else
1821 >                                        setTabAt(tab, i, en);
1822 >                                }
1823 >                                break;
1824 >                            }
1825 >                            pred = e;
1826 >                            if ((e = e.next) == null) {
1827 >                                if (!onlyIfPresent && (val = mf.apply(k, null)) != null) {
1828 >                                    pred.next = new Node(h, k, val, null);
1829 >                                    delta = 1;
1830 >                                    if (count >= TREE_THRESHOLD)
1831 >                                        replaceWithTreeBin(tab, i, k);
1832 >                                }
1833 >                                break;
1834                              }
1835                          }
1836 <                        if (tabAt(tab, i) == e) {
1837 <                            validated = true;
1838 <                            relaxedSetTabAt(nextTab, idx, lastRun);
1839 <                            for (Node p = e; p != lastRun; p = p.next) {
1840 <                                int h = p.hash;
1841 <                                int j = h & mask;
1842 <                                Node r = relaxedTabAt(nextTab, j);
1843 <                                relaxedSetTabAt(nextTab, j,
1844 <                                                new Node(h, p.key, p.val, r));
1836 >                    }
1837 >                } finally {
1838 >                    if (!f.casHash(fh | LOCKED, fh)) {
1839 >                        f.hash = fh;
1840 >                        synchronized (f) { f.notifyAll(); };
1841 >                    }
1842 >                }
1843 >                if (count != 0) {
1844 >                    if (tab.length <= 64)
1845 >                        count = 2;
1846 >                    break;
1847 >                }
1848 >            }
1849 >        }
1850 >        if (delta != 0) {
1851 >            counter.add((long)delta);
1852 >            if (count > 1)
1853 >                checkForResize();
1854 >        }
1855 >        return val;
1856 >    }
1857 >
1858 >    /** Implementation for merge */
1859 >    @SuppressWarnings("unchecked") private final Object internalMerge
1860 >        (K k, V v, BiFun<? super V, ? super V, ? extends V> mf) {
1861 >        int h = spread(k.hashCode());
1862 >        Object val = null;
1863 >        int delta = 0;
1864 >        int count = 0;
1865 >        for (Node[] tab = table;;) {
1866 >            int i; Node f; int fh; Object fk, fv;
1867 >            if (tab == null)
1868 >                tab = initTable();
1869 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1870 >                if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1871 >                    delta = 1;
1872 >                    val = v;
1873 >                    break;
1874 >                }
1875 >            }
1876 >            else if ((fh = f.hash) == MOVED) {
1877 >                if ((fk = f.key) instanceof TreeBin) {
1878 >                    TreeBin t = (TreeBin)fk;
1879 >                    t.acquire(0);
1880 >                    try {
1881 >                        if (tabAt(tab, i) == f) {
1882 >                            count = 1;
1883 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1884 >                            val = (p == null) ? v : mf.apply((V)p.val, v);
1885 >                            if (val != null) {
1886 >                                if (p != null)
1887 >                                    p.val = val;
1888 >                                else {
1889 >                                    count = 2;
1890 >                                    delta = 1;
1891 >                                    t.putTreeNode(h, k, val);
1892 >                                }
1893 >                            }
1894 >                            else if (p != null) {
1895 >                                delta = -1;
1896 >                                t.deleteTreeNode(p);
1897                              }
609                            setTabAt(tab, i, fwd);
1898                          }
1899 +                    } finally {
1900 +                        t.release(0);
1901                      }
1902 <                    if (validated)
1902 >                    if (count != 0)
1903                          break;
1904                  }
1905 +                else
1906 +                    tab = (Node[])fk;
1907 +            }
1908 +            else if ((fh & LOCKED) != 0) {
1909 +                checkForResize();
1910 +                f.tryAwaitLock(tab, i);
1911 +            }
1912 +            else if (f.casHash(fh, fh | LOCKED)) {
1913 +                try {
1914 +                    if (tabAt(tab, i) == f) {
1915 +                        count = 1;
1916 +                        for (Node e = f, pred = null;; ++count) {
1917 +                            Object ek, ev;
1918 +                            if ((e.hash & HASH_BITS) == h &&
1919 +                                (ev = e.val) != null &&
1920 +                                ((ek = e.key) == k || k.equals(ek))) {
1921 +                                val = mf.apply(v, (V)ev);
1922 +                                if (val != null)
1923 +                                    e.val = val;
1924 +                                else {
1925 +                                    delta = -1;
1926 +                                    Node en = e.next;
1927 +                                    if (pred != null)
1928 +                                        pred.next = en;
1929 +                                    else
1930 +                                        setTabAt(tab, i, en);
1931 +                                }
1932 +                                break;
1933 +                            }
1934 +                            pred = e;
1935 +                            if ((e = e.next) == null) {
1936 +                                val = v;
1937 +                                pred.next = new Node(h, k, val, null);
1938 +                                delta = 1;
1939 +                                if (count >= TREE_THRESHOLD)
1940 +                                    replaceWithTreeBin(tab, i, k);
1941 +                                break;
1942 +                            }
1943 +                        }
1944 +                    }
1945 +                } finally {
1946 +                    if (!f.casHash(fh | LOCKED, fh)) {
1947 +                        f.hash = fh;
1948 +                        synchronized (f) { f.notifyAll(); };
1949 +                    }
1950 +                }
1951 +                if (count != 0) {
1952 +                    if (tab.length <= 64)
1953 +                        count = 2;
1954 +                    break;
1955 +                }
1956 +            }
1957 +        }
1958 +        if (delta != 0) {
1959 +            counter.add((long)delta);
1960 +            if (count > 1)
1961 +                checkForResize();
1962 +        }
1963 +        return val;
1964 +    }
1965 +
1966 +    /** Implementation for putAll */
1967 +    private final void internalPutAll(Map<?, ?> m) {
1968 +        tryPresize(m.size());
1969 +        long delta = 0L;     // number of uncommitted additions
1970 +        boolean npe = false; // to throw exception on exit for nulls
1971 +        try {                // to clean up counts on other exceptions
1972 +            for (Map.Entry<?, ?> entry : m.entrySet()) {
1973 +                Object k, v;
1974 +                if (entry == null || (k = entry.getKey()) == null ||
1975 +                    (v = entry.getValue()) == null) {
1976 +                    npe = true;
1977 +                    break;
1978 +                }
1979 +                int h = spread(k.hashCode());
1980 +                for (Node[] tab = table;;) {
1981 +                    int i; Node f; int fh; Object fk;
1982 +                    if (tab == null)
1983 +                        tab = initTable();
1984 +                    else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){
1985 +                        if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1986 +                            ++delta;
1987 +                            break;
1988 +                        }
1989 +                    }
1990 +                    else if ((fh = f.hash) == MOVED) {
1991 +                        if ((fk = f.key) instanceof TreeBin) {
1992 +                            TreeBin t = (TreeBin)fk;
1993 +                            boolean validated = false;
1994 +                            t.acquire(0);
1995 +                            try {
1996 +                                if (tabAt(tab, i) == f) {
1997 +                                    validated = true;
1998 +                                    TreeNode p = t.getTreeNode(h, k, t.root);
1999 +                                    if (p != null)
2000 +                                        p.val = v;
2001 +                                    else {
2002 +                                        t.putTreeNode(h, k, v);
2003 +                                        ++delta;
2004 +                                    }
2005 +                                }
2006 +                            } finally {
2007 +                                t.release(0);
2008 +                            }
2009 +                            if (validated)
2010 +                                break;
2011 +                        }
2012 +                        else
2013 +                            tab = (Node[])fk;
2014 +                    }
2015 +                    else if ((fh & LOCKED) != 0) {
2016 +                        counter.add(delta);
2017 +                        delta = 0L;
2018 +                        checkForResize();
2019 +                        f.tryAwaitLock(tab, i);
2020 +                    }
2021 +                    else if (f.casHash(fh, fh | LOCKED)) {
2022 +                        int count = 0;
2023 +                        try {
2024 +                            if (tabAt(tab, i) == f) {
2025 +                                count = 1;
2026 +                                for (Node e = f;; ++count) {
2027 +                                    Object ek, ev;
2028 +                                    if ((e.hash & HASH_BITS) == h &&
2029 +                                        (ev = e.val) != null &&
2030 +                                        ((ek = e.key) == k || k.equals(ek))) {
2031 +                                        e.val = v;
2032 +                                        break;
2033 +                                    }
2034 +                                    Node last = e;
2035 +                                    if ((e = e.next) == null) {
2036 +                                        ++delta;
2037 +                                        last.next = new Node(h, k, v, null);
2038 +                                        if (count >= TREE_THRESHOLD)
2039 +                                            replaceWithTreeBin(tab, i, k);
2040 +                                        break;
2041 +                                    }
2042 +                                }
2043 +                            }
2044 +                        } finally {
2045 +                            if (!f.casHash(fh | LOCKED, fh)) {
2046 +                                f.hash = fh;
2047 +                                synchronized (f) { f.notifyAll(); };
2048 +                            }
2049 +                        }
2050 +                        if (count != 0) {
2051 +                            if (count > 1) {
2052 +                                counter.add(delta);
2053 +                                delta = 0L;
2054 +                                checkForResize();
2055 +                            }
2056 +                            break;
2057 +                        }
2058 +                    }
2059 +                }
2060 +            }
2061 +        } finally {
2062 +            if (delta != 0)
2063 +                counter.add(delta);
2064 +        }
2065 +        if (npe)
2066 +            throw new NullPointerException();
2067 +    }
2068 +
2069 +    /* ---------------- Table Initialization and Resizing -------------- */
2070 +
2071 +    /**
2072 +     * Returns a power of two table size for the given desired capacity.
2073 +     * See Hackers Delight, sec 3.2
2074 +     */
2075 +    private static final int tableSizeFor(int c) {
2076 +        int n = c - 1;
2077 +        n |= n >>> 1;
2078 +        n |= n >>> 2;
2079 +        n |= n >>> 4;
2080 +        n |= n >>> 8;
2081 +        n |= n >>> 16;
2082 +        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
2083 +    }
2084 +
2085 +    /**
2086 +     * Initializes table, using the size recorded in sizeCtl.
2087 +     */
2088 +    private final Node[] initTable() {
2089 +        Node[] tab; int sc;
2090 +        while ((tab = table) == null) {
2091 +            if ((sc = sizeCtl) < 0)
2092 +                Thread.yield(); // lost initialization race; just spin
2093 +            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2094 +                try {
2095 +                    if ((tab = table) == null) {
2096 +                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
2097 +                        tab = table = new Node[n];
2098 +                        sc = n - (n >>> 2);
2099 +                    }
2100 +                } finally {
2101 +                    sizeCtl = sc;
2102 +                }
2103 +                break;
2104              }
2105          }
2106 +        return tab;
2107      }
2108  
2109      /**
2110 <     * If not already resizing, initializes or creates next table and
2111 <     * transfers bins. Rechecks occupancy after a transfer to see if
2112 <     * another resize is already needed because resizings are lagging
2113 <     * additions.
624 <     *
625 <     * @param sizeHint overridden capacity target (nonzero only from putAll)
626 <     * @return current table
2110 >     * If table is too small and not already resizing, creates next
2111 >     * table and transfers bins.  Rechecks occupancy after a transfer
2112 >     * to see if another resize is already needed because resizings
2113 >     * are lagging additions.
2114       */
2115 <    private final Node[] grow(int sizeHint) {
2116 <        if (resizing == 0 &&
2117 <            UNSAFE.compareAndSwapInt(this, resizingOffset, 0, 1)) {
2115 >    private final void checkForResize() {
2116 >        Node[] tab; int n, sc;
2117 >        while ((tab = table) != null &&
2118 >               (n = tab.length) < MAXIMUM_CAPACITY &&
2119 >               (sc = sizeCtl) >= 0 && counter.sum() >= (long)sc &&
2120 >               UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2121              try {
2122 <                for (;;) {
2123 <                    int cap, n;
2124 <                    Node[] tab = table;
635 <                    if (tab == null) {
636 <                        int c = initCap;
637 <                        if (c < sizeHint)
638 <                            c = sizeHint;
639 <                        if (c == DEFAULT_CAPACITY)
640 <                            cap = c;
641 <                        else if (c >= MAXIMUM_CAPACITY)
642 <                            cap = MAXIMUM_CAPACITY;
643 <                        else {
644 <                            cap = MINIMUM_CAPACITY;
645 <                            while (cap < c)
646 <                                cap <<= 1;
647 <                        }
648 <                    }
649 <                    else if ((n = tab.length) < MAXIMUM_CAPACITY &&
650 <                             (sizeHint <= 0 || n < sizeHint))
651 <                        cap = n << 1;
652 <                    else
653 <                        break;
654 <                    threshold = (int)(cap * loadFactor) - THRESHOLD_OFFSET;
655 <                    Node[] nextTab = new Node[cap];
656 <                    if (tab != null)
657 <                        transfer(tab, nextTab);
658 <                    table = nextTab;
659 <                    if (tab == null || cap >= MAXIMUM_CAPACITY ||
660 <                        (sizeHint > 0 && cap >= sizeHint) ||
661 <                        counter.sum() < threshold)
662 <                        break;
2122 >                if (tab == table) {
2123 >                    table = rebuild(tab);
2124 >                    sc = (n << 1) - (n >>> 1);
2125                  }
2126              } finally {
2127 <                resizing = 0;
2127 >                sizeCtl = sc;
2128              }
2129          }
668        else if (table == null)
669            Thread.yield(); // lost initialization race; just spin
670        return table;
2130      }
2131  
2132      /**
2133 <     * Implementation for putAll and constructor with Map
2134 <     * argument. Tries to first override initial capacity or grow
2135 <     * based on map size to pre-allocate table space.
2133 >     * Tries to presize table to accommodate the given number of elements.
2134 >     *
2135 >     * @param size number of elements (doesn't need to be perfectly accurate)
2136       */
2137 <    private final void internalPutAll(Map<? extends K, ? extends V> m) {
2138 <        int s = m.size();
2139 <        grow((s >= (MAXIMUM_CAPACITY >>> 1))? s : s + (s >>> 1));
2140 <        for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
2141 <            Object k = e.getKey();
2142 <            Object v = e.getValue();
2143 <            if (k == null || v == null)
2144 <                throw new NullPointerException();
2145 <            internalPut(k, v, true);
2137 >    private final void tryPresize(int size) {
2138 >        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
2139 >            tableSizeFor(size + (size >>> 1) + 1);
2140 >        int sc;
2141 >        while ((sc = sizeCtl) >= 0) {
2142 >            Node[] tab = table; int n;
2143 >            if (tab == null || (n = tab.length) == 0) {
2144 >                n = (sc > c) ? sc : c;
2145 >                if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2146 >                    try {
2147 >                        if (table == tab) {
2148 >                            table = new Node[n];
2149 >                            sc = n - (n >>> 2);
2150 >                        }
2151 >                    } finally {
2152 >                        sizeCtl = sc;
2153 >                    }
2154 >                }
2155 >            }
2156 >            else if (c <= sc || n >= MAXIMUM_CAPACITY)
2157 >                break;
2158 >            else if (UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
2159 >                try {
2160 >                    if (table == tab) {
2161 >                        table = rebuild(tab);
2162 >                        sc = (n << 1) - (n >>> 1);
2163 >                    }
2164 >                } finally {
2165 >                    sizeCtl = sc;
2166 >                }
2167 >            }
2168          }
2169      }
2170  
2171 <    /**
2172 <     * Implementation for clear. Steps through each bin, removing all nodes.
2171 >    /*
2172 >     * Moves and/or copies the nodes in each bin to new table. See
2173 >     * above for explanation.
2174 >     *
2175 >     * @return the new table
2176       */
2177 <    private final void internalClear() {
2178 <        long deletions = 0L;
2179 <        int i = 0;
2180 <        Node[] tab = table;
2181 <        while (tab != null && i < tab.length) {
2182 <            Node e = tabAt(tab, i);
2183 <            if (e == null)
2184 <                ++i;
2185 <            else if (e.hash < 0)
2186 <                tab = (Node[])e.key;
2187 <            else {
2177 >    private static final Node[] rebuild(Node[] tab) {
2178 >        int n = tab.length;
2179 >        Node[] nextTab = new Node[n << 1];
2180 >        Node fwd = new Node(MOVED, nextTab, null, null);
2181 >        int[] buffer = null;       // holds bins to revisit; null until needed
2182 >        Node rev = null;           // reverse forwarder; null until needed
2183 >        int nbuffered = 0;         // the number of bins in buffer list
2184 >        int bufferIndex = 0;       // buffer index of current buffered bin
2185 >        int bin = n - 1;           // current non-buffered bin or -1 if none
2186 >
2187 >        for (int i = bin;;) {      // start upwards sweep
2188 >            int fh; Node f;
2189 >            if ((f = tabAt(tab, i)) == null) {
2190 >                if (bin >= 0) {    // Unbuffered; no lock needed (or available)
2191 >                    if (!casTabAt(tab, i, f, fwd))
2192 >                        continue;
2193 >                }
2194 >                else {             // transiently use a locked forwarding node
2195 >                    Node g = new Node(MOVED|LOCKED, nextTab, null, null);
2196 >                    if (!casTabAt(tab, i, f, g))
2197 >                        continue;
2198 >                    setTabAt(nextTab, i, null);
2199 >                    setTabAt(nextTab, i + n, null);
2200 >                    setTabAt(tab, i, fwd);
2201 >                    if (!g.casHash(MOVED|LOCKED, MOVED)) {
2202 >                        g.hash = MOVED;
2203 >                        synchronized (g) { g.notifyAll(); }
2204 >                    }
2205 >                }
2206 >            }
2207 >            else if ((fh = f.hash) == MOVED) {
2208 >                Object fk = f.key;
2209 >                if (fk instanceof TreeBin) {
2210 >                    TreeBin t = (TreeBin)fk;
2211 >                    boolean validated = false;
2212 >                    t.acquire(0);
2213 >                    try {
2214 >                        if (tabAt(tab, i) == f) {
2215 >                            validated = true;
2216 >                            splitTreeBin(nextTab, i, t);
2217 >                            setTabAt(tab, i, fwd);
2218 >                        }
2219 >                    } finally {
2220 >                        t.release(0);
2221 >                    }
2222 >                    if (!validated)
2223 >                        continue;
2224 >                }
2225 >            }
2226 >            else if ((fh & LOCKED) == 0 && f.casHash(fh, fh|LOCKED)) {
2227                  boolean validated = false;
2228 <                synchronized(e) {
2229 <                    if (tabAt(tab, i) == e) {
2228 >                try {              // split to lo and hi lists; copying as needed
2229 >                    if (tabAt(tab, i) == f) {
2230                          validated = true;
2231 <                        do {
2232 <                            if (e.val != null) {
710 <                                e.val = null;
711 <                                ++deletions;
712 <                            }
713 <                        } while ((e = e.next) != null);
714 <                        setTabAt(tab, i, null);
2231 >                        splitBin(nextTab, i, f);
2232 >                        setTabAt(tab, i, fwd);
2233                      }
2234 <                }
2235 <                if (validated) {
2236 <                    ++i;
2237 <                    if (deletions > THRESHOLD_OFFSET) { // bound lag in counts
720 <                        counter.add(-deletions);
721 <                        deletions = 0L;
2234 >                } finally {
2235 >                    if (!f.casHash(fh | LOCKED, fh)) {
2236 >                        f.hash = fh;
2237 >                        synchronized (f) { f.notifyAll(); };
2238                      }
2239                  }
2240 +                if (!validated)
2241 +                    continue;
2242              }
2243 +            else {
2244 +                if (buffer == null) // initialize buffer for revisits
2245 +                    buffer = new int[TRANSFER_BUFFER_SIZE];
2246 +                if (bin < 0 && bufferIndex > 0) {
2247 +                    int j = buffer[--bufferIndex];
2248 +                    buffer[bufferIndex] = i;
2249 +                    i = j;         // swap with another bin
2250 +                    continue;
2251 +                }
2252 +                if (bin < 0 || nbuffered >= TRANSFER_BUFFER_SIZE) {
2253 +                    f.tryAwaitLock(tab, i);
2254 +                    continue;      // no other options -- block
2255 +                }
2256 +                if (rev == null)   // initialize reverse-forwarder
2257 +                    rev = new Node(MOVED, tab, null, null);
2258 +                if (tabAt(tab, i) != f || (f.hash & LOCKED) == 0)
2259 +                    continue;      // recheck before adding to list
2260 +                buffer[nbuffered++] = i;
2261 +                setTabAt(nextTab, i, rev);     // install place-holders
2262 +                setTabAt(nextTab, i + n, rev);
2263 +            }
2264 +
2265 +            if (bin > 0)
2266 +                i = --bin;
2267 +            else if (buffer != null && nbuffered > 0) {
2268 +                bin = -1;
2269 +                i = buffer[bufferIndex = --nbuffered];
2270 +            }
2271 +            else
2272 +                return nextTab;
2273          }
726        if (deletions != 0L)
727            counter.add(-deletions);
2274      }
2275  
2276      /**
2277 <     * Base class for key, value, and entry iterators, plus internal
2278 <     * implementations of public traversal-based methods, to avoid
733 <     * duplicating traversal code.
2277 >     * Splits a normal bin with list headed by e into lo and hi parts;
2278 >     * installs in given table.
2279       */
2280 <    class HashIterator {
2281 <        private Node next;          // the next entry to return
2282 <        private Node[] tab;         // current table; updated if resized
2283 <        private Node lastReturned;  // the last entry returned, for remove
2284 <        private Object nextVal;     // cached value of next
2285 <        private int index;          // index of bin to use next
2286 <        private int baseIndex;      // current index of initial table
2287 <        private final int baseSize; // initial table size
2280 >    private static void splitBin(Node[] nextTab, int i, Node e) {
2281 >        int bit = nextTab.length >>> 1; // bit to split on
2282 >        int runBit = e.hash & bit;
2283 >        Node lastRun = e, lo = null, hi = null;
2284 >        for (Node p = e.next; p != null; p = p.next) {
2285 >            int b = p.hash & bit;
2286 >            if (b != runBit) {
2287 >                runBit = b;
2288 >                lastRun = p;
2289 >            }
2290 >        }
2291 >        if (runBit == 0)
2292 >            lo = lastRun;
2293 >        else
2294 >            hi = lastRun;
2295 >        for (Node p = e; p != lastRun; p = p.next) {
2296 >            int ph = p.hash & HASH_BITS;
2297 >            Object pk = p.key, pv = p.val;
2298 >            if ((ph & bit) == 0)
2299 >                lo = new Node(ph, pk, pv, lo);
2300 >            else
2301 >                hi = new Node(ph, pk, pv, hi);
2302 >        }
2303 >        setTabAt(nextTab, i, lo);
2304 >        setTabAt(nextTab, i + bit, hi);
2305 >    }
2306  
2307 <        HashIterator() {
2308 <            Node[] t = tab = table;
2309 <            if (t == null)
2310 <                baseSize = 0;
2307 >    /**
2308 >     * Splits a tree bin into lo and hi parts; installs in given table.
2309 >     */
2310 >    private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2311 >        int bit = nextTab.length >>> 1;
2312 >        TreeBin lt = new TreeBin();
2313 >        TreeBin ht = new TreeBin();
2314 >        int lc = 0, hc = 0;
2315 >        for (Node e = t.first; e != null; e = e.next) {
2316 >            int h = e.hash & HASH_BITS;
2317 >            Object k = e.key, v = e.val;
2318 >            if ((h & bit) == 0) {
2319 >                ++lc;
2320 >                lt.putTreeNode(h, k, v);
2321 >            }
2322              else {
2323 <                baseSize = t.length;
2324 <                advance(null);
2323 >                ++hc;
2324 >                ht.putTreeNode(h, k, v);
2325              }
2326          }
2327 +        Node ln, hn; // throw away trees if too small
2328 +        if (lc <= (TREE_THRESHOLD >>> 1)) {
2329 +            ln = null;
2330 +            for (Node p = lt.first; p != null; p = p.next)
2331 +                ln = new Node(p.hash, p.key, p.val, ln);
2332 +        }
2333 +        else
2334 +            ln = new Node(MOVED, lt, null, null);
2335 +        setTabAt(nextTab, i, ln);
2336 +        if (hc <= (TREE_THRESHOLD >>> 1)) {
2337 +            hn = null;
2338 +            for (Node p = ht.first; p != null; p = p.next)
2339 +                hn = new Node(p.hash, p.key, p.val, hn);
2340 +        }
2341 +        else
2342 +            hn = new Node(MOVED, ht, null, null);
2343 +        setTabAt(nextTab, i + bit, hn);
2344 +    }
2345  
2346 <        public final boolean hasNext()         { return next != null; }
2347 <        public final boolean hasMoreElements() { return next != null; }
2348 <
2349 <        /**
2350 <         * Advances next.  Normally, iteration proceeds bin-by-bin
2351 <         * traversing lists.  However, if the table has been resized,
2352 <         * then all future steps must traverse both the bin at the
2353 <         * current index as well as at (index + baseSize); and so on
2354 <         * for further resizings. To paranoically cope with potential
2355 <         * (improper) sharing of iterators across threads, table reads
2356 <         * are bounds-checked.
2357 <         */
2358 <        final void advance(Node e) {
2359 <            for (;;) {
2360 <                Node[] t; int i; // for bounds checks
2361 <                if (e != null) {
2362 <                    Object ek = e.key, ev = e.val;
2363 <                    if (ev != null && ek != null) {
2364 <                        nextVal = ev;
2365 <                        next = e;
2366 <                        break;
2346 >    /**
2347 >     * Implementation for clear. Steps through each bin, removing all
2348 >     * nodes.
2349 >     */
2350 >    private final void internalClear() {
2351 >        long delta = 0L; // negative number of deletions
2352 >        int i = 0;
2353 >        Node[] tab = table;
2354 >        while (tab != null && i < tab.length) {
2355 >            int fh; Object fk;
2356 >            Node f = tabAt(tab, i);
2357 >            if (f == null)
2358 >                ++i;
2359 >            else if ((fh = f.hash) == MOVED) {
2360 >                if ((fk = f.key) instanceof TreeBin) {
2361 >                    TreeBin t = (TreeBin)fk;
2362 >                    t.acquire(0);
2363 >                    try {
2364 >                        if (tabAt(tab, i) == f) {
2365 >                            for (Node p = t.first; p != null; p = p.next) {
2366 >                                if (p.val != null) { // (currently always true)
2367 >                                    p.val = null;
2368 >                                    --delta;
2369 >                                }
2370 >                            }
2371 >                            t.first = null;
2372 >                            t.root = null;
2373 >                            ++i;
2374 >                        }
2375 >                    } finally {
2376 >                        t.release(0);
2377                      }
776                    e = e.next;
2378                  }
2379 <                else if (baseIndex < baseSize && (t = tab) != null &&
2380 <                         t.length > (i = index) && i >= 0) {
2381 <                    if ((e = tabAt(t, i)) != null && e.hash < 0) {
2382 <                        tab = (Node[])e.key;
2383 <                        e = null;
2379 >                else
2380 >                    tab = (Node[])fk;
2381 >            }
2382 >            else if ((fh & LOCKED) != 0) {
2383 >                counter.add(delta); // opportunistically update count
2384 >                delta = 0L;
2385 >                f.tryAwaitLock(tab, i);
2386 >            }
2387 >            else if (f.casHash(fh, fh | LOCKED)) {
2388 >                try {
2389 >                    if (tabAt(tab, i) == f) {
2390 >                        for (Node e = f; e != null; e = e.next) {
2391 >                            if (e.val != null) {  // (currently always true)
2392 >                                e.val = null;
2393 >                                --delta;
2394 >                            }
2395 >                        }
2396 >                        setTabAt(tab, i, null);
2397 >                        ++i;
2398 >                    }
2399 >                } finally {
2400 >                    if (!f.casHash(fh | LOCKED, fh)) {
2401 >                        f.hash = fh;
2402 >                        synchronized (f) { f.notifyAll(); };
2403                      }
784                    else if (i + baseSize < t.length)
785                        index += baseSize;    // visit forwarded upper slots
786                    else
787                        index = ++baseIndex;
788                }
789                else {
790                    next = null;
791                    break;
2404                  }
2405              }
2406          }
2407 +        if (delta != 0)
2408 +            counter.add(delta);
2409 +    }
2410  
2411 <        final Object nextKey() {
797 <            Node e = next;
798 <            if (e == null)
799 <                throw new NoSuchElementException();
800 <            Object k = e.key;
801 <            advance((lastReturned = e).next);
802 <            return k;
803 <        }
2411 >    /* ----------------Table Traversal -------------- */
2412  
2413 <        final Object nextValue() {
2414 <            Node e = next;
2415 <            if (e == null)
2416 <                throw new NoSuchElementException();
2417 <            Object v = nextVal;
2418 <            advance((lastReturned = e).next);
2419 <            return v;
2413 >    /**
2414 >     * Encapsulates traversal for methods such as containsValue; also
2415 >     * serves as a base class for other iterators and bulk tasks.
2416 >     *
2417 >     * At each step, the iterator snapshots the key ("nextKey") and
2418 >     * value ("nextVal") of a valid node (i.e., one that, at point of
2419 >     * snapshot, has a non-null user value). Because val fields can
2420 >     * change (including to null, indicating deletion), field nextVal
2421 >     * might not be accurate at point of use, but still maintains the
2422 >     * weak consistency property of holding a value that was once
2423 >     * valid. To support iterator.remove, the nextKey field is not
2424 >     * updated (nulled out) when the iterator cannot advance.
2425 >     *
2426 >     * Internal traversals directly access these fields, as in:
2427 >     * {@code while (it.advance() != null) { process(it.nextKey); }}
2428 >     *
2429 >     * Exported iterators must track whether the iterator has advanced
2430 >     * (in hasNext vs next) (by setting/checking/nulling field
2431 >     * nextVal), and then extract key, value, or key-value pairs as
2432 >     * return values of next().
2433 >     *
2434 >     * The iterator visits once each still-valid node that was
2435 >     * reachable upon iterator construction. It might miss some that
2436 >     * were added to a bin after the bin was visited, which is OK wrt
2437 >     * consistency guarantees. Maintaining this property in the face
2438 >     * of possible ongoing resizes requires a fair amount of
2439 >     * bookkeeping state that is difficult to optimize away amidst
2440 >     * volatile accesses.  Even so, traversal maintains reasonable
2441 >     * throughput.
2442 >     *
2443 >     * Normally, iteration proceeds bin-by-bin traversing lists.
2444 >     * However, if the table has been resized, then all future steps
2445 >     * must traverse both the bin at the current index as well as at
2446 >     * (index + baseSize); and so on for further resizings. To
2447 >     * paranoically cope with potential sharing by users of iterators
2448 >     * across threads, iteration terminates if a bounds checks fails
2449 >     * for a table read.
2450 >     *
2451 >     * This class extends ForkJoinTask to streamline parallel
2452 >     * iteration in bulk operations (see BulkTask). This adds only an
2453 >     * int of space overhead, which is close enough to negligible in
2454 >     * cases where it is not needed to not worry about it.  Because
2455 >     * ForkJoinTask is Serializable, but iterators need not be, we
2456 >     * need to add warning suppressions.
2457 >     */
2458 >    @SuppressWarnings("serial") static class Traverser<K,V,R> extends ForkJoinTask<R> {
2459 >        final ConcurrentHashMapV8<K, V> map;
2460 >        Node next;           // the next entry to use
2461 >        Object nextKey;      // cached key field of next
2462 >        Object nextVal;      // cached val field of next
2463 >        Node[] tab;          // current table; updated if resized
2464 >        int index;           // index of bin to use next
2465 >        int baseIndex;       // current index of initial table
2466 >        int baseLimit;       // index bound for initial table
2467 >        int baseSize;        // initial table size
2468 >
2469 >        /** Creates iterator for all entries in the table. */
2470 >        Traverser(ConcurrentHashMapV8<K, V> map) {
2471 >            this.map = map;
2472 >        }
2473 >
2474 >        /** Creates iterator for split() methods */
2475 >        Traverser(Traverser<K,V,?> it) {
2476 >            ConcurrentHashMapV8<K, V> m; Node[] t;
2477 >            if ((m = this.map = it.map) == null)
2478 >                t = null;
2479 >            else if ((t = it.tab) == null && // force parent tab initialization
2480 >                     (t = it.tab = m.table) != null)
2481 >                it.baseLimit = it.baseSize = t.length;
2482 >            this.tab = t;
2483 >            this.baseSize = it.baseSize;
2484 >            it.baseLimit = this.index = this.baseIndex =
2485 >                ((this.baseLimit = it.baseLimit) + it.baseIndex + 1) >>> 1;
2486          }
2487  
2488 <        final WriteThroughEntry nextEntry() {
2488 >        /**
2489 >         * Advances next; returns nextVal or null if terminated.
2490 >         * See above for explanation.
2491 >         */
2492 >        final Object advance() {
2493              Node e = next;
2494 <            if (e == null)
2495 <                throw new NoSuchElementException();
2496 <            WriteThroughEntry entry =
2497 <                new WriteThroughEntry(e.key, nextVal);
2498 <            advance((lastReturned = e).next);
2499 <            return entry;
2494 >            Object ev = null;
2495 >            outer: do {
2496 >                if (e != null)                  // advance past used/skipped node
2497 >                    e = e.next;
2498 >                while (e == null) {             // get to next non-null bin
2499 >                    ConcurrentHashMapV8<K, V> m;
2500 >                    Node[] t; int b, i, n; Object ek; // checks must use locals
2501 >                    if ((t = tab) != null)
2502 >                        n = t.length;
2503 >                    else if ((m = map) != null && (t = tab = m.table) != null)
2504 >                        n = baseLimit = baseSize = t.length;
2505 >                    else
2506 >                        break outer;
2507 >                    if ((b = baseIndex) >= baseLimit ||
2508 >                        (i = index) < 0 || i >= n)
2509 >                        break outer;
2510 >                    if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2511 >                        if ((ek = e.key) instanceof TreeBin)
2512 >                            e = ((TreeBin)ek).first;
2513 >                        else {
2514 >                            tab = (Node[])ek;
2515 >                            continue;           // restarts due to null val
2516 >                        }
2517 >                    }                           // visit upper slots if present
2518 >                    index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2519 >                }
2520 >                nextKey = e.key;
2521 >            } while ((ev = e.val) == null);    // skip deleted or special nodes
2522 >            next = e;
2523 >            return nextVal = ev;
2524          }
2525  
2526          public final void remove() {
2527 <            if (lastReturned == null)
2527 >            Object k = nextKey;
2528 >            if (k == null && (advance() == null || (k = nextKey) == null))
2529                  throw new IllegalStateException();
2530 <            ConcurrentHashMapV8.this.remove(lastReturned.key);
828 <            lastReturned = null;
2530 >            map.internalReplace(k, null, null);
2531          }
2532  
2533 <        /** Helper for serialization */
2534 <        final void writeEntries(java.io.ObjectOutputStream s)
833 <            throws java.io.IOException {
834 <            Node e;
835 <            while ((e = next) != null) {
836 <                s.writeObject(e.key);
837 <                s.writeObject(nextVal);
838 <                advance(e.next);
839 <            }
840 <        }
841 <
842 <        /** Helper for containsValue */
843 <        final boolean containsVal(Object value) {
844 <            if (value != null) {
845 <                Node e;
846 <                while ((e = next) != null) {
847 <                    Object v = nextVal;
848 <                    if (value == v || value.equals(v))
849 <                        return true;
850 <                    advance(e.next);
851 <                }
852 <            }
853 <            return false;
2533 >        public final boolean hasNext() {
2534 >            return nextVal != null || advance() != null;
2535          }
2536  
2537 <        /** Helper for Map.hashCode */
2538 <        final int mapHashCode() {
2539 <            int h = 0;
2540 <            Node e;
860 <            while ((e = next) != null) {
861 <                h += e.key.hashCode() ^ nextVal.hashCode();
862 <                advance(e.next);
863 <            }
864 <            return h;
865 <        }
866 <
867 <        /** Helper for Map.toString */
868 <        final String mapToString() {
869 <            Node e = next;
870 <            if (e == null)
871 <                return "{}";
872 <            StringBuilder sb = new StringBuilder();
873 <            sb.append('{');
874 <            for (;;) {
875 <                sb.append(e.key   == this ? "(this Map)" : e.key);
876 <                sb.append('=');
877 <                sb.append(nextVal == this ? "(this Map)" : nextVal);
878 <                advance(e.next);
879 <                if ((e = next) != null)
880 <                    sb.append(',').append(' ');
881 <                else
882 <                    return sb.append('}').toString();
883 <            }
884 <        }
2537 >        public final boolean hasMoreElements() { return hasNext(); }
2538 >        public final void setRawResult(Object x) { }
2539 >        public R getRawResult() { return null; }
2540 >        public boolean exec() { return true; }
2541      }
2542  
2543      /* ---------------- Public operations -------------- */
2544  
2545      /**
2546 <     * Creates a new, empty map with the specified initial
891 <     * capacity, load factor and concurrency level.
892 <     *
893 <     * @param initialCapacity the initial capacity. The implementation
894 <     * performs internal sizing to accommodate this many elements.
895 <     * @param loadFactor  the load factor threshold, used to control resizing.
896 <     * Resizing may be performed when the average number of elements per
897 <     * bin exceeds this threshold.
898 <     * @param concurrencyLevel the estimated number of concurrently
899 <     * updating threads. The implementation may use this value as
900 <     * a sizing hint.
901 <     * @throws IllegalArgumentException if the initial capacity is
902 <     * negative or the load factor or concurrencyLevel are
903 <     * nonpositive.
2546 >     * Creates a new, empty map with the default initial table size (16).
2547       */
2548 <    public ConcurrentHashMapV8(int initialCapacity,
906 <                             float loadFactor, int concurrencyLevel) {
907 <        if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0)
908 <            throw new IllegalArgumentException();
909 <        this.initCap = initialCapacity;
910 <        this.loadFactor = loadFactor;
2548 >    public ConcurrentHashMapV8() {
2549          this.counter = new LongAdder();
2550      }
2551  
2552      /**
2553 <     * Creates a new, empty map with the specified initial capacity
2554 <     * and load factor and with the default concurrencyLevel (16).
2553 >     * Creates a new, empty map with an initial table size
2554 >     * accommodating the specified number of elements without the need
2555 >     * to dynamically resize.
2556       *
2557       * @param initialCapacity The implementation performs internal
2558       * sizing to accommodate this many elements.
2559 <     * @param loadFactor  the load factor threshold, used to control resizing.
2560 <     * Resizing may be performed when the average number of elements per
2561 <     * bin exceeds this threshold.
2559 >     * @throws IllegalArgumentException if the initial capacity of
2560 >     * elements is negative
2561 >     */
2562 >    public ConcurrentHashMapV8(int initialCapacity) {
2563 >        if (initialCapacity < 0)
2564 >            throw new IllegalArgumentException();
2565 >        int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
2566 >                   MAXIMUM_CAPACITY :
2567 >                   tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
2568 >        this.counter = new LongAdder();
2569 >        this.sizeCtl = cap;
2570 >    }
2571 >
2572 >    /**
2573 >     * Creates a new map with the same mappings as the given map.
2574 >     *
2575 >     * @param m the map
2576 >     */
2577 >    public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
2578 >        this.counter = new LongAdder();
2579 >        this.sizeCtl = DEFAULT_CAPACITY;
2580 >        internalPutAll(m);
2581 >    }
2582 >
2583 >    /**
2584 >     * Creates a new, empty map with an initial table size based on
2585 >     * the given number of elements ({@code initialCapacity}) and
2586 >     * initial table density ({@code loadFactor}).
2587 >     *
2588 >     * @param initialCapacity the initial capacity. The implementation
2589 >     * performs internal sizing to accommodate this many elements,
2590 >     * given the specified load factor.
2591 >     * @param loadFactor the load factor (table density) for
2592 >     * establishing the initial table size
2593       * @throws IllegalArgumentException if the initial capacity of
2594       * elements is negative or the load factor is nonpositive
2595       *
2596       * @since 1.6
2597       */
2598      public ConcurrentHashMapV8(int initialCapacity, float loadFactor) {
2599 <        this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL);
2599 >        this(initialCapacity, loadFactor, 1);
2600      }
2601  
2602      /**
2603 <     * Creates a new, empty map with the specified initial capacity,
2604 <     * and with default load factor (0.75) and concurrencyLevel (16).
2603 >     * Creates a new, empty map with an initial table size based on
2604 >     * the given number of elements ({@code initialCapacity}), table
2605 >     * density ({@code loadFactor}), and number of concurrently
2606 >     * updating threads ({@code concurrencyLevel}).
2607       *
2608       * @param initialCapacity the initial capacity. The implementation
2609 <     * performs internal sizing to accommodate this many elements.
2610 <     * @throws IllegalArgumentException if the initial capacity of
2611 <     * elements is negative.
2609 >     * performs internal sizing to accommodate this many elements,
2610 >     * given the specified load factor.
2611 >     * @param loadFactor the load factor (table density) for
2612 >     * establishing the initial table size
2613 >     * @param concurrencyLevel the estimated number of concurrently
2614 >     * updating threads. The implementation may use this value as
2615 >     * a sizing hint.
2616 >     * @throws IllegalArgumentException if the initial capacity is
2617 >     * negative or the load factor or concurrencyLevel are
2618 >     * nonpositive
2619       */
2620 <    public ConcurrentHashMapV8(int initialCapacity) {
2621 <        this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
2620 >    public ConcurrentHashMapV8(int initialCapacity,
2621 >                               float loadFactor, int concurrencyLevel) {
2622 >        if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
2623 >            throw new IllegalArgumentException();
2624 >        if (initialCapacity < concurrencyLevel)   // Use at least as many bins
2625 >            initialCapacity = concurrencyLevel;   // as estimated threads
2626 >        long size = (long)(1.0 + (long)initialCapacity / loadFactor);
2627 >        int cap = (size >= (long)MAXIMUM_CAPACITY) ?
2628 >            MAXIMUM_CAPACITY : tableSizeFor((int)size);
2629 >        this.counter = new LongAdder();
2630 >        this.sizeCtl = cap;
2631      }
2632  
2633      /**
2634 <     * Creates a new, empty map with a default initial capacity (16),
2635 <     * load factor (0.75) and concurrencyLevel (16).
2634 >     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2635 >     * from the given type to {@code Boolean.TRUE}.
2636 >     *
2637 >     * @return the new set
2638       */
2639 <    public ConcurrentHashMapV8() {
2640 <        this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
2639 >    public static <K> KeySetView<K,Boolean> newKeySet() {
2640 >        return new KeySetView<K,Boolean>(new ConcurrentHashMapV8<K,Boolean>(),
2641 >                                      Boolean.TRUE);
2642      }
2643  
2644      /**
2645 <     * Creates a new map with the same mappings as the given map.
2646 <     * The map is created with a capacity of 1.5 times the number
956 <     * of mappings in the given map or 16 (whichever is greater),
957 <     * and a default load factor (0.75) and concurrencyLevel (16).
2645 >     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2646 >     * from the given type to {@code Boolean.TRUE}.
2647       *
2648 <     * @param m the map
2648 >     * @param initialCapacity The implementation performs internal
2649 >     * sizing to accommodate this many elements.
2650 >     * @throws IllegalArgumentException if the initial capacity of
2651 >     * elements is negative
2652 >     * @return the new set
2653       */
2654 <    public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
2655 <        this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
2656 <        if (m == null)
964 <            throw new NullPointerException();
965 <        internalPutAll(m);
2654 >    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2655 >        return new KeySetView<K,Boolean>(new ConcurrentHashMapV8<K,Boolean>(initialCapacity),
2656 >                                      Boolean.TRUE);
2657      }
2658  
2659      /**
2660 <     * Returns {@code true} if this map contains no key-value mappings.
970 <     *
971 <     * @return {@code true} if this map contains no key-value mappings
2660 >     * {@inheritDoc}
2661       */
2662      public boolean isEmpty() {
2663          return counter.sum() <= 0L; // ignore transient negative values
2664      }
2665  
2666      /**
2667 <     * Returns the number of key-value mappings in this map.  If the
979 <     * map contains more than {@code Integer.MAX_VALUE} elements, returns
980 <     * {@code Integer.MAX_VALUE}.
981 <     *
982 <     * @return the number of key-value mappings in this map
2667 >     * {@inheritDoc}
2668       */
2669      public int size() {
2670          long n = counter.sum();
2671 <        return n <= 0L? 0 : n >= Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)n;
2671 >        return ((n < 0L) ? 0 :
2672 >                (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
2673 >                (int)n);
2674 >    }
2675 >
2676 >    /**
2677 >     * Returns the number of mappings. This method should be used
2678 >     * instead of {@link #size} because a ConcurrentHashMapV8 may
2679 >     * contain more mappings than can be represented as an int. The
2680 >     * value returned is a snapshot; the actual count may differ if
2681 >     * there are ongoing concurrent insertions or removals.
2682 >     *
2683 >     * @return the number of mappings
2684 >     */
2685 >    public long mappingCount() {
2686 >        long n = counter.sum();
2687 >        return (n < 0L) ? 0L : n; // ignore transient negative values
2688      }
2689  
2690      /**
# Line 997 | Line 2698 | public class ConcurrentHashMapV8<K, V>
2698       *
2699       * @throws NullPointerException if the specified key is null
2700       */
2701 <    @SuppressWarnings("unchecked")
1001 <    public V get(Object key) {
2701 >    @SuppressWarnings("unchecked") public V get(Object key) {
2702          if (key == null)
2703              throw new NullPointerException();
2704          return (V)internalGet(key);
2705      }
2706  
2707      /**
2708 +     * Returns the value to which the specified key is mapped,
2709 +     * or the given defaultValue if this map contains no mapping for the key.
2710 +     *
2711 +     * @param key the key
2712 +     * @param defaultValue the value to return if this map contains
2713 +     * no mapping for the given key
2714 +     * @return the mapping for the key, if present; else the defaultValue
2715 +     * @throws NullPointerException if the specified key is null
2716 +     */
2717 +    @SuppressWarnings("unchecked") public V getValueOrDefault(Object key, V defaultValue) {
2718 +        if (key == null)
2719 +            throw new NullPointerException();
2720 +        V v = (V) internalGet(key);
2721 +        return v == null ? defaultValue : v;
2722 +    }
2723 +
2724 +    /**
2725       * Tests if the specified object is a key in this table.
2726       *
2727       * @param  key   possible key
2728       * @return {@code true} if and only if the specified object
2729       *         is a key in this table, as determined by the
2730 <     *         {@code equals} method; {@code false} otherwise.
2730 >     *         {@code equals} method; {@code false} otherwise
2731       * @throws NullPointerException if the specified key is null
2732       */
2733      public boolean containsKey(Object key) {
# Line 1021 | Line 2738 | public class ConcurrentHashMapV8<K, V>
2738  
2739      /**
2740       * Returns {@code true} if this map maps one or more keys to the
2741 <     * specified value. Note: This method requires a full internal
2742 <     * traversal of the hash table, and so is much slower than
1026 <     * method {@code containsKey}.
2741 >     * specified value. Note: This method may require a full traversal
2742 >     * of the map, and is much slower than method {@code containsKey}.
2743       *
2744       * @param value value whose presence in this map is to be tested
2745       * @return {@code true} if this map maps one or more keys to the
# Line 1033 | Line 2749 | public class ConcurrentHashMapV8<K, V>
2749      public boolean containsValue(Object value) {
2750          if (value == null)
2751              throw new NullPointerException();
2752 <        return new HashIterator().containsVal(value);
2752 >        Object v;
2753 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2754 >        while ((v = it.advance()) != null) {
2755 >            if (v == value || value.equals(v))
2756 >                return true;
2757 >        }
2758 >        return false;
2759      }
2760  
2761      /**
# Line 1068 | Line 2790 | public class ConcurrentHashMapV8<K, V>
2790       *         {@code null} if there was no mapping for {@code key}
2791       * @throws NullPointerException if the specified key or value is null
2792       */
2793 <    @SuppressWarnings("unchecked")
1072 <    public V put(K key, V value) {
2793 >    @SuppressWarnings("unchecked") public V put(K key, V value) {
2794          if (key == null || value == null)
2795              throw new NullPointerException();
2796 <        return (V)internalPut(key, value, true);
2796 >        return (V)internalPut(key, value);
2797      }
2798  
2799      /**
# Line 1082 | Line 2803 | public class ConcurrentHashMapV8<K, V>
2803       *         or {@code null} if there was no mapping for the key
2804       * @throws NullPointerException if the specified key or value is null
2805       */
2806 <    @SuppressWarnings("unchecked")
1086 <    public V putIfAbsent(K key, V value) {
2806 >    @SuppressWarnings("unchecked") public V putIfAbsent(K key, V value) {
2807          if (key == null || value == null)
2808              throw new NullPointerException();
2809 <        return (V)internalPut(key, value, false);
2809 >        return (V)internalPutIfAbsent(key, value);
2810      }
2811  
2812      /**
# Line 1097 | Line 2817 | public class ConcurrentHashMapV8<K, V>
2817       * @param m mappings to be stored in this map
2818       */
2819      public void putAll(Map<? extends K, ? extends V> m) {
1100        if (m == null)
1101            throw new NullPointerException();
2820          internalPutAll(m);
2821      }
2822  
2823      /**
2824       * If the specified key is not already associated with a value,
2825 <     * computes its value using the given mappingFunction, and if
2826 <     * non-null, enters it into the map.  This is equivalent to
2827 <     *
2828 <     * <pre>
2829 <     *   if (map.containsKey(key))
2830 <     *       return map.get(key);
2831 <     *   value = mappingFunction.map(key);
2832 <     *   if (value != null)
2833 <     *      map.put(key, value);
2834 <     *   return value;
2835 <     * </pre>
2836 <     *
2837 <     * except that the action is performed atomically.  Some attempted
2838 <     * operations on this map by other threads may be blocked while
2839 <     * computation is in progress, so the computation should be short
2840 <     * and simple, and must not attempt to update any other mappings
2841 <     * of this Map. The most common usage is to construct a new object
2842 <     * serving as an initial mapped value, or memoized result.
2825 >     * computes its value using the given mappingFunction and enters
2826 >     * it into the map unless null.  This is equivalent to
2827 >     * <pre> {@code
2828 >     * if (map.containsKey(key))
2829 >     *   return map.get(key);
2830 >     * value = mappingFunction.apply(key);
2831 >     * if (value != null)
2832 >     *   map.put(key, value);
2833 >     * return value;}</pre>
2834 >     *
2835 >     * except that the action is performed atomically.  If the
2836 >     * function returns {@code null} no mapping is recorded. If the
2837 >     * function itself throws an (unchecked) exception, the exception
2838 >     * is rethrown to its caller, and no mapping is recorded.  Some
2839 >     * attempted update operations on this map by other threads may be
2840 >     * blocked while computation is in progress, so the computation
2841 >     * should be short and simple, and must not attempt to update any
2842 >     * other mappings of this Map. The most appropriate usage is to
2843 >     * construct a new object serving as an initial mapped value, or
2844 >     * memoized result, as in:
2845 >     *
2846 >     *  <pre> {@code
2847 >     * map.computeIfAbsent(key, new Fun<K, V>() {
2848 >     *   public V map(K k) { return new Value(f(k)); }});}</pre>
2849       *
2850       * @param key key with which the specified value is to be associated
2851       * @param mappingFunction the function to compute a value
2852       * @return the current (existing or computed) value associated with
2853 <     *         the specified key, or {@code null} if the computation
1130 <     *         returned {@code null}.
2853 >     *         the specified key, or null if the computed value is null
2854       * @throws NullPointerException if the specified key or mappingFunction
2855 <     *         is null,
2855 >     *         is null
2856 >     * @throws IllegalStateException if the computation detectably
2857 >     *         attempts a recursive update to this map that would
2858 >     *         otherwise never complete
2859       * @throws RuntimeException or Error if the mappingFunction does so,
2860 <     *         in which case the mapping is left unestablished.
2860 >     *         in which case the mapping is left unestablished
2861       */
2862 <    public V computeIfAbsent(K key, MappingFunction<? super K, ? extends V> mappingFunction) {
2862 >    @SuppressWarnings("unchecked") public V computeIfAbsent
2863 >        (K key, Fun<? super K, ? extends V> mappingFunction) {
2864          if (key == null || mappingFunction == null)
2865              throw new NullPointerException();
2866 <        return internalCompute(key, mappingFunction, false);
2866 >        return (V)internalComputeIfAbsent(key, mappingFunction);
2867      }
2868  
2869      /**
2870 <     * Computes the value associated with he given key using the given
2871 <     * mappingFunction, and if non-null, enters it into the map.  This
2872 <     * is equivalent to
2870 >     * If the given key is present, computes a new mapping value given a key and
2871 >     * its current mapped value. This is equivalent to
2872 >     *  <pre> {@code
2873 >     *   if (map.containsKey(key)) {
2874 >     *     value = remappingFunction.apply(key, map.get(key));
2875 >     *     if (value != null)
2876 >     *       map.put(key, value);
2877 >     *     else
2878 >     *       map.remove(key);
2879 >     *   }
2880 >     * }</pre>
2881 >     *
2882 >     * except that the action is performed atomically.  If the
2883 >     * function returns {@code null}, the mapping is removed.  If the
2884 >     * function itself throws an (unchecked) exception, the exception
2885 >     * is rethrown to its caller, and the current mapping is left
2886 >     * unchanged.  Some attempted update operations on this map by
2887 >     * other threads may be blocked while computation is in progress,
2888 >     * so the computation should be short and simple, and must not
2889 >     * attempt to update any other mappings of this Map. For example,
2890 >     * to either create or append new messages to a value mapping:
2891       *
2892 <     * <pre>
2893 <     *   value = mappingFunction.map(key);
2892 >     * @param key key with which the specified value is to be associated
2893 >     * @param remappingFunction the function to compute a value
2894 >     * @return the new value associated with the specified key, or null if none
2895 >     * @throws NullPointerException if the specified key or remappingFunction
2896 >     *         is null
2897 >     * @throws IllegalStateException if the computation detectably
2898 >     *         attempts a recursive update to this map that would
2899 >     *         otherwise never complete
2900 >     * @throws RuntimeException or Error if the remappingFunction does so,
2901 >     *         in which case the mapping is unchanged
2902 >     */
2903 >    @SuppressWarnings("unchecked") public V computeIfPresent
2904 >        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2905 >        if (key == null || remappingFunction == null)
2906 >            throw new NullPointerException();
2907 >        return (V)internalCompute(key, true, remappingFunction);
2908 >    }
2909 >
2910 >    /**
2911 >     * Computes a new mapping value given a key and
2912 >     * its current mapped value (or {@code null} if there is no current
2913 >     * mapping). This is equivalent to
2914 >     *  <pre> {@code
2915 >     *   value = remappingFunction.apply(key, map.get(key));
2916       *   if (value != null)
2917 <     *      map.put(key, value);
2917 >     *     map.put(key, value);
2918       *   else
2919 <     *      return map.get(key);
2920 <     * </pre>
2919 >     *     map.remove(key);
2920 >     * }</pre>
2921       *
2922 <     * except that the action is performed atomically.  Some attempted
2923 <     * operations on this map by other threads may be blocked while
2924 <     * computation is in progress, so the computation should be short
2925 <     * and simple, and must not attempt to update any other mappings
2926 <     * of this Map.
2922 >     * except that the action is performed atomically.  If the
2923 >     * function returns {@code null}, the mapping is removed.  If the
2924 >     * function itself throws an (unchecked) exception, the exception
2925 >     * is rethrown to its caller, and the current mapping is left
2926 >     * unchanged.  Some attempted update operations on this map by
2927 >     * other threads may be blocked while computation is in progress,
2928 >     * so the computation should be short and simple, and must not
2929 >     * attempt to update any other mappings of this Map. For example,
2930 >     * to either create or append new messages to a value mapping:
2931 >     *
2932 >     * <pre> {@code
2933 >     * Map<Key, String> map = ...;
2934 >     * final String msg = ...;
2935 >     * map.compute(key, new BiFun<Key, String, String>() {
2936 >     *   public String apply(Key k, String v) {
2937 >     *    return (v == null) ? msg : v + msg;});}}</pre>
2938       *
2939       * @param key key with which the specified value is to be associated
2940 <     * @param mappingFunction the function to compute a value
2941 <     * @return the current value associated with
2942 <     *         the specified key, or {@code null} if the computation
2943 <     *         returned {@code null} and the value was not otherwise present.
2944 <     * @throws NullPointerException if the specified key or mappingFunction
2945 <     *         is null,
2946 <     * @throws RuntimeException or Error if the mappingFunction does so,
2947 <     *         in which case the mapping is unchanged.
2948 <     */
2949 <    public V compute(K key, MappingFunction<? super K, ? extends V> mappingFunction) {
2950 <        if (key == null || mappingFunction == null)
2940 >     * @param remappingFunction the function to compute a value
2941 >     * @return the new value associated with the specified key, or null if none
2942 >     * @throws NullPointerException if the specified key or remappingFunction
2943 >     *         is null
2944 >     * @throws IllegalStateException if the computation detectably
2945 >     *         attempts a recursive update to this map that would
2946 >     *         otherwise never complete
2947 >     * @throws RuntimeException or Error if the remappingFunction does so,
2948 >     *         in which case the mapping is unchanged
2949 >     */
2950 >    @SuppressWarnings("unchecked") public V compute
2951 >        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2952 >        if (key == null || remappingFunction == null)
2953              throw new NullPointerException();
2954 <        return internalCompute(key, mappingFunction, true);
2954 >        return (V)internalCompute(key, false, remappingFunction);
2955 >    }
2956 >
2957 >    /**
2958 >     * If the specified key is not already associated
2959 >     * with a value, associate it with the given value.
2960 >     * Otherwise, replace the value with the results of
2961 >     * the given remapping function. This is equivalent to:
2962 >     *  <pre> {@code
2963 >     *   if (!map.containsKey(key))
2964 >     *     map.put(value);
2965 >     *   else {
2966 >     *     newValue = remappingFunction.apply(map.get(key), value);
2967 >     *     if (value != null)
2968 >     *       map.put(key, value);
2969 >     *     else
2970 >     *       map.remove(key);
2971 >     *   }
2972 >     * }</pre>
2973 >     * except that the action is performed atomically.  If the
2974 >     * function returns {@code null}, the mapping is removed.  If the
2975 >     * function itself throws an (unchecked) exception, the exception
2976 >     * is rethrown to its caller, and the current mapping is left
2977 >     * unchanged.  Some attempted update operations on this map by
2978 >     * other threads may be blocked while computation is in progress,
2979 >     * so the computation should be short and simple, and must not
2980 >     * attempt to update any other mappings of this Map.
2981 >     */
2982 >    @SuppressWarnings("unchecked") public V merge
2983 >        (K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2984 >        if (key == null || value == null || remappingFunction == null)
2985 >            throw new NullPointerException();
2986 >        return (V)internalMerge(key, value, remappingFunction);
2987      }
2988  
2989      /**
# Line 1183 | Line 2995 | public class ConcurrentHashMapV8<K, V>
2995       *         {@code null} if there was no mapping for {@code key}
2996       * @throws NullPointerException if the specified key is null
2997       */
2998 <    @SuppressWarnings("unchecked")
1187 <    public V remove(Object key) {
2998 >    @SuppressWarnings("unchecked") public V remove(Object key) {
2999          if (key == null)
3000              throw new NullPointerException();
3001          return (V)internalReplace(key, null, null);
# Line 1221 | Line 3032 | public class ConcurrentHashMapV8<K, V>
3032       *         or {@code null} if there was no mapping for the key
3033       * @throws NullPointerException if the specified key or value is null
3034       */
3035 <    @SuppressWarnings("unchecked")
1225 <    public V replace(K key, V value) {
3035 >    @SuppressWarnings("unchecked") public V replace(K key, V value) {
3036          if (key == null || value == null)
3037              throw new NullPointerException();
3038          return (V)internalReplace(key, value, null);
# Line 1238 | Line 3048 | public class ConcurrentHashMapV8<K, V>
3048      /**
3049       * Returns a {@link Set} view of the keys contained in this map.
3050       * The set is backed by the map, so changes to the map are
3051 <     * reflected in the set, and vice-versa.  The set supports element
1242 <     * removal, which removes the corresponding mapping from this map,
1243 <     * via the {@code Iterator.remove}, {@code Set.remove},
1244 <     * {@code removeAll}, {@code retainAll}, and {@code clear}
1245 <     * operations.  It does not support the {@code add} or
1246 <     * {@code addAll} operations.
3051 >     * reflected in the set, and vice-versa.
3052       *
3053 <     * <p>The view's {@code iterator} is a "weakly consistent" iterator
1249 <     * that will never throw {@link ConcurrentModificationException},
1250 <     * and guarantees to traverse elements as they existed upon
1251 <     * construction of the iterator, and may (but is not guaranteed to)
1252 <     * reflect any modifications subsequent to construction.
3053 >     * @return the set view
3054       */
3055 <    public Set<K> keySet() {
3056 <        Set<K> ks = keySet;
3057 <        return (ks != null) ? ks : (keySet = new KeySet());
3055 >    public KeySetView<K,V> keySet() {
3056 >        KeySetView<K,V> ks = keySet;
3057 >        return (ks != null) ? ks : (keySet = new KeySetView<K,V>(this, null));
3058 >    }
3059 >
3060 >    /**
3061 >     * Returns a {@link Set} view of the keys in this map, using the
3062 >     * given common mapped value for any additions (i.e., {@link
3063 >     * Collection#add} and {@link Collection#addAll}). This is of
3064 >     * course only appropriate if it is acceptable to use the same
3065 >     * value for all additions from this view.
3066 >     *
3067 >     * @param mappedValue the mapped value to use for any
3068 >     * additions.
3069 >     * @return the set view
3070 >     * @throws NullPointerException if the mappedValue is null
3071 >     */
3072 >    public KeySetView<K,V> keySet(V mappedValue) {
3073 >        if (mappedValue == null)
3074 >            throw new NullPointerException();
3075 >        return new KeySetView<K,V>(this, mappedValue);
3076      }
3077  
3078      /**
# Line 1273 | Line 3092 | public class ConcurrentHashMapV8<K, V>
3092       * reflect any modifications subsequent to construction.
3093       */
3094      public Collection<V> values() {
3095 <        Collection<V> vs = values;
3096 <        return (vs != null) ? vs : (values = new Values());
3095 >        Values<K,V> vs = values;
3096 >        return (vs != null) ? vs : (values = new Values<K,V>(this));
3097      }
3098  
3099      /**
# Line 1294 | Line 3113 | public class ConcurrentHashMapV8<K, V>
3113       * reflect any modifications subsequent to construction.
3114       */
3115      public Set<Map.Entry<K,V>> entrySet() {
3116 <        Set<Map.Entry<K,V>> es = entrySet;
3117 <        return (es != null) ? es : (entrySet = new EntrySet());
3116 >        EntrySet<K,V> es = entrySet;
3117 >        return (es != null) ? es : (entrySet = new EntrySet<K,V>(this));
3118      }
3119  
3120      /**
# Line 1305 | Line 3124 | public class ConcurrentHashMapV8<K, V>
3124       * @see #keySet()
3125       */
3126      public Enumeration<K> keys() {
3127 <        return new KeyIterator();
3127 >        return new KeyIterator<K,V>(this);
3128      }
3129  
3130      /**
# Line 1315 | Line 3134 | public class ConcurrentHashMapV8<K, V>
3134       * @see #values()
3135       */
3136      public Enumeration<V> elements() {
3137 <        return new ValueIterator();
3137 >        return new ValueIterator<K,V>(this);
3138 >    }
3139 >
3140 >    /**
3141 >     * Returns a partitionable iterator of the keys in this map.
3142 >     *
3143 >     * @return a partitionable iterator of the keys in this map
3144 >     */
3145 >    public Spliterator<K> keySpliterator() {
3146 >        return new KeyIterator<K,V>(this);
3147 >    }
3148 >
3149 >    /**
3150 >     * Returns a partitionable iterator of the values in this map.
3151 >     *
3152 >     * @return a partitionable iterator of the values in this map
3153 >     */
3154 >    public Spliterator<V> valueSpliterator() {
3155 >        return new ValueIterator<K,V>(this);
3156 >    }
3157 >
3158 >    /**
3159 >     * Returns a partitionable iterator of the entries in this map.
3160 >     *
3161 >     * @return a partitionable iterator of the entries in this map
3162 >     */
3163 >    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
3164 >        return new EntryIterator<K,V>(this);
3165      }
3166  
3167      /**
# Line 1326 | Line 3172 | public class ConcurrentHashMapV8<K, V>
3172       * @return the hash code value for this map
3173       */
3174      public int hashCode() {
3175 <        return new HashIterator().mapHashCode();
3175 >        int h = 0;
3176 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3177 >        Object v;
3178 >        while ((v = it.advance()) != null) {
3179 >            h += it.nextKey.hashCode() ^ v.hashCode();
3180 >        }
3181 >        return h;
3182      }
3183  
3184      /**
# Line 1341 | Line 3193 | public class ConcurrentHashMapV8<K, V>
3193       * @return a string representation of this map
3194       */
3195      public String toString() {
3196 <        return new HashIterator().mapToString();
3196 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3197 >        StringBuilder sb = new StringBuilder();
3198 >        sb.append('{');
3199 >        Object v;
3200 >        if ((v = it.advance()) != null) {
3201 >            for (;;) {
3202 >                Object k = it.nextKey;
3203 >                sb.append(k == this ? "(this Map)" : k);
3204 >                sb.append('=');
3205 >                sb.append(v == this ? "(this Map)" : v);
3206 >                if ((v = it.advance()) == null)
3207 >                    break;
3208 >                sb.append(',').append(' ');
3209 >            }
3210 >        }
3211 >        return sb.append('}').toString();
3212      }
3213  
3214      /**
# Line 1355 | Line 3222 | public class ConcurrentHashMapV8<K, V>
3222       * @return {@code true} if the specified object is equal to this map
3223       */
3224      public boolean equals(Object o) {
3225 <        if (o == this)
3226 <            return true;
3227 <        if (!(o instanceof Map))
3228 <            return false;
3229 <        Map<?,?> m = (Map<?,?>) o;
3230 <        try {
3231 <            for (Map.Entry<K,V> e : this.entrySet())
3232 <                if (! e.getValue().equals(m.get(e.getKey())))
3225 >        if (o != this) {
3226 >            if (!(o instanceof Map))
3227 >                return false;
3228 >            Map<?,?> m = (Map<?,?>) o;
3229 >            Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3230 >            Object val;
3231 >            while ((val = it.advance()) != null) {
3232 >                Object v = m.get(it.nextKey);
3233 >                if (v == null || (v != val && !v.equals(val)))
3234                      return false;
3235 +            }
3236              for (Map.Entry<?,?> e : m.entrySet()) {
3237 <                Object k = e.getKey();
3238 <                Object v = e.getValue();
3239 <                if (k == null || v == null || !v.equals(get(k)))
3237 >                Object mk, mv, v;
3238 >                if ((mk = e.getKey()) == null ||
3239 >                    (mv = e.getValue()) == null ||
3240 >                    (v = internalGet(mk)) == null ||
3241 >                    (mv != v && !mv.equals(v)))
3242                      return false;
3243              }
3244 <            return true;
3245 <        } catch (ClassCastException unused) {
3246 <            return false;
3247 <        } catch (NullPointerException unused) {
3248 <            return false;
3244 >        }
3245 >        return true;
3246 >    }
3247 >
3248 >    /* ----------------Iterators -------------- */
3249 >
3250 >    @SuppressWarnings("serial") static final class KeyIterator<K,V> extends Traverser<K,V,Object>
3251 >        implements Spliterator<K>, Enumeration<K> {
3252 >        KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3253 >        KeyIterator(Traverser<K,V,Object> it) {
3254 >            super(it);
3255 >        }
3256 >        public KeyIterator<K,V> split() {
3257 >            if (nextKey != null)
3258 >                throw new IllegalStateException();
3259 >            return new KeyIterator<K,V>(this);
3260 >        }
3261 >        @SuppressWarnings("unchecked") public final K next() {
3262 >            if (nextVal == null && advance() == null)
3263 >                throw new NoSuchElementException();
3264 >            Object k = nextKey;
3265 >            nextVal = null;
3266 >            return (K) k;
3267 >        }
3268 >
3269 >        public final K nextElement() { return next(); }
3270 >    }
3271 >
3272 >    @SuppressWarnings("serial") static final class ValueIterator<K,V> extends Traverser<K,V,Object>
3273 >        implements Spliterator<V>, Enumeration<V> {
3274 >        ValueIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3275 >        ValueIterator(Traverser<K,V,Object> it) {
3276 >            super(it);
3277 >        }
3278 >        public ValueIterator<K,V> split() {
3279 >            if (nextKey != null)
3280 >                throw new IllegalStateException();
3281 >            return new ValueIterator<K,V>(this);
3282 >        }
3283 >
3284 >        @SuppressWarnings("unchecked") public final V next() {
3285 >            Object v;
3286 >            if ((v = nextVal) == null && (v = advance()) == null)
3287 >                throw new NoSuchElementException();
3288 >            nextVal = null;
3289 >            return (V) v;
3290 >        }
3291 >
3292 >        public final V nextElement() { return next(); }
3293 >    }
3294 >
3295 >    @SuppressWarnings("serial") static final class EntryIterator<K,V> extends Traverser<K,V,Object>
3296 >        implements Spliterator<Map.Entry<K,V>> {
3297 >        EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3298 >        EntryIterator(Traverser<K,V,Object> it) {
3299 >            super(it);
3300 >        }
3301 >        public EntryIterator<K,V> split() {
3302 >            if (nextKey != null)
3303 >                throw new IllegalStateException();
3304 >            return new EntryIterator<K,V>(this);
3305 >        }
3306 >
3307 >        @SuppressWarnings("unchecked") public final Map.Entry<K,V> next() {
3308 >            Object v;
3309 >            if ((v = nextVal) == null && (v = advance()) == null)
3310 >                throw new NoSuchElementException();
3311 >            Object k = nextKey;
3312 >            nextVal = null;
3313 >            return new MapEntry<K,V>((K)k, (V)v, map);
3314          }
3315      }
3316  
3317      /**
3318 <     * Custom Entry class used by EntryIterator.next(), that relays
1383 <     * setValue changes to the underlying map.
3318 >     * Exported Entry for iterators
3319       */
3320 <    final class WriteThroughEntry extends AbstractMap.SimpleEntry<K,V> {
3321 <        @SuppressWarnings("unchecked")
3322 <        WriteThroughEntry(Object k, Object v) {
3323 <            super((K)k, (V)v);
3320 >    static final class MapEntry<K,V> implements Map.Entry<K, V> {
3321 >        final K key; // non-null
3322 >        V val;       // non-null
3323 >        final ConcurrentHashMapV8<K, V> map;
3324 >        MapEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
3325 >            this.key = key;
3326 >            this.val = val;
3327 >            this.map = map;
3328 >        }
3329 >        public final K getKey()       { return key; }
3330 >        public final V getValue()     { return val; }
3331 >        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
3332 >        public final String toString(){ return key + "=" + val; }
3333 >
3334 >        public final boolean equals(Object o) {
3335 >            Object k, v; Map.Entry<?,?> e;
3336 >            return ((o instanceof Map.Entry) &&
3337 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3338 >                    (v = e.getValue()) != null &&
3339 >                    (k == key || k.equals(key)) &&
3340 >                    (v == val || v.equals(val)));
3341          }
3342  
3343          /**
3344           * Sets our entry's value and writes through to the map. The
3345 <         * value to return is somewhat arbitrary here. Since a
3346 <         * WriteThroughEntry does not necessarily track asynchronous
3347 <         * changes, the most recent "previous" value could be
3348 <         * different from what we return (or could even have been
3349 <         * removed in which case the put will re-establish). We do not
1398 <         * and cannot guarantee more.
3345 >         * value to return is somewhat arbitrary here. Since we do not
3346 >         * necessarily track asynchronous changes, the most recent
3347 >         * "previous" value could be different from what we return (or
3348 >         * could even have been removed in which case the put will
3349 >         * re-establish). We do not and cannot guarantee more.
3350           */
3351 <        public V setValue(V value) {
3351 >        public final V setValue(V value) {
3352              if (value == null) throw new NullPointerException();
3353 <            V v = super.setValue(value);
3354 <            ConcurrentHashMapV8.this.put(getKey(), value);
3353 >            V v = val;
3354 >            val = value;
3355 >            map.put(key, value);
3356              return v;
3357          }
3358      }
3359  
3360 <    final class KeyIterator extends HashIterator
1409 <        implements Iterator<K>, Enumeration<K> {
1410 <        @SuppressWarnings("unchecked")
1411 <        public final K next()        { return (K)super.nextKey(); }
1412 <        @SuppressWarnings("unchecked")
1413 <        public final K nextElement() { return (K)super.nextKey(); }
1414 <    }
1415 <
1416 <    final class ValueIterator extends HashIterator
1417 <        implements Iterator<V>, Enumeration<V> {
1418 <        @SuppressWarnings("unchecked")
1419 <        public final V next()        { return (V)super.nextValue(); }
1420 <        @SuppressWarnings("unchecked")
1421 <        public final V nextElement() { return (V)super.nextValue(); }
1422 <    }
3360 >    /* ----------------Views -------------- */
3361  
3362 <    final class EntryIterator extends HashIterator
3363 <        implements Iterator<Entry<K,V>> {
3364 <        public final Map.Entry<K,V> next() { return super.nextEntry(); }
3365 <    }
3362 >    /**
3363 >     * Base class for views.
3364 >     */
3365 >    static abstract class CHMView<K, V> {
3366 >        final ConcurrentHashMapV8<K, V> map;
3367 >        CHMView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
3368 >        public final int size()                 { return map.size(); }
3369 >        public final boolean isEmpty()          { return map.isEmpty(); }
3370 >        public final void clear()               { map.clear(); }
3371 >
3372 >        // implementations below rely on concrete classes supplying these
3373 >        abstract public Iterator<?> iterator();
3374 >        abstract public boolean contains(Object o);
3375 >        abstract public boolean remove(Object o);
3376 >
3377 >        private static final String oomeMsg = "Required array size too large";
3378 >
3379 >        public final Object[] toArray() {
3380 >            long sz = map.mappingCount();
3381 >            if (sz > (long)(MAX_ARRAY_SIZE))
3382 >                throw new OutOfMemoryError(oomeMsg);
3383 >            int n = (int)sz;
3384 >            Object[] r = new Object[n];
3385 >            int i = 0;
3386 >            Iterator<?> it = iterator();
3387 >            while (it.hasNext()) {
3388 >                if (i == n) {
3389 >                    if (n >= MAX_ARRAY_SIZE)
3390 >                        throw new OutOfMemoryError(oomeMsg);
3391 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3392 >                        n = MAX_ARRAY_SIZE;
3393 >                    else
3394 >                        n += (n >>> 1) + 1;
3395 >                    r = Arrays.copyOf(r, n);
3396 >                }
3397 >                r[i++] = it.next();
3398 >            }
3399 >            return (i == n) ? r : Arrays.copyOf(r, i);
3400 >        }
3401  
3402 <    final class KeySet extends AbstractSet<K> {
3403 <        public int size() {
3404 <            return ConcurrentHashMapV8.this.size();
3402 >        @SuppressWarnings("unchecked") public final <T> T[] toArray(T[] a) {
3403 >            long sz = map.mappingCount();
3404 >            if (sz > (long)(MAX_ARRAY_SIZE))
3405 >                throw new OutOfMemoryError(oomeMsg);
3406 >            int m = (int)sz;
3407 >            T[] r = (a.length >= m) ? a :
3408 >                (T[])java.lang.reflect.Array
3409 >                .newInstance(a.getClass().getComponentType(), m);
3410 >            int n = r.length;
3411 >            int i = 0;
3412 >            Iterator<?> it = iterator();
3413 >            while (it.hasNext()) {
3414 >                if (i == n) {
3415 >                    if (n >= MAX_ARRAY_SIZE)
3416 >                        throw new OutOfMemoryError(oomeMsg);
3417 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
3418 >                        n = MAX_ARRAY_SIZE;
3419 >                    else
3420 >                        n += (n >>> 1) + 1;
3421 >                    r = Arrays.copyOf(r, n);
3422 >                }
3423 >                r[i++] = (T)it.next();
3424 >            }
3425 >            if (a == r && i < n) {
3426 >                r[i] = null; // null-terminate
3427 >                return r;
3428 >            }
3429 >            return (i == n) ? r : Arrays.copyOf(r, i);
3430          }
3431 <        public boolean isEmpty() {
3432 <            return ConcurrentHashMapV8.this.isEmpty();
3431 >
3432 >        public final int hashCode() {
3433 >            int h = 0;
3434 >            for (Iterator<?> it = iterator(); it.hasNext();)
3435 >                h += it.next().hashCode();
3436 >            return h;
3437          }
3438 <        public void clear() {
3439 <            ConcurrentHashMapV8.this.clear();
3438 >
3439 >        public final String toString() {
3440 >            StringBuilder sb = new StringBuilder();
3441 >            sb.append('[');
3442 >            Iterator<?> it = iterator();
3443 >            if (it.hasNext()) {
3444 >                for (;;) {
3445 >                    Object e = it.next();
3446 >                    sb.append(e == this ? "(this Collection)" : e);
3447 >                    if (!it.hasNext())
3448 >                        break;
3449 >                    sb.append(',').append(' ');
3450 >                }
3451 >            }
3452 >            return sb.append(']').toString();
3453          }
3454 <        public Iterator<K> iterator() {
3455 <            return new KeyIterator();
3454 >
3455 >        public final boolean containsAll(Collection<?> c) {
3456 >            if (c != this) {
3457 >                for (Iterator<?> it = c.iterator(); it.hasNext();) {
3458 >                    Object e = it.next();
3459 >                    if (e == null || !contains(e))
3460 >                        return false;
3461 >                }
3462 >            }
3463 >            return true;
3464          }
3465 <        public boolean contains(Object o) {
3466 <            return ConcurrentHashMapV8.this.containsKey(o);
3465 >
3466 >        public final boolean removeAll(Collection<?> c) {
3467 >            boolean modified = false;
3468 >            for (Iterator<?> it = iterator(); it.hasNext();) {
3469 >                if (c.contains(it.next())) {
3470 >                    it.remove();
3471 >                    modified = true;
3472 >                }
3473 >            }
3474 >            return modified;
3475          }
3476 <        public boolean remove(Object o) {
3477 <            return ConcurrentHashMapV8.this.remove(o) != null;
3476 >
3477 >        public final boolean retainAll(Collection<?> c) {
3478 >            boolean modified = false;
3479 >            for (Iterator<?> it = iterator(); it.hasNext();) {
3480 >                if (!c.contains(it.next())) {
3481 >                    it.remove();
3482 >                    modified = true;
3483 >                }
3484 >            }
3485 >            return modified;
3486          }
3487 +
3488      }
3489  
3490 <    final class Values extends AbstractCollection<V> {
3491 <        public int size() {
3492 <            return ConcurrentHashMapV8.this.size();
3493 <        }
3494 <        public boolean isEmpty() {
3495 <            return ConcurrentHashMapV8.this.isEmpty();
3490 >    static final class Values<K,V> extends CHMView<K,V>
3491 >        implements Collection<V> {
3492 >        Values(ConcurrentHashMapV8<K, V> map)   { super(map); }
3493 >        public final boolean contains(Object o) { return map.containsValue(o); }
3494 >        public final boolean remove(Object o) {
3495 >            if (o != null) {
3496 >                Iterator<V> it = new ValueIterator<K,V>(map);
3497 >                while (it.hasNext()) {
3498 >                    if (o.equals(it.next())) {
3499 >                        it.remove();
3500 >                        return true;
3501 >                    }
3502 >                }
3503 >            }
3504 >            return false;
3505          }
3506 <        public void clear() {
3507 <            ConcurrentHashMapV8.this.clear();
3506 >        public final Iterator<V> iterator() {
3507 >            return new ValueIterator<K,V>(map);
3508          }
3509 <        public Iterator<V> iterator() {
3510 <            return new ValueIterator();
3509 >        public final boolean add(V e) {
3510 >            throw new UnsupportedOperationException();
3511          }
3512 <        public boolean contains(Object o) {
3513 <            return ConcurrentHashMapV8.this.containsValue(o);
3512 >        public final boolean addAll(Collection<? extends V> c) {
3513 >            throw new UnsupportedOperationException();
3514          }
3515 +
3516      }
3517  
3518 <    final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
3519 <        public int size() {
3520 <            return ConcurrentHashMapV8.this.size();
3521 <        }
3522 <        public boolean isEmpty() {
3523 <            return ConcurrentHashMapV8.this.isEmpty();
3524 <        }
3525 <        public void clear() {
3526 <            ConcurrentHashMapV8.this.clear();
3527 <        }
3528 <        public Iterator<Map.Entry<K,V>> iterator() {
3529 <            return new EntryIterator();
3530 <        }
3531 <        public boolean contains(Object o) {
3532 <            if (!(o instanceof Map.Entry))
3533 <                return false;
3534 <            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
3535 <            V v = ConcurrentHashMapV8.this.get(e.getKey());
3536 <            return v != null && v.equals(e.getValue());
3537 <        }
3538 <        public boolean remove(Object o) {
3539 <            if (!(o instanceof Map.Entry))
3540 <                return false;
3541 <            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
3542 <            return ConcurrentHashMapV8.this.remove(e.getKey(), e.getValue());
3518 >    static final class EntrySet<K,V> extends CHMView<K,V>
3519 >        implements Set<Map.Entry<K,V>> {
3520 >        EntrySet(ConcurrentHashMapV8<K, V> map) { super(map); }
3521 >        public final boolean contains(Object o) {
3522 >            Object k, v, r; Map.Entry<?,?> e;
3523 >            return ((o instanceof Map.Entry) &&
3524 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3525 >                    (r = map.get(k)) != null &&
3526 >                    (v = e.getValue()) != null &&
3527 >                    (v == r || v.equals(r)));
3528 >        }
3529 >        public final boolean remove(Object o) {
3530 >            Object k, v; Map.Entry<?,?> e;
3531 >            return ((o instanceof Map.Entry) &&
3532 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3533 >                    (v = e.getValue()) != null &&
3534 >                    map.remove(k, v));
3535 >        }
3536 >        public final Iterator<Map.Entry<K,V>> iterator() {
3537 >            return new EntryIterator<K,V>(map);
3538 >        }
3539 >        public final boolean add(Entry<K,V> e) {
3540 >            throw new UnsupportedOperationException();
3541 >        }
3542 >        public final boolean addAll(Collection<? extends Entry<K,V>> c) {
3543 >            throw new UnsupportedOperationException();
3544 >        }
3545 >        public boolean equals(Object o) {
3546 >            Set<?> c;
3547 >            return ((o instanceof Set) &&
3548 >                    ((c = (Set<?>)o) == this ||
3549 >                     (containsAll(c) && c.containsAll(this))));
3550          }
3551      }
3552  
3553      /* ---------------- Serialization Support -------------- */
3554  
3555      /**
3556 <     * Helper class used in previous version, declared for the sake of
3557 <     * serialization compatibility
3556 >     * Stripped-down version of helper class used in previous version,
3557 >     * declared for the sake of serialization compatibility
3558       */
3559 <    static class Segment<K,V> extends java.util.concurrent.locks.ReentrantLock
1503 <        implements Serializable {
3559 >    static class Segment<K,V> implements Serializable {
3560          private static final long serialVersionUID = 2249069246763182397L;
3561          final float loadFactor;
3562          Segment(float lf) { this.loadFactor = lf; }
# Line 1515 | Line 3571 | public class ConcurrentHashMapV8<K, V>
3571       * for each key-value mapping, followed by a null pair.
3572       * The key-value mappings are emitted in no particular order.
3573       */
3574 <    @SuppressWarnings("unchecked")
3575 <    private void writeObject(java.io.ObjectOutputStream s)
1520 <            throws java.io.IOException {
3574 >    @SuppressWarnings("unchecked") private void writeObject(java.io.ObjectOutputStream s)
3575 >        throws java.io.IOException {
3576          if (segments == null) { // for serialization compatibility
3577              segments = (Segment<K,V>[])
3578                  new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
3579              for (int i = 0; i < segments.length; ++i)
3580 <                segments[i] = new Segment<K,V>(loadFactor);
3580 >                segments[i] = new Segment<K,V>(LOAD_FACTOR);
3581          }
3582          s.defaultWriteObject();
3583 <        new HashIterator().writeEntries(s);
3583 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3584 >        Object v;
3585 >        while ((v = it.advance()) != null) {
3586 >            s.writeObject(it.nextKey);
3587 >            s.writeObject(v);
3588 >        }
3589          s.writeObject(null);
3590          s.writeObject(null);
3591          segments = null; // throw away
3592      }
3593  
3594      /**
3595 <     * Reconstitutes the  instance from a
1536 <     * stream (i.e., deserializes it).
3595 >     * Reconstitutes the instance from a stream (that is, deserializes it).
3596       * @param s the stream
3597       */
3598 <    @SuppressWarnings("unchecked")
3599 <    private void readObject(java.io.ObjectInputStream s)
1541 <            throws java.io.IOException, ClassNotFoundException {
3598 >    @SuppressWarnings("unchecked") private void readObject(java.io.ObjectInputStream s)
3599 >        throws java.io.IOException, ClassNotFoundException {
3600          s.defaultReadObject();
1543        // find load factor in a segment, if one exists
1544        if (segments != null && segments.length != 0)
1545            this.loadFactor = segments[0].loadFactor;
1546        else
1547            this.loadFactor = DEFAULT_LOAD_FACTOR;
1548        this.initCap = DEFAULT_CAPACITY;
1549        LongAdder ct = new LongAdder(); // force final field write
1550        UNSAFE.putObjectVolatile(this, counterOffset, ct);
3601          this.segments = null; // unneeded
3602 +        // initialize transient final field
3603 +        UNSAFE.putObjectVolatile(this, counterOffset, new LongAdder());
3604  
3605 <        // Read the keys and values, and put the mappings in the table
3605 >        // Create all nodes, then place in table once size is known
3606 >        long size = 0L;
3607 >        Node p = null;
3608          for (;;) {
3609 <            K key = (K) s.readObject();
3610 <            V value = (V) s.readObject();
3611 <            if (key == null)
3609 >            K k = (K) s.readObject();
3610 >            V v = (V) s.readObject();
3611 >            if (k != null && v != null) {
3612 >                int h = spread(k.hashCode());
3613 >                p = new Node(h, k, v, p);
3614 >                ++size;
3615 >            }
3616 >            else
3617                  break;
1559            put(key, value);
3618          }
3619 +        if (p != null) {
3620 +            boolean init = false;
3621 +            int n;
3622 +            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
3623 +                n = MAXIMUM_CAPACITY;
3624 +            else {
3625 +                int sz = (int)size;
3626 +                n = tableSizeFor(sz + (sz >>> 1) + 1);
3627 +            }
3628 +            int sc = sizeCtl;
3629 +            boolean collide = false;
3630 +            if (n > sc &&
3631 +                UNSAFE.compareAndSwapInt(this, sizeCtlOffset, sc, -1)) {
3632 +                try {
3633 +                    if (table == null) {
3634 +                        init = true;
3635 +                        Node[] tab = new Node[n];
3636 +                        int mask = n - 1;
3637 +                        while (p != null) {
3638 +                            int j = p.hash & mask;
3639 +                            Node next = p.next;
3640 +                            Node q = p.next = tabAt(tab, j);
3641 +                            setTabAt(tab, j, p);
3642 +                            if (!collide && q != null && q.hash == p.hash)
3643 +                                collide = true;
3644 +                            p = next;
3645 +                        }
3646 +                        table = tab;
3647 +                        counter.add(size);
3648 +                        sc = n - (n >>> 2);
3649 +                    }
3650 +                } finally {
3651 +                    sizeCtl = sc;
3652 +                }
3653 +                if (collide) { // rescan and convert to TreeBins
3654 +                    Node[] tab = table;
3655 +                    for (int i = 0; i < tab.length; ++i) {
3656 +                        int c = 0;
3657 +                        for (Node e = tabAt(tab, i); e != null; e = e.next) {
3658 +                            if (++c > TREE_THRESHOLD &&
3659 +                                (e.key instanceof Comparable)) {
3660 +                                replaceWithTreeBin(tab, i, e.key);
3661 +                                break;
3662 +                            }
3663 +                        }
3664 +                    }
3665 +                }
3666 +            }
3667 +            if (!init) { // Can only happen if unsafely published.
3668 +                while (p != null) {
3669 +                    internalPut(p.key, p.val);
3670 +                    p = p.next;
3671 +                }
3672 +            }
3673 +        }
3674 +    }
3675 +
3676 +
3677 +    // -------------------------------------------------------
3678 +
3679 +    // Sams
3680 +    /** Interface describing a void action of one argument */
3681 +    public interface Action<A> { void apply(A a); }
3682 +    /** Interface describing a void action of two arguments */
3683 +    public interface BiAction<A,B> { void apply(A a, B b); }
3684 +    /** Interface describing a function of one argument */
3685 +    public interface Fun<A,T> { T apply(A a); }
3686 +    /** Interface describing a function of two arguments */
3687 +    public interface BiFun<A,B,T> { T apply(A a, B b); }
3688 +    /** Interface describing a function of no arguments */
3689 +    public interface Generator<T> { T apply(); }
3690 +    /** Interface describing a function mapping its argument to a double */
3691 +    public interface ObjectToDouble<A> { double apply(A a); }
3692 +    /** Interface describing a function mapping its argument to a long */
3693 +    public interface ObjectToLong<A> { long apply(A a); }
3694 +    /** Interface describing a function mapping its argument to an int */
3695 +    public interface ObjectToInt<A> {int apply(A a); }
3696 +    /** Interface describing a function mapping two arguments to a double */
3697 +    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
3698 +    /** Interface describing a function mapping two arguments to a long */
3699 +    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
3700 +    /** Interface describing a function mapping two arguments to an int */
3701 +    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
3702 +    /** Interface describing a function mapping a double to a double */
3703 +    public interface DoubleToDouble { double apply(double a); }
3704 +    /** Interface describing a function mapping a long to a long */
3705 +    public interface LongToLong { long apply(long a); }
3706 +    /** Interface describing a function mapping an int to an int */
3707 +    public interface IntToInt { int apply(int a); }
3708 +    /** Interface describing a function mapping two doubles to a double */
3709 +    public interface DoubleByDoubleToDouble { double apply(double a, double b); }
3710 +    /** Interface describing a function mapping two longs to a long */
3711 +    public interface LongByLongToLong { long apply(long a, long b); }
3712 +    /** Interface describing a function mapping two ints to an int */
3713 +    public interface IntByIntToInt { int apply(int a, int b); }
3714 +
3715 +
3716 +    // -------------------------------------------------------
3717 +
3718 +    /**
3719 +     * Performs the given action for each (key, value).
3720 +     *
3721 +     * @param action the action
3722 +     */
3723 +    public void forEach(BiAction<K,V> action) {
3724 +        ForkJoinTasks.forEach
3725 +            (this, action).invoke();
3726 +    }
3727 +
3728 +    /**
3729 +     * Performs the given action for each non-null transformation
3730 +     * of each (key, value).
3731 +     *
3732 +     * @param transformer a function returning the transformation
3733 +     * for an element, or null of there is no transformation (in
3734 +     * which case the action is not applied).
3735 +     * @param action the action
3736 +     */
3737 +    public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
3738 +                            Action<U> action) {
3739 +        ForkJoinTasks.forEach
3740 +            (this, transformer, action).invoke();
3741 +    }
3742 +
3743 +    /**
3744 +     * Returns a non-null result from applying the given search
3745 +     * function on each (key, value), or null if none.  Upon
3746 +     * success, further element processing is suppressed and the
3747 +     * results of any other parallel invocations of the search
3748 +     * function are ignored.
3749 +     *
3750 +     * @param searchFunction a function returning a non-null
3751 +     * result on success, else null
3752 +     * @return a non-null result from applying the given search
3753 +     * function on each (key, value), or null if none
3754 +     */
3755 +    public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) {
3756 +        return ForkJoinTasks.search
3757 +            (this, searchFunction).invoke();
3758 +    }
3759 +
3760 +    /**
3761 +     * Returns the result of accumulating the given transformation
3762 +     * of all (key, value) pairs using the given reducer to
3763 +     * combine values, or null if none.
3764 +     *
3765 +     * @param transformer a function returning the transformation
3766 +     * for an element, or null of there is no transformation (in
3767 +     * which case it is not combined).
3768 +     * @param reducer a commutative associative combining function
3769 +     * @return the result of accumulating the given transformation
3770 +     * of all (key, value) pairs
3771 +     */
3772 +    public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
3773 +                        BiFun<? super U, ? super U, ? extends U> reducer) {
3774 +        return ForkJoinTasks.reduce
3775 +            (this, transformer, reducer).invoke();
3776 +    }
3777 +
3778 +    /**
3779 +     * Returns the result of accumulating the given transformation
3780 +     * of all (key, value) pairs using the given reducer to
3781 +     * combine values, and the given basis as an identity value.
3782 +     *
3783 +     * @param transformer a function returning the transformation
3784 +     * for an element
3785 +     * @param basis the identity (initial default value) for the reduction
3786 +     * @param reducer a commutative associative combining function
3787 +     * @return the result of accumulating the given transformation
3788 +     * of all (key, value) pairs
3789 +     */
3790 +    public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
3791 +                                 double basis,
3792 +                                 DoubleByDoubleToDouble reducer) {
3793 +        return ForkJoinTasks.reduceToDouble
3794 +            (this, transformer, basis, reducer).invoke();
3795 +    }
3796 +
3797 +    /**
3798 +     * Returns the result of accumulating the given transformation
3799 +     * of all (key, value) pairs using the given reducer to
3800 +     * combine values, and the given basis as an identity value.
3801 +     *
3802 +     * @param transformer a function returning the transformation
3803 +     * for an element
3804 +     * @param basis the identity (initial default value) for the reduction
3805 +     * @param reducer a commutative associative combining function
3806 +     * @return the result of accumulating the given transformation
3807 +     * of all (key, value) pairs
3808 +     */
3809 +    public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
3810 +                             long basis,
3811 +                             LongByLongToLong reducer) {
3812 +        return ForkJoinTasks.reduceToLong
3813 +            (this, transformer, basis, reducer).invoke();
3814 +    }
3815 +
3816 +    /**
3817 +     * Returns the result of accumulating the given transformation
3818 +     * of all (key, value) pairs using the given reducer to
3819 +     * combine values, and the given basis as an identity value.
3820 +     *
3821 +     * @param transformer a function returning the transformation
3822 +     * for an element
3823 +     * @param basis the identity (initial default value) for the reduction
3824 +     * @param reducer a commutative associative combining function
3825 +     * @return the result of accumulating the given transformation
3826 +     * of all (key, value) pairs
3827 +     */
3828 +    public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
3829 +                           int basis,
3830 +                           IntByIntToInt reducer) {
3831 +        return ForkJoinTasks.reduceToInt
3832 +            (this, transformer, basis, reducer).invoke();
3833 +    }
3834 +
3835 +    /**
3836 +     * Performs the given action for each key.
3837 +     *
3838 +     * @param action the action
3839 +     */
3840 +    public void forEachKey(Action<K> action) {
3841 +        ForkJoinTasks.forEachKey
3842 +            (this, action).invoke();
3843 +    }
3844 +
3845 +    /**
3846 +     * Performs the given action for each non-null transformation
3847 +     * of each key.
3848 +     *
3849 +     * @param transformer a function returning the transformation
3850 +     * for an element, or null of there is no transformation (in
3851 +     * which case the action is not applied).
3852 +     * @param action the action
3853 +     */
3854 +    public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
3855 +                               Action<U> action) {
3856 +        ForkJoinTasks.forEachKey
3857 +            (this, transformer, action).invoke();
3858 +    }
3859 +
3860 +    /**
3861 +     * Returns a non-null result from applying the given search
3862 +     * function on each key, or null if none. Upon success,
3863 +     * further element processing is suppressed and the results of
3864 +     * any other parallel invocations of the search function are
3865 +     * ignored.
3866 +     *
3867 +     * @param searchFunction a function returning a non-null
3868 +     * result on success, else null
3869 +     * @return a non-null result from applying the given search
3870 +     * function on each key, or null if none
3871 +     */
3872 +    public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
3873 +        return ForkJoinTasks.searchKeys
3874 +            (this, searchFunction).invoke();
3875 +    }
3876 +
3877 +    /**
3878 +     * Returns the result of accumulating all keys using the given
3879 +     * reducer to combine values, or null if none.
3880 +     *
3881 +     * @param reducer a commutative associative combining function
3882 +     * @return the result of accumulating all keys using the given
3883 +     * reducer to combine values, or null if none
3884 +     */
3885 +    public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
3886 +        return ForkJoinTasks.reduceKeys
3887 +            (this, reducer).invoke();
3888 +    }
3889 +
3890 +    /**
3891 +     * Returns the result of accumulating the given transformation
3892 +     * of all keys using the given reducer to combine values, or
3893 +     * null if none.
3894 +     *
3895 +     * @param transformer a function returning the transformation
3896 +     * for an element, or null of there is no transformation (in
3897 +     * which case it is not combined).
3898 +     * @param reducer a commutative associative combining function
3899 +     * @return the result of accumulating the given transformation
3900 +     * of all keys
3901 +     */
3902 +    public <U> U reduceKeys(Fun<? super K, ? extends U> transformer,
3903 +                            BiFun<? super U, ? super U, ? extends U> reducer) {
3904 +        return ForkJoinTasks.reduceKeys
3905 +            (this, transformer, reducer).invoke();
3906 +    }
3907 +
3908 +    /**
3909 +     * Returns the result of accumulating the given transformation
3910 +     * of all keys using the given reducer to combine values, and
3911 +     * the given basis as an identity value.
3912 +     *
3913 +     * @param transformer a function returning the transformation
3914 +     * for an element
3915 +     * @param basis the identity (initial default value) for the reduction
3916 +     * @param reducer a commutative associative combining function
3917 +     * @return  the result of accumulating the given transformation
3918 +     * of all keys
3919 +     */
3920 +    public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
3921 +                                     double basis,
3922 +                                     DoubleByDoubleToDouble reducer) {
3923 +        return ForkJoinTasks.reduceKeysToDouble
3924 +            (this, transformer, basis, reducer).invoke();
3925 +    }
3926 +
3927 +    /**
3928 +     * Returns the result of accumulating the given transformation
3929 +     * of all keys using the given reducer to combine values, and
3930 +     * the given basis as an identity value.
3931 +     *
3932 +     * @param transformer a function returning the transformation
3933 +     * for an element
3934 +     * @param basis the identity (initial default value) for the reduction
3935 +     * @param reducer a commutative associative combining function
3936 +     * @return the result of accumulating the given transformation
3937 +     * of all keys
3938 +     */
3939 +    public long reduceKeysToLong(ObjectToLong<? super K> transformer,
3940 +                                 long basis,
3941 +                                 LongByLongToLong reducer) {
3942 +        return ForkJoinTasks.reduceKeysToLong
3943 +            (this, transformer, basis, reducer).invoke();
3944 +    }
3945 +
3946 +    /**
3947 +     * Returns the result of accumulating the given transformation
3948 +     * of all keys using the given reducer to combine values, and
3949 +     * the given basis as an identity value.
3950 +     *
3951 +     * @param transformer a function returning the transformation
3952 +     * for an element
3953 +     * @param basis the identity (initial default value) for the reduction
3954 +     * @param reducer a commutative associative combining function
3955 +     * @return the result of accumulating the given transformation
3956 +     * of all keys
3957 +     */
3958 +    public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3959 +                               int basis,
3960 +                               IntByIntToInt reducer) {
3961 +        return ForkJoinTasks.reduceKeysToInt
3962 +            (this, transformer, basis, reducer).invoke();
3963 +    }
3964 +
3965 +    /**
3966 +     * Performs the given action for each value.
3967 +     *
3968 +     * @param action the action
3969 +     */
3970 +    public void forEachValue(Action<V> action) {
3971 +        ForkJoinTasks.forEachValue
3972 +            (this, action).invoke();
3973 +    }
3974 +
3975 +    /**
3976 +     * Performs the given action for each non-null transformation
3977 +     * of each value.
3978 +     *
3979 +     * @param transformer a function returning the transformation
3980 +     * for an element, or null of there is no transformation (in
3981 +     * which case the action is not applied).
3982 +     */
3983 +    public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
3984 +                                 Action<U> action) {
3985 +        ForkJoinTasks.forEachValue
3986 +            (this, transformer, action).invoke();
3987 +    }
3988 +
3989 +    /**
3990 +     * Returns a non-null result from applying the given search
3991 +     * function on each value, or null if none.  Upon success,
3992 +     * further element processing is suppressed and the results of
3993 +     * any other parallel invocations of the search function are
3994 +     * ignored.
3995 +     *
3996 +     * @param searchFunction a function returning a non-null
3997 +     * result on success, else null
3998 +     * @return a non-null result from applying the given search
3999 +     * function on each value, or null if none
4000 +     *
4001 +     */
4002 +    public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
4003 +        return ForkJoinTasks.searchValues
4004 +            (this, searchFunction).invoke();
4005 +    }
4006 +
4007 +    /**
4008 +     * Returns the result of accumulating all values using the
4009 +     * given reducer to combine values, or null if none.
4010 +     *
4011 +     * @param reducer a commutative associative combining function
4012 +     * @return  the result of accumulating all values
4013 +     */
4014 +    public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
4015 +        return ForkJoinTasks.reduceValues
4016 +            (this, reducer).invoke();
4017 +    }
4018 +
4019 +    /**
4020 +     * Returns the result of accumulating the given transformation
4021 +     * of all values using the given reducer to combine values, or
4022 +     * null if none.
4023 +     *
4024 +     * @param transformer a function returning the transformation
4025 +     * for an element, or null of there is no transformation (in
4026 +     * which case it is not combined).
4027 +     * @param reducer a commutative associative combining function
4028 +     * @return the result of accumulating the given transformation
4029 +     * of all values
4030 +     */
4031 +    public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
4032 +                              BiFun<? super U, ? super U, ? extends U> reducer) {
4033 +        return ForkJoinTasks.reduceValues
4034 +            (this, transformer, reducer).invoke();
4035 +    }
4036 +
4037 +    /**
4038 +     * Returns the result of accumulating the given transformation
4039 +     * of all values using the given reducer to combine values,
4040 +     * and the given basis as an identity value.
4041 +     *
4042 +     * @param transformer a function returning the transformation
4043 +     * for an element
4044 +     * @param basis the identity (initial default value) for the reduction
4045 +     * @param reducer a commutative associative combining function
4046 +     * @return the result of accumulating the given transformation
4047 +     * of all values
4048 +     */
4049 +    public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
4050 +                                       double basis,
4051 +                                       DoubleByDoubleToDouble reducer) {
4052 +        return ForkJoinTasks.reduceValuesToDouble
4053 +            (this, transformer, basis, reducer).invoke();
4054 +    }
4055 +
4056 +    /**
4057 +     * Returns the result of accumulating the given transformation
4058 +     * of all values using the given reducer to combine values,
4059 +     * and the given basis as an identity value.
4060 +     *
4061 +     * @param transformer a function returning the transformation
4062 +     * for an element
4063 +     * @param basis the identity (initial default value) for the reduction
4064 +     * @param reducer a commutative associative combining function
4065 +     * @return the result of accumulating the given transformation
4066 +     * of all values
4067 +     */
4068 +    public long reduceValuesToLong(ObjectToLong<? super V> transformer,
4069 +                                   long basis,
4070 +                                   LongByLongToLong reducer) {
4071 +        return ForkJoinTasks.reduceValuesToLong
4072 +            (this, transformer, basis, reducer).invoke();
4073 +    }
4074 +
4075 +    /**
4076 +     * Returns the result of accumulating the given transformation
4077 +     * of all values using the given reducer to combine values,
4078 +     * and the given basis as an identity value.
4079 +     *
4080 +     * @param transformer a function returning the transformation
4081 +     * for an element
4082 +     * @param basis the identity (initial default value) for the reduction
4083 +     * @param reducer a commutative associative combining function
4084 +     * @return the result of accumulating the given transformation
4085 +     * of all values
4086 +     */
4087 +    public int reduceValuesToInt(ObjectToInt<? super V> transformer,
4088 +                                 int basis,
4089 +                                 IntByIntToInt reducer) {
4090 +        return ForkJoinTasks.reduceValuesToInt
4091 +            (this, transformer, basis, reducer).invoke();
4092 +    }
4093 +
4094 +    /**
4095 +     * Performs the given action for each entry.
4096 +     *
4097 +     * @param action the action
4098 +     */
4099 +    public void forEachEntry(Action<Map.Entry<K,V>> action) {
4100 +        ForkJoinTasks.forEachEntry
4101 +            (this, action).invoke();
4102 +    }
4103 +
4104 +    /**
4105 +     * Performs the given action for each non-null transformation
4106 +     * of each entry.
4107 +     *
4108 +     * @param transformer a function returning the transformation
4109 +     * for an element, or null of there is no transformation (in
4110 +     * which case the action is not applied).
4111 +     * @param action the action
4112 +     */
4113 +    public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
4114 +                                 Action<U> action) {
4115 +        ForkJoinTasks.forEachEntry
4116 +            (this, transformer, action).invoke();
4117 +    }
4118 +
4119 +    /**
4120 +     * Returns a non-null result from applying the given search
4121 +     * function on each entry, or null if none.  Upon success,
4122 +     * further element processing is suppressed and the results of
4123 +     * any other parallel invocations of the search function are
4124 +     * ignored.
4125 +     *
4126 +     * @param searchFunction a function returning a non-null
4127 +     * result on success, else null
4128 +     * @return a non-null result from applying the given search
4129 +     * function on each entry, or null if none
4130 +     */
4131 +    public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4132 +        return ForkJoinTasks.searchEntries
4133 +            (this, searchFunction).invoke();
4134 +    }
4135 +
4136 +    /**
4137 +     * Returns the result of accumulating all entries using the
4138 +     * given reducer to combine values, or null if none.
4139 +     *
4140 +     * @param reducer a commutative associative combining function
4141 +     * @return the result of accumulating all entries
4142 +     */
4143 +    public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4144 +        return ForkJoinTasks.reduceEntries
4145 +            (this, reducer).invoke();
4146 +    }
4147 +
4148 +    /**
4149 +     * Returns the result of accumulating the given transformation
4150 +     * of all entries using the given reducer to combine values,
4151 +     * or null if none.
4152 +     *
4153 +     * @param transformer a function returning the transformation
4154 +     * for an element, or null of there is no transformation (in
4155 +     * which case it is not combined).
4156 +     * @param reducer a commutative associative combining function
4157 +     * @return the result of accumulating the given transformation
4158 +     * of all entries
4159 +     */
4160 +    public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer,
4161 +                               BiFun<? super U, ? super U, ? extends U> reducer) {
4162 +        return ForkJoinTasks.reduceEntries
4163 +            (this, transformer, reducer).invoke();
4164 +    }
4165 +
4166 +    /**
4167 +     * Returns the result of accumulating the given transformation
4168 +     * of all entries using the given reducer to combine values,
4169 +     * and the given basis as an identity value.
4170 +     *
4171 +     * @param transformer a function returning the transformation
4172 +     * for an element
4173 +     * @param basis the identity (initial default value) for the reduction
4174 +     * @param reducer a commutative associative combining function
4175 +     * @return the result of accumulating the given transformation
4176 +     * of all entries
4177 +     */
4178 +    public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
4179 +                                        double basis,
4180 +                                        DoubleByDoubleToDouble reducer) {
4181 +        return ForkJoinTasks.reduceEntriesToDouble
4182 +            (this, transformer, basis, reducer).invoke();
4183 +    }
4184 +
4185 +    /**
4186 +     * Returns the result of accumulating the given transformation
4187 +     * of all entries using the given reducer to combine values,
4188 +     * and the given basis as an identity value.
4189 +     *
4190 +     * @param transformer a function returning the transformation
4191 +     * for an element
4192 +     * @param basis the identity (initial default value) for the reduction
4193 +     * @param reducer a commutative associative combining function
4194 +     * @return  the result of accumulating the given transformation
4195 +     * of all entries
4196 +     */
4197 +    public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4198 +                                    long basis,
4199 +                                    LongByLongToLong reducer) {
4200 +        return ForkJoinTasks.reduceEntriesToLong
4201 +            (this, transformer, basis, reducer).invoke();
4202 +    }
4203 +
4204 +    /**
4205 +     * Returns the result of accumulating the given transformation
4206 +     * of all entries using the given reducer to combine values,
4207 +     * and the given basis as an identity value.
4208 +     *
4209 +     * @param transformer a function returning the transformation
4210 +     * for an element
4211 +     * @param basis the identity (initial default value) for the reduction
4212 +     * @param reducer a commutative associative combining function
4213 +     * @return the result of accumulating the given transformation
4214 +     * of all entries
4215 +     */
4216 +    public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer,
4217 +                                  int basis,
4218 +                                  IntByIntToInt reducer) {
4219 +        return ForkJoinTasks.reduceEntriesToInt
4220 +            (this, transformer, basis, reducer).invoke();
4221 +    }
4222 +
4223 +    // ---------------------------------------------------------------------
4224 +
4225 +    /**
4226 +     * Predefined tasks for performing bulk parallel operations on
4227 +     * ConcurrentHashMapV8s. These tasks follow the forms and rules used
4228 +     * for bulk operations. Each method has the same name, but returns
4229 +     * a task rather than invoking it. These methods may be useful in
4230 +     * custom applications such as submitting a task without waiting
4231 +     * for completion, using a custom pool, or combining with other
4232 +     * tasks.
4233 +     */
4234 +    public static class ForkJoinTasks {
4235 +        private ForkJoinTasks() {}
4236 +
4237 +        /**
4238 +         * Returns a task that when invoked, performs the given
4239 +         * action for each (key, value)
4240 +         *
4241 +         * @param map the map
4242 +         * @param action the action
4243 +         * @return the task
4244 +         */
4245 +        public static <K,V> ForkJoinTask<Void> forEach
4246 +            (ConcurrentHashMapV8<K,V> map,
4247 +             BiAction<K,V> action) {
4248 +            if (action == null) throw new NullPointerException();
4249 +            return new ForEachMappingTask<K,V>(map, null, -1, null, action);
4250 +        }
4251 +
4252 +        /**
4253 +         * Returns a task that when invoked, performs the given
4254 +         * action for each non-null transformation of each (key, value)
4255 +         *
4256 +         * @param map the map
4257 +         * @param transformer a function returning the transformation
4258 +         * for an element, or null if there is no transformation (in
4259 +         * which case the action is not applied)
4260 +         * @param action the action
4261 +         * @return the task
4262 +         */
4263 +        public static <K,V,U> ForkJoinTask<Void> forEach
4264 +            (ConcurrentHashMapV8<K,V> map,
4265 +             BiFun<? super K, ? super V, ? extends U> transformer,
4266 +             Action<U> action) {
4267 +            if (transformer == null || action == null)
4268 +                throw new NullPointerException();
4269 +            return new ForEachTransformedMappingTask<K,V,U>
4270 +                (map, null, -1, null, transformer, action);
4271 +        }
4272 +
4273 +        /**
4274 +         * Returns a task that when invoked, returns a non-null result
4275 +         * from applying the given search function on each (key,
4276 +         * value), or null if none. Upon success, further element
4277 +         * processing is suppressed and the results of any other
4278 +         * parallel invocations of the search function are ignored.
4279 +         *
4280 +         * @param map the map
4281 +         * @param searchFunction a function returning a non-null
4282 +         * result on success, else null
4283 +         * @return the task
4284 +         */
4285 +        public static <K,V,U> ForkJoinTask<U> search
4286 +            (ConcurrentHashMapV8<K,V> map,
4287 +             BiFun<? super K, ? super V, ? extends U> searchFunction) {
4288 +            if (searchFunction == null) throw new NullPointerException();
4289 +            return new SearchMappingsTask<K,V,U>
4290 +                (map, null, -1, null, searchFunction,
4291 +                 new AtomicReference<U>());
4292 +        }
4293 +
4294 +        /**
4295 +         * Returns a task that when invoked, returns the result of
4296 +         * accumulating the given transformation of all (key, value) pairs
4297 +         * using the given reducer to combine values, or null if none.
4298 +         *
4299 +         * @param map the map
4300 +         * @param transformer a function returning the transformation
4301 +         * for an element, or null if there is no transformation (in
4302 +         * which case it is not combined).
4303 +         * @param reducer a commutative associative combining function
4304 +         * @return the task
4305 +         */
4306 +        public static <K,V,U> ForkJoinTask<U> reduce
4307 +            (ConcurrentHashMapV8<K,V> map,
4308 +             BiFun<? super K, ? super V, ? extends U> transformer,
4309 +             BiFun<? super U, ? super U, ? extends U> reducer) {
4310 +            if (transformer == null || reducer == null)
4311 +                throw new NullPointerException();
4312 +            return new MapReduceMappingsTask<K,V,U>
4313 +                (map, null, -1, null, transformer, reducer);
4314 +        }
4315 +
4316 +        /**
4317 +         * Returns a task that when invoked, returns the result of
4318 +         * accumulating the given transformation of all (key, value) pairs
4319 +         * using the given reducer to combine values, and the given
4320 +         * basis as an identity value.
4321 +         *
4322 +         * @param map the map
4323 +         * @param transformer a function returning the transformation
4324 +         * for an element
4325 +         * @param basis the identity (initial default value) for the reduction
4326 +         * @param reducer a commutative associative combining function
4327 +         * @return the task
4328 +         */
4329 +        public static <K,V> ForkJoinTask<Double> reduceToDouble
4330 +            (ConcurrentHashMapV8<K,V> map,
4331 +             ObjectByObjectToDouble<? super K, ? super V> transformer,
4332 +             double basis,
4333 +             DoubleByDoubleToDouble reducer) {
4334 +            if (transformer == null || reducer == null)
4335 +                throw new NullPointerException();
4336 +            return new MapReduceMappingsToDoubleTask<K,V>
4337 +                (map, null, -1, null, transformer, basis, reducer);
4338 +        }
4339 +
4340 +        /**
4341 +         * Returns a task that when invoked, returns the result of
4342 +         * accumulating the given transformation of all (key, value) pairs
4343 +         * using the given reducer to combine values, and the given
4344 +         * basis as an identity value.
4345 +         *
4346 +         * @param map the map
4347 +         * @param transformer a function returning the transformation
4348 +         * for an element
4349 +         * @param basis the identity (initial default value) for the reduction
4350 +         * @param reducer a commutative associative combining function
4351 +         * @return the task
4352 +         */
4353 +        public static <K,V> ForkJoinTask<Long> reduceToLong
4354 +            (ConcurrentHashMapV8<K,V> map,
4355 +             ObjectByObjectToLong<? super K, ? super V> transformer,
4356 +             long basis,
4357 +             LongByLongToLong reducer) {
4358 +            if (transformer == null || reducer == null)
4359 +                throw new NullPointerException();
4360 +            return new MapReduceMappingsToLongTask<K,V>
4361 +                (map, null, -1, null, transformer, basis, reducer);
4362 +        }
4363 +
4364 +        /**
4365 +         * Returns a task that when invoked, returns the result of
4366 +         * accumulating the given transformation of all (key, value) pairs
4367 +         * using the given reducer to combine values, and the given
4368 +         * basis as an identity value.
4369 +         *
4370 +         * @param transformer a function returning the transformation
4371 +         * for an element
4372 +         * @param basis the identity (initial default value) for the reduction
4373 +         * @param reducer a commutative associative combining function
4374 +         * @return the task
4375 +         */
4376 +        public static <K,V> ForkJoinTask<Integer> reduceToInt
4377 +            (ConcurrentHashMapV8<K,V> map,
4378 +             ObjectByObjectToInt<? super K, ? super V> transformer,
4379 +             int basis,
4380 +             IntByIntToInt reducer) {
4381 +            if (transformer == null || reducer == null)
4382 +                throw new NullPointerException();
4383 +            return new MapReduceMappingsToIntTask<K,V>
4384 +                (map, null, -1, null, transformer, basis, reducer);
4385 +        }
4386 +
4387 +        /**
4388 +         * Returns a task that when invoked, performs the given action
4389 +         * for each key.
4390 +         *
4391 +         * @param map the map
4392 +         * @param action the action
4393 +         * @return the task
4394 +         */
4395 +        public static <K,V> ForkJoinTask<Void> forEachKey
4396 +            (ConcurrentHashMapV8<K,V> map,
4397 +             Action<K> action) {
4398 +            if (action == null) throw new NullPointerException();
4399 +            return new ForEachKeyTask<K,V>(map, null, -1, null, action);
4400 +        }
4401 +
4402 +        /**
4403 +         * Returns a task that when invoked, performs the given action
4404 +         * for each non-null transformation of each key.
4405 +         *
4406 +         * @param map the map
4407 +         * @param transformer a function returning the transformation
4408 +         * for an element, or null if there is no transformation (in
4409 +         * which case the action is not applied)
4410 +         * @param action the action
4411 +         * @return the task
4412 +         */
4413 +        public static <K,V,U> ForkJoinTask<Void> forEachKey
4414 +            (ConcurrentHashMapV8<K,V> map,
4415 +             Fun<? super K, ? extends U> transformer,
4416 +             Action<U> action) {
4417 +            if (transformer == null || action == null)
4418 +                throw new NullPointerException();
4419 +            return new ForEachTransformedKeyTask<K,V,U>
4420 +                (map, null, -1, null, transformer, action);
4421 +        }
4422 +
4423 +        /**
4424 +         * Returns a task that when invoked, returns a non-null result
4425 +         * from applying the given search function on each key, or
4426 +         * null if none.  Upon success, further element processing is
4427 +         * suppressed and the results of any other parallel
4428 +         * invocations of the search function are ignored.
4429 +         *
4430 +         * @param map the map
4431 +         * @param searchFunction a function returning a non-null
4432 +         * result on success, else null
4433 +         * @return the task
4434 +         */
4435 +        public static <K,V,U> ForkJoinTask<U> searchKeys
4436 +            (ConcurrentHashMapV8<K,V> map,
4437 +             Fun<? super K, ? extends U> searchFunction) {
4438 +            if (searchFunction == null) throw new NullPointerException();
4439 +            return new SearchKeysTask<K,V,U>
4440 +                (map, null, -1, null, searchFunction,
4441 +                 new AtomicReference<U>());
4442 +        }
4443 +
4444 +        /**
4445 +         * Returns a task that when invoked, returns the result of
4446 +         * accumulating all keys using the given reducer to combine
4447 +         * values, or null if none.
4448 +         *
4449 +         * @param map the map
4450 +         * @param reducer a commutative associative combining function
4451 +         * @return the task
4452 +         */
4453 +        public static <K,V> ForkJoinTask<K> reduceKeys
4454 +            (ConcurrentHashMapV8<K,V> map,
4455 +             BiFun<? super K, ? super K, ? extends K> reducer) {
4456 +            if (reducer == null) throw new NullPointerException();
4457 +            return new ReduceKeysTask<K,V>
4458 +                (map, null, -1, null, reducer);
4459 +        }
4460 +
4461 +        /**
4462 +         * Returns a task that when invoked, returns the result of
4463 +         * accumulating the given transformation of all keys using the given
4464 +         * reducer to combine values, or null if none.
4465 +         *
4466 +         * @param map the map
4467 +         * @param transformer a function returning the transformation
4468 +         * for an element, or null if there is no transformation (in
4469 +         * which case it is not combined).
4470 +         * @param reducer a commutative associative combining function
4471 +         * @return the task
4472 +         */
4473 +        public static <K,V,U> ForkJoinTask<U> reduceKeys
4474 +            (ConcurrentHashMapV8<K,V> map,
4475 +             Fun<? super K, ? extends U> transformer,
4476 +             BiFun<? super U, ? super U, ? extends U> reducer) {
4477 +            if (transformer == null || reducer == null)
4478 +                throw new NullPointerException();
4479 +            return new MapReduceKeysTask<K,V,U>
4480 +                (map, null, -1, null, transformer, reducer);
4481 +        }
4482 +
4483 +        /**
4484 +         * Returns a task that when invoked, returns the result of
4485 +         * accumulating the given transformation of all keys using the given
4486 +         * reducer to combine values, and the given basis as an
4487 +         * identity value.
4488 +         *
4489 +         * @param map the map
4490 +         * @param transformer a function returning the transformation
4491 +         * for an element
4492 +         * @param basis the identity (initial default value) for the reduction
4493 +         * @param reducer a commutative associative combining function
4494 +         * @return the task
4495 +         */
4496 +        public static <K,V> ForkJoinTask<Double> reduceKeysToDouble
4497 +            (ConcurrentHashMapV8<K,V> map,
4498 +             ObjectToDouble<? super K> transformer,
4499 +             double basis,
4500 +             DoubleByDoubleToDouble reducer) {
4501 +            if (transformer == null || reducer == null)
4502 +                throw new NullPointerException();
4503 +            return new MapReduceKeysToDoubleTask<K,V>
4504 +                (map, null, -1, null, transformer, basis, reducer);
4505 +        }
4506 +
4507 +        /**
4508 +         * Returns a task that when invoked, returns the result of
4509 +         * accumulating the given transformation of all keys using the given
4510 +         * reducer to combine values, and the given basis as an
4511 +         * identity value.
4512 +         *
4513 +         * @param map the map
4514 +         * @param transformer a function returning the transformation
4515 +         * for an element
4516 +         * @param basis the identity (initial default value) for the reduction
4517 +         * @param reducer a commutative associative combining function
4518 +         * @return the task
4519 +         */
4520 +        public static <K,V> ForkJoinTask<Long> reduceKeysToLong
4521 +            (ConcurrentHashMapV8<K,V> map,
4522 +             ObjectToLong<? super K> transformer,
4523 +             long basis,
4524 +             LongByLongToLong reducer) {
4525 +            if (transformer == null || reducer == null)
4526 +                throw new NullPointerException();
4527 +            return new MapReduceKeysToLongTask<K,V>
4528 +                (map, null, -1, null, transformer, basis, reducer);
4529 +        }
4530 +
4531 +        /**
4532 +         * Returns a task that when invoked, returns the result of
4533 +         * accumulating the given transformation of all keys using the given
4534 +         * reducer to combine values, and the given basis as an
4535 +         * identity value.
4536 +         *
4537 +         * @param map the map
4538 +         * @param transformer a function returning the transformation
4539 +         * for an element
4540 +         * @param basis the identity (initial default value) for the reduction
4541 +         * @param reducer a commutative associative combining function
4542 +         * @return the task
4543 +         */
4544 +        public static <K,V> ForkJoinTask<Integer> reduceKeysToInt
4545 +            (ConcurrentHashMapV8<K,V> map,
4546 +             ObjectToInt<? super K> transformer,
4547 +             int basis,
4548 +             IntByIntToInt reducer) {
4549 +            if (transformer == null || reducer == null)
4550 +                throw new NullPointerException();
4551 +            return new MapReduceKeysToIntTask<K,V>
4552 +                (map, null, -1, null, transformer, basis, reducer);
4553 +        }
4554 +
4555 +        /**
4556 +         * Returns a task that when invoked, performs the given action
4557 +         * for each value.
4558 +         *
4559 +         * @param map the map
4560 +         * @param action the action
4561 +         */
4562 +        public static <K,V> ForkJoinTask<Void> forEachValue
4563 +            (ConcurrentHashMapV8<K,V> map,
4564 +             Action<V> action) {
4565 +            if (action == null) throw new NullPointerException();
4566 +            return new ForEachValueTask<K,V>(map, null, -1, null, action);
4567 +        }
4568 +
4569 +        /**
4570 +         * Returns a task that when invoked, performs the given action
4571 +         * for each non-null transformation of each value.
4572 +         *
4573 +         * @param map the map
4574 +         * @param transformer a function returning the transformation
4575 +         * for an element, or null if there is no transformation (in
4576 +         * which case the action is not applied)
4577 +         * @param action the action
4578 +         */
4579 +        public static <K,V,U> ForkJoinTask<Void> forEachValue
4580 +            (ConcurrentHashMapV8<K,V> map,
4581 +             Fun<? super V, ? extends U> transformer,
4582 +             Action<U> action) {
4583 +            if (transformer == null || action == null)
4584 +                throw new NullPointerException();
4585 +            return new ForEachTransformedValueTask<K,V,U>
4586 +                (map, null, -1, null, transformer, action);
4587 +        }
4588 +
4589 +        /**
4590 +         * Returns a task that when invoked, returns a non-null result
4591 +         * from applying the given search function on each value, or
4592 +         * null if none.  Upon success, further element processing is
4593 +         * suppressed and the results of any other parallel
4594 +         * invocations of the search function are ignored.
4595 +         *
4596 +         * @param map the map
4597 +         * @param searchFunction a function returning a non-null
4598 +         * result on success, else null
4599 +         * @return the task
4600 +         */
4601 +        public static <K,V,U> ForkJoinTask<U> searchValues
4602 +            (ConcurrentHashMapV8<K,V> map,
4603 +             Fun<? super V, ? extends U> searchFunction) {
4604 +            if (searchFunction == null) throw new NullPointerException();
4605 +            return new SearchValuesTask<K,V,U>
4606 +                (map, null, -1, null, searchFunction,
4607 +                 new AtomicReference<U>());
4608 +        }
4609 +
4610 +        /**
4611 +         * Returns a task that when invoked, returns the result of
4612 +         * accumulating all values using the given reducer to combine
4613 +         * values, or null if none.
4614 +         *
4615 +         * @param map the map
4616 +         * @param reducer a commutative associative combining function
4617 +         * @return the task
4618 +         */
4619 +        public static <K,V> ForkJoinTask<V> reduceValues
4620 +            (ConcurrentHashMapV8<K,V> map,
4621 +             BiFun<? super V, ? super V, ? extends V> reducer) {
4622 +            if (reducer == null) throw new NullPointerException();
4623 +            return new ReduceValuesTask<K,V>
4624 +                (map, null, -1, null, reducer);
4625 +        }
4626 +
4627 +        /**
4628 +         * Returns a task that when invoked, returns the result of
4629 +         * accumulating the given transformation of all values using the
4630 +         * given reducer to combine values, or null if none.
4631 +         *
4632 +         * @param map the map
4633 +         * @param transformer a function returning the transformation
4634 +         * for an element, or null if there is no transformation (in
4635 +         * which case it is not combined).
4636 +         * @param reducer a commutative associative combining function
4637 +         * @return the task
4638 +         */
4639 +        public static <K,V,U> ForkJoinTask<U> reduceValues
4640 +            (ConcurrentHashMapV8<K,V> map,
4641 +             Fun<? super V, ? extends U> transformer,
4642 +             BiFun<? super U, ? super U, ? extends U> reducer) {
4643 +            if (transformer == null || reducer == null)
4644 +                throw new NullPointerException();
4645 +            return new MapReduceValuesTask<K,V,U>
4646 +                (map, null, -1, null, transformer, reducer);
4647 +        }
4648 +
4649 +        /**
4650 +         * Returns a task that when invoked, returns the result of
4651 +         * accumulating the given transformation of all values using the
4652 +         * given reducer to combine values, and the given basis as an
4653 +         * identity value.
4654 +         *
4655 +         * @param map the map
4656 +         * @param transformer a function returning the transformation
4657 +         * for an element
4658 +         * @param basis the identity (initial default value) for the reduction
4659 +         * @param reducer a commutative associative combining function
4660 +         * @return the task
4661 +         */
4662 +        public static <K,V> ForkJoinTask<Double> reduceValuesToDouble
4663 +            (ConcurrentHashMapV8<K,V> map,
4664 +             ObjectToDouble<? super V> transformer,
4665 +             double basis,
4666 +             DoubleByDoubleToDouble reducer) {
4667 +            if (transformer == null || reducer == null)
4668 +                throw new NullPointerException();
4669 +            return new MapReduceValuesToDoubleTask<K,V>
4670 +                (map, null, -1, null, transformer, basis, reducer);
4671 +        }
4672 +
4673 +        /**
4674 +         * Returns a task that when invoked, returns the result of
4675 +         * accumulating the given transformation of all values using the
4676 +         * given reducer to combine values, and the given basis as an
4677 +         * identity value.
4678 +         *
4679 +         * @param map the map
4680 +         * @param transformer a function returning the transformation
4681 +         * for an element
4682 +         * @param basis the identity (initial default value) for the reduction
4683 +         * @param reducer a commutative associative combining function
4684 +         * @return the task
4685 +         */
4686 +        public static <K,V> ForkJoinTask<Long> reduceValuesToLong
4687 +            (ConcurrentHashMapV8<K,V> map,
4688 +             ObjectToLong<? super V> transformer,
4689 +             long basis,
4690 +             LongByLongToLong reducer) {
4691 +            if (transformer == null || reducer == null)
4692 +                throw new NullPointerException();
4693 +            return new MapReduceValuesToLongTask<K,V>
4694 +                (map, null, -1, null, transformer, basis, reducer);
4695 +        }
4696 +
4697 +        /**
4698 +         * Returns a task that when invoked, returns the result of
4699 +         * accumulating the given transformation of all values using the
4700 +         * given reducer to combine values, and the given basis as an
4701 +         * identity value.
4702 +         *
4703 +         * @param map the map
4704 +         * @param transformer a function returning the transformation
4705 +         * for an element
4706 +         * @param basis the identity (initial default value) for the reduction
4707 +         * @param reducer a commutative associative combining function
4708 +         * @return the task
4709 +         */
4710 +        public static <K,V> ForkJoinTask<Integer> reduceValuesToInt
4711 +            (ConcurrentHashMapV8<K,V> map,
4712 +             ObjectToInt<? super V> transformer,
4713 +             int basis,
4714 +             IntByIntToInt reducer) {
4715 +            if (transformer == null || reducer == null)
4716 +                throw new NullPointerException();
4717 +            return new MapReduceValuesToIntTask<K,V>
4718 +                (map, null, -1, null, transformer, basis, reducer);
4719 +        }
4720 +
4721 +        /**
4722 +         * Returns a task that when invoked, perform the given action
4723 +         * for each entry.
4724 +         *
4725 +         * @param map the map
4726 +         * @param action the action
4727 +         */
4728 +        public static <K,V> ForkJoinTask<Void> forEachEntry
4729 +            (ConcurrentHashMapV8<K,V> map,
4730 +             Action<Map.Entry<K,V>> action) {
4731 +            if (action == null) throw new NullPointerException();
4732 +            return new ForEachEntryTask<K,V>(map, null, -1, null, action);
4733 +        }
4734 +
4735 +        /**
4736 +         * Returns a task that when invoked, perform the given action
4737 +         * for each non-null transformation of each entry.
4738 +         *
4739 +         * @param map the map
4740 +         * @param transformer a function returning the transformation
4741 +         * for an element, or null if there is no transformation (in
4742 +         * which case the action is not applied)
4743 +         * @param action the action
4744 +         */
4745 +        public static <K,V,U> ForkJoinTask<Void> forEachEntry
4746 +            (ConcurrentHashMapV8<K,V> map,
4747 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
4748 +             Action<U> action) {
4749 +            if (transformer == null || action == null)
4750 +                throw new NullPointerException();
4751 +            return new ForEachTransformedEntryTask<K,V,U>
4752 +                (map, null, -1, null, transformer, action);
4753 +        }
4754 +
4755 +        /**
4756 +         * Returns a task that when invoked, returns a non-null result
4757 +         * from applying the given search function on each entry, or
4758 +         * null if none.  Upon success, further element processing is
4759 +         * suppressed and the results of any other parallel
4760 +         * invocations of the search function are ignored.
4761 +         *
4762 +         * @param map the map
4763 +         * @param searchFunction a function returning a non-null
4764 +         * result on success, else null
4765 +         * @return the task
4766 +         */
4767 +        public static <K,V,U> ForkJoinTask<U> searchEntries
4768 +            (ConcurrentHashMapV8<K,V> map,
4769 +             Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4770 +            if (searchFunction == null) throw new NullPointerException();
4771 +            return new SearchEntriesTask<K,V,U>
4772 +                (map, null, -1, null, searchFunction,
4773 +                 new AtomicReference<U>());
4774 +        }
4775 +
4776 +        /**
4777 +         * Returns a task that when invoked, returns the result of
4778 +         * accumulating all entries using the given reducer to combine
4779 +         * values, or null if none.
4780 +         *
4781 +         * @param map the map
4782 +         * @param reducer a commutative associative combining function
4783 +         * @return the task
4784 +         */
4785 +        public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries
4786 +            (ConcurrentHashMapV8<K,V> map,
4787 +             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4788 +            if (reducer == null) throw new NullPointerException();
4789 +            return new ReduceEntriesTask<K,V>
4790 +                (map, null, -1, null, reducer);
4791 +        }
4792 +
4793 +        /**
4794 +         * Returns a task that when invoked, returns the result of
4795 +         * accumulating the given transformation of all entries using the
4796 +         * given reducer to combine values, or null if none.
4797 +         *
4798 +         * @param map the map
4799 +         * @param transformer a function returning the transformation
4800 +         * for an element, or null if there is no transformation (in
4801 +         * which case it is not combined).
4802 +         * @param reducer a commutative associative combining function
4803 +         * @return the task
4804 +         */
4805 +        public static <K,V,U> ForkJoinTask<U> reduceEntries
4806 +            (ConcurrentHashMapV8<K,V> map,
4807 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
4808 +             BiFun<? super U, ? super U, ? extends U> reducer) {
4809 +            if (transformer == null || reducer == null)
4810 +                throw new NullPointerException();
4811 +            return new MapReduceEntriesTask<K,V,U>
4812 +                (map, null, -1, null, transformer, reducer);
4813 +        }
4814 +
4815 +        /**
4816 +         * Returns a task that when invoked, returns the result of
4817 +         * accumulating the given transformation of all entries using the
4818 +         * given reducer to combine values, and the given basis as an
4819 +         * identity value.
4820 +         *
4821 +         * @param map the map
4822 +         * @param transformer a function returning the transformation
4823 +         * for an element
4824 +         * @param basis the identity (initial default value) for the reduction
4825 +         * @param reducer a commutative associative combining function
4826 +         * @return the task
4827 +         */
4828 +        public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble
4829 +            (ConcurrentHashMapV8<K,V> map,
4830 +             ObjectToDouble<Map.Entry<K,V>> transformer,
4831 +             double basis,
4832 +             DoubleByDoubleToDouble reducer) {
4833 +            if (transformer == null || reducer == null)
4834 +                throw new NullPointerException();
4835 +            return new MapReduceEntriesToDoubleTask<K,V>
4836 +                (map, null, -1, null, transformer, basis, reducer);
4837 +        }
4838 +
4839 +        /**
4840 +         * Returns a task that when invoked, returns the result of
4841 +         * accumulating the given transformation of all entries using the
4842 +         * given reducer to combine values, and the given basis as an
4843 +         * identity value.
4844 +         *
4845 +         * @param map the map
4846 +         * @param transformer a function returning the transformation
4847 +         * for an element
4848 +         * @param basis the identity (initial default value) for the reduction
4849 +         * @param reducer a commutative associative combining function
4850 +         * @return the task
4851 +         */
4852 +        public static <K,V> ForkJoinTask<Long> reduceEntriesToLong
4853 +            (ConcurrentHashMapV8<K,V> map,
4854 +             ObjectToLong<Map.Entry<K,V>> transformer,
4855 +             long basis,
4856 +             LongByLongToLong reducer) {
4857 +            if (transformer == null || reducer == null)
4858 +                throw new NullPointerException();
4859 +            return new MapReduceEntriesToLongTask<K,V>
4860 +                (map, null, -1, null, transformer, basis, reducer);
4861 +        }
4862 +
4863 +        /**
4864 +         * Returns a task that when invoked, returns the result of
4865 +         * accumulating the given transformation of all entries using the
4866 +         * given reducer to combine values, and the given basis as an
4867 +         * identity value.
4868 +         *
4869 +         * @param map the map
4870 +         * @param transformer a function returning the transformation
4871 +         * for an element
4872 +         * @param basis the identity (initial default value) for the reduction
4873 +         * @param reducer a commutative associative combining function
4874 +         * @return the task
4875 +         */
4876 +        public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt
4877 +            (ConcurrentHashMapV8<K,V> map,
4878 +             ObjectToInt<Map.Entry<K,V>> transformer,
4879 +             int basis,
4880 +             IntByIntToInt reducer) {
4881 +            if (transformer == null || reducer == null)
4882 +                throw new NullPointerException();
4883 +            return new MapReduceEntriesToIntTask<K,V>
4884 +                (map, null, -1, null, transformer, basis, reducer);
4885 +        }
4886 +    }
4887 +
4888 +    // -------------------------------------------------------
4889 +
4890 +    /**
4891 +     * Base for FJ tasks for bulk operations. This adds a variant of
4892 +     * CountedCompleters and some split and merge bookkeeping to
4893 +     * iterator functionality. The forEach and reduce methods are
4894 +     * similar to those illustrated in CountedCompleter documentation,
4895 +     * except that bottom-up reduction completions perform them within
4896 +     * their compute methods. The search methods are like forEach
4897 +     * except they continually poll for success and exit early.  Also,
4898 +     * exceptions are handled in a simpler manner, by just trying to
4899 +     * complete root task exceptionally.
4900 +     */
4901 +    @SuppressWarnings("serial") static abstract class BulkTask<K,V,R> extends Traverser<K,V,R> {
4902 +        final BulkTask<K,V,?> parent;  // completion target
4903 +        int batch;                     // split control; -1 for unknown
4904 +        int pending;                   // completion control
4905 +
4906 +        BulkTask(ConcurrentHashMapV8<K,V> map, BulkTask<K,V,?> parent,
4907 +                 int batch) {
4908 +            super(map);
4909 +            this.parent = parent;
4910 +            this.batch = batch;
4911 +            if (parent != null && map != null) { // split parent
4912 +                Node[] t;
4913 +                if ((t = parent.tab) == null &&
4914 +                    (t = parent.tab = map.table) != null)
4915 +                    parent.baseLimit = parent.baseSize = t.length;
4916 +                this.tab = t;
4917 +                this.baseSize = parent.baseSize;
4918 +                int hi = this.baseLimit = parent.baseLimit;
4919 +                parent.baseLimit = this.index = this.baseIndex =
4920 +                    (hi + parent.baseIndex + 1) >>> 1;
4921 +            }
4922 +        }
4923 +
4924 +        /**
4925 +         * Forces root task to complete.
4926 +         * @param ex if null, complete normally, else exceptionally
4927 +         * @return false to simplify use
4928 +         */
4929 +        final boolean tryCompleteComputation(Throwable ex) {
4930 +            for (BulkTask<K,V,?> a = this;;) {
4931 +                BulkTask<K,V,?> p = a.parent;
4932 +                if (p == null) {
4933 +                    if (ex != null)
4934 +                        a.completeExceptionally(ex);
4935 +                    else
4936 +                        a.quietlyComplete();
4937 +                    return false;
4938 +                }
4939 +                a = p;
4940 +            }
4941 +        }
4942 +
4943 +        /**
4944 +         * Version of tryCompleteComputation for function screening checks
4945 +         */
4946 +        final boolean abortOnNullFunction() {
4947 +            return tryCompleteComputation(new Error("Unexpected null function"));
4948 +        }
4949 +
4950 +        // utilities
4951 +
4952 +        /** CompareAndSet pending count */
4953 +        final boolean casPending(int cmp, int val) {
4954 +            return U.compareAndSwapInt(this, PENDING, cmp, val);
4955 +        }
4956 +
4957 +        /**
4958 +         * Returns approx exp2 of the number of times (minus one) to
4959 +         * split task by two before executing leaf action. This value
4960 +         * is faster to compute and more convenient to use as a guide
4961 +         * to splitting than is the depth, since it is used while
4962 +         * dividing by two anyway.
4963 +         */
4964 +        final int batch() {
4965 +            ConcurrentHashMapV8<K, V> m; int b; Node[] t;  ForkJoinPool pool;
4966 +            if ((b = batch) < 0 && (m = map) != null) { // force initialization
4967 +                if ((t = tab) == null && (t = tab = m.table) != null)
4968 +                    baseLimit = baseSize = t.length;
4969 +                if (t != null) {
4970 +                    long n = m.counter.sum();
4971 +                    int par = (pool = getPool()) == null?
4972 +                        ForkJoinPool.getCommonPoolParallelism() :
4973 +                        pool.getParallelism();
4974 +                    int sp = par << 3; // slack of 8
4975 +                    b = batch = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp;
4976 +                }
4977 +            }
4978 +            return b;
4979 +        }
4980 +
4981 +        /**
4982 +         * Returns exportable snapshot entry.
4983 +         */
4984 +        static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
4985 +            return new AbstractMap.SimpleEntry<K,V>(k, v);
4986 +        }
4987 +
4988 +        // Unsafe mechanics
4989 +        private static final sun.misc.Unsafe U;
4990 +        private static final long PENDING;
4991 +        static {
4992 +            try {
4993 +                U = getUnsafe();
4994 +                PENDING = U.objectFieldOffset
4995 +                    (BulkTask.class.getDeclaredField("pending"));
4996 +            } catch (Exception e) {
4997 +                throw new Error(e);
4998 +            }
4999 +        }
5000 +    }
5001 +
5002 +    /**
5003 +     * Base class for non-reductive actions
5004 +     */
5005 +    @SuppressWarnings("serial") static abstract class BulkAction<K,V,R> extends BulkTask<K,V,R> {
5006 +        BulkAction<K,V,?> nextTask;
5007 +        BulkAction(ConcurrentHashMapV8<K,V> map, BulkTask<K,V,?> parent,
5008 +                   int batch, BulkAction<K,V,?> nextTask) {
5009 +            super(map, parent, batch);
5010 +            this.nextTask = nextTask;
5011 +        }
5012 +
5013 +        /**
5014 +         * Try to complete task and upward parents. Upon hitting
5015 +         * non-completed parent, if a non-FJ task, try to help out the
5016 +         * computation.
5017 +         */
5018 +        final void tryComplete(BulkAction<K,V,?> subtasks) {
5019 +            BulkTask<K,V,?> a = this, s = a;
5020 +            for (int c;;) {
5021 +                if ((c = a.pending) == 0) {
5022 +                    if ((a = (s = a).parent) == null) {
5023 +                        s.quietlyComplete();
5024 +                        break;
5025 +                    }
5026 +                }
5027 +                else if (a.casPending(c, c - 1)) {
5028 +                    if (subtasks != null && !inForkJoinPool()) {
5029 +                        while ((s = a.parent) != null)
5030 +                            a = s;
5031 +                        while (!a.isDone()) {
5032 +                            BulkAction<K,V,?> next = subtasks.nextTask;
5033 +                            if (subtasks.tryUnfork())
5034 +                                subtasks.exec();
5035 +                            if ((subtasks = next) == null)
5036 +                                break;
5037 +                        }
5038 +                    }
5039 +                    break;
5040 +                }
5041 +            }
5042 +        }
5043 +
5044 +    }
5045 +
5046 +    /*
5047 +     * Task classes. Coded in a regular but ugly format/style to
5048 +     * simplify checks that each variant differs in the right way from
5049 +     * others.
5050 +     */
5051 +
5052 +    @SuppressWarnings("serial") static final class ForEachKeyTask<K,V>
5053 +        extends BulkAction<K,V,Void> {
5054 +        final Action<K> action;
5055 +        ForEachKeyTask
5056 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5057 +             ForEachKeyTask<K,V> nextTask,
5058 +             Action<K> action) {
5059 +            super(m, p, b, nextTask);
5060 +            this.action = action;
5061 +        }
5062 +        @SuppressWarnings("unchecked") public final boolean exec() {
5063 +            final Action<K> action = this.action;
5064 +            if (action == null)
5065 +                return abortOnNullFunction();
5066 +            ForEachKeyTask<K,V> subtasks = null;
5067 +            try {
5068 +                int b = batch(), c;
5069 +                while (b > 1 && baseIndex != baseLimit) {
5070 +                    do {} while (!casPending(c = pending, c+1));
5071 +                    (subtasks = new ForEachKeyTask<K,V>
5072 +                     (map, this, b >>>= 1, subtasks, action)).fork();
5073 +                }
5074 +                while (advance() != null)
5075 +                    action.apply((K)nextKey);
5076 +            } catch (Throwable ex) {
5077 +                return tryCompleteComputation(ex);
5078 +            }
5079 +            tryComplete(subtasks);
5080 +            return false;
5081 +        }
5082 +    }
5083 +
5084 +    @SuppressWarnings("serial") static final class ForEachValueTask<K,V>
5085 +        extends BulkAction<K,V,Void> {
5086 +        final Action<V> action;
5087 +        ForEachValueTask
5088 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5089 +             ForEachValueTask<K,V> nextTask,
5090 +             Action<V> action) {
5091 +            super(m, p, b, nextTask);
5092 +            this.action = action;
5093 +        }
5094 +        @SuppressWarnings("unchecked") public final boolean exec() {
5095 +            final Action<V> action = this.action;
5096 +            if (action == null)
5097 +                return abortOnNullFunction();
5098 +            ForEachValueTask<K,V> subtasks = null;
5099 +            try {
5100 +                int b = batch(), c;
5101 +                while (b > 1 && baseIndex != baseLimit) {
5102 +                    do {} while (!casPending(c = pending, c+1));
5103 +                    (subtasks = new ForEachValueTask<K,V>
5104 +                     (map, this, b >>>= 1, subtasks, action)).fork();
5105 +                }
5106 +                Object v;
5107 +                while ((v = advance()) != null)
5108 +                    action.apply((V)v);
5109 +            } catch (Throwable ex) {
5110 +                return tryCompleteComputation(ex);
5111 +            }
5112 +            tryComplete(subtasks);
5113 +            return false;
5114 +        }
5115 +    }
5116 +
5117 +    @SuppressWarnings("serial") static final class ForEachEntryTask<K,V>
5118 +        extends BulkAction<K,V,Void> {
5119 +        final Action<Entry<K,V>> action;
5120 +        ForEachEntryTask
5121 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5122 +             ForEachEntryTask<K,V> nextTask,
5123 +             Action<Entry<K,V>> action) {
5124 +            super(m, p, b, nextTask);
5125 +            this.action = action;
5126 +        }
5127 +        @SuppressWarnings("unchecked") public final boolean exec() {
5128 +            final Action<Entry<K,V>> action = this.action;
5129 +            if (action == null)
5130 +                return abortOnNullFunction();
5131 +            ForEachEntryTask<K,V> subtasks = null;
5132 +            try {
5133 +                int b = batch(), c;
5134 +                while (b > 1 && baseIndex != baseLimit) {
5135 +                    do {} while (!casPending(c = pending, c+1));
5136 +                    (subtasks = new ForEachEntryTask<K,V>
5137 +                     (map, this, b >>>= 1, subtasks, action)).fork();
5138 +                }
5139 +                Object v;
5140 +                while ((v = advance()) != null)
5141 +                    action.apply(entryFor((K)nextKey, (V)v));
5142 +            } catch (Throwable ex) {
5143 +                return tryCompleteComputation(ex);
5144 +            }
5145 +            tryComplete(subtasks);
5146 +            return false;
5147 +        }
5148 +    }
5149 +
5150 +    @SuppressWarnings("serial") static final class ForEachMappingTask<K,V>
5151 +        extends BulkAction<K,V,Void> {
5152 +        final BiAction<K,V> action;
5153 +        ForEachMappingTask
5154 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5155 +             ForEachMappingTask<K,V> nextTask,
5156 +             BiAction<K,V> action) {
5157 +            super(m, p, b, nextTask);
5158 +            this.action = action;
5159 +        }
5160 +        @SuppressWarnings("unchecked") public final boolean exec() {
5161 +            final BiAction<K,V> action = this.action;
5162 +            if (action == null)
5163 +                return abortOnNullFunction();
5164 +            ForEachMappingTask<K,V> subtasks = null;
5165 +            try {
5166 +                int b = batch(), c;
5167 +                while (b > 1 && baseIndex != baseLimit) {
5168 +                    do {} while (!casPending(c = pending, c+1));
5169 +                    (subtasks = new ForEachMappingTask<K,V>
5170 +                     (map, this, b >>>= 1, subtasks, action)).fork();
5171 +                }
5172 +                Object v;
5173 +                while ((v = advance()) != null)
5174 +                    action.apply((K)nextKey, (V)v);
5175 +            } catch (Throwable ex) {
5176 +                return tryCompleteComputation(ex);
5177 +            }
5178 +            tryComplete(subtasks);
5179 +            return false;
5180 +        }
5181 +    }
5182 +
5183 +    @SuppressWarnings("serial") static final class ForEachTransformedKeyTask<K,V,U>
5184 +        extends BulkAction<K,V,Void> {
5185 +        final Fun<? super K, ? extends U> transformer;
5186 +        final Action<U> action;
5187 +        ForEachTransformedKeyTask
5188 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5189 +             ForEachTransformedKeyTask<K,V,U> nextTask,
5190 +             Fun<? super K, ? extends U> transformer,
5191 +             Action<U> action) {
5192 +            super(m, p, b, nextTask);
5193 +            this.transformer = transformer;
5194 +            this.action = action;
5195 +
5196 +        }
5197 +        @SuppressWarnings("unchecked") public final boolean exec() {
5198 +            final Fun<? super K, ? extends U> transformer =
5199 +                this.transformer;
5200 +            final Action<U> action = this.action;
5201 +            if (transformer == null || action == null)
5202 +                return abortOnNullFunction();
5203 +            ForEachTransformedKeyTask<K,V,U> subtasks = null;
5204 +            try {
5205 +                int b = batch(), c;
5206 +                while (b > 1 && baseIndex != baseLimit) {
5207 +                    do {} while (!casPending(c = pending, c+1));
5208 +                    (subtasks = new ForEachTransformedKeyTask<K,V,U>
5209 +                     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5210 +                }
5211 +                U u;
5212 +                while (advance() != null) {
5213 +                    if ((u = transformer.apply((K)nextKey)) != null)
5214 +                        action.apply(u);
5215 +                }
5216 +            } catch (Throwable ex) {
5217 +                return tryCompleteComputation(ex);
5218 +            }
5219 +            tryComplete(subtasks);
5220 +            return false;
5221 +        }
5222 +    }
5223 +
5224 +    @SuppressWarnings("serial") static final class ForEachTransformedValueTask<K,V,U>
5225 +        extends BulkAction<K,V,Void> {
5226 +        final Fun<? super V, ? extends U> transformer;
5227 +        final Action<U> action;
5228 +        ForEachTransformedValueTask
5229 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5230 +             ForEachTransformedValueTask<K,V,U> nextTask,
5231 +             Fun<? super V, ? extends U> transformer,
5232 +             Action<U> action) {
5233 +            super(m, p, b, nextTask);
5234 +            this.transformer = transformer;
5235 +            this.action = action;
5236 +
5237 +        }
5238 +        @SuppressWarnings("unchecked") public final boolean exec() {
5239 +            final Fun<? super V, ? extends U> transformer =
5240 +                this.transformer;
5241 +            final Action<U> action = this.action;
5242 +            if (transformer == null || action == null)
5243 +                return abortOnNullFunction();
5244 +            ForEachTransformedValueTask<K,V,U> subtasks = null;
5245 +            try {
5246 +                int b = batch(), c;
5247 +                while (b > 1 && baseIndex != baseLimit) {
5248 +                    do {} while (!casPending(c = pending, c+1));
5249 +                    (subtasks = new ForEachTransformedValueTask<K,V,U>
5250 +                     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5251 +                }
5252 +                Object v; U u;
5253 +                while ((v = advance()) != null) {
5254 +                    if ((u = transformer.apply((V)v)) != null)
5255 +                        action.apply(u);
5256 +                }
5257 +            } catch (Throwable ex) {
5258 +                return tryCompleteComputation(ex);
5259 +            }
5260 +            tryComplete(subtasks);
5261 +            return false;
5262 +        }
5263 +    }
5264 +
5265 +    @SuppressWarnings("serial") static final class ForEachTransformedEntryTask<K,V,U>
5266 +        extends BulkAction<K,V,Void> {
5267 +        final Fun<Map.Entry<K,V>, ? extends U> transformer;
5268 +        final Action<U> action;
5269 +        ForEachTransformedEntryTask
5270 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5271 +             ForEachTransformedEntryTask<K,V,U> nextTask,
5272 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
5273 +             Action<U> action) {
5274 +            super(m, p, b, nextTask);
5275 +            this.transformer = transformer;
5276 +            this.action = action;
5277 +
5278 +        }
5279 +        @SuppressWarnings("unchecked") public final boolean exec() {
5280 +            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5281 +                this.transformer;
5282 +            final Action<U> action = this.action;
5283 +            if (transformer == null || action == null)
5284 +                return abortOnNullFunction();
5285 +            ForEachTransformedEntryTask<K,V,U> subtasks = null;
5286 +            try {
5287 +                int b = batch(), c;
5288 +                while (b > 1 && baseIndex != baseLimit) {
5289 +                    do {} while (!casPending(c = pending, c+1));
5290 +                    (subtasks = new ForEachTransformedEntryTask<K,V,U>
5291 +                     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5292 +                }
5293 +                Object v; U u;
5294 +                while ((v = advance()) != null) {
5295 +                    if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5296 +                        action.apply(u);
5297 +                }
5298 +            } catch (Throwable ex) {
5299 +                return tryCompleteComputation(ex);
5300 +            }
5301 +            tryComplete(subtasks);
5302 +            return false;
5303 +        }
5304 +    }
5305 +
5306 +    @SuppressWarnings("serial") static final class ForEachTransformedMappingTask<K,V,U>
5307 +        extends BulkAction<K,V,Void> {
5308 +        final BiFun<? super K, ? super V, ? extends U> transformer;
5309 +        final Action<U> action;
5310 +        ForEachTransformedMappingTask
5311 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5312 +             ForEachTransformedMappingTask<K,V,U> nextTask,
5313 +             BiFun<? super K, ? super V, ? extends U> transformer,
5314 +             Action<U> action) {
5315 +            super(m, p, b, nextTask);
5316 +            this.transformer = transformer;
5317 +            this.action = action;
5318 +
5319 +        }
5320 +        @SuppressWarnings("unchecked") public final boolean exec() {
5321 +            final BiFun<? super K, ? super V, ? extends U> transformer =
5322 +                this.transformer;
5323 +            final Action<U> action = this.action;
5324 +            if (transformer == null || action == null)
5325 +                return abortOnNullFunction();
5326 +            ForEachTransformedMappingTask<K,V,U> subtasks = null;
5327 +            try {
5328 +                int b = batch(), c;
5329 +                while (b > 1 && baseIndex != baseLimit) {
5330 +                    do {} while (!casPending(c = pending, c+1));
5331 +                    (subtasks = new ForEachTransformedMappingTask<K,V,U>
5332 +                     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5333 +                }
5334 +                Object v; U u;
5335 +                while ((v = advance()) != null) {
5336 +                    if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5337 +                        action.apply(u);
5338 +                }
5339 +            } catch (Throwable ex) {
5340 +                return tryCompleteComputation(ex);
5341 +            }
5342 +            tryComplete(subtasks);
5343 +            return false;
5344 +        }
5345 +    }
5346 +
5347 +    @SuppressWarnings("serial") static final class SearchKeysTask<K,V,U>
5348 +        extends BulkAction<K,V,U> {
5349 +        final Fun<? super K, ? extends U> searchFunction;
5350 +        final AtomicReference<U> result;
5351 +        SearchKeysTask
5352 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5353 +             SearchKeysTask<K,V,U> nextTask,
5354 +             Fun<? super K, ? extends U> searchFunction,
5355 +             AtomicReference<U> result) {
5356 +            super(m, p, b, nextTask);
5357 +            this.searchFunction = searchFunction; this.result = result;
5358 +        }
5359 +        @SuppressWarnings("unchecked") public final boolean exec() {
5360 +            AtomicReference<U> result = this.result;
5361 +            final Fun<? super K, ? extends U> searchFunction =
5362 +                this.searchFunction;
5363 +            if (searchFunction == null || result == null)
5364 +                return abortOnNullFunction();
5365 +            SearchKeysTask<K,V,U> subtasks = null;
5366 +            try {
5367 +                int b = batch(), c;
5368 +                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5369 +                    do {} while (!casPending(c = pending, c+1));
5370 +                    (subtasks = new SearchKeysTask<K,V,U>
5371 +                     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5372 +                }
5373 +                U u;
5374 +                while (result.get() == null && advance() != null) {
5375 +                    if ((u = searchFunction.apply((K)nextKey)) != null) {
5376 +                        if (result.compareAndSet(null, u))
5377 +                            tryCompleteComputation(null);
5378 +                        break;
5379 +                    }
5380 +                }
5381 +            } catch (Throwable ex) {
5382 +                return tryCompleteComputation(ex);
5383 +            }
5384 +            tryComplete(subtasks);
5385 +            return false;
5386 +        }
5387 +        public final U getRawResult() { return result.get(); }
5388 +    }
5389 +
5390 +    @SuppressWarnings("serial") static final class SearchValuesTask<K,V,U>
5391 +        extends BulkAction<K,V,U> {
5392 +        final Fun<? super V, ? extends U> searchFunction;
5393 +        final AtomicReference<U> result;
5394 +        SearchValuesTask
5395 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5396 +             SearchValuesTask<K,V,U> nextTask,
5397 +             Fun<? super V, ? extends U> searchFunction,
5398 +             AtomicReference<U> result) {
5399 +            super(m, p, b, nextTask);
5400 +            this.searchFunction = searchFunction; this.result = result;
5401 +        }
5402 +        @SuppressWarnings("unchecked") public final boolean exec() {
5403 +            AtomicReference<U> result = this.result;
5404 +            final Fun<? super V, ? extends U> searchFunction =
5405 +                this.searchFunction;
5406 +            if (searchFunction == null || result == null)
5407 +                return abortOnNullFunction();
5408 +            SearchValuesTask<K,V,U> subtasks = null;
5409 +            try {
5410 +                int b = batch(), c;
5411 +                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5412 +                    do {} while (!casPending(c = pending, c+1));
5413 +                    (subtasks = new SearchValuesTask<K,V,U>
5414 +                     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5415 +                }
5416 +                Object v; U u;
5417 +                while (result.get() == null && (v = advance()) != null) {
5418 +                    if ((u = searchFunction.apply((V)v)) != null) {
5419 +                        if (result.compareAndSet(null, u))
5420 +                            tryCompleteComputation(null);
5421 +                        break;
5422 +                    }
5423 +                }
5424 +            } catch (Throwable ex) {
5425 +                return tryCompleteComputation(ex);
5426 +            }
5427 +            tryComplete(subtasks);
5428 +            return false;
5429 +        }
5430 +        public final U getRawResult() { return result.get(); }
5431 +    }
5432 +
5433 +    @SuppressWarnings("serial") static final class SearchEntriesTask<K,V,U>
5434 +        extends BulkAction<K,V,U> {
5435 +        final Fun<Entry<K,V>, ? extends U> searchFunction;
5436 +        final AtomicReference<U> result;
5437 +        SearchEntriesTask
5438 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5439 +             SearchEntriesTask<K,V,U> nextTask,
5440 +             Fun<Entry<K,V>, ? extends U> searchFunction,
5441 +             AtomicReference<U> result) {
5442 +            super(m, p, b, nextTask);
5443 +            this.searchFunction = searchFunction; this.result = result;
5444 +        }
5445 +        @SuppressWarnings("unchecked") public final boolean exec() {
5446 +            AtomicReference<U> result = this.result;
5447 +            final Fun<Entry<K,V>, ? extends U> searchFunction =
5448 +                this.searchFunction;
5449 +            if (searchFunction == null || result == null)
5450 +                return abortOnNullFunction();
5451 +            SearchEntriesTask<K,V,U> subtasks = null;
5452 +            try {
5453 +                int b = batch(), c;
5454 +                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5455 +                    do {} while (!casPending(c = pending, c+1));
5456 +                    (subtasks = new SearchEntriesTask<K,V,U>
5457 +                     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5458 +                }
5459 +                Object v; U u;
5460 +                while (result.get() == null && (v = advance()) != null) {
5461 +                    if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
5462 +                        if (result.compareAndSet(null, u))
5463 +                            tryCompleteComputation(null);
5464 +                        break;
5465 +                    }
5466 +                }
5467 +            } catch (Throwable ex) {
5468 +                return tryCompleteComputation(ex);
5469 +            }
5470 +            tryComplete(subtasks);
5471 +            return false;
5472 +        }
5473 +        public final U getRawResult() { return result.get(); }
5474 +    }
5475 +
5476 +    @SuppressWarnings("serial") static final class SearchMappingsTask<K,V,U>
5477 +        extends BulkAction<K,V,U> {
5478 +        final BiFun<? super K, ? super V, ? extends U> searchFunction;
5479 +        final AtomicReference<U> result;
5480 +        SearchMappingsTask
5481 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5482 +             SearchMappingsTask<K,V,U> nextTask,
5483 +             BiFun<? super K, ? super V, ? extends U> searchFunction,
5484 +             AtomicReference<U> result) {
5485 +            super(m, p, b, nextTask);
5486 +            this.searchFunction = searchFunction; this.result = result;
5487 +        }
5488 +        @SuppressWarnings("unchecked") public final boolean exec() {
5489 +            AtomicReference<U> result = this.result;
5490 +            final BiFun<? super K, ? super V, ? extends U> searchFunction =
5491 +                this.searchFunction;
5492 +            if (searchFunction == null || result == null)
5493 +                return abortOnNullFunction();
5494 +            SearchMappingsTask<K,V,U> subtasks = null;
5495 +            try {
5496 +                int b = batch(), c;
5497 +                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5498 +                    do {} while (!casPending(c = pending, c+1));
5499 +                    (subtasks = new SearchMappingsTask<K,V,U>
5500 +                     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5501 +                }
5502 +                Object v; U u;
5503 +                while (result.get() == null && (v = advance()) != null) {
5504 +                    if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5505 +                        if (result.compareAndSet(null, u))
5506 +                            tryCompleteComputation(null);
5507 +                        break;
5508 +                    }
5509 +                }
5510 +            } catch (Throwable ex) {
5511 +                return tryCompleteComputation(ex);
5512 +            }
5513 +            tryComplete(subtasks);
5514 +            return false;
5515 +        }
5516 +        public final U getRawResult() { return result.get(); }
5517 +    }
5518 +
5519 +    @SuppressWarnings("serial") static final class ReduceKeysTask<K,V>
5520 +        extends BulkTask<K,V,K> {
5521 +        final BiFun<? super K, ? super K, ? extends K> reducer;
5522 +        K result;
5523 +        ReduceKeysTask<K,V> rights, nextRight;
5524 +        ReduceKeysTask
5525 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5526 +             ReduceKeysTask<K,V> nextRight,
5527 +             BiFun<? super K, ? super K, ? extends K> reducer) {
5528 +            super(m, p, b); this.nextRight = nextRight;
5529 +            this.reducer = reducer;
5530 +        }
5531 +        @SuppressWarnings("unchecked") public final boolean exec() {
5532 +            final BiFun<? super K, ? super K, ? extends K> reducer =
5533 +                this.reducer;
5534 +            if (reducer == null)
5535 +                return abortOnNullFunction();
5536 +            try {
5537 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5538 +                    do {} while (!casPending(c = pending, c+1));
5539 +                    (rights = new ReduceKeysTask<K,V>
5540 +                     (map, this, b >>>= 1, rights, reducer)).fork();
5541 +                }
5542 +                K r = null;
5543 +                while (advance() != null) {
5544 +                    K u = (K)nextKey;
5545 +                    r = (r == null) ? u : reducer.apply(r, u);
5546 +                }
5547 +                result = r;
5548 +                for (ReduceKeysTask<K,V> t = this, s;;) {
5549 +                    int c; BulkTask<K,V,?> par; K tr, sr;
5550 +                    if ((c = t.pending) == 0) {
5551 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5552 +                            if ((sr = s.result) != null)
5553 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5554 +                        }
5555 +                        if ((par = t.parent) == null ||
5556 +                            !(par instanceof ReduceKeysTask)) {
5557 +                            t.quietlyComplete();
5558 +                            break;
5559 +                        }
5560 +                        t = (ReduceKeysTask<K,V>)par;
5561 +                    }
5562 +                    else if (t.casPending(c, c - 1))
5563 +                        break;
5564 +                }
5565 +            } catch (Throwable ex) {
5566 +                return tryCompleteComputation(ex);
5567 +            }
5568 +            ReduceKeysTask<K,V> s = rights;
5569 +            if (s != null && !inForkJoinPool()) {
5570 +                do  {
5571 +                    if (s.tryUnfork())
5572 +                        s.exec();
5573 +                } while ((s = s.nextRight) != null);
5574 +            }
5575 +            return false;
5576 +        }
5577 +        public final K getRawResult() { return result; }
5578 +    }
5579 +
5580 +    @SuppressWarnings("serial") static final class ReduceValuesTask<K,V>
5581 +        extends BulkTask<K,V,V> {
5582 +        final BiFun<? super V, ? super V, ? extends V> reducer;
5583 +        V result;
5584 +        ReduceValuesTask<K,V> rights, nextRight;
5585 +        ReduceValuesTask
5586 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5587 +             ReduceValuesTask<K,V> nextRight,
5588 +             BiFun<? super V, ? super V, ? extends V> reducer) {
5589 +            super(m, p, b); this.nextRight = nextRight;
5590 +            this.reducer = reducer;
5591 +        }
5592 +        @SuppressWarnings("unchecked") public final boolean exec() {
5593 +            final BiFun<? super V, ? super V, ? extends V> reducer =
5594 +                this.reducer;
5595 +            if (reducer == null)
5596 +                return abortOnNullFunction();
5597 +            try {
5598 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5599 +                    do {} while (!casPending(c = pending, c+1));
5600 +                    (rights = new ReduceValuesTask<K,V>
5601 +                     (map, this, b >>>= 1, rights, reducer)).fork();
5602 +                }
5603 +                V r = null;
5604 +                Object v;
5605 +                while ((v = advance()) != null) {
5606 +                    V u = (V)v;
5607 +                    r = (r == null) ? u : reducer.apply(r, u);
5608 +                }
5609 +                result = r;
5610 +                for (ReduceValuesTask<K,V> t = this, s;;) {
5611 +                    int c; BulkTask<K,V,?> par; V tr, sr;
5612 +                    if ((c = t.pending) == 0) {
5613 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5614 +                            if ((sr = s.result) != null)
5615 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5616 +                        }
5617 +                        if ((par = t.parent) == null ||
5618 +                            !(par instanceof ReduceValuesTask)) {
5619 +                            t.quietlyComplete();
5620 +                            break;
5621 +                        }
5622 +                        t = (ReduceValuesTask<K,V>)par;
5623 +                    }
5624 +                    else if (t.casPending(c, c - 1))
5625 +                        break;
5626 +                }
5627 +            } catch (Throwable ex) {
5628 +                return tryCompleteComputation(ex);
5629 +            }
5630 +            ReduceValuesTask<K,V> s = rights;
5631 +            if (s != null && !inForkJoinPool()) {
5632 +                do  {
5633 +                    if (s.tryUnfork())
5634 +                        s.exec();
5635 +                } while ((s = s.nextRight) != null);
5636 +            }
5637 +            return false;
5638 +        }
5639 +        public final V getRawResult() { return result; }
5640 +    }
5641 +
5642 +    @SuppressWarnings("serial") static final class ReduceEntriesTask<K,V>
5643 +        extends BulkTask<K,V,Map.Entry<K,V>> {
5644 +        final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5645 +        Map.Entry<K,V> result;
5646 +        ReduceEntriesTask<K,V> rights, nextRight;
5647 +        ReduceEntriesTask
5648 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5649 +             ReduceEntriesTask<K,V> nextRight,
5650 +             BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5651 +            super(m, p, b); this.nextRight = nextRight;
5652 +            this.reducer = reducer;
5653 +        }
5654 +        @SuppressWarnings("unchecked") public final boolean exec() {
5655 +            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
5656 +                this.reducer;
5657 +            if (reducer == null)
5658 +                return abortOnNullFunction();
5659 +            try {
5660 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5661 +                    do {} while (!casPending(c = pending, c+1));
5662 +                    (rights = new ReduceEntriesTask<K,V>
5663 +                     (map, this, b >>>= 1, rights, reducer)).fork();
5664 +                }
5665 +                Map.Entry<K,V> r = null;
5666 +                Object v;
5667 +                while ((v = advance()) != null) {
5668 +                    Map.Entry<K,V> u = entryFor((K)nextKey, (V)v);
5669 +                    r = (r == null) ? u : reducer.apply(r, u);
5670 +                }
5671 +                result = r;
5672 +                for (ReduceEntriesTask<K,V> t = this, s;;) {
5673 +                    int c; BulkTask<K,V,?> par; Map.Entry<K,V> tr, sr;
5674 +                    if ((c = t.pending) == 0) {
5675 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5676 +                            if ((sr = s.result) != null)
5677 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5678 +                        }
5679 +                        if ((par = t.parent) == null ||
5680 +                            !(par instanceof ReduceEntriesTask)) {
5681 +                            t.quietlyComplete();
5682 +                            break;
5683 +                        }
5684 +                        t = (ReduceEntriesTask<K,V>)par;
5685 +                    }
5686 +                    else if (t.casPending(c, c - 1))
5687 +                        break;
5688 +                }
5689 +            } catch (Throwable ex) {
5690 +                return tryCompleteComputation(ex);
5691 +            }
5692 +            ReduceEntriesTask<K,V> s = rights;
5693 +            if (s != null && !inForkJoinPool()) {
5694 +                do  {
5695 +                    if (s.tryUnfork())
5696 +                        s.exec();
5697 +                } while ((s = s.nextRight) != null);
5698 +            }
5699 +            return false;
5700 +        }
5701 +        public final Map.Entry<K,V> getRawResult() { return result; }
5702 +    }
5703 +
5704 +    @SuppressWarnings("serial") static final class MapReduceKeysTask<K,V,U>
5705 +        extends BulkTask<K,V,U> {
5706 +        final Fun<? super K, ? extends U> transformer;
5707 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5708 +        U result;
5709 +        MapReduceKeysTask<K,V,U> rights, nextRight;
5710 +        MapReduceKeysTask
5711 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5712 +             MapReduceKeysTask<K,V,U> nextRight,
5713 +             Fun<? super K, ? extends U> transformer,
5714 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5715 +            super(m, p, b); this.nextRight = nextRight;
5716 +            this.transformer = transformer;
5717 +            this.reducer = reducer;
5718 +        }
5719 +        @SuppressWarnings("unchecked") public final boolean exec() {
5720 +            final Fun<? super K, ? extends U> transformer =
5721 +                this.transformer;
5722 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5723 +                this.reducer;
5724 +            if (transformer == null || reducer == null)
5725 +                return abortOnNullFunction();
5726 +            try {
5727 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5728 +                    do {} while (!casPending(c = pending, c+1));
5729 +                    (rights = new MapReduceKeysTask<K,V,U>
5730 +                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5731 +                }
5732 +                U r = null, u;
5733 +                while (advance() != null) {
5734 +                    if ((u = transformer.apply((K)nextKey)) != null)
5735 +                        r = (r == null) ? u : reducer.apply(r, u);
5736 +                }
5737 +                result = r;
5738 +                for (MapReduceKeysTask<K,V,U> t = this, s;;) {
5739 +                    int c; BulkTask<K,V,?> par; U tr, sr;
5740 +                    if ((c = t.pending) == 0) {
5741 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5742 +                            if ((sr = s.result) != null)
5743 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5744 +                        }
5745 +                        if ((par = t.parent) == null ||
5746 +                            !(par instanceof MapReduceKeysTask)) {
5747 +                            t.quietlyComplete();
5748 +                            break;
5749 +                        }
5750 +                        t = (MapReduceKeysTask<K,V,U>)par;
5751 +                    }
5752 +                    else if (t.casPending(c, c - 1))
5753 +                        break;
5754 +                }
5755 +            } catch (Throwable ex) {
5756 +                return tryCompleteComputation(ex);
5757 +            }
5758 +            MapReduceKeysTask<K,V,U> s = rights;
5759 +            if (s != null && !inForkJoinPool()) {
5760 +                do  {
5761 +                    if (s.tryUnfork())
5762 +                        s.exec();
5763 +                } while ((s = s.nextRight) != null);
5764 +            }
5765 +            return false;
5766 +        }
5767 +        public final U getRawResult() { return result; }
5768      }
5769  
5770 +    @SuppressWarnings("serial") static final class MapReduceValuesTask<K,V,U>
5771 +        extends BulkTask<K,V,U> {
5772 +        final Fun<? super V, ? extends U> transformer;
5773 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5774 +        U result;
5775 +        MapReduceValuesTask<K,V,U> rights, nextRight;
5776 +        MapReduceValuesTask
5777 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5778 +             MapReduceValuesTask<K,V,U> nextRight,
5779 +             Fun<? super V, ? extends U> transformer,
5780 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5781 +            super(m, p, b); this.nextRight = nextRight;
5782 +            this.transformer = transformer;
5783 +            this.reducer = reducer;
5784 +        }
5785 +        @SuppressWarnings("unchecked") public final boolean exec() {
5786 +            final Fun<? super V, ? extends U> transformer =
5787 +                this.transformer;
5788 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5789 +                this.reducer;
5790 +            if (transformer == null || reducer == null)
5791 +                return abortOnNullFunction();
5792 +            try {
5793 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5794 +                    do {} while (!casPending(c = pending, c+1));
5795 +                    (rights = new MapReduceValuesTask<K,V,U>
5796 +                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5797 +                }
5798 +                U r = null, u;
5799 +                Object v;
5800 +                while ((v = advance()) != null) {
5801 +                    if ((u = transformer.apply((V)v)) != null)
5802 +                        r = (r == null) ? u : reducer.apply(r, u);
5803 +                }
5804 +                result = r;
5805 +                for (MapReduceValuesTask<K,V,U> t = this, s;;) {
5806 +                    int c; BulkTask<K,V,?> par; U tr, sr;
5807 +                    if ((c = t.pending) == 0) {
5808 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5809 +                            if ((sr = s.result) != null)
5810 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5811 +                        }
5812 +                        if ((par = t.parent) == null ||
5813 +                            !(par instanceof MapReduceValuesTask)) {
5814 +                            t.quietlyComplete();
5815 +                            break;
5816 +                        }
5817 +                        t = (MapReduceValuesTask<K,V,U>)par;
5818 +                    }
5819 +                    else if (t.casPending(c, c - 1))
5820 +                        break;
5821 +                }
5822 +            } catch (Throwable ex) {
5823 +                return tryCompleteComputation(ex);
5824 +            }
5825 +            MapReduceValuesTask<K,V,U> s = rights;
5826 +            if (s != null && !inForkJoinPool()) {
5827 +                do  {
5828 +                    if (s.tryUnfork())
5829 +                        s.exec();
5830 +                } while ((s = s.nextRight) != null);
5831 +            }
5832 +            return false;
5833 +        }
5834 +        public final U getRawResult() { return result; }
5835 +    }
5836 +
5837 +    @SuppressWarnings("serial") static final class MapReduceEntriesTask<K,V,U>
5838 +        extends BulkTask<K,V,U> {
5839 +        final Fun<Map.Entry<K,V>, ? extends U> transformer;
5840 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5841 +        U result;
5842 +        MapReduceEntriesTask<K,V,U> rights, nextRight;
5843 +        MapReduceEntriesTask
5844 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5845 +             MapReduceEntriesTask<K,V,U> nextRight,
5846 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
5847 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5848 +            super(m, p, b); this.nextRight = nextRight;
5849 +            this.transformer = transformer;
5850 +            this.reducer = reducer;
5851 +        }
5852 +        @SuppressWarnings("unchecked") public final boolean exec() {
5853 +            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5854 +                this.transformer;
5855 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5856 +                this.reducer;
5857 +            if (transformer == null || reducer == null)
5858 +                return abortOnNullFunction();
5859 +            try {
5860 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5861 +                    do {} while (!casPending(c = pending, c+1));
5862 +                    (rights = new MapReduceEntriesTask<K,V,U>
5863 +                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5864 +                }
5865 +                U r = null, u;
5866 +                Object v;
5867 +                while ((v = advance()) != null) {
5868 +                    if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5869 +                        r = (r == null) ? u : reducer.apply(r, u);
5870 +                }
5871 +                result = r;
5872 +                for (MapReduceEntriesTask<K,V,U> t = this, s;;) {
5873 +                    int c; BulkTask<K,V,?> par; U tr, sr;
5874 +                    if ((c = t.pending) == 0) {
5875 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5876 +                            if ((sr = s.result) != null)
5877 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5878 +                        }
5879 +                        if ((par = t.parent) == null ||
5880 +                            !(par instanceof MapReduceEntriesTask)) {
5881 +                            t.quietlyComplete();
5882 +                            break;
5883 +                        }
5884 +                        t = (MapReduceEntriesTask<K,V,U>)par;
5885 +                    }
5886 +                    else if (t.casPending(c, c - 1))
5887 +                        break;
5888 +                }
5889 +            } catch (Throwable ex) {
5890 +                return tryCompleteComputation(ex);
5891 +            }
5892 +            MapReduceEntriesTask<K,V,U> s = rights;
5893 +            if (s != null && !inForkJoinPool()) {
5894 +                do  {
5895 +                    if (s.tryUnfork())
5896 +                        s.exec();
5897 +                } while ((s = s.nextRight) != null);
5898 +            }
5899 +            return false;
5900 +        }
5901 +        public final U getRawResult() { return result; }
5902 +    }
5903 +
5904 +    @SuppressWarnings("serial") static final class MapReduceMappingsTask<K,V,U>
5905 +        extends BulkTask<K,V,U> {
5906 +        final BiFun<? super K, ? super V, ? extends U> transformer;
5907 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5908 +        U result;
5909 +        MapReduceMappingsTask<K,V,U> rights, nextRight;
5910 +        MapReduceMappingsTask
5911 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5912 +             MapReduceMappingsTask<K,V,U> nextRight,
5913 +             BiFun<? super K, ? super V, ? extends U> transformer,
5914 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5915 +            super(m, p, b); this.nextRight = nextRight;
5916 +            this.transformer = transformer;
5917 +            this.reducer = reducer;
5918 +        }
5919 +        @SuppressWarnings("unchecked") public final boolean exec() {
5920 +            final BiFun<? super K, ? super V, ? extends U> transformer =
5921 +                this.transformer;
5922 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5923 +                this.reducer;
5924 +            if (transformer == null || reducer == null)
5925 +                return abortOnNullFunction();
5926 +            try {
5927 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5928 +                    do {} while (!casPending(c = pending, c+1));
5929 +                    (rights = new MapReduceMappingsTask<K,V,U>
5930 +                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5931 +                }
5932 +                U r = null, u;
5933 +                Object v;
5934 +                while ((v = advance()) != null) {
5935 +                    if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5936 +                        r = (r == null) ? u : reducer.apply(r, u);
5937 +                }
5938 +                result = r;
5939 +                for (MapReduceMappingsTask<K,V,U> t = this, s;;) {
5940 +                    int c; BulkTask<K,V,?> par; U tr, sr;
5941 +                    if ((c = t.pending) == 0) {
5942 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5943 +                            if ((sr = s.result) != null)
5944 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5945 +                        }
5946 +                        if ((par = t.parent) == null ||
5947 +                            !(par instanceof MapReduceMappingsTask)) {
5948 +                            t.quietlyComplete();
5949 +                            break;
5950 +                        }
5951 +                        t = (MapReduceMappingsTask<K,V,U>)par;
5952 +                    }
5953 +                    else if (t.casPending(c, c - 1))
5954 +                        break;
5955 +                }
5956 +            } catch (Throwable ex) {
5957 +                return tryCompleteComputation(ex);
5958 +            }
5959 +            MapReduceMappingsTask<K,V,U> s = rights;
5960 +            if (s != null && !inForkJoinPool()) {
5961 +                do  {
5962 +                    if (s.tryUnfork())
5963 +                        s.exec();
5964 +                } while ((s = s.nextRight) != null);
5965 +            }
5966 +            return false;
5967 +        }
5968 +        public final U getRawResult() { return result; }
5969 +    }
5970 +
5971 +    @SuppressWarnings("serial") static final class MapReduceKeysToDoubleTask<K,V>
5972 +        extends BulkTask<K,V,Double> {
5973 +        final ObjectToDouble<? super K> transformer;
5974 +        final DoubleByDoubleToDouble reducer;
5975 +        final double basis;
5976 +        double result;
5977 +        MapReduceKeysToDoubleTask<K,V> rights, nextRight;
5978 +        MapReduceKeysToDoubleTask
5979 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5980 +             MapReduceKeysToDoubleTask<K,V> nextRight,
5981 +             ObjectToDouble<? super K> transformer,
5982 +             double basis,
5983 +             DoubleByDoubleToDouble reducer) {
5984 +            super(m, p, b); this.nextRight = nextRight;
5985 +            this.transformer = transformer;
5986 +            this.basis = basis; this.reducer = reducer;
5987 +        }
5988 +        @SuppressWarnings("unchecked") public final boolean exec() {
5989 +            final ObjectToDouble<? super K> transformer =
5990 +                this.transformer;
5991 +            final DoubleByDoubleToDouble reducer = this.reducer;
5992 +            if (transformer == null || reducer == null)
5993 +                return abortOnNullFunction();
5994 +            try {
5995 +                final double id = this.basis;
5996 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5997 +                    do {} while (!casPending(c = pending, c+1));
5998 +                    (rights = new MapReduceKeysToDoubleTask<K,V>
5999 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6000 +                }
6001 +                double r = id;
6002 +                while (advance() != null)
6003 +                    r = reducer.apply(r, transformer.apply((K)nextKey));
6004 +                result = r;
6005 +                for (MapReduceKeysToDoubleTask<K,V> t = this, s;;) {
6006 +                    int c; BulkTask<K,V,?> par;
6007 +                    if ((c = t.pending) == 0) {
6008 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6009 +                            t.result = reducer.apply(t.result, s.result);
6010 +                        }
6011 +                        if ((par = t.parent) == null ||
6012 +                            !(par instanceof MapReduceKeysToDoubleTask)) {
6013 +                            t.quietlyComplete();
6014 +                            break;
6015 +                        }
6016 +                        t = (MapReduceKeysToDoubleTask<K,V>)par;
6017 +                    }
6018 +                    else if (t.casPending(c, c - 1))
6019 +                        break;
6020 +                }
6021 +            } catch (Throwable ex) {
6022 +                return tryCompleteComputation(ex);
6023 +            }
6024 +            MapReduceKeysToDoubleTask<K,V> s = rights;
6025 +            if (s != null && !inForkJoinPool()) {
6026 +                do  {
6027 +                    if (s.tryUnfork())
6028 +                        s.exec();
6029 +                } while ((s = s.nextRight) != null);
6030 +            }
6031 +            return false;
6032 +        }
6033 +        public final Double getRawResult() { return result; }
6034 +    }
6035 +
6036 +    @SuppressWarnings("serial") static final class MapReduceValuesToDoubleTask<K,V>
6037 +        extends BulkTask<K,V,Double> {
6038 +        final ObjectToDouble<? super V> transformer;
6039 +        final DoubleByDoubleToDouble reducer;
6040 +        final double basis;
6041 +        double result;
6042 +        MapReduceValuesToDoubleTask<K,V> rights, nextRight;
6043 +        MapReduceValuesToDoubleTask
6044 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6045 +             MapReduceValuesToDoubleTask<K,V> nextRight,
6046 +             ObjectToDouble<? super V> transformer,
6047 +             double basis,
6048 +             DoubleByDoubleToDouble reducer) {
6049 +            super(m, p, b); this.nextRight = nextRight;
6050 +            this.transformer = transformer;
6051 +            this.basis = basis; this.reducer = reducer;
6052 +        }
6053 +        @SuppressWarnings("unchecked") public final boolean exec() {
6054 +            final ObjectToDouble<? super V> transformer =
6055 +                this.transformer;
6056 +            final DoubleByDoubleToDouble reducer = this.reducer;
6057 +            if (transformer == null || reducer == null)
6058 +                return abortOnNullFunction();
6059 +            try {
6060 +                final double id = this.basis;
6061 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6062 +                    do {} while (!casPending(c = pending, c+1));
6063 +                    (rights = new MapReduceValuesToDoubleTask<K,V>
6064 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6065 +                }
6066 +                double r = id;
6067 +                Object v;
6068 +                while ((v = advance()) != null)
6069 +                    r = reducer.apply(r, transformer.apply((V)v));
6070 +                result = r;
6071 +                for (MapReduceValuesToDoubleTask<K,V> t = this, s;;) {
6072 +                    int c; BulkTask<K,V,?> par;
6073 +                    if ((c = t.pending) == 0) {
6074 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6075 +                            t.result = reducer.apply(t.result, s.result);
6076 +                        }
6077 +                        if ((par = t.parent) == null ||
6078 +                            !(par instanceof MapReduceValuesToDoubleTask)) {
6079 +                            t.quietlyComplete();
6080 +                            break;
6081 +                        }
6082 +                        t = (MapReduceValuesToDoubleTask<K,V>)par;
6083 +                    }
6084 +                    else if (t.casPending(c, c - 1))
6085 +                        break;
6086 +                }
6087 +            } catch (Throwable ex) {
6088 +                return tryCompleteComputation(ex);
6089 +            }
6090 +            MapReduceValuesToDoubleTask<K,V> s = rights;
6091 +            if (s != null && !inForkJoinPool()) {
6092 +                do  {
6093 +                    if (s.tryUnfork())
6094 +                        s.exec();
6095 +                } while ((s = s.nextRight) != null);
6096 +            }
6097 +            return false;
6098 +        }
6099 +        public final Double getRawResult() { return result; }
6100 +    }
6101 +
6102 +    @SuppressWarnings("serial") static final class MapReduceEntriesToDoubleTask<K,V>
6103 +        extends BulkTask<K,V,Double> {
6104 +        final ObjectToDouble<Map.Entry<K,V>> transformer;
6105 +        final DoubleByDoubleToDouble reducer;
6106 +        final double basis;
6107 +        double result;
6108 +        MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
6109 +        MapReduceEntriesToDoubleTask
6110 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6111 +             MapReduceEntriesToDoubleTask<K,V> nextRight,
6112 +             ObjectToDouble<Map.Entry<K,V>> transformer,
6113 +             double basis,
6114 +             DoubleByDoubleToDouble reducer) {
6115 +            super(m, p, b); this.nextRight = nextRight;
6116 +            this.transformer = transformer;
6117 +            this.basis = basis; this.reducer = reducer;
6118 +        }
6119 +        @SuppressWarnings("unchecked") public final boolean exec() {
6120 +            final ObjectToDouble<Map.Entry<K,V>> transformer =
6121 +                this.transformer;
6122 +            final DoubleByDoubleToDouble reducer = this.reducer;
6123 +            if (transformer == null || reducer == null)
6124 +                return abortOnNullFunction();
6125 +            try {
6126 +                final double id = this.basis;
6127 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6128 +                    do {} while (!casPending(c = pending, c+1));
6129 +                    (rights = new MapReduceEntriesToDoubleTask<K,V>
6130 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6131 +                }
6132 +                double r = id;
6133 +                Object v;
6134 +                while ((v = advance()) != null)
6135 +                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
6136 +                result = r;
6137 +                for (MapReduceEntriesToDoubleTask<K,V> t = this, s;;) {
6138 +                    int c; BulkTask<K,V,?> par;
6139 +                    if ((c = t.pending) == 0) {
6140 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6141 +                            t.result = reducer.apply(t.result, s.result);
6142 +                        }
6143 +                        if ((par = t.parent) == null ||
6144 +                            !(par instanceof MapReduceEntriesToDoubleTask)) {
6145 +                            t.quietlyComplete();
6146 +                            break;
6147 +                        }
6148 +                        t = (MapReduceEntriesToDoubleTask<K,V>)par;
6149 +                    }
6150 +                    else if (t.casPending(c, c - 1))
6151 +                        break;
6152 +                }
6153 +            } catch (Throwable ex) {
6154 +                return tryCompleteComputation(ex);
6155 +            }
6156 +            MapReduceEntriesToDoubleTask<K,V> s = rights;
6157 +            if (s != null && !inForkJoinPool()) {
6158 +                do  {
6159 +                    if (s.tryUnfork())
6160 +                        s.exec();
6161 +                } while ((s = s.nextRight) != null);
6162 +            }
6163 +            return false;
6164 +        }
6165 +        public final Double getRawResult() { return result; }
6166 +    }
6167 +
6168 +    @SuppressWarnings("serial") static final class MapReduceMappingsToDoubleTask<K,V>
6169 +        extends BulkTask<K,V,Double> {
6170 +        final ObjectByObjectToDouble<? super K, ? super V> transformer;
6171 +        final DoubleByDoubleToDouble reducer;
6172 +        final double basis;
6173 +        double result;
6174 +        MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
6175 +        MapReduceMappingsToDoubleTask
6176 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6177 +             MapReduceMappingsToDoubleTask<K,V> nextRight,
6178 +             ObjectByObjectToDouble<? super K, ? super V> transformer,
6179 +             double basis,
6180 +             DoubleByDoubleToDouble reducer) {
6181 +            super(m, p, b); this.nextRight = nextRight;
6182 +            this.transformer = transformer;
6183 +            this.basis = basis; this.reducer = reducer;
6184 +        }
6185 +        @SuppressWarnings("unchecked") public final boolean exec() {
6186 +            final ObjectByObjectToDouble<? super K, ? super V> transformer =
6187 +                this.transformer;
6188 +            final DoubleByDoubleToDouble reducer = this.reducer;
6189 +            if (transformer == null || reducer == null)
6190 +                return abortOnNullFunction();
6191 +            try {
6192 +                final double id = this.basis;
6193 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6194 +                    do {} while (!casPending(c = pending, c+1));
6195 +                    (rights = new MapReduceMappingsToDoubleTask<K,V>
6196 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6197 +                }
6198 +                double r = id;
6199 +                Object v;
6200 +                while ((v = advance()) != null)
6201 +                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
6202 +                result = r;
6203 +                for (MapReduceMappingsToDoubleTask<K,V> t = this, s;;) {
6204 +                    int c; BulkTask<K,V,?> par;
6205 +                    if ((c = t.pending) == 0) {
6206 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6207 +                            t.result = reducer.apply(t.result, s.result);
6208 +                        }
6209 +                        if ((par = t.parent) == null ||
6210 +                            !(par instanceof MapReduceMappingsToDoubleTask)) {
6211 +                            t.quietlyComplete();
6212 +                            break;
6213 +                        }
6214 +                        t = (MapReduceMappingsToDoubleTask<K,V>)par;
6215 +                    }
6216 +                    else if (t.casPending(c, c - 1))
6217 +                        break;
6218 +                }
6219 +            } catch (Throwable ex) {
6220 +                return tryCompleteComputation(ex);
6221 +            }
6222 +            MapReduceMappingsToDoubleTask<K,V> s = rights;
6223 +            if (s != null && !inForkJoinPool()) {
6224 +                do  {
6225 +                    if (s.tryUnfork())
6226 +                        s.exec();
6227 +                } while ((s = s.nextRight) != null);
6228 +            }
6229 +            return false;
6230 +        }
6231 +        public final Double getRawResult() { return result; }
6232 +    }
6233 +
6234 +    @SuppressWarnings("serial") static final class MapReduceKeysToLongTask<K,V>
6235 +        extends BulkTask<K,V,Long> {
6236 +        final ObjectToLong<? super K> transformer;
6237 +        final LongByLongToLong reducer;
6238 +        final long basis;
6239 +        long result;
6240 +        MapReduceKeysToLongTask<K,V> rights, nextRight;
6241 +        MapReduceKeysToLongTask
6242 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6243 +             MapReduceKeysToLongTask<K,V> nextRight,
6244 +             ObjectToLong<? super K> transformer,
6245 +             long basis,
6246 +             LongByLongToLong reducer) {
6247 +            super(m, p, b); this.nextRight = nextRight;
6248 +            this.transformer = transformer;
6249 +            this.basis = basis; this.reducer = reducer;
6250 +        }
6251 +        @SuppressWarnings("unchecked") public final boolean exec() {
6252 +            final ObjectToLong<? super K> transformer =
6253 +                this.transformer;
6254 +            final LongByLongToLong reducer = this.reducer;
6255 +            if (transformer == null || reducer == null)
6256 +                return abortOnNullFunction();
6257 +            try {
6258 +                final long id = this.basis;
6259 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6260 +                    do {} while (!casPending(c = pending, c+1));
6261 +                    (rights = new MapReduceKeysToLongTask<K,V>
6262 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6263 +                }
6264 +                long r = id;
6265 +                while (advance() != null)
6266 +                    r = reducer.apply(r, transformer.apply((K)nextKey));
6267 +                result = r;
6268 +                for (MapReduceKeysToLongTask<K,V> t = this, s;;) {
6269 +                    int c; BulkTask<K,V,?> par;
6270 +                    if ((c = t.pending) == 0) {
6271 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6272 +                            t.result = reducer.apply(t.result, s.result);
6273 +                        }
6274 +                        if ((par = t.parent) == null ||
6275 +                            !(par instanceof MapReduceKeysToLongTask)) {
6276 +                            t.quietlyComplete();
6277 +                            break;
6278 +                        }
6279 +                        t = (MapReduceKeysToLongTask<K,V>)par;
6280 +                    }
6281 +                    else if (t.casPending(c, c - 1))
6282 +                        break;
6283 +                }
6284 +            } catch (Throwable ex) {
6285 +                return tryCompleteComputation(ex);
6286 +            }
6287 +            MapReduceKeysToLongTask<K,V> s = rights;
6288 +            if (s != null && !inForkJoinPool()) {
6289 +                do  {
6290 +                    if (s.tryUnfork())
6291 +                        s.exec();
6292 +                } while ((s = s.nextRight) != null);
6293 +            }
6294 +            return false;
6295 +        }
6296 +        public final Long getRawResult() { return result; }
6297 +    }
6298 +
6299 +    @SuppressWarnings("serial") static final class MapReduceValuesToLongTask<K,V>
6300 +        extends BulkTask<K,V,Long> {
6301 +        final ObjectToLong<? super V> transformer;
6302 +        final LongByLongToLong reducer;
6303 +        final long basis;
6304 +        long result;
6305 +        MapReduceValuesToLongTask<K,V> rights, nextRight;
6306 +        MapReduceValuesToLongTask
6307 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6308 +             MapReduceValuesToLongTask<K,V> nextRight,
6309 +             ObjectToLong<? super V> transformer,
6310 +             long basis,
6311 +             LongByLongToLong reducer) {
6312 +            super(m, p, b); this.nextRight = nextRight;
6313 +            this.transformer = transformer;
6314 +            this.basis = basis; this.reducer = reducer;
6315 +        }
6316 +        @SuppressWarnings("unchecked") public final boolean exec() {
6317 +            final ObjectToLong<? super V> transformer =
6318 +                this.transformer;
6319 +            final LongByLongToLong reducer = this.reducer;
6320 +            if (transformer == null || reducer == null)
6321 +                return abortOnNullFunction();
6322 +            try {
6323 +                final long id = this.basis;
6324 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6325 +                    do {} while (!casPending(c = pending, c+1));
6326 +                    (rights = new MapReduceValuesToLongTask<K,V>
6327 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6328 +                }
6329 +                long r = id;
6330 +                Object v;
6331 +                while ((v = advance()) != null)
6332 +                    r = reducer.apply(r, transformer.apply((V)v));
6333 +                result = r;
6334 +                for (MapReduceValuesToLongTask<K,V> t = this, s;;) {
6335 +                    int c; BulkTask<K,V,?> par;
6336 +                    if ((c = t.pending) == 0) {
6337 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6338 +                            t.result = reducer.apply(t.result, s.result);
6339 +                        }
6340 +                        if ((par = t.parent) == null ||
6341 +                            !(par instanceof MapReduceValuesToLongTask)) {
6342 +                            t.quietlyComplete();
6343 +                            break;
6344 +                        }
6345 +                        t = (MapReduceValuesToLongTask<K,V>)par;
6346 +                    }
6347 +                    else if (t.casPending(c, c - 1))
6348 +                        break;
6349 +                }
6350 +            } catch (Throwable ex) {
6351 +                return tryCompleteComputation(ex);
6352 +            }
6353 +            MapReduceValuesToLongTask<K,V> s = rights;
6354 +            if (s != null && !inForkJoinPool()) {
6355 +                do  {
6356 +                    if (s.tryUnfork())
6357 +                        s.exec();
6358 +                } while ((s = s.nextRight) != null);
6359 +            }
6360 +            return false;
6361 +        }
6362 +        public final Long getRawResult() { return result; }
6363 +    }
6364 +
6365 +    @SuppressWarnings("serial") static final class MapReduceEntriesToLongTask<K,V>
6366 +        extends BulkTask<K,V,Long> {
6367 +        final ObjectToLong<Map.Entry<K,V>> transformer;
6368 +        final LongByLongToLong reducer;
6369 +        final long basis;
6370 +        long result;
6371 +        MapReduceEntriesToLongTask<K,V> rights, nextRight;
6372 +        MapReduceEntriesToLongTask
6373 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6374 +             MapReduceEntriesToLongTask<K,V> nextRight,
6375 +             ObjectToLong<Map.Entry<K,V>> transformer,
6376 +             long basis,
6377 +             LongByLongToLong reducer) {
6378 +            super(m, p, b); this.nextRight = nextRight;
6379 +            this.transformer = transformer;
6380 +            this.basis = basis; this.reducer = reducer;
6381 +        }
6382 +        @SuppressWarnings("unchecked") public final boolean exec() {
6383 +            final ObjectToLong<Map.Entry<K,V>> transformer =
6384 +                this.transformer;
6385 +            final LongByLongToLong reducer = this.reducer;
6386 +            if (transformer == null || reducer == null)
6387 +                return abortOnNullFunction();
6388 +            try {
6389 +                final long id = this.basis;
6390 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6391 +                    do {} while (!casPending(c = pending, c+1));
6392 +                    (rights = new MapReduceEntriesToLongTask<K,V>
6393 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6394 +                }
6395 +                long r = id;
6396 +                Object v;
6397 +                while ((v = advance()) != null)
6398 +                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
6399 +                result = r;
6400 +                for (MapReduceEntriesToLongTask<K,V> t = this, s;;) {
6401 +                    int c; BulkTask<K,V,?> par;
6402 +                    if ((c = t.pending) == 0) {
6403 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6404 +                            t.result = reducer.apply(t.result, s.result);
6405 +                        }
6406 +                        if ((par = t.parent) == null ||
6407 +                            !(par instanceof MapReduceEntriesToLongTask)) {
6408 +                            t.quietlyComplete();
6409 +                            break;
6410 +                        }
6411 +                        t = (MapReduceEntriesToLongTask<K,V>)par;
6412 +                    }
6413 +                    else if (t.casPending(c, c - 1))
6414 +                        break;
6415 +                }
6416 +            } catch (Throwable ex) {
6417 +                return tryCompleteComputation(ex);
6418 +            }
6419 +            MapReduceEntriesToLongTask<K,V> s = rights;
6420 +            if (s != null && !inForkJoinPool()) {
6421 +                do  {
6422 +                    if (s.tryUnfork())
6423 +                        s.exec();
6424 +                } while ((s = s.nextRight) != null);
6425 +            }
6426 +            return false;
6427 +        }
6428 +        public final Long getRawResult() { return result; }
6429 +    }
6430 +
6431 +    @SuppressWarnings("serial") static final class MapReduceMappingsToLongTask<K,V>
6432 +        extends BulkTask<K,V,Long> {
6433 +        final ObjectByObjectToLong<? super K, ? super V> transformer;
6434 +        final LongByLongToLong reducer;
6435 +        final long basis;
6436 +        long result;
6437 +        MapReduceMappingsToLongTask<K,V> rights, nextRight;
6438 +        MapReduceMappingsToLongTask
6439 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6440 +             MapReduceMappingsToLongTask<K,V> nextRight,
6441 +             ObjectByObjectToLong<? super K, ? super V> transformer,
6442 +             long basis,
6443 +             LongByLongToLong reducer) {
6444 +            super(m, p, b); this.nextRight = nextRight;
6445 +            this.transformer = transformer;
6446 +            this.basis = basis; this.reducer = reducer;
6447 +        }
6448 +        @SuppressWarnings("unchecked") public final boolean exec() {
6449 +            final ObjectByObjectToLong<? super K, ? super V> transformer =
6450 +                this.transformer;
6451 +            final LongByLongToLong reducer = this.reducer;
6452 +            if (transformer == null || reducer == null)
6453 +                return abortOnNullFunction();
6454 +            try {
6455 +                final long id = this.basis;
6456 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6457 +                    do {} while (!casPending(c = pending, c+1));
6458 +                    (rights = new MapReduceMappingsToLongTask<K,V>
6459 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6460 +                }
6461 +                long r = id;
6462 +                Object v;
6463 +                while ((v = advance()) != null)
6464 +                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
6465 +                result = r;
6466 +                for (MapReduceMappingsToLongTask<K,V> t = this, s;;) {
6467 +                    int c; BulkTask<K,V,?> par;
6468 +                    if ((c = t.pending) == 0) {
6469 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6470 +                            t.result = reducer.apply(t.result, s.result);
6471 +                        }
6472 +                        if ((par = t.parent) == null ||
6473 +                            !(par instanceof MapReduceMappingsToLongTask)) {
6474 +                            t.quietlyComplete();
6475 +                            break;
6476 +                        }
6477 +                        t = (MapReduceMappingsToLongTask<K,V>)par;
6478 +                    }
6479 +                    else if (t.casPending(c, c - 1))
6480 +                        break;
6481 +                }
6482 +            } catch (Throwable ex) {
6483 +                return tryCompleteComputation(ex);
6484 +            }
6485 +            MapReduceMappingsToLongTask<K,V> s = rights;
6486 +            if (s != null && !inForkJoinPool()) {
6487 +                do  {
6488 +                    if (s.tryUnfork())
6489 +                        s.exec();
6490 +                } while ((s = s.nextRight) != null);
6491 +            }
6492 +            return false;
6493 +        }
6494 +        public final Long getRawResult() { return result; }
6495 +    }
6496 +
6497 +    @SuppressWarnings("serial") static final class MapReduceKeysToIntTask<K,V>
6498 +        extends BulkTask<K,V,Integer> {
6499 +        final ObjectToInt<? super K> transformer;
6500 +        final IntByIntToInt reducer;
6501 +        final int basis;
6502 +        int result;
6503 +        MapReduceKeysToIntTask<K,V> rights, nextRight;
6504 +        MapReduceKeysToIntTask
6505 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6506 +             MapReduceKeysToIntTask<K,V> nextRight,
6507 +             ObjectToInt<? super K> transformer,
6508 +             int basis,
6509 +             IntByIntToInt reducer) {
6510 +            super(m, p, b); this.nextRight = nextRight;
6511 +            this.transformer = transformer;
6512 +            this.basis = basis; this.reducer = reducer;
6513 +        }
6514 +        @SuppressWarnings("unchecked") public final boolean exec() {
6515 +            final ObjectToInt<? super K> transformer =
6516 +                this.transformer;
6517 +            final IntByIntToInt reducer = this.reducer;
6518 +            if (transformer == null || reducer == null)
6519 +                return abortOnNullFunction();
6520 +            try {
6521 +                final int id = this.basis;
6522 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6523 +                    do {} while (!casPending(c = pending, c+1));
6524 +                    (rights = new MapReduceKeysToIntTask<K,V>
6525 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6526 +                }
6527 +                int r = id;
6528 +                while (advance() != null)
6529 +                    r = reducer.apply(r, transformer.apply((K)nextKey));
6530 +                result = r;
6531 +                for (MapReduceKeysToIntTask<K,V> t = this, s;;) {
6532 +                    int c; BulkTask<K,V,?> par;
6533 +                    if ((c = t.pending) == 0) {
6534 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6535 +                            t.result = reducer.apply(t.result, s.result);
6536 +                        }
6537 +                        if ((par = t.parent) == null ||
6538 +                            !(par instanceof MapReduceKeysToIntTask)) {
6539 +                            t.quietlyComplete();
6540 +                            break;
6541 +                        }
6542 +                        t = (MapReduceKeysToIntTask<K,V>)par;
6543 +                    }
6544 +                    else if (t.casPending(c, c - 1))
6545 +                        break;
6546 +                }
6547 +            } catch (Throwable ex) {
6548 +                return tryCompleteComputation(ex);
6549 +            }
6550 +            MapReduceKeysToIntTask<K,V> s = rights;
6551 +            if (s != null && !inForkJoinPool()) {
6552 +                do  {
6553 +                    if (s.tryUnfork())
6554 +                        s.exec();
6555 +                } while ((s = s.nextRight) != null);
6556 +            }
6557 +            return false;
6558 +        }
6559 +        public final Integer getRawResult() { return result; }
6560 +    }
6561 +
6562 +    @SuppressWarnings("serial") static final class MapReduceValuesToIntTask<K,V>
6563 +        extends BulkTask<K,V,Integer> {
6564 +        final ObjectToInt<? super V> transformer;
6565 +        final IntByIntToInt reducer;
6566 +        final int basis;
6567 +        int result;
6568 +        MapReduceValuesToIntTask<K,V> rights, nextRight;
6569 +        MapReduceValuesToIntTask
6570 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6571 +             MapReduceValuesToIntTask<K,V> nextRight,
6572 +             ObjectToInt<? super V> transformer,
6573 +             int basis,
6574 +             IntByIntToInt reducer) {
6575 +            super(m, p, b); this.nextRight = nextRight;
6576 +            this.transformer = transformer;
6577 +            this.basis = basis; this.reducer = reducer;
6578 +        }
6579 +        @SuppressWarnings("unchecked") public final boolean exec() {
6580 +            final ObjectToInt<? super V> transformer =
6581 +                this.transformer;
6582 +            final IntByIntToInt reducer = this.reducer;
6583 +            if (transformer == null || reducer == null)
6584 +                return abortOnNullFunction();
6585 +            try {
6586 +                final int id = this.basis;
6587 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6588 +                    do {} while (!casPending(c = pending, c+1));
6589 +                    (rights = new MapReduceValuesToIntTask<K,V>
6590 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6591 +                }
6592 +                int r = id;
6593 +                Object v;
6594 +                while ((v = advance()) != null)
6595 +                    r = reducer.apply(r, transformer.apply((V)v));
6596 +                result = r;
6597 +                for (MapReduceValuesToIntTask<K,V> t = this, s;;) {
6598 +                    int c; BulkTask<K,V,?> par;
6599 +                    if ((c = t.pending) == 0) {
6600 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6601 +                            t.result = reducer.apply(t.result, s.result);
6602 +                        }
6603 +                        if ((par = t.parent) == null ||
6604 +                            !(par instanceof MapReduceValuesToIntTask)) {
6605 +                            t.quietlyComplete();
6606 +                            break;
6607 +                        }
6608 +                        t = (MapReduceValuesToIntTask<K,V>)par;
6609 +                    }
6610 +                    else if (t.casPending(c, c - 1))
6611 +                        break;
6612 +                }
6613 +            } catch (Throwable ex) {
6614 +                return tryCompleteComputation(ex);
6615 +            }
6616 +            MapReduceValuesToIntTask<K,V> s = rights;
6617 +            if (s != null && !inForkJoinPool()) {
6618 +                do  {
6619 +                    if (s.tryUnfork())
6620 +                        s.exec();
6621 +                } while ((s = s.nextRight) != null);
6622 +            }
6623 +            return false;
6624 +        }
6625 +        public final Integer getRawResult() { return result; }
6626 +    }
6627 +
6628 +    @SuppressWarnings("serial") static final class MapReduceEntriesToIntTask<K,V>
6629 +        extends BulkTask<K,V,Integer> {
6630 +        final ObjectToInt<Map.Entry<K,V>> transformer;
6631 +        final IntByIntToInt reducer;
6632 +        final int basis;
6633 +        int result;
6634 +        MapReduceEntriesToIntTask<K,V> rights, nextRight;
6635 +        MapReduceEntriesToIntTask
6636 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6637 +             MapReduceEntriesToIntTask<K,V> nextRight,
6638 +             ObjectToInt<Map.Entry<K,V>> transformer,
6639 +             int basis,
6640 +             IntByIntToInt reducer) {
6641 +            super(m, p, b); this.nextRight = nextRight;
6642 +            this.transformer = transformer;
6643 +            this.basis = basis; this.reducer = reducer;
6644 +        }
6645 +        @SuppressWarnings("unchecked") public final boolean exec() {
6646 +            final ObjectToInt<Map.Entry<K,V>> transformer =
6647 +                this.transformer;
6648 +            final IntByIntToInt reducer = this.reducer;
6649 +            if (transformer == null || reducer == null)
6650 +                return abortOnNullFunction();
6651 +            try {
6652 +                final int id = this.basis;
6653 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6654 +                    do {} while (!casPending(c = pending, c+1));
6655 +                    (rights = new MapReduceEntriesToIntTask<K,V>
6656 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6657 +                }
6658 +                int r = id;
6659 +                Object v;
6660 +                while ((v = advance()) != null)
6661 +                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
6662 +                result = r;
6663 +                for (MapReduceEntriesToIntTask<K,V> t = this, s;;) {
6664 +                    int c; BulkTask<K,V,?> par;
6665 +                    if ((c = t.pending) == 0) {
6666 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6667 +                            t.result = reducer.apply(t.result, s.result);
6668 +                        }
6669 +                        if ((par = t.parent) == null ||
6670 +                            !(par instanceof MapReduceEntriesToIntTask)) {
6671 +                            t.quietlyComplete();
6672 +                            break;
6673 +                        }
6674 +                        t = (MapReduceEntriesToIntTask<K,V>)par;
6675 +                    }
6676 +                    else if (t.casPending(c, c - 1))
6677 +                        break;
6678 +                }
6679 +            } catch (Throwable ex) {
6680 +                return tryCompleteComputation(ex);
6681 +            }
6682 +            MapReduceEntriesToIntTask<K,V> s = rights;
6683 +            if (s != null && !inForkJoinPool()) {
6684 +                do  {
6685 +                    if (s.tryUnfork())
6686 +                        s.exec();
6687 +                } while ((s = s.nextRight) != null);
6688 +            }
6689 +            return false;
6690 +        }
6691 +        public final Integer getRawResult() { return result; }
6692 +    }
6693 +
6694 +    @SuppressWarnings("serial") static final class MapReduceMappingsToIntTask<K,V>
6695 +        extends BulkTask<K,V,Integer> {
6696 +        final ObjectByObjectToInt<? super K, ? super V> transformer;
6697 +        final IntByIntToInt reducer;
6698 +        final int basis;
6699 +        int result;
6700 +        MapReduceMappingsToIntTask<K,V> rights, nextRight;
6701 +        MapReduceMappingsToIntTask
6702 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6703 +             MapReduceMappingsToIntTask<K,V> rights,
6704 +             ObjectByObjectToInt<? super K, ? super V> transformer,
6705 +             int basis,
6706 +             IntByIntToInt reducer) {
6707 +            super(m, p, b); this.nextRight = nextRight;
6708 +            this.transformer = transformer;
6709 +            this.basis = basis; this.reducer = reducer;
6710 +        }
6711 +        @SuppressWarnings("unchecked") public final boolean exec() {
6712 +            final ObjectByObjectToInt<? super K, ? super V> transformer =
6713 +                this.transformer;
6714 +            final IntByIntToInt reducer = this.reducer;
6715 +            if (transformer == null || reducer == null)
6716 +                return abortOnNullFunction();
6717 +            try {
6718 +                final int id = this.basis;
6719 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6720 +                    do {} while (!casPending(c = pending, c+1));
6721 +                    (rights = new MapReduceMappingsToIntTask<K,V>
6722 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6723 +                }
6724 +                int r = id;
6725 +                Object v;
6726 +                while ((v = advance()) != null)
6727 +                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
6728 +                result = r;
6729 +                for (MapReduceMappingsToIntTask<K,V> t = this, s;;) {
6730 +                    int c; BulkTask<K,V,?> par;
6731 +                    if ((c = t.pending) == 0) {
6732 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6733 +                            t.result = reducer.apply(t.result, s.result);
6734 +                        }
6735 +                        if ((par = t.parent) == null ||
6736 +                            !(par instanceof MapReduceMappingsToIntTask)) {
6737 +                            t.quietlyComplete();
6738 +                            break;
6739 +                        }
6740 +                        t = (MapReduceMappingsToIntTask<K,V>)par;
6741 +                    }
6742 +                    else if (t.casPending(c, c - 1))
6743 +                        break;
6744 +                }
6745 +            } catch (Throwable ex) {
6746 +                return tryCompleteComputation(ex);
6747 +            }
6748 +            MapReduceMappingsToIntTask<K,V> s = rights;
6749 +            if (s != null && !inForkJoinPool()) {
6750 +                do  {
6751 +                    if (s.tryUnfork())
6752 +                        s.exec();
6753 +                } while ((s = s.nextRight) != null);
6754 +            }
6755 +            return false;
6756 +        }
6757 +        public final Integer getRawResult() { return result; }
6758 +    }
6759 +
6760 +
6761      // Unsafe mechanics
6762      private static final sun.misc.Unsafe UNSAFE;
6763      private static final long counterOffset;
6764 <    private static final long resizingOffset;
6764 >    private static final long sizeCtlOffset;
6765      private static final long ABASE;
6766      private static final int ASHIFT;
6767  
# Line 1574 | Line 6772 | public class ConcurrentHashMapV8<K, V>
6772              Class<?> k = ConcurrentHashMapV8.class;
6773              counterOffset = UNSAFE.objectFieldOffset
6774                  (k.getDeclaredField("counter"));
6775 <            resizingOffset = UNSAFE.objectFieldOffset
6776 <                (k.getDeclaredField("resizing"));
6775 >            sizeCtlOffset = UNSAFE.objectFieldOffset
6776 >                (k.getDeclaredField("sizeCtl"));
6777              Class<?> sc = Node[].class;
6778              ABASE = UNSAFE.arrayBaseOffset(sc);
6779              ss = UNSAFE.arrayIndexScale(sc);
# Line 1614 | Line 6812 | public class ConcurrentHashMapV8<K, V>
6812              }
6813          }
6814      }
1617
6815   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines