jdk/src/share/classes/java/util/stream/DoublePipeline.java
author psandoz
Thu, 16 Jan 2014 18:20:31 +0100
changeset 22289 bb9c71b84919
parent 22111 83c31b33708e
child 25526 d3cbdae6e9f9
permissions -rw-r--r--
8029452: Fork/Join task ForEachOps.ForEachOrderedTask clarifications and minor improvements Reviewed-by: mduigou, briangoetz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     1
/*
22111
83c31b33708e 8031187: DoubleStream.count is incorrect for a stream containing > Integer.MAX_VALUE elements
psandoz
parents: 22101
diff changeset
     2
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
17182
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
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   382
         * holds the high-order bits of the running sum, index 1 holds
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   383
         * the low-order bits of the sum computed via compensated
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   384
         * summation, and index 2 holds the simple sum used to compute
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   385
         * the proper result if the stream contains infinite values of
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   386
         * the same sign.
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   387
         */
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   388
        double[] summation = collect(() -> new double[3],
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   389
                               (ll, d) -> {
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   390
                                   Collectors.sumWithCompensation(ll, d);
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   391
                                   ll[2] += d;
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   392
                               },
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   393
                               (ll, rr) -> {
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   394
                                   Collectors.sumWithCompensation(ll, rr[0]);
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   395
                                   Collectors.sumWithCompensation(ll, rr[1]);
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   396
                                   ll[2] += rr[2];
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   397
                               });
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   398
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   399
        return Collectors.computeFinalSum(summation);
