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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines