jdk/src/share/classes/java/util/stream/DoublePipeline.java
author darcy
Sun, 01 Dec 2013 23:35:28 -0800
changeset 21946 b4cb3bbeb52a
parent 20866 36155ee613ef
child 22101 231247ddf41a
permissions -rw-r--r--
8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors Reviewed-by: psandoz, mduigou
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) 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.DoubleSummaryStatistics;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    28
import java.util.Objects;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    29
import java.util.OptionalDouble;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    30
import java.util.PrimitiveIterator;
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.Spliterators;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    33
import java.util.function.BiConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    34
import java.util.function.BinaryOperator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    35
import java.util.function.DoubleBinaryOperator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    36
import java.util.function.DoubleConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    37
import java.util.function.DoubleFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    38
import java.util.function.DoublePredicate;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    39
import java.util.function.DoubleToIntFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    40
import java.util.function.DoubleToLongFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    41
import java.util.function.DoubleUnaryOperator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    42
import java.util.function.IntFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    43
import java.util.function.ObjDoubleConsumer;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    44
import java.util.function.Supplier;
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
 * Abstract base class for an intermediate pipeline stage or pipeline source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    48
 * stage implementing whose elements are of type {@code double}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    49
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    50
 * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    51
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    52
 * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    53
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    54
abstract class DoublePipeline<E_IN>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    55
        extends AbstractPipeline<E_IN, Double, DoubleStream>
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    56
        implements DoubleStream {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    57
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    58
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    59
     * Constructor for the head of a stream pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    60
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    61
     * @param source {@code Supplier<Spliterator>} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    62
     * @param sourceFlags the source flags for the stream source, described in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    63
     * {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    64
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    65
    DoublePipeline(Supplier<? extends Spliterator<Double>> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    66
                   int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    67
        super(source, sourceFlags, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    68
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    69
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    70
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    71
     * Constructor for the head of a stream pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    73
     * @param source {@code Spliterator} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    74
     * @param sourceFlags the source flags for the stream source, described in
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    75
     * {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    76
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    77
    DoublePipeline(Spliterator<Double> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    78
                   int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
        super(source, sourceFlags, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    81
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    82
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    83
     * Constructor for appending an intermediate operation onto an existing
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    84
     * pipeline.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    86
     * @param upstream the upstream element source.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    87
     * @param opFlags the operation flags
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    88
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    89
    DoublePipeline(AbstractPipeline<?, E_IN, ?> upstream, int opFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    90
        super(upstream, opFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    91
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    92
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    93
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    94
     * Adapt a {@code Sink<Double> to a {@code DoubleConsumer}, ideally simply
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    95
     * by casting.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    96
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    97
    private static DoubleConsumer adapt(Sink<Double> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    98
        if (sink instanceof DoubleConsumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    99
            return (DoubleConsumer) sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   100
        } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
            if (Tripwire.ENABLED)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   102
                Tripwire.trip(AbstractPipeline.class,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   103
                              "using DoubleStream.adapt(Sink<Double> s)");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   104
            return sink::accept;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   105
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   106
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   107
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   108
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   109
     * Adapt a {@code Spliterator<Double>} to a {@code Spliterator.OfDouble}.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   110
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   111
     * @implNote
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   112
     * The implementation attempts to cast to a Spliterator.OfDouble, and throws
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   113
     * an exception if this cast is not possible.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   114
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   115
    private static Spliterator.OfDouble adapt(Spliterator<Double> s) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   116
        if (s instanceof Spliterator.OfDouble) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   117
            return (Spliterator.OfDouble) s;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   118
        } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   119
            if (Tripwire.ENABLED)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   120
                Tripwire.trip(AbstractPipeline.class,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
                              "using DoubleStream.adapt(Spliterator<Double> s)");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
            throw new UnsupportedOperationException("DoubleStream.adapt(Spliterator<Double> s)");
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   125
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   126
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   127
    // Shape-specific methods
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   128
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   129
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   130
    final StreamShape getOutputShape() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   131
        return StreamShape.DOUBLE_VALUE;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   132
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   133
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   134
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   135
    final <P_IN> Node<Double> evaluateToNode(PipelineHelper<Double> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   136
                                             Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   137
                                             boolean flattenTree,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   138
                                             IntFunction<Double[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   139
        return Nodes.collectDouble(helper, spliterator, flattenTree);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   140
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   141
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   142
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   143
    final <P_IN> Spliterator<Double> wrap(PipelineHelper<Double> ph,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   144
                                          Supplier<Spliterator<P_IN>> supplier,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   145
                                          boolean isParallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   146
        return new StreamSpliterators.DoubleWrappingSpliterator<>(ph, supplier, isParallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   147
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   148
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   149
    @Override
19220
d3d40ccb544e 8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents: 18528
diff changeset
   150
    @SuppressWarnings("unchecked")
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   151
    final Spliterator.OfDouble lazySpliterator(Supplier<? extends Spliterator<Double>> supplier) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   152
        return new StreamSpliterators.DelegatingSpliterator.OfDouble((Supplier<Spliterator.OfDouble>) supplier);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   153
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   154
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   155
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   156
    final void forEachWithCancel(Spliterator<Double> spliterator, Sink<Double> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   157
        Spliterator.OfDouble spl = adapt(spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   158
        DoubleConsumer adaptedSink = adapt(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   159
        do { } while (!sink.cancellationRequested() && spl.tryAdvance(adaptedSink));
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   163
    final  Node.Builder<Double> makeNodeBuilder(long exactSizeIfKnown, IntFunction<Double[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   164
        return Nodes.doubleBuilder(exactSizeIfKnown);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   165
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   166
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   167
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   168
    // DoubleStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   169
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   170
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   171
    public final PrimitiveIterator.OfDouble iterator() {
18155
889970e5b728 8015792: Rename Spliterators.spliteratorFromIterator to Spliterators.iterator
psandoz
parents: 17182
diff changeset
   172
        return Spliterators.iterator(spliterator());
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   173
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   174
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   175
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   176
    public final Spliterator.OfDouble spliterator() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   177
        return adapt(super.spliterator());
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   178
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   179
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   180
    // Stateless intermediate ops from DoubleStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   181
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   182
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   183
    public final Stream<Double> boxed() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   184
        return mapToObj(Double::valueOf);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   185
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   186
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   187
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   188
    public final DoubleStream map(DoubleUnaryOperator mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   189
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   190
        return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   191
                                       StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   192
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   193
            Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   194
                return new Sink.ChainedDouble<Double>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   195
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   196
                    public void accept(double t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   197
                        downstream.accept(mapper.applyAsDouble(t));
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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   204
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   205
    public final <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   206
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   207
        return new ReferencePipeline.StatelessOp<Double, U>(this, StreamShape.DOUBLE_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   208
                                                            StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   209
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   210
            Sink<Double> opWrapSink(int flags, Sink<U> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   211
                return new Sink.ChainedDouble<U>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   212
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   213
                    public void accept(double t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   214
                        downstream.accept(mapper.apply(t));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   215
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   216
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   217
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   218
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   219
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   220
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   221
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   222
    public final IntStream mapToInt(DoubleToIntFunction mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   223
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   224
        return new IntPipeline.StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   225
                                                   StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   226
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   227
            Sink<Double> opWrapSink(int flags, Sink<Integer> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   228
                return new Sink.ChainedDouble<Integer>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   229
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   230
                    public void accept(double t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   231
                        downstream.accept(mapper.applyAsInt(t));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   232
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   233
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   234
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   235
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   236
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   237
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   238
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   239
    public final LongStream mapToLong(DoubleToLongFunction mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   240
        Objects.requireNonNull(mapper);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   241
        return new LongPipeline.StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   242
                                                    StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   243
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   244
            Sink<Double> opWrapSink(int flags, Sink<Long> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   245
                return new Sink.ChainedDouble<Long>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   246
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   247
                    public void accept(double t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   248
                        downstream.accept(mapper.applyAsLong(t));
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
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   252
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   253
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   254
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   255
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   256
    public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   257
        return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   258
                                        StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   259
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   260
            Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   261
                return new Sink.ChainedDouble<Double>(sink) {
18528
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   262
                    @Override
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   263
                    public void begin(long size) {
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   264
                        downstream.begin(-1);
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   265
                    }
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   266
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   267
                    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   268
                    public void accept(double t) {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19593
diff changeset
   269
                        try (DoubleStream result = mapper.apply(t)) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19593
diff changeset
   270
                            // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19593
diff changeset
   271
                            if (result != null)
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19593
diff changeset
   272
                                result.sequential().forEach(i -> downstream.accept(i));
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19593
diff changeset
   273
                        }
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   274
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   275
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   276
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   277
        };
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   281
    public DoubleStream unordered() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   282
        if (!isOrdered())
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   283
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   284
        return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_ORDERED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   285
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   286
            Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   287
                return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   288
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   289
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   290
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   291
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   292
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   293
    public final DoubleStream filter(DoublePredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   294
        Objects.requireNonNull(predicate);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   295
        return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   296
                                       StreamOpFlag.NOT_SIZED) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   297
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   298
            Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   299
                return new Sink.ChainedDouble<Double>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   300
                    @Override
18528
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   301
                    public void begin(long size) {
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   302
                        downstream.begin(-1);
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   303
                    }
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   304
9b02581cf079 8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents: 18171
diff changeset
   305
                    @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   306
                    public void accept(double t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   307
                        if (predicate.test(t))
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   308
                            downstream.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   309
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   310
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   311
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   312
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   313
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   314
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   315
    @Override
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   316
    public final DoubleStream peek(DoubleConsumer action) {
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   317
        Objects.requireNonNull(action);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   318
        return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   319
                                       0) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   320
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   321
            Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 19220
diff changeset
   322
                return new Sink.ChainedDouble<Double>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   323
                    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   324
                    public void accept(double t) {
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   325
                        action.accept(t);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   326
                        downstream.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   327
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   328
                };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   329
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   330
        };
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
    // Stateful intermediate ops from DoubleStream
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 final DoubleStream limit(long maxSize) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   337
        if (maxSize < 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   338
            throw new IllegalArgumentException(Long.toString(maxSize));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   339
        return SliceOps.makeDouble(this, (long) 0, maxSize);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   340
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   341
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   342
    @Override
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   343
    public final DoubleStream skip(long n) {
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   344
        if (n < 0)
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   345
            throw new IllegalArgumentException(Long.toString(n));
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   346
        if (n == 0)
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   347
            return this;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   348
        else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   349
            long limit = -1;
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   350
            return SliceOps.makeDouble(this, n, limit);
17182
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   355
    public final DoubleStream sorted() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   356
        return SortedOps.makeDouble(this);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   357
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   358
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   359
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   360
    public final DoubleStream distinct() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   361
        // While functional and quick to implement, this approach is not very efficient.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   362
        // An efficient version requires a double-specific map/set implementation.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   363
        return boxed().distinct().mapToDouble(i -> (double) i);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   364
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   365
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   366
    // Terminal ops from DoubleStream
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 forEach(DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   370
        evaluate(ForEachOps.makeDouble(consumer, false));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   371
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   372
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   373
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   374
    public void forEachOrdered(DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   375
        evaluate(ForEachOps.makeDouble(consumer, true));
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 final double sum() {
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   380
        /*
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   381
         * In the arrays allocated for the collect operation, index 0
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   382
         * holds the high-order bits of the running sum and index 1
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   383
         * holds the low-order bits of the sum computed via
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   384
         * compensated summation.
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   385
         */
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   386
        double[] summation = collect(() -> new double[2],
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   387
                               (ll, d) -> {
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   388
                                   Collectors.sumWithCompensation(ll, d);
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   389
                               },
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   390
                               (ll, rr) -> {
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   391
                                   Collectors.sumWithCompensation(ll, rr[0]);
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   392
                                   Collectors.sumWithCompensation(ll, rr[1]);
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   393
                               });
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   394
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   395
        // Better error bounds to add both terms as the final sum
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   396
        return summation[0] + summation[1];
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   397
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   398
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   399
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   400
    public final OptionalDouble min() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   401
        return reduce(Math::min);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   402
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   403
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   404
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   405
    public final OptionalDouble max() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   406
        return reduce(Math::max);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   407
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   408
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   409
    /**
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   410
     * {@inheritDoc}
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   411
     *
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   412
     * @implNote The {@code double} format can represent all
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   413
     * consecutive integers in the range -2<sup>53</sup> to
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   414
     * 2<sup>53</sup>. If the pipeline has more than 2<sup>53</sup>
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   415
     * values, the divisor in the average computation will saturate at
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   416
     * 2<sup>53</sup>, leading to additional numerical errors.
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   417
     */
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   418
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   419
    public final OptionalDouble average() {
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   420
        /*
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   421
         * In the arrays allocated for the collect operation, index 0
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   422
         * holds the high-order bits of the running sum, index 1 holds
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   423
         * the low-order bits of the sum computed via compensated
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   424
         * summation, and index 2 holds the number of values seen.
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   425
         */
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   426
        double[] avg = collect(() -> new double[3],
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   427
                               (ll, d) -> {
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   428
                                   ll[2]++;
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   429
                                   Collectors.sumWithCompensation(ll, d);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   430
                               },
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   431
                               (ll, rr) -> {
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   432
                                   Collectors.sumWithCompensation(ll, rr[0]);
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   433
                                   Collectors.sumWithCompensation(ll, rr[1]);
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   434
                                   ll[2] += rr[2];
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   435
                               });
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   436
        return avg[2] > 0
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   437
            // Better error bounds to add both terms as the final sum to compute average
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   438
            ? OptionalDouble.of((avg[0] + avg[1]) / avg[2])
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   439
            : OptionalDouble.empty();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   440
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   441
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   442
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   443
    public final long count() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   444
        return mapToObj(e -> null).mapToInt(e -> 1).sum();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   445
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   446
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   447
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   448
    public final DoubleSummaryStatistics summaryStatistics() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   449
        return collect(DoubleSummaryStatistics::new, DoubleSummaryStatistics::accept,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   450
                       DoubleSummaryStatistics::combine);
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   454
    public final double reduce(double identity, DoubleBinaryOperator op) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   455
        return evaluate(ReduceOps.makeDouble(identity, op));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   456
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   457
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   458
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   459
    public final OptionalDouble reduce(DoubleBinaryOperator op) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   460
        return evaluate(ReduceOps.makeDouble(op));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   461
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   462
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   463
    @Override
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   464
    public final <R> R collect(Supplier<R> supplier,
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   465
                               ObjDoubleConsumer<R> accumulator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   466
                               BiConsumer<R, R> combiner) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   467
        BinaryOperator<R> operator = (left, right) -> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   468
            combiner.accept(left, right);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   469
            return left;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   470
        };
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   471
        return evaluate(ReduceOps.makeDouble(supplier, accumulator, operator));
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   472
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   473
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   474
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   475
    public final boolean anyMatch(DoublePredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   476
        return evaluate(MatchOps.makeDouble(predicate, MatchOps.MatchKind.ANY));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   477
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   478
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   479
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   480
    public final boolean allMatch(DoublePredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   481
        return evaluate(MatchOps.makeDouble(predicate, MatchOps.MatchKind.ALL));
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
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   485
    public final boolean noneMatch(DoublePredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   486
        return evaluate(MatchOps.makeDouble(predicate, MatchOps.MatchKind.NONE));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   487
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   488
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   489
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   490
    public final OptionalDouble findFirst() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   491
        return evaluate(FindOps.makeDouble(true));
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 final OptionalDouble findAny() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   496
        return evaluate(FindOps.makeDouble(false));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   497
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   498
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   499
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   500
    public final double[] toArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   501
        return Nodes.flattenDouble((Node.OfDouble) evaluateToArrayNode(Double[]::new))
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   502
                        .asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   503
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   504
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   505
    //
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   506
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   507
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   508
     * Source stage of a DoubleStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   509
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   510
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   511
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   512
    static class Head<E_IN> extends DoublePipeline<E_IN> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   513
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   514
         * Constructor for the source stage of a DoubleStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   515
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   516
         * @param source {@code Supplier<Spliterator>} describing the stream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   517
         *               source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   518
         * @param sourceFlags the source flags for the stream source, described
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   519
         *                    in {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   520
         * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   521
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   522
        Head(Supplier<? extends Spliterator<Double>> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   523
             int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   524
            super(source, sourceFlags, parallel);
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
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   528
         * Constructor for the source stage of a DoubleStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   529
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   530
         * @param source {@code Spliterator} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   531
         * @param sourceFlags the source flags for the stream source, described
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   532
         *                    in {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   533
         * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   534
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   535
        Head(Spliterator<Double> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   536
             int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   537
            super(source, sourceFlags, parallel);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   538
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   539
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   540
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   541
        final boolean opIsStateful() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   542
            throw new UnsupportedOperationException();
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
        final Sink<E_IN> opWrapSink(int flags, Sink<Double> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   547
            throw new UnsupportedOperationException();
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
        // Optimized sequential terminal operations for the head of the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   551
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   552
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   553
        public void forEach(DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   554
            if (!isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   555
                adapt(sourceStageSpliterator()).forEachRemaining(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   556
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   557
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   558
                super.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   559
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   560
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   561
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   562
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   563
        public void forEachOrdered(DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   564
            if (!isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   565
                adapt(sourceStageSpliterator()).forEachRemaining(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   566
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   567
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   568
                super.forEachOrdered(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   569
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   570
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   571
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   572
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   573
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   574
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   575
     * Base class for a stateless intermediate stage of a DoubleStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   576
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   577
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   578
     * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   579
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   580
    abstract static class StatelessOp<E_IN> extends DoublePipeline<E_IN> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   581
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   582
         * Construct a new DoubleStream by appending a stateless intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   583
         * operation to an existing stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   584
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   585
         * @param upstream the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   586
         * @param inputShape the stream shape for the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   587
         * @param opFlags operation flags for the new stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   588
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   589
        StatelessOp(AbstractPipeline<?, E_IN, ?> upstream,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   590
                    StreamShape inputShape,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   591
                    int opFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   592
            super(upstream, opFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   593
            assert upstream.getOutputShape() == inputShape;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   594
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   595
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   596
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   597
        final boolean opIsStateful() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   598
            return false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   599
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   600
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   601
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   602
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   603
     * Base class for a stateful intermediate stage of a DoubleStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   604
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   605
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   606
     * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   607
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   608
    abstract static class StatefulOp<E_IN> extends DoublePipeline<E_IN> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   609
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   610
         * Construct a new DoubleStream by appending a stateful intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   611
         * operation to an existing stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   612
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   613
         * @param upstream the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   614
         * @param inputShape the stream shape for the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   615
         * @param opFlags operation flags for the new stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   616
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   617
        StatefulOp(AbstractPipeline<?, E_IN, ?> upstream,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   618
                   StreamShape inputShape,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   619
                   int opFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   620
            super(upstream, opFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   621
            assert upstream.getOutputShape() == inputShape;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   622
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   623
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   624
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   625
        final boolean opIsStateful() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   626
            return true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   627
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   628
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   629
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   630
        abstract <P_IN> Node<Double> opEvaluateParallel(PipelineHelper<Double> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   631
                                                        Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   632
                                                        IntFunction<Double[]> generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   633
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   634
}