17182
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 final OptionalDouble min() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   404
        return reduce(Math::min);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   405
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   406
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   407
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   408
    public final OptionalDouble max() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   409
        return reduce(Math::max);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   410
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   411
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   412
    /**
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   413
     * {@inheritDoc}
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   414
     *
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   415
     * @implNote The {@code double} format can represent all
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   416
     * 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
   417
     * 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
   418
     * 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
   419
     * 2<sup>53</sup>, leading to additional numerical errors.
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   420
     */
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   421
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   422
    public final OptionalDouble average() {
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   423
        /*
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   424
         * 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
   425
         * 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
   426
         * the low-order bits of the sum computed via compensated
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   427
         * summation, index 2 holds the number of values seen, index 3
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   428
         * holds the simple sum.
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   429
         */
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   430
        double[] avg = collect(() -> new double[4],
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   431
                               (ll, d) -> {
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   432
                                   ll[2]++;
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   433
                                   Collectors.sumWithCompensation(ll, d);
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   434
                                   ll[3] += d;
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   435
                               },
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   436
                               (ll, rr) -> {
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   437
                                   Collectors.sumWithCompensation(ll, rr[0]);
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   438
                                   Collectors.sumWithCompensation(ll, rr[1]);
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   439
                                   ll[2] += rr[2];
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   440
                                   ll[3] += rr[3];
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   441
                               });
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   442
        return avg[2] > 0
22101
231247ddf41a 8030212: Several api.java.util.stream tests got "NaN" value instead of "Infinity" or "-Infinity"
darcy
parents: 21946
diff changeset
   443
            ? OptionalDouble.of(Collectors.computeFinalSum(avg) / avg[2])
21946
b4cb3bbeb52a 8006572: DoubleStream.sum() & DoubleSummaryStats implementations that reduce numerical errors
darcy
parents: 20866
diff changeset
   444
            : OptionalDouble.empty();
17182
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 long count() {
22111
83c31b33708e 8031187: DoubleStream.count is incorrect for a stream containing > Integer.MAX_VALUE elements
psandoz
parents: 22101
diff changeset
   449
        return mapToLong(e -> 1L).sum();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   450
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   451
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   452
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   453
    public final DoubleSummaryStatistics summaryStatistics() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   454
        return collect(DoubleSummaryStatistics::new, DoubleSummaryStatistics::accept,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   455
                       DoubleSummaryStatistics::combine);
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 double reduce(double identity, DoubleBinaryOperator op) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   460
        return evaluate(ReduceOps.makeDouble(identity, 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
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   464
    public final OptionalDouble reduce(DoubleBinaryOperator op) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   465
        return evaluate(ReduceOps.makeDouble(op));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   466
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   467
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   468
    @Override
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   469
    public final <R> R collect(Supplier<R> supplier,
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   470
                               ObjDoubleConsumer<R> accumulator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   471
                               BiConsumer<R, R> combiner) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   472
        BinaryOperator<R> operator = (left, right) -> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   473
            combiner.accept(left, right);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   474
            return left;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   475
        };
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   476
        return evaluate(ReduceOps.makeDouble(supplier, accumulator, operator));
17182
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 anyMatch(DoublePredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   481
        return evaluate(MatchOps.makeDouble(predicate, MatchOps.MatchKind.ANY));
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 allMatch(DoublePredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   486
        return evaluate(MatchOps.makeDouble(predicate, MatchOps.MatchKind.ALL));
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 boolean noneMatch(DoublePredicate predicate) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   491
        return evaluate(MatchOps.makeDouble(predicate, MatchOps.MatchKind.NONE));
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 findFirst() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   496
        return evaluate(FindOps.makeDouble(true));
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 OptionalDouble findAny() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   501
        return evaluate(FindOps.makeDouble(false));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   502
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   503
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   504
    @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   505
    public final double[] toArray() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   506
        return Nodes.flattenDouble((Node.OfDouble) evaluateToArrayNode(Double[]::new))
18171
2725a30c1a02 8016251: Balanced spliterator for SpinedBuffer
psandoz
parents: 18155
diff changeset
   507
                        .asPrimitiveArray();
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   508
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   509
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   510
    //
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   511
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   512
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   513
     * Source stage of a DoubleStream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   514
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   515
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   516
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   517
    static class Head<E_IN> extends DoublePipeline<E_IN> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   518
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   519
         * Constructor for the source stage of a DoubleStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   520
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   521
         * @param source {@code Supplier<Spliterator>} describing the stream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   522
         *               source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   523
         * @param sourceFlags the source flags for the stream source, described
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   524
         *                    in {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   525
         * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   526
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   527
        Head(Supplier<? extends Spliterator<Double>> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   528
             int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   529
            super(source, sourceFlags, parallel);
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
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   533
         * Constructor for the source stage of a DoubleStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   534
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   535
         * @param source {@code Spliterator} describing the stream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   536
         * @param sourceFlags the source flags for the stream source, described
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   537
         *                    in {@link StreamOpFlag}
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   538
         * @param parallel {@code true} if the pipeline is parallel
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   539
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   540
        Head(Spliterator<Double> source,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   541
             int sourceFlags, boolean parallel) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   542
            super(source, sourceFlags, parallel);
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 boolean opIsStateful() {
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   551
        final Sink<E_IN> opWrapSink(int flags, Sink<Double> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   552
            throw new UnsupportedOperationException();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   553
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   554
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   555
        // Optimized sequential terminal operations for the head of the pipeline
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   556
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   557
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   558
        public void forEach(DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   559
            if (!isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   560
                adapt(sourceStageSpliterator()).forEachRemaining(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   561
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   562
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   563
                super.forEach(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   564
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   565
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   566
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   567
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   568
        public void forEachOrdered(DoubleConsumer consumer) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   569
            if (!isParallel()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   570
                adapt(sourceStageSpliterator()).forEachRemaining(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   571
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   572
            else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   573
                super.forEachOrdered(consumer);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   574
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   575
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   576
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   577
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   578
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   579
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   580
     * Base class for a stateless intermediate stage of a DoubleStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   581
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   582
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   583
     * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   584
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   585
    abstract static class StatelessOp<E_IN> extends DoublePipeline<E_IN> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   586
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   587
         * Construct a new DoubleStream by appending a stateless intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   588
         * operation to an existing stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   589
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   590
         * @param upstream the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   591
         * @param inputShape the stream shape for the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   592
         * @param opFlags operation flags for the new stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   593
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   594
        StatelessOp(AbstractPipeline<?, E_IN, ?> upstream,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   595
                    StreamShape inputShape,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   596
                    int opFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   597
            super(upstream, opFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   598
            assert upstream.getOutputShape() == inputShape;
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
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   602
        final boolean opIsStateful() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   603
            return false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   604
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   605
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   606
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   607
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   608
     * Base class for a stateful intermediate stage of a DoubleStream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   609
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   610
     * @param <E_IN> type of elements in the upstream source
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   611
     * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   612
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   613
    abstract static class StatefulOp<E_IN> extends DoublePipeline<E_IN> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   614
        /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   615
         * Construct a new DoubleStream by appending a stateful intermediate
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   616
         * operation to an existing stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   617
         *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   618
         * @param upstream the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   619
         * @param inputShape the stream shape for the upstream pipeline stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   620
         * @param opFlags operation flags for the new stage
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   621
         */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   622
        StatefulOp(AbstractPipeline<?, E_IN, ?> upstream,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   623
                   StreamShape inputShape,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   624
                   int opFlags) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   625
            super(upstream, opFlags);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   626
            assert upstream.getOutputShape() == inputShape;
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
        final boolean opIsStateful() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   631
            return true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   632
        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   633
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   634
        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   635
        abstract <P_IN> Node<Double> opEvaluateParallel(PipelineHelper<Double> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   636
                                                        Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   637
                                                        IntFunction<Double[]> generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   638
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   639
}