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