Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code of WithFirstSpliterator #232

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .idea/benchmark.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__net_sf_jopt_simple_jopt_simple_4_6.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_apache_commons_commons_math3_3_2.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions .idea/libraries/Maven__org_openjdk_jmh_jmh_core_1_21.xml

This file was deleted.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_openjdk_jmh_jmh_core_1_25.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions benchmark/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.21</version>
<version>1.25</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.21</version>
<version>1.25</version>
</dependency>
</dependencies>
<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2015, 2020 StreamEx contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package one.util.streamex.benchmark.withFirst;

import one.util.streamex.IntStreamEx;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

@State(Scope.Benchmark)
public class WithFirstBenchmark {
@Param({"100000"})
private int N;

@Benchmark
public Object[] parallelOld() {
return IntStreamEx.range(N).boxed().parallel().withFirstOld().toArray();
}

@Benchmark
public boolean parallelOldShortCircuit() {
return IntStreamEx.range(N).boxed().parallel().withFirstOld().anyMatch(x -> x.getKey() == -1);
}

@Benchmark
public Object[] parallelNew() {
return IntStreamEx.range(N).boxed().parallel().withFirst().toArray();
}

@Benchmark
public boolean parallelNewShortCircuit() {
return IntStreamEx.range(N).boxed().parallel().withFirst().anyMatch(x -> x.getKey() == -1);
}

@Benchmark
public Object[] sequentialOld() {
return IntStreamEx.range(N).boxed().withFirstOld().toArray();
}

@Benchmark
public boolean sequentialOldShortCircuit() {
return IntStreamEx.range(N).boxed().withFirstOld().anyMatch(x -> x.getKey() == -1);
}

@Benchmark
public Object[] sequentialNew() {
return IntStreamEx.range(N).boxed().withFirst().toArray();
}

@Benchmark
public boolean sequentialNewShortCircuit() {
return IntStreamEx.range(N).boxed().withFirst().anyMatch(x -> x.getKey() == -1);
}

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(WithFirstBenchmark.class.getSimpleName())
.build();

new Runner(opt).run();
}
}
23 changes: 13 additions & 10 deletions src/main/java/one/util/streamex/StreamEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -1764,11 +1764,6 @@ public <U> StreamEx<U> intervalMap(BiPredicate<? super T, ? super T> sameInterva
* <p>
* This is a <a href="package-summary.html#StreamOps">quasi-intermediate
* operation</a>.
*
* <p>
* The size of the resulting stream is one element less than the input
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, indeed this should be changed. I forgot to update this part in version 0.6.4 when semantics of withFirst changed. However, it's better to say that the size remains the same than removing this completely. I will apply this change separately.

* stream. If the input stream is empty or contains just one element, then
* the output stream will be empty.
*
* @param <R> The element type of the new stream
* @param mapper a non-interfering, stateless function to apply to the first
Expand All @@ -1791,11 +1786,6 @@ public <R> StreamEx<R> withFirst(BiFunction<? super T, ? super T, ? extends R> m
* <p>
* This is a <a href="package-summary.html#StreamOps">quasi-intermediate
* operation</a>.
*
* <p>
* The size of the resulting stream is one element less than the input
* stream. If the input stream is empty or contains just one element, then
* the output stream will be empty.
*
* @return the new stream
* @see #withFirst(BiFunction)
Expand All @@ -1807,6 +1797,19 @@ public EntryStream<T, T> withFirst() {
SimpleImmutableEntry::new);
return new EntryStream<>(spliterator, context);
}

@Deprecated
public <R> StreamEx<R> withFirstOld(BiFunction<? super T, ? super T, ? extends R> mapper) {
WithFirstSpliteratorOld<T, R> spliterator = new WithFirstSpliteratorOld<>(spliterator(), mapper);
return new StreamEx<>(spliterator, context);
}

@Deprecated
public EntryStream<T, T> withFirstOld() {
WithFirstSpliteratorOld<T, Entry<T, T>> spliterator = new WithFirstSpliteratorOld<>(spliterator(),
SimpleImmutableEntry::new);
return new EntryStream<>(spliterator, context);
}

/**
* Creates a new {@link StreamEx} which is the result of applying of the
Expand Down
Loading