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.40 by jsr166, Sat Jun 9 17:02:07 2012 UTC vs.
Revision 1.59 by dl, Tue Aug 14 13:16:50 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 +
12 + import java.util.Comparator;
13   import java.util.Arrays;
14   import java.util.Map;
15   import java.util.Set;
# Line 25 | Line 27 | import java.util.concurrent.ConcurrentMa
27   import java.util.concurrent.ThreadLocalRandom;
28   import java.util.concurrent.locks.LockSupport;
29   import java.util.concurrent.locks.AbstractQueuedSynchronizer;
30 + import java.util.concurrent.atomic.AtomicReference;
31 +
32   import java.io.Serializable;
33  
34   /**
# Line 43 | Line 47 | import java.io.Serializable;
47   * block, so may overlap with update operations (including {@code put}
48   * and {@code remove}). Retrievals reflect the results of the most
49   * recently <em>completed</em> update operations holding upon their
50 < * onset.  For aggregate operations such as {@code putAll} and {@code
51 < * clear}, concurrent retrievals may reflect insertion or removal of
52 < * only some entries.  Similarly, Iterators and Enumerations return
53 < * elements reflecting the state of the hash table at some point at or
54 < * since the creation of the iterator/enumeration.  They do
55 < * <em>not</em> throw {@link ConcurrentModificationException}.
56 < * However, iterators are designed to be used by only one thread at a
57 < * time.  Bear in mind that the results of aggregate status methods
58 < * including {@code size}, {@code isEmpty}, and {@code containsValue}
59 < * are typically useful only when a map is not undergoing concurrent
60 < * updates in other threads.  Otherwise the results of these methods
61 < * reflect transient states that may be adequate for monitoring
62 < * or estimation purposes, but not for program control.
50 > * onset. (More formally, an update operation for a given key bears a
51 > * <em>happens-before</em> relation with any (non-null) retrieval for
52 > * that key reporting the updated value.)  For aggregate operations
53 > * such as {@code putAll} and {@code clear}, concurrent retrievals may
54 > * reflect insertion or removal of only some entries.  Similarly,
55 > * Iterators and Enumerations return elements reflecting the state of
56 > * the hash table at some point at or since the creation of the
57 > * iterator/enumeration.  They do <em>not</em> throw {@link
58 > * ConcurrentModificationException}.  However, iterators are designed
59 > * to be used by only one thread at a time.  Bear in mind that the
60 > * results of aggregate status methods including {@code size}, {@code
61 > * isEmpty}, and {@code containsValue} are typically useful only when
62 > * a map is not undergoing concurrent updates in other threads.
63 > * Otherwise the results of these methods reflect transient states
64 > * that may be adequate for monitoring or estimation purposes, but not
65 > * for program control.
66   *
67   * <p> The table is dynamically expanded when there are too many
68   * collisions (i.e., keys that have distinct hash codes but fall into
# Line 90 | Line 97 | import java.io.Serializable;
97   * Java Collections Framework</a>.
98   *
99   * <p><em>jsr166e note: This class is a candidate replacement for
100 < * java.util.concurrent.ConcurrentHashMap.<em>
100 > * java.util.concurrent.ConcurrentHashMap.  During transition, this
101 > * class declares and uses nested functional interfaces with different
102 > * names but the same forms as those expected for JDK8.<em>
103   *
104   * @since 1.5
105   * @author Doug Lea
# Line 98 | Line 107 | import java.io.Serializable;
107   * @param <V> the type of mapped values
108   */
109   public class ConcurrentHashMapV8<K, V>
110 <        implements ConcurrentMap<K, V>, Serializable {
110 >    implements ConcurrentMap<K, V>, Serializable {
111      private static final long serialVersionUID = 7249069246763182397L;
112  
113      /**
114 <     * A function computing a mapping from the given key to a value.
115 <     * This is a place-holder for an upcoming JDK8 interface.
116 <     */
117 <    public static interface MappingFunction<K, V> {
118 <        /**
119 <         * Returns a non-null value for the given key.
120 <         *
121 <         * @param key the (non-null) key
122 <         * @return a non-null value
123 <         */
124 <        V map(K key);
125 <    }
126 <
127 <    /**
128 <     * A function computing a new mapping given a key and its current
129 <     * mapped value (or {@code null} if there is no current
130 <     * mapping). This is a place-holder for an upcoming JDK8
131 <     * interface.
114 >     * A partitionable iterator. A Spliterator can be traversed
115 >     * directly, but can also be partitioned (before traversal) by
116 >     * creating another Spliterator that covers a non-overlapping
117 >     * portion of the elements, and so may be amenable to parallel
118 >     * execution.
119 >     *
120 >     * <p> This interface exports a subset of expected JDK8
121 >     * functionality.
122 >     *
123 >     * <p>Sample usage: Here is one (of the several) ways to compute
124 >     * the sum of the values held in a map using the ForkJoin
125 >     * framework. As illustrated here, Spliterators are well suited to
126 >     * designs in which a task repeatedly splits off half its work
127 >     * into forked subtasks until small enough to process directly,
128 >     * and then joins these subtasks. Variants of this style can also
129 >     * be used in completion-based designs.
130 >     *
131 >     * <pre>
132 >     * {@code ConcurrentHashMapV8<String, Long> m = ...
133 >     * // split as if have 8 * parallelism, for load balance
134 >     * int n = m.size();
135 >     * int p = aForkJoinPool.getParallelism() * 8;
136 >     * int split = (n < p)? n : p;
137 >     * long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null));
138 >     * // ...
139 >     * static class SumValues extends RecursiveTask<Long> {
140 >     *   final Spliterator<Long> s;
141 >     *   final int split;             // split while > 1
142 >     *   final SumValues nextJoin;    // records forked subtasks to join
143 >     *   SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
144 >     *     this.s = s; this.depth = depth; this.nextJoin = nextJoin;
145 >     *   }
146 >     *   public Long compute() {
147 >     *     long sum = 0;
148 >     *     SumValues subtasks = null; // fork subtasks
149 >     *     for (int s = split >>> 1; s > 0; s >>>= 1)
150 >     *       (subtasks = new SumValues(s.split(), s, subtasks)).fork();
151 >     *     while (s.hasNext())        // directly process remaining elements
152 >     *       sum += s.next();
153 >     *     for (SumValues t = subtasks; t != null; t = t.nextJoin)
154 >     *       sum += t.join();         // collect subtask results
155 >     *     return sum;
156 >     *   }
157 >     * }
158 >     * }</pre>
159       */
160 <    public static interface RemappingFunction<K, V> {
160 >    public static interface Spliterator<T> extends Iterator<T> {
161          /**
162 <         * Returns a new value given a key and its current value.
162 >         * Returns a Spliterator covering approximately half of the
163 >         * elements, guaranteed not to overlap with those subsequently
164 >         * returned by this Spliterator.  After invoking this method,
165 >         * the current Spliterator will <em>not</em> produce any of
166 >         * the elements of the returned Spliterator, but the two
167 >         * Spliterators together will produce all of the elements that
168 >         * would have been produced by this Spliterator had this
169 >         * method not been called. The exact number of elements
170 >         * produced by the returned Spliterator is not guaranteed, and
171 >         * may be zero (i.e., with {@code hasNext()} reporting {@code
172 >         * false}) if this Spliterator cannot be further split.
173           *
174 <         * @param key the (non-null) key
175 <         * @param value the current value, or null if there is no mapping
176 <         * @return a non-null value
174 >         * @return a Spliterator covering approximately half of the
175 >         * elements
176 >         * @throws IllegalStateException if this Spliterator has
177 >         * already commenced traversing elements
178           */
179 <        V remap(K key, V value);
179 >        Spliterator<T> split();
180      }
181  
182      /*
# Line 283 | Line 330 | public class ConcurrentHashMapV8<K, V>
330       * When there are no lock acquisition failures, this is arranged
331       * simply by proceeding from the last bin (table.length - 1) up
332       * towards the first.  Upon seeing a forwarding node, traversals
333 <     * (see class InternalIterator) arrange to move to the new table
333 >     * (see class Iter) arrange to move to the new table
334       * without revisiting nodes.  However, when any node is skipped
335       * during a transfer, all earlier table bins may have become
336       * visible, so are initialized with a reverse-forwarding node back
# Line 293 | Line 340 | public class ConcurrentHashMapV8<K, V>
340       * mechanics trigger only when necessary.
341       *
342       * The traversal scheme also applies to partial traversals of
343 <     * ranges of bins (via an alternate InternalIterator constructor)
344 <     * to support partitioned aggregate operations (that are not
345 <     * otherwise implemented yet).  Also, read-only operations give up
346 <     * if ever forwarded to a null table, which provides support for
347 <     * shutdown-style clearing, which is also not currently
301 <     * implemented.
343 >     * ranges of bins (via an alternate Traverser constructor)
344 >     * to support partitioned aggregate operations.  Also, read-only
345 >     * operations give up if ever forwarded to a null table, which
346 >     * provides support for shutdown-style clearing, which is also not
347 >     * currently implemented.
348       *
349       * Lazy table initialization minimizes footprint until first use,
350       * and also avoids resizings when the first operation is from a
# Line 436 | Line 482 | public class ConcurrentHashMapV8<K, V>
482       * inline assignments below.
483       */
484  
485 <    static final Node tabAt(Node[] tab, int i) { // used by InternalIterator
485 >    static final Node tabAt(Node[] tab, int i) { // used by Iter
486          return (Node)UNSAFE.getObjectVolatile(tab, ((long)i<<ASHIFT)+ABASE);
487      }
488  
# Line 452 | Line 498 | public class ConcurrentHashMapV8<K, V>
498  
499      /**
500       * Key-value entry. Note that this is never exported out as a
501 <     * user-visible Map.Entry (see WriteThroughEntry and SnapshotEntry
502 <     * below). Nodes with a hash field of MOVED are special, and do
503 <     * not contain user keys or values.  Otherwise, keys are never
504 <     * null, and null val fields indicate that a node is in the
505 <     * process of being deleted or created. For purposes of read-only
506 <     * access, a key may be read before a val, but can only be used
507 <     * after checking val to be non-null.
501 >     * user-visible Map.Entry (see MapEntry below). Nodes with a hash
502 >     * field of MOVED are special, and do not contain user keys or
503 >     * values.  Otherwise, keys are never null, and null val fields
504 >     * indicate that a node is in the process of being deleted or
505 >     * created. For purposes of read-only access, a key may be read
506 >     * before a val, but can only be used after checking val to be
507 >     * non-null.
508       */
509      static class Node {
510          volatile int hash;
# Line 571 | Line 617 | public class ConcurrentHashMapV8<K, V>
617       * handle this, the tree is ordered primarily by hash value, then
618       * by getClass().getName() order, and then by Comparator order
619       * among elements of the same class.  On lookup at a node, if
620 <     * non-Comparable, both left and right children may need to be
621 <     * searched in the case of tied hash values. (This corresponds to
622 <     * the full list search that would be necessary if all elements
623 <     * were non-Comparable and had tied hashes.)
620 >     * elements are not comparable or compare as 0, both left and
621 >     * right children may need to be searched in the case of tied hash
622 >     * values. (This corresponds to the full list search that would be
623 >     * necessary if all elements were non-Comparable and had tied
624 >     * hashes.)  The red-black balancing code is updated from
625 >     * pre-jdk-collections
626 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
627 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
628 >     * Algorithms" (CLR).
629       *
630       * TreeBins also maintain a separate locking discipline than
631       * regular bins. Because they are forwarded via special MOVED
632       * nodes at bin heads (which can never change once established),
633 <     * we cannot use use those nodes as locks. Instead, TreeBin
633 >     * we cannot use those nodes as locks. Instead, TreeBin
634       * extends AbstractQueuedSynchronizer to support a simple form of
635       * read-write lock. For update operations and table validation,
636       * the exclusive form of lock behaves in the same way as bin-head
# Line 598 | Line 649 | public class ConcurrentHashMapV8<K, V>
649       */
650      static final class TreeBin extends AbstractQueuedSynchronizer {
651          private static final long serialVersionUID = 2249069246763182397L;
652 <        TreeNode root;  // root of tree
653 <        TreeNode first; // head of next-pointer list
652 >        transient TreeNode root;  // root of tree
653 >        transient TreeNode first; // head of next-pointer list
654  
655          /* AQS overrides */
656          public final boolean isHeldExclusively() { return getState() > 0; }
# Line 629 | Line 680 | public class ConcurrentHashMapV8<K, V>
680              return c == -1;
681          }
682  
683 +        /** From CLR */
684 +        private void rotateLeft(TreeNode p) {
685 +            if (p != null) {
686 +                TreeNode r = p.right, pp, rl;
687 +                if ((rl = p.right = r.left) != null)
688 +                    rl.parent = p;
689 +                if ((pp = r.parent = p.parent) == null)
690 +                    root = r;
691 +                else if (pp.left == p)
692 +                    pp.left = r;
693 +                else
694 +                    pp.right = r;
695 +                r.left = p;
696 +                p.parent = r;
697 +            }
698 +        }
699 +
700 +        /** From CLR */
701 +        private void rotateRight(TreeNode p) {
702 +            if (p != null) {
703 +                TreeNode l = p.left, pp, lr;
704 +                if ((lr = p.left = l.right) != null)
705 +                    lr.parent = p;
706 +                if ((pp = l.parent = p.parent) == null)
707 +                    root = l;
708 +                else if (pp.right == p)
709 +                    pp.right = l;
710 +                else
711 +                    pp.left = l;
712 +                l.right = p;
713 +                p.parent = l;
714 +            }
715 +        }
716 +
717          /**
718 <         * Return the TreeNode (or null if not found) for the given key
718 >         * Returns the TreeNode (or null if not found) for the given key
719           * starting at given root.
720           */
721          @SuppressWarnings("unchecked") // suppress Comparable cast warning
722          final TreeNode getTreeNode(int h, Object k, TreeNode p) {
723              Class<?> c = k.getClass();
724              while (p != null) {
725 <                int dir, ph;  Object pk; Class<?> pc; TreeNode r;
726 <                if (h < (ph = p.hash))
727 <                    dir = -1;
728 <                else if (h > ph)
729 <                    dir = 1;
730 <                else if ((pk = p.key) == k || k.equals(pk))
731 <                    return p;
732 <                else if (c != (pc = pk.getClass()))
733 <                    dir = c.getName().compareTo(pc.getName());
734 <                else if (k instanceof Comparable)
735 <                    dir = ((Comparable)k).compareTo((Comparable)pk);
736 <                else
737 <                    dir = 0;
738 <                TreeNode pr = p.right;
739 <                if (dir > 0)
740 <                    p = pr;
741 <                else if (dir == 0 && pr != null && h >= pr.hash &&
742 <                         (r = getTreeNode(h, k, pr)) != null)
743 <                    return r;
725 >                int dir, ph;  Object pk; Class<?> pc;
726 >                if ((ph = p.hash) == h) {
727 >                    if ((pk = p.key) == k || k.equals(pk))
728 >                        return p;
729 >                    if (c != (pc = pk.getClass()) ||
730 >                        !(k instanceof Comparable) ||
731 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
732 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
733 >                        TreeNode r = null, s = null, pl, pr;
734 >                        if (dir >= 0) {
735 >                            if ((pl = p.left) != null && h <= pl.hash)
736 >                                s = pl;
737 >                        }
738 >                        else if ((pr = p.right) != null && h >= pr.hash)
739 >                            s = pr;
740 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
741 >                            return r;
742 >                    }
743 >                }
744                  else
745 <                    p = p.left;
745 >                    dir = (h < ph) ? -1 : 1;
746 >                p = (dir > 0) ? p.right : p.left;
747              }
748              return null;
749          }
# Line 690 | Line 776 | public class ConcurrentHashMapV8<K, V>
776          }
777  
778          /**
779 <         * Find or add a node
779 >         * Finds or adds a node.
780           * @return null if added
781           */
782          @SuppressWarnings("unchecked") // suppress Comparable cast warning
783          final TreeNode putTreeNode(int h, Object k, Object v) {
784              Class<?> c = k.getClass();
785 <            TreeNode p = root;
785 >            TreeNode pp = root, p = null;
786              int dir = 0;
787 <            if (p != null) {
788 <                for (;;) {
789 <                    int ph;  Object pk; Class<?> pc; TreeNode r;
790 <                    if (h < (ph = p.hash))
791 <                        dir = -1;
706 <                    else if (h > ph)
707 <                        dir = 1;
708 <                    else if ((pk = p.key) == k || k.equals(pk))
787 >            while (pp != null) { // find existing node or leaf to insert at
788 >                int ph;  Object pk; Class<?> pc;
789 >                p = pp;
790 >                if ((ph = p.hash) == h) {
791 >                    if ((pk = p.key) == k || k.equals(pk))
792                          return p;
793 <                    else if (c != (pc = (pk = p.key).getClass()))
794 <                        dir = c.getName().compareTo(pc.getName());
795 <                    else if (k instanceof Comparable)
796 <                        dir = ((Comparable)k).compareTo((Comparable)pk);
797 <                    else
798 <                        dir = 0;
799 <                    TreeNode pr = p.right, pl;
800 <                    if (dir > 0) {
801 <                        if (pr == null)
802 <                            break;
803 <                        p = pr;
793 >                    if (c != (pc = pk.getClass()) ||
794 >                        !(k instanceof Comparable) ||
795 >                        (dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) {
796 >                        dir = (c == pc) ? 0 : c.getName().compareTo(pc.getName());
797 >                        TreeNode r = null, s = null, pl, pr;
798 >                        if (dir >= 0) {
799 >                            if ((pl = p.left) != null && h <= pl.hash)
800 >                                s = pl;
801 >                        }
802 >                        else if ((pr = p.right) != null && h >= pr.hash)
803 >                            s = pr;
804 >                        if (s != null && (r = getTreeNode(h, k, s)) != null)
805 >                            return r;
806                      }
722                    else if (dir == 0 && pr != null && h >= pr.hash &&
723                             (r = getTreeNode(h, k, pr)) != null)
724                        return r;
725                    else if ((pl = p.left) == null)
726                        break;
727                    else
728                        p = pl;
807                  }
808 +                else
809 +                    dir = (h < ph) ? -1 : 1;
810 +                pp = (dir > 0) ? p.right : p.left;
811              }
812 +
813              TreeNode f = first;
814 <            TreeNode r = first = new TreeNode(h, k, v, f, p);
814 >            TreeNode x = first = new TreeNode(h, k, v, f, p);
815              if (p == null)
816 <                root = r;
817 <            else {
816 >                root = x;
817 >            else { // attach and rebalance; adapted from CLR
818 >                TreeNode xp, xpp;
819 >                if (f != null)
820 >                    f.prev = x;
821                  if (dir <= 0)
822 <                    p.left = r;
822 >                    p.left = x;
823                  else
824 <                    p.right = r;
825 <                if (f != null)
826 <                    f.prev = r;
827 <                fixAfterInsertion(r);
824 >                    p.right = x;
825 >                x.red = true;
826 >                while (x != null && (xp = x.parent) != null && xp.red &&
827 >                       (xpp = xp.parent) != null) {
828 >                    TreeNode xppl = xpp.left;
829 >                    if (xp == xppl) {
830 >                        TreeNode y = xpp.right;
831 >                        if (y != null && y.red) {
832 >                            y.red = false;
833 >                            xp.red = false;
834 >                            xpp.red = true;
835 >                            x = xpp;
836 >                        }
837 >                        else {
838 >                            if (x == xp.right) {
839 >                                rotateLeft(x = xp);
840 >                                xpp = (xp = x.parent) == null ? null : xp.parent;
841 >                            }
842 >                            if (xp != null) {
843 >                                xp.red = false;
844 >                                if (xpp != null) {
845 >                                    xpp.red = true;
846 >                                    rotateRight(xpp);
847 >                                }
848 >                            }
849 >                        }
850 >                    }
851 >                    else {
852 >                        TreeNode y = xppl;
853 >                        if (y != null && y.red) {
854 >                            y.red = false;
855 >                            xp.red = false;
856 >                            xpp.red = true;
857 >                            x = xpp;
858 >                        }
859 >                        else {
860 >                            if (x == xp.left) {
861 >                                rotateRight(x = xp);
862 >                                xpp = (xp = x.parent) == null ? null : xp.parent;
863 >                            }
864 >                            if (xp != null) {
865 >                                xp.red = false;
866 >                                if (xpp != null) {
867 >                                    xpp.red = true;
868 >                                    rotateLeft(xpp);
869 >                                }
870 >                            }
871 >                        }
872 >                    }
873 >                }
874 >                TreeNode r = root;
875 >                if (r != null && r.red)
876 >                    r.red = false;
877              }
878              return null;
879          }
# Line 765 | Line 899 | public class ConcurrentHashMapV8<K, V>
899              TreeNode pl = p.left;
900              TreeNode pr = p.right;
901              if (pl != null && pr != null) {
902 <                TreeNode s = pr;
903 <                while (s.left != null) // find successor
904 <                    s = s.left;
902 >                TreeNode s = pr, sl;
903 >                while ((sl = s.left) != null) // find successor
904 >                    s = sl;
905                  boolean c = s.red; s.red = p.red; p.red = c; // swap colors
906                  TreeNode sr = s.right;
907                  TreeNode pp = p.parent;
# Line 819 | Line 953 | public class ConcurrentHashMapV8<K, V>
953                      pp.right = replacement;
954                  p.left = p.right = p.parent = null;
955              }
956 <            if (!p.red)
957 <                fixAfterDeletion(replacement);
958 <            if (p == replacement && (pp = p.parent) != null) {
959 <                if (p == pp.left) // detach pointers
960 <                    pp.left = null;
961 <                else if (p == pp.right)
962 <                    pp.right = null;
829 <                p.parent = null;
830 <            }
831 <        }
832 <
833 <        // CLR code updated from pre-jdk-collections version at
834 <        // http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java
835 <
836 <        /** From CLR */
837 <        private void rotateLeft(TreeNode p) {
838 <            if (p != null) {
839 <                TreeNode r = p.right, pp, rl;
840 <                if ((rl = p.right = r.left) != null)
841 <                    rl.parent = p;
842 <                if ((pp = r.parent = p.parent) == null)
843 <                    root = r;
844 <                else if (pp.left == p)
845 <                    pp.left = r;
846 <                else
847 <                    pp.right = r;
848 <                r.left = p;
849 <                p.parent = r;
850 <            }
851 <        }
852 <
853 <        /** From CLR */
854 <        private void rotateRight(TreeNode p) {
855 <            if (p != null) {
856 <                TreeNode l = p.left, pp, lr;
857 <                if ((lr = p.left = l.right) != null)
858 <                    lr.parent = p;
859 <                if ((pp = l.parent = p.parent) == null)
860 <                    root = l;
861 <                else if (pp.right == p)
862 <                    pp.right = l;
863 <                else
864 <                    pp.left = l;
865 <                l.right = p;
866 <                p.parent = l;
867 <            }
868 <        }
869 <
870 <        /** From CLR */
871 <        private void fixAfterInsertion(TreeNode x) {
872 <            x.red = true;
873 <            TreeNode xp, xpp;
874 <            while (x != null && (xp = x.parent) != null && xp.red &&
875 <                   (xpp = xp.parent) != null) {
876 <                TreeNode xppl = xpp.left;
877 <                if (xp == xppl) {
878 <                    TreeNode y = xpp.right;
879 <                    if (y != null && y.red) {
880 <                        y.red = false;
881 <                        xp.red = false;
882 <                        xpp.red = true;
883 <                        x = xpp;
884 <                    }
885 <                    else {
886 <                        if (x == xp.right) {
887 <                            x = xp;
888 <                            rotateLeft(x);
889 <                            xpp = (xp = x.parent) == null ? null : xp.parent;
890 <                        }
891 <                        if (xp != null) {
892 <                            xp.red = false;
893 <                            if (xpp != null) {
894 <                                xpp.red = true;
895 <                                rotateRight(xpp);
896 <                            }
897 <                        }
898 <                    }
899 <                }
900 <                else {
901 <                    TreeNode y = xppl;
902 <                    if (y != null && y.red) {
903 <                        y.red = false;
904 <                        xp.red = false;
905 <                        xpp.red = true;
906 <                        x = xpp;
956 >            if (!p.red) { // rebalance, from CLR
957 >                TreeNode x = replacement;
958 >                while (x != null) {
959 >                    TreeNode xp, xpl;
960 >                    if (x.red || (xp = x.parent) == null) {
961 >                        x.red = false;
962 >                        break;
963                      }
964 <                    else {
965 <                        if (x == xp.left) {
966 <                            x = xp;
967 <                            rotateRight(x);
968 <                            xpp = (xp = x.parent) == null ? null : xp.parent;
964 >                    if (x == (xpl = xp.left)) {
965 >                        TreeNode sib = xp.right;
966 >                        if (sib != null && sib.red) {
967 >                            sib.red = false;
968 >                            xp.red = true;
969 >                            rotateLeft(xp);
970 >                            sib = (xp = x.parent) == null ? null : xp.right;
971                          }
972 <                        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;
972 >                        if (sib == null)
973                              x = xp;
953                        }
974                          else {
975 <                            if (sr == null || !sr.red) {
976 <                                if (sl != null)
977 <                                    sl.red = false;
975 >                            TreeNode sl = sib.left, sr = sib.right;
976 >                            if ((sr == null || !sr.red) &&
977 >                                (sl == null || !sl.red)) {
978                                  sib.red = true;
979 <                                rotateRight(sib);
960 <                                sib = (xp = x.parent) == null ? null : xp.right;
979 >                                x = xp;
980                              }
981 <                            if (sib != null) {
982 <                                sib.red = (xp == null) ? false : xp.red;
983 <                                if ((sr = sib.right) != null)
984 <                                    sr.red = false;
985 <                            }
986 <                            if (xp != null) {
987 <                                xp.red = false;
988 <                                rotateLeft(xp);
981 >                            else {
982 >                                if (sr == null || !sr.red) {
983 >                                    if (sl != null)
984 >                                        sl.red = false;
985 >                                    sib.red = true;
986 >                                    rotateRight(sib);
987 >                                    sib = (xp = x.parent) == null ? null : xp.right;
988 >                                }
989 >                                if (sib != null) {
990 >                                    sib.red = (xp == null) ? false : xp.red;
991 >                                    if ((sr = sib.right) != null)
992 >                                        sr.red = false;
993 >                                }
994 >                                if (xp != null) {
995 >                                    xp.red = false;
996 >                                    rotateLeft(xp);
997 >                                }
998 >                                x = root;
999                              }
971                            x = root;
1000                          }
1001                      }
1002 <                }
1003 <                else { // symmetric
1004 <                    TreeNode sib = xpl;
1005 <                    if (sib != null && sib.red) {
1006 <                        sib.red = false;
1007 <                        xp.red = true;
1008 <                        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;
1002 >                    else { // symmetric
1003 >                        TreeNode sib = xpl;
1004 >                        if (sib != null && sib.red) {
1005 >                            sib.red = false;
1006 >                            xp.red = true;
1007 >                            rotateRight(xp);
1008 >                            sib = (xp = x.parent) == null ? null : xp.left;
1009                          }
1010 +                        if (sib == null)
1011 +                            x = xp;
1012                          else {
1013 <                            if (sl == null || !sl.red) {
1014 <                                if (sr != null)
1015 <                                    sr.red = false;
1013 >                            TreeNode sl = sib.left, sr = sib.right;
1014 >                            if ((sl == null || !sl.red) &&
1015 >                                (sr == null || !sr.red)) {
1016                                  sib.red = true;
1017 <                                rotateLeft(sib);
998 <                                sib = (xp = x.parent) == null ? null : xp.left;
999 <                            }
1000 <                            if (sib != null) {
1001 <                                sib.red = (xp == null) ? false : xp.red;
1002 <                                if ((sl = sib.left) != null)
1003 <                                    sl.red = false;
1017 >                                x = xp;
1018                              }
1019 <                            if (xp != null) {
1020 <                                xp.red = false;
1021 <                                rotateRight(xp);
1019 >                            else {
1020 >                                if (sl == null || !sl.red) {
1021 >                                    if (sr != null)
1022 >                                        sr.red = false;
1023 >                                    sib.red = true;
1024 >                                    rotateLeft(sib);
1025 >                                    sib = (xp = x.parent) == null ? null : xp.left;
1026 >                                }
1027 >                                if (sib != null) {
1028 >                                    sib.red = (xp == null) ? false : xp.red;
1029 >                                    if ((sl = sib.left) != null)
1030 >                                        sl.red = false;
1031 >                                }
1032 >                                if (xp != null) {
1033 >                                    xp.red = false;
1034 >                                    rotateRight(xp);
1035 >                                }
1036 >                                x = root;
1037                              }
1009                            x = root;
1038                          }
1039                      }
1040                  }
1041              }
1042 +            if (p == replacement && (pp = p.parent) != null) {
1043 +                if (p == pp.left) // detach pointers
1044 +                    pp.left = null;
1045 +                else if (p == pp.right)
1046 +                    pp.right = null;
1047 +                p.parent = null;
1048 +            }
1049          }
1050      }
1051  
# Line 1172 | Line 1207 | public class ConcurrentHashMapV8<K, V>
1207      }
1208  
1209      /*
1210 <     * Internal versions of the five insertion methods, each a
1210 >     * Internal versions of the six insertion methods, each a
1211       * little more complicated than the last. All have
1212       * the same basic structure as the first (internalPut):
1213       *  1. If table uninitialized, create
# Line 1190 | Line 1225 | public class ConcurrentHashMapV8<K, V>
1225       *    returns from function call.
1226       *  * compute uses the same function-call mechanics, but without
1227       *    the prescans
1228 +     *  * merge acts as putIfAbsent in the absent case, but invokes the
1229 +     *    update function if present
1230       *  * putAll attempts to pre-allocate enough table space
1231       *    and more lazily performs count updates and checks.
1232       *
# Line 1386 | Line 1423 | public class ConcurrentHashMapV8<K, V>
1423  
1424      /** Implementation for computeIfAbsent */
1425      private final Object internalComputeIfAbsent(K k,
1426 <                                                 MappingFunction<? super K, ?> mf) {
1426 >                                                 Fun<? super K, ?> mf) {
1427          int h = spread(k.hashCode());
1428          Object val = null;
1429          int count = 0;
# Line 1399 | Line 1436 | public class ConcurrentHashMapV8<K, V>
1436                  if (casTabAt(tab, i, null, node)) {
1437                      count = 1;
1438                      try {
1439 <                        if ((val = mf.map(k)) != null)
1439 >                        if ((val = mf.apply(k)) != null)
1440                              node.val = val;
1441                      } finally {
1442                          if (val == null)
# Line 1424 | Line 1461 | public class ConcurrentHashMapV8<K, V>
1461                              TreeNode p = t.getTreeNode(h, k, t.root);
1462                              if (p != null)
1463                                  val = p.val;
1464 <                            else if ((val = mf.map(k)) != null) {
1464 >                            else if ((val = mf.apply(k)) != null) {
1465                                  added = true;
1466                                  count = 2;
1467                                  t.putTreeNode(h, k, val);
# Line 1478 | Line 1515 | public class ConcurrentHashMapV8<K, V>
1515                                  }
1516                                  Node last = e;
1517                                  if ((e = e.next) == null) {
1518 <                                    if ((val = mf.map(k)) != null) {
1518 >                                    if ((val = mf.apply(k)) != null) {
1519                                          added = true;
1520                                          last.next = new Node(h, k, val, null);
1521                                          if (count >= TREE_THRESHOLD)
# Line 1504 | Line 1541 | public class ConcurrentHashMapV8<K, V>
1541                  }
1542              }
1543          }
1544 <        if (val == null)
1545 <            throw new NullPointerException();
1546 <        counter.add(1L);
1547 <        if (count > 1)
1548 <            checkForResize();
1544 >        if (val != null) {
1545 >            counter.add(1L);
1546 >            if (count > 1)
1547 >                checkForResize();
1548 >        }
1549          return val;
1550      }
1551  
1552      /** Implementation for compute */
1553      @SuppressWarnings("unchecked")
1554 <    private final Object internalCompute(K k,
1555 <                                         RemappingFunction<? super K, V> mf) {
1554 >    private final Object internalCompute(K k, boolean onlyIfPresent,
1555 >                                             BiFun<? super K, ? super V, ? extends V> mf) {
1556          int h = spread(k.hashCode());
1557          Object val = null;
1558 <        boolean added = false;
1558 >        int delta = 0;
1559          int count = 0;
1560          for (Node[] tab = table;;) {
1561              Node f; int i, fh; Object fk;
1562              if (tab == null)
1563                  tab = initTable();
1564              else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1565 +                if (onlyIfPresent)
1566 +                    break;
1567                  Node node = new Node(fh = h | LOCKED, k, null, null);
1568                  if (casTabAt(tab, i, null, node)) {
1569                      try {
1570                          count = 1;
1571 <                        if ((val = mf.remap(k, null)) != null) {
1571 >                        if ((val = mf.apply(k, null)) != null) {
1572                              node.val = val;
1573 <                            added = true;
1573 >                            delta = 1;
1574                          }
1575                      } finally {
1576 <                        if (!added)
1576 >                        if (delta == 0)
1577                              setTabAt(tab, i, null);
1578                          if (!node.casHash(fh, h)) {
1579                              node.hash = h;
# Line 1554 | Line 1593 | public class ConcurrentHashMapV8<K, V>
1593                              count = 1;
1594                              TreeNode p = t.getTreeNode(h, k, t.root);
1595                              Object pv = (p == null) ? null : p.val;
1596 <                            if ((val = mf.remap(k, (V)pv)) != null) {
1596 >                            if ((val = mf.apply(k, (V)pv)) != null) {
1597                                  if (p != null)
1598                                      p.val = val;
1599                                  else {
1600                                      count = 2;
1601 <                                    added = true;
1601 >                                    delta = 1;
1602                                      t.putTreeNode(h, k, val);
1603                                  }
1604                              }
1605 +                            else if (p != null) {
1606 +                                delta = -1;
1607 +                                t.deleteTreeNode(p);
1608 +                            }
1609                          }
1610                      } finally {
1611                          t.release(0);
# Line 1581 | Line 1624 | public class ConcurrentHashMapV8<K, V>
1624                  try {
1625                      if (tabAt(tab, i) == f) {
1626                          count = 1;
1627 <                        for (Node e = f;; ++count) {
1627 >                        for (Node e = f, pred = null;; ++count) {
1628                              Object ek, ev;
1629                              if ((e.hash & HASH_BITS) == h &&
1630                                  (ev = e.val) != null &&
1631                                  ((ek = e.key) == k || k.equals(ek))) {
1632 <                                val = mf.remap(k, (V)ev);
1632 >                                val = mf.apply(k, (V)ev);
1633                                  if (val != null)
1634                                      e.val = val;
1635 +                                else {
1636 +                                    delta = -1;
1637 +                                    Node en = e.next;
1638 +                                    if (pred != null)
1639 +                                        pred.next = en;
1640 +                                    else
1641 +                                        setTabAt(tab, i, en);
1642 +                                }
1643                                  break;
1644                              }
1645 <                            Node last = e;
1645 >                            pred = e;
1646                              if ((e = e.next) == null) {
1647 <                                if ((val = mf.remap(k, null)) != null) {
1648 <                                    last.next = new Node(h, k, val, null);
1649 <                                    added = true;
1647 >                                if (!onlyIfPresent && (val = mf.apply(k, null)) != null) {
1648 >                                    pred.next = new Node(h, k, val, null);
1649 >                                    delta = 1;
1650                                      if (count >= TREE_THRESHOLD)
1651                                          replaceWithTreeBin(tab, i, k);
1652                                  }
# Line 1616 | Line 1667 | public class ConcurrentHashMapV8<K, V>
1667                  }
1668              }
1669          }
1670 <        if (val == null)
1671 <            throw new NullPointerException();
1672 <        if (added) {
1673 <            counter.add(1L);
1670 >        if (delta != 0) {
1671 >            counter.add((long)delta);
1672 >            if (count > 1)
1673 >                checkForResize();
1674 >        }
1675 >        return val;
1676 >    }
1677 >
1678 >    /** Implementation for merge */
1679 >    @SuppressWarnings("unchecked")
1680 >    private final Object internalMerge(K k, V v,
1681 >                                       BiFun<? super V, ? super V, ? extends V> mf) {
1682 >        int h = spread(k.hashCode());
1683 >        Object val = null;
1684 >        int delta = 0;
1685 >        int count = 0;
1686 >        for (Node[] tab = table;;) {
1687 >            int i; Node f; int fh; Object fk, fv;
1688 >            if (tab == null)
1689 >                tab = initTable();
1690 >            else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) {
1691 >                if (casTabAt(tab, i, null, new Node(h, k, v, null))) {
1692 >                    delta = 1;
1693 >                    val = v;
1694 >                    break;
1695 >                }
1696 >            }
1697 >            else if ((fh = f.hash) == MOVED) {
1698 >                if ((fk = f.key) instanceof TreeBin) {
1699 >                    TreeBin t = (TreeBin)fk;
1700 >                    t.acquire(0);
1701 >                    try {
1702 >                        if (tabAt(tab, i) == f) {
1703 >                            count = 1;
1704 >                            TreeNode p = t.getTreeNode(h, k, t.root);
1705 >                            val = (p == null) ? v : mf.apply((V)p.val, v);
1706 >                            if (val != null) {
1707 >                                if (p != null)
1708 >                                    p.val = val;
1709 >                                else {
1710 >                                    count = 2;
1711 >                                    delta = 1;
1712 >                                    t.putTreeNode(h, k, val);
1713 >                                }
1714 >                            }
1715 >                            else if (p != null) {
1716 >                                delta = -1;
1717 >                                t.deleteTreeNode(p);
1718 >                            }
1719 >                        }
1720 >                    } finally {
1721 >                        t.release(0);
1722 >                    }
1723 >                    if (count != 0)
1724 >                        break;
1725 >                }
1726 >                else
1727 >                    tab = (Node[])fk;
1728 >            }
1729 >            else if ((fh & LOCKED) != 0) {
1730 >                checkForResize();
1731 >                f.tryAwaitLock(tab, i);
1732 >            }
1733 >            else if (f.casHash(fh, fh | LOCKED)) {
1734 >                try {
1735 >                    if (tabAt(tab, i) == f) {
1736 >                        count = 1;
1737 >                        for (Node e = f, pred = null;; ++count) {
1738 >                            Object ek, ev;
1739 >                            if ((e.hash & HASH_BITS) == h &&
1740 >                                (ev = e.val) != null &&
1741 >                                ((ek = e.key) == k || k.equals(ek))) {
1742 >                                val = mf.apply(v, (V)ev);
1743 >                                if (val != null)
1744 >                                    e.val = val;
1745 >                                else {
1746 >                                    delta = -1;
1747 >                                    Node en = e.next;
1748 >                                    if (pred != null)
1749 >                                        pred.next = en;
1750 >                                    else
1751 >                                        setTabAt(tab, i, en);
1752 >                                }
1753 >                                break;
1754 >                            }
1755 >                            pred = e;
1756 >                            if ((e = e.next) == null) {
1757 >                                val = v;
1758 >                                pred.next = new Node(h, k, val, null);
1759 >                                delta = 1;
1760 >                                if (count >= TREE_THRESHOLD)
1761 >                                    replaceWithTreeBin(tab, i, k);
1762 >                                break;
1763 >                            }
1764 >                        }
1765 >                    }
1766 >                } finally {
1767 >                    if (!f.casHash(fh | LOCKED, fh)) {
1768 >                        f.hash = fh;
1769 >                        synchronized (f) { f.notifyAll(); };
1770 >                    }
1771 >                }
1772 >                if (count != 0) {
1773 >                    if (tab.length <= 64)
1774 >                        count = 2;
1775 >                    break;
1776 >                }
1777 >            }
1778 >        }
1779 >        if (delta != 0) {
1780 >            counter.add((long)delta);
1781              if (count > 1)
1782                  checkForResize();
1783          }
# Line 1937 | Line 2095 | public class ConcurrentHashMapV8<K, V>
2095      }
2096  
2097      /**
2098 <     * Split a normal bin with list headed by e into lo and hi parts;
2099 <     * install in given table
2098 >     * Splits a normal bin with list headed by e into lo and hi parts;
2099 >     * installs in given table.
2100       */
2101      private static void splitBin(Node[] nextTab, int i, Node e) {
2102          int bit = nextTab.length >>> 1; // bit to split on
# Line 1968 | Line 2126 | public class ConcurrentHashMapV8<K, V>
2126      }
2127  
2128      /**
2129 <     * Split a tree bin into lo and hi parts; install in given table
2129 >     * Splits a tree bin into lo and hi parts; installs in given table.
2130       */
2131      private static void splitTreeBin(Node[] nextTab, int i, TreeBin t) {
2132          int bit = nextTab.length >>> 1;
# Line 2071 | Line 2229 | public class ConcurrentHashMapV8<K, V>
2229  
2230      /**
2231       * Encapsulates traversal for methods such as containsValue; also
2232 <     * serves as a base class for other iterators.
2232 >     * serves as a base class for other iterators and bulk tasks.
2233       *
2234       * At each step, the iterator snapshots the key ("nextKey") and
2235       * value ("nextVal") of a valid node (i.e., one that, at point of
# Line 2082 | Line 2240 | public class ConcurrentHashMapV8<K, V>
2240       * valid.
2241       *
2242       * Internal traversals directly access these fields, as in:
2243 <     * {@code while (it.next != null) { process(it.nextKey); it.advance(); }}
2243 >     * {@code while (it.advance() != null) { process(it.nextKey); }}
2244       *
2245 <     * Exported iterators (subclasses of ViewIterator) extract key,
2246 <     * value, or key-value pairs as return values of Iterator.next(),
2247 <     * and encapsulate the it.next check as hasNext();
2245 >     * Exported iterators must track whether the iterator has advanced
2246 >     * (in hasNext vs next) (by setting/checking/nulling field
2247 >     * nextVal), and then extract key, value, or key-value pairs as
2248 >     * return values of next().
2249       *
2250       * The iterator visits once each still-valid node that was
2251       * reachable upon iterator construction. It might miss some that
# Line 2105 | Line 2264 | public class ConcurrentHashMapV8<K, V>
2264       * across threads, iteration terminates if a bounds checks fails
2265       * for a table read.
2266       *
2267 <     * The range-based constructor enables creation of parallel
2268 <     * range-splitting traversals. (Not yet implemented.)
2267 >     * This class extends ForkJoinTask to streamline parallel
2268 >     * iteration in bulk operations (see BulkTask). This adds only an
2269 >     * int of space overhead, which is close enough to negligible in
2270 >     * cases where it is not needed to not worry about it.  Because
2271 >     * ForkJoinTask is Serializable, but iterators need not be, we
2272 >     * need to add warning suppressions.
2273       */
2274 <    static class InternalIterator {
2274 >    @SuppressWarnings("serial")
2275 >    static class Traverser<K,V,R> extends ForkJoinTask<R> {
2276 >        final ConcurrentHashMapV8<K, V> map;
2277          Node next;           // the next entry to use
2278          Node last;           // the last entry used
2279          Object nextKey;      // cached key field of next
# Line 2116 | Line 2281 | public class ConcurrentHashMapV8<K, V>
2281          Node[] tab;          // current table; updated if resized
2282          int index;           // index of bin to use next
2283          int baseIndex;       // current index of initial table
2284 <        final int baseLimit; // index bound for initial table
2284 >        int baseLimit;       // index bound for initial table
2285          final int baseSize;  // initial table size
2286  
2287          /** Creates iterator for all entries in the table. */
2288 <        InternalIterator(Node[] tab) {
2289 <            this.tab = tab;
2288 >        Traverser(ConcurrentHashMapV8<K, V> map) {
2289 >            this.tab = (this.map = map).table;
2290              baseLimit = baseSize = (tab == null) ? 0 : tab.length;
2126            index = baseIndex = 0;
2127            next = null;
2128            advance();
2129        }
2130
2131        /** Creates iterator for the given range of the table */
2132        InternalIterator(Node[] tab, int lo, int hi) {
2133            this.tab = tab;
2134            baseSize = (tab == null) ? 0 : tab.length;
2135            baseLimit = (hi <= baseSize) ? hi : baseSize;
2136            index = baseIndex = (lo >= 0) ? lo : 0;
2137            next = null;
2138            advance();
2291          }
2292  
2293 <        /** Advances next. See above for explanation. */
2294 <        final void advance() {
2293 >        /** Creates iterator for split() methods */
2294 >        Traverser(Traverser<K,V,?> it, boolean split) {
2295 >            this.map = it.map;
2296 >            this.tab = it.tab;
2297 >            this.baseSize = it.baseSize;
2298 >            int lo = it.baseIndex;
2299 >            int hi = this.baseLimit = it.baseLimit;
2300 >            int i;
2301 >            if (split) // adjust parent
2302 >                i = it.baseLimit = (lo + hi + 1) >>> 1;
2303 >            else       // clone parent
2304 >                i = lo;
2305 >            this.index = this.baseIndex = i;
2306 >        }
2307 >
2308 >        /**
2309 >         * Advances next; returns nextVal or null if terminated.
2310 >         * See above for explanation.
2311 >         */
2312 >        final Object advance() {
2313              Node e = last = next;
2314 +            Object ev = null;
2315              outer: do {
2316                  if (e != null)                  // advance past used/skipped node
2317                      e = e.next;
# Line 2160 | Line 2331 | public class ConcurrentHashMapV8<K, V>
2331                      index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2332                  }
2333                  nextKey = e.key;
2334 <            } while ((nextVal = e.val) == null);// skip deleted or special nodes
2334 >            } while ((ev = e.val) == null);    // skip deleted or special nodes
2335              next = e;
2336 +            return nextVal = ev;
2337 +        }
2338 +
2339 +        public final void remove() {
2340 +            if (nextVal == null && last == null)
2341 +                advance();
2342 +            Node e = last;
2343 +            if (e == null)
2344 +                throw new IllegalStateException();
2345 +            last = null;
2346 +            map.remove(e.key);
2347 +        }
2348 +
2349 +        public final boolean hasNext() {
2350 +            return nextVal != null || advance() != null;
2351          }
2352 +
2353 +        public final boolean hasMoreElements() { return hasNext(); }
2354 +        public final void setRawResult(Object x) { }
2355 +        public R getRawResult() { return null; }
2356 +        public boolean exec() { return true; }
2357      }
2358  
2359      /* ---------------- Public operations -------------- */
2360  
2361      /**
2362 <     * Creates a new, empty map with the default initial table size (16),
2362 >     * Creates a new, empty map with the default initial table size (16).
2363       */
2364      public ConcurrentHashMapV8() {
2365          this.counter = new LongAdder();
# Line 2249 | Line 2440 | public class ConcurrentHashMapV8<K, V>
2440          if (initialCapacity < concurrencyLevel)   // Use at least as many bins
2441              initialCapacity = concurrencyLevel;   // as estimated threads
2442          long size = (long)(1.0 + (long)initialCapacity / loadFactor);
2443 <        int cap = ((size >= (long)MAXIMUM_CAPACITY) ?
2444 <                   MAXIMUM_CAPACITY: tableSizeFor((int)size));
2443 >        int cap = (size >= (long)MAXIMUM_CAPACITY) ?
2444 >            MAXIMUM_CAPACITY : tableSizeFor((int)size);
2445          this.counter = new LongAdder();
2446          this.sizeCtl = cap;
2447      }
# Line 2272 | Line 2463 | public class ConcurrentHashMapV8<K, V>
2463                  (int)n);
2464      }
2465  
2466 <    final long longSize() { // accurate version of size needed for views
2466 >    /**
2467 >     * Returns the number of mappings. This method should be used
2468 >     * instead of {@link #size} because a ConcurrentHashMap may
2469 >     * contain more mappings than can be represented as an int. The
2470 >     * value returned is a snapshot; the actual count may differ if
2471 >     * there are ongoing concurrent insertions of removals.
2472 >     *
2473 >     * @return the number of mappings
2474 >     */
2475 >    public long mappingCount() {
2476          long n = counter.sum();
2477 <        return (n < 0L) ? 0L : n;
2477 >        return (n < 0L) ? 0L : n; // ignore transient negative values
2478      }
2479  
2480      /**
# Line 2324 | Line 2524 | public class ConcurrentHashMapV8<K, V>
2524          if (value == null)
2525              throw new NullPointerException();
2526          Object v;
2527 <        InternalIterator it = new InternalIterator(table);
2528 <        while (it.next != null) {
2529 <            if ((v = it.nextVal) == value || value.equals(v))
2527 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2528 >        while ((v = it.advance()) != null) {
2529 >            if (v == value || value.equals(v))
2530                  return true;
2331            it.advance();
2531          }
2532          return false;
2533      }
# Line 2399 | Line 2598 | public class ConcurrentHashMapV8<K, V>
2598  
2599      /**
2600       * If the specified key is not already associated with a value,
2601 <     * computes its value using the given mappingFunction and
2602 <     * enters it into the map.  This is equivalent to
2601 >     * computes its value using the given mappingFunction and enters
2602 >     * it into the map unless null.  This is equivalent to
2603       * <pre> {@code
2604       * if (map.containsKey(key))
2605       *   return map.get(key);
2606 <     * value = mappingFunction.map(key);
2607 <     * map.put(key, value);
2606 >     * value = mappingFunction.apply(key);
2607 >     * if (value != null)
2608 >     *   map.put(key, value);
2609       * return value;}</pre>
2610       *
2611       * except that the action is performed atomically.  If the
2612 <     * function returns {@code null} (in which case a {@code
2613 <     * NullPointerException} is thrown), or the function itself throws
2614 <     * an (unchecked) exception, the exception is rethrown to its
2615 <     * caller, and no mapping is recorded.  Some attempted update
2616 <     * operations on this map by other threads may be blocked while
2617 <     * computation is in progress, so the computation should be short
2618 <     * and simple, and must not attempt to update any other mappings
2619 <     * of this Map. The most appropriate usage is to construct a new
2620 <     * object serving as an initial mapped value, or memoized result,
2421 <     * as in:
2612 >     * function returns {@code null} no mapping is recorded. If the
2613 >     * function itself throws an (unchecked) exception, the exception
2614 >     * is rethrown to its caller, and no mapping is recorded.  Some
2615 >     * attempted update operations on this map by other threads may be
2616 >     * blocked while computation is in progress, so the computation
2617 >     * should be short and simple, and must not attempt to update any
2618 >     * other mappings of this Map. The most appropriate usage is to
2619 >     * construct a new object serving as an initial mapped value, or
2620 >     * memoized result, as in:
2621       *
2622       *  <pre> {@code
2623 <     * map.computeIfAbsent(key, new MappingFunction<K, V>() {
2623 >     * map.computeIfAbsent(key, new Fun<K, V>() {
2624       *   public V map(K k) { return new Value(f(k)); }});}</pre>
2625       *
2626       * @param key key with which the specified value is to be associated
2627       * @param mappingFunction the function to compute a value
2628       * @return the current (existing or computed) value associated with
2629 <     *         the specified key.
2630 <     * @throws NullPointerException if the specified key, mappingFunction,
2631 <     *         or computed value is null
2629 >     *         the specified key, or null if the computed value is null.
2630 >     * @throws NullPointerException if the specified key or mappingFunction
2631 >     *         is null
2632       * @throws IllegalStateException if the computation detectably
2633       *         attempts a recursive update to this map that would
2634       *         otherwise never complete
# Line 2437 | Line 2636 | public class ConcurrentHashMapV8<K, V>
2636       *         in which case the mapping is left unestablished
2637       */
2638      @SuppressWarnings("unchecked")
2639 <    public V computeIfAbsent(K key, MappingFunction<? super K, ? extends V> mappingFunction) {
2639 >    public V computeIfAbsent(K key, Fun<? super K, ? extends V> mappingFunction) {
2640          if (key == null || mappingFunction == null)
2641              throw new NullPointerException();
2642          return (V)internalComputeIfAbsent(key, mappingFunction);
2643      }
2644  
2645      /**
2646 <     * Computes and enters a new mapping value given a key and
2646 >     * If the given key is present, computes a new mapping value given a key and
2647 >     * its current mapped value. This is equivalent to
2648 >     *  <pre> {@code
2649 >     *   if (map.containsKey(key)) {
2650 >     *     value = remappingFunction.apply(key, map.get(key));
2651 >     *     if (value != null)
2652 >     *       map.put(key, value);
2653 >     *     else
2654 >     *       map.remove(key);
2655 >     *   }
2656 >     * }</pre>
2657 >     *
2658 >     * except that the action is performed atomically.  If the
2659 >     * function returns {@code null}, the mapping is removed.  If the
2660 >     * function itself throws an (unchecked) exception, the exception
2661 >     * is rethrown to its caller, and the current mapping is left
2662 >     * unchanged.  Some attempted update operations on this map by
2663 >     * other threads may be blocked while computation is in progress,
2664 >     * so the computation should be short and simple, and must not
2665 >     * attempt to update any other mappings of this Map. For example,
2666 >     * to either create or append new messages to a value mapping:
2667 >     *
2668 >     * @param key key with which the specified value is to be associated
2669 >     * @param remappingFunction the function to compute a value
2670 >     * @return the new value associated with the specified key, or null if none
2671 >     * @throws NullPointerException if the specified key or remappingFunction
2672 >     *         is null
2673 >     * @throws IllegalStateException if the computation detectably
2674 >     *         attempts a recursive update to this map that would
2675 >     *         otherwise never complete
2676 >     * @throws RuntimeException or Error if the remappingFunction does so,
2677 >     *         in which case the mapping is unchanged
2678 >     */
2679 >    @SuppressWarnings("unchecked")
2680 >    public V computeIfPresent(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2681 >        if (key == null || remappingFunction == null)
2682 >            throw new NullPointerException();
2683 >        return (V)internalCompute(key, true, remappingFunction);
2684 >    }
2685 >
2686 >    /**
2687 >     * Computes a new mapping value given a key and
2688       * its current mapped value (or {@code null} if there is no current
2689       * mapping). This is equivalent to
2690       *  <pre> {@code
2691 <     *  map.put(key, remappingFunction.remap(key, map.get(key));
2691 >     *   value = remappingFunction.apply(key, map.get(key));
2692 >     *   if (value != null)
2693 >     *     map.put(key, value);
2694 >     *   else
2695 >     *     map.remove(key);
2696       * }</pre>
2697       *
2698       * except that the action is performed atomically.  If the
2699 <     * function returns {@code null} (in which case a {@code
2700 <     * NullPointerException} is thrown), or the function itself throws
2701 <     * an (unchecked) exception, the exception is rethrown to its
2702 <     * caller, and current mapping is left unchanged.  Some attempted
2703 <     * update operations on this map by other threads may be blocked
2704 <     * while computation is in progress, so the computation should be
2705 <     * short and simple, and must not attempt to update any other
2706 <     * mappings of this Map. For example, to either create or
2463 <     * append new messages to a value mapping:
2699 >     * function returns {@code null}, the mapping is removed.  If the
2700 >     * function itself throws an (unchecked) exception, the exception
2701 >     * is rethrown to its caller, and the current mapping is left
2702 >     * unchanged.  Some attempted update operations on this map by
2703 >     * other threads may be blocked while computation is in progress,
2704 >     * so the computation should be short and simple, and must not
2705 >     * attempt to update any other mappings of this Map. For example,
2706 >     * to either create or append new messages to a value mapping:
2707       *
2708       * <pre> {@code
2709       * Map<Key, String> map = ...;
2710       * final String msg = ...;
2711 <     * map.compute(key, new RemappingFunction<Key, String>() {
2712 <     *   public String remap(Key k, String v) {
2711 >     * map.compute(key, new BiFun<Key, String, String>() {
2712 >     *   public String apply(Key k, String v) {
2713       *    return (v == null) ? msg : v + msg;});}}</pre>
2714       *
2715       * @param key key with which the specified value is to be associated
2716       * @param remappingFunction the function to compute a value
2717 <     * @return the new value associated with
2475 <     *         the specified key.
2717 >     * @return the new value associated with the specified key, or null if none
2718       * @throws NullPointerException if the specified key or remappingFunction
2719 <     *         or computed value is null
2719 >     *         is null
2720       * @throws IllegalStateException if the computation detectably
2721       *         attempts a recursive update to this map that would
2722       *         otherwise never complete
# Line 2482 | Line 2724 | public class ConcurrentHashMapV8<K, V>
2724       *         in which case the mapping is unchanged
2725       */
2726      @SuppressWarnings("unchecked")
2727 <    public V compute(K key, RemappingFunction<? super K, V> remappingFunction) {
2727 >    public V compute(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
2728          if (key == null || remappingFunction == null)
2729              throw new NullPointerException();
2730 <        return (V)internalCompute(key, remappingFunction);
2730 >        return (V)internalCompute(key, false, remappingFunction);
2731 >    }
2732 >
2733 >    /**
2734 >     * If the specified key is not already associated
2735 >     * with a value, associate it with the given value.
2736 >     * Otherwise, replace the value with the results of
2737 >     * the given remapping function. This is equivalent to:
2738 >     *  <pre> {@code
2739 >     *   if (!map.containsKey(key))
2740 >     *     map.put(value);
2741 >     *   else {
2742 >     *     newValue = remappingFunction.apply(map.get(key), value);
2743 >     *     if (value != null)
2744 >     *       map.put(key, value);
2745 >     *     else
2746 >     *       map.remove(key);
2747 >     *   }
2748 >     * }</pre>
2749 >     * except that the action is performed atomically.  If the
2750 >     * function returns {@code null}, the mapping is removed.  If the
2751 >     * function itself throws an (unchecked) exception, the exception
2752 >     * is rethrown to its caller, and the current mapping is left
2753 >     * unchanged.  Some attempted update operations on this map by
2754 >     * other threads may be blocked while computation is in progress,
2755 >     * so the computation should be short and simple, and must not
2756 >     * attempt to update any other mappings of this Map.
2757 >     */
2758 >    @SuppressWarnings("unchecked")
2759 >    public V merge(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
2760 >        if (key == null || value == null || remappingFunction == null)
2761 >            throw new NullPointerException();
2762 >        return (V)internalMerge(key, value, remappingFunction);
2763      }
2764  
2765      /**
# Line 2498 | Line 2772 | public class ConcurrentHashMapV8<K, V>
2772       * @throws NullPointerException if the specified key is null
2773       */
2774      @SuppressWarnings("unchecked")
2775 <    public V remove(Object key) {
2775 >        public V remove(Object key) {
2776          if (key == null)
2777              throw new NullPointerException();
2778          return (V)internalReplace(key, null, null);
# Line 2536 | Line 2810 | public class ConcurrentHashMapV8<K, V>
2810       * @throws NullPointerException if the specified key or value is null
2811       */
2812      @SuppressWarnings("unchecked")
2813 <    public V replace(K key, V value) {
2813 >        public V replace(K key, V value) {
2814          if (key == null || value == null)
2815              throw new NullPointerException();
2816          return (V)internalReplace(key, value, null);
# Line 2633 | Line 2907 | public class ConcurrentHashMapV8<K, V>
2907      }
2908  
2909      /**
2910 +     * Returns a partitionable iterator of the keys in this map.
2911 +     *
2912 +     * @return a partitionable iterator of the keys in this map
2913 +     */
2914 +    public Spliterator<K> keySpliterator() {
2915 +        return new KeyIterator<K,V>(this);
2916 +    }
2917 +
2918 +    /**
2919 +     * Returns a partitionable iterator of the values in this map.
2920 +     *
2921 +     * @return a partitionable iterator of the values in this map
2922 +     */
2923 +    public Spliterator<V> valueSpliterator() {
2924 +        return new ValueIterator<K,V>(this);
2925 +    }
2926 +
2927 +    /**
2928 +     * Returns a partitionable iterator of the entries in this map.
2929 +     *
2930 +     * @return a partitionable iterator of the entries in this map
2931 +     */
2932 +    public Spliterator<Map.Entry<K,V>> entrySpliterator() {
2933 +        return new EntryIterator<K,V>(this);
2934 +    }
2935 +
2936 +    /**
2937       * Returns the hash code value for this {@link Map}, i.e.,
2938       * the sum of, for each key-value pair in the map,
2939       * {@code key.hashCode() ^ value.hashCode()}.
# Line 2641 | Line 2942 | public class ConcurrentHashMapV8<K, V>
2942       */
2943      public int hashCode() {
2944          int h = 0;
2945 <        InternalIterator it = new InternalIterator(table);
2946 <        while (it.next != null) {
2947 <            h += it.nextKey.hashCode() ^ it.nextVal.hashCode();
2948 <            it.advance();
2945 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2946 >        Object v;
2947 >        while ((v = it.advance()) != null) {
2948 >            h += it.nextKey.hashCode() ^ v.hashCode();
2949          }
2950          return h;
2951      }
# Line 2661 | Line 2962 | public class ConcurrentHashMapV8<K, V>
2962       * @return a string representation of this map
2963       */
2964      public String toString() {
2965 <        InternalIterator it = new InternalIterator(table);
2965 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2966          StringBuilder sb = new StringBuilder();
2967          sb.append('{');
2968 <        if (it.next != null) {
2968 >        Object v;
2969 >        if ((v = it.advance()) != null) {
2970              for (;;) {
2971 <                Object k = it.nextKey, v = it.nextVal;
2971 >                Object k = it.nextKey;
2972                  sb.append(k == this ? "(this Map)" : k);
2973                  sb.append('=');
2974                  sb.append(v == this ? "(this Map)" : v);
2975 <                it.advance();
2674 <                if (it.next == null)
2975 >                if ((v = it.advance()) == null)
2976                      break;
2977                  sb.append(',').append(' ');
2978              }
# Line 2694 | Line 2995 | public class ConcurrentHashMapV8<K, V>
2995              if (!(o instanceof Map))
2996                  return false;
2997              Map<?,?> m = (Map<?,?>) o;
2998 <            InternalIterator it = new InternalIterator(table);
2999 <            while (it.next != null) {
3000 <                Object val = it.nextVal;
2998 >            Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2999 >            Object val;
3000 >            while ((val = it.advance()) != null) {
3001                  Object v = m.get(it.nextKey);
3002                  if (v == null || (v != val && !v.equals(val)))
3003                      return false;
2703                it.advance();
3004              }
3005              for (Map.Entry<?,?> e : m.entrySet()) {
3006                  Object mk, mv, v;
# Line 2716 | Line 3016 | public class ConcurrentHashMapV8<K, V>
3016  
3017      /* ----------------Iterators -------------- */
3018  
3019 <    /**
3020 <     * Base class for key, value, and entry iterators.  Adds a map
3021 <     * reference to InternalIterator to support Iterator.remove.
3022 <     */
3023 <    static abstract class ViewIterator<K,V> extends InternalIterator {
3024 <        final ConcurrentHashMapV8<K, V> map;
2725 <        ViewIterator(ConcurrentHashMapV8<K, V> map) {
2726 <            super(map.table);
2727 <            this.map = map;
3019 >    @SuppressWarnings("serial")
3020 >    static final class KeyIterator<K,V> extends Traverser<K,V,Object>
3021 >        implements Spliterator<K>, Enumeration<K> {
3022 >        KeyIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3023 >        KeyIterator(Traverser<K,V,Object> it, boolean split) {
3024 >            super(it, split);
3025          }
3026 <
3027 <        public final void remove() {
2731 <            if (last == null)
3026 >        public KeyIterator<K,V> split() {
3027 >            if (last != null || (next != null && nextVal == null))
3028                  throw new IllegalStateException();
3029 <            map.remove(last.key);
2734 <            last = null;
3029 >            return new KeyIterator<K,V>(this, true);
3030          }
2736
2737        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
3031          @SuppressWarnings("unchecked")
3032 <        public final K next() {
3033 <            if (next == null)
3032 >            public final K next() {
3033 >            if (nextVal == null && advance() == null)
3034                  throw new NoSuchElementException();
3035              Object k = nextKey;
3036 <            advance();
3037 <            return (K)k;
3036 >            nextVal = null;
3037 >            return (K) k;
3038          }
3039  
3040          public final K nextElement() { return next(); }
3041      }
3042  
3043 <    static final class ValueIterator<K,V> extends ViewIterator<K,V>
3044 <        implements Iterator<V>, Enumeration<V> {
3043 >    @SuppressWarnings("serial")
3044 >    static final class ValueIterator<K,V> extends Traverser<K,V,Object>
3045 >        implements Spliterator<V>, Enumeration<V> {
3046          ValueIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3047 +        ValueIterator(Traverser<K,V,Object> it, boolean split) {
3048 +            super(it, split);
3049 +        }
3050 +        public ValueIterator<K,V> split() {
3051 +            if (last != null || (next != null && nextVal == null))
3052 +                throw new IllegalStateException();
3053 +            return new ValueIterator<K,V>(this, true);
3054 +        }
3055  
3056          @SuppressWarnings("unchecked")
3057 <        public final V next() {
3058 <            if (next == null)
3057 >            public final V next() {
3058 >            Object v;
3059 >            if ((v = nextVal) == null && (v = advance()) == null)
3060                  throw new NoSuchElementException();
3061 <            Object v = nextVal;
3062 <            advance();
2767 <            return (V)v;
3061 >            nextVal = null;
3062 >            return (V) v;
3063          }
3064  
3065          public final V nextElement() { return next(); }
3066      }
3067  
3068 <    static final class EntryIterator<K,V> extends ViewIterator<K,V>
3069 <        implements Iterator<Map.Entry<K,V>> {
3068 >    @SuppressWarnings("serial")
3069 >    static final class EntryIterator<K,V> extends Traverser<K,V,Object>
3070 >        implements Spliterator<Map.Entry<K,V>> {
3071          EntryIterator(ConcurrentHashMapV8<K, V> map) { super(map); }
3072 <
3073 <        @SuppressWarnings("unchecked")
3074 <        public final Map.Entry<K,V> next() {
3075 <            if (next == null)
3076 <                throw new NoSuchElementException();
3077 <            Object k = nextKey;
3078 <            Object v = nextVal;
2783 <            advance();
2784 <            return new WriteThroughEntry<K,V>((K)k, (V)v, map);
3072 >        EntryIterator(Traverser<K,V,Object> it, boolean split) {
3073 >            super(it, split);
3074 >        }
3075 >        public EntryIterator<K,V> split() {
3076 >            if (last != null || (next != null && nextVal == null))
3077 >                throw new IllegalStateException();
3078 >            return new EntryIterator<K,V>(this, true);
3079          }
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); }
3080  
3081          @SuppressWarnings("unchecked")
3082 <        public final Map.Entry<K,V> next() {
3083 <            if (next == null)
3082 >            public final Map.Entry<K,V> next() {
3083 >            Object v;
3084 >            if ((v = nextVal) == null && (v = advance()) == null)
3085                  throw new NoSuchElementException();
3086              Object k = nextKey;
3087 <            Object v = nextVal;
3088 <            advance();
2799 <            return new SnapshotEntry<K,V>((K)k, (V)v);
3087 >            nextVal = null;
3088 >            return new MapEntry<K,V>((K)k, (V)v, map);
3089          }
3090      }
3091  
3092      /**
3093 <     * Base of writeThrough and Snapshot entry classes
3093 >     * Exported Entry for iterators
3094       */
3095 <    static abstract class MapEntry<K,V> implements Map.Entry<K, V> {
3095 >    static final class MapEntry<K,V> implements Map.Entry<K, V> {
3096          final K key; // non-null
3097          V val;       // non-null
3098 <        MapEntry(K key, V val)        { this.key = key; this.val = val; }
3098 >        final ConcurrentHashMapV8<K, V> map;
3099 >        MapEntry(K key, V val, ConcurrentHashMapV8<K, V> map) {
3100 >            this.key = key;
3101 >            this.val = val;
3102 >            this.map = map;
3103 >        }
3104          public final K getKey()       { return key; }
3105          public final V getValue()     { return val; }
3106          public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
# Line 2821 | Line 3115 | public class ConcurrentHashMapV8<K, V>
3115                      (v == val || v.equals(val)));
3116          }
3117  
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
3118          /**
3119           * Sets our entry's value and writes through to the map. The
3120 <         * value to return is somewhat arbitrary here. Since a
3121 <         * WriteThroughEntry does not necessarily track asynchronous
3122 <         * changes, the most recent "previous" value could be
3123 <         * different from what we return (or could even have been
3124 <         * removed in which case the put will re-establish). We do not
2846 <         * and cannot guarantee more.
3120 >         * value to return is somewhat arbitrary here. Since we do not
3121 >         * necessarily track asynchronous changes, the most recent
3122 >         * "previous" value could be different from what we return (or
3123 >         * could even have been removed in which case the put will
3124 >         * re-establish). We do not and cannot guarantee more.
3125           */
3126          public final V setValue(V value) {
3127              if (value == null) throw new NullPointerException();
# Line 2854 | Line 3132 | public class ConcurrentHashMapV8<K, V>
3132          }
3133      }
3134  
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
3135      /* ----------------Views -------------- */
3136  
3137      /**
3138 <     * Base class for views. This is done mainly to allow adding
2875 <     * customized parallel traversals (not yet implemented.)
3138 >     * Base class for views.
3139       */
3140 <    static abstract class MapView<K, V> {
3140 >    static abstract class CHMView<K, V> {
3141          final ConcurrentHashMapV8<K, V> map;
3142 <        MapView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
3142 >        CHMView(ConcurrentHashMapV8<K, V> map)  { this.map = map; }
3143          public final int size()                 { return map.size(); }
3144          public final boolean isEmpty()          { return map.isEmpty(); }
3145          public final void clear()               { map.clear(); }
3146  
3147          // implementations below rely on concrete classes supplying these
3148 <        abstract Iterator<?> iter();
3148 >        abstract public Iterator<?> iterator();
3149          abstract public boolean contains(Object o);
3150          abstract public boolean remove(Object o);
3151  
3152          private static final String oomeMsg = "Required array size too large";
3153  
3154          public final Object[] toArray() {
3155 <            long sz = map.longSize();
3155 >            long sz = map.mappingCount();
3156              if (sz > (long)(MAX_ARRAY_SIZE))
3157                  throw new OutOfMemoryError(oomeMsg);
3158              int n = (int)sz;
3159              Object[] r = new Object[n];
3160              int i = 0;
3161 <            Iterator<?> it = iter();
3161 >            Iterator<?> it = iterator();
3162              while (it.hasNext()) {
3163                  if (i == n) {
3164                      if (n >= MAX_ARRAY_SIZE)
# Line 2912 | Line 3175 | public class ConcurrentHashMapV8<K, V>
3175          }
3176  
3177          @SuppressWarnings("unchecked")
3178 <        public final <T> T[] toArray(T[] a) {
3179 <            long sz = map.longSize();
3178 >            public final <T> T[] toArray(T[] a) {
3179 >            long sz = map.mappingCount();
3180              if (sz > (long)(MAX_ARRAY_SIZE))
3181                  throw new OutOfMemoryError(oomeMsg);
3182              int m = (int)sz;
# Line 2922 | Line 3185 | public class ConcurrentHashMapV8<K, V>
3185                  .newInstance(a.getClass().getComponentType(), m);
3186              int n = r.length;
3187              int i = 0;
3188 <            Iterator<?> it = iter();
3188 >            Iterator<?> it = iterator();
3189              while (it.hasNext()) {
3190                  if (i == n) {
3191                      if (n >= MAX_ARRAY_SIZE)
# Line 2944 | Line 3207 | public class ConcurrentHashMapV8<K, V>
3207  
3208          public final int hashCode() {
3209              int h = 0;
3210 <            for (Iterator<?> it = iter(); it.hasNext();)
3210 >            for (Iterator<?> it = iterator(); it.hasNext();)
3211                  h += it.next().hashCode();
3212              return h;
3213          }
# Line 2952 | Line 3215 | public class ConcurrentHashMapV8<K, V>
3215          public final String toString() {
3216              StringBuilder sb = new StringBuilder();
3217              sb.append('[');
3218 <            Iterator<?> it = iter();
3218 >            Iterator<?> it = iterator();
3219              if (it.hasNext()) {
3220                  for (;;) {
3221                      Object e = it.next();
# Line 2978 | Line 3241 | public class ConcurrentHashMapV8<K, V>
3241  
3242          public final boolean removeAll(Collection<?> c) {
3243              boolean modified = false;
3244 <            for (Iterator<?> it = iter(); it.hasNext();) {
3244 >            for (Iterator<?> it = iterator(); it.hasNext();) {
3245                  if (c.contains(it.next())) {
3246                      it.remove();
3247                      modified = true;
# Line 2989 | Line 3252 | public class ConcurrentHashMapV8<K, V>
3252  
3253          public final boolean retainAll(Collection<?> c) {
3254              boolean modified = false;
3255 <            for (Iterator<?> it = iter(); it.hasNext();) {
3255 >            for (Iterator<?> it = iterator(); it.hasNext();) {
3256                  if (!c.contains(it.next())) {
3257                      it.remove();
3258                      modified = true;
# Line 3000 | Line 3263 | public class ConcurrentHashMapV8<K, V>
3263  
3264      }
3265  
3266 <    static final class KeySet<K,V> extends MapView<K,V> implements Set<K> {
3267 <        KeySet(ConcurrentHashMapV8<K, V> map)   { super(map); }
3266 >    static final class KeySet<K,V> extends CHMView<K,V> implements Set<K> {
3267 >        KeySet(ConcurrentHashMapV8<K, V> map)  {
3268 >            super(map);
3269 >        }
3270          public final boolean contains(Object o) { return map.containsKey(o); }
3271          public final boolean remove(Object o)   { return map.remove(o) != null; }
3007
3272          public final Iterator<K> iterator() {
3273              return new KeyIterator<K,V>(map);
3274          }
3011        final Iterator<?> iter() {
3012            return new KeyIterator<K,V>(map);
3013        }
3275          public final boolean add(K e) {
3276              throw new UnsupportedOperationException();
3277          }
# Line 3025 | Line 3286 | public class ConcurrentHashMapV8<K, V>
3286          }
3287      }
3288  
3289 <    static final class Values<K,V> extends MapView<K,V>
3289 >
3290 >    static final class Values<K,V> extends CHMView<K,V>
3291          implements Collection<V> {
3292          Values(ConcurrentHashMapV8<K, V> map)   { super(map); }
3293          public final boolean contains(Object o) { return map.containsValue(o); }
3032
3294          public final boolean remove(Object o) {
3295              if (o != null) {
3296                  Iterator<V> it = new ValueIterator<K,V>(map);
# Line 3045 | Line 3306 | public class ConcurrentHashMapV8<K, V>
3306          public final Iterator<V> iterator() {
3307              return new ValueIterator<K,V>(map);
3308          }
3048        final Iterator<?> iter() {
3049            return new ValueIterator<K,V>(map);
3050        }
3309          public final boolean add(V e) {
3310              throw new UnsupportedOperationException();
3311          }
3312          public final boolean addAll(Collection<? extends V> c) {
3313              throw new UnsupportedOperationException();
3314          }
3315 +
3316      }
3317  
3318 <    static final class EntrySet<K,V> extends MapView<K,V>
3318 >    static final class EntrySet<K,V> extends CHMView<K,V>
3319          implements Set<Map.Entry<K,V>> {
3320          EntrySet(ConcurrentHashMapV8<K, V> map) { super(map); }
3062
3321          public final boolean contains(Object o) {
3322              Object k, v, r; Map.Entry<?,?> e;
3323              return ((o instanceof Map.Entry) &&
# Line 3068 | Line 3326 | public class ConcurrentHashMapV8<K, V>
3326                      (v = e.getValue()) != null &&
3327                      (v == r || v.equals(r)));
3328          }
3071
3329          public final boolean remove(Object o) {
3330              Object k, v; Map.Entry<?,?> e;
3331              return ((o instanceof Map.Entry) &&
# Line 3076 | Line 3333 | public class ConcurrentHashMapV8<K, V>
3333                      (v = e.getValue()) != null &&
3334                      map.remove(k, v));
3335          }
3079
3336          public final Iterator<Map.Entry<K,V>> iterator() {
3337              return new EntryIterator<K,V>(map);
3338          }
3083        final Iterator<?> iter() {
3084            return new SnapshotEntryIterator<K,V>(map);
3085        }
3339          public final boolean add(Entry<K,V> e) {
3340              throw new UnsupportedOperationException();
3341          }
# Line 3119 | Line 3372 | public class ConcurrentHashMapV8<K, V>
3372       * The key-value mappings are emitted in no particular order.
3373       */
3374      @SuppressWarnings("unchecked")
3375 <    private void writeObject(java.io.ObjectOutputStream s)
3376 <            throws java.io.IOException {
3375 >        private void writeObject(java.io.ObjectOutputStream s)
3376 >        throws java.io.IOException {
3377          if (segments == null) { // for serialization compatibility
3378              segments = (Segment<K,V>[])
3379                  new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
# Line 3128 | Line 3381 | public class ConcurrentHashMapV8<K, V>
3381                  segments[i] = new Segment<K,V>(LOAD_FACTOR);
3382          }
3383          s.defaultWriteObject();
3384 <        InternalIterator it = new InternalIterator(table);
3385 <        while (it.next != null) {
3384 >        Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
3385 >        Object v;
3386 >        while ((v = it.advance()) != null) {
3387              s.writeObject(it.nextKey);
3388 <            s.writeObject(it.nextVal);
3135 <            it.advance();
3388 >            s.writeObject(v);
3389          }
3390          s.writeObject(null);
3391          s.writeObject(null);
# Line 3144 | Line 3397 | public class ConcurrentHashMapV8<K, V>
3397       * @param s the stream
3398       */
3399      @SuppressWarnings("unchecked")
3400 <    private void readObject(java.io.ObjectInputStream s)
3401 <            throws java.io.IOException, ClassNotFoundException {
3400 >        private void readObject(java.io.ObjectInputStream s)
3401 >        throws java.io.IOException, ClassNotFoundException {
3402          s.defaultReadObject();
3403          this.segments = null; // unneeded
3404          // initialize transient final field
# Line 3219 | Line 3472 | public class ConcurrentHashMapV8<K, V>
3472                      p = p.next;
3473                  }
3474              }
3475 +        }
3476 +    }
3477 +
3478 +
3479 +    // -------------------------------------------------------
3480 +
3481 +    // Sams
3482 +    /** Interface describing a void action of one argument */
3483 +    public interface Action<A> { void apply(A a); }
3484 +    /** Interface describing a void action of two arguments */
3485 +    public interface BiAction<A,B> { void apply(A a, B b); }
3486 +    /** Interface describing a function of one argument */
3487 +    public interface Fun<A,T> { T apply(A a); }
3488 +    /** Interface describing a function of two arguments */
3489 +    public interface BiFun<A,B,T> { T apply(A a, B b); }
3490 +    /** Interface describing a function of no arguments */
3491 +    public interface Generator<T> { T apply(); }
3492 +    /** Interface describing a function mapping its argument to a double */
3493 +    public interface ObjectToDouble<A> { double apply(A a); }
3494 +    /** Interface describing a function mapping its argument to a long */
3495 +    public interface ObjectToLong<A> { long apply(A a); }
3496 +    /** Interface describing a function mapping its argument to an int */
3497 +    public interface ObjectToInt<A> {int apply(A a); }
3498 +    /** Interface describing a function mapping two arguments to a double */
3499 +    public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
3500 +    /** Interface describing a function mapping two arguments to a long */
3501 +    public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
3502 +    /** Interface describing a function mapping two arguments to an int */
3503 +    public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
3504 +    /** Interface describing a function mapping a double to a double */
3505 +    public interface DoubleToDouble { double apply(double a); }
3506 +    /** Interface describing a function mapping a long to a long */
3507 +    public interface LongToLong { long apply(long a); }
3508 +    /** Interface describing a function mapping an int to an int */
3509 +    public interface IntToInt { int apply(int a); }
3510 +    /** Interface describing a function mapping two doubles to a double */
3511 +    public interface DoubleByDoubleToDouble { double apply(double a, double b); }
3512 +    /** Interface describing a function mapping two longs to a long */
3513 +    public interface LongByLongToLong { long apply(long a, long b); }
3514 +    /** Interface describing a function mapping two ints to an int */
3515 +    public interface IntByIntToInt { int apply(int a, int b); }
3516 +
3517 +
3518 +    // -------------------------------------------------------
3519 +
3520 +    /**
3521 +     * Returns an extended {@link Parallel} view of this map using the
3522 +     * given executor for bulk parallel operations.
3523 +     *
3524 +     * @param executor the executor
3525 +     * @return a parallel view
3526 +     */
3527 +    public Parallel parallel(ForkJoinPool executor)  {
3528 +        return new Parallel(executor);
3529 +    }
3530 +
3531 +    /**
3532 +     * An extended view of a ConcurrentHashMap supporting bulk
3533 +     * parallel operations. These operations are designed to be
3534 +     * safely, and often sensibly, applied even with maps that are
3535 +     * being concurrently updated by other threads; for example, when
3536 +     * computing a snapshot summary of the values in a shared
3537 +     * registry.  There are three kinds of operation, each with four
3538 +     * forms, accepting functions with Keys, Values, Entries, and
3539 +     * (Key, Value) arguments and/or return values. Because the
3540 +     * elements of a ConcurrentHashMap are not ordered in any
3541 +     * particular way, and may be processed in different orders in
3542 +     * different parallel executions, the correctness of supplied
3543 +     * functions should not depend on any ordering, or on any other
3544 +     * objects or values that may transiently change while computation
3545 +     * is in progress; and except for forEach actions, should ideally
3546 +     * be side-effect-free.
3547 +     *
3548 +     * <ul>
3549 +     * <li> forEach: Perform a given action on each element.
3550 +     * A variant form applies a given transformation on each element
3551 +     * before performing the action.</li>
3552 +     *
3553 +     * <li> search: Return the first available non-null result of
3554 +     * applying a given function on each element; skipping further
3555 +     * search when a result is found.</li>
3556 +     *
3557 +     * <li> reduce: Accumulate each element.  The supplied reduction
3558 +     * function cannot rely on ordering (more formally, it should be
3559 +     * both associative and commutative).  There are five variants:
3560 +     *
3561 +     * <ul>
3562 +     *
3563 +     * <li> Plain reductions. (There is not a form of this method for
3564 +     * (key, value) function arguments since there is no corresponding
3565 +     * return type.)</li>
3566 +     *
3567 +     * <li> Mapped reductions that accumulate the results of a given
3568 +     * function applied to each element.</li>
3569 +     *
3570 +     * <li> Reductions to scalar doubles, longs, and ints, using a
3571 +     * given basis value.</li>
3572 +     *
3573 +     * </li>
3574 +     * </ul>
3575 +     * </ul>
3576 +     *
3577 +     * <p>The concurrency properties of the bulk operations follow
3578 +     * from those of ConcurrentHashMap: Any non-null result returned
3579 +     * from {@code get(key)} and related access methods bears a
3580 +     * happens-before relation with the associated insertion or
3581 +     * update.  The result of any bulk operation reflects the
3582 +     * composition of these per-element relations (but is not
3583 +     * necessarily atomic with respect to the map as a whole unless it
3584 +     * is somehow known to be quiescent).  Conversely, because keys
3585 +     * and values in the map are never null, null serves as a reliable
3586 +     * atomic indicator of the current lack of any result.  To
3587 +     * maintain this property, null serves as an implicit basis for
3588 +     * all non-scalar reduction operations. For the double, long, and
3589 +     * int versions, the basis should be one that, when combined with
3590 +     * any other value, returns that other value (more formally, it
3591 +     * should be the identity element for the reduction). Most common
3592 +     * reductions have these properties; for example, computing a sum
3593 +     * with basis 0 or a minimum with basis MAX_VALUE.
3594 +     *
3595 +     * <p>Search and transformation functions provided as arguments
3596 +     * should similarly return null to indicate the lack of any result
3597 +     * (in which case it is not used). In the case of mapped
3598 +     * reductions, this also enables transformations to serve as
3599 +     * filters, returning null (or, in the case of primitive
3600 +     * specializations, the identity basis) if the element should not
3601 +     * be combined. You can create compound transformations and
3602 +     * filterings by composing them yourself under this "null means
3603 +     * there is nothing there now" rule before using them in search or
3604 +     * reduce operations.
3605 +     *
3606 +     * <p>Methods accepting and/or returning Entry arguments maintain
3607 +     * key-value associations. They may be useful for example when
3608 +     * finding the key for the greatest value. Note that "plain" Entry
3609 +     * arguments can be supplied using {@code new
3610 +     * AbstractMap.SimpleEntry(k,v)}.
3611 +     *
3612 +     * <p> Bulk operations may complete abruptly, throwing an
3613 +     * exception encountered in the application of a supplied
3614 +     * function. Bear in mind when handling such exceptions that other
3615 +     * concurrently executing functions could also have thrown
3616 +     * exceptions, or would have done so if the first exception had
3617 +     * not occurred.
3618 +     *
3619 +     * <p>Parallel speedups compared to sequential processing are
3620 +     * common but not guaranteed.  Operations involving brief
3621 +     * functions on small maps may execute more slowly than sequential
3622 +     * loops if the underlying work to parallelize the computation is
3623 +     * more expensive than the computation itself. Similarly,
3624 +     * parallelization may not lead to much actual parallelism if all
3625 +     * processors are busy performing unrelated tasks.
3626 +     *
3627 +     * <p> All arguments to all task methods must be non-null.
3628 +     *
3629 +     * <p><em>jsr166e note: During transition, this class
3630 +     * uses nested functional interfaces with different names but the
3631 +     * same forms as those expected for JDK8.<em>
3632 +     */
3633 +    public class Parallel {
3634 +        final ForkJoinPool fjp;
3635 +
3636 +        /**
3637 +         * Returns an extended view of this map using the given
3638 +         * executor for bulk parallel operations.
3639 +         *
3640 +         * @param executor the executor
3641 +         */
3642 +        public Parallel(ForkJoinPool executor)  {
3643 +            this.fjp = executor;
3644 +        }
3645 +
3646 +        /**
3647 +         * Performs the given action for each (key, value).
3648 +         *
3649 +         * @param action the action
3650 +         */
3651 +        public void forEach(BiAction<K,V> action) {
3652 +            fjp.invoke(ForkJoinTasks.forEach
3653 +                       (ConcurrentHashMapV8.this, action));
3654 +        }
3655 +
3656 +        /**
3657 +         * Performs the given action for each non-null transformation
3658 +         * of each (key, value).
3659 +         *
3660 +         * @param transformer a function returning the transformation
3661 +         * for an element, or null of there is no transformation (in
3662 +         * which case the action is not applied).
3663 +         * @param action the action
3664 +         */
3665 +        public <U> void forEach(BiFun<? super K, ? super V, ? extends U> transformer,
3666 +                                Action<U> action) {
3667 +            fjp.invoke(ForkJoinTasks.forEach
3668 +                       (ConcurrentHashMapV8.this, transformer, action));
3669 +        }
3670 +
3671 +        /**
3672 +         * Returns a non-null result from applying the given search
3673 +         * function on each (key, value), or null if none.  Upon
3674 +         * success, further element processing is suppressed and the
3675 +         * results of any other parallel invocations of the search
3676 +         * function are ignored.
3677 +         *
3678 +         * @param searchFunction a function returning a non-null
3679 +         * result on success, else null
3680 +         * @return a non-null result from applying the given search
3681 +         * function on each (key, value), or null if none
3682 +         */
3683 +        public <U> U search(BiFun<? super K, ? super V, ? extends U> searchFunction) {
3684 +            return fjp.invoke(ForkJoinTasks.search
3685 +                              (ConcurrentHashMapV8.this, searchFunction));
3686 +        }
3687 +
3688 +        /**
3689 +         * Returns the result of accumulating the given transformation
3690 +         * of all (key, value) pairs using the given reducer to
3691 +         * combine values, or null if none.
3692 +         *
3693 +         * @param transformer a function returning the transformation
3694 +         * for an element, or null of there is no transformation (in
3695 +         * which case it is not combined).
3696 +         * @param reducer a commutative associative combining function
3697 +         * @return the result of accumulating the given transformation
3698 +         * of all (key, value) pairs
3699 +         */
3700 +        public <U> U reduce(BiFun<? super K, ? super V, ? extends U> transformer,
3701 +                            BiFun<? super U, ? super U, ? extends U> reducer) {
3702 +            return fjp.invoke(ForkJoinTasks.reduce
3703 +                              (ConcurrentHashMapV8.this, transformer, reducer));
3704 +        }
3705 +
3706 +        /**
3707 +         * Returns the result of accumulating the given transformation
3708 +         * of all (key, value) pairs using the given reducer to
3709 +         * combine values, and the given basis as an identity value.
3710 +         *
3711 +         * @param transformer a function returning the transformation
3712 +         * for an element
3713 +         * @param basis the identity (initial default value) for the reduction
3714 +         * @param reducer a commutative associative combining function
3715 +         * @return the result of accumulating the given transformation
3716 +         * of all (key, value) pairs
3717 +         */
3718 +        public double reduceToDouble(ObjectByObjectToDouble<? super K, ? super V> transformer,
3719 +                                     double basis,
3720 +                                     DoubleByDoubleToDouble reducer) {
3721 +            return fjp.invoke(ForkJoinTasks.reduceToDouble
3722 +                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3723 +        }
3724 +
3725 +        /**
3726 +         * Returns the result of accumulating the given transformation
3727 +         * of all (key, value) pairs using the given reducer to
3728 +         * combine values, and the given basis as an identity value.
3729 +         *
3730 +         * @param transformer a function returning the transformation
3731 +         * for an element
3732 +         * @param basis the identity (initial default value) for the reduction
3733 +         * @param reducer a commutative associative combining function
3734 +         * @return the result of accumulating the given transformation
3735 +         * of all (key, value) pairs
3736 +         */
3737 +        public long reduceToLong(ObjectByObjectToLong<? super K, ? super V> transformer,
3738 +                                 long basis,
3739 +                                 LongByLongToLong reducer) {
3740 +            return fjp.invoke(ForkJoinTasks.reduceToLong
3741 +                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3742 +        }
3743 +
3744 +        /**
3745 +         * Returns the result of accumulating the given transformation
3746 +         * of all (key, value) pairs using the given reducer to
3747 +         * combine values, and the given basis as an identity value.
3748 +         *
3749 +         * @param transformer a function returning the transformation
3750 +         * for an element
3751 +         * @param basis the identity (initial default value) for the reduction
3752 +         * @param reducer a commutative associative combining function
3753 +         * @return the result of accumulating the given transformation
3754 +         * of all (key, value) pairs
3755 +         */
3756 +        public int reduceToInt(ObjectByObjectToInt<? super K, ? super V> transformer,
3757 +                               int basis,
3758 +                               IntByIntToInt reducer) {
3759 +            return fjp.invoke(ForkJoinTasks.reduceToInt
3760 +                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3761 +        }
3762 +
3763 +        /**
3764 +         * Performs the given action for each key.
3765 +         *
3766 +         * @param action the action
3767 +         */
3768 +        public void forEachKey(Action<K> action) {
3769 +            fjp.invoke(ForkJoinTasks.forEachKey
3770 +                       (ConcurrentHashMapV8.this, action));
3771 +        }
3772 +
3773 +        /**
3774 +         * Performs the given action for each non-null transformation
3775 +         * of each key.
3776 +         *
3777 +         * @param transformer a function returning the transformation
3778 +         * for an element, or null of there is no transformation (in
3779 +         * which case the action is not applied).
3780 +         * @param action the action
3781 +         */
3782 +        public <U> void forEachKey(Fun<? super K, ? extends U> transformer,
3783 +                                   Action<U> action) {
3784 +            fjp.invoke(ForkJoinTasks.forEachKey
3785 +                       (ConcurrentHashMapV8.this, transformer, action));
3786 +        }
3787 +
3788 +        /**
3789 +         * Returns a non-null result from applying the given search
3790 +         * function on each key, or null if none. Upon success,
3791 +         * further element processing is suppressed and the results of
3792 +         * any other parallel invocations of the search function are
3793 +         * ignored.
3794 +         *
3795 +         * @param searchFunction a function returning a non-null
3796 +         * result on success, else null
3797 +         * @return a non-null result from applying the given search
3798 +         * function on each key, or null if none
3799 +         */
3800 +        public <U> U searchKeys(Fun<? super K, ? extends U> searchFunction) {
3801 +            return fjp.invoke(ForkJoinTasks.searchKeys
3802 +                              (ConcurrentHashMapV8.this, searchFunction));
3803 +        }
3804 +
3805 +        /**
3806 +         * Returns the result of accumulating all keys using the given
3807 +         * reducer to combine values, or null if none.
3808 +         *
3809 +         * @param reducer a commutative associative combining function
3810 +         * @return the result of accumulating all keys using the given
3811 +         * reducer to combine values, or null if none
3812 +         */
3813 +        public K reduceKeys(BiFun<? super K, ? super K, ? extends K> reducer) {
3814 +            return fjp.invoke(ForkJoinTasks.reduceKeys
3815 +                              (ConcurrentHashMapV8.this, reducer));
3816 +        }
3817 +
3818 +        /**
3819 +         * Returns the result of accumulating the given transformation
3820 +         * of all keys using the given reducer to combine values, or
3821 +         * null if none.
3822 +         *
3823 +         * @param transformer a function returning the transformation
3824 +         * for an element, or null of there is no transformation (in
3825 +         * which case it is not combined).
3826 +         * @param reducer a commutative associative combining function
3827 +         * @return the result of accumulating the given transformation
3828 +         * of all keys
3829 +         */
3830 +        public <U> U reduceKeys(Fun<? super K, ? extends U> transformer,
3831 +                                BiFun<? super U, ? super U, ? extends U> reducer) {
3832 +            return fjp.invoke(ForkJoinTasks.reduceKeys
3833 +                              (ConcurrentHashMapV8.this, transformer, reducer));
3834 +        }
3835 +
3836 +        /**
3837 +         * Returns the result of accumulating the given transformation
3838 +         * of all keys using the given reducer to combine values, and
3839 +         * the given basis as an identity value.
3840 +         *
3841 +         * @param transformer a function returning the transformation
3842 +         * for an element
3843 +         * @param basis the identity (initial default value) for the reduction
3844 +         * @param reducer a commutative associative combining function
3845 +         * @return  the result of accumulating the given transformation
3846 +         * of all keys
3847 +         */
3848 +        public double reduceKeysToDouble(ObjectToDouble<? super K> transformer,
3849 +                                         double basis,
3850 +                                         DoubleByDoubleToDouble reducer) {
3851 +            return fjp.invoke(ForkJoinTasks.reduceKeysToDouble
3852 +                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3853 +        }
3854 +
3855 +        /**
3856 +         * Returns the result of accumulating the given transformation
3857 +         * of all keys using the given reducer to combine values, and
3858 +         * the given basis as an identity value.
3859 +         *
3860 +         * @param transformer a function returning the transformation
3861 +         * for an element
3862 +         * @param basis the identity (initial default value) for the reduction
3863 +         * @param reducer a commutative associative combining function
3864 +         * @return the result of accumulating the given transformation
3865 +         * of all keys
3866 +         */
3867 +        public long reduceKeysToLong(ObjectToLong<? super K> transformer,
3868 +                                     long basis,
3869 +                                     LongByLongToLong reducer) {
3870 +            return fjp.invoke(ForkJoinTasks.reduceKeysToLong
3871 +                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3872 +        }
3873 +
3874 +        /**
3875 +         * Returns the result of accumulating the given transformation
3876 +         * of all keys using the given reducer to combine values, and
3877 +         * the given basis as an identity value.
3878 +         *
3879 +         * @param transformer a function returning the transformation
3880 +         * for an element
3881 +         * @param basis the identity (initial default value) for the reduction
3882 +         * @param reducer a commutative associative combining function
3883 +         * @return the result of accumulating the given transformation
3884 +         * of all keys
3885 +         */
3886 +        public int reduceKeysToInt(ObjectToInt<? super K> transformer,
3887 +                                   int basis,
3888 +                                   IntByIntToInt reducer) {
3889 +            return fjp.invoke(ForkJoinTasks.reduceKeysToInt
3890 +                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3891 +        }
3892 +
3893 +        /**
3894 +         * Performs the given action for each value.
3895 +         *
3896 +         * @param action the action
3897 +         */
3898 +        public void forEachValue(Action<V> action) {
3899 +            fjp.invoke(ForkJoinTasks.forEachValue
3900 +                       (ConcurrentHashMapV8.this, action));
3901 +        }
3902 +
3903 +        /**
3904 +         * Performs the given action for each non-null transformation
3905 +         * of each value.
3906 +         *
3907 +         * @param transformer a function returning the transformation
3908 +         * for an element, or null of there is no transformation (in
3909 +         * which case the action is not applied).
3910 +         */
3911 +        public <U> void forEachValue(Fun<? super V, ? extends U> transformer,
3912 +                                     Action<U> action) {
3913 +            fjp.invoke(ForkJoinTasks.forEachValue
3914 +                       (ConcurrentHashMapV8.this, transformer, action));
3915 +        }
3916 +
3917 +        /**
3918 +         * Returns a non-null result from applying the given search
3919 +         * function on each value, or null if none.  Upon success,
3920 +         * further element processing is suppressed and the results of
3921 +         * any other parallel invocations of the search function are
3922 +         * ignored.
3923 +         *
3924 +         * @param searchFunction a function returning a non-null
3925 +         * result on success, else null
3926 +         * @return a non-null result from applying the given search
3927 +         * function on each value, or null if none
3928 +         *
3929 +         */
3930 +        public <U> U searchValues(Fun<? super V, ? extends U> searchFunction) {
3931 +            return fjp.invoke(ForkJoinTasks.searchValues
3932 +                              (ConcurrentHashMapV8.this, searchFunction));
3933 +        }
3934 +
3935 +        /**
3936 +         * Returns the result of accumulating all values using the
3937 +         * given reducer to combine values, or null if none.
3938 +         *
3939 +         * @param reducer a commutative associative combining function
3940 +         * @return  the result of accumulating all values
3941 +         */
3942 +        public V reduceValues(BiFun<? super V, ? super V, ? extends V> reducer) {
3943 +            return fjp.invoke(ForkJoinTasks.reduceValues
3944 +                              (ConcurrentHashMapV8.this, reducer));
3945 +        }
3946 +
3947 +        /**
3948 +         * Returns the result of accumulating the given transformation
3949 +         * of all values using the given reducer to combine values, or
3950 +         * null if none.
3951 +         *
3952 +         * @param transformer a function returning the transformation
3953 +         * for an element, or null of there is no transformation (in
3954 +         * which case it is not combined).
3955 +         * @param reducer a commutative associative combining function
3956 +         * @return the result of accumulating the given transformation
3957 +         * of all values
3958 +         */
3959 +        public <U> U reduceValues(Fun<? super V, ? extends U> transformer,
3960 +                                  BiFun<? super U, ? super U, ? extends U> reducer) {
3961 +            return fjp.invoke(ForkJoinTasks.reduceValues
3962 +                              (ConcurrentHashMapV8.this, transformer, reducer));
3963 +        }
3964 +
3965 +        /**
3966 +         * Returns the result of accumulating the given transformation
3967 +         * of all values using the given reducer to combine values,
3968 +         * and the given basis as an identity value.
3969 +         *
3970 +         * @param transformer a function returning the transformation
3971 +         * for an element
3972 +         * @param basis the identity (initial default value) for the reduction
3973 +         * @param reducer a commutative associative combining function
3974 +         * @return the result of accumulating the given transformation
3975 +         * of all values
3976 +         */
3977 +        public double reduceValuesToDouble(ObjectToDouble<? super V> transformer,
3978 +                                           double basis,
3979 +                                           DoubleByDoubleToDouble reducer) {
3980 +            return fjp.invoke(ForkJoinTasks.reduceValuesToDouble
3981 +                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
3982 +        }
3983 +
3984 +        /**
3985 +         * Returns the result of accumulating the given transformation
3986 +         * of all values using the given reducer to combine values,
3987 +         * and the given basis as an identity value.
3988 +         *
3989 +         * @param transformer a function returning the transformation
3990 +         * for an element
3991 +         * @param basis the identity (initial default value) for the reduction
3992 +         * @param reducer a commutative associative combining function
3993 +         * @return the result of accumulating the given transformation
3994 +         * of all values
3995 +         */
3996 +        public long reduceValuesToLong(ObjectToLong<? super V> transformer,
3997 +                                       long basis,
3998 +                                       LongByLongToLong reducer) {
3999 +            return fjp.invoke(ForkJoinTasks.reduceValuesToLong
4000 +                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4001 +        }
4002 +
4003 +        /**
4004 +         * Returns the result of accumulating the given transformation
4005 +         * of all values using the given reducer to combine values,
4006 +         * and the given basis as an identity value.
4007 +         *
4008 +         * @param transformer a function returning the transformation
4009 +         * for an element
4010 +         * @param basis the identity (initial default value) for the reduction
4011 +         * @param reducer a commutative associative combining function
4012 +         * @return the result of accumulating the given transformation
4013 +         * of all values
4014 +         */
4015 +        public int reduceValuesToInt(ObjectToInt<? super V> transformer,
4016 +                                     int basis,
4017 +                                     IntByIntToInt reducer) {
4018 +            return fjp.invoke(ForkJoinTasks.reduceValuesToInt
4019 +                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4020 +        }
4021 +
4022 +        /**
4023 +         * Performs the given action for each entry.
4024 +         *
4025 +         * @param action the action
4026 +         */
4027 +        public void forEachEntry(Action<Map.Entry<K,V>> action) {
4028 +            fjp.invoke(ForkJoinTasks.forEachEntry
4029 +                       (ConcurrentHashMapV8.this, action));
4030 +        }
4031 +
4032 +        /**
4033 +         * Performs the given action for each non-null transformation
4034 +         * of each entry.
4035 +         *
4036 +         * @param transformer a function returning the transformation
4037 +         * for an element, or null of there is no transformation (in
4038 +         * which case the action is not applied).
4039 +         * @param action the action
4040 +         */
4041 +        public <U> void forEachEntry(Fun<Map.Entry<K,V>, ? extends U> transformer,
4042 +                                     Action<U> action) {
4043 +            fjp.invoke(ForkJoinTasks.forEachEntry
4044 +                       (ConcurrentHashMapV8.this, transformer, action));
4045 +        }
4046 +
4047 +        /**
4048 +         * Returns a non-null result from applying the given search
4049 +         * function on each entry, or null if none.  Upon success,
4050 +         * further element processing is suppressed and the results of
4051 +         * any other parallel invocations of the search function are
4052 +         * ignored.
4053 +         *
4054 +         * @param searchFunction a function returning a non-null
4055 +         * result on success, else null
4056 +         * @return a non-null result from applying the given search
4057 +         * function on each entry, or null if none
4058 +         */
4059 +        public <U> U searchEntries(Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4060 +            return fjp.invoke(ForkJoinTasks.searchEntries
4061 +                              (ConcurrentHashMapV8.this, searchFunction));
4062 +        }
4063 +
4064 +        /**
4065 +         * Returns the result of accumulating all entries using the
4066 +         * given reducer to combine values, or null if none.
4067 +         *
4068 +         * @param reducer a commutative associative combining function
4069 +         * @return the result of accumulating all entries
4070 +         */
4071 +        public Map.Entry<K,V> reduceEntries(BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4072 +            return fjp.invoke(ForkJoinTasks.reduceEntries
4073 +                              (ConcurrentHashMapV8.this, reducer));
4074 +        }
4075 +
4076 +        /**
4077 +         * Returns the result of accumulating the given transformation
4078 +         * of all entries using the given reducer to combine values,
4079 +         * or null if none.
4080 +         *
4081 +         * @param transformer a function returning the transformation
4082 +         * for an element, or null of there is no transformation (in
4083 +         * which case it is not combined).
4084 +         * @param reducer a commutative associative combining function
4085 +         * @return the result of accumulating the given transformation
4086 +         * of all entries
4087 +         */
4088 +        public <U> U reduceEntries(Fun<Map.Entry<K,V>, ? extends U> transformer,
4089 +                                   BiFun<? super U, ? super U, ? extends U> reducer) {
4090 +            return fjp.invoke(ForkJoinTasks.reduceEntries
4091 +                              (ConcurrentHashMapV8.this, transformer, reducer));
4092 +        }
4093 +
4094 +        /**
4095 +         * Returns the result of accumulating the given transformation
4096 +         * of all entries using the given reducer to combine values,
4097 +         * and the given basis as an identity value.
4098 +         *
4099 +         * @param transformer a function returning the transformation
4100 +         * for an element
4101 +         * @param basis the identity (initial default value) for the reduction
4102 +         * @param reducer a commutative associative combining function
4103 +         * @return the result of accumulating the given transformation
4104 +         * of all entries
4105 +         */
4106 +        public double reduceEntriesToDouble(ObjectToDouble<Map.Entry<K,V>> transformer,
4107 +                                            double basis,
4108 +                                            DoubleByDoubleToDouble reducer) {
4109 +            return fjp.invoke(ForkJoinTasks.reduceEntriesToDouble
4110 +                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4111 +        }
4112 +
4113 +        /**
4114 +         * Returns the result of accumulating the given transformation
4115 +         * of all entries using the given reducer to combine values,
4116 +         * and the given basis as an identity value.
4117 +         *
4118 +         * @param transformer a function returning the transformation
4119 +         * for an element
4120 +         * @param basis the identity (initial default value) for the reduction
4121 +         * @param reducer a commutative associative combining function
4122 +         * @return  the result of accumulating the given transformation
4123 +         * of all entries
4124 +         */
4125 +        public long reduceEntriesToLong(ObjectToLong<Map.Entry<K,V>> transformer,
4126 +                                        long basis,
4127 +                                        LongByLongToLong reducer) {
4128 +            return fjp.invoke(ForkJoinTasks.reduceEntriesToLong
4129 +                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4130 +        }
4131 +
4132 +        /**
4133 +         * Returns the result of accumulating the given transformation
4134 +         * of all entries using the given reducer to combine values,
4135 +         * and the given basis as an identity value.
4136 +         *
4137 +         * @param transformer a function returning the transformation
4138 +         * for an element
4139 +         * @param basis the identity (initial default value) for the reduction
4140 +         * @param reducer a commutative associative combining function
4141 +         * @return the result of accumulating the given transformation
4142 +         * of all entries
4143 +         */
4144 +        public int reduceEntriesToInt(ObjectToInt<Map.Entry<K,V>> transformer,
4145 +                                      int basis,
4146 +                                      IntByIntToInt reducer) {
4147 +            return fjp.invoke(ForkJoinTasks.reduceEntriesToInt
4148 +                              (ConcurrentHashMapV8.this, transformer, basis, reducer));
4149 +        }
4150 +    }
4151 +
4152 +    // ---------------------------------------------------------------------
4153 +
4154 +    /**
4155 +     * Predefined tasks for performing bulk parallel operations on
4156 +     * ConcurrentHashMaps. These tasks follow the forms and rules used
4157 +     * in class {@link Parallel}. Each method has the same name, but
4158 +     * returns a task rather than invoking it. These methods may be
4159 +     * useful in custom applications such as submitting a task without
4160 +     * waiting for completion, or combining with other tasks.
4161 +     */
4162 +    public static class ForkJoinTasks {
4163 +        private ForkJoinTasks() {}
4164 +
4165 +        /**
4166 +         * Returns a task that when invoked, performs the given
4167 +         * action for each (key, value)
4168 +         *
4169 +         * @param map the map
4170 +         * @param action the action
4171 +         * @return the task
4172 +         */
4173 +        public static <K,V> ForkJoinTask<Void> forEach
4174 +            (ConcurrentHashMapV8<K,V> map,
4175 +             BiAction<K,V> action) {
4176 +            if (action == null) throw new NullPointerException();
4177 +            return new ForEachMappingTask<K,V>(map, action);
4178 +        }
4179 +
4180 +        /**
4181 +         * Returns a task that when invoked, performs the given
4182 +         * action for each non-null transformation of each (key, value)
4183 +         *
4184 +         * @param map the map
4185 +         * @param transformer a function returning the transformation
4186 +         * for an element, or null of there is no transformation (in
4187 +         * which case the action is not applied).
4188 +         * @param action the action
4189 +         * @return the task
4190 +         */
4191 +        public static <K,V,U> ForkJoinTask<Void> forEach
4192 +            (ConcurrentHashMapV8<K,V> map,
4193 +             BiFun<? super K, ? super V, ? extends U> transformer,
4194 +             Action<U> action) {
4195 +            if (transformer == null || action == null)
4196 +                throw new NullPointerException();
4197 +            return new ForEachTransformedMappingTask<K,V,U>
4198 +                (map, transformer, action);
4199 +        }
4200 +
4201 +        /**
4202 +         * Returns a task that when invoked, returns a non-null result
4203 +         * from applying the given search function on each (key,
4204 +         * value), or null if none. Upon success, further element
4205 +         * processing is suppressed and the results of any other
4206 +         * parallel invocations of the search function are ignored.
4207 +         *
4208 +         * @param map the map
4209 +         * @param searchFunction a function returning a non-null
4210 +         * result on success, else null
4211 +         * @return the task
4212 +         */
4213 +        public static <K,V,U> ForkJoinTask<U> search
4214 +            (ConcurrentHashMapV8<K,V> map,
4215 +             BiFun<? super K, ? super V, ? extends U> searchFunction) {
4216 +            if (searchFunction == null) throw new NullPointerException();
4217 +            return new SearchMappingsTask<K,V,U>
4218 +                (map, searchFunction,
4219 +                 new AtomicReference<U>());
4220 +        }
4221 +
4222 +        /**
4223 +         * Returns a task that when invoked, returns the result of
4224 +         * accumulating the given transformation of all (key, value) pairs
4225 +         * using the given reducer to combine values, or null if none.
4226 +         *
4227 +         * @param map the map
4228 +         * @param transformer a function returning the transformation
4229 +         * for an element, or null of there is no transformation (in
4230 +         * which case it is not combined).
4231 +         * @param reducer a commutative associative combining function
4232 +         * @return the task
4233 +         */
4234 +        public static <K,V,U> ForkJoinTask<U> reduce
4235 +            (ConcurrentHashMapV8<K,V> map,
4236 +             BiFun<? super K, ? super V, ? extends U> transformer,
4237 +             BiFun<? super U, ? super U, ? extends U> reducer) {
4238 +            if (transformer == null || reducer == null)
4239 +                throw new NullPointerException();
4240 +            return new MapReduceMappingsTask<K,V,U>
4241 +                (map, transformer, reducer);
4242 +        }
4243 +
4244 +        /**
4245 +         * Returns a task that when invoked, returns the result of
4246 +         * accumulating the given transformation of all (key, value) pairs
4247 +         * using the given reducer to combine values, and the given
4248 +         * basis as an identity value.
4249 +         *
4250 +         * @param map the map
4251 +         * @param transformer a function returning the transformation
4252 +         * for an element
4253 +         * @param basis the identity (initial default value) for the reduction
4254 +         * @param reducer a commutative associative combining function
4255 +         * @return the task
4256 +         */
4257 +        public static <K,V> ForkJoinTask<Double> reduceToDouble
4258 +            (ConcurrentHashMapV8<K,V> map,
4259 +             ObjectByObjectToDouble<? super K, ? super V> transformer,
4260 +             double basis,
4261 +             DoubleByDoubleToDouble reducer) {
4262 +            if (transformer == null || reducer == null)
4263 +                throw new NullPointerException();
4264 +            return new MapReduceMappingsToDoubleTask<K,V>
4265 +                (map, transformer, basis, reducer);
4266 +        }
4267 +
4268 +        /**
4269 +         * Returns a task that when invoked, returns the result of
4270 +         * accumulating the given transformation of all (key, value) pairs
4271 +         * using the given reducer to combine values, and the given
4272 +         * basis as an identity value.
4273 +         *
4274 +         * @param map the map
4275 +         * @param transformer a function returning the transformation
4276 +         * for an element
4277 +         * @param basis the identity (initial default value) for the reduction
4278 +         * @param reducer a commutative associative combining function
4279 +         * @return the task
4280 +         */
4281 +        public static <K,V> ForkJoinTask<Long> reduceToLong
4282 +            (ConcurrentHashMapV8<K,V> map,
4283 +             ObjectByObjectToLong<? super K, ? super V> transformer,
4284 +             long basis,
4285 +             LongByLongToLong reducer) {
4286 +            if (transformer == null || reducer == null)
4287 +                throw new NullPointerException();
4288 +            return new MapReduceMappingsToLongTask<K,V>
4289 +                (map, transformer, basis, reducer);
4290 +        }
4291 +
4292 +        /**
4293 +         * Returns a task that when invoked, returns the result of
4294 +         * accumulating the given transformation of all (key, value) pairs
4295 +         * using the given reducer to combine values, and the given
4296 +         * basis as an identity value.
4297 +         *
4298 +         * @param transformer a function returning the transformation
4299 +         * for an element
4300 +         * @param basis the identity (initial default value) for the reduction
4301 +         * @param reducer a commutative associative combining function
4302 +         * @return the task
4303 +         */
4304 +        public static <K,V> ForkJoinTask<Integer> reduceToInt
4305 +            (ConcurrentHashMapV8<K,V> map,
4306 +             ObjectByObjectToInt<? super K, ? super V> transformer,
4307 +             int basis,
4308 +             IntByIntToInt reducer) {
4309 +            if (transformer == null || reducer == null)
4310 +                throw new NullPointerException();
4311 +            return new MapReduceMappingsToIntTask<K,V>
4312 +                (map, transformer, basis, reducer);
4313 +        }
4314 +
4315 +        /**
4316 +         * Returns a task that when invoked, performs the given action
4317 +         * for each key.
4318 +         *
4319 +         * @param map the map
4320 +         * @param action the action
4321 +         * @return the task
4322 +         */
4323 +        public static <K,V> ForkJoinTask<Void> forEachKey
4324 +            (ConcurrentHashMapV8<K,V> map,
4325 +             Action<K> action) {
4326 +            if (action == null) throw new NullPointerException();
4327 +            return new ForEachKeyTask<K,V>(map, action);
4328 +        }
4329 +
4330 +        /**
4331 +         * Returns a task that when invoked, performs the given action
4332 +         * for each non-null transformation of each key.
4333 +         *
4334 +         * @param map the map
4335 +         * @param transformer a function returning the transformation
4336 +         * for an element, or null of there is no transformation (in
4337 +         * which case the action is not applied).
4338 +         * @param action the action
4339 +         * @return the task
4340 +         */
4341 +        public static <K,V,U> ForkJoinTask<Void> forEachKey
4342 +            (ConcurrentHashMapV8<K,V> map,
4343 +             Fun<? super K, ? extends U> transformer,
4344 +             Action<U> action) {
4345 +            if (transformer == null || action == null)
4346 +                throw new NullPointerException();
4347 +            return new ForEachTransformedKeyTask<K,V,U>
4348 +                (map, transformer, action);
4349 +        }
4350 +
4351 +        /**
4352 +         * Returns a task that when invoked, returns a non-null result
4353 +         * from applying the given search function on each key, or
4354 +         * null if none.  Upon success, further element processing is
4355 +         * suppressed and the results of any other parallel
4356 +         * invocations of the search function are ignored.
4357 +         *
4358 +         * @param map the map
4359 +         * @param searchFunction a function returning a non-null
4360 +         * result on success, else null
4361 +         * @return the task
4362 +         */
4363 +        public static <K,V,U> ForkJoinTask<U> searchKeys
4364 +            (ConcurrentHashMapV8<K,V> map,
4365 +             Fun<? super K, ? extends U> searchFunction) {
4366 +            if (searchFunction == null) throw new NullPointerException();
4367 +            return new SearchKeysTask<K,V,U>
4368 +                (map, searchFunction,
4369 +                 new AtomicReference<U>());
4370 +        }
4371 +
4372 +        /**
4373 +         * Returns a task that when invoked, returns the result of
4374 +         * accumulating all keys using the given reducer to combine
4375 +         * values, or null if none.
4376 +         *
4377 +         * @param map the map
4378 +         * @param reducer a commutative associative combining function
4379 +         * @return the task
4380 +         */
4381 +        public static <K,V> ForkJoinTask<K> reduceKeys
4382 +            (ConcurrentHashMapV8<K,V> map,
4383 +             BiFun<? super K, ? super K, ? extends K> reducer) {
4384 +            if (reducer == null) throw new NullPointerException();
4385 +            return new ReduceKeysTask<K,V>
4386 +                (map, reducer);
4387 +        }
4388 +
4389 +        /**
4390 +         * Returns a task that when invoked, returns the result of
4391 +         * accumulating the given transformation of all keys using the given
4392 +         * reducer to combine values, or null if none.
4393 +         *
4394 +         * @param map the map
4395 +         * @param transformer a function returning the transformation
4396 +         * for an element, or null of there is no transformation (in
4397 +         * which case it is not combined).
4398 +         * @param reducer a commutative associative combining function
4399 +         * @return the task
4400 +         */
4401 +        public static <K,V,U> ForkJoinTask<U> reduceKeys
4402 +            (ConcurrentHashMapV8<K,V> map,
4403 +             Fun<? super K, ? extends U> transformer,
4404 +             BiFun<? super U, ? super U, ? extends U> reducer) {
4405 +            if (transformer == null || reducer == null)
4406 +                throw new NullPointerException();
4407 +            return new MapReduceKeysTask<K,V,U>
4408 +                (map, transformer, reducer);
4409 +        }
4410 +
4411 +        /**
4412 +         * Returns a task that when invoked, returns the result of
4413 +         * accumulating the given transformation of all keys using the given
4414 +         * reducer to combine values, and the given basis as an
4415 +         * identity value.
4416 +         *
4417 +         * @param map the map
4418 +         * @param transformer a function returning the transformation
4419 +         * for an element
4420 +         * @param basis the identity (initial default value) for the reduction
4421 +         * @param reducer a commutative associative combining function
4422 +         * @return the task
4423 +         */
4424 +        public static <K,V> ForkJoinTask<Double> reduceKeysToDouble
4425 +            (ConcurrentHashMapV8<K,V> map,
4426 +             ObjectToDouble<? super K> transformer,
4427 +             double basis,
4428 +             DoubleByDoubleToDouble reducer) {
4429 +            if (transformer == null || reducer == null)
4430 +                throw new NullPointerException();
4431 +            return new MapReduceKeysToDoubleTask<K,V>
4432 +                (map, transformer, basis, reducer);
4433 +        }
4434 +
4435 +        /**
4436 +         * Returns a task that when invoked, returns the result of
4437 +         * accumulating the given transformation of all keys using the given
4438 +         * reducer to combine values, and the given basis as an
4439 +         * identity value.
4440 +         *
4441 +         * @param map the map
4442 +         * @param transformer a function returning the transformation
4443 +         * for an element
4444 +         * @param basis the identity (initial default value) for the reduction
4445 +         * @param reducer a commutative associative combining function
4446 +         * @return the task
4447 +         */
4448 +        public static <K,V> ForkJoinTask<Long> reduceKeysToLong
4449 +            (ConcurrentHashMapV8<K,V> map,
4450 +             ObjectToLong<? super K> transformer,
4451 +             long basis,
4452 +             LongByLongToLong reducer) {
4453 +            if (transformer == null || reducer == null)
4454 +                throw new NullPointerException();
4455 +            return new MapReduceKeysToLongTask<K,V>
4456 +                (map, transformer, basis, reducer);
4457 +        }
4458 +
4459 +        /**
4460 +         * Returns a task that when invoked, returns the result of
4461 +         * accumulating the given transformation of all keys using the given
4462 +         * reducer to combine values, and the given basis as an
4463 +         * identity value.
4464 +         *
4465 +         * @param map the map
4466 +         * @param transformer a function returning the transformation
4467 +         * for an element
4468 +         * @param basis the identity (initial default value) for the reduction
4469 +         * @param reducer a commutative associative combining function
4470 +         * @return the task
4471 +         */
4472 +        public static <K,V> ForkJoinTask<Integer> reduceKeysToInt
4473 +            (ConcurrentHashMapV8<K,V> map,
4474 +             ObjectToInt<? super K> transformer,
4475 +             int basis,
4476 +             IntByIntToInt reducer) {
4477 +            if (transformer == null || reducer == null)
4478 +                throw new NullPointerException();
4479 +            return new MapReduceKeysToIntTask<K,V>
4480 +                (map, transformer, basis, reducer);
4481 +        }
4482 +
4483 +        /**
4484 +         * Returns a task that when invoked, performs the given action
4485 +         * for each value.
4486 +         *
4487 +         * @param map the map
4488 +         * @param action the action
4489 +         */
4490 +        public static <K,V> ForkJoinTask<Void> forEachValue
4491 +            (ConcurrentHashMapV8<K,V> map,
4492 +             Action<V> action) {
4493 +            if (action == null) throw new NullPointerException();
4494 +            return new ForEachValueTask<K,V>(map, action);
4495 +        }
4496 +
4497 +        /**
4498 +         * Returns a task that when invoked, performs the given action
4499 +         * for each non-null transformation of each value.
4500 +         *
4501 +         * @param map the map
4502 +         * @param transformer a function returning the transformation
4503 +         * for an element, or null of there is no transformation (in
4504 +         * which case the action is not applied).
4505 +         * @param action the action
4506 +         */
4507 +        public static <K,V,U> ForkJoinTask<Void> forEachValue
4508 +            (ConcurrentHashMapV8<K,V> map,
4509 +             Fun<? super V, ? extends U> transformer,
4510 +             Action<U> action) {
4511 +            if (transformer == null || action == null)
4512 +                throw new NullPointerException();
4513 +            return new ForEachTransformedValueTask<K,V,U>
4514 +                (map, transformer, action);
4515 +        }
4516 +
4517 +        /**
4518 +         * Returns a task that when invoked, returns a non-null result
4519 +         * from applying the given search function on each value, or
4520 +         * null if none.  Upon success, further element processing is
4521 +         * suppressed and the results of any other parallel
4522 +         * invocations of the search function are ignored.
4523 +         *
4524 +         * @param map the map
4525 +         * @param searchFunction a function returning a non-null
4526 +         * result on success, else null
4527 +         * @return the task
4528 +         *
4529 +         */
4530 +        public static <K,V,U> ForkJoinTask<U> searchValues
4531 +            (ConcurrentHashMapV8<K,V> map,
4532 +             Fun<? super V, ? extends U> searchFunction) {
4533 +            if (searchFunction == null) throw new NullPointerException();
4534 +            return new SearchValuesTask<K,V,U>
4535 +                (map, searchFunction,
4536 +                 new AtomicReference<U>());
4537 +        }
4538 +
4539 +        /**
4540 +         * Returns a task that when invoked, returns the result of
4541 +         * accumulating all values using the given reducer to combine
4542 +         * values, or null if none.
4543 +         *
4544 +         * @param map the map
4545 +         * @param reducer a commutative associative combining function
4546 +         * @return the task
4547 +         */
4548 +        public static <K,V> ForkJoinTask<V> reduceValues
4549 +            (ConcurrentHashMapV8<K,V> map,
4550 +             BiFun<? super V, ? super V, ? extends V> reducer) {
4551 +            if (reducer == null) throw new NullPointerException();
4552 +            return new ReduceValuesTask<K,V>
4553 +                (map, reducer);
4554 +        }
4555 +
4556 +        /**
4557 +         * Returns a task that when invoked, returns the result of
4558 +         * accumulating the given transformation of all values using the
4559 +         * given reducer to combine values, or null if none.
4560 +         *
4561 +         * @param map the map
4562 +         * @param transformer a function returning the transformation
4563 +         * for an element, or null of there is no transformation (in
4564 +         * which case it is not combined).
4565 +         * @param reducer a commutative associative combining function
4566 +         * @return the task
4567 +         */
4568 +        public static <K,V,U> ForkJoinTask<U> reduceValues
4569 +            (ConcurrentHashMapV8<K,V> map,
4570 +             Fun<? super V, ? extends U> transformer,
4571 +             BiFun<? super U, ? super U, ? extends U> reducer) {
4572 +            if (transformer == null || reducer == null)
4573 +                throw new NullPointerException();
4574 +            return new MapReduceValuesTask<K,V,U>
4575 +                (map, transformer, reducer);
4576 +        }
4577 +
4578 +        /**
4579 +         * Returns a task that when invoked, returns the result of
4580 +         * accumulating the given transformation of all values using the
4581 +         * given reducer to combine values, and the given basis as an
4582 +         * identity value.
4583 +         *
4584 +         * @param map the map
4585 +         * @param transformer a function returning the transformation
4586 +         * for an element
4587 +         * @param basis the identity (initial default value) for the reduction
4588 +         * @param reducer a commutative associative combining function
4589 +         * @return the task
4590 +         */
4591 +        public static <K,V> ForkJoinTask<Double> reduceValuesToDouble
4592 +            (ConcurrentHashMapV8<K,V> map,
4593 +             ObjectToDouble<? super V> transformer,
4594 +             double basis,
4595 +             DoubleByDoubleToDouble reducer) {
4596 +            if (transformer == null || reducer == null)
4597 +                throw new NullPointerException();
4598 +            return new MapReduceValuesToDoubleTask<K,V>
4599 +                (map, transformer, basis, reducer);
4600 +        }
4601 +
4602 +        /**
4603 +         * Returns a task that when invoked, returns the result of
4604 +         * accumulating the given transformation of all values using the
4605 +         * given reducer to combine values, and the given basis as an
4606 +         * identity value.
4607 +         *
4608 +         * @param map the map
4609 +         * @param transformer a function returning the transformation
4610 +         * for an element
4611 +         * @param basis the identity (initial default value) for the reduction
4612 +         * @param reducer a commutative associative combining function
4613 +         * @return the task
4614 +         */
4615 +        public static <K,V> ForkJoinTask<Long> reduceValuesToLong
4616 +            (ConcurrentHashMapV8<K,V> map,
4617 +             ObjectToLong<? super V> transformer,
4618 +             long basis,
4619 +             LongByLongToLong reducer) {
4620 +            if (transformer == null || reducer == null)
4621 +                throw new NullPointerException();
4622 +            return new MapReduceValuesToLongTask<K,V>
4623 +                (map, transformer, basis, reducer);
4624 +        }
4625 +
4626 +        /**
4627 +         * Returns a task that when invoked, returns the result of
4628 +         * accumulating the given transformation of all values using the
4629 +         * given reducer to combine values, and the given basis as an
4630 +         * identity value.
4631 +         *
4632 +         * @param map the map
4633 +         * @param transformer a function returning the transformation
4634 +         * for an element
4635 +         * @param basis the identity (initial default value) for the reduction
4636 +         * @param reducer a commutative associative combining function
4637 +         * @return the task
4638 +         */
4639 +        public static <K,V> ForkJoinTask<Integer> reduceValuesToInt
4640 +            (ConcurrentHashMapV8<K,V> map,
4641 +             ObjectToInt<? super V> transformer,
4642 +             int basis,
4643 +             IntByIntToInt reducer) {
4644 +            if (transformer == null || reducer == null)
4645 +                throw new NullPointerException();
4646 +            return new MapReduceValuesToIntTask<K,V>
4647 +                (map, transformer, basis, reducer);
4648 +        }
4649 +
4650 +        /**
4651 +         * Returns a task that when invoked, perform the given action
4652 +         * for each entry.
4653 +         *
4654 +         * @param map the map
4655 +         * @param action the action
4656 +         */
4657 +        public static <K,V> ForkJoinTask<Void> forEachEntry
4658 +            (ConcurrentHashMapV8<K,V> map,
4659 +             Action<Map.Entry<K,V>> action) {
4660 +            if (action == null) throw new NullPointerException();
4661 +            return new ForEachEntryTask<K,V>(map, action);
4662 +        }
4663 +
4664 +        /**
4665 +         * Returns a task that when invoked, perform the given action
4666 +         * for each non-null transformation of each entry.
4667 +         *
4668 +         * @param map the map
4669 +         * @param transformer a function returning the transformation
4670 +         * for an element, or null of there is no transformation (in
4671 +         * which case the action is not applied).
4672 +         * @param action the action
4673 +         */
4674 +        public static <K,V,U> ForkJoinTask<Void> forEachEntry
4675 +            (ConcurrentHashMapV8<K,V> map,
4676 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
4677 +             Action<U> action) {
4678 +            if (transformer == null || action == null)
4679 +                throw new NullPointerException();
4680 +            return new ForEachTransformedEntryTask<K,V,U>
4681 +                (map, transformer, action);
4682 +        }
4683 +
4684 +        /**
4685 +         * Returns a task that when invoked, returns a non-null result
4686 +         * from applying the given search function on each entry, or
4687 +         * null if none.  Upon success, further element processing is
4688 +         * suppressed and the results of any other parallel
4689 +         * invocations of the search function are ignored.
4690 +         *
4691 +         * @param map the map
4692 +         * @param searchFunction a function returning a non-null
4693 +         * result on success, else null
4694 +         * @return the task
4695 +         *
4696 +         */
4697 +        public static <K,V,U> ForkJoinTask<U> searchEntries
4698 +            (ConcurrentHashMapV8<K,V> map,
4699 +             Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4700 +            if (searchFunction == null) throw new NullPointerException();
4701 +            return new SearchEntriesTask<K,V,U>
4702 +                (map, searchFunction,
4703 +                 new AtomicReference<U>());
4704 +        }
4705 +
4706 +        /**
4707 +         * Returns a task that when invoked, returns the result of
4708 +         * accumulating all entries using the given reducer to combine
4709 +         * values, or null if none.
4710 +         *
4711 +         * @param map the map
4712 +         * @param reducer a commutative associative combining function
4713 +         * @return the task
4714 +         */
4715 +        public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries
4716 +            (ConcurrentHashMapV8<K,V> map,
4717 +             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4718 +            if (reducer == null) throw new NullPointerException();
4719 +            return new ReduceEntriesTask<K,V>
4720 +                (map, reducer);
4721 +        }
4722 +
4723 +        /**
4724 +         * Returns a task that when invoked, returns the result of
4725 +         * accumulating the given transformation of all entries using the
4726 +         * given reducer to combine values, or null if none.
4727 +         *
4728 +         * @param map the map
4729 +         * @param transformer a function returning the transformation
4730 +         * for an element, or null of there is no transformation (in
4731 +         * which case it is not combined).
4732 +         * @param reducer a commutative associative combining function
4733 +         * @return the task
4734 +         */
4735 +        public static <K,V,U> ForkJoinTask<U> reduceEntries
4736 +            (ConcurrentHashMapV8<K,V> map,
4737 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
4738 +             BiFun<? super U, ? super U, ? extends U> reducer) {
4739 +            if (transformer == null || reducer == null)
4740 +                throw new NullPointerException();
4741 +            return new MapReduceEntriesTask<K,V,U>
4742 +                (map, transformer, reducer);
4743 +        }
4744 +
4745 +        /**
4746 +         * Returns a task that when invoked, returns the result of
4747 +         * accumulating the given transformation of all entries using the
4748 +         * given reducer to combine values, and the given basis as an
4749 +         * identity value.
4750 +         *
4751 +         * @param map the map
4752 +         * @param transformer a function returning the transformation
4753 +         * for an element
4754 +         * @param basis the identity (initial default value) for the reduction
4755 +         * @param reducer a commutative associative combining function
4756 +         * @return the task
4757 +         */
4758 +        public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble
4759 +            (ConcurrentHashMapV8<K,V> map,
4760 +             ObjectToDouble<Map.Entry<K,V>> transformer,
4761 +             double basis,
4762 +             DoubleByDoubleToDouble reducer) {
4763 +            if (transformer == null || reducer == null)
4764 +                throw new NullPointerException();
4765 +            return new MapReduceEntriesToDoubleTask<K,V>
4766 +                (map, transformer, basis, reducer);
4767 +        }
4768 +
4769 +        /**
4770 +         * Returns a task that when invoked, returns the result of
4771 +         * accumulating the given transformation of all entries using the
4772 +         * given reducer to combine values, and the given basis as an
4773 +         * identity value.
4774 +         *
4775 +         * @param map the map
4776 +         * @param transformer a function returning the transformation
4777 +         * for an element
4778 +         * @param basis the identity (initial default value) for the reduction
4779 +         * @param reducer a commutative associative combining function
4780 +         * @return the task
4781 +         */
4782 +        public static <K,V> ForkJoinTask<Long> reduceEntriesToLong
4783 +            (ConcurrentHashMapV8<K,V> map,
4784 +             ObjectToLong<Map.Entry<K,V>> transformer,
4785 +             long basis,
4786 +             LongByLongToLong reducer) {
4787 +            if (transformer == null || reducer == null)
4788 +                throw new NullPointerException();
4789 +            return new MapReduceEntriesToLongTask<K,V>
4790 +                (map, transformer, basis, reducer);
4791 +        }
4792 +
4793 +        /**
4794 +         * Returns a task that when invoked, returns the result of
4795 +         * accumulating the given transformation of all entries using the
4796 +         * given reducer to combine values, and the given basis as an
4797 +         * identity value.
4798 +         *
4799 +         * @param map the map
4800 +         * @param transformer a function returning the transformation
4801 +         * for an element
4802 +         * @param basis the identity (initial default value) for the reduction
4803 +         * @param reducer a commutative associative combining function
4804 +         * @return the task
4805 +         */
4806 +        public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt
4807 +            (ConcurrentHashMapV8<K,V> map,
4808 +             ObjectToInt<Map.Entry<K,V>> transformer,
4809 +             int basis,
4810 +             IntByIntToInt reducer) {
4811 +            if (transformer == null || reducer == null)
4812 +                throw new NullPointerException();
4813 +            return new MapReduceEntriesToIntTask<K,V>
4814 +                (map, transformer, basis, reducer);
4815 +        }
4816 +    }
4817 +
4818 +    // -------------------------------------------------------
4819 +
4820 +    /**
4821 +     * Base for FJ tasks for bulk operations. This adds a variant of
4822 +     * CountedCompleters and some split and merge bookkeeping to
4823 +     * iterator functionality. The forEach and reduce methods are
4824 +     * similar to those illustrated in CountedCompleter documentation,
4825 +     * except that bottom-up reduction completions perform them within
4826 +     * their compute methods. The search methods are like forEach
4827 +     * except they continually poll for success and exit early.  Also,
4828 +     * exceptions are handled in a simpler manner, by just trying to
4829 +     * complete root task exceptionally.
4830 +     */
4831 +    @SuppressWarnings("serial")
4832 +    static abstract class BulkTask<K,V,R> extends Traverser<K,V,R> {
4833 +        final BulkTask<K,V,?> parent;  // completion target
4834 +        int batch;                     // split control
4835 +        int pending;                   // completion control
4836 +
4837 +        /** Constructor for root tasks */
4838 +        BulkTask(ConcurrentHashMapV8<K,V> map) {
4839 +            super(map);
4840 +            this.parent = null;
4841 +            this.batch = -1; // force call to batch() on execution
4842 +        }
4843 +
4844 +        /** Constructor for subtasks */
4845 +        BulkTask(BulkTask<K,V,?> parent, int batch, boolean split) {
4846 +            super(parent, split);
4847 +            this.parent = parent;
4848 +            this.batch = batch;
4849 +        }
4850 +
4851 +        // FJ methods
4852 +
4853 +        /**
4854 +         * Propagates completion. Note that all reduce actions
4855 +         * bypass this method to combine while completing.
4856 +         */
4857 +        final void tryComplete() {
4858 +            BulkTask<K,V,?> a = this, s = a;
4859 +            for (int c;;) {
4860 +                if ((c = a.pending) == 0) {
4861 +                    if ((a = (s = a).parent) == null) {
4862 +                        s.quietlyComplete();
4863 +                        break;
4864 +                    }
4865 +                }
4866 +                else if (U.compareAndSwapInt(a, PENDING, c, c - 1))
4867 +                    break;
4868 +            }
4869 +        }
4870  
4871 +        /**
4872 +         * Forces root task to throw exception unless already complete.
4873 +         */
4874 +        final void tryAbortComputation(Throwable ex) {
4875 +            for (BulkTask<K,V,?> a = this;;) {
4876 +                BulkTask<K,V,?> p = a.parent;
4877 +                if (p == null) {
4878 +                    a.completeExceptionally(ex);
4879 +                    break;
4880 +                }
4881 +                a = p;
4882 +            }
4883          }
4884 +
4885 +        public final boolean exec() {
4886 +            try {
4887 +                compute();
4888 +            }
4889 +            catch (Throwable ex) {
4890 +                tryAbortComputation(ex);
4891 +            }
4892 +            return false;
4893 +        }
4894 +
4895 +        public abstract void compute();
4896 +
4897 +        // utilities
4898 +
4899 +        /** CompareAndSet pending count */
4900 +        final boolean casPending(int cmp, int val) {
4901 +            return U.compareAndSwapInt(this, PENDING, cmp, val);
4902 +        }
4903 +
4904 +        /**
4905 +         * Returns approx exp2 of the number of times (minus one) to
4906 +         * split task by two before executing leaf action. This value
4907 +         * is faster to compute and more convenient to use as a guide
4908 +         * to splitting than is the depth, since it is used while
4909 +         * dividing by two anyway.
4910 +         */
4911 +        final int batch() {
4912 +            int b = batch;
4913 +            if (b < 0) {
4914 +                long n = map.counter.sum();
4915 +                int sp = getPool().getParallelism() << 3; // slack of 8
4916 +                b = batch = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp;
4917 +            }
4918 +            return b;
4919 +        }
4920 +
4921 +        /**
4922 +         * Error message for hoisted null checks of functions
4923 +         */
4924 +        static final String NullFunctionMessage =
4925 +            "Unexpected null function";
4926 +
4927 +        /**
4928 +         * Returns exportable snapshot entry.
4929 +         */
4930 +        static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) {
4931 +            return new AbstractMap.SimpleEntry<K,V>(k, v);
4932 +        }
4933 +
4934 +        // Unsafe mechanics
4935 +        private static final sun.misc.Unsafe U;
4936 +        private static final long PENDING;
4937 +        static {
4938 +            try {
4939 +                U = getUnsafe();
4940 +                PENDING = U.objectFieldOffset
4941 +                    (BulkTask.class.getDeclaredField("pending"));
4942 +            } catch (Exception e) {
4943 +                throw new Error(e);
4944 +            }
4945 +        }
4946 +    }
4947 +
4948 +    /*
4949 +     * Task classes. Coded in a regular but ugly format/style to
4950 +     * simplify checks that each variant differs in the right way from
4951 +     * others.
4952 +     */
4953 +
4954 +    @SuppressWarnings("serial")
4955 +    static final class ForEachKeyTask<K,V>
4956 +        extends BulkTask<K,V,Void> {
4957 +        final Action<K> action;
4958 +        ForEachKeyTask
4959 +            (ConcurrentHashMapV8<K,V> m,
4960 +             Action<K> action) {
4961 +            super(m);
4962 +            this.action = action;
4963 +        }
4964 +        ForEachKeyTask
4965 +            (BulkTask<K,V,?> p, int b, boolean split,
4966 +             Action<K> action) {
4967 +            super(p, b, split);
4968 +            this.action = action;
4969 +        }
4970 +        @SuppressWarnings("unchecked") public final void compute() {
4971 +            final Action<K> action = this.action;
4972 +            if (action == null)
4973 +                throw new Error(NullFunctionMessage);
4974 +            int b = batch(), c;
4975 +            while (b > 1 && baseIndex != baseLimit) {
4976 +                do {} while (!casPending(c = pending, c+1));
4977 +                new ForEachKeyTask<K,V>(this, b >>>= 1, true, action).fork();
4978 +            }
4979 +            while (advance() != null)
4980 +                action.apply((K)nextKey);
4981 +            tryComplete();
4982 +        }
4983 +    }
4984 +
4985 +    @SuppressWarnings("serial")
4986 +    static final class ForEachValueTask<K,V>
4987 +        extends BulkTask<K,V,Void> {
4988 +        final Action<V> action;
4989 +        ForEachValueTask
4990 +            (ConcurrentHashMapV8<K,V> m,
4991 +             Action<V> action) {
4992 +            super(m);
4993 +            this.action = action;
4994 +        }
4995 +        ForEachValueTask
4996 +            (BulkTask<K,V,?> p, int b, boolean split,
4997 +             Action<V> action) {
4998 +            super(p, b, split);
4999 +            this.action = action;
5000 +        }
5001 +        @SuppressWarnings("unchecked") public final void compute() {
5002 +            final Action<V> action = this.action;
5003 +            if (action == null)
5004 +                throw new Error(NullFunctionMessage);
5005 +            int b = batch(), c;
5006 +            while (b > 1 && baseIndex != baseLimit) {
5007 +                do {} while (!casPending(c = pending, c+1));
5008 +                new ForEachValueTask<K,V>(this, b >>>= 1, true, action).fork();
5009 +            }
5010 +            Object v;
5011 +            while ((v = advance()) != null)
5012 +                action.apply((V)v);
5013 +            tryComplete();
5014 +        }
5015 +    }
5016 +
5017 +    @SuppressWarnings("serial")
5018 +    static final class ForEachEntryTask<K,V>
5019 +        extends BulkTask<K,V,Void> {
5020 +        final Action<Entry<K,V>> action;
5021 +        ForEachEntryTask
5022 +            (ConcurrentHashMapV8<K,V> m,
5023 +             Action<Entry<K,V>> action) {
5024 +            super(m);
5025 +            this.action = action;
5026 +        }
5027 +        ForEachEntryTask
5028 +            (BulkTask<K,V,?> p, int b, boolean split,
5029 +             Action<Entry<K,V>> action) {
5030 +            super(p, b, split);
5031 +            this.action = action;
5032 +        }
5033 +        @SuppressWarnings("unchecked") public final void compute() {
5034 +            final Action<Entry<K,V>> action = this.action;
5035 +            if (action == null)
5036 +                throw new Error(NullFunctionMessage);
5037 +            int b = batch(), c;
5038 +            while (b > 1 && baseIndex != baseLimit) {
5039 +                do {} while (!casPending(c = pending, c+1));
5040 +                new ForEachEntryTask<K,V>(this, b >>>= 1, true, action).fork();
5041 +            }
5042 +            Object v;
5043 +            while ((v = advance()) != null)
5044 +                action.apply(entryFor((K)nextKey, (V)v));
5045 +            tryComplete();
5046 +        }
5047 +    }
5048 +
5049 +    @SuppressWarnings("serial")
5050 +    static final class ForEachMappingTask<K,V>
5051 +        extends BulkTask<K,V,Void> {
5052 +        final BiAction<K,V> action;
5053 +        ForEachMappingTask
5054 +            (ConcurrentHashMapV8<K,V> m,
5055 +             BiAction<K,V> action) {
5056 +            super(m);
5057 +            this.action = action;
5058 +        }
5059 +        ForEachMappingTask
5060 +            (BulkTask<K,V,?> p, int b, boolean split,
5061 +             BiAction<K,V> action) {
5062 +            super(p, b, split);
5063 +            this.action = action;
5064 +        }
5065 +
5066 +        @SuppressWarnings("unchecked") public final void compute() {
5067 +            final BiAction<K,V> action = this.action;
5068 +            if (action == null)
5069 +                throw new Error(NullFunctionMessage);
5070 +            int b = batch(), c;
5071 +            while (b > 1 && baseIndex != baseLimit) {
5072 +                do {} while (!casPending(c = pending, c+1));
5073 +                new ForEachMappingTask<K,V>(this, b >>>= 1, true,
5074 +                                            action).fork();
5075 +            }
5076 +            Object v;
5077 +            while ((v = advance()) != null)
5078 +                action.apply((K)nextKey, (V)v);
5079 +            tryComplete();
5080 +        }
5081 +    }
5082 +
5083 +    @SuppressWarnings("serial")
5084 +    static final class ForEachTransformedKeyTask<K,V,U>
5085 +        extends BulkTask<K,V,Void> {
5086 +        final Fun<? super K, ? extends U> transformer;
5087 +        final Action<U> action;
5088 +        ForEachTransformedKeyTask
5089 +            (ConcurrentHashMapV8<K,V> m,
5090 +             Fun<? super K, ? extends U> transformer,
5091 +             Action<U> action) {
5092 +            super(m);
5093 +            this.transformer = transformer;
5094 +            this.action = action;
5095 +
5096 +        }
5097 +        ForEachTransformedKeyTask
5098 +            (BulkTask<K,V,?> p, int b, boolean split,
5099 +             Fun<? super K, ? extends U> transformer,
5100 +             Action<U> action) {
5101 +            super(p, b, split);
5102 +            this.transformer = transformer;
5103 +            this.action = action;
5104 +        }
5105 +        @SuppressWarnings("unchecked") public final void compute() {
5106 +            final Fun<? super K, ? extends U> transformer =
5107 +                this.transformer;
5108 +            final Action<U> action = this.action;
5109 +            if (transformer == null || action == null)
5110 +                throw new Error(NullFunctionMessage);
5111 +            int b = batch(), c;
5112 +            while (b > 1 && baseIndex != baseLimit) {
5113 +                do {} while (!casPending(c = pending, c+1));
5114 +                new ForEachTransformedKeyTask<K,V,U>
5115 +                    (this, b >>>= 1, true, transformer, action).fork();
5116 +            }
5117 +            U u;
5118 +            while (advance() != null) {
5119 +                if ((u = transformer.apply((K)nextKey)) != null)
5120 +                    action.apply(u);
5121 +            }
5122 +            tryComplete();
5123 +        }
5124 +    }
5125 +
5126 +    @SuppressWarnings("serial")
5127 +    static final class ForEachTransformedValueTask<K,V,U>
5128 +        extends BulkTask<K,V,Void> {
5129 +        final Fun<? super V, ? extends U> transformer;
5130 +        final Action<U> action;
5131 +        ForEachTransformedValueTask
5132 +            (ConcurrentHashMapV8<K,V> m,
5133 +             Fun<? super V, ? extends U> transformer,
5134 +             Action<U> action) {
5135 +            super(m);
5136 +            this.transformer = transformer;
5137 +            this.action = action;
5138 +
5139 +        }
5140 +        ForEachTransformedValueTask
5141 +            (BulkTask<K,V,?> p, int b, boolean split,
5142 +             Fun<? super V, ? extends U> transformer,
5143 +             Action<U> action) {
5144 +            super(p, b, split);
5145 +            this.transformer = transformer;
5146 +            this.action = action;
5147 +        }
5148 +        @SuppressWarnings("unchecked") public final void compute() {
5149 +            final Fun<? super V, ? extends U> transformer =
5150 +                this.transformer;
5151 +            final Action<U> action = this.action;
5152 +            if (transformer == null || action == null)
5153 +                throw new Error(NullFunctionMessage);
5154 +            int b = batch(), c;
5155 +            while (b > 1 && baseIndex != baseLimit) {
5156 +                do {} while (!casPending(c = pending, c+1));
5157 +                new ForEachTransformedValueTask<K,V,U>
5158 +                    (this, b >>>= 1, true, transformer, action).fork();
5159 +            }
5160 +            Object v; U u;
5161 +            while ((v = advance()) != null) {
5162 +                if ((u = transformer.apply((V)v)) != null)
5163 +                    action.apply(u);
5164 +            }
5165 +            tryComplete();
5166 +        }
5167 +    }
5168 +
5169 +    @SuppressWarnings("serial")
5170 +    static final class ForEachTransformedEntryTask<K,V,U>
5171 +        extends BulkTask<K,V,Void> {
5172 +        final Fun<Map.Entry<K,V>, ? extends U> transformer;
5173 +        final Action<U> action;
5174 +        ForEachTransformedEntryTask
5175 +            (ConcurrentHashMapV8<K,V> m,
5176 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
5177 +             Action<U> action) {
5178 +            super(m);
5179 +            this.transformer = transformer;
5180 +            this.action = action;
5181 +
5182 +        }
5183 +        ForEachTransformedEntryTask
5184 +            (BulkTask<K,V,?> p, int b, boolean split,
5185 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
5186 +             Action<U> action) {
5187 +            super(p, b, split);
5188 +            this.transformer = transformer;
5189 +            this.action = action;
5190 +        }
5191 +        @SuppressWarnings("unchecked") public final void compute() {
5192 +            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5193 +                this.transformer;
5194 +            final Action<U> action = this.action;
5195 +            if (transformer == null || action == null)
5196 +                throw new Error(NullFunctionMessage);
5197 +            int b = batch(), c;
5198 +            while (b > 1 && baseIndex != baseLimit) {
5199 +                do {} while (!casPending(c = pending, c+1));
5200 +                new ForEachTransformedEntryTask<K,V,U>
5201 +                    (this, b >>>= 1, true, transformer, action).fork();
5202 +            }
5203 +            Object v; U u;
5204 +            while ((v = advance()) != null) {
5205 +                if ((u = transformer.apply(entryFor((K)nextKey, (V)v))) != null)
5206 +                    action.apply(u);
5207 +            }
5208 +            tryComplete();
5209 +        }
5210 +    }
5211 +
5212 +    @SuppressWarnings("serial")
5213 +    static final class ForEachTransformedMappingTask<K,V,U>
5214 +        extends BulkTask<K,V,Void> {
5215 +        final BiFun<? super K, ? super V, ? extends U> transformer;
5216 +        final Action<U> action;
5217 +        ForEachTransformedMappingTask
5218 +            (ConcurrentHashMapV8<K,V> m,
5219 +             BiFun<? super K, ? super V, ? extends U> transformer,
5220 +             Action<U> action) {
5221 +            super(m);
5222 +            this.transformer = transformer;
5223 +            this.action = action;
5224 +
5225 +        }
5226 +        ForEachTransformedMappingTask
5227 +            (BulkTask<K,V,?> p, int b, boolean split,
5228 +             BiFun<? super K, ? super V, ? extends U> transformer,
5229 +             Action<U> action) {
5230 +            super(p, b, split);
5231 +            this.transformer = transformer;
5232 +            this.action = action;
5233 +        }
5234 +        @SuppressWarnings("unchecked") public final void compute() {
5235 +            final BiFun<? super K, ? super V, ? extends U> transformer =
5236 +                this.transformer;
5237 +            final Action<U> action = this.action;
5238 +            if (transformer == null || action == null)
5239 +                throw new Error(NullFunctionMessage);
5240 +            int b = batch(), c;
5241 +            while (b > 1 && baseIndex != baseLimit) {
5242 +                do {} while (!casPending(c = pending, c+1));
5243 +                new ForEachTransformedMappingTask<K,V,U>
5244 +                    (this, b >>>= 1, true, transformer, action).fork();
5245 +            }
5246 +            Object v; U u;
5247 +            while ((v = advance()) != null) {
5248 +                if ((u = transformer.apply((K)nextKey, (V)v)) != null)
5249 +                    action.apply(u);
5250 +            }
5251 +            tryComplete();
5252 +        }
5253 +    }
5254 +
5255 +    @SuppressWarnings("serial")
5256 +    static final class SearchKeysTask<K,V,U>
5257 +        extends BulkTask<K,V,U> {
5258 +        final Fun<? super K, ? extends U> searchFunction;
5259 +        final AtomicReference<U> result;
5260 +        SearchKeysTask
5261 +            (ConcurrentHashMapV8<K,V> m,
5262 +             Fun<? super K, ? extends U> searchFunction,
5263 +             AtomicReference<U> result) {
5264 +            super(m);
5265 +            this.searchFunction = searchFunction; this.result = result;
5266 +        }
5267 +        SearchKeysTask
5268 +            (BulkTask<K,V,?> p, int b, boolean split,
5269 +             Fun<? super K, ? extends U> searchFunction,
5270 +             AtomicReference<U> result) {
5271 +            super(p, b, split);
5272 +            this.searchFunction = searchFunction; this.result = result;
5273 +        }
5274 +        @SuppressWarnings("unchecked") public final void compute() {
5275 +            AtomicReference<U> result = this.result;
5276 +            final Fun<? super K, ? extends U> searchFunction =
5277 +                this.searchFunction;
5278 +            if (searchFunction == null || result == null)
5279 +                throw new Error(NullFunctionMessage);
5280 +            int b = batch(), c;
5281 +            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5282 +                do {} while (!casPending(c = pending, c+1));
5283 +                new SearchKeysTask<K,V,U>(this, b >>>= 1, true,
5284 +                                          searchFunction, result).fork();
5285 +            }
5286 +            U u;
5287 +            while (result.get() == null && advance() != null) {
5288 +                if ((u = searchFunction.apply((K)nextKey)) != null) {
5289 +                    if (result.compareAndSet(null, u)) {
5290 +                        for (BulkTask<K,V,?> a = this, p;;) {
5291 +                            if ((p = a.parent) == null) {
5292 +                                a.quietlyComplete();
5293 +                                break;
5294 +                            }
5295 +                            a = p;
5296 +                        }
5297 +                    }
5298 +                    break;
5299 +                }
5300 +            }
5301 +            tryComplete();
5302 +        }
5303 +        public final U getRawResult() { return result.get(); }
5304 +    }
5305 +
5306 +    @SuppressWarnings("serial")
5307 +    static final class SearchValuesTask<K,V,U>
5308 +        extends BulkTask<K,V,U> {
5309 +        final Fun<? super V, ? extends U> searchFunction;
5310 +        final AtomicReference<U> result;
5311 +        SearchValuesTask
5312 +            (ConcurrentHashMapV8<K,V> m,
5313 +             Fun<? super V, ? extends U> searchFunction,
5314 +             AtomicReference<U> result) {
5315 +            super(m);
5316 +            this.searchFunction = searchFunction; this.result = result;
5317 +        }
5318 +        SearchValuesTask
5319 +            (BulkTask<K,V,?> p, int b, boolean split,
5320 +             Fun<? super V, ? extends U> searchFunction,
5321 +             AtomicReference<U> result) {
5322 +            super(p, b, split);
5323 +            this.searchFunction = searchFunction; this.result = result;
5324 +        }
5325 +        @SuppressWarnings("unchecked") public final void compute() {
5326 +            AtomicReference<U> result = this.result;
5327 +            final Fun<? super V, ? extends U> searchFunction =
5328 +                this.searchFunction;
5329 +            if (searchFunction == null || result == null)
5330 +                throw new Error(NullFunctionMessage);
5331 +            int b = batch(), c;
5332 +            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5333 +                do {} while (!casPending(c = pending, c+1));
5334 +                new SearchValuesTask<K,V,U>(this, b >>>= 1, true,
5335 +                                            searchFunction, result).fork();
5336 +            }
5337 +            Object v; U u;
5338 +            while (result.get() == null && (v = advance()) != null) {
5339 +                if ((u = searchFunction.apply((V)v)) != null) {
5340 +                    if (result.compareAndSet(null, u)) {
5341 +                        for (BulkTask<K,V,?> a = this, p;;) {
5342 +                            if ((p = a.parent) == null) {
5343 +                                a.quietlyComplete();
5344 +                                break;
5345 +                            }
5346 +                            a = p;
5347 +                        }
5348 +                    }
5349 +                    break;
5350 +                }
5351 +            }
5352 +            tryComplete();
5353 +        }
5354 +        public final U getRawResult() { return result.get(); }
5355 +    }
5356 +
5357 +    @SuppressWarnings("serial")
5358 +    static final class SearchEntriesTask<K,V,U>
5359 +        extends BulkTask<K,V,U> {
5360 +        final Fun<Entry<K,V>, ? extends U> searchFunction;
5361 +        final AtomicReference<U> result;
5362 +        SearchEntriesTask
5363 +            (ConcurrentHashMapV8<K,V> m,
5364 +             Fun<Entry<K,V>, ? extends U> searchFunction,
5365 +             AtomicReference<U> result) {
5366 +            super(m);
5367 +            this.searchFunction = searchFunction; this.result = result;
5368 +        }
5369 +        SearchEntriesTask
5370 +            (BulkTask<K,V,?> p, int b, boolean split,
5371 +             Fun<Entry<K,V>, ? extends U> searchFunction,
5372 +             AtomicReference<U> result) {
5373 +            super(p, b, split);
5374 +            this.searchFunction = searchFunction; this.result = result;
5375 +        }
5376 +        @SuppressWarnings("unchecked") public final void compute() {
5377 +            AtomicReference<U> result = this.result;
5378 +            final Fun<Entry<K,V>, ? extends U> searchFunction =
5379 +                this.searchFunction;
5380 +            if (searchFunction == null || result == null)
5381 +                throw new Error(NullFunctionMessage);
5382 +            int b = batch(), c;
5383 +            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5384 +                do {} while (!casPending(c = pending, c+1));
5385 +                new SearchEntriesTask<K,V,U>(this, b >>>= 1, true,
5386 +                                             searchFunction, result).fork();
5387 +            }
5388 +            Object v; U u;
5389 +            while (result.get() == null && (v = advance()) != null) {
5390 +                if ((u = searchFunction.apply(entryFor((K)nextKey, (V)v))) != null) {
5391 +                    if (result.compareAndSet(null, u)) {
5392 +                        for (BulkTask<K,V,?> a = this, p;;) {
5393 +                            if ((p = a.parent) == null) {
5394 +                                a.quietlyComplete();
5395 +                                break;
5396 +                            }
5397 +                            a = p;
5398 +                        }
5399 +                    }
5400 +                    break;
5401 +                }
5402 +            }
5403 +            tryComplete();
5404 +        }
5405 +        public final U getRawResult() { return result.get(); }
5406 +    }
5407 +
5408 +    @SuppressWarnings("serial")
5409 +    static final class SearchMappingsTask<K,V,U>
5410 +        extends BulkTask<K,V,U> {
5411 +        final BiFun<? super K, ? super V, ? extends U> searchFunction;
5412 +        final AtomicReference<U> result;
5413 +        SearchMappingsTask
5414 +            (ConcurrentHashMapV8<K,V> m,
5415 +             BiFun<? super K, ? super V, ? extends U> searchFunction,
5416 +             AtomicReference<U> result) {
5417 +            super(m);
5418 +            this.searchFunction = searchFunction; this.result = result;
5419 +        }
5420 +        SearchMappingsTask
5421 +            (BulkTask<K,V,?> p, int b, boolean split,
5422 +             BiFun<? super K, ? super V, ? extends U> searchFunction,
5423 +             AtomicReference<U> result) {
5424 +            super(p, b, split);
5425 +            this.searchFunction = searchFunction; this.result = result;
5426 +        }
5427 +        @SuppressWarnings("unchecked") public final void compute() {
5428 +            AtomicReference<U> result = this.result;
5429 +            final BiFun<? super K, ? super V, ? extends U> searchFunction =
5430 +                this.searchFunction;
5431 +            if (searchFunction == null || result == null)
5432 +                throw new Error(NullFunctionMessage);
5433 +            int b = batch(), c;
5434 +            while (b > 1 && baseIndex != baseLimit && result.get() == null) {
5435 +                do {} while (!casPending(c = pending, c+1));
5436 +                new SearchMappingsTask<K,V,U>(this, b >>>= 1, true,
5437 +                                              searchFunction, result).fork();
5438 +            }
5439 +            Object v; U u;
5440 +            while (result.get() == null && (v = advance()) != null) {
5441 +                if ((u = searchFunction.apply((K)nextKey, (V)v)) != null) {
5442 +                    if (result.compareAndSet(null, u)) {
5443 +                        for (BulkTask<K,V,?> a = this, p;;) {
5444 +                            if ((p = a.parent) == null) {
5445 +                                a.quietlyComplete();
5446 +                                break;
5447 +                            }
5448 +                            a = p;
5449 +                        }
5450 +                    }
5451 +                    break;
5452 +                }
5453 +            }
5454 +            tryComplete();
5455 +        }
5456 +        public final U getRawResult() { return result.get(); }
5457 +    }
5458 +
5459 +    @SuppressWarnings("serial")
5460 +    static final class ReduceKeysTask<K,V>
5461 +        extends BulkTask<K,V,K> {
5462 +        final BiFun<? super K, ? super K, ? extends K> reducer;
5463 +        K result;
5464 +        ReduceKeysTask<K,V> sibling;
5465 +        ReduceKeysTask
5466 +            (ConcurrentHashMapV8<K,V> m,
5467 +             BiFun<? super K, ? super K, ? extends K> reducer) {
5468 +            super(m);
5469 +            this.reducer = reducer;
5470 +        }
5471 +        ReduceKeysTask
5472 +            (BulkTask<K,V,?> p, int b, boolean split,
5473 +             BiFun<? super K, ? super K, ? extends K> reducer) {
5474 +            super(p, b, split);
5475 +            this.reducer = reducer;
5476 +        }
5477 +
5478 +        @SuppressWarnings("unchecked") public final void compute() {
5479 +            ReduceKeysTask<K,V> t = this;
5480 +            final BiFun<? super K, ? super K, ? extends K> reducer =
5481 +                this.reducer;
5482 +            if (reducer == null)
5483 +                throw new Error(NullFunctionMessage);
5484 +            int b = batch();
5485 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5486 +                b >>>= 1;
5487 +                t.pending = 1;
5488 +                ReduceKeysTask<K,V> rt =
5489 +                    new ReduceKeysTask<K,V>
5490 +                    (t, b, true, reducer);
5491 +                t = new ReduceKeysTask<K,V>
5492 +                    (t, b, false, reducer);
5493 +                t.sibling = rt;
5494 +                rt.sibling = t;
5495 +                rt.fork();
5496 +            }
5497 +            K r = null;
5498 +            while (t.advance() != null) {
5499 +                K u = (K)t.nextKey;
5500 +                r = (r == null) ? u : reducer.apply(r, u);
5501 +            }
5502 +            t.result = r;
5503 +            for (;;) {
5504 +                int c; BulkTask<K,V,?> par; ReduceKeysTask<K,V> s, p; K u;
5505 +                if ((par = t.parent) == null ||
5506 +                    !(par instanceof ReduceKeysTask)) {
5507 +                    t.quietlyComplete();
5508 +                    break;
5509 +                }
5510 +                else if ((c = (p = (ReduceKeysTask<K,V>)par).pending) == 0) {
5511 +                    if ((s = t.sibling) != null && (u = s.result) != null)
5512 +                        r = (r == null) ? u : reducer.apply(r, u);
5513 +                    (t = p).result = r;
5514 +                }
5515 +                else if (p.casPending(c, 0))
5516 +                    break;
5517 +            }
5518 +        }
5519 +        public final K getRawResult() { return result; }
5520 +    }
5521 +
5522 +    @SuppressWarnings("serial")
5523 +    static final class ReduceValuesTask<K,V>
5524 +        extends BulkTask<K,V,V> {
5525 +        final BiFun<? super V, ? super V, ? extends V> reducer;
5526 +        V result;
5527 +        ReduceValuesTask<K,V> sibling;
5528 +        ReduceValuesTask
5529 +            (ConcurrentHashMapV8<K,V> m,
5530 +             BiFun<? super V, ? super V, ? extends V> reducer) {
5531 +            super(m);
5532 +            this.reducer = reducer;
5533 +        }
5534 +        ReduceValuesTask
5535 +            (BulkTask<K,V,?> p, int b, boolean split,
5536 +             BiFun<? super V, ? super V, ? extends V> reducer) {
5537 +            super(p, b, split);
5538 +            this.reducer = reducer;
5539 +        }
5540 +
5541 +        @SuppressWarnings("unchecked") public final void compute() {
5542 +            ReduceValuesTask<K,V> t = this;
5543 +            final BiFun<? super V, ? super V, ? extends V> reducer =
5544 +                this.reducer;
5545 +            if (reducer == null)
5546 +                throw new Error(NullFunctionMessage);
5547 +            int b = batch();
5548 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5549 +                b >>>= 1;
5550 +                t.pending = 1;
5551 +                ReduceValuesTask<K,V> rt =
5552 +                    new ReduceValuesTask<K,V>
5553 +                    (t, b, true, reducer);
5554 +                t = new ReduceValuesTask<K,V>
5555 +                    (t, b, false, reducer);
5556 +                t.sibling = rt;
5557 +                rt.sibling = t;
5558 +                rt.fork();
5559 +            }
5560 +            V r = null;
5561 +            Object v;
5562 +            while ((v = t.advance()) != null) {
5563 +                V u = (V)v;
5564 +                r = (r == null) ? u : reducer.apply(r, u);
5565 +            }
5566 +            t.result = r;
5567 +            for (;;) {
5568 +                int c; BulkTask<K,V,?> par; ReduceValuesTask<K,V> s, p; V u;
5569 +                if ((par = t.parent) == null ||
5570 +                    !(par instanceof ReduceValuesTask)) {
5571 +                    t.quietlyComplete();
5572 +                    break;
5573 +                }
5574 +                else if ((c = (p = (ReduceValuesTask<K,V>)par).pending) == 0) {
5575 +                    if ((s = t.sibling) != null && (u = s.result) != null)
5576 +                        r = (r == null) ? u : reducer.apply(r, u);
5577 +                    (t = p).result = r;
5578 +                }
5579 +                else if (p.casPending(c, 0))
5580 +                    break;
5581 +            }
5582 +        }
5583 +        public final V getRawResult() { return result; }
5584 +    }
5585 +
5586 +    @SuppressWarnings("serial")
5587 +    static final class ReduceEntriesTask<K,V>
5588 +        extends BulkTask<K,V,Map.Entry<K,V>> {
5589 +        final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5590 +        Map.Entry<K,V> result;
5591 +        ReduceEntriesTask<K,V> sibling;
5592 +        ReduceEntriesTask
5593 +            (ConcurrentHashMapV8<K,V> m,
5594 +             BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5595 +            super(m);
5596 +            this.reducer = reducer;
5597 +        }
5598 +        ReduceEntriesTask
5599 +            (BulkTask<K,V,?> p, int b, boolean split,
5600 +             BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5601 +            super(p, b, split);
5602 +            this.reducer = reducer;
5603 +        }
5604 +
5605 +        @SuppressWarnings("unchecked") public final void compute() {
5606 +            ReduceEntriesTask<K,V> t = this;
5607 +            final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer =
5608 +                this.reducer;
5609 +            if (reducer == null)
5610 +                throw new Error(NullFunctionMessage);
5611 +            int b = batch();
5612 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5613 +                b >>>= 1;
5614 +                t.pending = 1;
5615 +                ReduceEntriesTask<K,V> rt =
5616 +                    new ReduceEntriesTask<K,V>
5617 +                    (t, b, true, reducer);
5618 +                t = new ReduceEntriesTask<K,V>
5619 +                    (t, b, false, reducer);
5620 +                t.sibling = rt;
5621 +                rt.sibling = t;
5622 +                rt.fork();
5623 +            }
5624 +            Map.Entry<K,V> r = null;
5625 +            Object v;
5626 +            while ((v = t.advance()) != null) {
5627 +                Map.Entry<K,V> u = entryFor((K)t.nextKey, (V)v);
5628 +                r = (r == null) ? u : reducer.apply(r, u);
5629 +            }
5630 +            t.result = r;
5631 +            for (;;) {
5632 +                int c; BulkTask<K,V,?> par; ReduceEntriesTask<K,V> s, p;
5633 +                Map.Entry<K,V> u;
5634 +                if ((par = t.parent) == null ||
5635 +                    !(par instanceof ReduceEntriesTask)) {
5636 +                    t.quietlyComplete();
5637 +                    break;
5638 +                }
5639 +                else if ((c = (p = (ReduceEntriesTask<K,V>)par).pending) == 0) {
5640 +                    if ((s = t.sibling) != null && (u = s.result) != null)
5641 +                        r = (r == null) ? u : reducer.apply(r, u);
5642 +                    (t = p).result = r;
5643 +                }
5644 +                else if (p.casPending(c, 0))
5645 +                    break;
5646 +            }
5647 +        }
5648 +        public final Map.Entry<K,V> getRawResult() { return result; }
5649 +    }
5650 +
5651 +    @SuppressWarnings("serial")
5652 +    static final class MapReduceKeysTask<K,V,U>
5653 +        extends BulkTask<K,V,U> {
5654 +        final Fun<? super K, ? extends U> transformer;
5655 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5656 +        U result;
5657 +        MapReduceKeysTask<K,V,U> sibling;
5658 +        MapReduceKeysTask
5659 +            (ConcurrentHashMapV8<K,V> m,
5660 +             Fun<? super K, ? extends U> transformer,
5661 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5662 +            super(m);
5663 +            this.transformer = transformer;
5664 +            this.reducer = reducer;
5665 +        }
5666 +        MapReduceKeysTask
5667 +            (BulkTask<K,V,?> p, int b, boolean split,
5668 +             Fun<? super K, ? extends U> transformer,
5669 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5670 +            super(p, b, split);
5671 +            this.transformer = transformer;
5672 +            this.reducer = reducer;
5673 +        }
5674 +        @SuppressWarnings("unchecked") public final void compute() {
5675 +            MapReduceKeysTask<K,V,U> t = this;
5676 +            final Fun<? super K, ? extends U> transformer =
5677 +                this.transformer;
5678 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5679 +                this.reducer;
5680 +            if (transformer == null || reducer == null)
5681 +                throw new Error(NullFunctionMessage);
5682 +            int b = batch();
5683 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5684 +                b >>>= 1;
5685 +                t.pending = 1;
5686 +                MapReduceKeysTask<K,V,U> rt =
5687 +                    new MapReduceKeysTask<K,V,U>
5688 +                    (t, b, true, transformer, reducer);
5689 +                t = new MapReduceKeysTask<K,V,U>
5690 +                    (t, b, false, transformer, reducer);
5691 +                t.sibling = rt;
5692 +                rt.sibling = t;
5693 +                rt.fork();
5694 +            }
5695 +            U r = null, u;
5696 +            while (t.advance() != null) {
5697 +                if ((u = transformer.apply((K)t.nextKey)) != null)
5698 +                    r = (r == null) ? u : reducer.apply(r, u);
5699 +            }
5700 +            t.result = r;
5701 +            for (;;) {
5702 +                int c; BulkTask<K,V,?> par; MapReduceKeysTask<K,V,U> s, p;
5703 +                if ((par = t.parent) == null ||
5704 +                    !(par instanceof MapReduceKeysTask)) {
5705 +                    t.quietlyComplete();
5706 +                    break;
5707 +                }
5708 +                else if ((c = (p = (MapReduceKeysTask<K,V,U>)par).pending) == 0) {
5709 +                    if ((s = t.sibling) != null && (u = s.result) != null)
5710 +                        r = (r == null) ? u : reducer.apply(r, u);
5711 +                    (t = p).result = r;
5712 +                }
5713 +                else if (p.casPending(c, 0))
5714 +                    break;
5715 +            }
5716 +        }
5717 +        public final U getRawResult() { return result; }
5718 +    }
5719 +
5720 +    @SuppressWarnings("serial")
5721 +    static final class MapReduceValuesTask<K,V,U>
5722 +        extends BulkTask<K,V,U> {
5723 +        final Fun<? super V, ? extends U> transformer;
5724 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5725 +        U result;
5726 +        MapReduceValuesTask<K,V,U> sibling;
5727 +        MapReduceValuesTask
5728 +            (ConcurrentHashMapV8<K,V> m,
5729 +             Fun<? super V, ? extends U> transformer,
5730 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5731 +            super(m);
5732 +            this.transformer = transformer;
5733 +            this.reducer = reducer;
5734 +        }
5735 +        MapReduceValuesTask
5736 +            (BulkTask<K,V,?> p, int b, boolean split,
5737 +             Fun<? super V, ? extends U> transformer,
5738 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5739 +            super(p, b, split);
5740 +            this.transformer = transformer;
5741 +            this.reducer = reducer;
5742 +        }
5743 +        @SuppressWarnings("unchecked") public final void compute() {
5744 +            MapReduceValuesTask<K,V,U> t = this;
5745 +            final Fun<? super V, ? extends U> transformer =
5746 +                this.transformer;
5747 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5748 +                this.reducer;
5749 +            if (transformer == null || reducer == null)
5750 +                throw new Error(NullFunctionMessage);
5751 +            int b = batch();
5752 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5753 +                b >>>= 1;
5754 +                t.pending = 1;
5755 +                MapReduceValuesTask<K,V,U> rt =
5756 +                    new MapReduceValuesTask<K,V,U>
5757 +                    (t, b, true, transformer, reducer);
5758 +                t = new MapReduceValuesTask<K,V,U>
5759 +                    (t, b, false, transformer, reducer);
5760 +                t.sibling = rt;
5761 +                rt.sibling = t;
5762 +                rt.fork();
5763 +            }
5764 +            U r = null, u;
5765 +            Object v;
5766 +            while ((v = t.advance()) != null) {
5767 +                if ((u = transformer.apply((V)v)) != null)
5768 +                    r = (r == null) ? u : reducer.apply(r, u);
5769 +            }
5770 +            t.result = r;
5771 +            for (;;) {
5772 +                int c; BulkTask<K,V,?> par; MapReduceValuesTask<K,V,U> s, p;
5773 +                if ((par = t.parent) == null ||
5774 +                    !(par instanceof MapReduceValuesTask)) {
5775 +                    t.quietlyComplete();
5776 +                    break;
5777 +                }
5778 +                else if ((c = (p = (MapReduceValuesTask<K,V,U>)par).pending) == 0) {
5779 +                    if ((s = t.sibling) != null && (u = s.result) != null)
5780 +                        r = (r == null) ? u : reducer.apply(r, u);
5781 +                    (t = p).result = r;
5782 +                }
5783 +                else if (p.casPending(c, 0))
5784 +                    break;
5785 +            }
5786 +        }
5787 +        public final U getRawResult() { return result; }
5788 +    }
5789 +
5790 +    @SuppressWarnings("serial")
5791 +    static final class MapReduceEntriesTask<K,V,U>
5792 +        extends BulkTask<K,V,U> {
5793 +        final Fun<Map.Entry<K,V>, ? extends U> transformer;
5794 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5795 +        U result;
5796 +        MapReduceEntriesTask<K,V,U> sibling;
5797 +        MapReduceEntriesTask
5798 +            (ConcurrentHashMapV8<K,V> m,
5799 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
5800 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5801 +            super(m);
5802 +            this.transformer = transformer;
5803 +            this.reducer = reducer;
5804 +        }
5805 +        MapReduceEntriesTask
5806 +            (BulkTask<K,V,?> p, int b, boolean split,
5807 +             Fun<Map.Entry<K,V>, ? extends U> transformer,
5808 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5809 +            super(p, b, split);
5810 +            this.transformer = transformer;
5811 +            this.reducer = reducer;
5812 +        }
5813 +        @SuppressWarnings("unchecked") public final void compute() {
5814 +            MapReduceEntriesTask<K,V,U> t = this;
5815 +            final Fun<Map.Entry<K,V>, ? extends U> transformer =
5816 +                this.transformer;
5817 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5818 +                this.reducer;
5819 +            if (transformer == null || reducer == null)
5820 +                throw new Error(NullFunctionMessage);
5821 +            int b = batch();
5822 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5823 +                b >>>= 1;
5824 +                t.pending = 1;
5825 +                MapReduceEntriesTask<K,V,U> rt =
5826 +                    new MapReduceEntriesTask<K,V,U>
5827 +                    (t, b, true, transformer, reducer);
5828 +                t = new MapReduceEntriesTask<K,V,U>
5829 +                    (t, b, false, transformer, reducer);
5830 +                t.sibling = rt;
5831 +                rt.sibling = t;
5832 +                rt.fork();
5833 +            }
5834 +            U r = null, u;
5835 +            Object v;
5836 +            while ((v = t.advance()) != null) {
5837 +                if ((u = transformer.apply(entryFor((K)t.nextKey, (V)v))) != null)
5838 +                    r = (r == null) ? u : reducer.apply(r, u);
5839 +            }
5840 +            t.result = r;
5841 +            for (;;) {
5842 +                int c; BulkTask<K,V,?> par; MapReduceEntriesTask<K,V,U> s, p;
5843 +                if ((par = t.parent) == null ||
5844 +                    !(par instanceof MapReduceEntriesTask)) {
5845 +                    t.quietlyComplete();
5846 +                    break;
5847 +                }
5848 +                else if ((c = (p = (MapReduceEntriesTask<K,V,U>)par).pending) == 0) {
5849 +                    if ((s = t.sibling) != null && (u = s.result) != null)
5850 +                        r = (r == null) ? u : reducer.apply(r, u);
5851 +                    (t = p).result = r;
5852 +                }
5853 +                else if (p.casPending(c, 0))
5854 +                    break;
5855 +            }
5856 +        }
5857 +        public final U getRawResult() { return result; }
5858 +    }
5859 +
5860 +    @SuppressWarnings("serial")
5861 +    static final class MapReduceMappingsTask<K,V,U>
5862 +        extends BulkTask<K,V,U> {
5863 +        final BiFun<? super K, ? super V, ? extends U> transformer;
5864 +        final BiFun<? super U, ? super U, ? extends U> reducer;
5865 +        U result;
5866 +        MapReduceMappingsTask<K,V,U> sibling;
5867 +        MapReduceMappingsTask
5868 +            (ConcurrentHashMapV8<K,V> m,
5869 +             BiFun<? super K, ? super V, ? extends U> transformer,
5870 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5871 +            super(m);
5872 +            this.transformer = transformer;
5873 +            this.reducer = reducer;
5874 +        }
5875 +        MapReduceMappingsTask
5876 +            (BulkTask<K,V,?> p, int b, boolean split,
5877 +             BiFun<? super K, ? super V, ? extends U> transformer,
5878 +             BiFun<? super U, ? super U, ? extends U> reducer) {
5879 +            super(p, b, split);
5880 +            this.transformer = transformer;
5881 +            this.reducer = reducer;
5882 +        }
5883 +        @SuppressWarnings("unchecked") public final void compute() {
5884 +            MapReduceMappingsTask<K,V,U> t = this;
5885 +            final BiFun<? super K, ? super V, ? extends U> transformer =
5886 +                this.transformer;
5887 +            final BiFun<? super U, ? super U, ? extends U> reducer =
5888 +                this.reducer;
5889 +            if (transformer == null || reducer == null)
5890 +                throw new Error(NullFunctionMessage);
5891 +            int b = batch();
5892 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5893 +                b >>>= 1;
5894 +                t.pending = 1;
5895 +                MapReduceMappingsTask<K,V,U> rt =
5896 +                    new MapReduceMappingsTask<K,V,U>
5897 +                    (t, b, true, transformer, reducer);
5898 +                t = new MapReduceMappingsTask<K,V,U>
5899 +                    (t, b, false, transformer, reducer);
5900 +                t.sibling = rt;
5901 +                rt.sibling = t;
5902 +                rt.fork();
5903 +            }
5904 +            U r = null, u;
5905 +            Object v;
5906 +            while ((v = t.advance()) != null) {
5907 +                if ((u = transformer.apply((K)t.nextKey, (V)v)) != null)
5908 +                    r = (r == null) ? u : reducer.apply(r, u);
5909 +            }
5910 +            for (;;) {
5911 +                int c; BulkTask<K,V,?> par; MapReduceMappingsTask<K,V,U> s, p;
5912 +                if ((par = t.parent) == null ||
5913 +                    !(par instanceof MapReduceMappingsTask)) {
5914 +                    t.quietlyComplete();
5915 +                    break;
5916 +                }
5917 +                else if ((c = (p = (MapReduceMappingsTask<K,V,U>)par).pending) == 0) {
5918 +                    if ((s = t.sibling) != null && (u = s.result) != null)
5919 +                        r = (r == null) ? u : reducer.apply(r, u);
5920 +                    (t = p).result = r;
5921 +                }
5922 +                else if (p.casPending(c, 0))
5923 +                    break;
5924 +            }
5925 +        }
5926 +        public final U getRawResult() { return result; }
5927 +    }
5928 +
5929 +    @SuppressWarnings("serial")
5930 +    static final class MapReduceKeysToDoubleTask<K,V>
5931 +        extends BulkTask<K,V,Double> {
5932 +        final ObjectToDouble<? super K> transformer;
5933 +        final DoubleByDoubleToDouble reducer;
5934 +        final double basis;
5935 +        double result;
5936 +        MapReduceKeysToDoubleTask<K,V> sibling;
5937 +        MapReduceKeysToDoubleTask
5938 +            (ConcurrentHashMapV8<K,V> m,
5939 +             ObjectToDouble<? super K> transformer,
5940 +             double basis,
5941 +             DoubleByDoubleToDouble reducer) {
5942 +            super(m);
5943 +            this.transformer = transformer;
5944 +            this.basis = basis; this.reducer = reducer;
5945 +        }
5946 +        MapReduceKeysToDoubleTask
5947 +            (BulkTask<K,V,?> p, int b, boolean split,
5948 +             ObjectToDouble<? super K> transformer,
5949 +             double basis,
5950 +             DoubleByDoubleToDouble reducer) {
5951 +            super(p, b, split);
5952 +            this.transformer = transformer;
5953 +            this.basis = basis; this.reducer = reducer;
5954 +        }
5955 +        @SuppressWarnings("unchecked") public final void compute() {
5956 +            MapReduceKeysToDoubleTask<K,V> t = this;
5957 +            final ObjectToDouble<? super K> transformer =
5958 +                this.transformer;
5959 +            final DoubleByDoubleToDouble reducer = this.reducer;
5960 +            if (transformer == null || reducer == null)
5961 +                throw new Error(NullFunctionMessage);
5962 +            final double id = this.basis;
5963 +            int b = batch();
5964 +            while (b > 1 && t.baseIndex != t.baseLimit) {
5965 +                b >>>= 1;
5966 +                t.pending = 1;
5967 +                MapReduceKeysToDoubleTask<K,V> rt =
5968 +                    new MapReduceKeysToDoubleTask<K,V>
5969 +                    (t, b, true, transformer, id, reducer);
5970 +                t = new MapReduceKeysToDoubleTask<K,V>
5971 +                    (t, b, false, transformer, id, reducer);
5972 +                t.sibling = rt;
5973 +                rt.sibling = t;
5974 +                rt.fork();
5975 +            }
5976 +            double r = id;
5977 +            while (t.advance() != null)
5978 +                r = reducer.apply(r, transformer.apply((K)t.nextKey));
5979 +            t.result = r;
5980 +            for (;;) {
5981 +                int c; BulkTask<K,V,?> par; MapReduceKeysToDoubleTask<K,V> s, p;
5982 +                if ((par = t.parent) == null ||
5983 +                    !(par instanceof MapReduceKeysToDoubleTask)) {
5984 +                    t.quietlyComplete();
5985 +                    break;
5986 +                }
5987 +                else if ((c = (p = (MapReduceKeysToDoubleTask<K,V>)par).pending) == 0) {
5988 +                    if ((s = t.sibling) != null)
5989 +                        r = reducer.apply(r, s.result);
5990 +                    (t = p).result = r;
5991 +                }
5992 +                else if (p.casPending(c, 0))
5993 +                    break;
5994 +            }
5995 +        }
5996 +        public final Double getRawResult() { return result; }
5997      }
5998  
5999 +    @SuppressWarnings("serial")
6000 +    static final class MapReduceValuesToDoubleTask<K,V>
6001 +        extends BulkTask<K,V,Double> {
6002 +        final ObjectToDouble<? super V> transformer;
6003 +        final DoubleByDoubleToDouble reducer;
6004 +        final double basis;
6005 +        double result;
6006 +        MapReduceValuesToDoubleTask<K,V> sibling;
6007 +        MapReduceValuesToDoubleTask
6008 +            (ConcurrentHashMapV8<K,V> m,
6009 +             ObjectToDouble<? super V> transformer,
6010 +             double basis,
6011 +             DoubleByDoubleToDouble reducer) {
6012 +            super(m);
6013 +            this.transformer = transformer;
6014 +            this.basis = basis; this.reducer = reducer;
6015 +        }
6016 +        MapReduceValuesToDoubleTask
6017 +            (BulkTask<K,V,?> p, int b, boolean split,
6018 +             ObjectToDouble<? super V> transformer,
6019 +             double basis,
6020 +             DoubleByDoubleToDouble reducer) {
6021 +            super(p, b, split);
6022 +            this.transformer = transformer;
6023 +            this.basis = basis; this.reducer = reducer;
6024 +        }
6025 +        @SuppressWarnings("unchecked") public final void compute() {
6026 +            MapReduceValuesToDoubleTask<K,V> t = this;
6027 +            final ObjectToDouble<? super V> transformer =
6028 +                this.transformer;
6029 +            final DoubleByDoubleToDouble reducer = this.reducer;
6030 +            if (transformer == null || reducer == null)
6031 +                throw new Error(NullFunctionMessage);
6032 +            final double id = this.basis;
6033 +            int b = batch();
6034 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6035 +                b >>>= 1;
6036 +                t.pending = 1;
6037 +                MapReduceValuesToDoubleTask<K,V> rt =
6038 +                    new MapReduceValuesToDoubleTask<K,V>
6039 +                    (t, b, true, transformer, id, reducer);
6040 +                t = new MapReduceValuesToDoubleTask<K,V>
6041 +                    (t, b, false, transformer, id, reducer);
6042 +                t.sibling = rt;
6043 +                rt.sibling = t;
6044 +                rt.fork();
6045 +            }
6046 +            double r = id;
6047 +            Object v;
6048 +            while ((v = t.advance()) != null)
6049 +                r = reducer.apply(r, transformer.apply((V)v));
6050 +            t.result = r;
6051 +            for (;;) {
6052 +                int c; BulkTask<K,V,?> par; MapReduceValuesToDoubleTask<K,V> s, p;
6053 +                if ((par = t.parent) == null ||
6054 +                    !(par instanceof MapReduceValuesToDoubleTask)) {
6055 +                    t.quietlyComplete();
6056 +                    break;
6057 +                }
6058 +                else if ((c = (p = (MapReduceValuesToDoubleTask<K,V>)par).pending) == 0) {
6059 +                    if ((s = t.sibling) != null)
6060 +                        r = reducer.apply(r, s.result);
6061 +                    (t = p).result = r;
6062 +                }
6063 +                else if (p.casPending(c, 0))
6064 +                    break;
6065 +            }
6066 +        }
6067 +        public final Double getRawResult() { return result; }
6068 +    }
6069 +
6070 +    @SuppressWarnings("serial")
6071 +    static final class MapReduceEntriesToDoubleTask<K,V>
6072 +        extends BulkTask<K,V,Double> {
6073 +        final ObjectToDouble<Map.Entry<K,V>> transformer;
6074 +        final DoubleByDoubleToDouble reducer;
6075 +        final double basis;
6076 +        double result;
6077 +        MapReduceEntriesToDoubleTask<K,V> sibling;
6078 +        MapReduceEntriesToDoubleTask
6079 +            (ConcurrentHashMapV8<K,V> m,
6080 +             ObjectToDouble<Map.Entry<K,V>> transformer,
6081 +             double basis,
6082 +             DoubleByDoubleToDouble reducer) {
6083 +            super(m);
6084 +            this.transformer = transformer;
6085 +            this.basis = basis; this.reducer = reducer;
6086 +        }
6087 +        MapReduceEntriesToDoubleTask
6088 +            (BulkTask<K,V,?> p, int b, boolean split,
6089 +             ObjectToDouble<Map.Entry<K,V>> transformer,
6090 +             double basis,
6091 +             DoubleByDoubleToDouble reducer) {
6092 +            super(p, b, split);
6093 +            this.transformer = transformer;
6094 +            this.basis = basis; this.reducer = reducer;
6095 +        }
6096 +        @SuppressWarnings("unchecked") public final void compute() {
6097 +            MapReduceEntriesToDoubleTask<K,V> t = this;
6098 +            final ObjectToDouble<Map.Entry<K,V>> transformer =
6099 +                this.transformer;
6100 +            final DoubleByDoubleToDouble reducer = this.reducer;
6101 +            if (transformer == null || reducer == null)
6102 +                throw new Error(NullFunctionMessage);
6103 +            final double id = this.basis;
6104 +            int b = batch();
6105 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6106 +                b >>>= 1;
6107 +                t.pending = 1;
6108 +                MapReduceEntriesToDoubleTask<K,V> rt =
6109 +                    new MapReduceEntriesToDoubleTask<K,V>
6110 +                    (t, b, true, transformer, id, reducer);
6111 +                t = new MapReduceEntriesToDoubleTask<K,V>
6112 +                    (t, b, false, transformer, id, reducer);
6113 +                t.sibling = rt;
6114 +                rt.sibling = t;
6115 +                rt.fork();
6116 +            }
6117 +            double r = id;
6118 +            Object v;
6119 +            while ((v = t.advance()) != null)
6120 +                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6121 +            t.result = r;
6122 +            for (;;) {
6123 +                int c; BulkTask<K,V,?> par; MapReduceEntriesToDoubleTask<K,V> s, p;
6124 +                if ((par = t.parent) == null ||
6125 +                    !(par instanceof MapReduceEntriesToDoubleTask)) {
6126 +                    t.quietlyComplete();
6127 +                    break;
6128 +                }
6129 +                else if ((c = (p = (MapReduceEntriesToDoubleTask<K,V>)par).pending) == 0) {
6130 +                    if ((s = t.sibling) != null)
6131 +                        r = reducer.apply(r, s.result);
6132 +                    (t = p).result = r;
6133 +                }
6134 +                else if (p.casPending(c, 0))
6135 +                    break;
6136 +            }
6137 +        }
6138 +        public final Double getRawResult() { return result; }
6139 +    }
6140 +
6141 +    @SuppressWarnings("serial")
6142 +    static final class MapReduceMappingsToDoubleTask<K,V>
6143 +        extends BulkTask<K,V,Double> {
6144 +        final ObjectByObjectToDouble<? super K, ? super V> transformer;
6145 +        final DoubleByDoubleToDouble reducer;
6146 +        final double basis;
6147 +        double result;
6148 +        MapReduceMappingsToDoubleTask<K,V> sibling;
6149 +        MapReduceMappingsToDoubleTask
6150 +            (ConcurrentHashMapV8<K,V> m,
6151 +             ObjectByObjectToDouble<? super K, ? super V> transformer,
6152 +             double basis,
6153 +             DoubleByDoubleToDouble reducer) {
6154 +            super(m);
6155 +            this.transformer = transformer;
6156 +            this.basis = basis; this.reducer = reducer;
6157 +        }
6158 +        MapReduceMappingsToDoubleTask
6159 +            (BulkTask<K,V,?> p, int b, boolean split,
6160 +             ObjectByObjectToDouble<? super K, ? super V> transformer,
6161 +             double basis,
6162 +             DoubleByDoubleToDouble reducer) {
6163 +            super(p, b, split);
6164 +            this.transformer = transformer;
6165 +            this.basis = basis; this.reducer = reducer;
6166 +        }
6167 +        @SuppressWarnings("unchecked") public final void compute() {
6168 +            MapReduceMappingsToDoubleTask<K,V> t = this;
6169 +            final ObjectByObjectToDouble<? super K, ? super V> transformer =
6170 +                this.transformer;
6171 +            final DoubleByDoubleToDouble reducer = this.reducer;
6172 +            if (transformer == null || reducer == null)
6173 +                throw new Error(NullFunctionMessage);
6174 +            final double id = this.basis;
6175 +            int b = batch();
6176 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6177 +                b >>>= 1;
6178 +                t.pending = 1;
6179 +                MapReduceMappingsToDoubleTask<K,V> rt =
6180 +                    new MapReduceMappingsToDoubleTask<K,V>
6181 +                    (t, b, true, transformer, id, reducer);
6182 +                t = new MapReduceMappingsToDoubleTask<K,V>
6183 +                    (t, b, false, transformer, id, reducer);
6184 +                t.sibling = rt;
6185 +                rt.sibling = t;
6186 +                rt.fork();
6187 +            }
6188 +            double r = id;
6189 +            Object v;
6190 +            while ((v = t.advance()) != null)
6191 +                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6192 +            t.result = r;
6193 +            for (;;) {
6194 +                int c; BulkTask<K,V,?> par; MapReduceMappingsToDoubleTask<K,V> s, p;
6195 +                if ((par = t.parent) == null ||
6196 +                    !(par instanceof MapReduceMappingsToDoubleTask)) {
6197 +                    t.quietlyComplete();
6198 +                    break;
6199 +                }
6200 +                else if ((c = (p = (MapReduceMappingsToDoubleTask<K,V>)par).pending) == 0) {
6201 +                    if ((s = t.sibling) != null)
6202 +                        r = reducer.apply(r, s.result);
6203 +                    (t = p).result = r;
6204 +                }
6205 +                else if (p.casPending(c, 0))
6206 +                    break;
6207 +            }
6208 +        }
6209 +        public final Double getRawResult() { return result; }
6210 +    }
6211 +
6212 +    @SuppressWarnings("serial")
6213 +    static final class MapReduceKeysToLongTask<K,V>
6214 +        extends BulkTask<K,V,Long> {
6215 +        final ObjectToLong<? super K> transformer;
6216 +        final LongByLongToLong reducer;
6217 +        final long basis;
6218 +        long result;
6219 +        MapReduceKeysToLongTask<K,V> sibling;
6220 +        MapReduceKeysToLongTask
6221 +            (ConcurrentHashMapV8<K,V> m,
6222 +             ObjectToLong<? super K> transformer,
6223 +             long basis,
6224 +             LongByLongToLong reducer) {
6225 +            super(m);
6226 +            this.transformer = transformer;
6227 +            this.basis = basis; this.reducer = reducer;
6228 +        }
6229 +        MapReduceKeysToLongTask
6230 +            (BulkTask<K,V,?> p, int b, boolean split,
6231 +             ObjectToLong<? super K> transformer,
6232 +             long basis,
6233 +             LongByLongToLong reducer) {
6234 +            super(p, b, split);
6235 +            this.transformer = transformer;
6236 +            this.basis = basis; this.reducer = reducer;
6237 +        }
6238 +        @SuppressWarnings("unchecked") public final void compute() {
6239 +            MapReduceKeysToLongTask<K,V> t = this;
6240 +            final ObjectToLong<? super K> transformer =
6241 +                this.transformer;
6242 +            final LongByLongToLong reducer = this.reducer;
6243 +            if (transformer == null || reducer == null)
6244 +                throw new Error(NullFunctionMessage);
6245 +            final long id = this.basis;
6246 +            int b = batch();
6247 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6248 +                b >>>= 1;
6249 +                t.pending = 1;
6250 +                MapReduceKeysToLongTask<K,V> rt =
6251 +                    new MapReduceKeysToLongTask<K,V>
6252 +                    (t, b, true, transformer, id, reducer);
6253 +                t = new MapReduceKeysToLongTask<K,V>
6254 +                    (t, b, false, transformer, id, reducer);
6255 +                t.sibling = rt;
6256 +                rt.sibling = t;
6257 +                rt.fork();
6258 +            }
6259 +            long r = id;
6260 +            while (t.advance() != null)
6261 +                r = reducer.apply(r, transformer.apply((K)t.nextKey));
6262 +            t.result = r;
6263 +            for (;;) {
6264 +                int c; BulkTask<K,V,?> par; MapReduceKeysToLongTask<K,V> s, p;
6265 +                if ((par = t.parent) == null ||
6266 +                    !(par instanceof MapReduceKeysToLongTask)) {
6267 +                    t.quietlyComplete();
6268 +                    break;
6269 +                }
6270 +                else if ((c = (p = (MapReduceKeysToLongTask<K,V>)par).pending) == 0) {
6271 +                    if ((s = t.sibling) != null)
6272 +                        r = reducer.apply(r, s.result);
6273 +                    (t = p).result = r;
6274 +                }
6275 +                else if (p.casPending(c, 0))
6276 +                    break;
6277 +            }
6278 +        }
6279 +        public final Long getRawResult() { return result; }
6280 +    }
6281 +
6282 +    @SuppressWarnings("serial")
6283 +    static final class MapReduceValuesToLongTask<K,V>
6284 +        extends BulkTask<K,V,Long> {
6285 +        final ObjectToLong<? super V> transformer;
6286 +        final LongByLongToLong reducer;
6287 +        final long basis;
6288 +        long result;
6289 +        MapReduceValuesToLongTask<K,V> sibling;
6290 +        MapReduceValuesToLongTask
6291 +            (ConcurrentHashMapV8<K,V> m,
6292 +             ObjectToLong<? super V> transformer,
6293 +             long basis,
6294 +             LongByLongToLong reducer) {
6295 +            super(m);
6296 +            this.transformer = transformer;
6297 +            this.basis = basis; this.reducer = reducer;
6298 +        }
6299 +        MapReduceValuesToLongTask
6300 +            (BulkTask<K,V,?> p, int b, boolean split,
6301 +             ObjectToLong<? super V> transformer,
6302 +             long basis,
6303 +             LongByLongToLong reducer) {
6304 +            super(p, b, split);
6305 +            this.transformer = transformer;
6306 +            this.basis = basis; this.reducer = reducer;
6307 +        }
6308 +        @SuppressWarnings("unchecked") public final void compute() {
6309 +            MapReduceValuesToLongTask<K,V> t = this;
6310 +            final ObjectToLong<? super V> transformer =
6311 +                this.transformer;
6312 +            final LongByLongToLong reducer = this.reducer;
6313 +            if (transformer == null || reducer == null)
6314 +                throw new Error(NullFunctionMessage);
6315 +            final long id = this.basis;
6316 +            int b = batch();
6317 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6318 +                b >>>= 1;
6319 +                t.pending = 1;
6320 +                MapReduceValuesToLongTask<K,V> rt =
6321 +                    new MapReduceValuesToLongTask<K,V>
6322 +                    (t, b, true, transformer, id, reducer);
6323 +                t = new MapReduceValuesToLongTask<K,V>
6324 +                    (t, b, false, transformer, id, reducer);
6325 +                t.sibling = rt;
6326 +                rt.sibling = t;
6327 +                rt.fork();
6328 +            }
6329 +            long r = id;
6330 +            Object v;
6331 +            while ((v = t.advance()) != null)
6332 +                r = reducer.apply(r, transformer.apply((V)v));
6333 +            t.result = r;
6334 +            for (;;) {
6335 +                int c; BulkTask<K,V,?> par; MapReduceValuesToLongTask<K,V> s, p;
6336 +                if ((par = t.parent) == null ||
6337 +                    !(par instanceof MapReduceValuesToLongTask)) {
6338 +                    t.quietlyComplete();
6339 +                    break;
6340 +                }
6341 +                else if ((c = (p = (MapReduceValuesToLongTask<K,V>)par).pending) == 0) {
6342 +                    if ((s = t.sibling) != null)
6343 +                        r = reducer.apply(r, s.result);
6344 +                    (t = p).result = r;
6345 +                }
6346 +                else if (p.casPending(c, 0))
6347 +                    break;
6348 +            }
6349 +        }
6350 +        public final Long getRawResult() { return result; }
6351 +    }
6352 +
6353 +    @SuppressWarnings("serial")
6354 +    static final class MapReduceEntriesToLongTask<K,V>
6355 +        extends BulkTask<K,V,Long> {
6356 +        final ObjectToLong<Map.Entry<K,V>> transformer;
6357 +        final LongByLongToLong reducer;
6358 +        final long basis;
6359 +        long result;
6360 +        MapReduceEntriesToLongTask<K,V> sibling;
6361 +        MapReduceEntriesToLongTask
6362 +            (ConcurrentHashMapV8<K,V> m,
6363 +             ObjectToLong<Map.Entry<K,V>> transformer,
6364 +             long basis,
6365 +             LongByLongToLong reducer) {
6366 +            super(m);
6367 +            this.transformer = transformer;
6368 +            this.basis = basis; this.reducer = reducer;
6369 +        }
6370 +        MapReduceEntriesToLongTask
6371 +            (BulkTask<K,V,?> p, int b, boolean split,
6372 +             ObjectToLong<Map.Entry<K,V>> transformer,
6373 +             long basis,
6374 +             LongByLongToLong reducer) {
6375 +            super(p, b, split);
6376 +            this.transformer = transformer;
6377 +            this.basis = basis; this.reducer = reducer;
6378 +        }
6379 +        @SuppressWarnings("unchecked") public final void compute() {
6380 +            MapReduceEntriesToLongTask<K,V> t = this;
6381 +            final ObjectToLong<Map.Entry<K,V>> transformer =
6382 +                this.transformer;
6383 +            final LongByLongToLong reducer = this.reducer;
6384 +            if (transformer == null || reducer == null)
6385 +                throw new Error(NullFunctionMessage);
6386 +            final long id = this.basis;
6387 +            int b = batch();
6388 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6389 +                b >>>= 1;
6390 +                t.pending = 1;
6391 +                MapReduceEntriesToLongTask<K,V> rt =
6392 +                    new MapReduceEntriesToLongTask<K,V>
6393 +                    (t, b, true, transformer, id, reducer);
6394 +                t = new MapReduceEntriesToLongTask<K,V>
6395 +                    (t, b, false, transformer, id, reducer);
6396 +                t.sibling = rt;
6397 +                rt.sibling = t;
6398 +                rt.fork();
6399 +            }
6400 +            long r = id;
6401 +            Object v;
6402 +            while ((v = t.advance()) != null)
6403 +                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6404 +            t.result = r;
6405 +            for (;;) {
6406 +                int c; BulkTask<K,V,?> par; MapReduceEntriesToLongTask<K,V> s, p;
6407 +                if ((par = t.parent) == null ||
6408 +                    !(par instanceof MapReduceEntriesToLongTask)) {
6409 +                    t.quietlyComplete();
6410 +                    break;
6411 +                }
6412 +                else if ((c = (p = (MapReduceEntriesToLongTask<K,V>)par).pending) == 0) {
6413 +                    if ((s = t.sibling) != null)
6414 +                        r = reducer.apply(r, s.result);
6415 +                    (t = p).result = r;
6416 +                }
6417 +                else if (p.casPending(c, 0))
6418 +                    break;
6419 +            }
6420 +        }
6421 +        public final Long getRawResult() { return result; }
6422 +    }
6423 +
6424 +    @SuppressWarnings("serial")
6425 +    static final class MapReduceMappingsToLongTask<K,V>
6426 +        extends BulkTask<K,V,Long> {
6427 +        final ObjectByObjectToLong<? super K, ? super V> transformer;
6428 +        final LongByLongToLong reducer;
6429 +        final long basis;
6430 +        long result;
6431 +        MapReduceMappingsToLongTask<K,V> sibling;
6432 +        MapReduceMappingsToLongTask
6433 +            (ConcurrentHashMapV8<K,V> m,
6434 +             ObjectByObjectToLong<? super K, ? super V> transformer,
6435 +             long basis,
6436 +             LongByLongToLong reducer) {
6437 +            super(m);
6438 +            this.transformer = transformer;
6439 +            this.basis = basis; this.reducer = reducer;
6440 +        }
6441 +        MapReduceMappingsToLongTask
6442 +            (BulkTask<K,V,?> p, int b, boolean split,
6443 +             ObjectByObjectToLong<? super K, ? super V> transformer,
6444 +             long basis,
6445 +             LongByLongToLong reducer) {
6446 +            super(p, b, split);
6447 +            this.transformer = transformer;
6448 +            this.basis = basis; this.reducer = reducer;
6449 +        }
6450 +        @SuppressWarnings("unchecked") public final void compute() {
6451 +            MapReduceMappingsToLongTask<K,V> t = this;
6452 +            final ObjectByObjectToLong<? super K, ? super V> transformer =
6453 +                this.transformer;
6454 +            final LongByLongToLong reducer = this.reducer;
6455 +            if (transformer == null || reducer == null)
6456 +                throw new Error(NullFunctionMessage);
6457 +            final long id = this.basis;
6458 +            int b = batch();
6459 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6460 +                b >>>= 1;
6461 +                t.pending = 1;
6462 +                MapReduceMappingsToLongTask<K,V> rt =
6463 +                    new MapReduceMappingsToLongTask<K,V>
6464 +                    (t, b, true, transformer, id, reducer);
6465 +                t = new MapReduceMappingsToLongTask<K,V>
6466 +                    (t, b, false, transformer, id, reducer);
6467 +                t.sibling = rt;
6468 +                rt.sibling = t;
6469 +                rt.fork();
6470 +            }
6471 +            long r = id;
6472 +            Object v;
6473 +            while ((v = t.advance()) != null)
6474 +                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6475 +            t.result = r;
6476 +            for (;;) {
6477 +                int c; BulkTask<K,V,?> par; MapReduceMappingsToLongTask<K,V> s, p;
6478 +                if ((par = t.parent) == null ||
6479 +                    !(par instanceof MapReduceMappingsToLongTask)) {
6480 +                    t.quietlyComplete();
6481 +                    break;
6482 +                }
6483 +                else if ((c = (p = (MapReduceMappingsToLongTask<K,V>)par).pending) == 0) {
6484 +                    if ((s = t.sibling) != null)
6485 +                        r = reducer.apply(r, s.result);
6486 +                    (t = p).result = r;
6487 +                }
6488 +                else if (p.casPending(c, 0))
6489 +                    break;
6490 +            }
6491 +        }
6492 +        public final Long getRawResult() { return result; }
6493 +    }
6494 +
6495 +    @SuppressWarnings("serial")
6496 +    static final class MapReduceKeysToIntTask<K,V>
6497 +        extends BulkTask<K,V,Integer> {
6498 +        final ObjectToInt<? super K> transformer;
6499 +        final IntByIntToInt reducer;
6500 +        final int basis;
6501 +        int result;
6502 +        MapReduceKeysToIntTask<K,V> sibling;
6503 +        MapReduceKeysToIntTask
6504 +            (ConcurrentHashMapV8<K,V> m,
6505 +             ObjectToInt<? super K> transformer,
6506 +             int basis,
6507 +             IntByIntToInt reducer) {
6508 +            super(m);
6509 +            this.transformer = transformer;
6510 +            this.basis = basis; this.reducer = reducer;
6511 +        }
6512 +        MapReduceKeysToIntTask
6513 +            (BulkTask<K,V,?> p, int b, boolean split,
6514 +             ObjectToInt<? super K> transformer,
6515 +             int basis,
6516 +             IntByIntToInt reducer) {
6517 +            super(p, b, split);
6518 +            this.transformer = transformer;
6519 +            this.basis = basis; this.reducer = reducer;
6520 +        }
6521 +        @SuppressWarnings("unchecked") public final void compute() {
6522 +            MapReduceKeysToIntTask<K,V> t = this;
6523 +            final ObjectToInt<? super K> transformer =
6524 +                this.transformer;
6525 +            final IntByIntToInt reducer = this.reducer;
6526 +            if (transformer == null || reducer == null)
6527 +                throw new Error(NullFunctionMessage);
6528 +            final int id = this.basis;
6529 +            int b = batch();
6530 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6531 +                b >>>= 1;
6532 +                t.pending = 1;
6533 +                MapReduceKeysToIntTask<K,V> rt =
6534 +                    new MapReduceKeysToIntTask<K,V>
6535 +                    (t, b, true, transformer, id, reducer);
6536 +                t = new MapReduceKeysToIntTask<K,V>
6537 +                    (t, b, false, transformer, id, reducer);
6538 +                t.sibling = rt;
6539 +                rt.sibling = t;
6540 +                rt.fork();
6541 +            }
6542 +            int r = id;
6543 +            while (t.advance() != null)
6544 +                r = reducer.apply(r, transformer.apply((K)t.nextKey));
6545 +            t.result = r;
6546 +            for (;;) {
6547 +                int c; BulkTask<K,V,?> par; MapReduceKeysToIntTask<K,V> s, p;
6548 +                if ((par = t.parent) == null ||
6549 +                    !(par instanceof MapReduceKeysToIntTask)) {
6550 +                    t.quietlyComplete();
6551 +                    break;
6552 +                }
6553 +                else if ((c = (p = (MapReduceKeysToIntTask<K,V>)par).pending) == 0) {
6554 +                    if ((s = t.sibling) != null)
6555 +                        r = reducer.apply(r, s.result);
6556 +                    (t = p).result = r;
6557 +                }
6558 +                else if (p.casPending(c, 0))
6559 +                    break;
6560 +            }
6561 +        }
6562 +        public final Integer getRawResult() { return result; }
6563 +    }
6564 +
6565 +    @SuppressWarnings("serial")
6566 +    static final class MapReduceValuesToIntTask<K,V>
6567 +        extends BulkTask<K,V,Integer> {
6568 +        final ObjectToInt<? super V> transformer;
6569 +        final IntByIntToInt reducer;
6570 +        final int basis;
6571 +        int result;
6572 +        MapReduceValuesToIntTask<K,V> sibling;
6573 +        MapReduceValuesToIntTask
6574 +            (ConcurrentHashMapV8<K,V> m,
6575 +             ObjectToInt<? super V> transformer,
6576 +             int basis,
6577 +             IntByIntToInt reducer) {
6578 +            super(m);
6579 +            this.transformer = transformer;
6580 +            this.basis = basis; this.reducer = reducer;
6581 +        }
6582 +        MapReduceValuesToIntTask
6583 +            (BulkTask<K,V,?> p, int b, boolean split,
6584 +             ObjectToInt<? super V> transformer,
6585 +             int basis,
6586 +             IntByIntToInt reducer) {
6587 +            super(p, b, split);
6588 +            this.transformer = transformer;
6589 +            this.basis = basis; this.reducer = reducer;
6590 +        }
6591 +        @SuppressWarnings("unchecked") public final void compute() {
6592 +            MapReduceValuesToIntTask<K,V> t = this;
6593 +            final ObjectToInt<? super V> transformer =
6594 +                this.transformer;
6595 +            final IntByIntToInt reducer = this.reducer;
6596 +            if (transformer == null || reducer == null)
6597 +                throw new Error(NullFunctionMessage);
6598 +            final int id = this.basis;
6599 +            int b = batch();
6600 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6601 +                b >>>= 1;
6602 +                t.pending = 1;
6603 +                MapReduceValuesToIntTask<K,V> rt =
6604 +                    new MapReduceValuesToIntTask<K,V>
6605 +                    (t, b, true, transformer, id, reducer);
6606 +                t = new MapReduceValuesToIntTask<K,V>
6607 +                    (t, b, false, transformer, id, reducer);
6608 +                t.sibling = rt;
6609 +                rt.sibling = t;
6610 +                rt.fork();
6611 +            }
6612 +            int r = id;
6613 +            Object v;
6614 +            while ((v = t.advance()) != null)
6615 +                r = reducer.apply(r, transformer.apply((V)v));
6616 +            t.result = r;
6617 +            for (;;) {
6618 +                int c; BulkTask<K,V,?> par; MapReduceValuesToIntTask<K,V> s, p;
6619 +                if ((par = t.parent) == null ||
6620 +                    !(par instanceof MapReduceValuesToIntTask)) {
6621 +                    t.quietlyComplete();
6622 +                    break;
6623 +                }
6624 +                else if ((c = (p = (MapReduceValuesToIntTask<K,V>)par).pending) == 0) {
6625 +                    if ((s = t.sibling) != null)
6626 +                        r = reducer.apply(r, s.result);
6627 +                    (t = p).result = r;
6628 +                }
6629 +                else if (p.casPending(c, 0))
6630 +                    break;
6631 +            }
6632 +        }
6633 +        public final Integer getRawResult() { return result; }
6634 +    }
6635 +
6636 +    @SuppressWarnings("serial")
6637 +    static final class MapReduceEntriesToIntTask<K,V>
6638 +        extends BulkTask<K,V,Integer> {
6639 +        final ObjectToInt<Map.Entry<K,V>> transformer;
6640 +        final IntByIntToInt reducer;
6641 +        final int basis;
6642 +        int result;
6643 +        MapReduceEntriesToIntTask<K,V> sibling;
6644 +        MapReduceEntriesToIntTask
6645 +            (ConcurrentHashMapV8<K,V> m,
6646 +             ObjectToInt<Map.Entry<K,V>> transformer,
6647 +             int basis,
6648 +             IntByIntToInt reducer) {
6649 +            super(m);
6650 +            this.transformer = transformer;
6651 +            this.basis = basis; this.reducer = reducer;
6652 +        }
6653 +        MapReduceEntriesToIntTask
6654 +            (BulkTask<K,V,?> p, int b, boolean split,
6655 +             ObjectToInt<Map.Entry<K,V>> transformer,
6656 +             int basis,
6657 +             IntByIntToInt reducer) {
6658 +            super(p, b, split);
6659 +            this.transformer = transformer;
6660 +            this.basis = basis; this.reducer = reducer;
6661 +        }
6662 +        @SuppressWarnings("unchecked") public final void compute() {
6663 +            MapReduceEntriesToIntTask<K,V> t = this;
6664 +            final ObjectToInt<Map.Entry<K,V>> transformer =
6665 +                this.transformer;
6666 +            final IntByIntToInt reducer = this.reducer;
6667 +            if (transformer == null || reducer == null)
6668 +                throw new Error(NullFunctionMessage);
6669 +            final int id = this.basis;
6670 +            int b = batch();
6671 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6672 +                b >>>= 1;
6673 +                t.pending = 1;
6674 +                MapReduceEntriesToIntTask<K,V> rt =
6675 +                    new MapReduceEntriesToIntTask<K,V>
6676 +                    (t, b, true, transformer, id, reducer);
6677 +                t = new MapReduceEntriesToIntTask<K,V>
6678 +                    (t, b, false, transformer, id, reducer);
6679 +                t.sibling = rt;
6680 +                rt.sibling = t;
6681 +                rt.fork();
6682 +            }
6683 +            int r = id;
6684 +            Object v;
6685 +            while ((v = t.advance()) != null)
6686 +                r = reducer.apply(r, transformer.apply(entryFor((K)t.nextKey, (V)v)));
6687 +            t.result = r;
6688 +            for (;;) {
6689 +                int c; BulkTask<K,V,?> par; MapReduceEntriesToIntTask<K,V> s, p;
6690 +                if ((par = t.parent) == null ||
6691 +                    !(par instanceof MapReduceEntriesToIntTask)) {
6692 +                    t.quietlyComplete();
6693 +                    break;
6694 +                }
6695 +                else if ((c = (p = (MapReduceEntriesToIntTask<K,V>)par).pending) == 0) {
6696 +                    if ((s = t.sibling) != null)
6697 +                        r = reducer.apply(r, s.result);
6698 +                    (t = p).result = r;
6699 +                }
6700 +                else if (p.casPending(c, 0))
6701 +                    break;
6702 +            }
6703 +        }
6704 +        public final Integer getRawResult() { return result; }
6705 +    }
6706 +
6707 +    @SuppressWarnings("serial")
6708 +    static final class MapReduceMappingsToIntTask<K,V>
6709 +        extends BulkTask<K,V,Integer> {
6710 +        final ObjectByObjectToInt<? super K, ? super V> transformer;
6711 +        final IntByIntToInt reducer;
6712 +        final int basis;
6713 +        int result;
6714 +        MapReduceMappingsToIntTask<K,V> sibling;
6715 +        MapReduceMappingsToIntTask
6716 +            (ConcurrentHashMapV8<K,V> m,
6717 +             ObjectByObjectToInt<? super K, ? super V> transformer,
6718 +             int basis,
6719 +             IntByIntToInt reducer) {
6720 +            super(m);
6721 +            this.transformer = transformer;
6722 +            this.basis = basis; this.reducer = reducer;
6723 +        }
6724 +        MapReduceMappingsToIntTask
6725 +            (BulkTask<K,V,?> p, int b, boolean split,
6726 +             ObjectByObjectToInt<? super K, ? super V> transformer,
6727 +             int basis,
6728 +             IntByIntToInt reducer) {
6729 +            super(p, b, split);
6730 +            this.transformer = transformer;
6731 +            this.basis = basis; this.reducer = reducer;
6732 +        }
6733 +        @SuppressWarnings("unchecked") public final void compute() {
6734 +            MapReduceMappingsToIntTask<K,V> t = this;
6735 +            final ObjectByObjectToInt<? super K, ? super V> transformer =
6736 +                this.transformer;
6737 +            final IntByIntToInt reducer = this.reducer;
6738 +            if (transformer == null || reducer == null)
6739 +                throw new Error(NullFunctionMessage);
6740 +            final int id = this.basis;
6741 +            int b = batch();
6742 +            while (b > 1 && t.baseIndex != t.baseLimit) {
6743 +                b >>>= 1;
6744 +                t.pending = 1;
6745 +                MapReduceMappingsToIntTask<K,V> rt =
6746 +                    new MapReduceMappingsToIntTask<K,V>
6747 +                    (t, b, true, transformer, id, reducer);
6748 +                t = new MapReduceMappingsToIntTask<K,V>
6749 +                    (t, b, false, transformer, id, reducer);
6750 +                t.sibling = rt;
6751 +                rt.sibling = t;
6752 +                rt.fork();
6753 +            }
6754 +            int r = id;
6755 +            Object v;
6756 +            while ((v = t.advance()) != null)
6757 +                r = reducer.apply(r, transformer.apply((K)t.nextKey, (V)v));
6758 +            t.result = r;
6759 +            for (;;) {
6760 +                int c; BulkTask<K,V,?> par; MapReduceMappingsToIntTask<K,V> s, p;
6761 +                if ((par = t.parent) == null ||
6762 +                    !(par instanceof MapReduceMappingsToIntTask)) {
6763 +                    t.quietlyComplete();
6764 +                    break;
6765 +                }
6766 +                else if ((c = (p = (MapReduceMappingsToIntTask<K,V>)par).pending) == 0) {
6767 +                    if ((s = t.sibling) != null)
6768 +                        r = reducer.apply(r, s.result);
6769 +                    (t = p).result = r;
6770 +                }
6771 +                else if (p.casPending(c, 0))
6772 +                    break;
6773 +            }
6774 +        }
6775 +        public final Integer getRawResult() { return result; }
6776 +    }
6777 +
6778 +
6779      // Unsafe mechanics
6780      private static final sun.misc.Unsafe UNSAFE;
6781      private static final long counterOffset;
# Line 3277 | Line 6830 | public class ConcurrentHashMapV8<K, V>
6830              }
6831          }
6832      }
3280
6833   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines