Search in sources :

Example 1 with LongBuffer

use of one.util.streamex.Internals.LongBuffer in project streamex by amaembo.

the class LongStreamEx method scanLeft.

/**
 * Produces an array containing cumulative results of applying the
 * accumulation function going left to right.
 *
 * <p>
 * This is a terminal operation.
 *
 * <p>
 * For parallel stream it's not guaranteed that accumulator will always be
 * executed in the same thread.
 *
 * <p>
 * This method cannot take all the advantages of parallel streams as it must
 * process elements strictly left to right.
 *
 * @param accumulator a
 *        <a href="package-summary.html#NonInterference">non-interfering
 *        </a>, <a href="package-summary.html#Statelessness">stateless</a>
 *        function for incorporating an additional element into a result
 * @return the array where the first element is the first element of this
 *         stream and every successor element is the result of applying
 *         accumulator function to the previous array element and the
 *         corresponding stream element. The resulting array has the same
 *         length as this stream.
 * @see #foldLeft(LongBinaryOperator)
 * @since 0.5.1
 */
public long[] scanLeft(LongBinaryOperator accumulator) {
    Spliterator.OfLong spliterator = spliterator();
    int size = intSize(spliterator);
    LongBuffer buf = new LongBuffer(size >= 0 ? size : INITIAL_SIZE);
    delegate(spliterator).forEachOrdered(i -> buf.add(buf.size == 0 ? i : accumulator.applyAsLong(buf.data[buf.size - 1], i)));
    return buf.toArray();
}
Also used : LongBuffer(one.util.streamex.Internals.LongBuffer) Spliterator(java.util.Spliterator)

Aggregations

Spliterator (java.util.Spliterator)1 LongBuffer (one.util.streamex.Internals.LongBuffer)1