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