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.38 by dl, Wed Jun 6 15:41:23 2012 UTC vs.
Revision 1.72 by jsr166, Tue Oct 30 16:05:35 2012 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 // Snapshot Tue Jun  5 14:56:09 2012  Doug Lea  (dl at altair)
8
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 25 | Line 24 | import java.util.concurrent.ConcurrentMa
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 43 | 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 78 | 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 85 | Line 102 | import java.io.Serializable;
102   * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
103   * does <em>not</em> allow {@code null} to be used as a key or value.
104   *
105 + * <p>ConcurrentHashMapV8s support parallel operations using the {@link
106 + * ForkJoinPool#commonPool}. (Task that may be used in other contexts
107 + * are available in class {@link ForkJoinTasks}). These operations are
108 + * designed to be safely, and often sensibly, applied even with maps
109 + * that are being concurrently updated by other threads; for example,
110 + * when computing a snapshot summary of the values in a shared
111 + * registry.  There are three kinds of operation, each with four
112 + * forms, accepting functions with Keys, Values, Entries, and (Key,
113 + * Value) arguments and/or return values. Because the elements of a
114 + * ConcurrentHashMapV8 are not ordered in any particular way, and may be
115 + * processed in different orders in different parallel executions, the
116 + * correctness of supplied functions should not depend on any
117 + * ordering, or on any other objects or values that may transiently
118 + * change while computation is in progress; and except for forEach
119 + * actions, should ideally be side-effect-free.
120 + *
121 + * <ul>
122 + * <li> forEach: Perform a given action on each element.
123 + * A variant form applies a given transformation on each element
124 + * before performing the action.</li>
125 + *
126 + * <li> search: Return the first available non-null result of
127 + * applying a given function on each element; skipping further
128 + * search when a result is found.</li>
129 + *
130 + * <li> reduce: Accumulate each element.  The supplied reduction
131 + * function cannot rely on ordering (more formally, it should be
132 + * both associative and commutative).  There are five variants:
133 + *
134 + * <ul>
135 + *
136 + * <li> Plain reductions. (There is not a form of this method for
137 + * (key, value) function arguments since there is no corresponding
138 + * return type.)</li>
139 + *
140 + * <li> Mapped reductions that accumulate the results of a given
141 + * function applied to each element.</li>
142 + *
143 + * <li> Reductions to scalar doubles, longs, and ints, using a
144 + * given basis value.</li>
145 + *
146 + * </li>
147 + * </ul>
148 + * </ul>
149 + *
150 + * <p>The concurrency properties of bulk operations follow
151 + * from those of ConcurrentHashMapV8: Any non-null result returned
152 + * from {@code get(key)} and related access methods bears a
153 + * happens-before relation with the associated insertion or
154 + * update.  The result of any bulk operation reflects the
155 + * composition of these per-element relations (but is not
156 + * necessarily atomic with respect to the map as a whole unless it
157 + * is somehow known to be quiescent).  Conversely, because keys
158 + * and values in the map are never null, null serves as a reliable
159 + * atomic indicator of the current lack of any result.  To
160 + * maintain this property, null serves as an implicit basis for
161 + * all non-scalar reduction operations. For the double, long, and
162 + * int versions, the basis should be one that, when combined with
163 + * any other value, returns that other value (more formally, it
164 + * should be the identity element for the reduction). Most common
165 + * reductions have these properties; for example, computing a sum
166 + * with basis 0 or a minimum with basis MAX_VALUE.
167 + *
168 + * <p>Search and transformation functions provided as arguments
169 + * should similarly return null to indicate the lack of any result
170 + * (in which case it is not used). In the case of mapped
171 + * reductions, this also enables transformations to serve as
172 + * filters, returning null (or, in the case of primitive
173 + * specializations, the identity basis) if the element should not
174 + * be combined. You can create compound transformations and
175 + * filterings by composing them yourself under this "null means
176 + * there is nothing there now" rule before using them in search or
177 + * reduce operations.
178 + *
179 + * <p>Methods accepting and/or returning Entry arguments maintain
180 + * key-value associations. They may be useful for example when
181 + * finding the key for the greatest value. Note that "plain" Entry
182 + * arguments can be supplied using {@code new
183 + * AbstractMap.SimpleEntry(k,v)}.
184 + *
185 + * <p> Bulk operations may complete abruptly, throwing an
186 + * exception encountered in the application of a supplied
187 + * function. Bear in mind when handling such exceptions that other
188 + * concurrently executing functions could also have thrown
189 + * exceptions, or would have done so if the first exception had
190 + * not occurred.
191 + *
192 + * <p>Parallel speedups for bulk operations compared to sequential
193 + * processing are common but not guaranteed.  Operations involving
194 + * brief functions on small maps may execute more slowly than
195 + * sequential loops if the underlying work to parallelize the
196 + * computation is more expensive than the computation
197 + * itself. Similarly, parallelization may not lead to much actual
198 + * parallelism if all processors are busy performing unrelated tasks.
199 + *
200 + * <p> All arguments to all task methods must be non-null.
201 + *
202 + * <p><em>jsr166e note: During transition, this class
203 + * uses nested functional interfaces with different names but the
204 + * same forms as those expected for JDK8.<em>
205 + *
206   * <p>This class is a member of the
207   * <a href="{@docRoot}/../technotes/guides/collections/index.html">
208   * Java Collections Framework</a>.
209   *
92 * <p><em>jsr166e note: This class is a candidate replacement for
93 * java.util.concurrent.ConcurrentHashMap.<em>
94 *
210   * @since 1.5
211   * @author Doug Lea
212   * @param <K> the type of keys maintained by this map
213   * @param <V> the type of mapped values
214   */
215   public class ConcurrentHashMapV8<K, V>
216 <        implements ConcurrentMap<K, V>, Serializable {
216 >    implements ConcurrentMap<K, V>, Serializable {
217      private static final long serialVersionUID = 7249069246763182397L;
218  
219      /**
220 <     * A function computing a mapping from the given key to a value.
221 <     * This is a place-holder for an upcoming JDK8 interface.
220 >     * A partitionable iterator. A Spliterator can be traversed
221 >     * directly, but can also be partitioned (before traversal) by
222 >     * creating another Spliterator that covers a non-overlapping
223 >     * portion of the elements, and so may be amenable to parallel
224 >     * execution.
225 >     *
226 >     * <p> This interface exports a subset of expected JDK8
227 >     * functionality.
228 >     *
229 >     * <p>Sample usage: Here is one (of the several) ways to compute
230 >     * the sum of the values held in a map using the ForkJoin
231 >     * framework. As illustrated here, Spliterators are well suited to
232 >     * designs in which a task repeatedly splits off half its work
233 >     * into forked subtasks until small enough to process directly,
234 >     * and then joins these subtasks. Variants of this style can also
235 >     * be used in completion-based designs.
236 >     *
237 >     * <pre>
238 >     * {@code ConcurrentHashMapV8<String, Long> m = ...
239 >     * // split as if have 8 * parallelism, for load balance
240 >     * int n = m.size();
241 >     * int p = aForkJoinPool.getParallelism() * 8;
242 >     * int split = (n < p)? n : p;
243 >     * long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null));
244 >     * // ...
245 >     * static class SumValues extends RecursiveTask<Long> {
246 >     *   final Spliterator<Long> s;
247 >     *   final int split;             // split while > 1
248 >     *   final SumValues nextJoin;    // records forked subtasks to join
249 >     *   SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
250 >     *     this.s = s; this.depth = depth; this.nextJoin = nextJoin;
251 >     *   }
252 >     *   public Long compute() {
253 >     *     long sum = 0;
254 >     *     SumValues subtasks = null; // fork subtasks
255 >     *     for (int s = split >>> 1; s > 0; s >>>= 1)
256 >     *       (subtasks = new SumValues(s.split(), s, subtasks)).fork();
257 >     *     while (s.hasNext())        // directly process remaining elements
258 >     *       sum += s.next();
259 >     *     for (SumValues t = subtasks; t != null; t = t.nextJoin)
260 >     *       sum += t.join();         // collect subtask results
261 >     *     return sum;
262 >     *   }
263 >     * }
264 >     * }</pre>
265       */
266 <    public static interface MappingFunction<K, V> {
266 >    public static interface Spliterator<T> extends Iterator<T> {
267          /**
268 <         * Returns a non-null value for the given key.
268 >         * Returns a Spliterator covering approximately half of the
269 >         * elements, guaranteed not to overlap with those subsequently
270 >         * returned by this Spliterator.  After invoking this method,
271 >         * the current Spliterator will <em>not</em> produce any of
272 >         * the elements of the returned Spliterator, but the two
273 >         * Spliterators together will produce all of the elements that
274 >         * would have been produced by this Spliterator had this
275 >         * method not been called. The exact number of elements
276 >         * produced by the returned Spliterator is not guaranteed, and
277 >         * may be zero (i.e., with {@code hasNext()} reporting {@code
278 >         * false}) if this Spliterator cannot be further split.
279           *
280 <         * @param key the (non-null) key
281 <         * @return a non-null value
280 >         * @return a Spliterator covering approximately half of the
281 >         * elements
282 >         * @throws IllegalStateException if this Spliterator has
283 >         * already commenced traversing elements
284           */
285 <        V map(K key);
285 >        Spliterator<T> split();
286      }
287  
288      /**
289 <     * A function computing a new mapping given a key and its current
290 <     * mapped value (or {@code null} if there is no current
291 <     * mapping). This is a place-holder for an upcoming JDK8
292 <     * interface.
289 >     * A view of a ConcurrentHashMapV8 as a {@link Set} of keys, in
290 >     * which additions may optionally be enabled by mapping to a
291 >     * common value.  This class cannot be directly instantiated. See
292 >     * {@link #keySet}, {@link #keySet(Object)}, {@link #newKeySet()},
293 >     * {@link #newKeySet(int)}.
294 >     *
295 >     * <p>The view's {@code iterator} is a "weakly consistent" iterator
296 >     * that will never throw {@link ConcurrentModificationException},
297 >     * and guarantees to traverse elements as they existed upon
298 >     * construction of the iterator, and may (but is not guaranteed to)
299 >     * reflect any modifications subsequent to construction.
300       */
301 <    public static interface RemappingFunction<K, V> {
301 >    public static class KeySetView<K,V> extends CHMView<K,V> implements Set<K>, java.io.Serializable {
302 >        private static final long serialVersionUID = 7249069246763182397L;
303 >        private final V value;
304 >        KeySetView(ConcurrentHashMapV8<K, V> map, V value) {  // non-public
305 >            super(map);
306 >            this.value = value;
307 >        }
308 >
309          /**
310 <         * Returns a new value given a key and its current value.
310 >         * Returns the map backing this view.
311           *
312 <         * @param key the (non-null) key
129 <         * @param value the current value, or null if there is no mapping
130 <         * @return a non-null value
312 >         * @return the map backing this view
313           */
314 <        V remap(K key, V value);
314 >        public ConcurrentHashMapV8<K,V> getMap() { return map; }
315 >
316 >        /**
317 >         * Returns the default mapped value for additions,
318 >         * or {@code null} if additions are not supported.
319 >         *
320 >         * @return the default mapped value for additions, or {@code null}
321 >         * if not supported.
322 >         */
323 >        public V getMappedValue() { return value; }
324 >
325 >        // implement Set API
326 >
327 >        public boolean contains(Object o) { return map.containsKey(o); }
328 >        public boolean remove(Object o)   { return map.remove(o) != null; }
329 >        public Iterator<K> iterator()     { return new KeyIterator<K,V>(map); }
330 >        public boolean add(K e) {
331 >            V v;
332 >            if ((v = value) == null)
333 >                throw new UnsupportedOperationException();
334 >            if (e == null)
335 >                throw new NullPointerException();
336 >            return map.internalPutIfAbsent(e, v) == null;
337 >        }
338 >        public boolean addAll(Collection<? extends K> c) {
339 >            boolean added = false;
340 >            V v;
341 >            if ((v = value) == null)
342 >                throw new UnsupportedOperationException();
343 >            for (K e : c) {
344 >                if (e == null)
345 >                    throw new NullPointerException();
346 >                if (map.internalPutIfAbsent(e, v) == null)
347 >                    added = true;
348 >            }
349 >            return added;
350 >        }
351 >        public boolean equals(Object o) {
352 >            Set<?> c;
353 >            return ((o instanceof Set) &&
354 >                    ((c = (Set<?>)o) == this ||
355 >                     (containsAll(c) && c.containsAll(this))));
356 >        }
357      }
358  
359      /*
# Line 283 | Line 507 | public class ConcurrentHashMapV8<K, V>
507       * When there are no lock acquisition failures, this is arranged
508       * simply by proceeding from the last bin (table.length - 1) up
509       * towards the first.  Upon seeing a forwarding node, traversals
510 <     * (see class InternalIterator) arrange to move to the new table
510 >     * (see class Iter) arrange to move to the new table
511       * without revisiting nodes.  However, when any node is skipped
512       * during a transfer, all earlier table bins may have become
513       * visible, so are initialized with a reverse-forwarding node back
# Line 293 | Line 517 | public class ConcurrentHashMapV8<K, V>
517       * mechanics trigger only when necessary.
518       *
519       * The traversal scheme also applies to partial traversals of
520 <     * ranges of bins (via an alternate InternalIterator constructor)
521 <     * to support partitioned aggregate operations (that are not
522 <     * otherwise implemented yet).  Also, read-only operations give up
523 <     * if ever forwarded to a null table, which provides support for
524 <     * shutdown-style clearing, which is also not currently
301 <     * implemented.
520 >     * ranges of bins (via an alternate Traverser constructor)
521 >     * to support partitioned aggregate operations.  Also, read-only
522 >     * operations give up if ever forwarded to a null table, which
523 >     * provides support for shutdown-style clearing, which is also not
524 >     * currently implemented.
525       *
526       * Lazy table initialization minimizes footprint until first use,
527       * and also avoids resizings when the first operation is from a
# Line 415 | Line 638 | public class ConcurrentHashMapV8<K, V>
638      private transient volatile int sizeCtl;
639  
640      // views
641 <    private transient KeySet<K,V> keySet;
641 >    private transient KeySetView<K,V> keySet;
642      private transient Values<K,V> values;
643      private transient EntrySet<K,V> entrySet;
644  
# Line 436 | Line 659 | public class ConcurrentHashMapV8<K, V>
659       * inline assignments below.
660       */
661  
662 <    static final Node tabAt(Node[] tab, int i) { // used by InternalIterator
662 >    static final Node tabAt(Node[] tab, int i) { // used by Iter
663          return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
664      }
665  
# Line 452 | Line 675 | public class ConcurrentHashMapV8<K, V>
675  
676      /**
677       * Key-value entry. Note that this is never exported out as a
678 <     * user-visible Map.Entry (see WriteThroughEntry and SnapshotEntry
679 <     * below). Nodes with a hash field of MOVED are special, and do
680 <     * not contain user keys or values.  Otherwise, keys are never
681 <     * null, and null val fields indicate that a node is in the
682 <     * process of being deleted or created. For purposes of read-only
683 <     * access, a key may be read before a val, but can only be used
684 <     * after checking val to be non-null.
678 >     * user-visible Map.Entry (see MapEntry below). Nodes with a hash
679 >     * field of MOVED are special, and do not contain user keys or
680 >     * values.  Otherwise, keys are never null, and null val fields
681 >     * indicate that a node is in the process of being deleted or
682 >     * created. For purposes of read-only access, a key may be read
683 >     * before a val, but can only be used after checking val to be
684 >     * non-null.
685       */
686      static class Node {
687          volatile int hash;
# Line 494 | Line 717 | public class ConcurrentHashMapV8<K, V>
717           * unlocking lock (via a failed CAS from non-waiting LOCKED
718           * state), unlockers acquire the sync lock and perform a
719           * notifyAll.
720 +         *
721 +         * The initial sanity check on tab and bounds is not currently
722 +         * necessary in the only usages of this method, but enables
723 +         * use in other future contexts.
724           */
725          final void tryAwaitLock(Node[] tab, int i) {
726 <            if (tab != null && i >= 0 && i < tab.length) { // bounds check
726 >            if (tab != null && i >= 0 && i < tab.length) { // sanity check
727                  int r = ThreadLocalRandom.current().nextInt(); // randomize spins
728                  int spins = MAX_SPINS, h;
729                  while (tabAt(tab, i) == this && ((h = hash) & LOCKED) != 0) {
# Line 571 | Line 798 | public class ConcurrentHashMapV8<K, V>
798       * handle this, the tree is ordered primarily by hash value, then
799       * by getClass().getName() order, and then by Comparator order
800       * among elements of the same class.  On lookup at a node, if
801 <     * non-Comparable, both left and right children may need to be
802 <     * searched in the case of tied hash values. (This corresponds to
803 <     * the full list search that would be necessary if all elements
804 <     * were non-Comparable and had tied hashes.)
801 >     * elements are not comparable or compare as 0, both left and
802 >     * right children may need to be searched in the case of tied hash
803 >     * values. (This corresponds to the full list search that would be
804 >     * necessary if all elements were non-Comparable and had tied
805 >     * hashes.)  The red-black balancing code is updated from
806 >     * pre-jdk-collections
807 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
808 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
809 >     * Algorithms" (CLR).
810       *
811       * TreeBins also maintain a separate locking discipline than
812       * regular bins. Because they are forwarded via special MOVED
813       * nodes at bin heads (which can never change once established),
814 <     * we cannot use use those nodes as locks. Instead, TreeBin
814 >     * we cannot use those nodes as locks. Instead, TreeBin
815       * extends AbstractQueuedSynchronizer to support a simple form of
816       * read-write lock. For update operations and table validation,
817       * the exclusive form of lock behaves in the same way as bin-head
# Line 598 | Line 830 | public class ConcurrentHashMapV8<K, V>
830       */
831      static final class TreeBin extends AbstractQueuedSynchronizer {
832          private static final long serialVersionUID = 2249069246763182397L;
833 <        TreeNode root;  // root of tree
834 <        TreeNode first; // head of next-pointer list
833 >        transient TreeNode root;  // root of tree
834 >        transient TreeNode first; // head of next-pointer list
835  
836          /* AQS overrides */
837          public final boolean isHeldExclusively() { return getState() > 0; }
# Line 629 | Line 861 | public class ConcurrentHashMapV8<K, V>
861              return c == -1;
862          }
863  
864 +        /** From CLR */
865 +        private void rotateLeft(TreeNode p) {
866 +            if (p != null) {
867 +                TreeNode r = p.right, pp, rl;
868 +                if ((rl = p.right = r.left) != null)
869 +                    rl.parent = p;
870 +                if ((pp = r.parent = p.parent) == null)
871 +                    root = r;
872 +                else if (pp.left == p)
873 +                    pp.left = r;
874 +                else
875 +                    pp.right = r;
876 +                r.left = p;
877 +                p.parent = r;
878 +            }
879 +        }
880 +
881 +        /** From CLR */
882 +        private void rotateRight(TreeNode p) {
883 +            if (p != null) {
884 +                TreeNode l = p.left, pp, lr;
885 +                if ((lr = p.left = l.right) != null)
886 +                    lr.parent = p;
887 +                if ((pp = l.parent = p.parent) == null)
888 +                    root = l;
889 +                else if (pp.right == p)
890 +                    pp.right = l;
891 +                else
892 +                    pp.left = l;
893 +                l.right = p;
894 +                p.parent = l;
895 +            }
896 +        }
897 +
898          /**
899 <         * Return the TreeNode (or null if not found) for the given key
899 >         * Returns the TreeNode (or null if not found) for the given key
900           * starting at given root.
901           */
902 <        @SuppressWarnings("unchecked") // suppress Comparable cast warning
903 <        final TreeNode getTreeNode(int h, Object k, TreeNode p) {
902 >        @SuppressWarnings("unchecked") final TreeNode getTreeNode
903 >            (int h, Object k, TreeNode p) {
904              Class<?> c = k.getClass();
905              while (p != null) {
906 <                int dir, ph;  Object pk; Class<?> pc; TreeNode r;
907 <                if (h < (ph = p.hash))
908 <                    dir = -1;
909 <                else if (h > ph)
910 <                    dir = 1;
911 <                else if ((pk = p.key) == k || k.equals(pk))
912 <                    return p;
913 <                else if (c != (pc = pk.getClass()))
914 <                    dir = c.getName().compareTo(pc.getName());
915 <                else if (k instanceof Comparable)
916 <                    dir = ((Comparable)k).compareTo((Comparable)pk);
917 <                else
918 <                    dir = 0;
919 <                TreeNode pr = p.right;
920 <                if (dir > 0)
921 <                    p = pr;
922 <                else if (dir == 0 && pr != null && h >= pr.hash &&
923 <                         (r = getTreeNode(h, k, pr)) != null)
924 <                    return r;
906 >                int dir, ph;  Object pk; Class<?> pc;
907 >                if ((ph = p.hash) == h) {
908 >                    if ((pk = p.key) == k || k.equals(pk))
909 >                        return p;
910 >                    if (c != (pc = pk.getClass()) ||
911 >                        !(k instanceof Comparable) ||
912 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
913 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
914 >                        TreeNode r = null, s = null, pl, pr;
915 >                        if (dir >= 0) {
916 >                            if ((pl = p.left) != null && h <= pl.hash)
917 >                                s = pl;
918 >                        }
919 >                        else if ((pr = p.right) != null && h >= pr.hash)
920 >                            s = pr;
921 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
922 >                            return r;
923 >                    }
924 >                }
925                  else
926 <                    p = p.left;
926 >                    dir = (h < ph) ? -1 : 1;
927 >                p = (dir > 0) ? p.right : p.left;
928              }
929              return null;
930          }
# Line 690 | Line 957 | public class ConcurrentHashMapV8<K, V>
957          }
958  
959          /**
960 <         * Find or add a node
960 >         * Finds or adds a node.
961           * @return null if added
962           */
963 <        @SuppressWarnings("unchecked") // suppress Comparable cast warning
964 <        final TreeNode putTreeNode(int h, Object k, Object v) {
963 >        @SuppressWarnings("unchecked") final TreeNode putTreeNode
964 >            (int h, Object k, Object v) {
965              Class<?> c = k.getClass();
966 <            TreeNode p = root;
966 >            TreeNode pp = root, p = null;
967              int dir = 0;
968 <            if (p != null) {
969 <                for (;;) {
970 <                    int ph;  Object pk; Class<?> pc; TreeNode r;
971 <                    if (h < (ph = p.hash))
972 <                        dir = -1;
706 <                    else if (h > ph)
707 <                        dir = 1;
708 <                    else if ((pk = p.key) == k || k.equals(pk))
968 >            while (pp != null) { // find existing node or leaf to insert at
969 >                int ph;  Object pk; Class<?> pc;
970 >                p = pp;
971 >                if ((ph = p.hash) == h) {
972 >                    if ((pk = p.key) == k || k.equals(pk))
973                          return p;
974 <                    else if (c != (pc = (pk = p.key).getClass()))
975 <                        dir = c.getName().compareTo(pc.getName());
976 <                    else if (k instanceof Comparable)
977 <                        dir = ((Comparable)k).compareTo((Comparable)pk);
978 <                    else
979 <                        dir = 0;
980 <                    TreeNode pr = p.right, pl;
981 <                    if (dir > 0) {
982 <                        if (pr == null)
983 <                            break;
984 <                        p = pr;
985 <                    }
986 <                    else if (dir == 0 && pr != null && h >= pr.hash &&
987 <                             (r = getTreeNode(h, k, pr)) != null)
724 <                        return r;
725 <                    else if ((pl = p.left) == null)
726 <                        break;
727 <                    else
728 <                        p = pl;
974 >                    if (c != (pc = pk.getClass()) ||
975 >                        !(k instanceof Comparable) ||
976 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
977 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
978 >                        TreeNode r = null, s = null, pl, pr;
979 >                        if (dir >= 0) {
980 >                            if ((pl = p.left) != null && h <= pl.hash)
981 >                                s = pl;
982 >                        }
983 >                        else if ((pr = p.right) != null && h >= pr.hash)
984 >                            s = pr;
985 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
986 >                            return r;
987 >                    }
988                  }
989 +                else
990 +                    dir = (h < ph) ? -1 : 1;
991 +                pp = (dir > 0) ? p.right : p.left;
992              }
993 +
994              TreeNode f = first;
995 <            TreeNode r = first = new TreeNode(h, k, v, f, p);
995 >            TreeNode x = first = new TreeNode(h, k, v, f, p);
996              if (p == null)
997 <                root = r;
998 <            else {
997 >                root = x;
998 >            else { // attach and rebalance; adapted from CLR
999 >                TreeNode xp, xpp;
1000 >                if (f != null)
1001 >                    f.prev = x;
1002                  if (dir <= 0)
1003 <                    p.left = r;
1003 >                    p.left = x;
1004                  else
1005 <                    p.right = r;
1006 <                if (f != null)
1007 <                    f.prev = r;
1008 <                fixAfterInsertion(r);
1005 >                    p.right = x;
1006 >                x.red = true;
1007 >                while (x != null && (xp = x.parent) != null && xp.red &&
1008 >                       (xpp = xp.parent) != null) {
1009 >                    TreeNode xppl = xpp.left;
1010 >                    if (xp == xppl) {
1011 >                        TreeNode y = xpp.right;
1012 >                        if (y != null && y.red) {
1013 >                            y.red = false;
1014 >                            xp.red = false;
1015 >                            xpp.red = true;
1016 >                            x = xpp;
1017 >                        }
1018 >                        else {
1019 >                            if (x == xp.right) {
1020 >                                rotateLeft(x = xp);
1021 >                                xpp = (xp = x.parent) == null ? null : xp.parent;
1022 >                            }
1023 >                            if (xp != null) {
1024 >                                xp.red = false;
1025 >                                if (xpp != null) {
1026 >                                    xpp.red = true;
1027 >                                    rotateRight(xpp);
1028 >                                }
1029 >                            }
1030 >                        }
1031 >                    }
1032 >                    else {
1033 >                        TreeNode y = xppl;
1034 >                        if (y != null && y.red) {
1035 >                            y.red = false;
1036 >                            xp.red = false;
1037 >                            xpp.red = true;
1038 >                            x = xpp;
1039 >                        }
1040 >                        else {
1041 >                            if (x == xp.left) {
1042 >                                rotateRight(x = xp);
1043 >                                xpp = (xp = x.parent) == null ? null : xp.parent;
1044 >                            }
1045 >                            if (xp != null) {
1046 >                                xp.red = false;
1047 >                                if (xpp != null) {
1048 >                                    xpp.red = true;
1049 >                                    rotateLeft(xpp);
1050 >                                }
1051 >                            }
1052 >                        }
1053 >                    }
1054 >                }
1055 >                TreeNode r = root;
1056 >                if (r != null && r.red)
1057 >                    r.red = false;
1058              }
1059              return null;
1060          }
# Line 765 | Line 1080 | public class ConcurrentHashMapV8<K, V>
1080              TreeNode pl = p.left;
1081              TreeNode pr = p.right;
1082              if (pl != null && pr != null) {
1083 <                TreeNode s = pr;
1084 <                while (s.left != null) // find successor
1085 <                    s = s.left;
1083 >                TreeNode s = pr, sl;
1084 >                while ((sl = s.left) != null) // find successor
1085 >                    s = sl;
1086                  boolean c = s.red; s.red = p.red; p.red = c; // swap colors
1087                  TreeNode sr = s.right;
1088                  TreeNode pp = p.parent;
# Line 819 | Line 1134 | public class ConcurrentHashMapV8<K, V>
1134                      pp.right = replacement;
1135                  p.left = p.right = p.parent = null;
1136              }
1137 <            if (!p.red)
1138 <                fixAfterDeletion(replacement);
1139 <            if (p == replacement && (pp = p.parent) != null) {
1140 <                if (p == pp.left) // detach pointers
1141 <                    pp.left = null;
1142 <                else if (p == pp.right)
1143 <                    pp.right = null;
829 <                p.parent = null;
830 <            }
831 <        }
832 <
833 <        // CLR code updated from pre-jdk-collections version at
834 <        // http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java
835 <
836 <        /** From CLR */
837 <        private void rotateLeft(TreeNode p) {
838 <            if (p != null) {
839 <                TreeNode r = p.right, pp, rl;
840 <                if ((rl = p.right = r.left) != null)
841 <                    rl.parent = p;
842 <                if ((pp = r.parent = p.parent) == null)
843 <                    root = r;
844 <                else if (pp.left == p)
845 <                    pp.left = r;
846 <                else
847 <                    pp.right = r;
848 <                r.left = p;
849 <                p.parent = r;
850 <            }
851 <        }
852 <
853 <        /** From CLR */
854 <        private void rotateRight(TreeNode p) {
855 <            if (p != null) {
856 <                TreeNode l = p.left, pp, lr;
857 <                if ((lr = p.left = l.right) != null)
858 <                    lr.parent = p;
859 <                if ((pp = l.parent = p.parent) == null)
860 <                    root = l;
861 <                else if (pp.right == p)
862 <                    pp.right = l;
863 <                else
864 <                    pp.left = l;
865 <                l.right = p;
866 <                p.parent = l;
867 <            }
868 <        }
869 <
870 <        /** From CLR */
871 <        private void fixAfterInsertion(TreeNode x) {
872 <            x.red = true;
873 <            TreeNode xp, xpp;
874 <            while (x != null && (xp = x.parent) != null && xp.red &&
875 <                   (xpp = xp.parent) != null) {
876 <                TreeNode xppl = xpp.left;
877 <                if (xp == xppl) {
878 <                    TreeNode y = xpp.right;
879 <                    if (y != null && y.red) {
880 <                        y.red = false;
881 <                        xp.red = false;
882 <                        xpp.red = true;
883 <                        x = xpp;
884 <                    }
885 <                    else {
886 <                        if (x == xp.right) {
887 <                            x = xp;
888 <                            rotateLeft(x);
889 <                            xpp = (xp = x.parent) == null ? null : xp.parent;
890 <                        }
891 <                        if (xp != null) {
892 <                            xp.red = false;
893 <                            if (xpp != null) {
894 <                                xpp.red = true;
895 <                                rotateRight(xpp);
896 <                            }
897 <                        }
898 <                    }
899 <                }
900 <                else {
901 <                    TreeNode y = xppl;
902 <                    if (y != null && y.red) {
903 <                        y.red = false;
904 <                        xp.red = false;
905 <                        xpp.red = true;
906 <                        x = xpp;
1137 >            if (!p.red) { // rebalance, from CLR
1138 >                TreeNode x = replacement;
1139 >                while (x != null) {
1140 >                    TreeNode xp, xpl;
1141 >                    if (x.red || (xp = x.parent) == null) {
1142 >                        x.red = false;
1143 >                        break;
1144                      }
1145 <                    else {
1146 <                        if (x == xp.left) {
1147 <                            x = xp;
1148 <                            rotateRight(x);
1149 <                            xpp = (xp = x.parent) == null ? null : xp.parent;
1150 <                        }
1151 <                        if (xp != null) {
915 <                            xp.red = false;
916 <                            if (xpp != null) {
917 <                                xpp.red = true;
918 <                                rotateLeft(xpp);
919 <                            }
1145 >                    if (x == (xpl = xp.left)) {
1146 >                        TreeNode sib = xp.right;
1147 >                        if (sib != null && sib.red) {
1148 >                            sib.red = false;
1149 >                            xp.red = true;
1150 >                            rotateLeft(xp);
1151 >                            sib = (xp = x.parent) == null ? null : xp.right;
1152                          }
1153 <                    }
922 <                }
923 <            }
924 <            TreeNode r = root;
925 <            if (r != null && r.red)
926 <                r.red = false;
927 <        }
928 <
929 <        /** From CLR */
930 <        private void fixAfterDeletion(TreeNode x) {
931 <            while (x != null) {
932 <                TreeNode xp, xpl;
933 <                if (x.red || (xp = x.parent) == null) {
934 <                    x.red = false;
935 <                    break;
936 <                }
937 <                if (x == (xpl = xp.left)) {
938 <                    TreeNode sib = xp.right;
939 <                    if (sib != null && sib.red) {
940 <                        sib.red = false;
941 <                        xp.red = true;
942 <                        rotateLeft(xp);
943 <                        sib = (xp = x.parent) == null ? null : xp.right;
944 <                    }
945 <                    if (sib == null)
946 <                        x = xp;
947 <                    else {
948 <                        TreeNode sl = sib.left, sr = sib.right;
949 <                        if ((sr == null || !sr.red) &&
950 <                            (sl == null || !sl.red)) {
951 <                            sib.red = true;
1153 >                        if (sib == null)
1154                              x = xp;
953                        }
1155                          else {
1156 <                            if (sr == null || !sr.red) {
1157 <                                if (sl != null)
1158 <                                    sl.red = false;
1156 >                            TreeNode sl = sib.left, sr = sib.right;
1157 >                            if ((sr == null || !sr.red) &&
1158 >                                (sl == null || !sl.red)) {
1159                                  sib.red = true;
1160 <                                rotateRight(sib);
960 <                                sib = (xp = x.parent) == null ? null : xp.right;
961 <                            }
962 <                            if (sib != null) {
963 <                                sib.red = (xp == null)? false : xp.red;
964 <                                if ((sr = sib.right) != null)
965 <                                    sr.red = false;
1160 >                                x = xp;
1161                              }
1162 <                            if (xp != null) {
1163 <                                xp.red = false;
1164 <                                rotateLeft(xp);
1162 >                            else {
1163 >                                if (sr == null || !sr.red) {
1164 >                                    if (sl != null)
1165 >                                        sl.red = false;
1166 >                                    sib.red = true;
1167 >                                    rotateRight(sib);
1168 >                                    sib = (xp = x.parent) == null ? null : xp.right;
1169 >                                }
1170 >                                if (sib != null) {
1171 >                                    sib.red = (xp == null) ? false : xp.red;
1172 >                                    if ((sr = sib.right) != null)
1173 >                                        sr.red = false;
1174 >                                }
1175 >                                if (xp != null) {
1176 >                                    xp.red = false;
1177 >                                    rotateLeft(xp);
1178 >                                }
1179 >                                x = root;
1180                              }
971                            x = root;
1181                          }
1182                      }
1183 <                }
1184 <                else { // symmetric
1185 <                    TreeNode sib = xpl;
1186 <                    if (sib != null && sib.red) {
1187 <                        sib.red = false;
1188 <                        xp.red = true;
1189 <                        rotateRight(xp);
981 <                        sib = (xp = x.parent) == null ? null : xp.left;
982 <                    }
983 <                    if (sib == null)
984 <                        x = xp;
985 <                    else {
986 <                        TreeNode sl = sib.left, sr = sib.right;
987 <                        if ((sl == null || !sl.red) &&
988 <                            (sr == null || !sr.red)) {
989 <                            sib.red = true;
990 <                            x = xp;
1183 >                    else { // symmetric
1184 >                        TreeNode sib = xpl;
1185 >                        if (sib != null && sib.red) {
1186 >                            sib.red = false;
1187 >                            xp.red = true;
1188 >                            rotateRight(xp);
1189 >                            sib = (xp = x.parent) == null ? null : xp.left;
1190                          }
1191 +                        if (sib == null)
1192 +                            x = xp;
1193                          else {
1194 <                            if (sl == null || !sl.red) {
1195 <                                if (sr != null)
1196 <                                    sr.red = false;
1194 >                            TreeNode sl = sib.left, sr = sib.right;
1195 >                            if ((sl == null || !sl.red) &&
1196 >                                (sr == null || !sr.red)) {
1197                                  sib.red = true;
1198 <                                rotateLeft(sib);
998 <                                sib = (xp = x.parent) == null ? null : xp.left;
1198 >                                x = xp;
1199                              }
1200 <                            if (sib != null) {
1201 <                                sib.red = (xp == null)? false : xp.red;
1202 <                                if ((sl = sib.left) != null)
1203 <                                    sl.red = false;
1204 <                            }
1205 <                            if (xp != null) {
1206 <                                xp.red = false;
1207 <                                rotateRight(xp);
1200 >                            else {
1201 >                                if (sl == null || !sl.red) {
1202 >                                    if (sr != null)
1203 >                                        sr.red = false;
1204 >                                    sib.red = true;
1205 >                                    rotateLeft(sib);
1206 >                                    sib = (xp = x.parent) == null ? null : xp.left;
1207 >                                }
1208 >                                if (sib != null) {
1209 >                                    sib.red = (xp == null) ? false : xp.red;
1210 >                                    if ((sl = sib.left) != null)
1211 >                                        sl.red = false;
1212 >                                }
1213 >                                if (xp != null) {
1214 >                                    xp.red = false;
1215 >                                    rotateRight(xp);
1216 >                                }
1217 >                                x = root;
1218                              }
1009                            x = root;
1219                          }
1220                      }
1221                  }
1222              }
1223 +            if (p == replacement && (pp = p.parent) != null) {
1224 +                if (p == pp.left) // detach pointers
1225 +                    pp.left = null;
1226 +                else if (p == pp.right)
1227 +                    pp.right = null;
1228 +                p.parent = null;
1229 +            }
1230          }
1231      }
1232  
# Line 1025 | Line 1241 | public class ConcurrentHashMapV8<K, V>
1241       * we apply a transform that spreads the impact of higher bits
1242       * downward. There is a tradeoff between speed, utility, and
1243       * quality of bit-spreading. Because many common sets of hashes
1244 <     * are already reaonably distributed across bits (so don't benefit
1244 >     * are already reasonably distributed across bits (so don't benefit
1245       * from spreading), and because we use trees to handle large sets
1246       * of collisions in bins, we don't need excessively high quality.
1247       */
# Line 1172 | Line 1388 | public class ConcurrentHashMapV8<K, V>
1388      }
1389  
1390      /*
1391 <     * Internal versions of the five insertion methods, each a
1391 >     * Internal versions of the six insertion methods, each a
1392       * little more complicated than the last. All have
1393       * the same basic structure as the first (internalPut):
1394       *  1. If table uninitialized, create
# Line 1190 | Line 1406 | public class ConcurrentHashMapV8<K, V>
1406       *    returns from function call.
1407       *  * compute uses the same function-call mechanics, but without
1408       *    the prescans
1409 +     *  * merge acts as putIfAbsent in the absent case, but invokes the
1410 +     *    update function if present
1411       *  * putAll attempts to pre-allocate enough table space
1412       *    and more lazily performs count updates and checks.
1413       *
# Line 1386 | Line 1604 | public class ConcurrentHashMapV8<K, V>
1604  
1605      /** Implementation for computeIfAbsent */
1606      private final Object internalComputeIfAbsent(K k,
1607 <                                                 MappingFunction<? super K, ?> mf) {
1607 >                                                 Fun<? super K, ?> mf) {
1608          int h = spread(k.hashCode());
1609          Object val = null;
1610          int count = 0;
# Line 1399 | Line 1617 | public class ConcurrentHashMapV8<K, V>
1617                  if (casTabAt(tab, i, null, node)) {
1618                      count = 1;
1619                      try {
1620 <                        if ((val = mf.map(k)) != null)
1620 >                        if ((val = mf.apply(k)) != null)
1621                              node.val = val;
1622                      } finally {
1623                          if (val == null)
# Line 1424 | Line 1642 | public class ConcurrentHashMapV8<K, V>
1642                              TreeNode p = t.getTreeNode(h, k, t.root);
1643                              if (p != null)
1644                                  val = p.val;
1645 <                            else if ((val = mf.map(k)) != null) {
1645 >                            else if ((val = mf.apply(k)) != null) {
1646                                  added = true;
1647                                  count = 2;
1648                                  t.putTreeNode(h, k, val);
# Line 1478 | Line 1696 | public class ConcurrentHashMapV8<K, V>
1696                                  }
1697                                  Node last = e;
1698                                  if ((e = e.next) == null) {
1699 <                                    if ((val = mf.map(k)) != null) {
1699 >                                    if ((val = mf.apply(k)) != null) {
1700                                          added = true;
1701                                          last.next = new Node(h, k, val, null);
1702                                          if (count >= TREE_THRESHOLD)
# Line 1504 | Line 1722 | public class ConcurrentHashMapV8<K, V>
1722                  }
1723              }
1724          }
1725 <        if (val == null)
1726 <            throw new NullPointerException();
1727 <        counter.add(1L);
1728 <        if (count > 1)
1729 <            checkForResize();
1725 >        if (val != null) {
1726 >            counter.add(1L);
1727 >            if (count > 1)
1728 >                checkForResize();
1729 >        }
1730          return val;
1731      }
1732  
1733      /** Implementation for compute */
1734 <    @SuppressWarnings("unchecked")
1735 <    private final Object internalCompute(K k,
1518 <                                         RemappingFunction<? super K, V> mf) {
1734 >    @SuppressWarnings("unchecked") private final Object internalCompute
1735 >        (K k, boolean onlyIfPresent, BiFun<? super K, ? super V, ? extends V> mf) {
1736          int h = spread(k.hashCode());
1737          Object val = null;
1738 <        boolean added = false;
1738 >        int delta = 0;
1739          int count = 0;
1740          for (Node[] tab = table;;) {
1741              Node f; int i, fh; Object fk;
1742              if (tab == null)
1743                  tab = initTable();
1744              else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1745 +                if (onlyIfPresent)
1746 +                    break;
1747                  Node node = new Node(fh = h | LOCKED, k, null, null);
1748                  if (casTabAt(tab, i, null, node)) {
1749                      try {
1750                          count = 1;
1751 <                        if ((val = mf.remap(k, null)) != null) {
1751 >                        if ((val = mf.apply(k, null)) != null) {
1752                              node.val = val;
1753 <                            added = true;
1753 >                            delta = 1;
1754                          }
1755                      } finally {
1756 <                        if (!added)
1756 >                        if (delta == 0)
1757                              setTabAt(tab, i, null);
1758                          if (!node.casHash(fh, h)) {
1759                              node.hash = h;
# Line 1553 | Line 1772 | public class ConcurrentHashMapV8<K, V>
1772                          if (tabAt(tab, i) == f) {
1773                              count = 1;
1774                              TreeNode p = t.getTreeNode(h, k, t.root);
1775 <                            Object pv = (p == null)? null : p.val;
1776 <                            if ((val = mf.remap(k, (V)pv)) != null) {
1775 >                            Object pv = (p == null) ? null : p.val;
1776 >                            if ((val = mf.apply(k, (V)pv)) != null) {
1777                                  if (p != null)
1778                                      p.val = val;
1779                                  else {
1780                                      count = 2;
1781 <                                    added = true;
1781 >                                    delta = 1;
1782                                      t.putTreeNode(h, k, val);
1783                                  }
1784                              }
1785 +                            else if (p != null) {
1786 +                                delta = -1;
1787 +                                t.deleteTreeNode(p);
1788 +                            }
1789                          }
1790                      } finally {
1791                          t.release(0);
# Line 1581 | Line 1804 | public class ConcurrentHashMapV8<K, V>
1804                  try {
1805                      if (tabAt(tab, i) == f) {
1806                          count = 1;
1807 <                        for (Node e = f;; ++count) {
1807 >                        for (Node e = f, pred = null;; ++count) {
1808                              Object ek, ev;
1809                              if ((e.hash & HASH_BITS) == h &&
1810                                  (ev = e.val) != null &&
1811                                  ((ek = e.key) == k || k.equals(ek))) {
1812 <                                val = mf.remap(k, (V)ev);
1812 >                                val = mf.apply(k, (V)ev);
1813                                  if (val != null)
1814                                      e.val = val;
1815 +                                else {
1816 +                                    delta = -1;
1817 +                                    Node en = e.next;
1818 +                                    if (pred != null)
1819 +                                        pred.next = en;
1820 +                                    else
1821 +                                        setTabAt(tab, i, en);
1822 +                                }
1823                                  break;
1824                              }
1825 <                            Node last = e;
1825 >                            pred = e;
1826                              if ((e = e.next) == null) {
1827 <                                if ((val = mf.remap(k, null)) != null) {
1828 <                                    last.next = new Node(h, k, val, null);
1829 <                                    added = true;
1827 >                                if (!onlyIfPresent && (val = mf.apply(k, null)) != null) {
1828 >                                    pred.next = new Node(h, k, val, null);
1829 >                                    delta = 1;
1830                                      if (count >= TREE_THRESHOLD)
1831                                          replaceWithTreeBin(tab, i, k);
1832                                  }
# Line 1616 | Line 1847 | public class ConcurrentHashMapV8<K, V>
1847                  }
1848              }
1849          }
1850 <        if (val == null)
1851 <            throw new NullPointerException();
1852 <        if (added) {
1853 <            counter.add(1L);
1850 >        if (delta != 0) {
1851 >            counter.add((long)delta);
1852 >            if (count > 1)
1853 >                checkForResize();
1854 >        }
1855 >        return val;
1856 >    }
1857 >
1858 >    /** Implementation for merge */
1859 >    @SuppressWarnings("unchecked") private final Object internalMerge
1860 >        (K k, V v, BiFun<? super V, ? super V, ? extends V> mf) {
1861 >        int h = spread(k.hashCode());
1862 >        Object val = null;
1863 >        int delta = 0;
1864 >        int count = 0;
1865 >        for (Node[] tab = table;;) {
1866 >            int i; Node f; int fh; Object fk, fv;
1867 >            if (tab == null)
1868 >                tab = initTable();
1869 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1870 >                if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1871 >                    delta = 1;
1872 >                    val = v;
1873 >                    break;
1874 >                }
1875 >            }
1876 >            else if ((fh = f.hash) == MOVED) {
1877 >                if ((fk = f.key) instanceof TreeBin) {
1878 >                    TreeBin t = (TreeBin)fk;
1879 >                    t.acquire(0);
1880 >                    try {
1881 >                        if (tabAt(tab, i) == f) {
1882 >                            count = 1;
1883 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1884 >                            val = (p == null) ? v : mf.apply((V)p.val, v);
1885 >                            if (val != null) {
1886 >                                if (p != null)
1887 >                                    p.val = val;
1888 >                                else {
1889 >                                    count = 2;
1890 >                                    delta = 1;
1891 >                                    t.putTreeNode(h, k, val);
1892 >                                }
1893 >                            }
1894 >                            else if (p != null) {
1895 >                                delta = -1;
1896 >                                t.deleteTreeNode(p);
1897 >                            }
1898 >                        }
1899 >                    } finally {
1900 >                        t.release(0);
1901 >                    }
1902 >                    if (count != 0)
1903 >                        break;
1904 >                }
1905 >                else
1906 >                    tab = (Node[])fk;
1907 >            }
1908 >            else if ((fh & LOCKED) != 0) {
1909 >                checkForResize();
1910 >                f.tryAwaitLock(tab, i);
1911 >            }
1912 >            else if (f.casHash(fh, fh | LOCKED)) {
1913 >                try {
1914 >                    if (tabAt(tab, i) == f) {
1915 >                        count = 1;
1916 >                        for (Node e = f, pred = null;; ++count) {
1917 >                            Object ek, ev;
1918 >                            if ((e.hash & HASH_BITS) == h &&
1919 >                                (ev = e.val) != null &&
1920 >                                ((ek = e.key) == k || k.equals(ek))) {
1921 >                                val = mf.apply(v, (V)ev);
1922 >                                if (val != null)
1923 >                                    e.val = val;
1924 >                                else {
1925 >                                    delta = -1;
1926 >                                    Node en = e.next;
1927 >                                    if (pred != null)
1928 >                                        pred.next = en;
1929 >                                    else
1930 >                                        setTabAt(tab, i, en);
1931 >                                }
1932 >                                break;
1933 >                            }
1934 >                            pred = e;
1935 >                            if ((e = e.next) == null) {
1936 >                                val = v;
1937 >                                pred.next = new Node(h, k, val, null);
1938 >                                delta = 1;
1939 >                                if (count >= TREE_THRESHOLD)
1940 >                                    replaceWithTreeBin(tab, i, k);
1941 >                                break;
1942 >                            }
1943 >                        }
1944 >                    }
1945 >                } finally {
1946 >                    if (!f.casHash(fh | LOCKED, fh)) {
1947 >                        f.hash = fh;
1948 >                        synchronized (f) { f.notifyAll(); };
1949 >                    }
1950 >                }
1951 >                if (count != 0) {
1952 >                    if (tab.length <= 64)
1953 >                        count = 2;
1954 >                    break;
1955 >                }
1956 >            }
1957 >        }
1958 >        if (delta != 0) {
1959 >            counter.add((long)delta);
1960              if (count > 1)
1961                  checkForResize();
1962          }
# Line 1850 | Line 2187 | public class ConcurrentHashMapV8<K, V>
2187          for (int i = bin;;) {      // start upwards sweep
2188              int fh; Node f;
2189              if ((f = tabAt(tab, i)) == null) {
2190 <                if (bin >= 0) {    // no lock needed (or available)
2190 >                if (bin >= 0) {    // Unbuffered; no lock needed (or available)
2191                      if (!casTabAt(tab, i, f, fwd))
2192                          continue;
2193                  }
# Line 1937 | Line 2274 | public class ConcurrentHashMapV8<K, V>
2274      }
2275  
2276      /**
2277 <     * Split a normal bin with list headed by e into lo and hi parts;
2278 <     * install in given table
2277 >     * Splits a normal bin with list headed by e into lo and hi parts;
2278 >     * installs in given table.
2279       */
2280      private static void splitBin(Node[] nextTab, int i, Node e) {
2281          int bit = nextTab.length >>> 1; // bit to split on
# Line 1968 | Line 2305 | public class ConcurrentHashMapV8<K, V>
2305      }
2306  
2307      /**
2308 <     * Split a tree bin into lo and hi parts; install in given table
2308 >     * Splits a tree bin into lo and hi parts; installs in given table.
2309       */
2310      private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2311          int bit = nextTab.length >>> 1;
# Line 2026 | Line 2363 | public class ConcurrentHashMapV8<K, V>
2363                      try {
2364                          if (tabAt(tab, i) == f) {
2365                              for (Node p = t.first; p != null; p = p.next) {
2366 <                                p.val = null;
2367 <                                --delta;
2366 >                                if (p.val != null) { // (currently always true)
2367 >                                    p.val = null;
2368 >                                    --delta;
2369 >                                }
2370                              }
2371                              t.first = null;
2372                              t.root = null;
# Line 2049 | Line 2388 | public class ConcurrentHashMapV8<K, V>
2388                  try {
2389                      if (tabAt(tab, i) == f) {
2390                          for (Node e = f; e != null; e = e.next) {
2391 <                            e.val = null;
2392 <                            --delta;
2391 >                            if (e.val != null) {  // (currently always true)
2392 >                                e.val = null;
2393 >                                --delta;
2394 >                            }
2395                          }
2396                          setTabAt(tab, i, null);
2397                          ++i;
# Line 2071 | Line 2412 | public class ConcurrentHashMapV8<K, V>
2412  
2413      /**
2414       * Encapsulates traversal for methods such as containsValue; also
2415 <     * serves as a base class for other iterators.
2415 >     * serves as a base class for other iterators and bulk tasks.
2416       *
2417       * At each step, the iterator snapshots the key ("nextKey") and
2418       * value ("nextVal") of a valid node (i.e., one that, at point of
# Line 2079 | Line 2420 | public class ConcurrentHashMapV8<K, V>
2420       * change (including to null, indicating deletion), field nextVal
2421       * might not be accurate at point of use, but still maintains the
2422       * weak consistency property of holding a value that was once
2423 <     * valid.
2423 >     * valid. To support iterator.remove, the nextKey field is not
2424 >     * updated (nulled out) when the iterator cannot advance.
2425       *
2426       * Internal traversals directly access these fields, as in:
2427 <     * {@code while (it.next != null) { process(it.nextKey); it.advance(); }}
2427 >     * {@code while (it.advance() != null) { process(it.nextKey); }}
2428       *
2429 <     * Exported iterators (subclasses of ViewIterator) extract key,
2430 <     * value, or key-value pairs as return values of Iterator.next(),
2431 <     * and encapsulate the it.next check as hasNext();
2429 >     * Exported iterators must track whether the iterator has advanced
2430 >     * (in hasNext vs next) (by setting/checking/nulling field
2431 >     * nextVal), and then extract key, value, or key-value pairs as
2432 >     * return values of next().
2433       *
2434       * The iterator visits once each still-valid node that was
2435       * reachable upon iterator construction. It might miss some that
# Line 2105 | Line 2448 | public class ConcurrentHashMapV8<K, V>
2448       * across threads, iteration terminates if a bounds checks fails
2449       * for a table read.
2450       *
2451 <     * The range-based constructor enables creation of parallel
2452 <     * range-splitting traversals. (Not yet implemented.)
2451 >     * This class extends ForkJoinTask to streamline parallel
2452 >     * iteration in bulk operations (see BulkTask). This adds only an
2453 >     * int of space overhead, which is close enough to negligible in
2454 >     * cases where it is not needed to not worry about it.  Because
2455 >     * ForkJoinTask is Serializable, but iterators need not be, we
2456 >     * need to add warning suppressions.
2457       */
2458 <    static class InternalIterator {
2458 >    @SuppressWarnings("serial") static class Traverser<K,V,R> extends ForkJoinTask<R> {
2459 >        final ConcurrentHashMapV8<K, V> map;
2460          Node next;           // the next entry to use
2113        Node last;           // the last entry used
2461          Object nextKey;      // cached key field of next
2462          Object nextVal;      // cached val field of next
2463          Node[] tab;          // current table; updated if resized
2464          int index;           // index of bin to use next
2465          int baseIndex;       // current index of initial table
2466 <        final int baseLimit; // index bound for initial table
2467 <        final int baseSize;  // initial table size
2466 >        int baseLimit;       // index bound for initial table
2467 >        int baseSize;        // initial table size
2468  
2469          /** Creates iterator for all entries in the table. */
2470 <        InternalIterator(Node[] tab) {
2471 <            this.tab = tab;
2472 <            baseLimit = baseSize = (tab == null) ? 0 : tab.length;
2473 <            index = baseIndex = 0;
2474 <            next = null;
2475 <            advance();
2476 <        }
2477 <
2478 <        /** Creates iterator for the given range of the table */
2479 <        InternalIterator(Node[] tab, int lo, int hi) {
2480 <            this.tab = tab;
2481 <            baseSize = (tab == null) ? 0 : tab.length;
2482 <            baseLimit = (hi <= baseSize) ? hi : baseSize;
2483 <            index = baseIndex = (lo >= 0) ? lo : 0;
2484 <            next = null;
2485 <            advance();
2486 <        }
2487 <
2488 <        /** Advances next. See above for explanation. */
2489 <        final void advance() {
2490 <            Node e = last = next;
2470 >        Traverser(ConcurrentHashMapV8<K, V> map) {
2471 >            this.map = map;
2472 >        }
2473 >
2474 >        /** Creates iterator for split() methods */
2475 >        Traverser(Traverser<K,V,?> it) {
2476 >            ConcurrentHashMapV8<K, V> m; Node[] t;
2477 >            if ((m = this.map = it.map) == null)
2478 >                t = null;
2479 >            else if ((t = it.tab) == null && // force parent tab initialization
2480 >                     (t = it.tab = m.table) != null)
2481 >                it.baseLimit = it.baseSize = t.length;
2482 >            this.tab = t;
2483 >            this.baseSize = it.baseSize;
2484 >            it.baseLimit = this.index = this.baseIndex =
2485 >                ((this.baseLimit = it.baseLimit) + it.baseIndex + 1) >>> 1;
2486 >        }
2487 >
2488 >        /**
2489 >         * Advances next; returns nextVal or null if terminated.
2490 >         * See above for explanation.
2491 >         */
2492 >        final Object advance() {
2493 >            Node e = next;
2494 >            Object ev = null;
2495              outer: do {
2496                  if (e != null)                  // advance past used/skipped node
2497                      e = e.next;
2498                  while (e == null) {             // get to next non-null bin
2499 +                    ConcurrentHashMapV8<K, V> m;
2500                      Node[] t; int b, i, n; Object ek; // checks must use locals
2501 <                    if ((b = baseIndex) >= baseLimit || (i = index) < 0 ||
2502 <                        (t = tab) == null || i >= (n = t.length))
2501 >                    if ((t = tab) != null)
2502 >                        n = t.length;
2503 >                    else if ((m = map) != null && (t = tab = m.table) != null)
2504 >                        n = baseLimit = baseSize = t.length;
2505 >                    else
2506 >                        break outer;
2507 >                    if ((b = baseIndex) >= baseLimit ||
2508 >                        (i = index) < 0 || i >= n)
2509                          break outer;
2510 <                    else if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2510 >                    if ((e = tabAt(t, i)) != null && e.hash == MOVED) {
2511                          if ((ek = e.key) instanceof TreeBin)
2512                              e = ((TreeBin)ek).first;
2513                          else {
# Line 2160 | Line 2518 | public class ConcurrentHashMapV8<K, V>
2518                      index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2519                  }
2520                  nextKey = e.key;
2521 <            } while ((nextVal = e.val) == null);// skip deleted or special nodes
2521 >            } while ((ev = e.val) == null);    // skip deleted or special nodes
2522              next = e;
2523 +            return nextVal = ev;
2524          }
2525 +
2526 +        public final void remove() {
2527 +            Object k = nextKey;
2528 +            if (k == null && (advance() == null || (k = nextKey) == null))
2529 +                throw new IllegalStateException();
2530 +            map.internalReplace(k, null, null);
2531 +        }
2532 +
2533 +        public final boolean hasNext() {
2534 +            return nextVal != null || advance() != null;
2535 +        }
2536 +
2537 +        public final boolean hasMoreElements() { return hasNext(); }
2538 +        public final void setRawResult(Object x) { }
2539 +        public R getRawResult() { return null; }
2540 +        public boolean exec() { return true; }
2541      }
2542  
2543      /* ---------------- Public operations -------------- */
2544  
2545      /**
2546 <     * Creates a new, empty map with the default initial table size (16),
2546 >     * Creates a new, empty map with the default initial table size (16).
2547       */
2548      public ConcurrentHashMapV8() {
2549          this.counter = new LongAdder();
# Line 2249 | Line 2624 | public class ConcurrentHashMapV8<K, V>
2624          if (initialCapacity < concurrencyLevel)   // Use at least as many bins
2625              initialCapacity = concurrencyLevel;   // as estimated threads
2626          long size = (long)(1.0 + (long)initialCapacity / loadFactor);
2627 <        int cap = ((size >= (long)MAXIMUM_CAPACITY) ?
2628 <                   MAXIMUM_CAPACITY: tableSizeFor((int)size));
2627 >        int cap = (size >= (long)MAXIMUM_CAPACITY) ?
2628 >            MAXIMUM_CAPACITY : tableSizeFor((int)size);
2629          this.counter = new LongAdder();
2630          this.sizeCtl = cap;
2631      }
2632  
2633      /**
2634 +     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2635 +     * from the given type to {@code Boolean.TRUE}.
2636 +     *
2637 +     * @return the new set
2638 +     */
2639 +    public static <K> KeySetView<K,Boolean> newKeySet() {
2640 +        return new KeySetView<K,Boolean>(new ConcurrentHashMapV8<K,Boolean>(),
2641 +                                      Boolean.TRUE);
2642 +    }
2643 +
2644 +    /**
2645 +     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2646 +     * from the given type to {@code Boolean.TRUE}.
2647 +     *
2648 +     * @param initialCapacity The implementation performs internal
2649 +     * sizing to accommodate this many elements.
2650 +     * @throws IllegalArgumentException if the initial capacity of
2651 +     * elements is negative
2652 +     * @return the new set
2653 +     */
2654 +    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2655 +        return new KeySetView<K,Boolean>(new ConcurrentHashMapV8<K,Boolean>(initialCapacity),
2656 +                                      Boolean.TRUE);
2657 +    }
2658 +
2659 +    /**
2660       * {@inheritDoc}
2661       */
2662      public boolean isEmpty() {
# Line 2272 | Line 2673 | public class ConcurrentHashMapV8<K, V>
2673                  (int)n);
2674      }
2675  
2676 <    final long longSize() { // accurate version of size needed for views
2676 >    /**
2677 >     * Returns the number of mappings. This method should be used
2678 >     * instead of {@link #size} because a ConcurrentHashMapV8 may
2679 >     * contain more mappings than can be represented as an int. The
2680 >     * value returned is a snapshot; the actual count may differ if
2681 >     * there are ongoing concurrent insertions or removals.
2682 >     *
2683 >     * @return the number of mappings
2684 >     */
2685 >    public long mappingCount() {
2686          long n = counter.sum();
2687 <        return (n < 0L) ? 0L : n;
2687 >        return (n < 0L) ? 0L : n; // ignore transient negative values
2688      }
2689  
2690      /**
# Line 2288 | Line 2698 | public class ConcurrentHashMapV8<K, V>
2698       *
2699       * @throws NullPointerException if the specified key is null
2700       */
2701 <    @SuppressWarnings("unchecked")
2292 <    public V get(Object key) {
2701 >    @SuppressWarnings("unchecked") public V get(Object key) {
2702          if (key == null)
2703              throw new NullPointerException();
2704          return (V)internalGet(key);
2705      }
2706  
2707      /**
2708 +     * Returns the value to which the specified key is mapped,
2709 +     * or the given defaultValue if this map contains no mapping for the key.
2710 +     *
2711 +     * @param key the key
2712 +     * @param defaultValue the value to return if this map contains
2713 +     * no mapping for the given key
2714 +     * @return the mapping for the key, if present; else the defaultValue
2715 +     * @throws NullPointerException if the specified key is null
2716 +     */
2717 +    @SuppressWarnings("unchecked") public V getValueOrDefault(Object key, V defaultValue) {
2718 +        if (key == null)
2719 +            throw new NullPointerException();
2720 +        V v = (V) internalGet(key);
2721 +        return v == null ? defaultValue : v;
2722 +    }
2723 +
2724 +    /**
2725       * Tests if the specified object is a key in this table.
2726       *
2727       * @param  key   possible key
# Line 2324 | Line 2750 | public class ConcurrentHashMapV8<K, V>
2750          if (value == null)
2751              throw new NullPointerException();
2752          Object v;
2753 <        InternalIterator it = new InternalIterator(table);
2754 <        while (it.next != null) {
2755 <            if ((v = it.nextVal) == value || value.equals(v))
2753 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2754 >        while ((v = it.advance()) != null) {
2755 >            if (v == value || value.equals(v))
2756                  return true;
2331            it.advance();
2757          }
2758          return false;
2759      }
# Line 2365 | Line 2790 | public class ConcurrentHashMapV8<K, V>
2790       *         {@code null} if there was no mapping for {@code key}
2791       * @throws NullPointerException if the specified key or value is null
2792       */
2793 <    @SuppressWarnings("unchecked")
2369 <    public V put(K key, V value) {
2793 >    @SuppressWarnings("unchecked") public V put(K key, V value) {
2794          if (key == null || value == null)
2795              throw new NullPointerException();
2796          return (V)internalPut(key, value);
# Line 2379 | Line 2803 | public class ConcurrentHashMapV8<K, V>
2803       *         or {@code null} if there was no mapping for the key
2804       * @throws NullPointerException if the specified key or value is null
2805       */
2806 <    @SuppressWarnings("unchecked")
2383 <    public V putIfAbsent(K key, V value) {
2806 >    @SuppressWarnings("unchecked") public V putIfAbsent(K key, V value) {
2807          if (key == null || value == null)
2808              throw new NullPointerException();
2809          return (V)internalPutIfAbsent(key, value);
# Line 2399 | Line 2822 | public class ConcurrentHashMapV8<K, V>
2822  
2823      /**
2824       * If the specified key is not already associated with a value,
2825 <     * computes its value using the given mappingFunction and
2826 <     * enters it into the map.  This is equivalent to
2825 >     * computes its value using the given mappingFunction and enters
2826 >     * it into the map unless null.  This is equivalent to
2827       * <pre> {@code
2828       * if (map.containsKey(key))
2829       *   return map.get(key);
2830 <     * value = mappingFunction.map(key);
2831 <     * map.put(key, value);
2830 >     * value = mappingFunction.apply(key);
2831 >     * if (value != null)
2832 >     *   map.put(key, value);
2833       * return value;}</pre>
2834       *
2835       * except that the action is performed atomically.  If the
2836 <     * function returns {@code null} (in which case a {@code
2837 <     * NullPointerException} is thrown), or the function itself throws
2838 <     * an (unchecked) exception, the exception is rethrown to its
2839 <     * caller, and no mapping is recorded.  Some attempted update
2840 <     * operations on this map by other threads may be blocked while
2841 <     * computation is in progress, so the computation should be short
2842 <     * and simple, and must not attempt to update any other mappings
2843 <     * of this Map. The most appropriate usage is to construct a new
2844 <     * object serving as an initial mapped value, or memoized result,
2421 <     * as in:
2836 >     * function returns {@code null} no mapping is recorded. If the
2837 >     * function itself throws an (unchecked) exception, the exception
2838 >     * is rethrown to its caller, and no mapping is recorded.  Some
2839 >     * attempted update operations on this map by other threads may be
2840 >     * blocked while computation is in progress, so the computation
2841 >     * should be short and simple, and must not attempt to update any
2842 >     * other mappings of this Map. The most appropriate usage is to
2843 >     * construct a new object serving as an initial mapped value, or
2844 >     * memoized result, as in:
2845       *
2846       *  <pre> {@code
2847 <     * map.computeIfAbsent(key, new MappingFunction<K, V>() {
2847 >     * map.computeIfAbsent(key, new Fun<K, V>() {
2848       *   public V map(K k) { return new Value(f(k)); }});}</pre>
2849       *
2850       * @param key key with which the specified value is to be associated
2851       * @param mappingFunction the function to compute a value
2852       * @return the current (existing or computed) value associated with
2853 <     *         the specified key.
2854 <     * @throws NullPointerException if the specified key, mappingFunction,
2855 <     *         or computed value is null
2853 >     *         the specified key, or null if the computed value is null
2854 >     * @throws NullPointerException if the specified key or mappingFunction
2855 >     *         is null
2856       * @throws IllegalStateException if the computation detectably
2857       *         attempts a recursive update to this map that would
2858       *         otherwise never complete
2859       * @throws RuntimeException or Error if the mappingFunction does so,
2860       *         in which case the mapping is left unestablished
2861       */
2862 <    @SuppressWarnings("unchecked")
2863 <    public V computeIfAbsent(K key, MappingFunction<? super K, ? extends V> mappingFunction) {
2862 >    @SuppressWarnings("unchecked") public V computeIfAbsent
2863 >        (K key, Fun<? super K, ? extends V> mappingFunction) {
2864          if (key == null || mappingFunction == null)
2865              throw new NullPointerException();
2866          return (V)internalComputeIfAbsent(key, mappingFunction);
2867      }
2868  
2869      /**
2870 <     * Computes and enters a new mapping value given a key and
2870 >     * If the given key is present, computes a new mapping value given a key and
2871 >     * its current mapped value. This is equivalent to
2872 >     *  <pre> {@code
2873 >     *   if (map.containsKey(key)) {
2874 >     *     value = remappingFunction.apply(key, map.get(key));
2875 >     *     if (value != null)
2876 >     *       map.put(key, value);
2877 >     *     else
2878 >     *       map.remove(key);
2879 >     *   }
2880 >     * }</pre>
2881 >     *
2882 >     * except that the action is performed atomically.  If the
2883 >     * function returns {@code null}, the mapping is removed.  If the
2884 >     * function itself throws an (unchecked) exception, the exception
2885 >     * is rethrown to its caller, and the current mapping is left
2886 >     * unchanged.  Some attempted update operations on this map by
2887 >     * other threads may be blocked while computation is in progress,
2888 >     * so the computation should be short and simple, and must not
2889 >     * attempt to update any other mappings of this Map. For example,
2890 >     * to either create or append new messages to a value mapping:
2891 >     *
2892 >     * @param key key with which the specified value is to be associated
2893 >     * @param remappingFunction the function to compute a value
2894 >     * @return the new value associated with the specified key, or null if none
2895 >     * @throws NullPointerException if the specified key or remappingFunction
2896 >     *         is null
2897 >     * @throws IllegalStateException if the computation detectably
2898 >     *         attempts a recursive update to this map that would
2899 >     *         otherwise never complete
2900 >     * @throws RuntimeException or Error if the remappingFunction does so,
2901 >     *         in which case the mapping is unchanged
2902 >     */
2903 >    @SuppressWarnings("unchecked") public V computeIfPresent
2904 >        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2905 >        if (key == null || remappingFunction == null)
2906 >            throw new NullPointerException();
2907 >        return (V)internalCompute(key, true, remappingFunction);
2908 >    }
2909 >
2910 >    /**
2911 >     * Computes a new mapping value given a key and
2912       * its current mapped value (or {@code null} if there is no current
2913       * mapping). This is equivalent to
2914       *  <pre> {@code
2915 <     *  map.put(key, remappingFunction.remap(key, map.get(key));
2915 >     *   value = remappingFunction.apply(key, map.get(key));
2916 >     *   if (value != null)
2917 >     *     map.put(key, value);
2918 >     *   else
2919 >     *     map.remove(key);
2920       * }</pre>
2921       *
2922       * except that the action is performed atomically.  If the
2923 <     * function returns {@code null} (in which case a {@code
2924 <     * NullPointerException} is thrown), or the function itself throws
2925 <     * an (unchecked) exception, the exception is rethrown to its
2926 <     * caller, and current mapping is left unchanged.  Some attempted
2927 <     * update operations on this map by other threads may be blocked
2928 <     * while computation is in progress, so the computation should be
2929 <     * short and simple, and must not attempt to update any other
2930 <     * mappings of this Map. For example, to either create or
2463 <     * append new messages to a value mapping:
2923 >     * function returns {@code null}, the mapping is removed.  If the
2924 >     * function itself throws an (unchecked) exception, the exception
2925 >     * is rethrown to its caller, and the current mapping is left
2926 >     * unchanged.  Some attempted update operations on this map by
2927 >     * other threads may be blocked while computation is in progress,
2928 >     * so the computation should be short and simple, and must not
2929 >     * attempt to update any other mappings of this Map. For example,
2930 >     * to either create or append new messages to a value mapping:
2931       *
2932       * <pre> {@code
2933       * Map<Key, String> map = ...;
2934       * final String msg = ...;
2935 <     * map.compute(key, new RemappingFunction<Key, String>() {
2936 <     *   public String remap(Key k, String v) {
2935 >     * map.compute(key, new BiFun<Key, String, String>() {
2936 >     *   public String apply(Key k, String v) {
2937       *    return (v == null) ? msg : v + msg;});}}</pre>
2938       *
2939       * @param key key with which the specified value is to be associated
2940       * @param remappingFunction the function to compute a value
2941 <     * @return the new value associated with
2475 <     *         the specified key.
2941 >     * @return the new value associated with the specified key, or null if none
2942       * @throws NullPointerException if the specified key or remappingFunction
2943 <     *         or computed value is null
2943 >     *         is null
2944       * @throws IllegalStateException if the computation detectably
2945       *         attempts a recursive update to this map that would
2946       *         otherwise never complete
2947       * @throws RuntimeException or Error if the remappingFunction does so,
2948       *         in which case the mapping is unchanged
2949       */
2950 <    @SuppressWarnings("unchecked")
2951 <    public V compute(K key, RemappingFunction<? super K, V> remappingFunction) {
2950 >    @SuppressWarnings("unchecked") public V compute
2951 >        (K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2952          if (key == null || remappingFunction == null)
2953              throw new NullPointerException();
2954 <        return (V)internalCompute(key, remappingFunction);
2954 >        return (V)internalCompute(key, false, remappingFunction);
2955 >    }
2956 >
2957 >    /**
2958 >     * If the specified key is not already associated
2959 >     * with a value, associate it with the given value.
2960 >     * Otherwise, replace the value with the results of
2961 >     * the given remapping function. This is equivalent to:
2962 >     *  <pre> {@code
2963 >     *   if (!map.containsKey(key))
2964 >     *     map.put(value);
2965 >     *   else {
2966 >     *     newValue = remappingFunction.apply(map.get(key), value);
2967 >     *     if (value != null)
2968 >     *       map.put(key, value);
2969 >     *     else
2970 >     *       map.remove(key);
2971 >     *   }
2972 >     * }</pre>
2973 >     * except that the action is performed atomically.  If the
2974 >     * function returns {@code null}, the mapping is removed.  If the
2975 >     * function itself throws an (unchecked) exception, the exception
2976 >     * is rethrown to its caller, and the current mapping is left
2977 >     * unchanged.  Some attempted update operations on this map by
2978 >     * other threads may be blocked while computation is in progress,
2979 >     * so the computation should be short and simple, and must not
2980 >     * attempt to update any other mappings of this Map.
2981 >     */
2982 >    @SuppressWarnings("unchecked") public V merge
2983 >        (K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2984 >        if (key == null || value == null || remappingFunction == null)
2985 >            throw new NullPointerException();
2986 >        return (V)internalMerge(key, value, remappingFunction);
2987      }
2988  
2989      /**
# Line 2497 | Line 2995 | public class ConcurrentHashMapV8<K, V>
2995       *         {@code null} if there was no mapping for {@code key}
2996       * @throws NullPointerException if the specified key is null
2997       */
2998 <    @SuppressWarnings("unchecked")
2501 <    public V remove(Object key) {
2998 >    @SuppressWarnings("unchecked") public V remove(Object key) {
2999          if (key == null)
3000              throw new NullPointerException();
3001          return (V)internalReplace(key, null, null);
# Line 2535 | Line 3032 | public class ConcurrentHashMapV8<K, V>
3032       *         or {@code null} if there was no mapping for the key
3033       * @throws NullPointerException if the specified key or value is null
3034       */
3035 <    @SuppressWarnings("unchecked")
2539 <    public V replace(K key, V value) {
3035 >    @SuppressWarnings("unchecked") public V replace(K key, V value) {
3036          if (key == null || value == null)
3037              throw new NullPointerException();
3038          return (V)internalReplace(key, value, null);
# Line 2552 | Line 3048 | public class ConcurrentHashMapV8<K, V>
3048      /**
3049       * Returns a {@link Set} view of the keys contained in this map.
3050       * The set is backed by the map, so changes to the map are
3051 <     * reflected in the set, and vice-versa.  The set supports element
2556 <     * removal, which removes the corresponding mapping from this map,
2557 <     * via the {@code Iterator.remove}, {@code Set.remove},
2558 <     * {@code removeAll}, {@code retainAll}, and {@code clear}
2559 <     * operations.  It does not support the {@code add} or
2560 <     * {@code addAll} operations.
3051 >     * reflected in the set, and vice-versa.
3052       *
3053 <     * <p>The view's {@code iterator} is a "weakly consistent" iterator
3054 <     * that will never throw {@link ConcurrentModificationException},
3055 <     * and guarantees to traverse elements as they existed upon
3056 <     * construction of the iterator, and may (but is not guaranteed to)
3057 <     * reflect any modifications subsequent to construction.
3053 >     * @return the set view
3054 >     */
3055 >    public KeySetView<K,V> keySet() {
3056 >        KeySetView<K,V> ks = keySet;
3057 >        return (ks != null) ? ks : (keySet = new KeySetView<K,V>(this, null));
3058 >    }
3059 >
3060 >    /**
3061 >     * Returns a {@link Set} view of the keys in this map, using the
3062 >     * given common mapped value for any additions (i.e., {@link
3063 >     * Collection#add} and {@link Collection#addAll}). This is of
3064 >     * course only appropriate if it is acceptable to use the same
3065 >     * value for all additions from this view.
3066 >     *
3067 >     * @param mappedValue the mapped value to use for any
3068 >     * additions.
3069 >     * @return the set view
3070 >     * @throws NullPointerException if the mappedValue is null
3071       */
3072 <    public Set<K> keySet() {
3073 <        KeySet<K,V> ks = keySet;
3074 <        return (ks != null) ? ks : (keySet = new KeySet<K,V>(this));
3072 >    public KeySetView<K,V> keySet(V mappedValue) {
3073 >        if (mappedValue == null)
3074 >            throw new NullPointerException();
3075 >        return new KeySetView<K,V>(this, mappedValue);
3076      }
3077  
3078      /**
# Line 2633 | Line 3138 | public class ConcurrentHashMapV8<K, V>
3138      }
3139  
3140      /**
3141 +     * Returns a partitionable iterator of the keys in this map.
3142 +     *
3143 +     * @return a partitionable iterator of the keys in this map
3144 +     */
3145 +    public Spliterator<K> keySpliterator() {
3146 +        return new KeyIterator<K,V>(this);
3147 +    }
3148 +
3149 +    /**
3150 +     * Returns a partitionable iterator of the values in this map.
3151 +     *
3152 +     * @return a partitionable iterator of the values in this map
3153 +     */
3154 +    public Spliterator<V> valueSpliterator() {
3155 +        return new ValueIterator<K,V>(this);
3156 +    }
3157 +
3158 +    /**
3159 +     * Returns a partitionable iterator of the entries in this map.
3160 +     *
3161 +     * @return a partitionable iterator of the entries in this map
3162 +     */
3163 +    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
3164 +        return new EntryIterator<K,V>(this);
3165 +    }
3166 +
3167 +    /**
3168       * Returns the hash code value for this {@link Map}, i.e.,
3169       * the sum of, for each key-value pair in the map,
3170       * {@code key.hashCode() ^ value.hashCode()}.
# Line 2641 | Line 3173 | public class ConcurrentHashMapV8<K, V>
3173       */
3174      public int hashCode() {
3175          int h = 0;
3176 <        InternalIterator it = new InternalIterator(table);
3177 <        while (it.next != null) {
3178 <            h += it.nextKey.hashCode() ^ it.nextVal.hashCode();
3179 <            it.advance();
3176 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3177 >        Object v;
3178 >        while ((v = it.advance()) != null) {
3179 >            h += it.nextKey.hashCode() ^ v.hashCode();
3180          }
3181          return h;
3182      }
# Line 2661 | Line 3193 | public class ConcurrentHashMapV8<K, V>
3193       * @return a string representation of this map
3194       */
3195      public String toString() {
3196 <        InternalIterator it = new InternalIterator(table);
3196 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3197          StringBuilder sb = new StringBuilder();
3198          sb.append('{');
3199 <        if (it.next != null) {
3199 >        Object v;
3200 >        if ((v = it.advance()) != null) {
3201              for (;;) {
3202 <                Object k = it.nextKey, v = it.nextVal;
3202 >                Object k = it.nextKey;
3203                  sb.append(k == this ? "(this Map)" : k);
3204                  sb.append('=');
3205                  sb.append(v == this ? "(this Map)" : v);
3206 <                it.advance();
2674 <                if (it.next == null)
3206 >                if ((v = it.advance()) == null)
3207                      break;
3208                  sb.append(',').append(' ');
3209              }
# Line 2694 | Line 3226 | public class ConcurrentHashMapV8<K, V>
3226              if (!(o instanceof Map))
3227                  return false;
3228              Map<?,?> m = (Map<?,?>) o;
3229 <            InternalIterator it = new InternalIterator(table);
3230 <            while (it.next != null) {
3231 <                Object val = it.nextVal;
3229 >            Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3230 >            Object val;
3231 >            while ((val = it.advance()) != null) {
3232                  Object v = m.get(it.nextKey);
3233                  if (v == null || (v != val && !v.equals(val)))
3234                      return false;
2703                it.advance();
3235              }
3236              for (Map.Entry<?,?> e : m.entrySet()) {
3237                  Object mk, mv, v;
# Line 2716 | Line 3247 | public class ConcurrentHashMapV8<K, V>
3247  
3248      /* ----------------Iterators -------------- */
3249  
3250 <    /**
3251 <     * Base class for key, value, and entry iterators.  Adds a map
3252 <     * reference to InternalIterator to support Iterator.remove.
3253 <     */
3254 <    static abstract class ViewIterator<K,V> extends InternalIterator {
2724 <        final ConcurrentHashMapV8<K, V> map;
2725 <        ViewIterator(ConcurrentHashMapV8<K, V> map) {
2726 <            super(map.table);
2727 <            this.map = map;
3250 >    @SuppressWarnings("serial") static final class KeyIterator<K,V> extends Traverser<K,V,Object>
3251 >        implements Spliterator<K>, Enumeration<K> {
3252 >        KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3253 >        KeyIterator(Traverser<K,V,Object> it) {
3254 >            super(it);
3255          }
3256 <
3257 <        public final void remove() {
2731 <            if (last == null)
3256 >        public KeyIterator<K,V> split() {
3257 >            if (nextKey != null)
3258                  throw new IllegalStateException();
3259 <            map.remove(last.key);
2734 <            last = null;
3259 >            return new KeyIterator<K,V>(this);
3260          }
3261 <
3262 <        public final boolean hasNext()         { return next != null; }
2738 <        public final boolean hasMoreElements() { return next != null; }
2739 <    }
2740 <
2741 <    static final class KeyIterator<K,V> extends ViewIterator<K,V>
2742 <        implements Iterator<K>, Enumeration<K> {
2743 <        KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
2744 <
2745 <        @SuppressWarnings("unchecked")
2746 <        public final K next() {
2747 <            if (next == null)
3261 >        @SuppressWarnings("unchecked") public final K next() {
3262 >            if (nextVal == null && advance() == null)
3263                  throw new NoSuchElementException();
3264              Object k = nextKey;
3265 <            advance();
3266 <            return (K)k;
3265 >            nextVal = null;
3266 >            return (K) k;
3267          }
3268  
3269          public final K nextElement() { return next(); }
3270      }
3271  
3272 <    static final class ValueIterator<K,V> extends ViewIterator<K,V>
3273 <        implements Iterator<V>, Enumeration<V> {
3272 >    @SuppressWarnings("serial") static final class ValueIterator<K,V> extends Traverser<K,V,Object>
3273 >        implements Spliterator<V>, Enumeration<V> {
3274          ValueIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3275 +        ValueIterator(Traverser<K,V,Object> it) {
3276 +            super(it);
3277 +        }
3278 +        public ValueIterator<K,V> split() {
3279 +            if (nextKey != null)
3280 +                throw new IllegalStateException();
3281 +            return new ValueIterator<K,V>(this);
3282 +        }
3283  
3284 <        @SuppressWarnings("unchecked")
3285 <        public final V next() {
3286 <            if (next == null)
3284 >        @SuppressWarnings("unchecked") public final V next() {
3285 >            Object v;
3286 >            if ((v = nextVal) == null && (v = advance()) == null)
3287                  throw new NoSuchElementException();
3288 <            Object v = nextVal;
3289 <            advance();
2767 <            return (V)v;
3288 >            nextVal = null;
3289 >            return (V) v;
3290          }
3291  
3292          public final V nextElement() { return next(); }
3293      }
3294  
3295 <    static final class EntryIterator<K,V> extends ViewIterator<K,V>
3296 <        implements Iterator<Map.Entry<K,V>> {
3295 >    @SuppressWarnings("serial") static final class EntryIterator<K,V> extends Traverser<K,V,Object>
3296 >        implements Spliterator<Map.Entry<K,V>> {
3297          EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3298 <
3299 <        @SuppressWarnings("unchecked")
3300 <        public final Map.Entry<K,V> next() {
3301 <            if (next == null)
3302 <                throw new NoSuchElementException();
3303 <            Object k = nextKey;
3304 <            Object v = nextVal;
2783 <            advance();
2784 <            return new WriteThroughEntry<K,V>((K)k, (V)v, map);
3298 >        EntryIterator(Traverser<K,V,Object> it) {
3299 >            super(it);
3300 >        }
3301 >        public EntryIterator<K,V> split() {
3302 >            if (nextKey != null)
3303 >                throw new IllegalStateException();
3304 >            return new EntryIterator<K,V>(this);
3305          }
2786    }
2787
2788    static final class SnapshotEntryIterator<K,V> extends ViewIterator<K,V>
2789        implements Iterator<Map.Entry<K,V>> {
2790        SnapshotEntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3306  
3307 <        @SuppressWarnings("unchecked")
3308 <        public final Map.Entry<K,V> next() {
3309 <            if (next == null)
3307 >        @SuppressWarnings("unchecked") public final Map.Entry<K,V> next() {
3308 >            Object v;
3309 >            if ((v = nextVal) == null && (v = advance()) == null)
3310                  throw new NoSuchElementException();
3311              Object k = nextKey;
3312 <            Object v = nextVal;
3313 <            advance();
2799 <            return new SnapshotEntry<K,V>((K)k, (V)v);
3312 >            nextVal = null;
3313 >            return new MapEntry<K,V>((K)k, (V)v, map);
3314          }
3315      }
3316  
3317      /**
3318 <     * Base of writeThrough and Snapshot entry classes
3318 >     * Exported Entry for iterators
3319       */
3320 <    static abstract class MapEntry<K,V> implements Map.Entry<K, V> {
3320 >    static final class MapEntry<K,V> implements Map.Entry<K, V> {
3321          final K key; // non-null
3322          V val;       // non-null
3323 <        MapEntry(K key, V val)        { this.key = key; this.val = val; }
3323 >        final ConcurrentHashMapV8<K, V> map;
3324 >        MapEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
3325 >            this.key = key;
3326 >            this.val = val;
3327 >            this.map = map;
3328 >        }
3329          public final K getKey()       { return key; }
3330          public final V getValue()     { return val; }
3331          public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
# Line 2821 | Line 3340 | public class ConcurrentHashMapV8<K, V>
3340                      (v == val || v.equals(val)));
3341          }
3342  
2824        public abstract V setValue(V value);
2825    }
2826
2827    /**
2828     * Entry used by EntryIterator.next(), that relays setValue
2829     * changes to the underlying map.
2830     */
2831    static final class WriteThroughEntry<K,V> extends MapEntry<K,V>
2832        implements Map.Entry<K, V> {
2833        final ConcurrentHashMapV8<K, V> map;
2834        WriteThroughEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
2835            super(key, val);
2836            this.map = map;
2837        }
2838
3343          /**
3344           * Sets our entry's value and writes through to the map. The
3345 <         * value to return is somewhat arbitrary here. Since a
3346 <         * WriteThroughEntry does not necessarily track asynchronous
3347 <         * changes, the most recent "previous" value could be
3348 <         * different from what we return (or could even have been
3349 <         * removed in which case the put will re-establish). We do not
2846 <         * and cannot guarantee more.
3345 >         * value to return is somewhat arbitrary here. Since we do not
3346 >         * necessarily track asynchronous changes, the most recent
3347 >         * "previous" value could be different from what we return (or
3348 >         * could even have been removed in which case the put will
3349 >         * re-establish). We do not and cannot guarantee more.
3350           */
3351          public final V setValue(V value) {
3352              if (value == null) throw new NullPointerException();
# Line 2854 | Line 3357 | public class ConcurrentHashMapV8<K, V>
3357          }
3358      }
3359  
2857    /**
2858     * Internal version of entry, that doesn't write though changes
2859     */
2860    static final class SnapshotEntry<K,V> extends MapEntry<K,V>
2861        implements Map.Entry<K, V> {
2862        SnapshotEntry(K key, V val) { super(key, val); }
2863        public final V setValue(V value) { // only locally update
2864            if (value == null) throw new NullPointerException();
2865            V v = val;
2866            val = value;
2867            return v;
2868        }
2869    }
2870
3360      /* ----------------Views -------------- */
3361  
3362      /**
3363 <     * Base class for views. This is done mainly to allow adding
2875 <     * customized parallel traversals (not yet implemented.)
3363 >     * Base class for views.
3364       */
3365 <    static abstract class MapView<K, V> {
3365 >    static abstract class CHMView<K, V> {
3366          final ConcurrentHashMapV8<K, V> map;
3367 <        MapView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
3367 >        CHMView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
3368          public final int size()                 { return map.size(); }
3369          public final boolean isEmpty()          { return map.isEmpty(); }
3370          public final void clear()               { map.clear(); }
3371  
3372          // implementations below rely on concrete classes supplying these
3373 <        abstract Iterator<?> iter();
3373 >        abstract public Iterator<?> iterator();
3374          abstract public boolean contains(Object o);
3375          abstract public boolean remove(Object o);
3376  
3377          private static final String oomeMsg = "Required array size too large";
3378  
3379          public final Object[] toArray() {
3380 <            long sz = map.longSize();
3380 >            long sz = map.mappingCount();
3381              if (sz > (long)(MAX_ARRAY_SIZE))
3382                  throw new OutOfMemoryError(oomeMsg);
3383              int n = (int)sz;
3384              Object[] r = new Object[n];
3385              int i = 0;
3386 <            Iterator<?> it = iter();
3386 >            Iterator<?> it = iterator();
3387              while (it.hasNext()) {
3388                  if (i == n) {
3389                      if (n >= MAX_ARRAY_SIZE)
# Line 2911 | Line 3399 | public class ConcurrentHashMapV8<K, V>
3399              return (i == n) ? r : Arrays.copyOf(r, i);
3400          }
3401  
3402 <        @SuppressWarnings("unchecked")
3403 <        public final <T> T[] toArray(T[] a) {
2916 <            long sz = map.longSize();
3402 >        @SuppressWarnings("unchecked") public final <T> T[] toArray(T[] a) {
3403 >            long sz = map.mappingCount();
3404              if (sz > (long)(MAX_ARRAY_SIZE))
3405                  throw new OutOfMemoryError(oomeMsg);
3406              int m = (int)sz;
# Line 2922 | Line 3409 | public class ConcurrentHashMapV8<K, V>
3409                  .newInstance(a.getClass().getComponentType(), m);
3410              int n = r.length;
3411              int i = 0;
3412 <            Iterator<?> it = iter();
3412 >            Iterator<?> it = iterator();
3413              while (it.hasNext()) {
3414                  if (i == n) {
3415                      if (n >= MAX_ARRAY_SIZE)
# Line 2944 | Line 3431 | public class ConcurrentHashMapV8<K, V>
3431  
3432          public final int hashCode() {
3433              int h = 0;
3434 <            for (Iterator<?> it = iter(); it.hasNext();)
3434 >            for (Iterator<?> it = iterator(); it.hasNext();)
3435                  h += it.next().hashCode();
3436              return h;
3437          }
# Line 2952 | Line 3439 | public class ConcurrentHashMapV8<K, V>
3439          public final String toString() {
3440              StringBuilder sb = new StringBuilder();
3441              sb.append('[');
3442 <            Iterator<?> it = iter();
3442 >            Iterator<?> it = iterator();
3443              if (it.hasNext()) {
3444                  for (;;) {
3445                      Object e = it.next();
# Line 2978 | Line 3465 | public class ConcurrentHashMapV8<K, V>
3465  
3466          public final boolean removeAll(Collection<?> c) {
3467              boolean modified = false;
3468 <            for (Iterator<?> it = iter(); it.hasNext();) {
3468 >            for (Iterator<?> it = iterator(); it.hasNext();) {
3469                  if (c.contains(it.next())) {
3470                      it.remove();
3471                      modified = true;
# Line 2989 | Line 3476 | public class ConcurrentHashMapV8<K, V>
3476  
3477          public final boolean retainAll(Collection<?> c) {
3478              boolean modified = false;
3479 <            for (Iterator<?> it = iter(); it.hasNext();) {
3479 >            for (Iterator<?> it = iterator(); it.hasNext();) {
3480                  if (!c.contains(it.next())) {
3481                      it.remove();
3482                      modified = true;
# Line 3000 | Line 3487 | public class ConcurrentHashMapV8<K, V>
3487  
3488      }
3489  
3490 <    static final class KeySet<K,V> extends MapView<K,V> implements Set<K> {
3004 <        KeySet(ConcurrentHashMapV8<K, V> map)   { super(map); }
3005 <        public final boolean contains(Object o) { return map.containsKey(o); }
3006 <        public final boolean remove(Object o)   { return map.remove(o) != null; }
3007 <
3008 <        public final Iterator<K> iterator() {
3009 <            return new KeyIterator<K,V>(map);
3010 <        }
3011 <        final Iterator<?> iter() {
3012 <            return new KeyIterator<K,V>(map);
3013 <        }
3014 <        public final boolean add(K e) {
3015 <            throw new UnsupportedOperationException();
3016 <        }
3017 <        public final boolean addAll(Collection<? extends K> c) {
3018 <            throw new UnsupportedOperationException();
3019 <        }
3020 <        public boolean equals(Object o) {
3021 <            Set<?> c;
3022 <            return ((o instanceof Set) &&
3023 <                    ((c = (Set<?>)o) == this ||
3024 <                     (containsAll(c) && c.containsAll(this))));
3025 <        }
3026 <    }
3027 <
3028 <    static final class Values<K,V> extends MapView<K,V>
3490 >    static final class Values<K,V> extends CHMView<K,V>
3491          implements Collection<V> {
3492          Values(ConcurrentHashMapV8<K, V> map)   { super(map); }
3493          public final boolean contains(Object o) { return map.containsValue(o); }
3032
3494          public final boolean remove(Object o) {
3495              if (o != null) {
3496                  Iterator<V> it = new ValueIterator<K,V>(map);
# Line 3045 | Line 3506 | public class ConcurrentHashMapV8<K, V>
3506          public final Iterator<V> iterator() {
3507              return new ValueIterator<K,V>(map);
3508          }
3048        final Iterator<?> iter() {
3049            return new ValueIterator<K,V>(map);
3050        }
3509          public final boolean add(V e) {
3510              throw new UnsupportedOperationException();
3511          }
3512          public final boolean addAll(Collection<? extends V> c) {
3513              throw new UnsupportedOperationException();
3514          }
3515 +
3516      }
3517  
3518 <    static final class EntrySet<K,V> extends MapView<K,V>
3518 >    static final class EntrySet<K,V> extends CHMView<K,V>
3519          implements Set<Map.Entry<K,V>> {
3520          EntrySet(ConcurrentHashMapV8<K, V> map) { super(map); }
3062
3521          public final boolean contains(Object o) {
3522              Object k, v, r; Map.Entry<?,?> e;
3523              return ((o instanceof Map.Entry) &&
# Line 3068 | Line 3526 | public class ConcurrentHashMapV8<K, V>
3526                      (v = e.getValue()) != null &&
3527                      (v == r || v.equals(r)));
3528          }
3071
3529          public final boolean remove(Object o) {
3530              Object k, v; Map.Entry<?,?> e;
3531              return ((o instanceof Map.Entry) &&
# Line 3076 | Line 3533 | public class ConcurrentHashMapV8<K, V>
3533                      (v = e.getValue()) != null &&
3534                      map.remove(k, v));
3535          }
3079
3536          public final Iterator<Map.Entry<K,V>> iterator() {
3537              return new EntryIterator<K,V>(map);
3538          }
3083        final Iterator<?> iter() {
3084            return new SnapshotEntryIterator<K,V>(map);
3085        }
3539          public final boolean add(Entry<K,V> e) {
3540              throw new UnsupportedOperationException();
3541          }
# Line 3118 | Line 3571 | public class ConcurrentHashMapV8<K, V>
3571       * for each key-value mapping, followed by a null pair.
3572       * The key-value mappings are emitted in no particular order.
3573       */
3574 <    @SuppressWarnings("unchecked")
3575 <    private void writeObject(java.io.ObjectOutputStream s)
3123 <            throws java.io.IOException {
3574 >    @SuppressWarnings("unchecked") private void writeObject(java.io.ObjectOutputStream s)
3575 >        throws java.io.IOException {
3576          if (segments == null) { // for serialization compatibility
3577              segments = (Segment<K,V>[])
3578                  new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
# Line 3128 | Line 3580 | public class ConcurrentHashMapV8<K, V>
3580                  segments[i] = new Segment<K,V>(LOAD_FACTOR);
3581          }
3582          s.defaultWriteObject();
3583 <        InternalIterator it = new InternalIterator(table);
3584 <        while (it.next != null) {
3583 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3584 >        Object v;
3585 >        while ((v = it.advance()) != null) {
3586              s.writeObject(it.nextKey);
3587 <            s.writeObject(it.nextVal);
3135 <            it.advance();
3587 >            s.writeObject(v);
3588          }
3589          s.writeObject(null);
3590          s.writeObject(null);
# Line 3143 | Line 3595 | public class ConcurrentHashMapV8<K, V>
3595       * Reconstitutes the instance from a stream (that is, deserializes it).
3596       * @param s the stream
3597       */
3598 <    @SuppressWarnings("unchecked")
3599 <    private void readObject(java.io.ObjectInputStream s)
3148 <            throws java.io.IOException, ClassNotFoundException {
3598 >    @SuppressWarnings("unchecked") private void readObject(java.io.ObjectInputStream s)
3599 >        throws java.io.IOException, ClassNotFoundException {
3600          s.defaultReadObject();
3601          this.segments = null; // unneeded
3602          // initialize transient final field
# Line 3219 | Line 3670 | public class ConcurrentHashMapV8<K, V>
3670                      p = p.next;
3671                  }
3672              }
3673 +        }
3674 +    }
3675 +
3676 +
3677 +    // -------------------------------------------------------
3678 +
3679 +    // Sams
3680 +    /** Interface describing a void action of one argument */
3681 +    public interface Action<A> { void apply(A a); }
3682 +    /** Interface describing a void action of two arguments */
3683 +    public interface BiAction<A,B> { void apply(A a, B b); }
3684 +    /** Interface describing a function of one argument */
3685 +    public interface Fun<A,T> { T apply(A a); }
3686 +    /** Interface describing a function of two arguments */
3687 +    public interface BiFun<A,B,T> { T apply(A a, B b); }
3688 +    /** Interface describing a function of no arguments */
3689 +    public interface Generator<T> { T apply(); }
3690 +    /** Interface describing a function mapping its argument to a double */
3691 +    public interface ObjectToDouble<A> { double apply(A a); }
3692 +    /** Interface describing a function mapping its argument to a long */
3693 +    public interface ObjectToLong<A> { long apply(A a); }
3694 +    /** Interface describing a function mapping its argument to an int */
3695 +    public interface ObjectToInt<A> {int apply(A a); }
3696 +    /** Interface describing a function mapping two arguments to a double */
3697 +    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
3698 +    /** Interface describing a function mapping two arguments to a long */
3699 +    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
3700 +    /** Interface describing a function mapping two arguments to an int */
3701 +    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
3702 +    /** Interface describing a function mapping a double to a double */
3703 +    public interface DoubleToDouble { double apply(double a); }
3704 +    /** Interface describing a function mapping a long to a long */
3705 +    public interface LongToLong { long apply(long a); }
3706 +    /** Interface describing a function mapping an int to an int */
3707 +    public interface IntToInt { int apply(int a); }
3708 +    /** Interface describing a function mapping two doubles to a double */
3709 +    public interface DoubleByDoubleToDouble { double apply(double a, double b); }
3710 +    /** Interface describing a function mapping two longs to a long */
3711 +    public interface LongByLongToLong { long apply(long a, long b); }
3712 +    /** Interface describing a function mapping two ints to an int */
3713 +    public interface IntByIntToInt { int apply(int a, int b); }
3714 +
3715 +
3716 +    // -------------------------------------------------------
3717 +
3718 +    /**
3719 +     * Performs the given action for each (key, value).
3720 +     *
3721 +     * @param action the action
3722 +     */
3723 +    public void forEach(BiAction<K,V> action) {
3724 +        ForkJoinTasks.forEach
3725 +            (this, action).invoke();
3726 +    }
3727 +
3728 +    /**
3729 +     * Performs the given action for each non-null transformation
3730 +     * of each (key, value).
3731 +     *
3732 +     * @param transformer a function returning the transformation
3733 +     * for an element, or null of there is no transformation (in
3734 +     * which case the action is not applied).
3735 +     * @param action the action
3736 +     */
3737 +    public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
3738 +                            Action<U> action) {
3739 +        ForkJoinTasks.forEach
3740 +            (this, transformer, action).invoke();
3741 +    }
3742 +
3743 +    /**
3744 +     * Returns a non-null result from applying the given search
3745 +     * function on each (key, value), or null if none.  Upon
3746 +     * success, further element processing is suppressed and the
3747 +     * results of any other parallel invocations of the search
3748 +     * function are ignored.
3749 +     *
3750 +     * @param searchFunction a function returning a non-null
3751 +     * result on success, else null
3752 +     * @return a non-null result from applying the given search
3753 +     * function on each (key, value), or null if none
3754 +     */
3755 +    public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) {
3756 +        return ForkJoinTasks.search
3757 +            (this, searchFunction).invoke();
3758 +    }
3759 +
3760 +    /**
3761 +     * Returns the result of accumulating the given transformation
3762 +     * of all (key, value) pairs using the given reducer to
3763 +     * combine values, or null if none.
3764 +     *
3765 +     * @param transformer a function returning the transformation
3766 +     * for an element, or null of there is no transformation (in
3767 +     * which case it is not combined).
3768 +     * @param reducer a commutative associative combining function
3769 +     * @return the result of accumulating the given transformation
3770 +     * of all (key, value) pairs
3771 +     */
3772 +    public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
3773 +                        BiFun<? super U, ? super U, ? extends U> reducer) {
3774 +        return ForkJoinTasks.reduce
3775 +            (this, transformer, reducer).invoke();
3776 +    }
3777 +
3778 +    /**
3779 +     * Returns the result of accumulating the given transformation
3780 +     * of all (key, value) pairs using the given reducer to
3781 +     * combine values, and the given basis as an identity value.
3782 +     *
3783 +     * @param transformer a function returning the transformation
3784 +     * for an element
3785 +     * @param basis the identity (initial default value) for the reduction
3786 +     * @param reducer a commutative associative combining function
3787 +     * @return the result of accumulating the given transformation
3788 +     * of all (key, value) pairs
3789 +     */
3790 +    public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
3791 +                                 double basis,
3792 +                                 DoubleByDoubleToDouble reducer) {
3793 +        return ForkJoinTasks.reduceToDouble
3794 +            (this, transformer, basis, reducer).invoke();
3795 +    }
3796 +
3797 +    /**
3798 +     * Returns the result of accumulating the given transformation
3799 +     * of all (key, value) pairs using the given reducer to
3800 +     * combine values, and the given basis as an identity value.
3801 +     *
3802 +     * @param transformer a function returning the transformation
3803 +     * for an element
3804 +     * @param basis the identity (initial default value) for the reduction
3805 +     * @param reducer a commutative associative combining function
3806 +     * @return the result of accumulating the given transformation
3807 +     * of all (key, value) pairs
3808 +     */
3809 +    public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
3810 +                             long basis,
3811 +                             LongByLongToLong reducer) {
3812 +        return ForkJoinTasks.reduceToLong
3813 +            (this, transformer, basis, reducer).invoke();
3814 +    }
3815  
3816 +    /**
3817 +     * Returns the result of accumulating the given transformation
3818 +     * of all (key, value) pairs using the given reducer to
3819 +     * combine values, and the given basis as an identity value.
3820 +     *
3821 +     * @param transformer a function returning the transformation
3822 +     * for an element
3823 +     * @param basis the identity (initial default value) for the reduction
3824 +     * @param reducer a commutative associative combining function
3825 +     * @return the result of accumulating the given transformation
3826 +     * of all (key, value) pairs
3827 +     */
3828 +    public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
3829 +                           int basis,
3830 +                           IntByIntToInt reducer) {
3831 +        return ForkJoinTasks.reduceToInt
3832 +            (this, transformer, basis, reducer).invoke();
3833 +    }
3834 +
3835 +    /**
3836 +     * Performs the given action for each key.
3837 +     *
3838 +     * @param action the action
3839 +     */
3840 +    public void forEachKey(Action<K> action) {
3841 +        ForkJoinTasks.forEachKey
3842 +            (this, action).invoke();
3843 +    }
3844 +
3845 +    /**
3846 +     * Performs the given action for each non-null transformation
3847 +     * of each key.
3848 +     *
3849 +     * @param transformer a function returning the transformation
3850 +     * for an element, or null of there is no transformation (in
3851 +     * which case the action is not applied).
3852 +     * @param action the action
3853 +     */
3854 +    public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
3855 +                               Action<U> action) {
3856 +        ForkJoinTasks.forEachKey
3857 +            (this, transformer, action).invoke();
3858 +    }
3859 +
3860 +    /**
3861 +     * Returns a non-null result from applying the given search
3862 +     * function on each key, or null if none. Upon success,
3863 +     * further element processing is suppressed and the results of
3864 +     * any other parallel invocations of the search function are
3865 +     * ignored.
3866 +     *
3867 +     * @param searchFunction a function returning a non-null
3868 +     * result on success, else null
3869 +     * @return a non-null result from applying the given search
3870 +     * function on each key, or null if none
3871 +     */
3872 +    public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
3873 +        return ForkJoinTasks.searchKeys
3874 +            (this, searchFunction).invoke();
3875 +    }
3876 +
3877 +    /**
3878 +     * Returns the result of accumulating all keys using the given
3879 +     * reducer to combine values, or null if none.
3880 +     *
3881 +     * @param reducer a commutative associative combining function
3882 +     * @return the result of accumulating all keys using the given
3883 +     * reducer to combine values, or null if none
3884 +     */
3885 +    public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
3886 +        return ForkJoinTasks.reduceKeys
3887 +            (this, reducer).invoke();
3888 +    }
3889 +
3890 +    /**
3891 +     * Returns the result of accumulating the given transformation
3892 +     * of all keys using the given reducer to combine values, or
3893 +     * null if none.
3894 +     *
3895 +     * @param transformer a function returning the transformation
3896 +     * for an element, or null of there is no transformation (in
3897 +     * which case it is not combined).
3898 +     * @param reducer a commutative associative combining function
3899 +     * @return the result of accumulating the given transformation
3900 +     * of all keys
3901 +     */
3902 +    public <U> U reduceKeys(Fun<? super K, ? extends U> transformer,
3903 +                            BiFun<? super U, ? super U, ? extends U> reducer) {
3904 +        return ForkJoinTasks.reduceKeys
3905 +            (this, transformer, reducer).invoke();
3906 +    }
3907 +
3908 +    /**
3909 +     * Returns the result of accumulating the given transformation
3910 +     * of all keys using the given reducer to combine values, and
3911 +     * the given basis as an identity value.
3912 +     *
3913 +     * @param transformer a function returning the transformation
3914 +     * for an element
3915 +     * @param basis the identity (initial default value) for the reduction
3916 +     * @param reducer a commutative associative combining function
3917 +     * @return  the result of accumulating the given transformation
3918 +     * of all keys
3919 +     */
3920 +    public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
3921 +                                     double basis,
3922 +                                     DoubleByDoubleToDouble reducer) {
3923 +        return ForkJoinTasks.reduceKeysToDouble
3924 +            (this, transformer, basis, reducer).invoke();
3925 +    }
3926 +
3927 +    /**
3928 +     * Returns the result of accumulating the given transformation
3929 +     * of all keys using the given reducer to combine values, and
3930 +     * the given basis as an identity value.
3931 +     *
3932 +     * @param transformer a function returning the transformation
3933 +     * for an element
3934 +     * @param basis the identity (initial default value) for the reduction
3935 +     * @param reducer a commutative associative combining function
3936 +     * @return the result of accumulating the given transformation
3937 +     * of all keys
3938 +     */
3939 +    public long reduceKeysToLong(ObjectToLong<? super K> transformer,
3940 +                                 long basis,
3941 +                                 LongByLongToLong reducer) {
3942 +        return ForkJoinTasks.reduceKeysToLong
3943 +            (this, transformer, basis, reducer).invoke();
3944 +    }
3945 +
3946 +    /**
3947 +     * Returns the result of accumulating the given transformation
3948 +     * of all keys using the given reducer to combine values, and
3949 +     * the given basis as an identity value.
3950 +     *
3951 +     * @param transformer a function returning the transformation
3952 +     * for an element
3953 +     * @param basis the identity (initial default value) for the reduction
3954 +     * @param reducer a commutative associative combining function
3955 +     * @return the result of accumulating the given transformation
3956 +     * of all keys
3957 +     */
3958 +    public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3959 +                               int basis,
3960 +                               IntByIntToInt reducer) {
3961 +        return ForkJoinTasks.reduceKeysToInt
3962 +            (this, transformer, basis, reducer).invoke();
3963 +    }
3964 +
3965 +    /**
3966 +     * Performs the given action for each value.
3967 +     *
3968 +     * @param action the action
3969 +     */
3970 +    public void forEachValue(Action<V> action) {
3971 +        ForkJoinTasks.forEachValue
3972 +            (this, action).invoke();
3973 +    }
3974 +
3975 +    /**
3976 +     * Performs the given action for each non-null transformation
3977 +     * of each value.
3978 +     *
3979 +     * @param transformer a function returning the transformation
3980 +     * for an element, or null of there is no transformation (in
3981 +     * which case the action is not applied).
3982 +     */
3983 +    public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
3984 +                                 Action<U> action) {
3985 +        ForkJoinTasks.forEachValue
3986 +            (this, transformer, action).invoke();
3987 +    }
3988 +
3989 +    /**
3990 +     * Returns a non-null result from applying the given search
3991 +     * function on each value, or null if none.  Upon success,
3992 +     * further element processing is suppressed and the results of
3993 +     * any other parallel invocations of the search function are
3994 +     * ignored.
3995 +     *
3996 +     * @param searchFunction a function returning a non-null
3997 +     * result on success, else null
3998 +     * @return a non-null result from applying the given search
3999 +     * function on each value, or null if none
4000 +     *
4001 +     */
4002 +    public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
4003 +        return ForkJoinTasks.searchValues
4004 +            (this, searchFunction).invoke();
4005 +    }
4006 +
4007 +    /**
4008 +     * Returns the result of accumulating all values using the
4009 +     * given reducer to combine values, or null if none.
4010 +     *
4011 +     * @param reducer a commutative associative combining function
4012 +     * @return  the result of accumulating all values
4013 +     */
4014 +    public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
4015 +        return ForkJoinTasks.reduceValues
4016 +            (this, reducer).invoke();
4017 +    }
4018 +
4019 +    /**
4020 +     * Returns the result of accumulating the given transformation
4021 +     * of all values using the given reducer to combine values, or
4022 +     * null if none.
4023 +     *
4024 +     * @param transformer a function returning the transformation
4025 +     * for an element, or null of there is no transformation (in
4026 +     * which case it is not combined).
4027 +     * @param reducer a commutative associative combining function
4028 +     * @return the result of accumulating the given transformation
4029 +     * of all values
4030 +     */
4031 +    public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
4032 +                              BiFun<? super U, ? super U, ? extends U> reducer) {
4033 +        return ForkJoinTasks.reduceValues
4034 +            (this, transformer, reducer).invoke();
4035 +    }
4036 +
4037 +    /**
4038 +     * Returns the result of accumulating the given transformation
4039 +     * of all values using the given reducer to combine values,
4040 +     * and the given basis as an identity value.
4041 +     *
4042 +     * @param transformer a function returning the transformation
4043 +     * for an element
4044 +     * @param basis the identity (initial default value) for the reduction
4045 +     * @param reducer a commutative associative combining function
4046 +     * @return the result of accumulating the given transformation
4047 +     * of all values
4048 +     */
4049 +    public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
4050 +                                       double basis,
4051 +                                       DoubleByDoubleToDouble reducer) {
4052 +        return ForkJoinTasks.reduceValuesToDouble
4053 +            (this, transformer, basis, reducer).invoke();
4054 +    }
4055 +
4056 +    /**
4057 +     * Returns the result of accumulating the given transformation
4058 +     * of all values using the given reducer to combine values,
4059 +     * and the given basis as an identity value.
4060 +     *
4061 +     * @param transformer a function returning the transformation
4062 +     * for an element
4063 +     * @param basis the identity (initial default value) for the reduction
4064 +     * @param reducer a commutative associative combining function
4065 +     * @return the result of accumulating the given transformation
4066 +     * of all values
4067 +     */
4068 +    public long reduceValuesToLong(ObjectToLong<? super V> transformer,
4069 +                                   long basis,
4070 +                                   LongByLongToLong reducer) {
4071 +        return ForkJoinTasks.reduceValuesToLong
4072 +            (this, transformer, basis, reducer).invoke();
4073 +    }
4074 +
4075 +    /**
4076 +     * Returns the result of accumulating the given transformation
4077 +     * of all values using the given reducer to combine values,
4078 +     * and the given basis as an identity value.
4079 +     *
4080 +     * @param transformer a function returning the transformation
4081 +     * for an element
4082 +     * @param basis the identity (initial default value) for the reduction
4083 +     * @param reducer a commutative associative combining function
4084 +     * @return the result of accumulating the given transformation
4085 +     * of all values
4086 +     */
4087 +    public int reduceValuesToInt(ObjectToInt<? super V> transformer,
4088 +                                 int basis,
4089 +                                 IntByIntToInt reducer) {
4090 +        return ForkJoinTasks.reduceValuesToInt
4091 +            (this, transformer, basis, reducer).invoke();
4092 +    }
4093 +
4094 +    /**
4095 +     * Performs the given action for each entry.
4096 +     *
4097 +     * @param action the action
4098 +     */
4099 +    public void forEachEntry(Action<Map.Entry<K,V>> action) {
4100 +        ForkJoinTasks.forEachEntry
4101 +            (this, action).invoke();
4102 +    }
4103 +
4104 +    /**
4105 +     * Performs the given action for each non-null transformation
4106 +     * of each entry.
4107 +     *
4108 +     * @param transformer a function returning the transformation
4109 +     * for an element, or null of there is no transformation (in
4110 +     * which case the action is not applied).
4111 +     * @param action the action
4112 +     */
4113 +    public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
4114 +                                 Action<U> action) {
4115 +        ForkJoinTasks.forEachEntry
4116 +            (this, transformer, action).invoke();
4117 +    }
4118 +
4119 +    /**
4120 +     * Returns a non-null result from applying the given search
4121 +     * function on each entry, or null if none.  Upon success,
4122 +     * further element processing is suppressed and the results of
4123 +     * any other parallel invocations of the search function are
4124 +     * ignored.
4125 +     *
4126 +     * @param searchFunction a function returning a non-null
4127 +     * result on success, else null
4128 +     * @return a non-null result from applying the given search
4129 +     * function on each entry, or null if none
4130 +     */
4131 +    public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4132 +        return ForkJoinTasks.searchEntries
4133 +            (this, searchFunction).invoke();
4134 +    }
4135 +
4136 +    /**
4137 +     * Returns the result of accumulating all entries using the
4138 +     * given reducer to combine values, or null if none.
4139 +     *
4140 +     * @param reducer a commutative associative combining function
4141 +     * @return the result of accumulating all entries
4142 +     */
4143 +    public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4144 +        return ForkJoinTasks.reduceEntries
4145 +            (this, reducer).invoke();
4146 +    }
4147 +
4148 +    /**
4149 +     * Returns the result of accumulating the given transformation
4150 +     * of all entries using the given reducer to combine values,
4151 +     * or null if none.
4152 +     *
4153 +     * @param transformer a function returning the transformation
4154 +     * for an element, or null of there is no transformation (in
4155 +     * which case it is not combined).
4156 +     * @param reducer a commutative associative combining function
4157 +     * @return the result of accumulating the given transformation
4158 +     * of all entries
4159 +     */
4160 +    public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer,
4161 +                               BiFun<? super U, ? super U, ? extends U> reducer) {
4162 +        return ForkJoinTasks.reduceEntries
4163 +            (this, transformer, reducer).invoke();
4164 +    }
4165 +
4166 +    /**
4167 +     * Returns the result of accumulating the given transformation
4168 +     * of all entries using the given reducer to combine values,
4169 +     * and the given basis as an identity value.
4170 +     *
4171 +     * @param transformer a function returning the transformation
4172 +     * for an element
4173 +     * @param basis the identity (initial default value) for the reduction
4174 +     * @param reducer a commutative associative combining function
4175 +     * @return the result of accumulating the given transformation
4176 +     * of all entries
4177 +     */
4178 +    public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
4179 +                                        double basis,
4180 +                                        DoubleByDoubleToDouble reducer) {
4181 +        return ForkJoinTasks.reduceEntriesToDouble
4182 +            (this, transformer, basis, reducer).invoke();
4183 +    }
4184 +
4185 +    /**
4186 +     * Returns the result of accumulating the given transformation
4187 +     * of all entries using the given reducer to combine values,
4188 +     * and the given basis as an identity value.
4189 +     *
4190 +     * @param transformer a function returning the transformation
4191 +     * for an element
4192 +     * @param basis the identity (initial default value) for the reduction
4193 +     * @param reducer a commutative associative combining function
4194 +     * @return  the result of accumulating the given transformation
4195 +     * of all entries
4196 +     */
4197 +    public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4198 +                                    long basis,
4199 +                                    LongByLongToLong reducer) {
4200 +        return ForkJoinTasks.reduceEntriesToLong
4201 +            (this, transformer, basis, reducer).invoke();
4202 +    }
4203 +
4204 +    /**
4205 +     * Returns the result of accumulating the given transformation
4206 +     * of all entries using the given reducer to combine values,
4207 +     * and the given basis as an identity value.
4208 +     *
4209 +     * @param transformer a function returning the transformation
4210 +     * for an element
4211 +     * @param basis the identity (initial default value) for the reduction
4212 +     * @param reducer a commutative associative combining function
4213 +     * @return the result of accumulating the given transformation
4214 +     * of all entries
4215 +     */
4216 +    public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer,
4217 +                                  int basis,
4218 +                                  IntByIntToInt reducer) {
4219 +        return ForkJoinTasks.reduceEntriesToInt
4220 +            (this, transformer, basis, reducer).invoke();
4221 +    }
4222 +
4223 +    // ---------------------------------------------------------------------
4224 +
4225 +    /**
4226 +     * Predefined tasks for performing bulk parallel operations on
4227 +     * ConcurrentHashMapV8s. These tasks follow the forms and rules used
4228 +     * for bulk operations. Each method has the same name, but returns
4229 +     * a task rather than invoking it. These methods may be useful in
4230 +     * custom applications such as submitting a task without waiting
4231 +     * for completion, using a custom pool, or combining with other
4232 +     * tasks.
4233 +     */
4234 +    public static class ForkJoinTasks {
4235 +        private ForkJoinTasks() {}
4236 +
4237 +        /**
4238 +         * Returns a task that when invoked, performs the given
4239 +         * action for each (key, value)
4240 +         *
4241 +         * @param map the map
4242 +         * @param action the action
4243 +         * @return the task
4244 +         */
4245 +        public static <K,V> ForkJoinTask<Void> forEach
4246 +            (ConcurrentHashMapV8<K,V> map,
4247 +             BiAction<K,V> action) {
4248 +            if (action == null) throw new NullPointerException();
4249 +            return new ForEachMappingTask<K,V>(map, null, -1, null, action);
4250 +        }
4251 +
4252 +        /**
4253 +         * Returns a task that when invoked, performs the given
4254 +         * action for each non-null transformation of each (key, value)
4255 +         *
4256 +         * @param map the map
4257 +         * @param transformer a function returning the transformation
4258 +         * for an element, or null if there is no transformation (in
4259 +         * which case the action is not applied)
4260 +         * @param action the action
4261 +         * @return the task
4262 +         */
4263 +        public static <K,V,U> ForkJoinTask<Void> forEach
4264 +            (ConcurrentHashMapV8<K,V> map,
4265 +             BiFun<? super K, ? super V, ? extends U> transformer,
4266 +             Action<U> action) {
4267 +            if (transformer == null || action == null)
4268 +                throw new NullPointerException();
4269 +            return new ForEachTransformedMappingTask<K,V,U>
4270 +                (map, null, -1, null, transformer, action);
4271 +        }
4272 +
4273 +        /**
4274 +         * Returns a task that when invoked, returns a non-null result
4275 +         * from applying the given search function on each (key,
4276 +         * value), or null if none. Upon success, further element
4277 +         * processing is suppressed and the results of any other
4278 +         * parallel invocations of the search function are ignored.
4279 +         *
4280 +         * @param map the map
4281 +         * @param searchFunction a function returning a non-null
4282 +         * result on success, else null
4283 +         * @return the task
4284 +         */
4285 +        public static <K,V,U> ForkJoinTask<U> search
4286 +            (ConcurrentHashMapV8<K,V> map,
4287 +             BiFun<? super K, ? super V, ? extends U> searchFunction) {
4288 +            if (searchFunction == null) throw new NullPointerException();
4289 +            return new SearchMappingsTask<K,V,U>
4290 +                (map, null, -1, null, searchFunction,
4291 +                 new AtomicReference<U>());
4292 +        }
4293 +
4294 +        /**
4295 +         * Returns a task that when invoked, returns the result of
4296 +         * accumulating the given transformation of all (key, value) pairs
4297 +         * using the given reducer to combine values, or null if none.
4298 +         *
4299 +         * @param map the map
4300 +         * @param transformer a function returning the transformation
4301 +         * for an element, or null if there is no transformation (in
4302 +         * which case it is not combined).
4303 +         * @param reducer a commutative associative combining function
4304 +         * @return the task
4305 +         */
4306 +        public static <K,V,U> ForkJoinTask<U> reduce
4307 +            (ConcurrentHashMapV8<K,V> map,
4308 +             BiFun<? super K, ? super V, ? extends U> transformer,
4309 +             BiFun<? super U, ? super U, ? extends U> reducer) {
4310 +            if (transformer == null || reducer == null)
4311 +                throw new NullPointerException();
4312 +            return new MapReduceMappingsTask<K,V,U>
4313 +                (map, null, -1, null, transformer, reducer);
4314 +        }
4315 +
4316 +        /**
4317 +         * Returns a task that when invoked, returns the result of
4318 +         * accumulating the given transformation of all (key, value) pairs
4319 +         * using the given reducer to combine values, and the given
4320 +         * basis as an identity value.
4321 +         *
4322 +         * @param map the map
4323 +         * @param transformer a function returning the transformation
4324 +         * for an element
4325 +         * @param basis the identity (initial default value) for the reduction
4326 +         * @param reducer a commutative associative combining function
4327 +         * @return the task
4328 +         */
4329 +        public static <K,V> ForkJoinTask<Double> reduceToDouble
4330 +            (ConcurrentHashMapV8<K,V> map,
4331 +             ObjectByObjectToDouble<? super K, ? super V> transformer,
4332 +             double basis,
4333 +             DoubleByDoubleToDouble reducer) {
4334 +            if (transformer == null || reducer == null)
4335 +                throw new NullPointerException();
4336 +            return new MapReduceMappingsToDoubleTask<K,V>
4337 +                (map, null, -1, null, transformer, basis, reducer);
4338 +        }
4339 +
4340 +        /**
4341 +         * Returns a task that when invoked, returns the result of
4342 +         * accumulating the given transformation of all (key, value) pairs
4343 +         * using the given reducer to combine values, and the given
4344 +         * basis as an identity value.
4345 +         *
4346 +         * @param map the map
4347 +         * @param transformer a function returning the transformation
4348 +         * for an element
4349 +         * @param basis the identity (initial default value) for the reduction
4350 +         * @param reducer a commutative associative combining function
4351 +         * @return the task
4352 +         */
4353 +        public static <K,V> ForkJoinTask<Long> reduceToLong
4354 +            (ConcurrentHashMapV8<K,V> map,
4355 +             ObjectByObjectToLong<? super K, ? super V> transformer,
4356 +             long basis,
4357 +             LongByLongToLong reducer) {
4358 +            if (transformer == null || reducer == null)
4359 +                throw new NullPointerException();
4360 +            return new MapReduceMappingsToLongTask<K,V>
4361 +                (map, null, -1, null, transformer, basis, reducer);
4362 +        }
4363 +
4364 +        /**
4365 +         * Returns a task that when invoked, returns the result of
4366 +         * accumulating the given transformation of all (key, value) pairs
4367 +         * using the given reducer to combine values, and the given
4368 +         * basis as an identity value.
4369 +         *
4370 +         * @param transformer a function returning the transformation
4371 +         * for an element
4372 +         * @param basis the identity (initial default value) for the reduction
4373 +         * @param reducer a commutative associative combining function
4374 +         * @return the task
4375 +         */
4376 +        public static <K,V> ForkJoinTask<Integer> reduceToInt
4377 +            (ConcurrentHashMapV8<K,V> map,
4378 +             ObjectByObjectToInt<? super K, ? super V> transformer,
4379 +             int basis,
4380 +             IntByIntToInt reducer) {
4381 +            if (transformer == null || reducer == null)
4382 +                throw new NullPointerException();
4383 +            return new MapReduceMappingsToIntTask<K,V>
4384 +                (map, null, -1, null, transformer, basis, reducer);
4385 +        }
4386 +
4387 +        /**
4388 +         * Returns a task that when invoked, performs the given action
4389 +         * for each key.
4390 +         *
4391 +         * @param map the map
4392 +         * @param action the action
4393 +         * @return the task
4394 +         */
4395 +        public static <K,V> ForkJoinTask<Void> forEachKey
4396 +            (ConcurrentHashMapV8<K,V> map,
4397 +             Action<K> action) {
4398 +            if (action == null) throw new NullPointerException();
4399 +            return new ForEachKeyTask<K,V>(map, null, -1, null, action);
4400 +        }
4401 +
4402 +        /**
4403 +         * Returns a task that when invoked, performs the given action
4404 +         * for each non-null transformation of each key.
4405 +         *
4406 +         * @param map the map
4407 +         * @param transformer a function returning the transformation
4408 +         * for an element, or null if there is no transformation (in
4409 +         * which case the action is not applied)
4410 +         * @param action the action
4411 +         * @return the task
4412 +         */
4413 +        public static <K,V,U> ForkJoinTask<Void> forEachKey
4414 +            (ConcurrentHashMapV8<K,V> map,
4415 +             Fun<? super K, ? extends U> transformer,
4416 +             Action<U> action) {
4417 +            if (transformer == null || action == null)
4418 +                throw new NullPointerException();
4419 +            return new ForEachTransformedKeyTask<K,V,U>
4420 +                (map, null, -1, null, transformer, action);
4421 +        }
4422 +
4423 +        /**
4424 +         * Returns a task that when invoked, returns a non-null result
4425 +         * from applying the given search function on each key, or
4426 +         * null if none.  Upon success, further element processing is
4427 +         * suppressed and the results of any other parallel
4428 +         * invocations of the search function are ignored.
4429 +         *
4430 +         * @param map the map
4431 +         * @param searchFunction a function returning a non-null
4432 +         * result on success, else null
4433 +         * @return the task
4434 +         */
4435 +        public static <K,V,U> ForkJoinTask<U> searchKeys
4436 +            (ConcurrentHashMapV8<K,V> map,
4437 +             Fun<? super K, ? extends U> searchFunction) {
4438 +            if (searchFunction == null) throw new NullPointerException();
4439 +            return new SearchKeysTask<K,V,U>
4440 +                (map, null, -1, null, searchFunction,
4441 +                 new AtomicReference<U>());
4442 +        }
4443 +
4444 +        /**
4445 +         * Returns a task that when invoked, returns the result of
4446 +         * accumulating all keys using the given reducer to combine
4447 +         * values, or null if none.
4448 +         *
4449 +         * @param map the map
4450 +         * @param reducer a commutative associative combining function
4451 +         * @return the task
4452 +         */
4453 +        public static <K,V> ForkJoinTask<K> reduceKeys
4454 +            (ConcurrentHashMapV8<K,V> map,
4455 +             BiFun<? super K, ? super K, ? extends K> reducer) {
4456 +            if (reducer == null) throw new NullPointerException();
4457 +            return new ReduceKeysTask<K,V>
4458 +                (map, null, -1, null, reducer);
4459 +        }
4460 +
4461 +        /**
4462 +         * Returns a task that when invoked, returns the result of
4463 +         * accumulating the given transformation of all keys using the given
4464 +         * reducer to combine values, or null if none.
4465 +         *
4466 +         * @param map the map
4467 +         * @param transformer a function returning the transformation
4468 +         * for an element, or null if there is no transformation (in
4469 +         * which case it is not combined).
4470 +         * @param reducer a commutative associative combining function
4471 +         * @return the task
4472 +         */
4473 +        public static <K,V,U> ForkJoinTask<U> reduceKeys
4474 +            (ConcurrentHashMapV8<K,V> map,
4475 +             Fun<? super K, ? extends U> transformer,
4476 +             BiFun<? super U, ? super U, ? extends U> reducer) {
4477 +            if (transformer == null || reducer == null)
4478 +                throw new NullPointerException();
4479 +            return new MapReduceKeysTask<K,V,U>
4480 +                (map, null, -1, null, transformer, reducer);
4481 +        }
4482 +
4483 +        /**
4484 +         * Returns a task that when invoked, returns the result of
4485 +         * accumulating the given transformation of all keys using the given
4486 +         * reducer to combine values, and the given basis as an
4487 +         * identity value.
4488 +         *
4489 +         * @param map the map
4490 +         * @param transformer a function returning the transformation
4491 +         * for an element
4492 +         * @param basis the identity (initial default value) for the reduction
4493 +         * @param reducer a commutative associative combining function
4494 +         * @return the task
4495 +         */
4496 +        public static <K,V> ForkJoinTask<Double> reduceKeysToDouble
4497 +            (ConcurrentHashMapV8<K,V> map,
4498 +             ObjectToDouble<? super K> transformer,
4499 +             double basis,
4500 +             DoubleByDoubleToDouble reducer) {
4501 +            if (transformer == null || reducer == null)
4502 +                throw new NullPointerException();
4503 +            return new MapReduceKeysToDoubleTask<K,V>
4504 +                (map, null, -1, null, transformer, basis, reducer);
4505 +        }
4506 +
4507 +        /**
4508 +         * Returns a task that when invoked, returns the result of
4509 +         * accumulating the given transformation of all keys using the given
4510 +         * reducer to combine values, and the given basis as an
4511 +         * identity value.
4512 +         *
4513 +         * @param map the map
4514 +         * @param transformer a function returning the transformation
4515 +         * for an element
4516 +         * @param basis the identity (initial default value) for the reduction
4517 +         * @param reducer a commutative associative combining function
4518 +         * @return the task
4519 +         */
4520 +        public static <K,V> ForkJoinTask<Long> reduceKeysToLong
4521 +            (ConcurrentHashMapV8<K,V> map,
4522 +             ObjectToLong<? super K> transformer,
4523 +             long basis,
4524 +             LongByLongToLong reducer) {
4525 +            if (transformer == null || reducer == null)
4526 +                throw new NullPointerException();
4527 +            return new MapReduceKeysToLongTask<K,V>
4528 +                (map, null, -1, null, transformer, basis, reducer);
4529 +        }
4530 +
4531 +        /**
4532 +         * Returns a task that when invoked, returns the result of
4533 +         * accumulating the given transformation of all keys using the given
4534 +         * reducer to combine values, and the given basis as an
4535 +         * identity value.
4536 +         *
4537 +         * @param map the map
4538 +         * @param transformer a function returning the transformation
4539 +         * for an element
4540 +         * @param basis the identity (initial default value) for the reduction
4541 +         * @param reducer a commutative associative combining function
4542 +         * @return the task
4543 +         */
4544 +        public static <K,V> ForkJoinTask<Integer> reduceKeysToInt
4545 +            (ConcurrentHashMapV8<K,V> map,
4546 +             ObjectToInt<? super K> transformer,
4547 +             int basis,
4548 +             IntByIntToInt reducer) {
4549 +            if (transformer == null || reducer == null)
4550 +                throw new NullPointerException();
4551 +            return new MapReduceKeysToIntTask<K,V>
4552 +                (map, null, -1, null, transformer, basis, reducer);
4553 +        }
4554 +
4555 +        /**
4556 +         * Returns a task that when invoked, performs the given action
4557 +         * for each value.
4558 +         *
4559 +         * @param map the map
4560 +         * @param action the action
4561 +         */
4562 +        public static <K,V> ForkJoinTask<Void> forEachValue
4563 +            (ConcurrentHashMapV8<K,V> map,
4564 +             Action<V> action) {
4565 +            if (action == null) throw new NullPointerException();
4566 +            return new ForEachValueTask<K,V>(map, null, -1, null, action);
4567 +        }
4568 +
4569 +        /**
4570 +         * Returns a task that when invoked, performs the given action
4571 +         * for each non-null transformation of each value.
4572 +         *
4573 +         * @param map the map
4574 +         * @param transformer a function returning the transformation
4575 +         * for an element, or null if there is no transformation (in
4576 +         * which case the action is not applied)
4577 +         * @param action the action
4578 +         */
4579 +        public static <K,V,U> ForkJoinTask<Void> forEachValue
4580 +            (ConcurrentHashMapV8<K,V> map,
4581 +             Fun<? super V, ? extends U> transformer,
4582 +             Action<U> action) {
4583 +            if (transformer == null || action == null)
4584 +                throw new NullPointerException();
4585 +            return new ForEachTransformedValueTask<K,V,U>
4586 +                (map, null, -1, null, transformer, action);
4587 +        }
4588 +
4589 +        /**
4590 +         * Returns a task that when invoked, returns a non-null result
4591 +         * from applying the given search function on each value, or
4592 +         * null if none.  Upon success, further element processing is
4593 +         * suppressed and the results of any other parallel
4594 +         * invocations of the search function are ignored.
4595 +         *
4596 +         * @param map the map
4597 +         * @param searchFunction a function returning a non-null
4598 +         * result on success, else null
4599 +         * @return the task
4600 +         */
4601 +        public static <K,V,U> ForkJoinTask<U> searchValues
4602 +            (ConcurrentHashMapV8<K,V> map,
4603 +             Fun<? super V, ? extends U> searchFunction) {
4604 +            if (searchFunction == null) throw new NullPointerException();
4605 +            return new SearchValuesTask<K,V,U>
4606 +                (map, null, -1, null, searchFunction,
4607 +                 new AtomicReference<U>());
4608 +        }
4609 +
4610 +        /**
4611 +         * Returns a task that when invoked, returns the result of
4612 +         * accumulating all values using the given reducer to combine
4613 +         * values, or null if none.
4614 +         *
4615 +         * @param map the map
4616 +         * @param reducer a commutative associative combining function
4617 +         * @return the task
4618 +         */
4619 +        public static <K,V> ForkJoinTask<V> reduceValues
4620 +            (ConcurrentHashMapV8<K,V> map,
4621 +             BiFun<? super V, ? super V, ? extends V> reducer) {
4622 +            if (reducer == null) throw new NullPointerException();
4623 +            return new ReduceValuesTask<K,V>
4624 +                (map, null, -1, null, reducer);
4625 +        }
4626 +
4627 +        /**
4628 +         * Returns a task that when invoked, returns the result of
4629 +         * accumulating the given transformation of all values using the
4630 +         * given reducer to combine values, or null if none.
4631 +         *
4632 +         * @param map the map
4633 +         * @param transformer a function returning the transformation
4634 +         * for an element, or null if there is no transformation (in
4635 +         * which case it is not combined).
4636 +         * @param reducer a commutative associative combining function
4637 +         * @return the task
4638 +         */
4639 +        public static <K,V,U> ForkJoinTask<U> reduceValues
4640 +            (ConcurrentHashMapV8<K,V> map,
4641 +             Fun<? super V, ? extends U> transformer,
4642 +             BiFun<? super U, ? super U, ? extends U> reducer) {
4643 +            if (transformer == null || reducer == null)
4644 +                throw new NullPointerException();
4645 +            return new MapReduceValuesTask<K,V,U>
4646 +                (map, null, -1, null, transformer, reducer);
4647 +        }
4648 +
4649 +        /**
4650 +         * Returns a task that when invoked, returns the result of
4651 +         * accumulating the given transformation of all values using the
4652 +         * given reducer to combine values, and the given basis as an
4653 +         * identity value.
4654 +         *
4655 +         * @param map the map
4656 +         * @param transformer a function returning the transformation
4657 +         * for an element
4658 +         * @param basis the identity (initial default value) for the reduction
4659 +         * @param reducer a commutative associative combining function
4660 +         * @return the task
4661 +         */
4662 +        public static <K,V> ForkJoinTask<Double> reduceValuesToDouble
4663 +            (ConcurrentHashMapV8<K,V> map,
4664 +             ObjectToDouble<? super V> transformer,
4665 +             double basis,
4666 +             DoubleByDoubleToDouble reducer) {
4667 +            if (transformer == null || reducer == null)
4668 +                throw new NullPointerException();
4669 +            return new MapReduceValuesToDoubleTask<K,V>
4670 +                (map, null, -1, null, transformer, basis, reducer);
4671 +        }
4672 +
4673 +        /**
4674 +         * Returns a task that when invoked, returns the result of
4675 +         * accumulating the given transformation of all values using the
4676 +         * given reducer to combine values, and the given basis as an
4677 +         * identity value.
4678 +         *
4679 +         * @param map the map
4680 +         * @param transformer a function returning the transformation
4681 +         * for an element
4682 +         * @param basis the identity (initial default value) for the reduction
4683 +         * @param reducer a commutative associative combining function
4684 +         * @return the task
4685 +         */
4686 +        public static <K,V> ForkJoinTask<Long> reduceValuesToLong
4687 +            (ConcurrentHashMapV8<K,V> map,
4688 +             ObjectToLong<? super V> transformer,
4689 +             long basis,
4690 +             LongByLongToLong reducer) {
4691 +            if (transformer == null || reducer == null)
4692 +                throw new NullPointerException();
4693 +            return new MapReduceValuesToLongTask<K,V>
4694 +                (map, null, -1, null, transformer, basis, reducer);
4695 +        }
4696 +
4697 +        /**
4698 +         * Returns a task that when invoked, returns the result of
4699 +         * accumulating the given transformation of all values using the
4700 +         * given reducer to combine values, and the given basis as an
4701 +         * identity value.
4702 +         *
4703 +         * @param map the map
4704 +         * @param transformer a function returning the transformation
4705 +         * for an element
4706 +         * @param basis the identity (initial default value) for the reduction
4707 +         * @param reducer a commutative associative combining function
4708 +         * @return the task
4709 +         */
4710 +        public static <K,V> ForkJoinTask<Integer> reduceValuesToInt
4711 +            (ConcurrentHashMapV8<K,V> map,
4712 +             ObjectToInt<? super V> transformer,
4713 +             int basis,
4714 +             IntByIntToInt reducer) {
4715 +            if (transformer == null || reducer == null)
4716 +                throw new NullPointerException();
4717 +            return new MapReduceValuesToIntTask<K,V>
4718 +                (map, null, -1, null, transformer, basis, reducer);
4719 +        }
4720 +
4721 +        /**
4722 +         * Returns a task that when invoked, perform the given action
4723 +         * for each entry.
4724 +         *
4725 +         * @param map the map
4726 +         * @param action the action
4727 +         */
4728 +        public static <K,V> ForkJoinTask<Void> forEachEntry
4729 +            (ConcurrentHashMapV8<K,V> map,
4730 +             Action<Map.Entry<K,V>> action) {
4731 +            if (action == null) throw new NullPointerException();
4732 +            return new ForEachEntryTask<K,V>(map, null, -1, null, action);
4733 +        }
4734 +
4735 +        /**
4736 +         * Returns a task that when invoked, perform the given action
4737 +         * for each non-null transformation of each entry.
4738 +         *
4739 +         * @param map the map
4740 +         * @param transformer a function returning the transformation
4741 +         * for an element, or null if there is no transformation (in
4742 +         * which case the action is not applied)
4743 +         * @param action the action
4744 +         */
4745 +        public static <K,V,U> ForkJoinTask<Void> forEachEntry
4746 +            (ConcurrentHashMapV8<K,V> map,
4747 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
4748 +             Action<U> action) {
4749 +            if (transformer == null || action == null)
4750 +                throw new NullPointerException();
4751 +            return new ForEachTransformedEntryTask<K,V,U>
4752 +                (map, null, -1, null, transformer, action);
4753 +        }
4754 +
4755 +        /**
4756 +         * Returns a task that when invoked, returns a non-null result
4757 +         * from applying the given search function on each entry, or
4758 +         * null if none.  Upon success, further element processing is
4759 +         * suppressed and the results of any other parallel
4760 +         * invocations of the search function are ignored.
4761 +         *
4762 +         * @param map the map
4763 +         * @param searchFunction a function returning a non-null
4764 +         * result on success, else null
4765 +         * @return the task
4766 +         */
4767 +        public static <K,V,U> ForkJoinTask<U> searchEntries
4768 +            (ConcurrentHashMapV8<K,V> map,
4769 +             Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4770 +            if (searchFunction == null) throw new NullPointerException();
4771 +            return new SearchEntriesTask<K,V,U>
4772 +                (map, null, -1, null, searchFunction,
4773 +                 new AtomicReference<U>());
4774 +        }
4775 +
4776 +        /**
4777 +         * Returns a task that when invoked, returns the result of
4778 +         * accumulating all entries using the given reducer to combine
4779 +         * values, or null if none.
4780 +         *
4781 +         * @param map the map
4782 +         * @param reducer a commutative associative combining function
4783 +         * @return the task
4784 +         */
4785 +        public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries
4786 +            (ConcurrentHashMapV8<K,V> map,
4787 +             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4788 +            if (reducer == null) throw new NullPointerException();
4789 +            return new ReduceEntriesTask<K,V>
4790 +                (map, null, -1, null, reducer);
4791 +        }
4792 +
4793 +        /**
4794 +         * Returns a task that when invoked, returns the result of
4795 +         * accumulating the given transformation of all entries using the
4796 +         * given reducer to combine values, or null if none.
4797 +         *
4798 +         * @param map the map
4799 +         * @param transformer a function returning the transformation
4800 +         * for an element, or null if there is no transformation (in
4801 +         * which case it is not combined).
4802 +         * @param reducer a commutative associative combining function
4803 +         * @return the task
4804 +         */
4805 +        public static <K,V,U> ForkJoinTask<U> reduceEntries
4806 +            (ConcurrentHashMapV8<K,V> map,
4807 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
4808 +             BiFun<? super U, ? super U, ? extends U> reducer) {
4809 +            if (transformer == null || reducer == null)
4810 +                throw new NullPointerException();
4811 +            return new MapReduceEntriesTask<K,V,U>
4812 +                (map, null, -1, null, transformer, reducer);
4813 +        }
4814 +
4815 +        /**
4816 +         * Returns a task that when invoked, returns the result of
4817 +         * accumulating the given transformation of all entries using the
4818 +         * given reducer to combine values, and the given basis as an
4819 +         * identity value.
4820 +         *
4821 +         * @param map the map
4822 +         * @param transformer a function returning the transformation
4823 +         * for an element
4824 +         * @param basis the identity (initial default value) for the reduction
4825 +         * @param reducer a commutative associative combining function
4826 +         * @return the task
4827 +         */
4828 +        public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble
4829 +            (ConcurrentHashMapV8<K,V> map,
4830 +             ObjectToDouble<Map.Entry<K,V>> transformer,
4831 +             double basis,
4832 +             DoubleByDoubleToDouble reducer) {
4833 +            if (transformer == null || reducer == null)
4834 +                throw new NullPointerException();
4835 +            return new MapReduceEntriesToDoubleTask<K,V>
4836 +                (map, null, -1, null, transformer, basis, reducer);
4837 +        }
4838 +
4839 +        /**
4840 +         * Returns a task that when invoked, returns the result of
4841 +         * accumulating the given transformation of all entries using the
4842 +         * given reducer to combine values, and the given basis as an
4843 +         * identity value.
4844 +         *
4845 +         * @param map the map
4846 +         * @param transformer a function returning the transformation
4847 +         * for an element
4848 +         * @param basis the identity (initial default value) for the reduction
4849 +         * @param reducer a commutative associative combining function
4850 +         * @return the task
4851 +         */
4852 +        public static <K,V> ForkJoinTask<Long> reduceEntriesToLong
4853 +            (ConcurrentHashMapV8<K,V> map,
4854 +             ObjectToLong<Map.Entry<K,V>> transformer,
4855 +             long basis,
4856 +             LongByLongToLong reducer) {
4857 +            if (transformer == null || reducer == null)
4858 +                throw new NullPointerException();
4859 +            return new MapReduceEntriesToLongTask<K,V>
4860 +                (map, null, -1, null, transformer, basis, reducer);
4861 +        }
4862 +
4863 +        /**
4864 +         * Returns a task that when invoked, returns the result of
4865 +         * accumulating the given transformation of all entries using the
4866 +         * given reducer to combine values, and the given basis as an
4867 +         * identity value.
4868 +         *
4869 +         * @param map the map
4870 +         * @param transformer a function returning the transformation
4871 +         * for an element
4872 +         * @param basis the identity (initial default value) for the reduction
4873 +         * @param reducer a commutative associative combining function
4874 +         * @return the task
4875 +         */
4876 +        public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt
4877 +            (ConcurrentHashMapV8<K,V> map,
4878 +             ObjectToInt<Map.Entry<K,V>> transformer,
4879 +             int basis,
4880 +             IntByIntToInt reducer) {
4881 +            if (transformer == null || reducer == null)
4882 +                throw new NullPointerException();
4883 +            return new MapReduceEntriesToIntTask<K,V>
4884 +                (map, null, -1, null, transformer, basis, reducer);
4885          }
4886      }
4887  
4888 +    // -------------------------------------------------------
4889 +
4890 +    /**
4891 +     * Base for FJ tasks for bulk operations. This adds a variant of
4892 +     * CountedCompleters and some split and merge bookkeeping to
4893 +     * iterator functionality. The forEach and reduce methods are
4894 +     * similar to those illustrated in CountedCompleter documentation,
4895 +     * except that bottom-up reduction completions perform them within
4896 +     * their compute methods. The search methods are like forEach
4897 +     * except they continually poll for success and exit early.  Also,
4898 +     * exceptions are handled in a simpler manner, by just trying to
4899 +     * complete root task exceptionally.
4900 +     */
4901 +    @SuppressWarnings("serial") static abstract class BulkTask<K,V,R> extends Traverser<K,V,R> {
4902 +        final BulkTask<K,V,?> parent;  // completion target
4903 +        int batch;                     // split control; -1 for unknown
4904 +        int pending;                   // completion control
4905 +
4906 +        BulkTask(ConcurrentHashMapV8<K,V> map, BulkTask<K,V,?> parent,
4907 +                 int batch) {
4908 +            super(map);
4909 +            this.parent = parent;
4910 +            this.batch = batch;
4911 +            if (parent != null && map != null) { // split parent
4912 +                Node[] t;
4913 +                if ((t = parent.tab) == null &&
4914 +                    (t = parent.tab = map.table) != null)
4915 +                    parent.baseLimit = parent.baseSize = t.length;
4916 +                this.tab = t;
4917 +                this.baseSize = parent.baseSize;
4918 +                int hi = this.baseLimit = parent.baseLimit;
4919 +                parent.baseLimit = this.index = this.baseIndex =
4920 +                    (hi + parent.baseIndex + 1) >>> 1;
4921 +            }
4922 +        }
4923 +
4924 +        /**
4925 +         * Forces root task to complete.
4926 +         * @param ex if null, complete normally, else exceptionally
4927 +         * @return false to simplify use
4928 +         */
4929 +        final boolean tryCompleteComputation(Throwable ex) {
4930 +            for (BulkTask<K,V,?> a = this;;) {
4931 +                BulkTask<K,V,?> p = a.parent;
4932 +                if (p == null) {
4933 +                    if (ex != null)
4934 +                        a.completeExceptionally(ex);
4935 +                    else
4936 +                        a.quietlyComplete();
4937 +                    return false;
4938 +                }
4939 +                a = p;
4940 +            }
4941 +        }
4942 +
4943 +        /**
4944 +         * Version of tryCompleteComputation for function screening checks
4945 +         */
4946 +        final boolean abortOnNullFunction() {
4947 +            return tryCompleteComputation(new Error("Unexpected null function"));
4948 +        }
4949 +
4950 +        // utilities
4951 +
4952 +        /** CompareAndSet pending count */
4953 +        final boolean casPending(int cmp, int val) {
4954 +            return U.compareAndSwapInt(this, PENDING, cmp, val);
4955 +        }
4956 +
4957 +        /**
4958 +         * Returns approx exp2 of the number of times (minus one) to
4959 +         * split task by two before executing leaf action. This value
4960 +         * is faster to compute and more convenient to use as a guide
4961 +         * to splitting than is the depth, since it is used while
4962 +         * dividing by two anyway.
4963 +         */
4964 +        final int batch() {
4965 +            ConcurrentHashMapV8<K, V> m; int b; Node[] t;  ForkJoinPool pool;
4966 +            if ((b = batch) < 0 && (m = map) != null) { // force initialization
4967 +                if ((t = tab) == null && (t = tab = m.table) != null)
4968 +                    baseLimit = baseSize = t.length;
4969 +                if (t != null) {
4970 +                    long n = m.counter.sum();
4971 +                    int par = ((pool = getPool()) == null) ?
4972 +                        ForkJoinPool.getCommonPoolParallelism() :
4973 +                        pool.getParallelism();
4974 +                    int sp = par << 3; // slack of 8
4975 +                    b = batch = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp;
4976 +                }
4977 +            }
4978 +            return b;
4979 +        }
4980 +
4981 +        /**
4982 +         * Returns exportable snapshot entry.
4983 +         */
4984 +        static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
4985 +            return new AbstractMap.SimpleEntry<K,V>(k, v);
4986 +        }
4987 +
4988 +        // Unsafe mechanics
4989 +        private static final sun.misc.Unsafe U;
4990 +        private static final long PENDING;
4991 +        static {
4992 +            try {
4993 +                U = getUnsafe();
4994 +                PENDING = U.objectFieldOffset
4995 +                    (BulkTask.class.getDeclaredField("pending"));
4996 +            } catch (Exception e) {
4997 +                throw new Error(e);
4998 +            }
4999 +        }
5000 +    }
5001 +
5002 +    /**
5003 +     * Base class for non-reductive actions
5004 +     */
5005 +    @SuppressWarnings("serial") static abstract class BulkAction<K,V,R> extends BulkTask<K,V,R> {
5006 +        BulkAction<K,V,?> nextTask;
5007 +        BulkAction(ConcurrentHashMapV8<K,V> map, BulkTask<K,V,?> parent,
5008 +                   int batch, BulkAction<K,V,?> nextTask) {
5009 +            super(map, parent, batch);
5010 +            this.nextTask = nextTask;
5011 +        }
5012 +
5013 +        /**
5014 +         * Try to complete task and upward parents. Upon hitting
5015 +         * non-completed parent, if a non-FJ task, try to help out the
5016 +         * computation.
5017 +         */
5018 +        final void tryComplete(BulkAction<K,V,?> subtasks) {
5019 +            BulkTask<K,V,?> a = this, s = a;
5020 +            for (int c;;) {
5021 +                if ((c = a.pending) == 0) {
5022 +                    if ((a = (s = a).parent) == null) {
5023 +                        s.quietlyComplete();
5024 +                        break;
5025 +                    }
5026 +                }
5027 +                else if (a.casPending(c, c - 1)) {
5028 +                    if (subtasks != null && !inForkJoinPool()) {
5029 +                        while ((s = a.parent) != null)
5030 +                            a = s;
5031 +                        while (!a.isDone()) {
5032 +                            BulkAction<K,V,?> next = subtasks.nextTask;
5033 +                            if (subtasks.tryUnfork())
5034 +                                subtasks.exec();
5035 +                            if ((subtasks = next) == null)
5036 +                                break;
5037 +                        }
5038 +                    }
5039 +                    break;
5040 +                }
5041 +            }
5042 +        }
5043 +
5044 +    }
5045 +
5046 +    /*
5047 +     * Task classes. Coded in a regular but ugly format/style to
5048 +     * simplify checks that each variant differs in the right way from
5049 +     * others.
5050 +     */
5051 +
5052 +    @SuppressWarnings("serial") static final class ForEachKeyTask<K,V>
5053 +        extends BulkAction<K,V,Void> {
5054 +        final Action<K> action;
5055 +        ForEachKeyTask
5056 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5057 +             ForEachKeyTask<K,V> nextTask,
5058 +             Action<K> action) {
5059 +            super(m, p, b, nextTask);
5060 +            this.action = action;
5061 +        }
5062 +        @SuppressWarnings("unchecked") public final boolean exec() {
5063 +            final Action<K> action = this.action;
5064 +            if (action == null)
5065 +                return abortOnNullFunction();
5066 +            ForEachKeyTask<K,V> subtasks = null;
5067 +            try {
5068 +                int b = batch(), c;
5069 +                while (b > 1 && baseIndex != baseLimit) {
5070 +                    do {} while (!casPending(c = pending, c+1));
5071 +                    (subtasks = new ForEachKeyTask<K,V>
5072 +                     (map, this, b >>>= 1, subtasks, action)).fork();
5073 +                }
5074 +                while (advance() != null)
5075 +                    action.apply((K)nextKey);
5076 +            } catch (Throwable ex) {
5077 +                return tryCompleteComputation(ex);
5078 +            }
5079 +            tryComplete(subtasks);
5080 +            return false;
5081 +        }
5082 +    }
5083 +
5084 +    @SuppressWarnings("serial") static final class ForEachValueTask<K,V>
5085 +        extends BulkAction<K,V,Void> {
5086 +        final Action<V> action;
5087 +        ForEachValueTask
5088 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5089 +             ForEachValueTask<K,V> nextTask,
5090 +             Action<V> action) {
5091 +            super(m, p, b, nextTask);
5092 +            this.action = action;
5093 +        }
5094 +        @SuppressWarnings("unchecked") public final boolean exec() {
5095 +            final Action<V> action = this.action;
5096 +            if (action == null)
5097 +                return abortOnNullFunction();
5098 +            ForEachValueTask<K,V> subtasks = null;
5099 +            try {
5100 +                int b = batch(), c;
5101 +                while (b > 1 && baseIndex != baseLimit) {
5102 +                    do {} while (!casPending(c = pending, c+1));
5103 +                    (subtasks = new ForEachValueTask<K,V>
5104 +                     (map, this, b >>>= 1, subtasks, action)).fork();
5105 +                }
5106 +                Object v;
5107 +                while ((v = advance()) != null)
5108 +                    action.apply((V)v);
5109 +            } catch (Throwable ex) {
5110 +                return tryCompleteComputation(ex);
5111 +            }
5112 +            tryComplete(subtasks);
5113 +            return false;
5114 +        }
5115 +    }
5116 +
5117 +    @SuppressWarnings("serial") static final class ForEachEntryTask<K,V>
5118 +        extends BulkAction<K,V,Void> {
5119 +        final Action<Entry<K,V>> action;
5120 +        ForEachEntryTask
5121 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5122 +             ForEachEntryTask<K,V> nextTask,
5123 +             Action<Entry<K,V>> action) {
5124 +            super(m, p, b, nextTask);
5125 +            this.action = action;
5126 +        }
5127 +        @SuppressWarnings("unchecked") public final boolean exec() {
5128 +            final Action<Entry<K,V>> action = this.action;
5129 +            if (action == null)
5130 +                return abortOnNullFunction();
5131 +            ForEachEntryTask<K,V> subtasks = null;
5132 +            try {
5133 +                int b = batch(), c;
5134 +                while (b > 1 && baseIndex != baseLimit) {
5135 +                    do {} while (!casPending(c = pending, c+1));
5136 +                    (subtasks = new ForEachEntryTask<K,V>
5137 +                     (map, this, b >>>= 1, subtasks, action)).fork();
5138 +                }
5139 +                Object v;
5140 +                while ((v = advance()) != null)
5141 +                    action.apply(entryFor((K)nextKey, (V)v));
5142 +            } catch (Throwable ex) {
5143 +                return tryCompleteComputation(ex);
5144 +            }
5145 +            tryComplete(subtasks);
5146 +            return false;
5147 +        }
5148 +    }
5149 +
5150 +    @SuppressWarnings("serial") static final class ForEachMappingTask<K,V>
5151 +        extends BulkAction<K,V,Void> {
5152 +        final BiAction<K,V> action;
5153 +        ForEachMappingTask
5154 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5155 +             ForEachMappingTask<K,V> nextTask,
5156 +             BiAction<K,V> action) {
5157 +            super(m, p, b, nextTask);
5158 +            this.action = action;
5159 +        }
5160 +        @SuppressWarnings("unchecked") public final boolean exec() {
5161 +            final BiAction<K,V> action = this.action;
5162 +            if (action == null)
5163 +                return abortOnNullFunction();
5164 +            ForEachMappingTask<K,V> subtasks = null;
5165 +            try {
5166 +                int b = batch(), c;
5167 +                while (b > 1 && baseIndex != baseLimit) {
5168 +                    do {} while (!casPending(c = pending, c+1));
5169 +                    (subtasks = new ForEachMappingTask<K,V>
5170 +                     (map, this, b >>>= 1, subtasks, action)).fork();
5171 +                }
5172 +                Object v;
5173 +                while ((v = advance()) != null)
5174 +                    action.apply((K)nextKey, (V)v);
5175 +            } catch (Throwable ex) {
5176 +                return tryCompleteComputation(ex);
5177 +            }
5178 +            tryComplete(subtasks);
5179 +            return false;
5180 +        }
5181 +    }
5182 +
5183 +    @SuppressWarnings("serial") static final class ForEachTransformedKeyTask<K,V,U>
5184 +        extends BulkAction<K,V,Void> {
5185 +        final Fun<? super K, ? extends U> transformer;
5186 +        final Action<U> action;
5187 +        ForEachTransformedKeyTask
5188 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5189 +             ForEachTransformedKeyTask<K,V,U> nextTask,
5190 +             Fun<? super K, ? extends U> transformer,
5191 +             Action<U> action) {
5192 +            super(m, p, b, nextTask);
5193 +            this.transformer = transformer;
5194 +            this.action = action;
5195 +
5196 +        }
5197 +        @SuppressWarnings("unchecked") public final boolean exec() {
5198 +            final Fun<? super K, ? extends U> transformer =
5199 +                this.transformer;
5200 +            final Action<U> action = this.action;
5201 +            if (transformer == null || action == null)
5202 +                return abortOnNullFunction();
5203 +            ForEachTransformedKeyTask<K,V,U> subtasks = null;
5204 +            try {
5205 +                int b = batch(), c;
5206 +                while (b > 1 && baseIndex != baseLimit) {
5207 +                    do {} while (!casPending(c = pending, c+1));
5208 +                    (subtasks = new ForEachTransformedKeyTask<K,V,U>
5209 +                     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5210 +                }
5211 +                U u;
5212 +                while (advance() != null) {
5213 +                    if ((u = transformer.apply((K)nextKey)) != null)
5214 +                        action.apply(u);
5215 +                }
5216 +            } catch (Throwable ex) {
5217 +                return tryCompleteComputation(ex);
5218 +            }
5219 +            tryComplete(subtasks);
5220 +            return false;
5221 +        }
5222 +    }
5223 +
5224 +    @SuppressWarnings("serial") static final class ForEachTransformedValueTask<K,V,U>
5225 +        extends BulkAction<K,V,Void> {
5226 +        final Fun<? super V, ? extends U> transformer;
5227 +        final Action<U> action;
5228 +        ForEachTransformedValueTask
5229 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5230 +             ForEachTransformedValueTask<K,V,U> nextTask,
5231 +             Fun<? super V, ? extends U> transformer,
5232 +             Action<U> action) {
5233 +            super(m, p, b, nextTask);
5234 +            this.transformer = transformer;
5235 +            this.action = action;
5236 +
5237 +        }
5238 +        @SuppressWarnings("unchecked") public final boolean exec() {
5239 +            final Fun<? super V, ? extends U> transformer =
5240 +                this.transformer;
5241 +            final Action<U> action = this.action;
5242 +            if (transformer == null || action == null)
5243 +                return abortOnNullFunction();
5244 +            ForEachTransformedValueTask<K,V,U> subtasks = null;
5245 +            try {
5246 +                int b = batch(), c;
5247 +                while (b > 1 && baseIndex != baseLimit) {
5248 +                    do {} while (!casPending(c = pending, c+1));
5249 +                    (subtasks = new ForEachTransformedValueTask<K,V,U>
5250 +                     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5251 +                }
5252 +                Object v; U u;
5253 +                while ((v = advance()) != null) {
5254 +                    if ((u = transformer.apply((V)v)) != null)
5255 +                        action.apply(u);
5256 +                }
5257 +            } catch (Throwable ex) {
5258 +                return tryCompleteComputation(ex);
5259 +            }
5260 +            tryComplete(subtasks);
5261 +            return false;
5262 +        }
5263 +    }
5264 +
5265 +    @SuppressWarnings("serial") static final class ForEachTransformedEntryTask<K,V,U>
5266 +        extends BulkAction<K,V,Void> {
5267 +        final Fun<Map.Entry<K,V>, ? extends U> transformer;
5268 +        final Action<U> action;
5269 +        ForEachTransformedEntryTask
5270 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5271 +             ForEachTransformedEntryTask<K,V,U> nextTask,
5272 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
5273 +             Action<U> action) {
5274 +            super(m, p, b, nextTask);
5275 +            this.transformer = transformer;
5276 +            this.action = action;
5277 +
5278 +        }
5279 +        @SuppressWarnings("unchecked") public final boolean exec() {
5280 +            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5281 +                this.transformer;
5282 +            final Action<U> action = this.action;
5283 +            if (transformer == null || action == null)
5284 +                return abortOnNullFunction();
5285 +            ForEachTransformedEntryTask<K,V,U> subtasks = null;
5286 +            try {
5287 +                int b = batch(), c;
5288 +                while (b > 1 && baseIndex != baseLimit) {
5289 +                    do {} while (!casPending(c = pending, c+1));
5290 +                    (subtasks = new ForEachTransformedEntryTask<K,V,U>
5291 +                     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5292 +                }
5293 +                Object v; U u;
5294 +                while ((v = advance()) != null) {
5295 +                    if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5296 +                        action.apply(u);
5297 +                }
5298 +            } catch (Throwable ex) {
5299 +                return tryCompleteComputation(ex);
5300 +            }
5301 +            tryComplete(subtasks);
5302 +            return false;
5303 +        }
5304 +    }
5305 +
5306 +    @SuppressWarnings("serial") static final class ForEachTransformedMappingTask<K,V,U>
5307 +        extends BulkAction<K,V,Void> {
5308 +        final BiFun<? super K, ? super V, ? extends U> transformer;
5309 +        final Action<U> action;
5310 +        ForEachTransformedMappingTask
5311 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5312 +             ForEachTransformedMappingTask<K,V,U> nextTask,
5313 +             BiFun<? super K, ? super V, ? extends U> transformer,
5314 +             Action<U> action) {
5315 +            super(m, p, b, nextTask);
5316 +            this.transformer = transformer;
5317 +            this.action = action;
5318 +
5319 +        }
5320 +        @SuppressWarnings("unchecked") public final boolean exec() {
5321 +            final BiFun<? super K, ? super V, ? extends U> transformer =
5322 +                this.transformer;
5323 +            final Action<U> action = this.action;
5324 +            if (transformer == null || action == null)
5325 +                return abortOnNullFunction();
5326 +            ForEachTransformedMappingTask<K,V,U> subtasks = null;
5327 +            try {
5328 +                int b = batch(), c;
5329 +                while (b > 1 && baseIndex != baseLimit) {
5330 +                    do {} while (!casPending(c = pending, c+1));
5331 +                    (subtasks = new ForEachTransformedMappingTask<K,V,U>
5332 +                     (map, this, b >>>= 1, subtasks, transformer, action)).fork();
5333 +                }
5334 +                Object v; U u;
5335 +                while ((v = advance()) != null) {
5336 +                    if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5337 +                        action.apply(u);
5338 +                }
5339 +            } catch (Throwable ex) {
5340 +                return tryCompleteComputation(ex);
5341 +            }
5342 +            tryComplete(subtasks);
5343 +            return false;
5344 +        }
5345 +    }
5346 +
5347 +    @SuppressWarnings("serial") static final class SearchKeysTask<K,V,U>
5348 +        extends BulkAction<K,V,U> {
5349 +        final Fun<? super K, ? extends U> searchFunction;
5350 +        final AtomicReference<U> result;
5351 +        SearchKeysTask
5352 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5353 +             SearchKeysTask<K,V,U> nextTask,
5354 +             Fun<? super K, ? extends U> searchFunction,
5355 +             AtomicReference<U> result) {
5356 +            super(m, p, b, nextTask);
5357 +            this.searchFunction = searchFunction; this.result = result;
5358 +        }
5359 +        @SuppressWarnings("unchecked") public final boolean exec() {
5360 +            AtomicReference<U> result = this.result;
5361 +            final Fun<? super K, ? extends U> searchFunction =
5362 +                this.searchFunction;
5363 +            if (searchFunction == null || result == null)
5364 +                return abortOnNullFunction();
5365 +            SearchKeysTask<K,V,U> subtasks = null;
5366 +            try {
5367 +                int b = batch(), c;
5368 +                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5369 +                    do {} while (!casPending(c = pending, c+1));
5370 +                    (subtasks = new SearchKeysTask<K,V,U>
5371 +                     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5372 +                }
5373 +                U u;
5374 +                while (result.get() == null && advance() != null) {
5375 +                    if ((u = searchFunction.apply((K)nextKey)) != null) {
5376 +                        if (result.compareAndSet(null, u))
5377 +                            tryCompleteComputation(null);
5378 +                        break;
5379 +                    }
5380 +                }
5381 +            } catch (Throwable ex) {
5382 +                return tryCompleteComputation(ex);
5383 +            }
5384 +            tryComplete(subtasks);
5385 +            return false;
5386 +        }
5387 +        public final U getRawResult() { return result.get(); }
5388 +    }
5389 +
5390 +    @SuppressWarnings("serial") static final class SearchValuesTask<K,V,U>
5391 +        extends BulkAction<K,V,U> {
5392 +        final Fun<? super V, ? extends U> searchFunction;
5393 +        final AtomicReference<U> result;
5394 +        SearchValuesTask
5395 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5396 +             SearchValuesTask<K,V,U> nextTask,
5397 +             Fun<? super V, ? extends U> searchFunction,
5398 +             AtomicReference<U> result) {
5399 +            super(m, p, b, nextTask);
5400 +            this.searchFunction = searchFunction; this.result = result;
5401 +        }
5402 +        @SuppressWarnings("unchecked") public final boolean exec() {
5403 +            AtomicReference<U> result = this.result;
5404 +            final Fun<? super V, ? extends U> searchFunction =
5405 +                this.searchFunction;
5406 +            if (searchFunction == null || result == null)
5407 +                return abortOnNullFunction();
5408 +            SearchValuesTask<K,V,U> subtasks = null;
5409 +            try {
5410 +                int b = batch(), c;
5411 +                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5412 +                    do {} while (!casPending(c = pending, c+1));
5413 +                    (subtasks = new SearchValuesTask<K,V,U>
5414 +                     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5415 +                }
5416 +                Object v; U u;
5417 +                while (result.get() == null && (v = advance()) != null) {
5418 +                    if ((u = searchFunction.apply((V)v)) != null) {
5419 +                        if (result.compareAndSet(null, u))
5420 +                            tryCompleteComputation(null);
5421 +                        break;
5422 +                    }
5423 +                }
5424 +            } catch (Throwable ex) {
5425 +                return tryCompleteComputation(ex);
5426 +            }
5427 +            tryComplete(subtasks);
5428 +            return false;
5429 +        }
5430 +        public final U getRawResult() { return result.get(); }
5431 +    }
5432 +
5433 +    @SuppressWarnings("serial") static final class SearchEntriesTask<K,V,U>
5434 +        extends BulkAction<K,V,U> {
5435 +        final Fun<Entry<K,V>, ? extends U> searchFunction;
5436 +        final AtomicReference<U> result;
5437 +        SearchEntriesTask
5438 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5439 +             SearchEntriesTask<K,V,U> nextTask,
5440 +             Fun<Entry<K,V>, ? extends U> searchFunction,
5441 +             AtomicReference<U> result) {
5442 +            super(m, p, b, nextTask);
5443 +            this.searchFunction = searchFunction; this.result = result;
5444 +        }
5445 +        @SuppressWarnings("unchecked") public final boolean exec() {
5446 +            AtomicReference<U> result = this.result;
5447 +            final Fun<Entry<K,V>, ? extends U> searchFunction =
5448 +                this.searchFunction;
5449 +            if (searchFunction == null || result == null)
5450 +                return abortOnNullFunction();
5451 +            SearchEntriesTask<K,V,U> subtasks = null;
5452 +            try {
5453 +                int b = batch(), c;
5454 +                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5455 +                    do {} while (!casPending(c = pending, c+1));
5456 +                    (subtasks = new SearchEntriesTask<K,V,U>
5457 +                     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5458 +                }
5459 +                Object v; U u;
5460 +                while (result.get() == null && (v = advance()) != null) {
5461 +                    if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
5462 +                        if (result.compareAndSet(null, u))
5463 +                            tryCompleteComputation(null);
5464 +                        break;
5465 +                    }
5466 +                }
5467 +            } catch (Throwable ex) {
5468 +                return tryCompleteComputation(ex);
5469 +            }
5470 +            tryComplete(subtasks);
5471 +            return false;
5472 +        }
5473 +        public final U getRawResult() { return result.get(); }
5474 +    }
5475 +
5476 +    @SuppressWarnings("serial") static final class SearchMappingsTask<K,V,U>
5477 +        extends BulkAction<K,V,U> {
5478 +        final BiFun<? super K, ? super V, ? extends U> searchFunction;
5479 +        final AtomicReference<U> result;
5480 +        SearchMappingsTask
5481 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5482 +             SearchMappingsTask<K,V,U> nextTask,
5483 +             BiFun<? super K, ? super V, ? extends U> searchFunction,
5484 +             AtomicReference<U> result) {
5485 +            super(m, p, b, nextTask);
5486 +            this.searchFunction = searchFunction; this.result = result;
5487 +        }
5488 +        @SuppressWarnings("unchecked") public final boolean exec() {
5489 +            AtomicReference<U> result = this.result;
5490 +            final BiFun<? super K, ? super V, ? extends U> searchFunction =
5491 +                this.searchFunction;
5492 +            if (searchFunction == null || result == null)
5493 +                return abortOnNullFunction();
5494 +            SearchMappingsTask<K,V,U> subtasks = null;
5495 +            try {
5496 +                int b = batch(), c;
5497 +                while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5498 +                    do {} while (!casPending(c = pending, c+1));
5499 +                    (subtasks = new SearchMappingsTask<K,V,U>
5500 +                     (map, this, b >>>= 1, subtasks, searchFunction, result)).fork();
5501 +                }
5502 +                Object v; U u;
5503 +                while (result.get() == null && (v = advance()) != null) {
5504 +                    if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5505 +                        if (result.compareAndSet(null, u))
5506 +                            tryCompleteComputation(null);
5507 +                        break;
5508 +                    }
5509 +                }
5510 +            } catch (Throwable ex) {
5511 +                return tryCompleteComputation(ex);
5512 +            }
5513 +            tryComplete(subtasks);
5514 +            return false;
5515 +        }
5516 +        public final U getRawResult() { return result.get(); }
5517 +    }
5518 +
5519 +    @SuppressWarnings("serial") static final class ReduceKeysTask<K,V>
5520 +        extends BulkTask<K,V,K> {
5521 +        final BiFun<? super K, ? super K, ? extends K> reducer;
5522 +        K result;
5523 +        ReduceKeysTask<K,V> rights, nextRight;
5524 +        ReduceKeysTask
5525 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5526 +             ReduceKeysTask<K,V> nextRight,
5527 +             BiFun<? super K, ? super K, ? extends K> reducer) {
5528 +            super(m, p, b); this.nextRight = nextRight;
5529 +            this.reducer = reducer;
5530 +        }
5531 +        @SuppressWarnings("unchecked") public final boolean exec() {
5532 +            final BiFun<? super K, ? super K, ? extends K> reducer =
5533 +                this.reducer;
5534 +            if (reducer == null)
5535 +                return abortOnNullFunction();
5536 +            try {
5537 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5538 +                    do {} while (!casPending(c = pending, c+1));
5539 +                    (rights = new ReduceKeysTask<K,V>
5540 +                     (map, this, b >>>= 1, rights, reducer)).fork();
5541 +                }
5542 +                K r = null;
5543 +                while (advance() != null) {
5544 +                    K u = (K)nextKey;
5545 +                    r = (r == null) ? u : reducer.apply(r, u);
5546 +                }
5547 +                result = r;
5548 +                for (ReduceKeysTask<K,V> t = this, s;;) {
5549 +                    int c; BulkTask<K,V,?> par; K tr, sr;
5550 +                    if ((c = t.pending) == 0) {
5551 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5552 +                            if ((sr = s.result) != null)
5553 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5554 +                        }
5555 +                        if ((par = t.parent) == null ||
5556 +                            !(par instanceof ReduceKeysTask)) {
5557 +                            t.quietlyComplete();
5558 +                            break;
5559 +                        }
5560 +                        t = (ReduceKeysTask<K,V>)par;
5561 +                    }
5562 +                    else if (t.casPending(c, c - 1))
5563 +                        break;
5564 +                }
5565 +            } catch (Throwable ex) {
5566 +                return tryCompleteComputation(ex);
5567 +            }
5568 +            ReduceKeysTask<K,V> s = rights;
5569 +            if (s != null && !inForkJoinPool()) {
5570 +                do  {
5571 +                    if (s.tryUnfork())
5572 +                        s.exec();
5573 +                } while ((s = s.nextRight) != null);
5574 +            }
5575 +            return false;
5576 +        }
5577 +        public final K getRawResult() { return result; }
5578 +    }
5579 +
5580 +    @SuppressWarnings("serial") static final class ReduceValuesTask<K,V>
5581 +        extends BulkTask<K,V,V> {
5582 +        final BiFun<? super V, ? super V, ? extends V> reducer;
5583 +        V result;
5584 +        ReduceValuesTask<K,V> rights, nextRight;
5585 +        ReduceValuesTask
5586 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5587 +             ReduceValuesTask<K,V> nextRight,
5588 +             BiFun<? super V, ? super V, ? extends V> reducer) {
5589 +            super(m, p, b); this.nextRight = nextRight;
5590 +            this.reducer = reducer;
5591 +        }
5592 +        @SuppressWarnings("unchecked") public final boolean exec() {
5593 +            final BiFun<? super V, ? super V, ? extends V> reducer =
5594 +                this.reducer;
5595 +            if (reducer == null)
5596 +                return abortOnNullFunction();
5597 +            try {
5598 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5599 +                    do {} while (!casPending(c = pending, c+1));
5600 +                    (rights = new ReduceValuesTask<K,V>
5601 +                     (map, this, b >>>= 1, rights, reducer)).fork();
5602 +                }
5603 +                V r = null;
5604 +                Object v;
5605 +                while ((v = advance()) != null) {
5606 +                    V u = (V)v;
5607 +                    r = (r == null) ? u : reducer.apply(r, u);
5608 +                }
5609 +                result = r;
5610 +                for (ReduceValuesTask<K,V> t = this, s;;) {
5611 +                    int c; BulkTask<K,V,?> par; V tr, sr;
5612 +                    if ((c = t.pending) == 0) {
5613 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5614 +                            if ((sr = s.result) != null)
5615 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5616 +                        }
5617 +                        if ((par = t.parent) == null ||
5618 +                            !(par instanceof ReduceValuesTask)) {
5619 +                            t.quietlyComplete();
5620 +                            break;
5621 +                        }
5622 +                        t = (ReduceValuesTask<K,V>)par;
5623 +                    }
5624 +                    else if (t.casPending(c, c - 1))
5625 +                        break;
5626 +                }
5627 +            } catch (Throwable ex) {
5628 +                return tryCompleteComputation(ex);
5629 +            }
5630 +            ReduceValuesTask<K,V> s = rights;
5631 +            if (s != null && !inForkJoinPool()) {
5632 +                do  {
5633 +                    if (s.tryUnfork())
5634 +                        s.exec();
5635 +                } while ((s = s.nextRight) != null);
5636 +            }
5637 +            return false;
5638 +        }
5639 +        public final V getRawResult() { return result; }
5640 +    }
5641 +
5642 +    @SuppressWarnings("serial") static final class ReduceEntriesTask<K,V>
5643 +        extends BulkTask<K,V,Map.Entry<K,V>> {
5644 +        final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5645 +        Map.Entry<K,V> result;
5646 +        ReduceEntriesTask<K,V> rights, nextRight;
5647 +        ReduceEntriesTask
5648 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5649 +             ReduceEntriesTask<K,V> nextRight,
5650 +             BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5651 +            super(m, p, b); this.nextRight = nextRight;
5652 +            this.reducer = reducer;
5653 +        }
5654 +        @SuppressWarnings("unchecked") public final boolean exec() {
5655 +            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
5656 +                this.reducer;
5657 +            if (reducer == null)
5658 +                return abortOnNullFunction();
5659 +            try {
5660 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5661 +                    do {} while (!casPending(c = pending, c+1));
5662 +                    (rights = new ReduceEntriesTask<K,V>
5663 +                     (map, this, b >>>= 1, rights, reducer)).fork();
5664 +                }
5665 +                Map.Entry<K,V> r = null;
5666 +                Object v;
5667 +                while ((v = advance()) != null) {
5668 +                    Map.Entry<K,V> u = entryFor((K)nextKey, (V)v);
5669 +                    r = (r == null) ? u : reducer.apply(r, u);
5670 +                }
5671 +                result = r;
5672 +                for (ReduceEntriesTask<K,V> t = this, s;;) {
5673 +                    int c; BulkTask<K,V,?> par; Map.Entry<K,V> tr, sr;
5674 +                    if ((c = t.pending) == 0) {
5675 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5676 +                            if ((sr = s.result) != null)
5677 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5678 +                        }
5679 +                        if ((par = t.parent) == null ||
5680 +                            !(par instanceof ReduceEntriesTask)) {
5681 +                            t.quietlyComplete();
5682 +                            break;
5683 +                        }
5684 +                        t = (ReduceEntriesTask<K,V>)par;
5685 +                    }
5686 +                    else if (t.casPending(c, c - 1))
5687 +                        break;
5688 +                }
5689 +            } catch (Throwable ex) {
5690 +                return tryCompleteComputation(ex);
5691 +            }
5692 +            ReduceEntriesTask<K,V> s = rights;
5693 +            if (s != null && !inForkJoinPool()) {
5694 +                do  {
5695 +                    if (s.tryUnfork())
5696 +                        s.exec();
5697 +                } while ((s = s.nextRight) != null);
5698 +            }
5699 +            return false;
5700 +        }
5701 +        public final Map.Entry<K,V> getRawResult() { return result; }
5702 +    }
5703 +
5704 +    @SuppressWarnings("serial") static final class MapReduceKeysTask<K,V,U>
5705 +        extends BulkTask<K,V,U> {
5706 +        final Fun<? super K, ? extends U> transformer;
5707 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5708 +        U result;
5709 +        MapReduceKeysTask<K,V,U> rights, nextRight;
5710 +        MapReduceKeysTask
5711 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5712 +             MapReduceKeysTask<K,V,U> nextRight,
5713 +             Fun<? super K, ? extends U> transformer,
5714 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5715 +            super(m, p, b); this.nextRight = nextRight;
5716 +            this.transformer = transformer;
5717 +            this.reducer = reducer;
5718 +        }
5719 +        @SuppressWarnings("unchecked") public final boolean exec() {
5720 +            final Fun<? super K, ? extends U> transformer =
5721 +                this.transformer;
5722 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5723 +                this.reducer;
5724 +            if (transformer == null || reducer == null)
5725 +                return abortOnNullFunction();
5726 +            try {
5727 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5728 +                    do {} while (!casPending(c = pending, c+1));
5729 +                    (rights = new MapReduceKeysTask<K,V,U>
5730 +                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5731 +                }
5732 +                U r = null, u;
5733 +                while (advance() != null) {
5734 +                    if ((u = transformer.apply((K)nextKey)) != null)
5735 +                        r = (r == null) ? u : reducer.apply(r, u);
5736 +                }
5737 +                result = r;
5738 +                for (MapReduceKeysTask<K,V,U> t = this, s;;) {
5739 +                    int c; BulkTask<K,V,?> par; U tr, sr;
5740 +                    if ((c = t.pending) == 0) {
5741 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5742 +                            if ((sr = s.result) != null)
5743 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5744 +                        }
5745 +                        if ((par = t.parent) == null ||
5746 +                            !(par instanceof MapReduceKeysTask)) {
5747 +                            t.quietlyComplete();
5748 +                            break;
5749 +                        }
5750 +                        t = (MapReduceKeysTask<K,V,U>)par;
5751 +                    }
5752 +                    else if (t.casPending(c, c - 1))
5753 +                        break;
5754 +                }
5755 +            } catch (Throwable ex) {
5756 +                return tryCompleteComputation(ex);
5757 +            }
5758 +            MapReduceKeysTask<K,V,U> s = rights;
5759 +            if (s != null && !inForkJoinPool()) {
5760 +                do  {
5761 +                    if (s.tryUnfork())
5762 +                        s.exec();
5763 +                } while ((s = s.nextRight) != null);
5764 +            }
5765 +            return false;
5766 +        }
5767 +        public final U getRawResult() { return result; }
5768 +    }
5769 +
5770 +    @SuppressWarnings("serial") static final class MapReduceValuesTask<K,V,U>
5771 +        extends BulkTask<K,V,U> {
5772 +        final Fun<? super V, ? extends U> transformer;
5773 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5774 +        U result;
5775 +        MapReduceValuesTask<K,V,U> rights, nextRight;
5776 +        MapReduceValuesTask
5777 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5778 +             MapReduceValuesTask<K,V,U> nextRight,
5779 +             Fun<? super V, ? extends U> transformer,
5780 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5781 +            super(m, p, b); this.nextRight = nextRight;
5782 +            this.transformer = transformer;
5783 +            this.reducer = reducer;
5784 +        }
5785 +        @SuppressWarnings("unchecked") public final boolean exec() {
5786 +            final Fun<? super V, ? extends U> transformer =
5787 +                this.transformer;
5788 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5789 +                this.reducer;
5790 +            if (transformer == null || reducer == null)
5791 +                return abortOnNullFunction();
5792 +            try {
5793 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5794 +                    do {} while (!casPending(c = pending, c+1));
5795 +                    (rights = new MapReduceValuesTask<K,V,U>
5796 +                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5797 +                }
5798 +                U r = null, u;
5799 +                Object v;
5800 +                while ((v = advance()) != null) {
5801 +                    if ((u = transformer.apply((V)v)) != null)
5802 +                        r = (r == null) ? u : reducer.apply(r, u);
5803 +                }
5804 +                result = r;
5805 +                for (MapReduceValuesTask<K,V,U> t = this, s;;) {
5806 +                    int c; BulkTask<K,V,?> par; U tr, sr;
5807 +                    if ((c = t.pending) == 0) {
5808 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5809 +                            if ((sr = s.result) != null)
5810 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5811 +                        }
5812 +                        if ((par = t.parent) == null ||
5813 +                            !(par instanceof MapReduceValuesTask)) {
5814 +                            t.quietlyComplete();
5815 +                            break;
5816 +                        }
5817 +                        t = (MapReduceValuesTask<K,V,U>)par;
5818 +                    }
5819 +                    else if (t.casPending(c, c - 1))
5820 +                        break;
5821 +                }
5822 +            } catch (Throwable ex) {
5823 +                return tryCompleteComputation(ex);
5824 +            }
5825 +            MapReduceValuesTask<K,V,U> s = rights;
5826 +            if (s != null && !inForkJoinPool()) {
5827 +                do  {
5828 +                    if (s.tryUnfork())
5829 +                        s.exec();
5830 +                } while ((s = s.nextRight) != null);
5831 +            }
5832 +            return false;
5833 +        }
5834 +        public final U getRawResult() { return result; }
5835 +    }
5836 +
5837 +    @SuppressWarnings("serial") static final class MapReduceEntriesTask<K,V,U>
5838 +        extends BulkTask<K,V,U> {
5839 +        final Fun<Map.Entry<K,V>, ? extends U> transformer;
5840 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5841 +        U result;
5842 +        MapReduceEntriesTask<K,V,U> rights, nextRight;
5843 +        MapReduceEntriesTask
5844 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5845 +             MapReduceEntriesTask<K,V,U> nextRight,
5846 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
5847 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5848 +            super(m, p, b); this.nextRight = nextRight;
5849 +            this.transformer = transformer;
5850 +            this.reducer = reducer;
5851 +        }
5852 +        @SuppressWarnings("unchecked") public final boolean exec() {
5853 +            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5854 +                this.transformer;
5855 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5856 +                this.reducer;
5857 +            if (transformer == null || reducer == null)
5858 +                return abortOnNullFunction();
5859 +            try {
5860 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5861 +                    do {} while (!casPending(c = pending, c+1));
5862 +                    (rights = new MapReduceEntriesTask<K,V,U>
5863 +                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5864 +                }
5865 +                U r = null, u;
5866 +                Object v;
5867 +                while ((v = advance()) != null) {
5868 +                    if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5869 +                        r = (r == null) ? u : reducer.apply(r, u);
5870 +                }
5871 +                result = r;
5872 +                for (MapReduceEntriesTask<K,V,U> t = this, s;;) {
5873 +                    int c; BulkTask<K,V,?> par; U tr, sr;
5874 +                    if ((c = t.pending) == 0) {
5875 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5876 +                            if ((sr = s.result) != null)
5877 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5878 +                        }
5879 +                        if ((par = t.parent) == null ||
5880 +                            !(par instanceof MapReduceEntriesTask)) {
5881 +                            t.quietlyComplete();
5882 +                            break;
5883 +                        }
5884 +                        t = (MapReduceEntriesTask<K,V,U>)par;
5885 +                    }
5886 +                    else if (t.casPending(c, c - 1))
5887 +                        break;
5888 +                }
5889 +            } catch (Throwable ex) {
5890 +                return tryCompleteComputation(ex);
5891 +            }
5892 +            MapReduceEntriesTask<K,V,U> s = rights;
5893 +            if (s != null && !inForkJoinPool()) {
5894 +                do  {
5895 +                    if (s.tryUnfork())
5896 +                        s.exec();
5897 +                } while ((s = s.nextRight) != null);
5898 +            }
5899 +            return false;
5900 +        }
5901 +        public final U getRawResult() { return result; }
5902 +    }
5903 +
5904 +    @SuppressWarnings("serial") static final class MapReduceMappingsTask<K,V,U>
5905 +        extends BulkTask<K,V,U> {
5906 +        final BiFun<? super K, ? super V, ? extends U> transformer;
5907 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5908 +        U result;
5909 +        MapReduceMappingsTask<K,V,U> rights, nextRight;
5910 +        MapReduceMappingsTask
5911 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5912 +             MapReduceMappingsTask<K,V,U> nextRight,
5913 +             BiFun<? super K, ? super V, ? extends U> transformer,
5914 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5915 +            super(m, p, b); this.nextRight = nextRight;
5916 +            this.transformer = transformer;
5917 +            this.reducer = reducer;
5918 +        }
5919 +        @SuppressWarnings("unchecked") public final boolean exec() {
5920 +            final BiFun<? super K, ? super V, ? extends U> transformer =
5921 +                this.transformer;
5922 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5923 +                this.reducer;
5924 +            if (transformer == null || reducer == null)
5925 +                return abortOnNullFunction();
5926 +            try {
5927 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5928 +                    do {} while (!casPending(c = pending, c+1));
5929 +                    (rights = new MapReduceMappingsTask<K,V,U>
5930 +                     (map, this, b >>>= 1, rights, transformer, reducer)).fork();
5931 +                }
5932 +                U r = null, u;
5933 +                Object v;
5934 +                while ((v = advance()) != null) {
5935 +                    if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5936 +                        r = (r == null) ? u : reducer.apply(r, u);
5937 +                }
5938 +                result = r;
5939 +                for (MapReduceMappingsTask<K,V,U> t = this, s;;) {
5940 +                    int c; BulkTask<K,V,?> par; U tr, sr;
5941 +                    if ((c = t.pending) == 0) {
5942 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
5943 +                            if ((sr = s.result) != null)
5944 +                                t.result = ((tr = t.result) == null) ? sr : reducer.apply(tr, sr);
5945 +                        }
5946 +                        if ((par = t.parent) == null ||
5947 +                            !(par instanceof MapReduceMappingsTask)) {
5948 +                            t.quietlyComplete();
5949 +                            break;
5950 +                        }
5951 +                        t = (MapReduceMappingsTask<K,V,U>)par;
5952 +                    }
5953 +                    else if (t.casPending(c, c - 1))
5954 +                        break;
5955 +                }
5956 +            } catch (Throwable ex) {
5957 +                return tryCompleteComputation(ex);
5958 +            }
5959 +            MapReduceMappingsTask<K,V,U> s = rights;
5960 +            if (s != null && !inForkJoinPool()) {
5961 +                do  {
5962 +                    if (s.tryUnfork())
5963 +                        s.exec();
5964 +                } while ((s = s.nextRight) != null);
5965 +            }
5966 +            return false;
5967 +        }
5968 +        public final U getRawResult() { return result; }
5969 +    }
5970 +
5971 +    @SuppressWarnings("serial") static final class MapReduceKeysToDoubleTask<K,V>
5972 +        extends BulkTask<K,V,Double> {
5973 +        final ObjectToDouble<? super K> transformer;
5974 +        final DoubleByDoubleToDouble reducer;
5975 +        final double basis;
5976 +        double result;
5977 +        MapReduceKeysToDoubleTask<K,V> rights, nextRight;
5978 +        MapReduceKeysToDoubleTask
5979 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
5980 +             MapReduceKeysToDoubleTask<K,V> nextRight,
5981 +             ObjectToDouble<? super K> transformer,
5982 +             double basis,
5983 +             DoubleByDoubleToDouble reducer) {
5984 +            super(m, p, b); this.nextRight = nextRight;
5985 +            this.transformer = transformer;
5986 +            this.basis = basis; this.reducer = reducer;
5987 +        }
5988 +        @SuppressWarnings("unchecked") public final boolean exec() {
5989 +            final ObjectToDouble<? super K> transformer =
5990 +                this.transformer;
5991 +            final DoubleByDoubleToDouble reducer = this.reducer;
5992 +            if (transformer == null || reducer == null)
5993 +                return abortOnNullFunction();
5994 +            try {
5995 +                final double id = this.basis;
5996 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
5997 +                    do {} while (!casPending(c = pending, c+1));
5998 +                    (rights = new MapReduceKeysToDoubleTask<K,V>
5999 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6000 +                }
6001 +                double r = id;
6002 +                while (advance() != null)
6003 +                    r = reducer.apply(r, transformer.apply((K)nextKey));
6004 +                result = r;
6005 +                for (MapReduceKeysToDoubleTask<K,V> t = this, s;;) {
6006 +                    int c; BulkTask<K,V,?> par;
6007 +                    if ((c = t.pending) == 0) {
6008 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6009 +                            t.result = reducer.apply(t.result, s.result);
6010 +                        }
6011 +                        if ((par = t.parent) == null ||
6012 +                            !(par instanceof MapReduceKeysToDoubleTask)) {
6013 +                            t.quietlyComplete();
6014 +                            break;
6015 +                        }
6016 +                        t = (MapReduceKeysToDoubleTask<K,V>)par;
6017 +                    }
6018 +                    else if (t.casPending(c, c - 1))
6019 +                        break;
6020 +                }
6021 +            } catch (Throwable ex) {
6022 +                return tryCompleteComputation(ex);
6023 +            }
6024 +            MapReduceKeysToDoubleTask<K,V> s = rights;
6025 +            if (s != null && !inForkJoinPool()) {
6026 +                do  {
6027 +                    if (s.tryUnfork())
6028 +                        s.exec();
6029 +                } while ((s = s.nextRight) != null);
6030 +            }
6031 +            return false;
6032 +        }
6033 +        public final Double getRawResult() { return result; }
6034 +    }
6035 +
6036 +    @SuppressWarnings("serial") static final class MapReduceValuesToDoubleTask<K,V>
6037 +        extends BulkTask<K,V,Double> {
6038 +        final ObjectToDouble<? super V> transformer;
6039 +        final DoubleByDoubleToDouble reducer;
6040 +        final double basis;
6041 +        double result;
6042 +        MapReduceValuesToDoubleTask<K,V> rights, nextRight;
6043 +        MapReduceValuesToDoubleTask
6044 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6045 +             MapReduceValuesToDoubleTask<K,V> nextRight,
6046 +             ObjectToDouble<? super V> transformer,
6047 +             double basis,
6048 +             DoubleByDoubleToDouble reducer) {
6049 +            super(m, p, b); this.nextRight = nextRight;
6050 +            this.transformer = transformer;
6051 +            this.basis = basis; this.reducer = reducer;
6052 +        }
6053 +        @SuppressWarnings("unchecked") public final boolean exec() {
6054 +            final ObjectToDouble<? super V> transformer =
6055 +                this.transformer;
6056 +            final DoubleByDoubleToDouble reducer = this.reducer;
6057 +            if (transformer == null || reducer == null)
6058 +                return abortOnNullFunction();
6059 +            try {
6060 +                final double id = this.basis;
6061 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6062 +                    do {} while (!casPending(c = pending, c+1));
6063 +                    (rights = new MapReduceValuesToDoubleTask<K,V>
6064 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6065 +                }
6066 +                double r = id;
6067 +                Object v;
6068 +                while ((v = advance()) != null)
6069 +                    r = reducer.apply(r, transformer.apply((V)v));
6070 +                result = r;
6071 +                for (MapReduceValuesToDoubleTask<K,V> t = this, s;;) {
6072 +                    int c; BulkTask<K,V,?> par;
6073 +                    if ((c = t.pending) == 0) {
6074 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6075 +                            t.result = reducer.apply(t.result, s.result);
6076 +                        }
6077 +                        if ((par = t.parent) == null ||
6078 +                            !(par instanceof MapReduceValuesToDoubleTask)) {
6079 +                            t.quietlyComplete();
6080 +                            break;
6081 +                        }
6082 +                        t = (MapReduceValuesToDoubleTask<K,V>)par;
6083 +                    }
6084 +                    else if (t.casPending(c, c - 1))
6085 +                        break;
6086 +                }
6087 +            } catch (Throwable ex) {
6088 +                return tryCompleteComputation(ex);
6089 +            }
6090 +            MapReduceValuesToDoubleTask<K,V> s = rights;
6091 +            if (s != null && !inForkJoinPool()) {
6092 +                do  {
6093 +                    if (s.tryUnfork())
6094 +                        s.exec();
6095 +                } while ((s = s.nextRight) != null);
6096 +            }
6097 +            return false;
6098 +        }
6099 +        public final Double getRawResult() { return result; }
6100 +    }
6101 +
6102 +    @SuppressWarnings("serial") static final class MapReduceEntriesToDoubleTask<K,V>
6103 +        extends BulkTask<K,V,Double> {
6104 +        final ObjectToDouble<Map.Entry<K,V>> transformer;
6105 +        final DoubleByDoubleToDouble reducer;
6106 +        final double basis;
6107 +        double result;
6108 +        MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
6109 +        MapReduceEntriesToDoubleTask
6110 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6111 +             MapReduceEntriesToDoubleTask<K,V> nextRight,
6112 +             ObjectToDouble<Map.Entry<K,V>> transformer,
6113 +             double basis,
6114 +             DoubleByDoubleToDouble reducer) {
6115 +            super(m, p, b); this.nextRight = nextRight;
6116 +            this.transformer = transformer;
6117 +            this.basis = basis; this.reducer = reducer;
6118 +        }
6119 +        @SuppressWarnings("unchecked") public final boolean exec() {
6120 +            final ObjectToDouble<Map.Entry<K,V>> transformer =
6121 +                this.transformer;
6122 +            final DoubleByDoubleToDouble reducer = this.reducer;
6123 +            if (transformer == null || reducer == null)
6124 +                return abortOnNullFunction();
6125 +            try {
6126 +                final double id = this.basis;
6127 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6128 +                    do {} while (!casPending(c = pending, c+1));
6129 +                    (rights = new MapReduceEntriesToDoubleTask<K,V>
6130 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6131 +                }
6132 +                double r = id;
6133 +                Object v;
6134 +                while ((v = advance()) != null)
6135 +                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
6136 +                result = r;
6137 +                for (MapReduceEntriesToDoubleTask<K,V> t = this, s;;) {
6138 +                    int c; BulkTask<K,V,?> par;
6139 +                    if ((c = t.pending) == 0) {
6140 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6141 +                            t.result = reducer.apply(t.result, s.result);
6142 +                        }
6143 +                        if ((par = t.parent) == null ||
6144 +                            !(par instanceof MapReduceEntriesToDoubleTask)) {
6145 +                            t.quietlyComplete();
6146 +                            break;
6147 +                        }
6148 +                        t = (MapReduceEntriesToDoubleTask<K,V>)par;
6149 +                    }
6150 +                    else if (t.casPending(c, c - 1))
6151 +                        break;
6152 +                }
6153 +            } catch (Throwable ex) {
6154 +                return tryCompleteComputation(ex);
6155 +            }
6156 +            MapReduceEntriesToDoubleTask<K,V> s = rights;
6157 +            if (s != null && !inForkJoinPool()) {
6158 +                do  {
6159 +                    if (s.tryUnfork())
6160 +                        s.exec();
6161 +                } while ((s = s.nextRight) != null);
6162 +            }
6163 +            return false;
6164 +        }
6165 +        public final Double getRawResult() { return result; }
6166 +    }
6167 +
6168 +    @SuppressWarnings("serial") static final class MapReduceMappingsToDoubleTask<K,V>
6169 +        extends BulkTask<K,V,Double> {
6170 +        final ObjectByObjectToDouble<? super K, ? super V> transformer;
6171 +        final DoubleByDoubleToDouble reducer;
6172 +        final double basis;
6173 +        double result;
6174 +        MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
6175 +        MapReduceMappingsToDoubleTask
6176 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6177 +             MapReduceMappingsToDoubleTask<K,V> nextRight,
6178 +             ObjectByObjectToDouble<? super K, ? super V> transformer,
6179 +             double basis,
6180 +             DoubleByDoubleToDouble reducer) {
6181 +            super(m, p, b); this.nextRight = nextRight;
6182 +            this.transformer = transformer;
6183 +            this.basis = basis; this.reducer = reducer;
6184 +        }
6185 +        @SuppressWarnings("unchecked") public final boolean exec() {
6186 +            final ObjectByObjectToDouble<? super K, ? super V> transformer =
6187 +                this.transformer;
6188 +            final DoubleByDoubleToDouble reducer = this.reducer;
6189 +            if (transformer == null || reducer == null)
6190 +                return abortOnNullFunction();
6191 +            try {
6192 +                final double id = this.basis;
6193 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6194 +                    do {} while (!casPending(c = pending, c+1));
6195 +                    (rights = new MapReduceMappingsToDoubleTask<K,V>
6196 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6197 +                }
6198 +                double r = id;
6199 +                Object v;
6200 +                while ((v = advance()) != null)
6201 +                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
6202 +                result = r;
6203 +                for (MapReduceMappingsToDoubleTask<K,V> t = this, s;;) {
6204 +                    int c; BulkTask<K,V,?> par;
6205 +                    if ((c = t.pending) == 0) {
6206 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6207 +                            t.result = reducer.apply(t.result, s.result);
6208 +                        }
6209 +                        if ((par = t.parent) == null ||
6210 +                            !(par instanceof MapReduceMappingsToDoubleTask)) {
6211 +                            t.quietlyComplete();
6212 +                            break;
6213 +                        }
6214 +                        t = (MapReduceMappingsToDoubleTask<K,V>)par;
6215 +                    }
6216 +                    else if (t.casPending(c, c - 1))
6217 +                        break;
6218 +                }
6219 +            } catch (Throwable ex) {
6220 +                return tryCompleteComputation(ex);
6221 +            }
6222 +            MapReduceMappingsToDoubleTask<K,V> s = rights;
6223 +            if (s != null && !inForkJoinPool()) {
6224 +                do  {
6225 +                    if (s.tryUnfork())
6226 +                        s.exec();
6227 +                } while ((s = s.nextRight) != null);
6228 +            }
6229 +            return false;
6230 +        }
6231 +        public final Double getRawResult() { return result; }
6232 +    }
6233 +
6234 +    @SuppressWarnings("serial") static final class MapReduceKeysToLongTask<K,V>
6235 +        extends BulkTask<K,V,Long> {
6236 +        final ObjectToLong<? super K> transformer;
6237 +        final LongByLongToLong reducer;
6238 +        final long basis;
6239 +        long result;
6240 +        MapReduceKeysToLongTask<K,V> rights, nextRight;
6241 +        MapReduceKeysToLongTask
6242 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6243 +             MapReduceKeysToLongTask<K,V> nextRight,
6244 +             ObjectToLong<? super K> transformer,
6245 +             long basis,
6246 +             LongByLongToLong reducer) {
6247 +            super(m, p, b); this.nextRight = nextRight;
6248 +            this.transformer = transformer;
6249 +            this.basis = basis; this.reducer = reducer;
6250 +        }
6251 +        @SuppressWarnings("unchecked") public final boolean exec() {
6252 +            final ObjectToLong<? super K> transformer =
6253 +                this.transformer;
6254 +            final LongByLongToLong reducer = this.reducer;
6255 +            if (transformer == null || reducer == null)
6256 +                return abortOnNullFunction();
6257 +            try {
6258 +                final long id = this.basis;
6259 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6260 +                    do {} while (!casPending(c = pending, c+1));
6261 +                    (rights = new MapReduceKeysToLongTask<K,V>
6262 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6263 +                }
6264 +                long r = id;
6265 +                while (advance() != null)
6266 +                    r = reducer.apply(r, transformer.apply((K)nextKey));
6267 +                result = r;
6268 +                for (MapReduceKeysToLongTask<K,V> t = this, s;;) {
6269 +                    int c; BulkTask<K,V,?> par;
6270 +                    if ((c = t.pending) == 0) {
6271 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6272 +                            t.result = reducer.apply(t.result, s.result);
6273 +                        }
6274 +                        if ((par = t.parent) == null ||
6275 +                            !(par instanceof MapReduceKeysToLongTask)) {
6276 +                            t.quietlyComplete();
6277 +                            break;
6278 +                        }
6279 +                        t = (MapReduceKeysToLongTask<K,V>)par;
6280 +                    }
6281 +                    else if (t.casPending(c, c - 1))
6282 +                        break;
6283 +                }
6284 +            } catch (Throwable ex) {
6285 +                return tryCompleteComputation(ex);
6286 +            }
6287 +            MapReduceKeysToLongTask<K,V> s = rights;
6288 +            if (s != null && !inForkJoinPool()) {
6289 +                do  {
6290 +                    if (s.tryUnfork())
6291 +                        s.exec();
6292 +                } while ((s = s.nextRight) != null);
6293 +            }
6294 +            return false;
6295 +        }
6296 +        public final Long getRawResult() { return result; }
6297 +    }
6298 +
6299 +    @SuppressWarnings("serial") static final class MapReduceValuesToLongTask<K,V>
6300 +        extends BulkTask<K,V,Long> {
6301 +        final ObjectToLong<? super V> transformer;
6302 +        final LongByLongToLong reducer;
6303 +        final long basis;
6304 +        long result;
6305 +        MapReduceValuesToLongTask<K,V> rights, nextRight;
6306 +        MapReduceValuesToLongTask
6307 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6308 +             MapReduceValuesToLongTask<K,V> nextRight,
6309 +             ObjectToLong<? super V> transformer,
6310 +             long basis,
6311 +             LongByLongToLong reducer) {
6312 +            super(m, p, b); this.nextRight = nextRight;
6313 +            this.transformer = transformer;
6314 +            this.basis = basis; this.reducer = reducer;
6315 +        }
6316 +        @SuppressWarnings("unchecked") public final boolean exec() {
6317 +            final ObjectToLong<? super V> transformer =
6318 +                this.transformer;
6319 +            final LongByLongToLong reducer = this.reducer;
6320 +            if (transformer == null || reducer == null)
6321 +                return abortOnNullFunction();
6322 +            try {
6323 +                final long id = this.basis;
6324 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6325 +                    do {} while (!casPending(c = pending, c+1));
6326 +                    (rights = new MapReduceValuesToLongTask<K,V>
6327 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6328 +                }
6329 +                long r = id;
6330 +                Object v;
6331 +                while ((v = advance()) != null)
6332 +                    r = reducer.apply(r, transformer.apply((V)v));
6333 +                result = r;
6334 +                for (MapReduceValuesToLongTask<K,V> t = this, s;;) {
6335 +                    int c; BulkTask<K,V,?> par;
6336 +                    if ((c = t.pending) == 0) {
6337 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6338 +                            t.result = reducer.apply(t.result, s.result);
6339 +                        }
6340 +                        if ((par = t.parent) == null ||
6341 +                            !(par instanceof MapReduceValuesToLongTask)) {
6342 +                            t.quietlyComplete();
6343 +                            break;
6344 +                        }
6345 +                        t = (MapReduceValuesToLongTask<K,V>)par;
6346 +                    }
6347 +                    else if (t.casPending(c, c - 1))
6348 +                        break;
6349 +                }
6350 +            } catch (Throwable ex) {
6351 +                return tryCompleteComputation(ex);
6352 +            }
6353 +            MapReduceValuesToLongTask<K,V> s = rights;
6354 +            if (s != null && !inForkJoinPool()) {
6355 +                do  {
6356 +                    if (s.tryUnfork())
6357 +                        s.exec();
6358 +                } while ((s = s.nextRight) != null);
6359 +            }
6360 +            return false;
6361 +        }
6362 +        public final Long getRawResult() { return result; }
6363 +    }
6364 +
6365 +    @SuppressWarnings("serial") static final class MapReduceEntriesToLongTask<K,V>
6366 +        extends BulkTask<K,V,Long> {
6367 +        final ObjectToLong<Map.Entry<K,V>> transformer;
6368 +        final LongByLongToLong reducer;
6369 +        final long basis;
6370 +        long result;
6371 +        MapReduceEntriesToLongTask<K,V> rights, nextRight;
6372 +        MapReduceEntriesToLongTask
6373 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6374 +             MapReduceEntriesToLongTask<K,V> nextRight,
6375 +             ObjectToLong<Map.Entry<K,V>> transformer,
6376 +             long basis,
6377 +             LongByLongToLong reducer) {
6378 +            super(m, p, b); this.nextRight = nextRight;
6379 +            this.transformer = transformer;
6380 +            this.basis = basis; this.reducer = reducer;
6381 +        }
6382 +        @SuppressWarnings("unchecked") public final boolean exec() {
6383 +            final ObjectToLong<Map.Entry<K,V>> transformer =
6384 +                this.transformer;
6385 +            final LongByLongToLong reducer = this.reducer;
6386 +            if (transformer == null || reducer == null)
6387 +                return abortOnNullFunction();
6388 +            try {
6389 +                final long id = this.basis;
6390 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6391 +                    do {} while (!casPending(c = pending, c+1));
6392 +                    (rights = new MapReduceEntriesToLongTask<K,V>
6393 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6394 +                }
6395 +                long r = id;
6396 +                Object v;
6397 +                while ((v = advance()) != null)
6398 +                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
6399 +                result = r;
6400 +                for (MapReduceEntriesToLongTask<K,V> t = this, s;;) {
6401 +                    int c; BulkTask<K,V,?> par;
6402 +                    if ((c = t.pending) == 0) {
6403 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6404 +                            t.result = reducer.apply(t.result, s.result);
6405 +                        }
6406 +                        if ((par = t.parent) == null ||
6407 +                            !(par instanceof MapReduceEntriesToLongTask)) {
6408 +                            t.quietlyComplete();
6409 +                            break;
6410 +                        }
6411 +                        t = (MapReduceEntriesToLongTask<K,V>)par;
6412 +                    }
6413 +                    else if (t.casPending(c, c - 1))
6414 +                        break;
6415 +                }
6416 +            } catch (Throwable ex) {
6417 +                return tryCompleteComputation(ex);
6418 +            }
6419 +            MapReduceEntriesToLongTask<K,V> s = rights;
6420 +            if (s != null && !inForkJoinPool()) {
6421 +                do  {
6422 +                    if (s.tryUnfork())
6423 +                        s.exec();
6424 +                } while ((s = s.nextRight) != null);
6425 +            }
6426 +            return false;
6427 +        }
6428 +        public final Long getRawResult() { return result; }
6429 +    }
6430 +
6431 +    @SuppressWarnings("serial") static final class MapReduceMappingsToLongTask<K,V>
6432 +        extends BulkTask<K,V,Long> {
6433 +        final ObjectByObjectToLong<? super K, ? super V> transformer;
6434 +        final LongByLongToLong reducer;
6435 +        final long basis;
6436 +        long result;
6437 +        MapReduceMappingsToLongTask<K,V> rights, nextRight;
6438 +        MapReduceMappingsToLongTask
6439 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6440 +             MapReduceMappingsToLongTask<K,V> nextRight,
6441 +             ObjectByObjectToLong<? super K, ? super V> transformer,
6442 +             long basis,
6443 +             LongByLongToLong reducer) {
6444 +            super(m, p, b); this.nextRight = nextRight;
6445 +            this.transformer = transformer;
6446 +            this.basis = basis; this.reducer = reducer;
6447 +        }
6448 +        @SuppressWarnings("unchecked") public final boolean exec() {
6449 +            final ObjectByObjectToLong<? super K, ? super V> transformer =
6450 +                this.transformer;
6451 +            final LongByLongToLong reducer = this.reducer;
6452 +            if (transformer == null || reducer == null)
6453 +                return abortOnNullFunction();
6454 +            try {
6455 +                final long id = this.basis;
6456 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6457 +                    do {} while (!casPending(c = pending, c+1));
6458 +                    (rights = new MapReduceMappingsToLongTask<K,V>
6459 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6460 +                }
6461 +                long r = id;
6462 +                Object v;
6463 +                while ((v = advance()) != null)
6464 +                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
6465 +                result = r;
6466 +                for (MapReduceMappingsToLongTask<K,V> t = this, s;;) {
6467 +                    int c; BulkTask<K,V,?> par;
6468 +                    if ((c = t.pending) == 0) {
6469 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6470 +                            t.result = reducer.apply(t.result, s.result);
6471 +                        }
6472 +                        if ((par = t.parent) == null ||
6473 +                            !(par instanceof MapReduceMappingsToLongTask)) {
6474 +                            t.quietlyComplete();
6475 +                            break;
6476 +                        }
6477 +                        t = (MapReduceMappingsToLongTask<K,V>)par;
6478 +                    }
6479 +                    else if (t.casPending(c, c - 1))
6480 +                        break;
6481 +                }
6482 +            } catch (Throwable ex) {
6483 +                return tryCompleteComputation(ex);
6484 +            }
6485 +            MapReduceMappingsToLongTask<K,V> s = rights;
6486 +            if (s != null && !inForkJoinPool()) {
6487 +                do  {
6488 +                    if (s.tryUnfork())
6489 +                        s.exec();
6490 +                } while ((s = s.nextRight) != null);
6491 +            }
6492 +            return false;
6493 +        }
6494 +        public final Long getRawResult() { return result; }
6495 +    }
6496 +
6497 +    @SuppressWarnings("serial") static final class MapReduceKeysToIntTask<K,V>
6498 +        extends BulkTask<K,V,Integer> {
6499 +        final ObjectToInt<? super K> transformer;
6500 +        final IntByIntToInt reducer;
6501 +        final int basis;
6502 +        int result;
6503 +        MapReduceKeysToIntTask<K,V> rights, nextRight;
6504 +        MapReduceKeysToIntTask
6505 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6506 +             MapReduceKeysToIntTask<K,V> nextRight,
6507 +             ObjectToInt<? super K> transformer,
6508 +             int basis,
6509 +             IntByIntToInt reducer) {
6510 +            super(m, p, b); this.nextRight = nextRight;
6511 +            this.transformer = transformer;
6512 +            this.basis = basis; this.reducer = reducer;
6513 +        }
6514 +        @SuppressWarnings("unchecked") public final boolean exec() {
6515 +            final ObjectToInt<? super K> transformer =
6516 +                this.transformer;
6517 +            final IntByIntToInt reducer = this.reducer;
6518 +            if (transformer == null || reducer == null)
6519 +                return abortOnNullFunction();
6520 +            try {
6521 +                final int id = this.basis;
6522 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6523 +                    do {} while (!casPending(c = pending, c+1));
6524 +                    (rights = new MapReduceKeysToIntTask<K,V>
6525 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6526 +                }
6527 +                int r = id;
6528 +                while (advance() != null)
6529 +                    r = reducer.apply(r, transformer.apply((K)nextKey));
6530 +                result = r;
6531 +                for (MapReduceKeysToIntTask<K,V> t = this, s;;) {
6532 +                    int c; BulkTask<K,V,?> par;
6533 +                    if ((c = t.pending) == 0) {
6534 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6535 +                            t.result = reducer.apply(t.result, s.result);
6536 +                        }
6537 +                        if ((par = t.parent) == null ||
6538 +                            !(par instanceof MapReduceKeysToIntTask)) {
6539 +                            t.quietlyComplete();
6540 +                            break;
6541 +                        }
6542 +                        t = (MapReduceKeysToIntTask<K,V>)par;
6543 +                    }
6544 +                    else if (t.casPending(c, c - 1))
6545 +                        break;
6546 +                }
6547 +            } catch (Throwable ex) {
6548 +                return tryCompleteComputation(ex);
6549 +            }
6550 +            MapReduceKeysToIntTask<K,V> s = rights;
6551 +            if (s != null && !inForkJoinPool()) {
6552 +                do  {
6553 +                    if (s.tryUnfork())
6554 +                        s.exec();
6555 +                } while ((s = s.nextRight) != null);
6556 +            }
6557 +            return false;
6558 +        }
6559 +        public final Integer getRawResult() { return result; }
6560 +    }
6561 +
6562 +    @SuppressWarnings("serial") static final class MapReduceValuesToIntTask<K,V>
6563 +        extends BulkTask<K,V,Integer> {
6564 +        final ObjectToInt<? super V> transformer;
6565 +        final IntByIntToInt reducer;
6566 +        final int basis;
6567 +        int result;
6568 +        MapReduceValuesToIntTask<K,V> rights, nextRight;
6569 +        MapReduceValuesToIntTask
6570 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6571 +             MapReduceValuesToIntTask<K,V> nextRight,
6572 +             ObjectToInt<? super V> transformer,
6573 +             int basis,
6574 +             IntByIntToInt reducer) {
6575 +            super(m, p, b); this.nextRight = nextRight;
6576 +            this.transformer = transformer;
6577 +            this.basis = basis; this.reducer = reducer;
6578 +        }
6579 +        @SuppressWarnings("unchecked") public final boolean exec() {
6580 +            final ObjectToInt<? super V> transformer =
6581 +                this.transformer;
6582 +            final IntByIntToInt reducer = this.reducer;
6583 +            if (transformer == null || reducer == null)
6584 +                return abortOnNullFunction();
6585 +            try {
6586 +                final int id = this.basis;
6587 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6588 +                    do {} while (!casPending(c = pending, c+1));
6589 +                    (rights = new MapReduceValuesToIntTask<K,V>
6590 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6591 +                }
6592 +                int r = id;
6593 +                Object v;
6594 +                while ((v = advance()) != null)
6595 +                    r = reducer.apply(r, transformer.apply((V)v));
6596 +                result = r;
6597 +                for (MapReduceValuesToIntTask<K,V> t = this, s;;) {
6598 +                    int c; BulkTask<K,V,?> par;
6599 +                    if ((c = t.pending) == 0) {
6600 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6601 +                            t.result = reducer.apply(t.result, s.result);
6602 +                        }
6603 +                        if ((par = t.parent) == null ||
6604 +                            !(par instanceof MapReduceValuesToIntTask)) {
6605 +                            t.quietlyComplete();
6606 +                            break;
6607 +                        }
6608 +                        t = (MapReduceValuesToIntTask<K,V>)par;
6609 +                    }
6610 +                    else if (t.casPending(c, c - 1))
6611 +                        break;
6612 +                }
6613 +            } catch (Throwable ex) {
6614 +                return tryCompleteComputation(ex);
6615 +            }
6616 +            MapReduceValuesToIntTask<K,V> s = rights;
6617 +            if (s != null && !inForkJoinPool()) {
6618 +                do  {
6619 +                    if (s.tryUnfork())
6620 +                        s.exec();
6621 +                } while ((s = s.nextRight) != null);
6622 +            }
6623 +            return false;
6624 +        }
6625 +        public final Integer getRawResult() { return result; }
6626 +    }
6627 +
6628 +    @SuppressWarnings("serial") static final class MapReduceEntriesToIntTask<K,V>
6629 +        extends BulkTask<K,V,Integer> {
6630 +        final ObjectToInt<Map.Entry<K,V>> transformer;
6631 +        final IntByIntToInt reducer;
6632 +        final int basis;
6633 +        int result;
6634 +        MapReduceEntriesToIntTask<K,V> rights, nextRight;
6635 +        MapReduceEntriesToIntTask
6636 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6637 +             MapReduceEntriesToIntTask<K,V> nextRight,
6638 +             ObjectToInt<Map.Entry<K,V>> transformer,
6639 +             int basis,
6640 +             IntByIntToInt reducer) {
6641 +            super(m, p, b); this.nextRight = nextRight;
6642 +            this.transformer = transformer;
6643 +            this.basis = basis; this.reducer = reducer;
6644 +        }
6645 +        @SuppressWarnings("unchecked") public final boolean exec() {
6646 +            final ObjectToInt<Map.Entry<K,V>> transformer =
6647 +                this.transformer;
6648 +            final IntByIntToInt reducer = this.reducer;
6649 +            if (transformer == null || reducer == null)
6650 +                return abortOnNullFunction();
6651 +            try {
6652 +                final int id = this.basis;
6653 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6654 +                    do {} while (!casPending(c = pending, c+1));
6655 +                    (rights = new MapReduceEntriesToIntTask<K,V>
6656 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6657 +                }
6658 +                int r = id;
6659 +                Object v;
6660 +                while ((v = advance()) != null)
6661 +                    r = reducer.apply(r, transformer.apply(entryFor((K)nextKey, (V)v)));
6662 +                result = r;
6663 +                for (MapReduceEntriesToIntTask<K,V> t = this, s;;) {
6664 +                    int c; BulkTask<K,V,?> par;
6665 +                    if ((c = t.pending) == 0) {
6666 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6667 +                            t.result = reducer.apply(t.result, s.result);
6668 +                        }
6669 +                        if ((par = t.parent) == null ||
6670 +                            !(par instanceof MapReduceEntriesToIntTask)) {
6671 +                            t.quietlyComplete();
6672 +                            break;
6673 +                        }
6674 +                        t = (MapReduceEntriesToIntTask<K,V>)par;
6675 +                    }
6676 +                    else if (t.casPending(c, c - 1))
6677 +                        break;
6678 +                }
6679 +            } catch (Throwable ex) {
6680 +                return tryCompleteComputation(ex);
6681 +            }
6682 +            MapReduceEntriesToIntTask<K,V> s = rights;
6683 +            if (s != null && !inForkJoinPool()) {
6684 +                do  {
6685 +                    if (s.tryUnfork())
6686 +                        s.exec();
6687 +                } while ((s = s.nextRight) != null);
6688 +            }
6689 +            return false;
6690 +        }
6691 +        public final Integer getRawResult() { return result; }
6692 +    }
6693 +
6694 +    @SuppressWarnings("serial") static final class MapReduceMappingsToIntTask<K,V>
6695 +        extends BulkTask<K,V,Integer> {
6696 +        final ObjectByObjectToInt<? super K, ? super V> transformer;
6697 +        final IntByIntToInt reducer;
6698 +        final int basis;
6699 +        int result;
6700 +        MapReduceMappingsToIntTask<K,V> rights, nextRight;
6701 +        MapReduceMappingsToIntTask
6702 +            (ConcurrentHashMapV8<K,V> m, BulkTask<K,V,?> p, int b,
6703 +             MapReduceMappingsToIntTask<K,V> rights,
6704 +             ObjectByObjectToInt<? super K, ? super V> transformer,
6705 +             int basis,
6706 +             IntByIntToInt reducer) {
6707 +            super(m, p, b); this.nextRight = nextRight;
6708 +            this.transformer = transformer;
6709 +            this.basis = basis; this.reducer = reducer;
6710 +        }
6711 +        @SuppressWarnings("unchecked") public final boolean exec() {
6712 +            final ObjectByObjectToInt<? super K, ? super V> transformer =
6713 +                this.transformer;
6714 +            final IntByIntToInt reducer = this.reducer;
6715 +            if (transformer == null || reducer == null)
6716 +                return abortOnNullFunction();
6717 +            try {
6718 +                final int id = this.basis;
6719 +                for (int c, b = batch(); b > 1 && baseIndex != baseLimit;) {
6720 +                    do {} while (!casPending(c = pending, c+1));
6721 +                    (rights = new MapReduceMappingsToIntTask<K,V>
6722 +                     (map, this, b >>>= 1, rights, transformer, id, reducer)).fork();
6723 +                }
6724 +                int r = id;
6725 +                Object v;
6726 +                while ((v = advance()) != null)
6727 +                    r = reducer.apply(r, transformer.apply((K)nextKey, (V)v));
6728 +                result = r;
6729 +                for (MapReduceMappingsToIntTask<K,V> t = this, s;;) {
6730 +                    int c; BulkTask<K,V,?> par;
6731 +                    if ((c = t.pending) == 0) {
6732 +                        for (s = t.rights; s != null; s = t.rights = s.nextRight) {
6733 +                            t.result = reducer.apply(t.result, s.result);
6734 +                        }
6735 +                        if ((par = t.parent) == null ||
6736 +                            !(par instanceof MapReduceMappingsToIntTask)) {
6737 +                            t.quietlyComplete();
6738 +                            break;
6739 +                        }
6740 +                        t = (MapReduceMappingsToIntTask<K,V>)par;
6741 +                    }
6742 +                    else if (t.casPending(c, c - 1))
6743 +                        break;
6744 +                }
6745 +            } catch (Throwable ex) {
6746 +                return tryCompleteComputation(ex);
6747 +            }
6748 +            MapReduceMappingsToIntTask<K,V> s = rights;
6749 +            if (s != null && !inForkJoinPool()) {
6750 +                do  {
6751 +                    if (s.tryUnfork())
6752 +                        s.exec();
6753 +                } while ((s = s.nextRight) != null);
6754 +            }
6755 +            return false;
6756 +        }
6757 +        public final Integer getRawResult() { return result; }
6758 +    }
6759 +
6760 +
6761      // Unsafe mechanics
6762      private static final sun.misc.Unsafe UNSAFE;
6763      private static final long counterOffset;
# Line 3277 | Line 6812 | public class ConcurrentHashMapV8<K, V>
6812              }
6813          }
6814      }
3280
6815   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines