jdk/src/share/classes/java/util/stream/Nodes.java
author psandoz
Tue, 28 May 2013 15:22:30 +0200
changeset 17914 91e138d3b298
parent 17182 b786c0de868c
child 18171 2725a30c1a02
permissions -rw-r--r--
8014393: Minor typo in the spec for j.u.stream.Stream.findFirst() Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     1
/*
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     4
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    10
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    15
 * accompanied this code).
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    16
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    20
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    23
 * questions.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    24
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    25
package java.util.stream;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    26
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    27
import java.util.ArrayDeque;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    28
import java.util.Arrays;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    29
import java.util.Collection;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    30
import java.util.Deque;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    31
import java.util.List;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    32
import java.util.Objects;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    33
import java.util.Spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    34
import java.util.Spliterators;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    35
import java.util.concurrent.CountedCompleter;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    36
import java.util.function.Consumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    37
import java.util.function.DoubleConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    38
import java.util.function.IntConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    39
import java.util.function.IntFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    40
import java.util.function.LongConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    41
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    42
/**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    43
 * Factory methods for constructing implementations of {@link Node} and
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    44
 * {@link Node.Builder} and their primitive specializations.  Fork/Join tasks
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    45
 * for collecting output from a {@link PipelineHelper} to a {@link Node} and
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    46
 * flattening {@link Node}s.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    47
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    48
 * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    49
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    50
final class Nodes {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    51
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    52
    private Nodes() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    53
        throw new Error("no instances");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    54
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    55
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    56
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    57
     * The maximum size of an array that can be allocated.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    58
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    59
    static final long MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    60
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    61
    private static final Node EMPTY_NODE = new EmptyNode.OfRef();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    62
    private static final Node.OfInt EMPTY_INT_NODE = new EmptyNode.OfInt();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    63
    private static final Node.OfLong EMPTY_LONG_NODE = new EmptyNode.OfLong();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    64
    private static final Node.OfDouble EMPTY_DOUBLE_NODE = new EmptyNode.OfDouble();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    65
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    66
    // General shape-based node creation methods
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    67
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    68
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    69
     * Produces an empty node whose count is zero, has no children and no content.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    70
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    71
     * @param <T> the type of elements of the created node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
     * @param shape the shape of the node to be created
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    73
     * @return an empty node.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    74
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    75
    @SuppressWarnings("unchecked")
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    76
    static <T> Node<T> emptyNode(StreamShape shape) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    77
        switch (shape) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    78
            case REFERENCE:    return (Node<T>) EMPTY_NODE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
            case INT_VALUE:    return (Node<T>) EMPTY_INT_NODE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
            case LONG_VALUE:   return (Node<T>) EMPTY_LONG_NODE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    81
            case DOUBLE_VALUE: return (Node<T>) EMPTY_DOUBLE_NODE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    82
            default:
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    83
                throw new IllegalStateException("Unknown shape " + shape);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    84
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    86
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    87
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    88
     * Produces a concatenated {@link Node} that has two or more children.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    89
     * <p>The count of the concatenated node is equal to the sum of the count
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    90
     * of each child. Traversal of the concatenated node traverses the content
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    91
     * of each child in encounter order of the list of children. Splitting a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    92
     * spliterator obtained from the concatenated node preserves the encounter
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    93
     * order of the list of children.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    94
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    95
     * <p>The result may be a concatenated node, the input sole node if the size
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    96
     * of the list is 1, or an empty node.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    97
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    98
     * @param <T> the type of elements of the concatenated node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    99
     * @param shape the shape of the concatenated node to be created
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   100
     * @param nodes the input nodes
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
     * @return a {@code Node} covering the elements of the input nodes
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   102
     * @throws IllegalStateException if all {@link Node} elements of the list
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   103
     * are an not instance of type supported by this factory.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   104
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   105
    @SuppressWarnings("unchecked")
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   106
    static <T> Node<T> conc(StreamShape shape, List<? extends Node<T>> nodes) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   107
        int size = nodes.size();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   108
        if (size == 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   109
            return emptyNode(shape);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   110
        else if (size == 1)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   111
            return nodes.get(0);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   112
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   113
            // Create a right-balanced tree when there are more that 2 nodes
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   114
            switch (shape) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   115
                case REFERENCE: {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   116
                    List<Node<T>> refNodes = (List<Node<T>>) nodes;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   117
                    ConcNode<T> c = new ConcNode<>(refNodes.get(size - 2), refNodes.get(size - 1));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   118
                    for (int i = size - 3; i >= 0; i--) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   119
                        c = new ConcNode<>(refNodes.get(i), c);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   120
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
                    return c;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
                case INT_VALUE: {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
                    List<? extends Node.OfInt> intNodes = (List<? extends Node.OfInt>) nodes;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   125
                    IntConcNode c = new IntConcNode(intNodes.get(size - 2), intNodes.get(size - 1));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   126
                    for (int i = size - 3; i >= 0; i--) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   127
                        c = new IntConcNode(intNodes.get(i), c);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   128
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   129
                    return (Node<T>) c;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   130
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   131
                case LONG_VALUE: {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   132
                    List<? extends Node.OfLong> longNodes = (List<? extends Node.OfLong>) nodes;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   133
                    LongConcNode c = new LongConcNode(longNodes.get(size - 2), longNodes.get(size - 1));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   134
                    for (int i = size - 3; i >= 0; i--) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   135
                        c = new LongConcNode(longNodes.get(i), c);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   136
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   137
                    return (Node<T>) c;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   138
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   139
                case DOUBLE_VALUE: {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   140
                    List<? extends Node.OfDouble> doubleNodes = (List<? extends Node.OfDouble>) nodes;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   141
                    DoubleConcNode c = new DoubleConcNode(doubleNodes.get(size - 2), doubleNodes.get(size - 1));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   142
                    for (int i = size - 3; i >= 0; i--) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   143
                        c = new DoubleConcNode(doubleNodes.get(i), c);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   144
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   145
                    return (Node<T>) c;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   146
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   147
                default:
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   148
                    throw new IllegalStateException("Unknown shape " + shape);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   149
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   150
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   151
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   152
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   153
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   154
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   155
     * Truncate a {@link Node}, returning a node describing a subsequence of
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   156
     * the contents of the input node.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   157
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   158
     * @param <T> the type of elements of the input node and truncated node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   159
     * @param input the input node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   160
     * @param from the starting offset to include in the truncated node (inclusive)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   161
     * @param to the ending offset ot include in the truncated node (exclusive)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   162
     * @param generator the array factory (only used for reference nodes)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   163
     * @return the truncated node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   164
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   165
    @SuppressWarnings("unchecked")
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   166
    static <T> Node<T> truncateNode(Node<T> input, long from, long to, IntFunction<T[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   167
        StreamShape shape = input.getShape();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   168
        long size = truncatedSize(input.count(), from, to);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   169
        if (size == 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   170
            return emptyNode(shape);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   171
        else if (from == 0 && to >= input.count())
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   172
            return input;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   173
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   174
        switch (shape) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   175
            case REFERENCE: {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   176
                Spliterator<T> spliterator = input.spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   177
                Node.Builder<T> nodeBuilder = Nodes.builder(size, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   178
                nodeBuilder.begin(size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   179
                for (int i = 0; i < from && spliterator.tryAdvance(e -> { }); i++) { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   180
                for (int i = 0; (i < size) && spliterator.tryAdvance(nodeBuilder); i++) { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   181
                nodeBuilder.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   182
                return nodeBuilder.build();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   183
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   184
            case INT_VALUE: {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   185
                Spliterator.OfInt spliterator = ((Node.OfInt) input).spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   186
                Node.Builder.OfInt nodeBuilder = Nodes.intBuilder(size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   187
                nodeBuilder.begin(size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   188
                for (int i = 0; i < from && spliterator.tryAdvance((IntConsumer) e -> { }); i++) { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   189
                for (int i = 0; (i < size) && spliterator.tryAdvance((IntConsumer) nodeBuilder); i++) { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   190
                nodeBuilder.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   191
                return (Node<T>) nodeBuilder.build();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   192
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   193
            case LONG_VALUE: {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   194
                Spliterator.OfLong spliterator = ((Node.OfLong) input).spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   195
                Node.Builder.OfLong nodeBuilder = Nodes.longBuilder(size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   196
                nodeBuilder.begin(size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   197
                for (int i = 0; i < from && spliterator.tryAdvance((LongConsumer) e -> { }); i++) { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   198
                for (int i = 0; (i < size) && spliterator.tryAdvance((LongConsumer) nodeBuilder); i++) { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   199
                nodeBuilder.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   200
                return (Node<T>) nodeBuilder.build();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   201
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   202
            case DOUBLE_VALUE: {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   203
                Spliterator.OfDouble spliterator = ((Node.OfDouble) input).spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   204
                Node.Builder.OfDouble nodeBuilder = Nodes.doubleBuilder(size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   205
                nodeBuilder.begin(size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   206
                for (int i = 0; i < from && spliterator.tryAdvance((DoubleConsumer) e -> { }); i++) { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   207
                for (int i = 0; (i < size) && spliterator.tryAdvance((DoubleConsumer) nodeBuilder); i++) { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   208
                nodeBuilder.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   209
                return (Node<T>) nodeBuilder.build();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   210
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   211
            default:
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   212
                throw new IllegalStateException("Unknown shape " + shape);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   213
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   214
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   215
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   216
    private static long truncatedSize(long size, long from, long to) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   217
        if (from >= 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   218
            size = Math.max(0, size - from);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   219
        long limit = to - from;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   220
        if (limit >= 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   221
            size = Math.min(size, limit);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   222
        return size;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   223
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   224
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   225
    // Reference-based node methods
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   226
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   227
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   228
     * Produces a {@link Node} describing an array.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   229
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   230
     * <p>The node will hold a reference to the array and will not make a copy.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   231
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   232
     * @param <T> the type of elements held by the node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   233
     * @param array the array
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   234
     * @return a node holding an array
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   235
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   236
    static <T> Node<T> node(T[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   237
        return new ArrayNode<>(array);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   238
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   239
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   240
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   241
     * Produces a {@link Node} describing a {@link Collection}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   242
     * <p>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   243
     * The node will hold a reference to the collection and will not make a copy.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   244
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   245
     * @param <T> the type of elements held by the node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   246
     * @param c the collection
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   247
     * @return a node holding a collection
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   248
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   249
    static <T> Node<T> node(Collection<T> c) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   250
        return new CollectionNode<>(c);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   251
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   252
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   253
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   254
     * Produces a {@link Node.Builder}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   255
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   256
     * @param exactSizeIfKnown -1 if a variable size builder is requested,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   257
     * otherwise the exact capacity desired.  A fixed capacity builder will
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   258
     * fail if the wrong number of elements are added to the builder.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   259
     * @param generator the array factory
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   260
     * @param <T> the type of elements of the node builder
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   261
     * @return a {@code Node.Builder}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   262
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   263
    static <T> Node.Builder<T> builder(long exactSizeIfKnown, IntFunction<T[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   264
        return (exactSizeIfKnown >= 0 && exactSizeIfKnown < MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   265
               ? new FixedNodeBuilder<>(exactSizeIfKnown, generator)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   266
               : builder();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   267
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   268
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   269
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   270
     * Produces a variable size @{link Node.Builder}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   271
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   272
     * @param <T> the type of elements of the node builder
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   273
     * @return a {@code Node.Builder}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   274
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   275
    static <T> Node.Builder<T> builder() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   276
        return new SpinedNodeBuilder<>();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   277
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   278
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   279
    // Int nodes
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   280
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   281
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   282
     * Produces a {@link Node.OfInt} describing an int[] array.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   283
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   284
     * <p>The node will hold a reference to the array and will not make a copy.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   285
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   286
     * @param array the array
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   287
     * @return a node holding an array
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   288
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   289
    static Node.OfInt node(int[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   290
        return new IntArrayNode(array);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   291
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   292
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   293
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   294
     * Produces a {@link Node.Builder.OfInt}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   295
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   296
     * @param exactSizeIfKnown -1 if a variable size builder is requested,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   297
     * otherwise the exact capacity desired.  A fixed capacity builder will
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   298
     * fail if the wrong number of elements are added to the builder.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   299
     * @return a {@code Node.Builder.OfInt}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   300
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   301
    static Node.Builder.OfInt intBuilder(long exactSizeIfKnown) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   302
        return (exactSizeIfKnown >= 0 && exactSizeIfKnown < MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   303
               ? new IntFixedNodeBuilder(exactSizeIfKnown)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   304
               : intBuilder();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   305
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   306
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   307
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   308
     * Produces a variable size @{link Node.Builder.OfInt}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   309
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   310
     * @return a {@code Node.Builder.OfInt}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   311
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   312
    static Node.Builder.OfInt intBuilder() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   313
        return new IntSpinedNodeBuilder();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   314
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   315
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   316
    // Long nodes
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   317
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   318
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   319
     * Produces a {@link Node.OfLong} describing a long[] array.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   320
     * <p>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   321
     * The node will hold a reference to the array and will not make a copy.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   322
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   323
     * @param array the array
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   324
     * @return a node holding an array
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   325
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   326
    static Node.OfLong node(final long[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   327
        return new LongArrayNode(array);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   328
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   329
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   330
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   331
     * Produces a {@link Node.Builder.OfLong}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   332
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   333
     * @param exactSizeIfKnown -1 if a variable size builder is requested,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   334
     * otherwise the exact capacity desired.  A fixed capacity builder will
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   335
     * fail if the wrong number of elements are added to the builder.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   336
     * @return a {@code Node.Builder.OfLong}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   337
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   338
    static Node.Builder.OfLong longBuilder(long exactSizeIfKnown) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   339
        return (exactSizeIfKnown >= 0 && exactSizeIfKnown < MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   340
               ? new LongFixedNodeBuilder(exactSizeIfKnown)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   341
               : longBuilder();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   342
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   343
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   344
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   345
     * Produces a variable size @{link Node.Builder.OfLong}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   346
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   347
     * @return a {@code Node.Builder.OfLong}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   348
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   349
    static Node.Builder.OfLong longBuilder() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   350
        return new LongSpinedNodeBuilder();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   351
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   352
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   353
    // Double nodes
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   354
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   355
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   356
     * Produces a {@link Node.OfDouble} describing a double[] array.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   357
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   358
     * <p>The node will hold a reference to the array and will not make a copy.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   359
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   360
     * @param array the array
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   361
     * @return a node holding an array
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   362
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   363
    static Node.OfDouble node(final double[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   364
        return new DoubleArrayNode(array);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   365
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   366
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   367
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   368
     * Produces a {@link Node.Builder.OfDouble}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   369
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   370
     * @param exactSizeIfKnown -1 if a variable size builder is requested,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   371
     * otherwise the exact capacity desired.  A fixed capacity builder will
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   372
     * fail if the wrong number of elements are added to the builder.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   373
     * @return a {@code Node.Builder.OfDouble}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   374
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   375
    static Node.Builder.OfDouble doubleBuilder(long exactSizeIfKnown) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   376
        return (exactSizeIfKnown >= 0 && exactSizeIfKnown < MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   377
               ? new DoubleFixedNodeBuilder(exactSizeIfKnown)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   378
               : doubleBuilder();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   379
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   380
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   381
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   382
     * Produces a variable size @{link Node.Builder.OfDouble}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   383
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   384
     * @return a {@code Node.Builder.OfDouble}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   385
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   386
    static Node.Builder.OfDouble doubleBuilder() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   387
        return new DoubleSpinedNodeBuilder();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   388
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   389
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   390
    // Parallel evaluation of pipelines to nodes
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   391
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   392
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   393
     * Collect, in parallel, elements output from a pipeline and describe those
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   394
     * elements with a {@link Node}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   395
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   396
     * @implSpec
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   397
     * If the exact size of the output from the pipeline is known and the source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   398
     * {@link Spliterator} has the {@link Spliterator#SUBSIZED} characteristic,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   399
     * then a flat {@link Node} will be returned whose content is an array,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   400
     * since the size is known the array can be constructed in advance and
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   401
     * output elements can be placed into the array concurrently by leaf
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   402
     * tasks at the correct offsets.  If the exact size is not known, output
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   403
     * elements are collected into a conc-node whose shape mirrors that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   404
     * of the computation. This conc-node can then be flattened in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   405
     * parallel to produce a flat {@code Node} if desired.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   406
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   407
     * @param helper the pipeline helper describing the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   408
     * @param flattenTree whether a conc node should be flattened into a node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   409
     *                    describing an array before returning
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   410
     * @param generator the array generator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   411
     * @return a {@link Node} describing the output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   412
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   413
    public static <P_IN, P_OUT> Node<P_OUT> collect(PipelineHelper<P_OUT> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   414
                                                    Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   415
                                                    boolean flattenTree,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   416
                                                    IntFunction<P_OUT[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   417
        long size = helper.exactOutputSizeIfKnown(spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   418
        if (size >= 0 && spliterator.hasCharacteristics(Spliterator.SUBSIZED)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   419
            if (size >= MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   420
                throw new IllegalArgumentException("Stream size exceeds max array size");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   421
            P_OUT[] array = generator.apply((int) size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   422
            new SizedCollectorTask.OfRef<>(spliterator, helper, array).invoke();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   423
            return node(array);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   424
        } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   425
            Node<P_OUT> node = new CollectorTask<>(helper, generator, spliterator).invoke();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   426
            return flattenTree ? flatten(node, generator) : node;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   427
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   428
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   429
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   430
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   431
     * Collect, in parallel, elements output from an int-valued pipeline and
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   432
     * describe those elements with a {@link Node.OfInt}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   433
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   434
     * @implSpec
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   435
     * If the exact size of the output from the pipeline is known and the source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   436
     * {@link Spliterator} has the {@link Spliterator#SUBSIZED} characteristic,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   437
     * then a flat {@link Node} will be returned whose content is an array,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   438
     * since the size is known the array can be constructed in advance and
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   439
     * output elements can be placed into the array concurrently by leaf
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   440
     * tasks at the correct offsets.  If the exact size is not known, output
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   441
     * elements are collected into a conc-node whose shape mirrors that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   442
     * of the computation. This conc-node can then be flattened in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   443
     * parallel to produce a flat {@code Node.OfInt} if desired.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   444
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   445
     * @param <P_IN> the type of elements from the source Spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   446
     * @param helper the pipeline helper describing the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   447
     * @param flattenTree whether a conc node should be flattened into a node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   448
     *                    describing an array before returning
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   449
     * @return a {@link Node.OfInt} describing the output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   450
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   451
    public static <P_IN> Node.OfInt collectInt(PipelineHelper<Integer> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   452
                                               Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   453
                                               boolean flattenTree) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   454
        long size = helper.exactOutputSizeIfKnown(spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   455
        if (size >= 0 && spliterator.hasCharacteristics(Spliterator.SUBSIZED)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   456
            if (size >= MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   457
                throw new IllegalArgumentException("Stream size exceeds max array size");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   458
            int[] array = new int[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   459
            new SizedCollectorTask.OfInt<>(spliterator, helper, array).invoke();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   460
            return node(array);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   461
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   462
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   463
            Node.OfInt node = new IntCollectorTask<>(helper, spliterator).invoke();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   464
            return flattenTree ? flattenInt(node) : node;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   465
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   466
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   467
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   468
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   469
     * Collect, in parallel, elements output from a long-valued pipeline and
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   470
     * describe those elements with a {@link Node.OfLong}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   471
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   472
     * @implSpec
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   473
     * If the exact size of the output from the pipeline is known and the source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   474
     * {@link Spliterator} has the {@link Spliterator#SUBSIZED} characteristic,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   475
     * then a flat {@link Node} will be returned whose content is an array,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   476
     * since the size is known the array can be constructed in advance and
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   477
     * output elements can be placed into the array concurrently by leaf
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   478
     * tasks at the correct offsets.  If the exact size is not known, output
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   479
     * elements are collected into a conc-node whose shape mirrors that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   480
     * of the computation. This conc-node can then be flattened in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   481
     * parallel to produce a flat {@code Node.OfLong} if desired.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   482
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   483
     * @param <P_IN> the type of elements from the source Spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   484
     * @param helper the pipeline helper describing the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   485
     * @param flattenTree whether a conc node should be flattened into a node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   486
     *                    describing an array before returning
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   487
     * @return a {@link Node.OfLong} describing the output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   488
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   489
    public static <P_IN> Node.OfLong collectLong(PipelineHelper<Long> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   490
                                                 Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   491
                                                 boolean flattenTree) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   492
        long size = helper.exactOutputSizeIfKnown(spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   493
        if (size >= 0 && spliterator.hasCharacteristics(Spliterator.SUBSIZED)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   494
            if (size >= MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   495
                throw new IllegalArgumentException("Stream size exceeds max array size");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   496
            long[] array = new long[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   497
            new SizedCollectorTask.OfLong<>(spliterator, helper, array).invoke();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   498
            return node(array);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   499
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   500
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   501
            Node.OfLong node = new LongCollectorTask<>(helper, spliterator).invoke();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   502
            return flattenTree ? flattenLong(node) : node;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   503
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   504
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   505
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   506
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   507
     * Collect, in parallel, elements output from n double-valued pipeline and
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   508
     * describe those elements with a {@link Node.OfDouble}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   509
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   510
     * @implSpec
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   511
     * If the exact size of the output from the pipeline is known and the source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   512
     * {@link Spliterator} has the {@link Spliterator#SUBSIZED} characteristic,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   513
     * then a flat {@link Node} will be returned whose content is an array,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   514
     * since the size is known the array can be constructed in advance and
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   515
     * output elements can be placed into the array concurrently by leaf
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   516
     * tasks at the correct offsets.  If the exact size is not known, output
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   517
     * elements are collected into a conc-node whose shape mirrors that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   518
     * of the computation. This conc-node can then be flattened in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   519
     * parallel to produce a flat {@code Node.OfDouble} if desired.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   520
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   521
     * @param <P_IN> the type of elements from the source Spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   522
     * @param helper the pipeline helper describing the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   523
     * @param flattenTree whether a conc node should be flattened into a node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   524
     *                    describing an array before returning
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   525
     * @return a {@link Node.OfDouble} describing the output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   526
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   527
    public static <P_IN> Node.OfDouble collectDouble(PipelineHelper<Double> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   528
                                                     Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   529
                                                     boolean flattenTree) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   530
        long size = helper.exactOutputSizeIfKnown(spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   531
        if (size >= 0 && spliterator.hasCharacteristics(Spliterator.SUBSIZED)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   532
            if (size >= MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   533
                throw new IllegalArgumentException("Stream size exceeds max array size");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   534
            double[] array = new double[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   535
            new SizedCollectorTask.OfDouble<>(spliterator, helper, array).invoke();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   536
            return node(array);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   537
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   538
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   539
            Node.OfDouble node = new DoubleCollectorTask<>(helper, spliterator).invoke();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   540
            return flattenTree ? flattenDouble(node) : node;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   541
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   542
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   543
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   544
    // Parallel flattening of nodes
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   545
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   546
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   547
     * Flatten, in parallel, a {@link Node}.  A flattened node is one that has
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   548
     * no children.  If the node is already flat, it is simply returned.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   549
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   550
     * @implSpec
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   551
     * If a new node is to be created, the generator is used to create an array
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   552
     * whose length is {@link Node#count()}.  Then the node tree is traversed
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   553
     * and leaf node elements are placed in the array concurrently by leaf tasks
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   554
     * at the correct offsets.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   555
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   556
     * @param <T> type of elements contained by the node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   557
     * @param node the node to flatten
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   558
     * @param generator the array factory used to create array instances
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   559
     * @return a flat {@code Node}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   560
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   561
    public static <T> Node<T> flatten(Node<T> node, IntFunction<T[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   562
        if (node.getChildCount() > 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   563
            T[] array = generator.apply((int) node.count());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   564
            new ToArrayTask.OfRef<>(node, array, 0).invoke();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   565
            return node(array);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   566
        } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   567
            return node;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   568
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   569
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   570
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   571
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   572
     * Flatten, in parallel, a {@link Node.OfInt}.  A flattened node is one that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   573
     * has no children.  If the node is already flat, it is simply returned.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   574
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   575
     * @implSpec
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   576
     * If a new node is to be created, a new int[] array is created whose length
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   577
     * is {@link Node#count()}.  Then the node tree is traversed and leaf node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   578
     * elements are placed in the array concurrently by leaf tasks at the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   579
     * correct offsets.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   580
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   581
     * @param node the node to flatten
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   582
     * @return a flat {@code Node.OfInt}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   583
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   584
    public static Node.OfInt flattenInt(Node.OfInt node) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   585
        if (node.getChildCount() > 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   586
            int[] array = new int[(int) node.count()];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   587
            new ToArrayTask.OfInt(node, array, 0).invoke();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   588
            return node(array);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   589
        } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   590
            return node;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   591
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   592
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   593
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   594
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   595
     * Flatten, in parallel, a {@link Node.OfLong}.  A flattened node is one that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   596
     * has no children.  If the node is already flat, it is simply returned.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   597
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   598
     * @implSpec
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   599
     * If a new node is to be created, a new long[] array is created whose length
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   600
     * is {@link Node#count()}.  Then the node tree is traversed and leaf node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   601
     * elements are placed in the array concurrently by leaf tasks at the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   602
     * correct offsets.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   603
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   604
     * @param node the node to flatten
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   605
     * @return a flat {@code Node.OfLong}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   606
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   607
    public static Node.OfLong flattenLong(Node.OfLong node) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   608
        if (node.getChildCount() > 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   609
            long[] array = new long[(int) node.count()];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   610
            new ToArrayTask.OfLong(node, array, 0).invoke();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   611
            return node(array);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   612
        } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   613
            return node;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   614
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   615
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   616
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   617
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   618
     * Flatten, in parallel, a {@link Node.OfDouble}.  A flattened node is one that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   619
     * has no children.  If the node is already flat, it is simply returned.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   620
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   621
     * @implSpec
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   622
     * If a new node is to be created, a new double[] array is created whose length
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   623
     * is {@link Node#count()}.  Then the node tree is traversed and leaf node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   624
     * elements are placed in the array concurrently by leaf tasks at the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   625
     * correct offsets.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   626
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   627
     * @param node the node to flatten
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   628
     * @return a flat {@code Node.OfDouble}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   629
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   630
    public static Node.OfDouble flattenDouble(Node.OfDouble node) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   631
        if (node.getChildCount() > 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   632
            double[] array = new double[(int) node.count()];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   633
            new ToArrayTask.OfDouble(node, array, 0).invoke();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   634
            return node(array);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   635
        } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   636
            return node;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   637
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   638
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   639
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   640
    // Implementations
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   641
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   642
    private static abstract class EmptyNode<T, T_ARR, T_CONS> implements Node<T> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   643
        EmptyNode() { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   644
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   645
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   646
        public T[] asArray(IntFunction<T[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   647
            return generator.apply(0);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   648
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   649
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   650
        public void copyInto(T_ARR array, int offset) { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   651
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   652
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   653
        public long count() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   654
            return 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   655
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   656
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   657
        public void forEach(T_CONS consumer) { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   658
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   659
        private static class OfRef<T> extends EmptyNode<T, T[], Consumer<? super T>> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   660
            private OfRef() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   661
                super();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   662
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   663
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   664
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   665
            public Spliterator<T> spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   666
                return Spliterators.emptySpliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   667
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   668
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   669
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   670
        private static final class OfInt
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   671
                extends EmptyNode<Integer, int[], IntConsumer>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   672
                implements Node.OfInt {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   673
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   674
            OfInt() { } // Avoid creation of special accessor
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   675
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   676
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   677
            public Spliterator.OfInt spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   678
                return Spliterators.emptyIntSpliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   679
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   680
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   681
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   682
            public int[] asIntArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   683
                return EMPTY_INT_ARRAY;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   684
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   685
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   686
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   687
        private static final class OfLong
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   688
                extends EmptyNode<Long, long[], LongConsumer>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   689
                implements Node.OfLong {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   690
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   691
            OfLong() { } // Avoid creation of special accessor
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   692
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   693
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   694
            public Spliterator.OfLong spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   695
                return Spliterators.emptyLongSpliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   696
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   697
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   698
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   699
            public long[] asLongArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   700
                return EMPTY_LONG_ARRAY;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   701
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   702
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   703
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   704
        private static final class OfDouble
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   705
                extends EmptyNode<Double, double[], DoubleConsumer>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   706
                implements Node.OfDouble {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   707
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   708
            OfDouble() { } // Avoid creation of special accessor
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   709
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   710
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   711
            public Spliterator.OfDouble spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   712
                return Spliterators.emptyDoubleSpliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   713
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   714
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   715
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   716
            public double[] asDoubleArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   717
                return EMPTY_DOUBLE_ARRAY;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   718
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   719
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   720
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   721
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   722
    /** Node class for a reference array */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   723
    private static class ArrayNode<T> implements Node<T> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   724
        final T[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   725
        int curSize;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   726
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   727
        @SuppressWarnings("unchecked")
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   728
        ArrayNode(long size, IntFunction<T[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   729
            if (size >= MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   730
                throw new IllegalArgumentException("Stream size exceeds max array size");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   731
            this.array = generator.apply((int) size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   732
            this.curSize = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   733
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   734
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   735
        ArrayNode(T[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   736
            this.array = array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   737
            this.curSize = array.length;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   738
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   739
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   740
        // Node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   741
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   742
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   743
        public Spliterator<T> spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   744
            return Arrays.spliterator(array, 0, curSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   745
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   746
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   747
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   748
        public void copyInto(T[] dest, int destOffset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   749
            System.arraycopy(array, 0, dest, destOffset, curSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   750
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   751
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   752
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   753
        public T[] asArray(IntFunction<T[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   754
            if (array.length == curSize) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   755
                return array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   756
            } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   757
                throw new IllegalStateException();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   758
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   759
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   760
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   761
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   762
        public long count() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   763
            return curSize;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   764
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   765
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   766
        // Traversable
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   767
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   768
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   769
        public void forEach(Consumer<? super T> consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   770
            for (int i = 0; i < curSize; i++) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   771
                consumer.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   772
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   773
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   774
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   775
        //
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   776
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   777
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   778
        public String toString() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   779
            return String.format("ArrayNode[%d][%s]",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   780
                                 array.length - curSize, Arrays.toString(array));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   781
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   782
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   783
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   784
    /** Node class for a Collection */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   785
    private static final class CollectionNode<T> implements Node<T> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   786
        private final Collection<T> c;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   787
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   788
        CollectionNode(Collection<T> c) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   789
            this.c = c;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   790
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   791
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   792
        // Node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   793
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   794
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   795
        public Spliterator<T> spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   796
            return c.stream().spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   797
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   798
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   799
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   800
        public void copyInto(T[] array, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   801
            for (T t : c)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   802
                array[offset++] = t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   803
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   804
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   805
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   806
        @SuppressWarnings("unchecked")
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   807
        public T[] asArray(IntFunction<T[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   808
            return c.toArray(generator.apply(c.size()));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   809
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   810
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   811
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   812
        public long count() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   813
            return c.size();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   814
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   815
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   816
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   817
        public void forEach(Consumer<? super T> consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   818
            c.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   819
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   820
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   821
        //
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   822
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   823
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   824
        public String toString() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   825
            return String.format("CollectionNode[%d][%s]", c.size(), c);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   826
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   827
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   828
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   829
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   830
     * Node class for an internal node with two or more children
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   831
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   832
    static final class ConcNode<T> implements Node<T> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   833
        private final Node<T> left;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   834
        private final Node<T> right;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   835
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   836
        private final long size;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   837
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   838
        ConcNode(Node<T> left, Node<T> right) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   839
            this.left = left;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   840
            this.right = right;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   841
            // The Node count will be required when the Node spliterator is
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   842
            // obtained and it is cheaper to aggressively calculate bottom up
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   843
            // as the tree is built rather than later on from the top down
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   844
            // traversing the tree
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   845
            this.size = left.count() + right.count();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   846
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   847
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   848
        // Node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   849
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   850
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   851
        public Spliterator<T> spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   852
            return new Nodes.InternalNodeSpliterator.OfRef<>(this);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   853
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   854
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   855
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   856
        public int getChildCount() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   857
            return 2;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   858
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   859
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   860
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   861
        public Node<T> getChild(int i) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   862
            if (i == 0) return left;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   863
            if (i == 1) return right;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   864
            throw new IndexOutOfBoundsException();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   865
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   866
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   867
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   868
        public void copyInto(T[] array, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   869
            Objects.requireNonNull(array);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   870
            left.copyInto(array, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   871
            right.copyInto(array, offset + (int) left.count());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   872
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   873
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   874
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   875
        public T[] asArray(IntFunction<T[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   876
            T[] array = generator.apply((int) count());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   877
            copyInto(array, 0);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   878
            return array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   879
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   880
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   881
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   882
        public long count() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   883
            return size;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   884
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   885
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   886
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   887
        public void forEach(Consumer<? super T> consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   888
            left.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   889
            right.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   890
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   891
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   892
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   893
        public String toString() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   894
            if (count() < 32) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   895
                return String.format("ConcNode[%s.%s]", left, right);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   896
            } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   897
                return String.format("ConcNode[size=%d]", count());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   898
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   899
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   900
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   901
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   902
    /** Abstract class for spliterator for all internal node classes */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   903
    private static abstract class InternalNodeSpliterator<T,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   904
                                                          S extends Spliterator<T>,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   905
                                                          N extends Node<T>, C>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   906
            implements Spliterator<T> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   907
        // Node we are pointing to
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   908
        // null if full traversal has occurred
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   909
        N curNode;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   910
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   911
        // next child of curNode to consume
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   912
        int curChildIndex;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   913
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   914
        // The spliterator of the curNode if that node is last and has no children.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   915
        // This spliterator will be delegated to for splitting and traversing.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   916
        // null if curNode has children
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   917
        S lastNodeSpliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   918
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   919
        // spliterator used while traversing with tryAdvance
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   920
        // null if no partial traversal has occurred
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   921
        S tryAdvanceSpliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   922
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   923
        // node stack used when traversing to search and find leaf nodes
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   924
        // null if no partial traversal has occurred
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   925
        Deque<N> tryAdvanceStack;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   926
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   927
        InternalNodeSpliterator(N curNode) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   928
            this.curNode = curNode;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   929
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   930
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   931
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   932
         * Initiate a stack containing, in left-to-right order, the child nodes
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   933
         * covered by this spliterator
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   934
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   935
        protected final Deque<N> initStack() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   936
            // Bias size to the case where leaf nodes are close to this node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   937
            // 8 is the minimum initial capacity for the ArrayDeque implementation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   938
            Deque<N> stack = new ArrayDeque<>(8);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   939
            for (int i = curNode.getChildCount() - 1; i >= curChildIndex; i--)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   940
                stack.addFirst((N) curNode.getChild(i));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   941
            return stack;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   942
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   943
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   944
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   945
         * Depth first search, in left-to-right order, of the node tree, using
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   946
         * an explicit stack, to find the next non-empty leaf node.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   947
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   948
        protected final N findNextLeafNode(Deque<N> stack) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   949
            N n = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   950
            while ((n = stack.pollFirst()) != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   951
                if (n.getChildCount() == 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   952
                    if (n.count() > 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   953
                        return n;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   954
                } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   955
                    for (int i = n.getChildCount() - 1; i >= 0; i--)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   956
                        stack.addFirst((N) n.getChild(i));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   957
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   958
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   959
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   960
            return null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   961
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   962
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   963
        protected final boolean internalTryAdvance(C consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   964
            if (curNode == null)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   965
                return false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   966
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   967
            if (tryAdvanceSpliterator == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   968
                if (lastNodeSpliterator == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   969
                    // Initiate the node stack
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   970
                    tryAdvanceStack = initStack();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   971
                    N leaf = findNextLeafNode(tryAdvanceStack);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   972
                    if (leaf != null)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   973
                        tryAdvanceSpliterator = (S) leaf.spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   974
                    else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   975
                        // A non-empty leaf node was not found
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   976
                        // No elements to traverse
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   977
                        curNode = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   978
                        return false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   979
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   980
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   981
                else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   982
                    tryAdvanceSpliterator = lastNodeSpliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   983
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   984
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   985
            boolean hasNext = tryAdvance(tryAdvanceSpliterator, consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   986
            if (!hasNext) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   987
                if (lastNodeSpliterator == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   988
                    // Advance to the spliterator of the next non-empty leaf node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   989
                    Node<T> leaf = findNextLeafNode(tryAdvanceStack);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   990
                    if (leaf != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   991
                        tryAdvanceSpliterator = (S) leaf.spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   992
                        // Since the node is not-empty the spliterator can be advanced
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   993
                        return tryAdvance(tryAdvanceSpliterator, consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   994
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   995
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   996
                // No more elements to traverse
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   997
                curNode = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   998
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   999
            return hasNext;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1000
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1001
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1002
        protected abstract boolean tryAdvance(S spliterator, C consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1003
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1004
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1005
        @SuppressWarnings("unchecked")
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1006
        public S trySplit() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1007
            if (curNode == null || tryAdvanceSpliterator != null)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1008
                return null; // Cannot split if fully or partially traversed
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1009
            else if (lastNodeSpliterator != null)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1010
                return (S) lastNodeSpliterator.trySplit();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1011
            else if (curChildIndex < curNode.getChildCount() - 1)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1012
                return (S) curNode.getChild(curChildIndex++).spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1013
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1014
                curNode = (N) curNode.getChild(curChildIndex);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1015
                if (curNode.getChildCount() == 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1016
                    lastNodeSpliterator = (S) curNode.spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1017
                    return (S) lastNodeSpliterator.trySplit();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1018
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1019
                else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1020
                    curChildIndex = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1021
                    return (S) curNode.getChild(curChildIndex++).spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1022
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1023
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1024
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1025
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1026
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1027
        public long estimateSize() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1028
            if (curNode == null)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1029
                return 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1030
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1031
            // Will not reflect the effects of partial traversal.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1032
            // This is compliant with the specification
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1033
            if (lastNodeSpliterator != null)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1034
                return lastNodeSpliterator.estimateSize();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1035
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1036
                long size = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1037
                for (int i = curChildIndex; i < curNode.getChildCount(); i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1038
                    size += curNode.getChild(i).count();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1039
                return size;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1040
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1041
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1042
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1043
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1044
        public int characteristics() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1045
            return Spliterator.SIZED;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1046
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1047
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1048
        private static final class OfRef<T>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1049
                extends InternalNodeSpliterator<T, Spliterator<T>, Node<T>, Consumer<? super T>> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1050
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1051
            OfRef(Node<T> curNode) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1052
                super(curNode);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1053
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1054
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1055
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1056
            public boolean tryAdvance(Consumer<? super T> consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1057
                return internalTryAdvance(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1058
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1059
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1060
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1061
            protected boolean tryAdvance(Spliterator<T> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1062
                                         Consumer<? super T> consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1063
                return spliterator.tryAdvance(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1064
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1065
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1066
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1067
            public void forEachRemaining(Consumer<? super T> consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1068
                if (curNode == null)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1069
                    return;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1070
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1071
                if (tryAdvanceSpliterator == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1072
                    if (lastNodeSpliterator == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1073
                        Deque<Node<T>> stack = initStack();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1074
                        Node<T> leaf;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1075
                        while ((leaf = findNextLeafNode(stack)) != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1076
                            leaf.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1077
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1078
                        curNode = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1079
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1080
                    else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1081
                        lastNodeSpliterator.forEachRemaining(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1082
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1083
                else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1084
                    while(tryAdvance(consumer)) { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1085
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1086
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1087
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1088
        private static final class OfInt
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1089
                extends InternalNodeSpliterator<Integer, Spliterator.OfInt, Node.OfInt, IntConsumer>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1090
                implements Spliterator.OfInt {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1091
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1092
            OfInt(Node.OfInt cur) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1093
                super(cur);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1094
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1095
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1096
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1097
            public boolean tryAdvance(IntConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1098
                return internalTryAdvance(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1099
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1100
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1101
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1102
            protected boolean tryAdvance(Spliterator.OfInt spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1103
                                         IntConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1104
                return spliterator.tryAdvance(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1105
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1106
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1107
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1108
            public void forEachRemaining(IntConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1109
                if (curNode == null)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1110
                    return;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1111
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1112
                if (tryAdvanceSpliterator == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1113
                    if (lastNodeSpliterator == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1114
                        Deque<Node.OfInt> stack = initStack();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1115
                        Node.OfInt leaf;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1116
                        while ((leaf = findNextLeafNode(stack)) != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1117
                            leaf.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1118
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1119
                        curNode = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1120
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1121
                    else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1122
                        lastNodeSpliterator.forEachRemaining(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1123
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1124
                else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1125
                    while(tryAdvance(consumer)) { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1126
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1127
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1128
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1129
        private static final class OfLong
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1130
                extends InternalNodeSpliterator<Long, Spliterator.OfLong, Node.OfLong, LongConsumer>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1131
                implements Spliterator.OfLong {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1132
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1133
            OfLong(Node.OfLong cur) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1134
                super(cur);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1135
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1136
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1137
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1138
            public boolean tryAdvance(LongConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1139
                return internalTryAdvance(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1140
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1141
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1142
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1143
            protected boolean tryAdvance(Spliterator.OfLong spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1144
                                         LongConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1145
                return spliterator.tryAdvance(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1146
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1147
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1148
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1149
            public void forEachRemaining(LongConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1150
                if (curNode == null)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1151
                    return;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1152
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1153
                if (tryAdvanceSpliterator == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1154
                    if (lastNodeSpliterator == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1155
                        Deque<Node.OfLong> stack = initStack();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1156
                        Node.OfLong leaf;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1157
                        while ((leaf = findNextLeafNode(stack)) != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1158
                            leaf.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1159
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1160
                        curNode = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1161
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1162
                    else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1163
                        lastNodeSpliterator.forEachRemaining(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1164
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1165
                else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1166
                    while(tryAdvance(consumer)) { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1167
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1168
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1169
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1170
        private static final class OfDouble
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1171
                extends InternalNodeSpliterator<Double, Spliterator.OfDouble, Node.OfDouble, DoubleConsumer>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1172
                implements Spliterator.OfDouble {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1173
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1174
            OfDouble(Node.OfDouble cur) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1175
                super(cur);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1176
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1177
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1178
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1179
            public boolean tryAdvance(DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1180
                return internalTryAdvance(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1181
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1183
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1184
            protected boolean tryAdvance(Spliterator.OfDouble spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1185
                                         DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1186
                return spliterator.tryAdvance(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1187
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1188
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1189
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1190
            public void forEachRemaining(DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1191
                if (curNode == null)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1192
                    return;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1193
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1194
                if (tryAdvanceSpliterator == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1195
                    if (lastNodeSpliterator == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1196
                        Deque<Node.OfDouble> stack = initStack();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1197
                        Node.OfDouble leaf;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1198
                        while ((leaf = findNextLeafNode(stack)) != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1199
                            leaf.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1200
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1201
                        curNode = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1202
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1203
                    else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1204
                        lastNodeSpliterator.forEachRemaining(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1205
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1206
                else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1207
                    while(tryAdvance(consumer)) { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1208
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1209
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1210
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1211
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1212
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1213
     * Fixed-sized builder class for reference nodes
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1214
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1215
    private static final class FixedNodeBuilder<T>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1216
            extends ArrayNode<T>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1217
            implements Node.Builder<T> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1218
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1219
        FixedNodeBuilder(long size, IntFunction<T[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1220
            super(size, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1221
            assert size < MAX_ARRAY_SIZE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1222
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1223
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1224
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1225
        public Node<T> build() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1226
            if (curSize < array.length)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1227
                throw new IllegalStateException(String.format("Current size %d is less than fixed size %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1228
                                                              curSize, array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1229
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1230
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1231
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1232
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1233
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1234
            if (size != array.length)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1235
                throw new IllegalStateException(String.format("Begin size %d is not equal to fixed size %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1236
                                                              size, array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1237
            curSize = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1238
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1239
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1240
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1241
        public void accept(T t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1242
            if (curSize < array.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1243
                array[curSize++] = t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1244
            } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1245
                throw new IllegalStateException(String.format("Accept exceeded fixed size of %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1246
                                                              array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1247
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1248
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1249
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1250
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1251
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1252
            if (curSize < array.length)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1253
                throw new IllegalStateException(String.format("End size %d is less than fixed size %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1254
                                                              curSize, array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1255
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1256
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1257
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1258
        public String toString() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1259
            return String.format("FixedNodeBuilder[%d][%s]",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1260
                                 array.length - curSize, Arrays.toString(array));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1261
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1262
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1263
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1264
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1265
     * Variable-sized builder class for reference nodes
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1266
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1267
    private static final class SpinedNodeBuilder<T>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1268
            extends SpinedBuffer<T>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1269
            implements Node<T>, Node.Builder<T> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1270
        private boolean building = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1271
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1272
        SpinedNodeBuilder() {} // Avoid creation of special accessor
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1273
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1274
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1275
        public Spliterator<T> spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1276
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1277
            return super.spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1278
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1279
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1280
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1281
        public void forEach(Consumer<? super T> consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1282
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1283
            super.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1284
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1285
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1286
        //
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1287
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1288
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1289
            assert !building : "was already building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1290
            building = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1291
            clear();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1292
            ensureCapacity(size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1293
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1294
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1295
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1296
        public void accept(T t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1297
            assert building : "not building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1298
            super.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1299
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1300
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1301
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1302
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1303
            assert building : "was not building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1304
            building = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1305
            // @@@ check begin(size) and size
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1306
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1307
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1308
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1309
        public void copyInto(T[] array, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1310
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1311
            super.copyInto(array, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1312
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1313
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1314
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1315
        public T[] asArray(IntFunction<T[]> arrayFactory) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1316
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1317
            return super.asArray(arrayFactory);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1318
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1319
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1320
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1321
        public Node<T> build() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1322
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1323
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1324
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1325
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1326
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1327
    //
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1328
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1329
    private static final int[] EMPTY_INT_ARRAY = new int[0];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1330
    private static final long[] EMPTY_LONG_ARRAY = new long[0];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1331
    private static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1332
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1333
    private abstract static class AbstractPrimitiveConcNode<E, N extends Node<E>>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1334
            implements Node<E> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1335
        final N left;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1336
        final N right;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1337
        final long size;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1338
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1339
        AbstractPrimitiveConcNode(N left, N right) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1340
            this.left = left;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1341
            this.right = right;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1342
            // The Node count will be required when the Node spliterator is
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1343
            // obtained and it is cheaper to aggressively calculate bottom up as
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1344
            // the tree is built rather than later on by traversing the tree
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1345
            this.size = left.count() + right.count();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1346
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1347
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1348
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1349
        public int getChildCount() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1350
            return 2;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1351
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1352
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1353
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1354
        public N getChild(int i) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1355
            if (i == 0) return left;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1356
            if (i == 1) return right;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1357
            throw new IndexOutOfBoundsException();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1358
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1359
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1360
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1361
        public long count() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1362
            return size;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1363
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1364
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1365
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1366
        public String toString() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1367
            if (count() < 32)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1368
                return String.format("%s[%s.%s]", this.getClass().getName(), left, right);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1369
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1370
                return String.format("%s[size=%d]", this.getClass().getName(), count());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1371
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1372
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1373
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1374
    private static class IntArrayNode implements Node.OfInt {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1375
        final int[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1376
        int curSize;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1377
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1378
        IntArrayNode(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1379
            if (size >= MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1380
                throw new IllegalArgumentException("Stream size exceeds max array size");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1381
            this.array = new int[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1382
            this.curSize = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1383
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1384
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1385
        IntArrayNode(int[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1386
            this.array = array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1387
            this.curSize = array.length;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1388
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1389
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1390
        // Node
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1391
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1392
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1393
        public Spliterator.OfInt spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1394
            return Arrays.spliterator(array, 0, curSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1395
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1396
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1397
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1398
        public int[] asIntArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1399
            if (array.length == curSize) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1400
                return array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1401
            } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1402
                return Arrays.copyOf(array, curSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1403
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1404
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1405
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1406
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1407
        public void copyInto(int[] dest, int destOffset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1408
            System.arraycopy(array, 0, dest, destOffset, curSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1409
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1410
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1411
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1412
        public long count() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1413
            return curSize;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1414
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1415
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1416
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1417
        public void forEach(IntConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1418
            for (int i = 0; i < curSize; i++) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1419
                consumer.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1420
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1421
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1422
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1423
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1424
        public String toString() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1425
            return String.format("IntArrayNode[%d][%s]",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1426
                                 array.length - curSize, Arrays.toString(array));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1427
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1428
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1429
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1430
    private static class LongArrayNode implements Node.OfLong {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1431
        final long[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1432
        int curSize;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1433
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1434
        LongArrayNode(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1435
            if (size >= MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1436
                throw new IllegalArgumentException("Stream size exceeds max array size");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1437
            this.array = new long[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1438
            this.curSize = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1439
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1440
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1441
        LongArrayNode(long[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1442
            this.array = array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1443
            this.curSize = array.length;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1444
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1445
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1446
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1447
        public Spliterator.OfLong spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1448
            return Arrays.spliterator(array, 0, curSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1449
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1450
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1451
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1452
        public long[] asLongArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1453
            if (array.length == curSize) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1454
                return array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1455
            } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1456
                return Arrays.copyOf(array, curSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1457
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1458
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1459
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1460
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1461
        public void copyInto(long[] dest, int destOffset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1462
            System.arraycopy(array, 0, dest, destOffset, curSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1463
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1464
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1465
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1466
        public long count() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1467
            return curSize;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1468
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1469
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1470
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1471
        public void forEach(LongConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1472
            for (int i = 0; i < curSize; i++) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1473
                consumer.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1474
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1475
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1476
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1477
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1478
        public String toString() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1479
            return String.format("LongArrayNode[%d][%s]",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1480
                                 array.length - curSize, Arrays.toString(array));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1481
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1482
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1483
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1484
    private static class DoubleArrayNode implements Node.OfDouble {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1485
        final double[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1486
        int curSize;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1487
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1488
        DoubleArrayNode(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1489
            if (size >= MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1490
                throw new IllegalArgumentException("Stream size exceeds max array size");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1491
            this.array = new double[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1492
            this.curSize = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1493
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1494
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1495
        DoubleArrayNode(double[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1496
            this.array = array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1497
            this.curSize = array.length;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1498
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1499
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1500
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1501
        public Spliterator.OfDouble spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1502
            return Arrays.spliterator(array, 0, curSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1503
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1504
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1505
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1506
        public double[] asDoubleArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1507
            if (array.length == curSize) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1508
                return array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1509
            } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1510
                return Arrays.copyOf(array, curSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1511
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1512
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1513
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1514
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1515
        public void copyInto(double[] dest, int destOffset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1516
            System.arraycopy(array, 0, dest, destOffset, curSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1517
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1518
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1519
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1520
        public long count() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1521
            return curSize;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1522
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1523
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1524
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1525
        public void forEach(DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1526
            for (int i = 0; i < curSize; i++) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1527
                consumer.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1528
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1529
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1530
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1531
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1532
        public String toString() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1533
            return String.format("DoubleArrayNode[%d][%s]",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1534
                                 array.length - curSize, Arrays.toString(array));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1535
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1536
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1537
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1538
    static final class IntConcNode
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1539
            extends AbstractPrimitiveConcNode<Integer, Node.OfInt>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1540
            implements Node.OfInt {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1541
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1542
        IntConcNode(Node.OfInt left, Node.OfInt right) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1543
            super(left, right);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1544
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1545
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1546
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1547
        public void forEach(IntConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1548
            left.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1549
            right.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1550
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1551
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1552
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1553
        public Spliterator.OfInt spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1554
            return new InternalNodeSpliterator.OfInt(this);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1555
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1556
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1557
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1558
        public void copyInto(int[] array, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1559
            left.copyInto(array, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1560
            right.copyInto(array, offset + (int) left.count());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1561
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1562
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1563
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1564
        public int[] asIntArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1565
            int[] array = new int[(int) count()];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1566
            copyInto(array, 0);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1567
            return array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1568
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1569
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1570
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1571
    static final class LongConcNode
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1572
            extends AbstractPrimitiveConcNode<Long, Node.OfLong>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1573
            implements Node.OfLong {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1574
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1575
        LongConcNode(Node.OfLong left, Node.OfLong right) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1576
            super(left, right);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1577
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1578
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1579
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1580
        public void forEach(LongConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1581
            left.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1582
            right.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1583
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1584
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1585
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1586
        public Spliterator.OfLong spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1587
            return new InternalNodeSpliterator.OfLong(this);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1588
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1589
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1590
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1591
        public void copyInto(long[] array, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1592
            left.copyInto(array, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1593
            right.copyInto(array, offset + (int) left.count());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1594
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1595
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1596
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1597
        public long[] asLongArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1598
            long[] array = new long[(int) count()];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1599
            copyInto(array, 0);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1600
            return array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1601
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1602
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1603
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1604
    static final class DoubleConcNode
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1605
            extends AbstractPrimitiveConcNode<Double, Node.OfDouble>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1606
            implements Node.OfDouble {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1607
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1608
        DoubleConcNode(Node.OfDouble left, Node.OfDouble right) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1609
            super(left, right);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1610
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1611
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1612
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1613
        public void forEach(DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1614
            left.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1615
            right.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1616
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1617
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1618
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1619
        public Spliterator.OfDouble spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1620
            return new InternalNodeSpliterator.OfDouble(this);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1621
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1622
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1623
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1624
        public void copyInto(double[] array, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1625
            left.copyInto(array, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1626
            right.copyInto(array, offset + (int) left.count());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1627
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1628
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1629
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1630
        public double[] asDoubleArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1631
            double[] array = new double[(int) count()];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1632
            copyInto(array, 0);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1633
            return array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1634
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1635
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1636
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1637
    private static final class IntFixedNodeBuilder
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1638
            extends IntArrayNode
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1639
            implements Node.Builder.OfInt {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1640
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1641
        IntFixedNodeBuilder(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1642
            super(size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1643
            assert size < MAX_ARRAY_SIZE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1644
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1645
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1646
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1647
        public Node.OfInt build() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1648
            if (curSize < array.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1649
                throw new IllegalStateException(String.format("Current size %d is less than fixed size %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1650
                                                              curSize, array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1651
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1652
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1653
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1654
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1655
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1656
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1657
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1658
            if (size != array.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1659
                throw new IllegalStateException(String.format("Begin size %d is not equal to fixed size %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1660
                                                              size, array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1661
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1662
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1663
            curSize = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1664
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1665
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1666
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1667
        public void accept(int i) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1668
            if (curSize < array.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1669
                array[curSize++] = i;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1670
            } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1671
                throw new IllegalStateException(String.format("Accept exceeded fixed size of %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1672
                                                              array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1673
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1674
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1675
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1676
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1677
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1678
            if (curSize < array.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1679
                throw new IllegalStateException(String.format("End size %d is less than fixed size %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1680
                                                              curSize, array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1681
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1682
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1683
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1684
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1685
        public String toString() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1686
            return String.format("IntFixedNodeBuilder[%d][%s]",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1687
                                 array.length - curSize, Arrays.toString(array));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1688
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1689
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1690
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1691
    private static final class LongFixedNodeBuilder
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1692
            extends LongArrayNode
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1693
            implements Node.Builder.OfLong {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1694
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1695
        LongFixedNodeBuilder(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1696
            super(size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1697
            assert size < MAX_ARRAY_SIZE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1698
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1699
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1700
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1701
        public Node.OfLong build() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1702
            if (curSize < array.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1703
                throw new IllegalStateException(String.format("Current size %d is less than fixed size %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1704
                                                              curSize, array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1705
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1706
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1707
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1708
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1709
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1710
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1711
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1712
            if (size != array.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1713
                throw new IllegalStateException(String.format("Begin size %d is not equal to fixed size %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1714
                                                              size, array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1715
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1716
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1717
            curSize = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1718
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1719
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1720
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1721
        public void accept(long i) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1722
            if (curSize < array.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1723
                array[curSize++] = i;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1724
            } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1725
                throw new IllegalStateException(String.format("Accept exceeded fixed size of %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1726
                                                              array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1727
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1728
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1729
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1730
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1731
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1732
            if (curSize < array.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1733
                throw new IllegalStateException(String.format("End size %d is less than fixed size %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1734
                                                              curSize, array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1735
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1736
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1737
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1738
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1739
        public String toString() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1740
            return String.format("LongFixedNodeBuilder[%d][%s]",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1741
                                 array.length - curSize, Arrays.toString(array));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1742
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1743
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1744
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1745
    private static final class DoubleFixedNodeBuilder
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1746
            extends DoubleArrayNode
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1747
            implements Node.Builder.OfDouble {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1748
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1749
        DoubleFixedNodeBuilder(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1750
            super(size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1751
            assert size < MAX_ARRAY_SIZE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1752
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1753
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1754
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1755
        public Node.OfDouble build() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1756
            if (curSize < array.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1757
                throw new IllegalStateException(String.format("Current size %d is less than fixed size %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1758
                                                              curSize, array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1759
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1760
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1761
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1762
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1763
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1764
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1765
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1766
            if (size != array.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1767
                throw new IllegalStateException(String.format("Begin size %d is not equal to fixed size %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1768
                                                              size, array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1769
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1770
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1771
            curSize = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1772
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1773
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1774
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1775
        public void accept(double i) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1776
            if (curSize < array.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1777
                array[curSize++] = i;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1778
            } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1779
                throw new IllegalStateException(String.format("Accept exceeded fixed size of %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1780
                                                              array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1781
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1782
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1783
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1784
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1785
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1786
            if (curSize < array.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1787
                throw new IllegalStateException(String.format("End size %d is less than fixed size %d",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1788
                                                              curSize, array.length));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1789
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1790
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1791
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1792
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1793
        public String toString() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1794
            return String.format("DoubleFixedNodeBuilder[%d][%s]",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1795
                                 array.length - curSize, Arrays.toString(array));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1796
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1797
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1798
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1799
    private static final class IntSpinedNodeBuilder
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1800
            extends SpinedBuffer.OfInt
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1801
            implements Node.OfInt, Node.Builder.OfInt {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1802
        private boolean building = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1803
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1804
        IntSpinedNodeBuilder() {} // Avoid creation of special accessor
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1805
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1806
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1807
        public Spliterator.OfInt spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1808
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1809
            return super.spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1810
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1811
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1812
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1813
        public void forEach(IntConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1814
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1815
            super.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1816
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1817
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1818
        //
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1819
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1820
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1821
            assert !building : "was already building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1822
            building = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1823
            clear();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1824
            ensureCapacity(size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1825
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1826
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1827
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1828
        public void accept(int i) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1829
            assert building : "not building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1830
            super.accept(i);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1831
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1832
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1833
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1834
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1835
            assert building : "was not building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1836
            building = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1837
            // @@@ check begin(size) and size
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1838
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1839
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1840
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1841
        public void copyInto(int[] array, int offset) throws IndexOutOfBoundsException {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1842
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1843
            super.copyInto(array, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1844
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1845
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1846
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1847
        public int[] asIntArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1848
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1849
            return super.asIntArray();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1850
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1851
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1852
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1853
        public Node.OfInt build() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1854
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1855
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1856
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1857
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1858
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1859
    private static final class LongSpinedNodeBuilder
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1860
            extends SpinedBuffer.OfLong
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1861
            implements Node.OfLong, Node.Builder.OfLong {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1862
        private boolean building = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1863
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1864
        LongSpinedNodeBuilder() {} // Avoid creation of special accessor
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1865
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1866
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1867
        public Spliterator.OfLong spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1868
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1869
            return super.spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1870
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1871
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1872
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1873
        public void forEach(LongConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1874
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1875
            super.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1876
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1877
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1878
        //
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1879
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1880
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1881
            assert !building : "was already building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1882
            building = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1883
            clear();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1884
            ensureCapacity(size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1885
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1886
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1887
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1888
        public void accept(long i) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1889
            assert building : "not building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1890
            super.accept(i);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1891
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1892
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1893
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1894
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1895
            assert building : "was not building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1896
            building = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1897
            // @@@ check begin(size) and size
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1898
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1899
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1900
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1901
        public void copyInto(long[] array, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1902
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1903
            super.copyInto(array, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1904
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1905
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1906
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1907
        public long[] asLongArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1908
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1909
            return super.asLongArray();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1910
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1911
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1912
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1913
        public Node.OfLong build() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1914
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1915
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1916
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1917
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1918
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1919
    private static final class DoubleSpinedNodeBuilder
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1920
            extends SpinedBuffer.OfDouble
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1921
            implements Node.OfDouble, Node.Builder.OfDouble {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1922
        private boolean building = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1923
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1924
        DoubleSpinedNodeBuilder() {} // Avoid creation of special accessor
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1925
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1926
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1927
        public Spliterator.OfDouble spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1928
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1929
            return super.spliterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1930
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1931
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1932
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1933
        public void forEach(DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1934
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1935
            super.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1936
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1937
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1938
        //
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1939
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1940
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1941
            assert !building : "was already building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1942
            building = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1943
            clear();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1944
            ensureCapacity(size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1945
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1946
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1947
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1948
        public void accept(double i) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1949
            assert building : "not building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1950
            super.accept(i);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1951
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1952
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1953
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1954
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1955
            assert building : "was not building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1956
            building = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1957
            // @@@ check begin(size) and size
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1958
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1959
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1960
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1961
        public void copyInto(double[] array, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1962
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1963
            super.copyInto(array, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1964
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1965
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1966
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1967
        public double[] asDoubleArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1968
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1969
            return super.asDoubleArray();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1970
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1971
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1972
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1973
        public Node.OfDouble build() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1974
            assert !building : "during building";
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1975
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1976
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1977
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1978
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1979
    private static abstract class SizedCollectorTask<P_IN, P_OUT, T_SINK extends Sink<P_OUT>,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1980
                                                     K extends SizedCollectorTask<P_IN, P_OUT, T_SINK, K>>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1981
            extends CountedCompleter<Void>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1982
            implements Sink<P_OUT> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1983
        protected final Spliterator<P_IN> spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1984
        protected final PipelineHelper<P_OUT> helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1985
        protected final long targetSize;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1986
        protected long offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1987
        protected long length;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1988
        // For Sink implementation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1989
        protected int index, fence;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1990
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1991
        SizedCollectorTask(Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1992
                           PipelineHelper<P_OUT> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1993
                           int arrayLength) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1994
            assert spliterator.hasCharacteristics(Spliterator.SUBSIZED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1995
            this.spliterator = spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1996
            this.helper = helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1997
            this.targetSize = AbstractTask.suggestTargetSize(spliterator.estimateSize());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1998
            this.offset = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1999
            this.length = arrayLength;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2000
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2001
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2002
        SizedCollectorTask(K parent, Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2003
                           long offset, long length, int arrayLength) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2004
            super(parent);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2005
            assert spliterator.hasCharacteristics(Spliterator.SUBSIZED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2006
            this.spliterator = spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2007
            this.helper = parent.helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2008
            this.targetSize = parent.targetSize;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2009
            this.offset = offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2010
            this.length = length;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2011
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2012
            if (offset < 0 || length < 0 || (offset + length - 1 >= arrayLength)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2013
                throw new IllegalArgumentException(
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2014
                        String.format("offset and length interval [%d, %d + %d) is not within array size interval [0, %d)",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2015
                                      offset, offset, length, arrayLength));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2016
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2017
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2018
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2019
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2020
        public void compute() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2021
            SizedCollectorTask<P_IN, P_OUT, T_SINK, K> task = this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2022
            while (true) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2023
                Spliterator<P_IN> leftSplit;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2024
                if (!AbstractTask.suggestSplit(task.spliterator, task.targetSize)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2025
                    || ((leftSplit = task.spliterator.trySplit()) == null)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2026
                    if (task.offset + task.length >= MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2027
                        throw new IllegalArgumentException("Stream size exceeds max array size");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2028
                    T_SINK sink = (T_SINK) task;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2029
                    task.helper.wrapAndCopyInto(sink, task.spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2030
                    task.propagateCompletion();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2031
                    return;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2032
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2033
                else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2034
                    task.setPendingCount(1);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2035
                    long leftSplitSize = leftSplit.estimateSize();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2036
                    task.makeChild(leftSplit, task.offset, leftSplitSize).fork();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2037
                    task = task.makeChild(task.spliterator, task.offset + leftSplitSize,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2038
                                          task.length - leftSplitSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2039
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2040
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2041
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2042
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2043
        abstract K makeChild(Spliterator<P_IN> spliterator, long offset, long size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2044
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2045
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2046
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2047
            if(size > length)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2048
                throw new IllegalStateException("size passed to Sink.begin exceeds array length");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2049
            index = (int) offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2050
            fence = (int) offset + (int) length;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2051
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2052
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2053
        static final class OfRef<P_IN, P_OUT>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2054
                extends SizedCollectorTask<P_IN, P_OUT, Sink<P_OUT>, OfRef<P_IN, P_OUT>>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2055
                implements Sink<P_OUT> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2056
            private final P_OUT[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2057
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2058
            OfRef(Spliterator<P_IN> spliterator, PipelineHelper<P_OUT> helper, P_OUT[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2059
                super(spliterator, helper, array.length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2060
                this.array = array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2061
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2062
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2063
            OfRef(OfRef<P_IN, P_OUT> parent, Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2064
                  long offset, long length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2065
                super(parent, spliterator, offset, length, parent.array.length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2066
                this.array = parent.array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2067
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2068
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2069
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2070
            OfRef<P_IN, P_OUT> makeChild(Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2071
                                         long offset, long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2072
                return new OfRef<>(this, spliterator, offset, size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2073
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2074
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2075
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2076
            public void accept(P_OUT value) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2077
                if (index >= fence) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2078
                    throw new IndexOutOfBoundsException(Integer.toString(index));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2079
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2080
                array[index++] = value;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2081
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2082
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2083
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2084
        static final class OfInt<P_IN>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2085
                extends SizedCollectorTask<P_IN, Integer, Sink.OfInt, OfInt<P_IN>>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2086
                implements Sink.OfInt {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2087
            private final int[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2088
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2089
            OfInt(Spliterator<P_IN> spliterator, PipelineHelper<Integer> helper, int[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2090
                super(spliterator, helper, array.length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2091
                this.array = array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2092
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2093
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2094
            OfInt(SizedCollectorTask.OfInt<P_IN> parent, Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2095
                  long offset, long length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2096
                super(parent, spliterator, offset, length, parent.array.length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2097
                this.array = parent.array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2098
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2099
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2100
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2101
            SizedCollectorTask.OfInt<P_IN> makeChild(Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2102
                                                     long offset, long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2103
                return new SizedCollectorTask.OfInt<>(this, spliterator, offset, size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2104
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2105
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2106
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2107
            public void accept(int value) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2108
                if (index >= fence) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2109
                    throw new IndexOutOfBoundsException(Integer.toString(index));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2110
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2111
                array[index++] = value;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2112
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2113
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2114
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2115
        static final class OfLong<P_IN>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2116
                extends SizedCollectorTask<P_IN, Long, Sink.OfLong, OfLong<P_IN>>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2117
                implements Sink.OfLong {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2118
            private final long[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2119
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2120
            OfLong(Spliterator<P_IN> spliterator, PipelineHelper<Long> helper, long[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2121
                super(spliterator, helper, array.length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2122
                this.array = array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2123
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2124
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2125
            OfLong(SizedCollectorTask.OfLong<P_IN> parent, Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2126
                   long offset, long length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2127
                super(parent, spliterator, offset, length, parent.array.length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2128
                this.array = parent.array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2129
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2130
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2131
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2132
            SizedCollectorTask.OfLong<P_IN> makeChild(Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2133
                                                      long offset, long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2134
                return new SizedCollectorTask.OfLong<>(this, spliterator, offset, size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2135
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2136
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2137
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2138
            public void accept(long value) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2139
                if (index >= fence) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2140
                    throw new IndexOutOfBoundsException(Integer.toString(index));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2141
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2142
                array[index++] = value;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2143
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2144
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2145
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2146
        static final class OfDouble<P_IN>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2147
                extends SizedCollectorTask<P_IN, Double, Sink.OfDouble, OfDouble<P_IN>>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2148
                implements Sink.OfDouble {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2149
            private final double[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2150
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2151
            OfDouble(Spliterator<P_IN> spliterator, PipelineHelper<Double> helper, double[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2152
                super(spliterator, helper, array.length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2153
                this.array = array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2154
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2155
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2156
            OfDouble(SizedCollectorTask.OfDouble<P_IN> parent, Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2157
                     long offset, long length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2158
                super(parent, spliterator, offset, length, parent.array.length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2159
                this.array = parent.array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2160
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2161
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2162
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2163
            SizedCollectorTask.OfDouble<P_IN> makeChild(Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2164
                                                        long offset, long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2165
                return new SizedCollectorTask.OfDouble<>(this, spliterator, offset, size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2166
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2167
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2168
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2169
            public void accept(double value) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2170
                if (index >= fence) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2171
                    throw new IndexOutOfBoundsException(Integer.toString(index));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2172
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2173
                array[index++] = value;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2174
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2175
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2176
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2177
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2178
    private static abstract class ToArrayTask<T, T_NODE extends Node<T>,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2179
                                              K extends ToArrayTask<T, T_NODE, K>>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2180
            extends CountedCompleter<Void> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2181
        protected final T_NODE node;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2182
        protected final int offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2183
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2184
        ToArrayTask(T_NODE node, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2185
            this.node = node;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2186
            this.offset = offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2187
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2188
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2189
        ToArrayTask(K parent, T_NODE node, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2190
            super(parent);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2191
            this.node = node;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2192
            this.offset = offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2193
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2194
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2195
        abstract void copyNodeToArray();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2196
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2197
        abstract K makeChild(int childIndex, int offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2198
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2199
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2200
        public void compute() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2201
            ToArrayTask<T, T_NODE, K> task = this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2202
            while (true) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2203
                if (task.node.getChildCount() == 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2204
                    task.copyNodeToArray();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2205
                    task.propagateCompletion();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2206
                    return;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2207
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2208
                else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2209
                    task.setPendingCount(task.node.getChildCount() - 1);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2210
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2211
                    int size = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2212
                    int i = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2213
                    for (;i < task.node.getChildCount() - 1; i++) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2214
                        K leftTask = task.makeChild(i, task.offset + size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2215
                        size += leftTask.node.count();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2216
                        leftTask.fork();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2217
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2218
                    task = task.makeChild(i, task.offset + size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2219
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2220
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2221
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2222
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2223
        private static final class OfRef<T>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2224
                extends ToArrayTask<T, Node<T>, OfRef<T>> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2225
            private final T[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2226
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2227
            private OfRef(Node<T> node, T[] array, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2228
                super(node, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2229
                this.array = array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2230
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2231
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2232
            private OfRef(OfRef<T> parent, Node<T> node, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2233
                super(parent, node, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2234
                this.array = parent.array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2235
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2236
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2237
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2238
            OfRef<T> makeChild(int childIndex, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2239
                return new OfRef<>(this, node.getChild(childIndex), offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2240
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2241
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2242
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2243
            void copyNodeToArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2244
                node.copyInto(array, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2245
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2246
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2247
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2248
        private static final class OfInt
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2249
                extends ToArrayTask<Integer, Node.OfInt, OfInt> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2250
            private final int[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2251
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2252
            private OfInt(Node.OfInt node, int[] array, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2253
                super(node, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2254
                this.array = array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2255
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2256
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2257
            private OfInt(OfInt parent, Node.OfInt node, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2258
                super(parent, node, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2259
                this.array = parent.array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2260
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2261
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2262
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2263
            OfInt makeChild(int childIndex, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2264
                return new OfInt(this, node.getChild(childIndex), offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2265
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2266
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2267
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2268
            void copyNodeToArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2269
                node.copyInto(array, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2270
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2271
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2272
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2273
        private static final class OfLong
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2274
                extends ToArrayTask<Long, Node.OfLong, OfLong> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2275
            private final long[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2276
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2277
            private OfLong(Node.OfLong node, long[] array, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2278
                super(node, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2279
                this.array = array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2280
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2281
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2282
            private OfLong(OfLong parent, Node.OfLong node, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2283
                super(parent, node, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2284
                this.array = parent.array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2285
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2286
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2287
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2288
            OfLong makeChild(int childIndex, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2289
                return new OfLong(this, node.getChild(childIndex), offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2290
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2291
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2292
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2293
            void copyNodeToArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2294
                node.copyInto(array, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2295
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2296
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2297
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2298
        private static final class OfDouble
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2299
                extends ToArrayTask<Double, Node.OfDouble, OfDouble> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2300
            private final double[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2301
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2302
            private OfDouble(Node.OfDouble node, double[] array, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2303
                super(node, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2304
                this.array = array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2305
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2306
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2307
            private OfDouble(OfDouble parent, Node.OfDouble node, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2308
                super(parent, node, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2309
                this.array = parent.array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2310
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2311
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2312
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2313
            OfDouble makeChild(int childIndex, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2314
                return new OfDouble(this, node.getChild(childIndex), offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2315
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2316
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2317
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2318
            void copyNodeToArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2319
                node.copyInto(array, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2320
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2321
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2322
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2323
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2324
    private static final class CollectorTask<P_IN, P_OUT>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2325
            extends AbstractTask<P_IN, P_OUT, Node<P_OUT>, CollectorTask<P_IN, P_OUT>> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2326
        private final PipelineHelper<P_OUT> helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2327
        private final IntFunction<P_OUT[]> generator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2328
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2329
        CollectorTask(PipelineHelper<P_OUT> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2330
                      IntFunction<P_OUT[]> generator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2331
                      Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2332
            super(helper, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2333
            this.helper = helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2334
            this.generator = generator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2335
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2336
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2337
        CollectorTask(CollectorTask<P_IN, P_OUT> parent, Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2338
            super(parent, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2339
            helper = parent.helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2340
            generator = parent.generator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2341
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2342
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2343
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2344
        protected CollectorTask<P_IN, P_OUT> makeChild(Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2345
            return new CollectorTask<>(this, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2346
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2347
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2348
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2349
        protected Node<P_OUT> doLeaf() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2350
            Node.Builder<P_OUT> builder
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2351
                    = builder(helper.exactOutputSizeIfKnown(spliterator),
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2352
                                    generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2353
            return helper.wrapAndCopyInto(builder, spliterator).build();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2354
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2355
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2356
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2357
        public void onCompletion(CountedCompleter caller) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2358
            if (!isLeaf()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2359
                setLocalResult(new ConcNode<>(leftChild.getLocalResult(), rightChild.getLocalResult()));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2360
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2361
            super.onCompletion(caller);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2362
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2363
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2364
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2365
    private static final class IntCollectorTask<P_IN>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2366
            extends AbstractTask<P_IN, Integer, Node.OfInt, IntCollectorTask<P_IN>> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2367
        private final PipelineHelper<Integer> helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2368
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2369
        IntCollectorTask(PipelineHelper<Integer> helper, Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2370
            super(helper, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2371
            this.helper = helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2372
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2373
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2374
        IntCollectorTask(IntCollectorTask<P_IN> parent, Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2375
            super(parent, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2376
            helper = parent.helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2377
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2378
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2379
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2380
        protected IntCollectorTask<P_IN> makeChild(Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2381
            return new IntCollectorTask<>(this, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2382
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2383
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2384
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2385
        protected Node.OfInt doLeaf() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2386
            Node.Builder.OfInt builder = intBuilder(helper.exactOutputSizeIfKnown(spliterator));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2387
            return helper.wrapAndCopyInto(builder, spliterator).build();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2388
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2389
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2390
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2391
        public void onCompletion(CountedCompleter caller) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2392
            if (!isLeaf()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2393
                setLocalResult(new IntConcNode(leftChild.getLocalResult(), rightChild.getLocalResult()));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2394
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2395
            super.onCompletion(caller);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2396
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2397
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2398
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2399
    private static final class LongCollectorTask<P_IN>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2400
            extends AbstractTask<P_IN, Long, Node.OfLong, LongCollectorTask<P_IN>> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2401
        private final PipelineHelper<Long> helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2402
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2403
        LongCollectorTask(PipelineHelper<Long> helper, Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2404
            super(helper, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2405
            this.helper = helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2406
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2407
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2408
        LongCollectorTask(LongCollectorTask<P_IN> parent, Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2409
            super(parent, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2410
            helper = parent.helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2411
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2412
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2413
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2414
        protected LongCollectorTask<P_IN> makeChild(Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2415
            return new LongCollectorTask<>(this, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2416
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2417
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2418
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2419
        protected Node.OfLong doLeaf() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2420
            Node.Builder.OfLong builder = longBuilder(helper.exactOutputSizeIfKnown(spliterator));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2421
            return helper.wrapAndCopyInto(builder, spliterator).build();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2422
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2423
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2424
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2425
        public void onCompletion(CountedCompleter caller) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2426
            if (!isLeaf()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2427
                setLocalResult(new LongConcNode(leftChild.getLocalResult(), rightChild.getLocalResult()));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2428
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2429
            super.onCompletion(caller);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2430
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2431
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2432
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2433
    private static final class DoubleCollectorTask<P_IN>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2434
            extends AbstractTask<P_IN, Double, Node.OfDouble, DoubleCollectorTask<P_IN>> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2435
        private final PipelineHelper<Double> helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2436
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2437
        DoubleCollectorTask(PipelineHelper<Double> helper, Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2438
            super(helper, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2439
            this.helper = helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2440
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2441
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2442
        DoubleCollectorTask(DoubleCollectorTask<P_IN> parent, Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2443
            super(parent, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2444
            helper = parent.helper;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2445
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2446
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2447
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2448
        protected DoubleCollectorTask<P_IN> makeChild(Spliterator<P_IN> spliterator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2449
            return new DoubleCollectorTask<>(this, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2450
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2451
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2452
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2453
        protected Node.OfDouble doLeaf() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2454
            Node.Builder.OfDouble builder
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2455
                    = doubleBuilder(helper.exactOutputSizeIfKnown(spliterator));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2456
            return helper.wrapAndCopyInto(builder, spliterator).build();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2457
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2458
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2459
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2460
        public void onCompletion(CountedCompleter caller) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2461
            if (!isLeaf()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2462
                setLocalResult(new DoubleConcNode(leftChild.getLocalResult(), rightChild.getLocalResult()));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2463
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2464
            super.onCompletion(caller);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2465
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2466
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  2467
}