Skip to content

Commit

Permalink
Merge a35aac1 into a56b4a3
Browse files Browse the repository at this point in the history
  • Loading branch information
PromanSEW committed Apr 20, 2018
2 parents a56b4a3 + a35aac1 commit 048ff57
Show file tree
Hide file tree
Showing 54 changed files with 2,911 additions and 121 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
plugins {
id "net.saliman.cobertura" version "2.2.6"
id "com.github.kt3k.coveralls" version "2.8.1"
id "me.champeau.gradle.jmh" version "0.4.4"
}

allprojects {
Expand Down
9 changes: 9 additions & 0 deletions stream/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: 'java-library'
apply plugin: 'maven'
apply plugin: 'me.champeau.gradle.jmh'

archivesBaseName = 'stream'
version = '1.2.0-SNAPSHOT'
Expand Down Expand Up @@ -28,6 +29,14 @@ cobertura {
coverageExcludes = ['.*com\\.annimon\\.stream\\.internal\\.Compat.*']
}

jmh {
benchmarkMode = ['Throughput']
fork = 2
threads = 2
warmupIterations = 3
iterations = 5
}

// maven signing
if (ext.isReleaseVersion) {
apply from: 'signing.gradle'
Expand Down
52 changes: 52 additions & 0 deletions stream/src/jmh/java/com/annimon/stream/FilterIndexedBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.annimon.stream;

import com.annimon.stream.function.IndexedIntPredicate;
import com.annimon.stream.function.IndexedPredicate;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;

@State(Scope.Benchmark)
public class FilterIndexedBenchmark {

private int[] input;

@Param({"10000000"})
public int size;

@Setup
public void setup() {
input = new int[size];
for (int i = 0; i < size; i++) {
input[i] = i % 10;
}
}

@Benchmark
public void boxedFilterIndexed(Blackhole bh) {
bh.consume(IntStream.of(input)
.boxed()
.filterIndexed(new IndexedPredicate<Integer>() {
@Override
public boolean test(int index, Integer value) {
return index != value;
}
})
.count());
}

@Benchmark
public void primitiveFilterIndexed(Blackhole bh) {
bh.consume(IntStream.of(input)
.filterIndexed(new IndexedIntPredicate() {
@Override
public boolean test(int index, int value) {
return index != value;
}
})
.count());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.annimon.stream;

import com.annimon.stream.function.IndexedConsumer;
import com.annimon.stream.function.IndexedIntConsumer;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;

@State(Scope.Benchmark)
public class ForEachIndexedBenchmark {

private int[] input;

@Param({"10000000"})
public int size;

@Setup
public void setup() {
input = new int[size];
for (int i = 0; i < size; i++) {
input[i] = i % 10;
}
}

@Benchmark
public void boxedForEachIndexed(final Blackhole bh) {
IntStream.of(input)
.boxed()
.forEachIndexed(new IndexedConsumer<Integer>() {
@Override
public void accept(int index, Integer value) {
bh.consume(index);
bh.consume(value);
}
});
}

@Benchmark
public void primitiveForEachIndexed(final Blackhole bh) {
IntStream.of(input)
.forEachIndexed(new IndexedIntConsumer() {
@Override
public void accept(int index, int value) {
bh.consume(index);
bh.consume(value);
}
});
}
}
80 changes: 80 additions & 0 deletions stream/src/jmh/java/com/annimon/stream/IndexedBenchmarks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.annimon.stream;

import com.annimon.stream.function.IndexedConsumer;
import com.annimon.stream.function.IndexedFunction;
import com.annimon.stream.function.IndexedIntConsumer;
import com.annimon.stream.function.IndexedIntPredicate;
import com.annimon.stream.function.IndexedPredicate;
import com.annimon.stream.function.IntBinaryOperator;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;

@State(Scope.Benchmark)
public class IndexedBenchmarks {

private int[] input;

@Param({"10000000"})
public int size;

@Setup
public void setup() {
input = new int[size];
for (int i = 0; i < size; i++) {
input[i] = i % 10;
}
}

@Benchmark
public void boxedIndexed(final Blackhole bh) {
IntStream.of(input)
.boxed()
.filterIndexed(new IndexedPredicate<Integer>() {
@Override
public boolean test(int index, Integer value) {
return index % 5 == 0;
}
})
.mapIndexed(new IndexedFunction<Integer, Integer>() {
@Override
public Integer apply(int index, Integer value) {
return (index + value) / 2;
}
})
.forEachIndexed(new IndexedConsumer<Integer>() {
@Override
public void accept(int index, Integer value) {
bh.consume(index);
bh.consume(value);
}
});
}

@Benchmark
public void primitiveIndexed(final Blackhole bh) {
IntStream.of(input)
.filterIndexed(new IndexedIntPredicate() {
@Override
public boolean test(int index, int value) {
return index % 5 == 0;
}
})
.mapIndexed(new IntBinaryOperator() {
@Override
public int applyAsInt(int index, int value) {
return (index + value) / 2;
}
})
.forEachIndexed(new IndexedIntConsumer() {
@Override
public void accept(int index, int value) {
bh.consume(index);
bh.consume(value);
}
});
}
}
133 changes: 133 additions & 0 deletions stream/src/main/java/com/annimon/stream/DoubleStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.annimon.stream.internal.Compose;
import com.annimon.stream.internal.Operators;
import com.annimon.stream.internal.Params;
import com.annimon.stream.iterator.PrimitiveIndexedIterator;
import com.annimon.stream.iterator.PrimitiveIterator;
import com.annimon.stream.operator.*;
import java.io.Closeable;
Expand Down Expand Up @@ -302,6 +303,58 @@ public DoubleStream filter(final DoublePredicate predicate) {
return new DoubleStream(params, new DoubleFilter(iterator, predicate));
}

/**
* Returns a {@code DoubleStream} with elements that satisfy the given {@code IndexedDoublePredicate}.
*
* <p>This is an intermediate operation.
*
* <p>Example:
* <pre>
* predicate: (index, value) -&gt; (index + value) &gt; 6
* stream: [1, 2, 3, 4, 0, 11]
* index: [0, 1, 2, 3, 4, 5]
* sum: [1, 3, 5, 7, 4, 16]
* filter: [ 7, 16]
* result: [4, 11]
* </pre>
*
* @param predicate the {@code IndexedDoublePredicate} used to filter elements
* @return the new stream
* @since 1.2.1
*/
public DoubleStream filterIndexed(IndexedDoublePredicate predicate) {
return filterIndexed(0, 1, predicate);
}

/**
* Returns a {@code DoubleStream} with elements that satisfy the given {@code IndexedDoublePredicate}.
*
* <p>This is an intermediate operation.
*
* <p>Example:
* <pre>
* from: 4
* step: 3
* predicate: (index, value) -&gt; (index + value) &gt; 15
* stream: [1, 2, 3, 4, 0, 11]
* index: [4, 7, 10, 13, 16, 19]
* sum: [5, 9, 13, 17, 16, 30]
* filter: [ 17, 16, 30]
* result: [4, 0, 11]
* </pre>
*
* @param from the initial value of the index (inclusive)
* @param step the step of the index
* @param predicate the {@code IndexedDoublePredicate} used to filter elements
* @return the new stream
* @since 1.2.1
*/
public DoubleStream filterIndexed(int from, int step, IndexedDoublePredicate predicate) {
return new DoubleStream(params, new DoubleFilterIndexed(
new PrimitiveIndexedIterator.OfDouble(from, step, iterator),
predicate));
}

/**
* Returns {@code DoubleStream} with elements that does not satisfy the given predicate.
*
Expand Down Expand Up @@ -335,6 +388,56 @@ public DoubleStream map(final DoubleUnaryOperator mapper) {
return new DoubleStream(params, new DoubleMap(iterator, mapper));
}

/**
* Returns a {@code DoubleStream} with elements that obtained
* by applying the given {@code IndexedDoubleUnaryOperator}.
*
* <p>This is an intermediate operation.
*
* <p>Example:
* <pre>
* mapper: (index, value) -&gt; (index * value)
* stream: [1, 2, 3, 4]
* index: [0, 1, 2, 3]
* result: [0, 2, 6, 12]
* </pre>
*
* @param mapper the mapper function used to apply to each element
* @return the new stream
* @since 1.2.1
*/
public DoubleStream mapIndexed(IndexedDoubleUnaryOperator mapper) {
return mapIndexed(0, 1, mapper);
}

/**
* Returns a {@code DoubleStream} with elements that obtained
* by applying the given {@code IndexedDoubleUnaryOperator}.
*
* <p>This is an intermediate operation.
*
* <p>Example:
* <pre>
* from: -2
* step: 2
* mapper: (index, value) -&gt; (index * value)
* stream: [ 1, 2, 3, 4]
* index: [-2, 0, 2, 4]
* result: [-2, 0, 6, 16]
* </pre>
*
* @param from the initial value of the index (inclusive)
* @param step the step of the index
* @param mapper the mapper function used to apply to each element
* @return the new stream
* @since 1.2.1
*/
public DoubleStream mapIndexed(int from, int step, IndexedDoubleUnaryOperator mapper) {
return new DoubleStream(params, new DoubleMapIndexed(
new PrimitiveIndexedIterator.OfDouble(from, step, iterator),
mapper));
}

/**
* Returns a {@code Stream} consisting of the results of applying the given
* function to the elements of this stream.
Expand Down Expand Up @@ -666,6 +769,36 @@ public void forEach(DoubleConsumer action) {
}
}

/**
* Performs the given indexed action on each element.
*
* <p>This is a terminal operation.
*
* @param action the action to be performed on each element
* @since 1.2.1
*/
public void forEachIndexed(IndexedDoubleConsumer action) {
forEachIndexed(0, 1, action);
}

/**
* Performs the given indexed action on each element.
*
* <p>This is a terminal operation.
*
* @param from the initial value of the index (inclusive)
* @param step the step of the index
* @param action the action to be performed on each element
* @since 1.2.1
*/
public void forEachIndexed(int from, int step, IndexedDoubleConsumer action) {
int index = from;
while (iterator.hasNext()) {
action.accept(index, iterator.nextDouble());
index += step;
}
}

/**
* Performs a reduction on the elements of this stream, using the provided
* identity value and an associative accumulation function, and returns the
Expand Down

0 comments on commit 048ff57

Please sign in to comment.