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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines