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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines