src/java.base/share/classes/java/util/stream/SpinedBuffer.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/java.base/share/classes/java/util/stream/SpinedBuffer.java@3317bb8137f4
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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.ArrayList;
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.Iterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    30
import java.util.List;
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18171
diff changeset
    31
import java.util.Objects;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    32
import java.util.PrimitiveIterator;
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.function.Consumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    36
import java.util.function.DoubleConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    37
import java.util.function.IntConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    38
import java.util.function.IntFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    39
import java.util.function.LongConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    40
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    41
/**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    42
 * An ordered collection of elements.  Elements can be added, but not removed.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    43
 * Goes through a building phase, during which elements can be added, and a
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    44
 * traversal phase, during which elements can be traversed in order but no
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    45
 * further modifications are possible.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    46
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    47
 * <p> One or more arrays are used to store elements. The use of a multiple
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    48
 * arrays has better performance characteristics than a single array used by
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    49
 * {@link ArrayList}, as when the capacity of the list needs to be increased
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    50
 * no copying of elements is required.  This is usually beneficial in the case
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    51
 * where the results will be traversed a small number of times.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    52
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    53
 * @param <E> the type of elements in this list
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    54
 * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    55
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    56
class SpinedBuffer<E>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    57
        extends AbstractSpinedBuffer
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    58
        implements Consumer<E>, Iterable<E> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    59
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    60
    /*
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    61
     * We optimistically hope that all the data will fit into the first chunk,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    62
     * so we try to avoid inflating the spine[] and priorElementCount[] arrays
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    63
     * prematurely.  So methods must be prepared to deal with these arrays being
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    64
     * null.  If spine is non-null, then spineIndex points to the current chunk
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    65
     * within the spine, otherwise it is zero.  The spine and priorElementCount
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    66
     * arrays are always the same size, and for any i <= spineIndex,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    67
     * priorElementCount[i] is the sum of the sizes of all the prior chunks.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    68
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    69
     * The curChunk pointer is always valid.  The elementIndex is the index of
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    70
     * the next element to be written in curChunk; this may be past the end of
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    71
     * curChunk so we have to check before writing. When we inflate the spine
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
     * array, curChunk becomes the first element in it.  When we clear the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    73
     * buffer, we discard all chunks except the first one, which we clear,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    74
     * restoring it to the initial single-chunk state.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    75
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    76
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    77
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    78
     * Chunk that we're currently writing into; may or may not be aliased with
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
     * the first element of the spine.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    81
    protected E[] curChunk;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    82
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    83
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    84
     * All chunks, or null if there is only one chunk.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    86
    protected E[][] spine;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    87
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    88
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    89
     * Constructs an empty list with the specified initial capacity.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    90
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    91
     * @param  initialCapacity  the initial capacity of the list
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    92
     * @throws IllegalArgumentException if the specified initial capacity
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    93
     *         is negative
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    94
     */
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 21339
diff changeset
    95
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    96
    SpinedBuffer(int initialCapacity) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    97
        super(initialCapacity);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    98
        curChunk = (E[]) new Object[1 << initialChunkPower];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    99
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   100
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   102
     * Constructs an empty list with an initial capacity of sixteen.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   103
     */
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 21339
diff changeset
   104
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   105
    SpinedBuffer() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   106
        super();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   107
        curChunk = (E[]) new Object[1 << initialChunkPower];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   108
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   109
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   110
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   111
     * Returns the current capacity of the buffer
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   112
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   113
    protected long capacity() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   114
        return (spineIndex == 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   115
               ? curChunk.length
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   116
               : priorElementCount[spineIndex] + spine[spineIndex].length;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   117
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   118
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 21339
diff changeset
   119
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   120
    private void inflateSpine() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
        if (spine == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
            spine = (E[][]) new Object[MIN_SPINE_SIZE][];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
            priorElementCount = new long[MIN_SPINE_SIZE];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
            spine[0] = curChunk;
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   129
     * Ensure that the buffer has at least capacity to hold the target size
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   130
     */
22297
1c62c67d9dd2 8031373: Lint warnings in java.util.stream
briangoetz
parents: 21339
diff changeset
   131
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   132
    protected final void ensureCapacity(long targetSize) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   133
        long capacity = capacity();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   134
        if (targetSize > capacity) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   135
            inflateSpine();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   136
            for (int i=spineIndex+1; targetSize > capacity; i++) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   137
                if (i >= spine.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   138
                    int newSpineSize = spine.length * 2;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   139
                    spine = Arrays.copyOf(spine, newSpineSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   140
                    priorElementCount = Arrays.copyOf(priorElementCount, newSpineSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   141
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   142
                int nextChunkSize = chunkSize(i);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   143
                spine[i] = (E[]) new Object[nextChunkSize];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   144
                priorElementCount[i] = priorElementCount[i-1] + spine[i-1].length;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   145
                capacity += nextChunkSize;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   146
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   147
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   148
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   149
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   150
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   151
     * Force the buffer to increase its capacity.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   152
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   153
    protected void increaseCapacity() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   154
        ensureCapacity(capacity() + 1);
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   158
     * Retrieve the element at the specified index.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   159
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   160
    public E get(long index) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   161
        // @@@ can further optimize by caching last seen spineIndex,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   162
        // which is going to be right most of the time
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   163
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   164
        // Casts to int are safe since the spine array index is the index minus
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   165
        // the prior element count from the current spine
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   166
        if (spineIndex == 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   167
            if (index < elementIndex)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   168
                return curChunk[((int) index)];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   169
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   170
                throw new IndexOutOfBoundsException(Long.toString(index));
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
        if (index >= count())
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   174
            throw new IndexOutOfBoundsException(Long.toString(index));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   175
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   176
        for (int j=0; j <= spineIndex; j++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   177
            if (index < priorElementCount[j] + spine[j].length)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   178
                return spine[j][((int) (index - priorElementCount[j]))];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   179
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   180
        throw new IndexOutOfBoundsException(Long.toString(index));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   181
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   183
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   184
     * Copy the elements, starting at the specified offset, into the specified
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   185
     * array.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   186
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   187
    public void copyInto(E[] array, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   188
        long finalOffset = offset + count();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   189
        if (finalOffset > array.length || finalOffset < offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   190
            throw new IndexOutOfBoundsException("does not fit");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   191
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   192
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   193
        if (spineIndex == 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   194
            System.arraycopy(curChunk, 0, array, offset, elementIndex);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   195
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   196
            // full chunks
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   197
            for (int i=0; i < spineIndex; i++) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   198
                System.arraycopy(spine[i], 0, array, offset, spine[i].length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   199
                offset += spine[i].length;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   200
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   201
            if (elementIndex > 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   202
                System.arraycopy(curChunk, 0, array, offset, elementIndex);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   203
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   204
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   205
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   206
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   207
     * Create a new array using the specified array factory, and copy the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   208
     * elements into it.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   209
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   210
    public E[] asArray(IntFunction<E[]> arrayFactory) {
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   211
        long size = count();
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   212
        if (size >= Nodes.MAX_ARRAY_SIZE)
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   213
            throw new IllegalArgumentException(Nodes.BAD_SIZE);
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   214
        E[] result = arrayFactory.apply((int) size);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   215
        copyInto(result, 0);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   216
        return result;
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   220
    public void clear() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   221
        if (spine != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   222
            curChunk = spine[0];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   223
            for (int i=0; i<curChunk.length; i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   224
                curChunk[i] = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   225
            spine = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   226
            priorElementCount = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   227
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   228
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   229
            for (int i=0; i<elementIndex; i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   230
                curChunk[i] = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   231
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   232
        elementIndex = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   233
        spineIndex = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   234
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   235
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   236
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   237
    public Iterator<E> iterator() {
18155
889970e5b728 8015792: Rename Spliterators.spliteratorFromIterator to Spliterators.iterator
psandoz
parents: 17182
diff changeset
   238
        return Spliterators.iterator(spliterator());
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   239
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   240
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   241
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   242
    public void forEach(Consumer<? super E> consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   243
        // completed chunks, if any
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   244
        for (int j = 0; j < spineIndex; j++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   245
            for (E t : spine[j])
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   246
                consumer.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   247
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   248
        // current chunk
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   249
        for (int i=0; i<elementIndex; i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   250
            consumer.accept(curChunk[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   251
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   252
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   253
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   254
    public void accept(E e) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   255
        if (elementIndex == curChunk.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   256
            inflateSpine();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   257
            if (spineIndex+1 >= spine.length || spine[spineIndex+1] == null)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   258
                increaseCapacity();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   259
            elementIndex = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   260
            ++spineIndex;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   261
            curChunk = spine[spineIndex];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   262
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   263
        curChunk[elementIndex++] = e;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   264
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   265
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   266
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   267
    public String toString() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   268
        List<E> list = new ArrayList<>();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   269
        forEach(list::add);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   270
        return "SpinedBuffer:" + list.toString();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   271
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   272
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   273
    private static final int SPLITERATOR_CHARACTERISTICS
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   274
            = Spliterator.SIZED | Spliterator.ORDERED | Spliterator.SUBSIZED;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   275
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   276
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   277
     * Return a {@link Spliterator} describing the contents of the buffer.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   278
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   279
    public Spliterator<E> spliterator() {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   280
        class Splitr implements Spliterator<E> {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   281
            // The current spine index
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   282
            int splSpineIndex;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   283
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   284
            // Last spine index
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   285
            final int lastSpineIndex;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   286
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   287
            // The current element index into the current spine
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   288
            int splElementIndex;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   289
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   290
            // Last spine's last element index + 1
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   291
            final int lastSpineElementFence;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   292
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   293
            // When splSpineIndex >= lastSpineIndex and
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   294
            // splElementIndex >= lastSpineElementFence then
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   295
            // this spliterator is fully traversed
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   296
            // tryAdvance can set splSpineIndex > spineIndex if the last spine is full
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   297
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   298
            // The current spine array
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   299
            E[] splChunk;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   300
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   301
            Splitr(int firstSpineIndex, int lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   302
                   int firstSpineElementIndex, int lastSpineElementFence) {
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   303
                this.splSpineIndex = firstSpineIndex;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   304
                this.lastSpineIndex = lastSpineIndex;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   305
                this.splElementIndex = firstSpineElementIndex;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   306
                this.lastSpineElementFence = lastSpineElementFence;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   307
                assert spine != null || firstSpineIndex == 0 && lastSpineIndex == 0;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   308
                splChunk = (spine == null) ? curChunk : spine[firstSpineIndex];
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   309
            }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   310
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   311
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   312
            public long estimateSize() {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   313
                return (splSpineIndex == lastSpineIndex)
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   314
                       ? (long) lastSpineElementFence - splElementIndex
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   315
                       : // # of elements prior to end -
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   316
                       priorElementCount[lastSpineIndex] + lastSpineElementFence -
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   317
                       // # of elements prior to current
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   318
                       priorElementCount[splSpineIndex] - splElementIndex;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   319
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   320
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   321
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   322
            public int characteristics() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   323
                return SPLITERATOR_CHARACTERISTICS;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   324
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   325
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   326
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   327
            public boolean tryAdvance(Consumer<? super E> consumer) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18171
diff changeset
   328
                Objects.requireNonNull(consumer);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18171
diff changeset
   329
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   330
                if (splSpineIndex < lastSpineIndex
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   331
                    || (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   332
                    consumer.accept(splChunk[splElementIndex++]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   333
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   334
                    if (splElementIndex == splChunk.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   335
                        splElementIndex = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   336
                        ++splSpineIndex;
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   337
                        if (spine != null && splSpineIndex <= lastSpineIndex)
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   338
                            splChunk = spine[splSpineIndex];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   339
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   340
                    return true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   341
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   342
                return false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   343
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   344
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   345
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   346
            public void forEachRemaining(Consumer<? super E> consumer) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18171
diff changeset
   347
                Objects.requireNonNull(consumer);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18171
diff changeset
   348
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   349
                if (splSpineIndex < lastSpineIndex
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   350
                    || (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   351
                    int i = splElementIndex;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   352
                    // completed chunks, if any
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   353
                    for (int sp = splSpineIndex; sp < lastSpineIndex; sp++) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   354
                        E[] chunk = spine[sp];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   355
                        for (; i < chunk.length; i++) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   356
                            consumer.accept(chunk[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   357
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   358
                        i = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   359
                    }
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   360
                    // last (or current uncompleted) chunk
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   361
                    E[] chunk = (splSpineIndex == lastSpineIndex) ? splChunk : spine[lastSpineIndex];
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   362
                    int hElementIndex = lastSpineElementFence;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   363
                    for (; i < hElementIndex; i++) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   364
                        consumer.accept(chunk[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   365
                    }
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   366
                    // mark consumed
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   367
                    splSpineIndex = lastSpineIndex;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   368
                    splElementIndex = lastSpineElementFence;
17182
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
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   373
            public Spliterator<E> trySplit() {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   374
                if (splSpineIndex < lastSpineIndex) {
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   375
                    // split just before last chunk (if it is full this means 50:50 split)
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   376
                    Spliterator<E> ret = new Splitr(splSpineIndex, lastSpineIndex - 1,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   377
                                                    splElementIndex, spine[lastSpineIndex-1].length);
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   378
                    // position to start of last chunk
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   379
                    splSpineIndex = lastSpineIndex;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   380
                    splElementIndex = 0;
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   381
                    splChunk = spine[splSpineIndex];
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   382
                    return ret;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   383
                }
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   384
                else if (splSpineIndex == lastSpineIndex) {
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   385
                    int t = (lastSpineElementFence - splElementIndex) / 2;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   386
                    if (t == 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   387
                        return null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   388
                    else {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   389
                        Spliterator<E> ret = Arrays.spliterator(splChunk, splElementIndex, splElementIndex + t);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   390
                        splElementIndex += t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   391
                        return ret;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   392
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   393
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   394
                else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   395
                    return null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   396
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   397
            }
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   398
        }
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   399
        return new Splitr(0, spineIndex, 0, elementIndex);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   400
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   401
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   402
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   403
     * An ordered collection of primitive values.  Elements can be added, but
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   404
     * not removed. Goes through a building phase, during which elements can be
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   405
     * added, and a traversal phase, during which elements can be traversed in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   406
     * order but no further modifications are possible.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   407
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   408
     * <p> One or more arrays are used to store elements. The use of a multiple
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   409
     * arrays has better performance characteristics than a single array used by
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   410
     * {@link ArrayList}, as when the capacity of the list needs to be increased
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   411
     * no copying of elements is required.  This is usually beneficial in the case
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   412
     * where the results will be traversed a small number of times.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   413
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   414
     * @param <E> the wrapper type for this primitive type
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   415
     * @param <T_ARR> the array type for this primitive type
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   416
     * @param <T_CONS> the Consumer type for this primitive type
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   417
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   418
    abstract static class OfPrimitive<E, T_ARR, T_CONS>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   419
            extends AbstractSpinedBuffer implements Iterable<E> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   420
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   421
        /*
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   422
         * We optimistically hope that all the data will fit into the first chunk,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   423
         * so we try to avoid inflating the spine[] and priorElementCount[] arrays
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   424
         * prematurely.  So methods must be prepared to deal with these arrays being
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   425
         * null.  If spine is non-null, then spineIndex points to the current chunk
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   426
         * within the spine, otherwise it is zero.  The spine and priorElementCount
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   427
         * arrays are always the same size, and for any i <= spineIndex,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   428
         * priorElementCount[i] is the sum of the sizes of all the prior chunks.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   429
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   430
         * The curChunk pointer is always valid.  The elementIndex is the index of
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   431
         * the next element to be written in curChunk; this may be past the end of
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   432
         * curChunk so we have to check before writing. When we inflate the spine
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   433
         * array, curChunk becomes the first element in it.  When we clear the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   434
         * buffer, we discard all chunks except the first one, which we clear,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   435
         * restoring it to the initial single-chunk state.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   436
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   437
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   438
        // The chunk we're currently writing into
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   439
        T_ARR curChunk;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   440
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   441
        // All chunks, or null if there is only one chunk
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   442
        T_ARR[] spine;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   443
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   444
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   445
         * Constructs an empty list with the specified initial capacity.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   446
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   447
         * @param  initialCapacity  the initial capacity of the list
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   448
         * @throws IllegalArgumentException if the specified initial capacity
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   449
         *         is negative
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   450
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   451
        OfPrimitive(int initialCapacity) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   452
            super(initialCapacity);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   453
            curChunk = newArray(1 << initialChunkPower);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   454
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   455
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   456
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   457
         * Constructs an empty list with an initial capacity of sixteen.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   458
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   459
        OfPrimitive() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   460
            super();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   461
            curChunk = newArray(1 << initialChunkPower);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   462
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   463
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   464
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   465
        public abstract Iterator<E> iterator();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   466
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   467
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   468
        public abstract void forEach(Consumer<? super E> consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   469
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   470
        /** Create a new array-of-array of the proper type and size */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   471
        protected abstract T_ARR[] newArrayArray(int size);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   472
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   473
        /** Create a new array of the proper type and size */
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   474
        public abstract T_ARR newArray(int size);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   475
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   476
        /** Get the length of an array */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   477
        protected abstract int arrayLength(T_ARR array);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   478
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   479
        /** Iterate an array with the provided consumer */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   480
        protected abstract void arrayForEach(T_ARR array, int from, int to,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   481
                                             T_CONS consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   482
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   483
        protected long capacity() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   484
            return (spineIndex == 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   485
                   ? arrayLength(curChunk)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   486
                   : priorElementCount[spineIndex] + arrayLength(spine[spineIndex]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   487
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   488
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   489
        private void inflateSpine() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   490
            if (spine == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   491
                spine = newArrayArray(MIN_SPINE_SIZE);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   492
                priorElementCount = new long[MIN_SPINE_SIZE];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   493
                spine[0] = curChunk;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   494
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   495
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   496
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   497
        protected final void ensureCapacity(long targetSize) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   498
            long capacity = capacity();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   499
            if (targetSize > capacity) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   500
                inflateSpine();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   501
                for (int i=spineIndex+1; targetSize > capacity; i++) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   502
                    if (i >= spine.length) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   503
                        int newSpineSize = spine.length * 2;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   504
                        spine = Arrays.copyOf(spine, newSpineSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   505
                        priorElementCount = Arrays.copyOf(priorElementCount, newSpineSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   506
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   507
                    int nextChunkSize = chunkSize(i);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   508
                    spine[i] = newArray(nextChunkSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   509
                    priorElementCount[i] = priorElementCount[i-1] + arrayLength(spine[i - 1]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   510
                    capacity += nextChunkSize;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   511
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   512
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   513
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   514
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   515
        protected void increaseCapacity() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   516
            ensureCapacity(capacity() + 1);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   517
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   518
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   519
        protected int chunkFor(long index) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   520
            if (spineIndex == 0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   521
                if (index < elementIndex)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   522
                    return 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   523
                else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   524
                    throw new IndexOutOfBoundsException(Long.toString(index));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   525
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   526
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   527
            if (index >= count())
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   528
                throw new IndexOutOfBoundsException(Long.toString(index));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   529
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   530
            for (int j=0; j <= spineIndex; j++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   531
                if (index < priorElementCount[j] + arrayLength(spine[j]))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   532
                    return j;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   533
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   534
            throw new IndexOutOfBoundsException(Long.toString(index));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   535
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   536
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   537
        public void copyInto(T_ARR array, int offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   538
            long finalOffset = offset + count();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   539
            if (finalOffset > arrayLength(array) || finalOffset < offset) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   540
                throw new IndexOutOfBoundsException("does not fit");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   541
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   542
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   543
            if (spineIndex == 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   544
                System.arraycopy(curChunk, 0, array, offset, elementIndex);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   545
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   546
                // full chunks
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   547
                for (int i=0; i < spineIndex; i++) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   548
                    System.arraycopy(spine[i], 0, array, offset, arrayLength(spine[i]));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   549
                    offset += arrayLength(spine[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   550
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   551
                if (elementIndex > 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   552
                    System.arraycopy(curChunk, 0, array, offset, elementIndex);
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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   556
        public T_ARR asPrimitiveArray() {
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   557
            long size = count();
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   558
            if (size >= Nodes.MAX_ARRAY_SIZE)
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   559
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   560
            T_ARR result = newArray((int) size);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   561
            copyInto(result, 0);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   562
            return result;
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
        protected void preAccept() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   566
            if (elementIndex == arrayLength(curChunk)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   567
                inflateSpine();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   568
                if (spineIndex+1 >= spine.length || spine[spineIndex+1] == null)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   569
                    increaseCapacity();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   570
                elementIndex = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   571
                ++spineIndex;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   572
                curChunk = spine[spineIndex];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   573
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   574
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   575
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   576
        public void clear() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   577
            if (spine != null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   578
                curChunk = spine[0];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   579
                spine = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   580
                priorElementCount = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   581
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   582
            elementIndex = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   583
            spineIndex = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   584
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   585
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20507
diff changeset
   586
        @SuppressWarnings("overloads")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   587
        public void forEach(T_CONS consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   588
            // completed chunks, if any
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   589
            for (int j = 0; j < spineIndex; j++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   590
                arrayForEach(spine[j], 0, arrayLength(spine[j]), consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   591
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   592
            // current chunk
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   593
            arrayForEach(curChunk, 0, elementIndex, consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   594
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   595
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   596
        abstract class BaseSpliterator<T_SPLITR extends Spliterator.OfPrimitive<E, T_CONS, T_SPLITR>>
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   597
                implements Spliterator.OfPrimitive<E, T_CONS, T_SPLITR> {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   598
            // The current spine index
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   599
            int splSpineIndex;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   600
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   601
            // Last spine index
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   602
            final int lastSpineIndex;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   603
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   604
            // The current element index into the current spine
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   605
            int splElementIndex;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   606
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   607
            // Last spine's last element index + 1
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   608
            final int lastSpineElementFence;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   609
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   610
            // When splSpineIndex >= lastSpineIndex and
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   611
            // splElementIndex >= lastSpineElementFence then
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   612
            // this spliterator is fully traversed
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   613
            // tryAdvance can set splSpineIndex > spineIndex if the last spine is full
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   614
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   615
            // The current spine array
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   616
            T_ARR splChunk;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   617
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   618
            BaseSpliterator(int firstSpineIndex, int lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   619
                            int firstSpineElementIndex, int lastSpineElementFence) {
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   620
                this.splSpineIndex = firstSpineIndex;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   621
                this.lastSpineIndex = lastSpineIndex;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   622
                this.splElementIndex = firstSpineElementIndex;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   623
                this.lastSpineElementFence = lastSpineElementFence;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   624
                assert spine != null || firstSpineIndex == 0 && lastSpineIndex == 0;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   625
                splChunk = (spine == null) ? curChunk : spine[firstSpineIndex];
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   626
            }
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   627
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   628
            abstract T_SPLITR newSpliterator(int firstSpineIndex, int lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   629
                                             int firstSpineElementIndex, int lastSpineElementFence);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   630
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   631
            abstract void arrayForOne(T_ARR array, int index, T_CONS consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   632
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   633
            abstract T_SPLITR arraySpliterator(T_ARR array, int offset, int len);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   634
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   635
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   636
            public long estimateSize() {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   637
                return (splSpineIndex == lastSpineIndex)
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   638
                       ? (long) lastSpineElementFence - splElementIndex
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   639
                       : // # of elements prior to end -
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   640
                       priorElementCount[lastSpineIndex] + lastSpineElementFence -
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   641
                       // # of elements prior to current
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   642
                       priorElementCount[splSpineIndex] - splElementIndex;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   643
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   644
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   645
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   646
            public int characteristics() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   647
                return SPLITERATOR_CHARACTERISTICS;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   648
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   649
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   650
            @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   651
            public boolean tryAdvance(T_CONS consumer) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18171
diff changeset
   652
                Objects.requireNonNull(consumer);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18171
diff changeset
   653
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   654
                if (splSpineIndex < lastSpineIndex
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   655
                    || (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   656
                    arrayForOne(splChunk, splElementIndex++, consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   657
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   658
                    if (splElementIndex == arrayLength(splChunk)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   659
                        splElementIndex = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   660
                        ++splSpineIndex;
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   661
                        if (spine != null && splSpineIndex <= lastSpineIndex)
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   662
                            splChunk = spine[splSpineIndex];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   663
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   664
                    return true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   665
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   666
                return false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   667
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   668
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   669
            @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   670
            public void forEachRemaining(T_CONS consumer) {
19188
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18171
diff changeset
   671
                Objects.requireNonNull(consumer);
bbf287c5cd92 8020016: Numerous splitereator impls do not throw NPE for null Consumers
psandoz
parents: 18171
diff changeset
   672
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   673
                if (splSpineIndex < lastSpineIndex
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   674
                    || (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   675
                    int i = splElementIndex;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   676
                    // completed chunks, if any
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   677
                    for (int sp = splSpineIndex; sp < lastSpineIndex; sp++) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   678
                        T_ARR chunk = spine[sp];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   679
                        arrayForEach(chunk, i, arrayLength(chunk), consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   680
                        i = 0;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   681
                    }
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   682
                    // last (or current uncompleted) chunk
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   683
                    T_ARR chunk = (splSpineIndex == lastSpineIndex) ? splChunk : spine[lastSpineIndex];
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   684
                    arrayForEach(chunk, i, lastSpineElementFence, consumer);
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   685
                    // mark consumed
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   686
                    splSpineIndex = lastSpineIndex;
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   687
                    splElementIndex = lastSpineElementFence;
17182
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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   691
            @Override
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   692
            public T_SPLITR trySplit() {
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   693
                if (splSpineIndex < lastSpineIndex) {
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   694
                    // split just before last chunk (if it is full this means 50:50 split)
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   695
                    T_SPLITR ret = newSpliterator(splSpineIndex, lastSpineIndex - 1,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   696
                                                  splElementIndex, arrayLength(spine[lastSpineIndex - 1]));
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   697
                    // position us to start of last chunk
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   698
                    splSpineIndex = lastSpineIndex;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   699
                    splElementIndex = 0;
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   700
                    splChunk = spine[splSpineIndex];
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   701
                    return ret;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   702
                }
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   703
                else if (splSpineIndex == lastSpineIndex) {
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   704
                    int t = (lastSpineElementFence - splElementIndex) / 2;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   705
                    if (t == 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   706
                        return null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   707
                    else {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   708
                        T_SPLITR ret = arraySpliterator(splChunk, splElementIndex, t);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   709
                        splElementIndex += t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   710
                        return ret;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   711
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   712
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   713
                else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   714
                    return null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   715
                }
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
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   719
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   720
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   721
     * An ordered collection of {@code int} values.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   722
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   723
    static class OfInt extends SpinedBuffer.OfPrimitive<Integer, int[], IntConsumer>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   724
            implements IntConsumer {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   725
        OfInt() { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   726
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   727
        OfInt(int initialCapacity) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   728
            super(initialCapacity);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   729
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   730
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   731
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   732
        public void forEach(Consumer<? super Integer> consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   733
            if (consumer instanceof IntConsumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   734
                forEach((IntConsumer) consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   735
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   736
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   737
                if (Tripwire.ENABLED)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   738
                    Tripwire.trip(getClass(), "{0} calling SpinedBuffer.OfInt.forEach(Consumer)");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   739
                spliterator().forEachRemaining(consumer);
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   744
        protected int[][] newArrayArray(int size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   745
            return new int[size][];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   746
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   747
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   748
        @Override
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   749
        public int[] newArray(int size) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   750
            return new int[size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   751
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   752
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   753
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   754
        protected int arrayLength(int[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   755
            return array.length;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   756
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   757
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   758
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   759
        protected void arrayForEach(int[] array,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   760
                                    int from, int to,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   761
                                    IntConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   762
            for (int i = from; i < to; i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   763
                consumer.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   764
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   765
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   766
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   767
        public void accept(int i) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   768
            preAccept();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   769
            curChunk[elementIndex++] = i;
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
        public int get(long index) {
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   773
            // Casts to int are safe since the spine array index is the index minus
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   774
            // the prior element count from the current spine
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   775
            int ch = chunkFor(index);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   776
            if (spineIndex == 0 && ch == 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   777
                return curChunk[(int) index];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   778
            else
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   779
                return spine[ch][(int) (index - priorElementCount[ch])];
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   780
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   781
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   782
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   783
        public PrimitiveIterator.OfInt iterator() {
18155
889970e5b728 8015792: Rename Spliterators.spliteratorFromIterator to Spliterators.iterator
psandoz
parents: 17182
diff changeset
   784
            return Spliterators.iterator(spliterator());
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   785
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   786
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   787
        public Spliterator.OfInt spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   788
            class Splitr extends BaseSpliterator<Spliterator.OfInt>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   789
                    implements Spliterator.OfInt {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   790
                Splitr(int firstSpineIndex, int lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   791
                       int firstSpineElementIndex, int lastSpineElementFence) {
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   792
                    super(firstSpineIndex, lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   793
                          firstSpineElementIndex, lastSpineElementFence);
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   794
                }
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   795
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   796
                @Override
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   797
                Splitr newSpliterator(int firstSpineIndex, int lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   798
                                      int firstSpineElementIndex, int lastSpineElementFence) {
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   799
                    return new Splitr(firstSpineIndex, lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   800
                                      firstSpineElementIndex, lastSpineElementFence);
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   801
                }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   802
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   803
                @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   804
                void arrayForOne(int[] array, int index, IntConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   805
                    consumer.accept(array[index]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   806
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   807
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   808
                @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   809
                Spliterator.OfInt arraySpliterator(int[] array, int offset, int len) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   810
                    return Arrays.spliterator(array, offset, offset+len);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   811
                }
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   812
            }
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   813
            return new Splitr(0, spineIndex, 0, elementIndex);
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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   817
        public String toString() {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   818
            int[] array = asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   819
            if (array.length < 200) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   820
                return String.format("%s[length=%d, chunks=%d]%s",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   821
                                     getClass().getSimpleName(), array.length,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   822
                                     spineIndex, Arrays.toString(array));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   823
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   824
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   825
                int[] array2 = Arrays.copyOf(array, 200);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   826
                return String.format("%s[length=%d, chunks=%d]%s...",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   827
                                     getClass().getSimpleName(), array.length,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   828
                                     spineIndex, Arrays.toString(array2));
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
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   832
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   833
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   834
     * An ordered collection of {@code long} values.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   835
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   836
    static class OfLong extends SpinedBuffer.OfPrimitive<Long, long[], LongConsumer>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   837
            implements LongConsumer {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   838
        OfLong() { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   839
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   840
        OfLong(int initialCapacity) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   841
            super(initialCapacity);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   842
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   843
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   844
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   845
        public void forEach(Consumer<? super Long> consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   846
            if (consumer instanceof LongConsumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   847
                forEach((LongConsumer) consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   848
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   849
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   850
                if (Tripwire.ENABLED)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   851
                    Tripwire.trip(getClass(), "{0} calling SpinedBuffer.OfLong.forEach(Consumer)");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   852
                spliterator().forEachRemaining(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   853
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   854
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   855
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   856
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   857
        protected long[][] newArrayArray(int size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   858
            return new long[size][];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   859
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   860
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   861
        @Override
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   862
        public long[] newArray(int size) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   863
            return new long[size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   864
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   865
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   866
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   867
        protected int arrayLength(long[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   868
            return array.length;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   869
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   870
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   871
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   872
        protected void arrayForEach(long[] array,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   873
                                    int from, int to,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   874
                                    LongConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   875
            for (int i = from; i < to; i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   876
                consumer.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   877
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   878
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   879
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   880
        public void accept(long i) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   881
            preAccept();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   882
            curChunk[elementIndex++] = i;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   883
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   884
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   885
        public long get(long index) {
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   886
            // Casts to int are safe since the spine array index is the index minus
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   887
            // the prior element count from the current spine
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   888
            int ch = chunkFor(index);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   889
            if (spineIndex == 0 && ch == 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   890
                return curChunk[(int) index];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   891
            else
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
   892
                return spine[ch][(int) (index - priorElementCount[ch])];
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   893
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   894
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   895
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   896
        public PrimitiveIterator.OfLong iterator() {
18155
889970e5b728 8015792: Rename Spliterators.spliteratorFromIterator to Spliterators.iterator
psandoz
parents: 17182
diff changeset
   897
            return Spliterators.iterator(spliterator());
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   898
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   899
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   900
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   901
        public Spliterator.OfLong spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   902
            class Splitr extends BaseSpliterator<Spliterator.OfLong>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   903
                    implements Spliterator.OfLong {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   904
                Splitr(int firstSpineIndex, int lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   905
                       int firstSpineElementIndex, int lastSpineElementFence) {
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   906
                    super(firstSpineIndex, lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   907
                          firstSpineElementIndex, lastSpineElementFence);
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   908
                }
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   909
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   910
                @Override
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   911
                Splitr newSpliterator(int firstSpineIndex, int lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   912
                                      int firstSpineElementIndex, int lastSpineElementFence) {
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   913
                    return new Splitr(firstSpineIndex, lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   914
                                      firstSpineElementIndex, lastSpineElementFence);
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   915
                }
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   916
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   917
                @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   918
                void arrayForOne(long[] array, int index, LongConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   919
                    consumer.accept(array[index]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   920
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   921
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   922
                @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   923
                Spliterator.OfLong arraySpliterator(long[] array, int offset, int len) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   924
                    return Arrays.spliterator(array, offset, offset+len);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   925
                }
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   926
            }
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   927
            return new Splitr(0, spineIndex, 0, elementIndex);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   928
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   929
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   930
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   931
        public String toString() {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   932
            long[] array = asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   933
            if (array.length < 200) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   934
                return String.format("%s[length=%d, chunks=%d]%s",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   935
                                     getClass().getSimpleName(), array.length,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   936
                                     spineIndex, Arrays.toString(array));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   937
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   938
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   939
                long[] array2 = Arrays.copyOf(array, 200);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   940
                return String.format("%s[length=%d, chunks=%d]%s...",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   941
                                     getClass().getSimpleName(), array.length,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   942
                                     spineIndex, Arrays.toString(array2));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   943
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   944
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   945
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   946
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   947
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   948
     * An ordered collection of {@code double} values.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   949
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   950
    static class OfDouble
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   951
            extends SpinedBuffer.OfPrimitive<Double, double[], DoubleConsumer>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   952
            implements DoubleConsumer {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   953
        OfDouble() { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   954
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   955
        OfDouble(int initialCapacity) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   956
            super(initialCapacity);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   957
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   958
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   959
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   960
        public void forEach(Consumer<? super Double> consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   961
            if (consumer instanceof DoubleConsumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   962
                forEach((DoubleConsumer) consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   963
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   964
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   965
                if (Tripwire.ENABLED)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   966
                    Tripwire.trip(getClass(), "{0} calling SpinedBuffer.OfDouble.forEach(Consumer)");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   967
                spliterator().forEachRemaining(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   968
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   969
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   970
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   971
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   972
        protected double[][] newArrayArray(int size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   973
            return new double[size][];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   974
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   975
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   976
        @Override
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   977
        public double[] newArray(int size) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   978
            return new double[size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   979
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   980
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   981
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   982
        protected int arrayLength(double[] array) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   983
            return array.length;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   984
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   985
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   986
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   987
        protected void arrayForEach(double[] array,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   988
                                    int from, int to,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   989
                                    DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   990
            for (int i = from; i < to; i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   991
                consumer.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   992
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   993
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   994
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   995
        public void accept(double i) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   996
            preAccept();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   997
            curChunk[elementIndex++] = i;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   998
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   999
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1000
        public double get(long index) {
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
  1001
            // Casts to int are safe since the spine array index is the index minus
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
  1002
            // the prior element count from the current spine
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1003
            int ch = chunkFor(index);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1004
            if (spineIndex == 0 && ch == 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1005
                return curChunk[(int) index];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1006
            else
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 19188
diff changeset
  1007
                return spine[ch][(int) (index - priorElementCount[ch])];
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1008
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1009
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1010
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1011
        public PrimitiveIterator.OfDouble iterator() {
18155
889970e5b728 8015792: Rename Spliterators.spliteratorFromIterator to Spliterators.iterator
psandoz
parents: 17182
diff changeset
  1012
            return Spliterators.iterator(spliterator());
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1013
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1014
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1015
        public Spliterator.OfDouble spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1016
            class Splitr extends BaseSpliterator<Spliterator.OfDouble>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1017
                    implements Spliterator.OfDouble {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1018
                Splitr(int firstSpineIndex, int lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1019
                       int firstSpineElementIndex, int lastSpineElementFence) {
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1020
                    super(firstSpineIndex, lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1021
                          firstSpineElementIndex, lastSpineElementFence);
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1022
                }
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1023
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1024
                @Override
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1025
                Splitr newSpliterator(int firstSpineIndex, int lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1026
                                      int firstSpineElementIndex, int lastSpineElementFence) {
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1027
                    return new Splitr(firstSpineIndex, lastSpineIndex,
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1028
                                      firstSpineElementIndex, lastSpineElementFence);
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1029
                }
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1030
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1031
                @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1032
                void arrayForOne(double[] array, int index, DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1033
                    consumer.accept(array[index]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1034
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1035
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1036
                @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1037
                Spliterator.OfDouble arraySpliterator(double[] array, int offset, int len) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1038
                    return Arrays.spliterator(array, offset, offset+len);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1039
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1040
            }
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1041
            return new Splitr(0, spineIndex, 0, elementIndex);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1042
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1043
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1044
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1045
        public String toString() {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
  1046
            double[] array = asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1047
            if (array.length < 200) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1048
                return String.format("%s[length=%d, chunks=%d]%s",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1049
                                     getClass().getSimpleName(), array.length,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1050
                                     spineIndex, Arrays.toString(array));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1051
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1052
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1053
                double[] array2 = Arrays.copyOf(array, 200);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1054
                return String.format("%s[length=%d, chunks=%d]%s...",
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1055
                                     getClass().getSimpleName(), array.length,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1056
                                     spineIndex, Arrays.toString(array2));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1057
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1058
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1059
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1060
}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
  1061