jdk/src/share/classes/java/util/stream/SortedOps.java
author psandoz
Thu, 16 Jan 2014 18:20:31 +0100
changeset 22289 bb9c71b84919
parent 22078 bdec5d53e98c
child 22297 1c62c67d9dd2
permissions -rw-r--r--
8029452: Fork/Join task ForEachOps.ForEachOrderedTask clarifications and minor improvements Reviewed-by: mduigou, briangoetz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     1
/*
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     4
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    10
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    15
 * accompanied this code).
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    16
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    20
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    23
 * questions.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    24
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    25
package java.util.stream;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    26
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    27
import java.util.ArrayList;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    28
import java.util.Arrays;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    29
import java.util.Comparator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    30
import java.util.Objects;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    31
import java.util.Spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    32
import java.util.concurrent.ForkJoinTask;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    33
import java.util.function.IntFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    34
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    35
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    36
/**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    37
 * Factory methods for transforming streams into sorted streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    38
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    39
 * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    40
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    41
final class SortedOps {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    42
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    43
    private SortedOps() { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    44
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    45
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    46
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    47
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    48
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    49
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    50
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    51
    static <T> Stream<T> makeRef(AbstractPipeline<?, T, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    52
        return new OfRef<>(upstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    53
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    54
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    55
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    56
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    57
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    58
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    59
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    60
     * @param comparator the comparator to order elements by
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    61
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    62
    static <T> Stream<T> makeRef(AbstractPipeline<?, T, ?> upstream,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    63
                                Comparator<? super T> comparator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    64
        return new OfRef<>(upstream, comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    65
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    66
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    67
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    68
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    69
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    70
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    71
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    73
    static <T> IntStream makeInt(AbstractPipeline<?, Integer, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    74
        return new OfInt(upstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    75
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    76
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    77
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    78
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    81
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    82
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    83
    static <T> LongStream makeLong(AbstractPipeline<?, Long, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    84
        return new OfLong(upstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    86
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    87
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    88
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    89
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    90
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    91
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    92
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    93
    static <T> DoubleStream makeDouble(AbstractPipeline<?, Double, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    94
        return new OfDouble(upstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    95
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    96
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    97
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    98
     * Specialized subtype for sorting reference streams
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    99
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   100
    private static final class OfRef<T> extends ReferencePipeline.StatefulOp<T, T> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   102
         * Comparator used for sorting
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   103
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   104
        private final boolean isNaturalSort;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   105
        private final Comparator<? super T> comparator;
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
         * Sort using natural order of {@literal <T>} which must be
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   109
         * {@code Comparable}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   110
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   111
        OfRef(AbstractPipeline<?, T, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   112
            super(upstream, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   113
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   114
            this.isNaturalSort = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   115
            // Will throw CCE when we try to sort if T is not Comparable
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 18171
diff changeset
   116
            this.comparator = (Comparator<? super T>) Comparator.naturalOrder();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   117
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   118
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   119
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   120
         * Sort using the provided comparator.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
         * @param comparator The comparator to be used to evaluate ordering.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
        OfRef(AbstractPipeline<?, T, ?> upstream, Comparator<? super T> comparator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   125
            super(upstream, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   126
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.NOT_SORTED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   127
            this.isNaturalSort = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   128
            this.comparator = Objects.requireNonNull(comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   129
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   130
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   131
        @Override
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   132
        public Sink<T> opWrapSink(int flags, Sink<T> sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   133
            Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   134
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   135
            // If the input is already naturally sorted and this operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   136
            // also naturally sorted then this is a no-op
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   137
            if (StreamOpFlag.SORTED.isKnown(flags) && isNaturalSort)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   138
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   139
            else if (StreamOpFlag.SIZED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   140
                return new SizedRefSortingSink<>(sink, comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   141
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   142
                return new RefSortingSink<>(sink, comparator);
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   146
        public <P_IN> Node<T> opEvaluateParallel(PipelineHelper<T> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   147
                                                 Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   148
                                                 IntFunction<T[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   149
            // If the input is already naturally sorted and this operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   150
            // naturally sorts then collect the output
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   151
            if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags()) && isNaturalSort) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   152
                return helper.evaluate(spliterator, false, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   153
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   154
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   155
                // @@@ Weak two-pass parallel implementation; parallel collect, parallel sort
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   156
                T[] flattenedData = helper.evaluate(spliterator, true, generator).asArray(generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   157
                Arrays.parallelSort(flattenedData, comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   158
                return Nodes.node(flattenedData);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   159
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   160
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   161
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   162
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   163
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   164
     * Specialized subtype for sorting int streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   165
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   166
    private static final class OfInt extends IntPipeline.StatefulOp<Integer> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   167
        OfInt(AbstractPipeline<?, Integer, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   168
            super(upstream, StreamShape.INT_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   169
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   170
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   171
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   172
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   173
        public Sink<Integer> opWrapSink(int flags, Sink sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   174
            Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   175
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   176
            if (StreamOpFlag.SORTED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   177
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   178
            else if (StreamOpFlag.SIZED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   179
                return new SizedIntSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   180
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   181
                return new IntSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   182
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   183
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   184
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   185
        public <P_IN> Node<Integer> opEvaluateParallel(PipelineHelper<Integer> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   186
                                                       Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   187
                                                       IntFunction<Integer[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   188
            if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   189
                return helper.evaluate(spliterator, false, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   190
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   191
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   192
                Node.OfInt n = (Node.OfInt) helper.evaluate(spliterator, true, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   193
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 17182
diff changeset
   194
                int[] content = n.asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   195
                Arrays.parallelSort(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   196
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   197
                return Nodes.node(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   198
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   199
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   200
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   201
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   202
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   203
     * Specialized subtype for sorting long streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   204
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   205
    private static final class OfLong extends LongPipeline.StatefulOp<Long> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   206
        OfLong(AbstractPipeline<?, Long, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   207
            super(upstream, StreamShape.LONG_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   208
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
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
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18571
diff changeset
   212
        public Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   213
            Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   214
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   215
            if (StreamOpFlag.SORTED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   216
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   217
            else if (StreamOpFlag.SIZED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   218
                return new SizedLongSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   219
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   220
                return new LongSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   221
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   222
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   223
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   224
        public <P_IN> Node<Long> opEvaluateParallel(PipelineHelper<Long> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   225
                                                    Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   226
                                                    IntFunction<Long[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   227
            if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   228
                return helper.evaluate(spliterator, false, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   229
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   230
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   231
                Node.OfLong n = (Node.OfLong) helper.evaluate(spliterator, true, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   232
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 17182
diff changeset
   233
                long[] content = n.asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   234
                Arrays.parallelSort(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   235
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   236
                return Nodes.node(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   237
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   238
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   239
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   240
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   241
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   242
     * Specialized subtype for sorting double streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   243
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   244
    private static final class OfDouble extends DoublePipeline.StatefulOp<Double> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   245
        OfDouble(AbstractPipeline<?, Double, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   246
            super(upstream, StreamShape.DOUBLE_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   247
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   248
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   249
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   250
        @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18571
diff changeset
   251
        public Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   252
            Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   253
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   254
            if (StreamOpFlag.SORTED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   255
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   256
            else if (StreamOpFlag.SIZED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   257
                return new SizedDoubleSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   258
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   259
                return new DoubleSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   260
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   261
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   262
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   263
        public <P_IN> Node<Double> opEvaluateParallel(PipelineHelper<Double> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   264
                                                      Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   265
                                                      IntFunction<Double[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   266
            if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   267
                return helper.evaluate(spliterator, false, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   268
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   269
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   270
                Node.OfDouble n = (Node.OfDouble) helper.evaluate(spliterator, true, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   271
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 17182
diff changeset
   272
                double[] content = n.asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   273
                Arrays.parallelSort(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   274
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   275
                return Nodes.node(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   276
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   277
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   278
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   279
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   280
    /**
20503
074dd13d9cdf 8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents: 19593
diff changeset
   281
     * {@link Sink} for implementing sort on SIZED reference streams.
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   282
     */
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   283
    private static final class SizedRefSortingSink<T> extends Sink.ChainedReference<T, T> {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   284
        private final Comparator<? super T> comparator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   285
        private T[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   286
        private int offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   287
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   288
        SizedRefSortingSink(Sink<? super T> sink, Comparator<? super T> comparator) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   289
            super(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   290
            this.comparator = comparator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   291
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   292
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   293
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   294
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   295
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   296
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   297
            array = (T[]) new Object[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   298
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   299
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   300
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   301
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   302
            Arrays.sort(array, 0, offset, comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   303
            downstream.begin(offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   304
            for (int i = 0; i < offset; i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   305
                downstream.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   306
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   307
            array = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   308
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   309
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   310
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   311
        public void accept(T t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   312
            array[offset++] = t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   313
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   314
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   315
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   316
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   317
     * {@link Sink} for implementing sort on reference streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   318
     */
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   319
    private static final class RefSortingSink<T> extends Sink.ChainedReference<T, T> {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   320
        private final Comparator<? super T> comparator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   321
        private ArrayList<T> list;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   322
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   323
        RefSortingSink(Sink<? super T> sink, Comparator<? super T> comparator) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   324
            super(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   325
            this.comparator = comparator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   326
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   327
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   328
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   329
        public void begin(long size) {
20503
074dd13d9cdf 8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents: 19593
diff changeset
   330
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   331
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 20507
diff changeset
   332
            list = (size >= 0) ? new ArrayList<>((int) size) : new ArrayList<>();
17182
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 end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   337
            list.sort(comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   338
            downstream.begin(list.size());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   339
            list.forEach(downstream::accept);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   340
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   341
            list = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   342
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   343
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   344
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   345
        public void accept(T t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   346
            list.add(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   347
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   348
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   349
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   350
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   351
     * {@link Sink} for implementing sort on SIZED int streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   352
     */
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   353
    private static final class SizedIntSortingSink extends Sink.ChainedInt<Integer> {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   354
        private int[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   355
        private int offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   356
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   357
        SizedIntSortingSink(Sink<? super Integer> downstream) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   358
            super(downstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   359
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   360
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   361
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   362
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   363
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   364
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   365
            array = new int[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   366
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   367
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   368
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   369
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   370
            Arrays.sort(array, 0, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   371
            downstream.begin(offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   372
            for (int i = 0; i < offset; i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   373
                downstream.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   374
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   375
            array = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   376
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   377
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   378
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   379
        public void accept(int t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   380
            array[offset++] = t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   381
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   382
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   383
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   384
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   385
     * {@link Sink} for implementing sort on int streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   386
     */
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   387
    private static final class IntSortingSink extends Sink.ChainedInt<Integer> {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   388
        private SpinedBuffer.OfInt b;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   389
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   390
        IntSortingSink(Sink<? super Integer> sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   391
            super(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   392
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   393
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   394
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   395
        public void begin(long size) {
20503
074dd13d9cdf 8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents: 19593
diff changeset
   396
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   397
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   398
            b = (size > 0) ? new SpinedBuffer.OfInt((int) size) : new SpinedBuffer.OfInt();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   399
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   400
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   401
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   402
        public void end() {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 17182
diff changeset
   403
            int[] ints = b.asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   404
            Arrays.sort(ints);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   405
            downstream.begin(ints.length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   406
            for (int anInt : ints)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   407
                downstream.accept(anInt);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   408
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   409
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   410
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   411
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   412
        public void accept(int t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   413
            b.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   414
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   415
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   416
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   417
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   418
     * {@link Sink} for implementing sort on SIZED long streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   419
     */
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   420
    private static final class SizedLongSortingSink extends Sink.ChainedLong<Long> {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   421
        private long[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   422
        private int offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   423
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   424
        SizedLongSortingSink(Sink<? super Long> downstream) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   425
            super(downstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   426
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   427
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   428
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   429
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   430
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   431
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   432
            array = new long[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   433
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   434
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   435
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   436
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   437
            Arrays.sort(array, 0, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   438
            downstream.begin(offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   439
            for (int i = 0; i < offset; i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   440
                downstream.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   441
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   442
            array = null;
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   446
        public void accept(long t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   447
            array[offset++] = t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   448
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   449
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   450
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   451
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   452
     * {@link Sink} for implementing sort on long streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   453
     */
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   454
    private static final class LongSortingSink extends Sink.ChainedLong<Long> {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   455
        private SpinedBuffer.OfLong b;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   456
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   457
        LongSortingSink(Sink<? super Long> sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   458
            super(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   459
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   460
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   461
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   462
        public void begin(long size) {
20503
074dd13d9cdf 8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents: 19593
diff changeset
   463
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   464
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   465
            b = (size > 0) ? new SpinedBuffer.OfLong((int) size) : new SpinedBuffer.OfLong();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   466
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   467
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   468
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   469
        public void end() {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 17182
diff changeset
   470
            long[] longs = b.asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   471
            Arrays.sort(longs);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   472
            downstream.begin(longs.length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   473
            for (long aLong : longs)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   474
                downstream.accept(aLong);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   475
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   476
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   477
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   478
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   479
        public void accept(long t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   480
            b.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   481
        }
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
     * {@link Sink} for implementing sort on SIZED double streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   486
     */
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   487
    private static final class SizedDoubleSortingSink extends Sink.ChainedDouble<Double> {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   488
        private double[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   489
        private int offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   490
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   491
        SizedDoubleSortingSink(Sink<? super Double> downstream) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   492
            super(downstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   493
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   494
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   495
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   496
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   497
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   498
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   499
            array = new double[(int) size];
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   503
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   504
            Arrays.sort(array, 0, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   505
            downstream.begin(offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   506
            for (int i = 0; i < offset; i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   507
                downstream.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   508
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   509
            array = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   510
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   511
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   512
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   513
        public void accept(double t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   514
            array[offset++] = t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   515
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   516
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   517
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   518
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   519
     * {@link Sink} for implementing sort on double streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   520
     */
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   521
    private static final class DoubleSortingSink extends Sink.ChainedDouble<Double> {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   522
        private SpinedBuffer.OfDouble b;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   523
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   524
        DoubleSortingSink(Sink<? super Double> sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   525
            super(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   526
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   527
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   528
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   529
        public void begin(long size) {
20503
074dd13d9cdf 8025535: Unsafe typecast in java.util.stream.SortedOps
psandoz
parents: 19593
diff changeset
   530
            if (size >= Nodes.MAX_ARRAY_SIZE)
20507
8498104f92c3 8025534: Unsafe typecast in java.util.stream.Streams.Nodes
psandoz
parents: 20503
diff changeset
   531
                throw new IllegalArgumentException(Nodes.BAD_SIZE);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   532
            b = (size > 0) ? new SpinedBuffer.OfDouble((int) size) : new SpinedBuffer.OfDouble();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   533
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   534
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   535
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   536
        public void end() {
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 17182
diff changeset
   537
            double[] doubles = b.asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   538
            Arrays.sort(doubles);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   539
            downstream.begin(doubles.length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   540
            for (double aDouble : doubles)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   541
                downstream.accept(aDouble);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   542
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   543
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   544
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   545
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   546
        public void accept(double t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   547
            b.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   548
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   549
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   550
}