jdk/src/share/classes/java/util/stream/SortedOps.java
author psandoz
Tue, 28 May 2013 15:22:30 +0200
changeset 17914 91e138d3b298
parent 17182 b786c0de868c
child 18171 2725a30c1a02
permissions -rw-r--r--
8014393: Minor typo in the spec for j.u.stream.Stream.findFirst() Reviewed-by: alanb, chegar
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.Comparators;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    31
import java.util.Objects;
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.concurrent.ForkJoinTask;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    34
import java.util.function.IntFunction;
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
/**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    38
 * Factory methods for transforming streams into sorted streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    39
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    40
 * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    41
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    42
final class SortedOps {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    43
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    44
    private SortedOps() { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    45
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    46
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    47
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    48
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    49
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    50
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    51
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    52
    static <T> Stream<T> makeRef(AbstractPipeline<?, T, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    53
        return new OfRef<>(upstream);
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    57
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    58
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    59
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    60
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    61
     * @param comparator the comparator to order elements by
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    62
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    63
    static <T> Stream<T> makeRef(AbstractPipeline<?, T, ?> upstream,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    64
                                Comparator<? super T> comparator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    65
        return new OfRef<>(upstream, comparator);
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    69
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    70
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    71
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    73
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    74
    static <T> IntStream makeInt(AbstractPipeline<?, Integer, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    75
        return new OfInt(upstream);
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    81
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    82
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    83
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    84
    static <T> LongStream makeLong(AbstractPipeline<?, Long, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
        return new OfLong(upstream);
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    89
     * Appends a "sorted" operation to the provided stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    90
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    91
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    92
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    93
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    94
    static <T> DoubleStream makeDouble(AbstractPipeline<?, Double, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    95
        return new OfDouble(upstream);
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    99
     * Specialized subtype for sorting reference streams
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   100
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
    private static final class OfRef<T> extends ReferencePipeline.StatefulOp<T, T> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   102
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   103
         * Comparator used for sorting
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   104
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   105
        private final boolean isNaturalSort;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   106
        private final Comparator<? super T> comparator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   107
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   108
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   109
         * Sort using natural order of {@literal <T>} which must be
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   110
         * {@code Comparable}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   111
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   112
        OfRef(AbstractPipeline<?, T, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   113
            super(upstream, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   114
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   115
            this.isNaturalSort = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   116
            // Will throw CCE when we try to sort if T is not Comparable
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   117
            this.comparator = (Comparator<? super T>) Comparators.naturalOrder();
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
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
         * Sort using the provided comparator.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
         * @param comparator The comparator to be used to evaluate ordering.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   125
        OfRef(AbstractPipeline<?, T, ?> upstream, Comparator<? super T> comparator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   126
            super(upstream, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   127
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.NOT_SORTED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   128
            this.isNaturalSort = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   129
            this.comparator = Objects.requireNonNull(comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   130
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   131
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   132
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   133
        public Sink<T> opWrapSink(int flags, Sink sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   134
            Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   135
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   136
            // If the input is already naturally sorted and this operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   137
            // also naturally sorted then this is a no-op
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   138
            if (StreamOpFlag.SORTED.isKnown(flags) && isNaturalSort)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   139
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   140
            else if (StreamOpFlag.SIZED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   141
                return new SizedRefSortingSink<>(sink, comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   142
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   143
                return new RefSortingSink<>(sink, comparator);
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   147
        public <P_IN> Node<T> opEvaluateParallel(PipelineHelper<T> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   148
                                                 Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   149
                                                 IntFunction<T[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   150
            // If the input is already naturally sorted and this operation
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   151
            // naturally sorts then collect the output
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   152
            if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags()) && isNaturalSort) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   153
                return helper.evaluate(spliterator, false, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   154
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   155
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   156
                // @@@ Weak two-pass parallel implementation; parallel collect, parallel sort
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   157
                T[] flattenedData = helper.evaluate(spliterator, true, generator).asArray(generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   158
                Arrays.parallelSort(flattenedData, comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   159
                return Nodes.node(flattenedData);
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   165
     * Specialized subtype for sorting int streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   166
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   167
    private static final class OfInt extends IntPipeline.StatefulOp<Integer> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   168
        OfInt(AbstractPipeline<?, Integer, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   169
            super(upstream, StreamShape.INT_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   170
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   171
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   172
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   173
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   174
        public Sink<Integer> opWrapSink(int flags, Sink sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   175
            Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   176
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   177
            if (StreamOpFlag.SORTED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   178
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   179
            else if (StreamOpFlag.SIZED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   180
                return new SizedIntSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   181
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   182
                return new IntSortingSink(sink);
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   186
        public <P_IN> Node<Integer> opEvaluateParallel(PipelineHelper<Integer> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   187
                                                       Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   188
                                                       IntFunction<Integer[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   189
            if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   190
                return helper.evaluate(spliterator, false, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   191
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   192
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   193
                Node.OfInt n = (Node.OfInt) helper.evaluate(spliterator, true, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   194
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   195
                int[] content = n.asIntArray();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   196
                Arrays.parallelSort(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   197
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   198
                return Nodes.node(content);
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   204
     * Specialized subtype for sorting long streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   205
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   206
    private static final class OfLong extends LongPipeline.StatefulOp<Long> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   207
        OfLong(AbstractPipeline<?, Long, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   208
            super(upstream, StreamShape.LONG_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   209
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   210
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   211
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   212
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   213
        public Sink<Long> opWrapSink(int flags, Sink sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   214
            Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   215
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   216
            if (StreamOpFlag.SORTED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   217
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   218
            else if (StreamOpFlag.SIZED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   219
                return new SizedLongSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   220
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   221
                return new LongSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   222
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   223
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   224
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   225
        public <P_IN> Node<Long> opEvaluateParallel(PipelineHelper<Long> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   226
                                                    Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   227
                                                    IntFunction<Long[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   228
            if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   229
                return helper.evaluate(spliterator, false, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   230
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   231
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   232
                Node.OfLong n = (Node.OfLong) helper.evaluate(spliterator, true, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   233
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   234
                long[] content = n.asLongArray();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   235
                Arrays.parallelSort(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   236
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   237
                return Nodes.node(content);
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   243
     * Specialized subtype for sorting double streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   244
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   245
    private static final class OfDouble extends DoublePipeline.StatefulOp<Double> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   246
        OfDouble(AbstractPipeline<?, Double, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   247
            super(upstream, StreamShape.DOUBLE_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   248
                  StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   249
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   250
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   251
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   252
        public Sink<Double> opWrapSink(int flags, Sink sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   253
            Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   254
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   255
            if (StreamOpFlag.SORTED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   256
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   257
            else if (StreamOpFlag.SIZED.isKnown(flags))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   258
                return new SizedDoubleSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   259
            else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   260
                return new DoubleSortingSink(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   261
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   262
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   263
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   264
        public <P_IN> Node<Double> opEvaluateParallel(PipelineHelper<Double> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   265
                                                      Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   266
                                                      IntFunction<Double[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   267
            if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   268
                return helper.evaluate(spliterator, false, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   269
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   270
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   271
                Node.OfDouble n = (Node.OfDouble) helper.evaluate(spliterator, true, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   272
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   273
                double[] content = n.asDoubleArray();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   274
                Arrays.parallelSort(content);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   275
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   276
                return Nodes.node(content);
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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   281
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   282
     * {@link ForkJoinTask} for implementing sort on SIZED reference streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   283
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   284
    private static final class SizedRefSortingSink<T> extends Sink.ChainedReference<T> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   285
        private final Comparator<? super T> comparator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   286
        private T[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   287
        private int offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   288
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   289
        SizedRefSortingSink(Sink sink, Comparator<? super T> comparator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   290
            super(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   291
            this.comparator = comparator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   292
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   293
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   294
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   295
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   296
            if (size >= Nodes.MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   297
                throw new IllegalArgumentException("Stream size exceeds max array size");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   298
            array = (T[]) new Object[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   299
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   300
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   301
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   302
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   303
            // Need to use offset rather than array.length since the downstream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   304
            // many be short-circuiting
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   305
            // @@@ A better approach is to know if the downstream short-circuits
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   306
            //     and check sink.cancellationRequested
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   307
            Arrays.sort(array, 0, offset, comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   308
            downstream.begin(offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   309
            for (int i = 0; i < offset; i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   310
                downstream.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   311
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   312
            array = null;
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   316
        public void accept(T t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   317
            array[offset++] = t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   318
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   319
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   320
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   321
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   322
     * {@link Sink} for implementing sort on reference streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   323
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   324
    private static final class RefSortingSink<T> extends Sink.ChainedReference<T> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   325
        private final Comparator<? super T> comparator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   326
        private ArrayList<T> list;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   327
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   328
        RefSortingSink(Sink sink, Comparator<? super T> comparator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   329
            super(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   330
            this.comparator = comparator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   331
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   332
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   333
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   334
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   335
            list = (size >= 0) ? new ArrayList<T>((int) size) : new ArrayList<T>();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   336
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   337
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   338
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   339
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   340
            list.sort(comparator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   341
            downstream.begin(list.size());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   342
            list.forEach(downstream::accept);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   343
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   344
            list = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   345
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   346
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   347
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   348
        public void accept(T t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   349
            list.add(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   350
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   351
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   352
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   353
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   354
     * {@link Sink} for implementing sort on SIZED int streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   355
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   356
    private static final class SizedIntSortingSink extends Sink.ChainedInt {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   357
        private int[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   358
        private int offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   359
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   360
        SizedIntSortingSink(Sink downstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   361
            super(downstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   362
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   363
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   364
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   365
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   366
            if (size >= Nodes.MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   367
                throw new IllegalArgumentException("Stream size exceeds max array size");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   368
            array = new int[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   369
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   370
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   371
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   372
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   373
            Arrays.sort(array, 0, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   374
            downstream.begin(offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   375
            for (int i = 0; i < offset; i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   376
                downstream.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   377
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   378
            array = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   379
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   380
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   381
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   382
        public void accept(int t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   383
            array[offset++] = t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   384
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   385
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   386
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   387
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   388
     * {@link Sink} for implementing sort on int streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   389
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   390
    private static final class IntSortingSink extends Sink.ChainedInt {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   391
        private SpinedBuffer.OfInt b;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   392
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   393
        IntSortingSink(Sink sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   394
            super(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   395
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   396
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   397
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   398
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   399
            b = (size > 0) ? new SpinedBuffer.OfInt((int) size) : new SpinedBuffer.OfInt();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   400
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   401
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   402
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   403
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   404
            int[] ints = b.asIntArray();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   405
            Arrays.sort(ints);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   406
            downstream.begin(ints.length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   407
            for (int anInt : ints)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   408
                downstream.accept(anInt);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   409
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   410
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   411
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   412
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   413
        public void accept(int t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   414
            b.accept(t);
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   419
     * {@link Sink} for implementing sort on SIZED long streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   420
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   421
    private static final class SizedLongSortingSink extends Sink.ChainedLong {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   422
        private long[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   423
        private int offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   424
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   425
        SizedLongSortingSink(Sink downstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   426
            super(downstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   427
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   428
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   429
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   430
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   431
            if (size >= Nodes.MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   432
                throw new IllegalArgumentException("Stream size exceeds max array size");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   433
            array = new long[(int) size];
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   434
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   435
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   436
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   437
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   438
            Arrays.sort(array, 0, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   439
            downstream.begin(offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   440
            for (int i = 0; i < offset; i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   441
                downstream.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   442
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   443
            array = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   444
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   445
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   446
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   447
        public void accept(long t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   448
            array[offset++] = t;
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
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   453
     * {@link Sink} for implementing sort on long streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   454
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   455
    private static final class LongSortingSink extends Sink.ChainedLong {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   456
        private SpinedBuffer.OfLong b;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   457
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   458
        LongSortingSink(Sink sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   459
            super(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   460
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   461
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   462
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   463
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   464
            b = (size > 0) ? new SpinedBuffer.OfLong((int) size) : new SpinedBuffer.OfLong();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   465
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   466
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   467
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   468
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   469
            long[] longs = b.asLongArray();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   470
            Arrays.sort(longs);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   471
            downstream.begin(longs.length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   472
            for (long aLong : longs)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   473
                downstream.accept(aLong);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   474
            downstream.end();
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   478
        public void accept(long t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   479
            b.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   480
        }
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
     * {@link Sink} for implementing sort on SIZED double streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   485
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   486
    private static final class SizedDoubleSortingSink extends Sink.ChainedDouble {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   487
        private double[] array;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   488
        private int offset;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   489
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   490
        SizedDoubleSortingSink(Sink downstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   491
            super(downstream);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   492
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   493
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   494
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   495
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   496
            if (size >= Nodes.MAX_ARRAY_SIZE)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   497
                throw new IllegalArgumentException("Stream size exceeds max array size");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   498
            array = new double[(int) size];
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   502
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   503
            Arrays.sort(array, 0, offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   504
            downstream.begin(offset);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   505
            for (int i = 0; i < offset; i++)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   506
                downstream.accept(array[i]);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   507
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   508
            array = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   509
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   510
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   511
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   512
        public void accept(double t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   513
            array[offset++] = t;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   514
        }
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
     * {@link Sink} for implementing sort on double streams.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   519
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   520
    private static final class DoubleSortingSink extends Sink.ChainedDouble {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   521
        private SpinedBuffer.OfDouble b;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   522
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   523
        DoubleSortingSink(Sink sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   524
            super(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   525
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   526
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   527
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   528
        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   529
            b = (size > 0) ? new SpinedBuffer.OfDouble((int) size) : new SpinedBuffer.OfDouble();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   530
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   531
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   532
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   533
        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   534
            double[] doubles = b.asDoubleArray();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   535
            Arrays.sort(doubles);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   536
            downstream.begin(doubles.length);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   537
            for (double aDouble : doubles)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   538
                downstream.accept(aDouble);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   539
            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   540
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   541
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   542
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   543
        public void accept(double t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   544
            b.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   545
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   546
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   547
}