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