提交 4a117002 编写于 作者: P psandoz

8015492: Remove DoubleStream.range methods

Reviewed-by: alanb
上级 bcb20e82
......@@ -753,75 +753,4 @@ public interface DoubleStream extends BaseStream<Double, DoubleStream> {
},
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL));
}
/**
* Returns a sequential {@code DoubleStream} from {@code startInclusive} (inclusive)
* to {@code endExclusive} (exclusive) by an incremental step of 1.0.
*
* @implSpec
* The implementation behaves as if:
* <pre>{@code
* doubleRange(startInclusive, endExclusive, 1.0);
* }</pre>
*
* @param startInclusive the (inclusive) initial value
* @param endExclusive the exclusive upper bound
* @return a sequential {@code DoubleStream} for the range of {@code double}
* elements
*/
public static DoubleStream range(double startInclusive, double endExclusive) {
return range(startInclusive, endExclusive, 1.0);
}
/**
* Returns a sequential {@code DoubleStream} from {@code startInclusive}
* (inclusive) to {@code endExclusive} (exclusive) by {@code step}. If
* {@code startInclusive} is greater than or equal to {@code
* endExclusive}, an empty stream is returned.
*
* An equivalent sequence of increasing values can be produced
* sequentially using a {@code for} loop as follows:
* <pre>{@code
* long size = (long) Math.ceil((startInclusive - endExclusive) / step);
* long i = 0
* for (double v = startInclusive; i < size; i++, v = startInclusive + step * i) {
* ...
* }
* }</pre>
*
* @param startInclusive the (inclusive) initial value
* @param endExclusive the exclusive upper bound
* @param step the difference between consecutive values
* @return a sequential {@code DoubleStream} for tne range of {@code double}
* elements
* @throws IllegalArgumentException if {@code step} is less than or equal to
* 0. is {@code NaN}, or the count of elements in the range would be
* greater than {@code Long.MAX_VALUE}
*/
public static DoubleStream range(double startInclusive, double endExclusive, double step) {
// @@@ Need to check for ranges that may not produce distinct values
// such as when the step is very small
// Also clarify the size of the range which may produce more or less
// than expected
if (step <= 0 || Double.isNaN(step)) {
throw new IllegalArgumentException(String.format("Illegal step: %f", step));
} else {
double range = endExclusive - startInclusive;
if (range <= 0) {
return empty();
}
double size = Math.ceil((endExclusive - startInclusive) / step);
if (Double.isNaN(size)) {
throw new IllegalArgumentException(
String.format("Illegal range: %f size is NaN", size));
} else if (size > Long.MAX_VALUE) {
throw new IllegalArgumentException(
String.format("Illegal range: size %f > Long.MAX_VALUE", size));
} else {
return StreamSupport.doubleStream(
new Streams.RangeDoubleSpliterator(
startInclusive, endExclusive, step, 0, (long) size));
}
}
}
}
......@@ -192,87 +192,6 @@ class Streams {
}
}
/**
* A {@code double} range spliterator.
*
* <p>The traversing and splitting logic is equivalent to that of
* {@code RangeLongSpliterator} for increasing values with a {@code step} of
* {@code 1}.
*
* <p>A {@code double} value is calculated from the function
* {@code start + i * step} where {@code i} is the absolute position of the
* value when traversing an instance of this class that has not been split.
* This ensures the same values are produced at the same absolute positions
* regardless of how an instance of this class is split or traversed.
*/
static final class RangeDoubleSpliterator implements Spliterator.OfDouble {
private final double from;
private final double upTo;
private final double step;
private long lFrom;
private final long lUpTo;
RangeDoubleSpliterator(double from, double upTo, double step, long lFrom, long lUpTo) {
this.from = from;
this.upTo = upTo;
this.step = step;
this.lFrom = lFrom;
this.lUpTo = lUpTo;
}
@Override
public boolean tryAdvance(DoubleConsumer consumer) {
boolean hasNext = lFrom < lUpTo;
if (hasNext) {
consumer.accept(from + lFrom * step);
lFrom++;
}
return hasNext;
}
@Override
public void forEachRemaining(DoubleConsumer consumer) {
double hOrigin = from;
double hStep = step;
long hLUpTo = lUpTo;
long i = lFrom;
for (; i < hLUpTo; i++) {
consumer.accept(hOrigin + i * hStep);
}
lFrom = i;
}
@Override
public long estimateSize() {
return lUpTo - lFrom;
}
@Override
public int characteristics() {
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.IMMUTABLE | Spliterator.NONNULL |
Spliterator.DISTINCT | Spliterator.SORTED;
}
@Override
public Comparator<? super Double> getComparator() {
return null;
}
@Override
public Spliterator.OfDouble trySplit() {
return estimateSize() <= 1
? null
: new RangeDoubleSpliterator(from, upTo, step, lFrom, lFrom = lFrom + midPoint());
}
private long midPoint() {
// Size is known to be >= 2
return (lUpTo - lFrom) / 2;
}
}
private static abstract class AbstractStreamBuilderImpl<T, S extends Spliterator<T>> implements Spliterator<T> {
// >= 0 when building, < 0 when built
// -1 == no elements
......
......@@ -92,15 +92,6 @@ public class DoubleStreamTestDataProvider {
}
list.add(new Object[]{"SpinedList:" + name,
TestData.Factory.ofSpinedBuffer("SpinedList:" + name, isl)});
list.add(streamDataDescr("Primitives.range(0,l): " + doubles.length,
() -> DoubleStream.range(0, doubles.length)));
list.add(streamDataDescr("Primitives.range(0,l,2): " + doubles.length,
() -> DoubleStream.range(0, doubles.length, 2)));
list.add(streamDataDescr("Primitives.range(0,l,3): " + doubles.length,
() -> DoubleStream.range(0, doubles.length, 3)));
list.add(streamDataDescr("Primitives.range(0,l,7): " + doubles.length,
() -> DoubleStream.range(0, doubles.length, 7)));
}
testData = list.toArray(new Object[0][]);
}
......@@ -128,15 +119,6 @@ public class DoubleStreamTestDataProvider {
() -> Spliterators.spliterator(isl.iterator(), doubles.length, 0)));
spliterators.add(splitDescr("Primitives.s(SpinedBuffer.iterator()):" + name,
() -> Spliterators.spliteratorUnknownSize(isl.iterator(), 0)));
spliterators.add(splitDescr("Primitives.range(0,l):" + name,
() -> DoubleStream.range(0, doubles.length).spliterator()));
spliterators.add(splitDescr("Primitives.range(0,l,2):" + name,
() -> DoubleStream.range(0, doubles.length, 2).spliterator()));
spliterators.add(splitDescr("Primitives.range(0,l,3):" + name,
() -> DoubleStream.range(0, doubles.length, 3).spliterator()));
spliterators.add(splitDescr("Primitives.range(0,l,7):" + name,
() -> DoubleStream.range(0, doubles.length, 7).spliterator()));
// Need more!
}
spliteratorTestData = spliterators.toArray(new Object[0][]);
......@@ -144,10 +126,6 @@ public class DoubleStreamTestDataProvider {
}
static <T> Object[] streamDataDescr(String description, Supplier<DoubleStream> s) {
return new Object[] { description, TestData.Factory.ofDoubleSupplier(description, s) };
}
static <T> Object[] splitDescr(String description, Supplier<Spliterator.OfDouble> s) {
return new Object[] { description, s };
}
......
......@@ -110,7 +110,7 @@ public class ExplodeOpTest extends OpTestCase {
result = exerciseOps(data, s -> DoubleStream.empty());
assertEquals(0, result.size());
exerciseOps(data, s -> s.flatMap(e -> DoubleStream.range(0, e)));
exerciseOps(data, s -> s.flatMap(e -> DoubleStream.range(0, e).limit(10)));
exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, (int) e).doubles()));
exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, (int) e).limit(10).doubles()));
}
}
......@@ -205,7 +205,7 @@ public class ForEachOpTest extends OpTestCase {
public void testDoubleForEachOrdered() {
List<Integer> input = countTo(10000);
TestData.OfDouble data = TestData.Factory.ofDoubleSupplier("[1, 10000]",
() -> DoubleStream.range(1, 10001));
() -> IntStream.range(1, 10001).doubles());
Function<DoubleStream, List<Integer>> terminalFunc = s -> {
List<Integer> l = new ArrayList<>();
......
......@@ -221,124 +221,6 @@ public class RangeTest extends OpTestCase {
//
public void testDoubleRangeErrors() {
for (double start : Arrays.asList(1, 10, -1, -10)) {
for (double end : Arrays.asList(1, 10, -1, -10)) {
for (double step : Arrays.asList(0.0, +0.0, -0.0, 1.0, -1.0, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY)) {
try {
if (step > 0)
executeAndNoCatch(() -> DoubleStream.range(start, end, step));
else
executeAndCatch(() -> DoubleStream.range(start, end, step));
}
catch (AssertionError e) {
System.out.printf("start=%f, end=%f, step=%f%n", start, end, step);
throw e;
}
}
}
}
for (double start : Arrays.asList(0.0, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN)) {
for (double end : Arrays.asList(0.0, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN)) {
for (double step : Arrays.asList(1.0, -1.0, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN)) {
try {
if ((start == 0.0 && end == 0.0 && step > 0)
|| (start > end && step > 0)) {
executeAndNoCatch(() -> DoubleStream.range(start, end, step));
}
else {
executeAndCatch(() -> DoubleStream.range(start, end, step));
}
}
catch (AssertionError e) {
System.out.printf("start=%f, end=%f, step=%f%n", start, end, step);
throw e;
}
}
}
}
}
public void testDoubleRange() {
// Without step
for (double start : Arrays.asList(1, 1000, -1, -1000)) {
for (double end : Arrays.asList(1, 1000, -1, -1000)) {
double step = 1;
double size = start < end ? Math.ceil((end - start) / step) : 0;
double[] exp = new double[(int) size];
for (long i = 0; i < size; i++) {
exp[(int) i] = start + i * step;
}
double[] inc = DoubleStream.range(start, end).toArray();
assertEquals(inc.length, (int) size);
assertTrue(Arrays.equals(exp, inc));
withData(doubleRangeData(start, end, step)).stream(s -> s).
expectedResult(exp).exercise();
}
}
// With step
for (double start : Arrays.asList(1, 1000, -1, -1000)) {
for (double end : Arrays.asList(1, 1000, -1, -1000)) {
for (double step : Arrays.asList(1, -1, -2, 2)) {
if (step <= 0)
continue;
double size = start < end ? Math.ceil((end - start) / step) : 0;
double[] exp = new double[(int) size];
for (long i = 0; i < size; i++) {
exp[(int) i] = start + i * step;
}
double[] inc = DoubleStream.range(start, end, step).toArray();
assertEquals(inc.length, (int) size);
assertTrue(Arrays.equals(exp, inc));
withData(doubleRangeData(start, end, step)).stream(s -> s).
expectedResult(exp).exercise();
}
}
}
// With non-integer values
for (double step : Arrays.asList(Math.PI / 1000.0, Math.PI / 1000.0, Math.PI / 10000.0)) {
double start = -Math.PI;
double end = Math.PI;
double size = start < end ? Math.ceil((end - start) / step) : 0;
double[] exp = new double[(int) size];
for (long i = 0; i < size; i++) {
exp[(int) i] = start + i * step;
}
withData(doubleRangeData(start, end, step)).stream(s -> s).
expectedResult(exp).exercise();
}
}
TestData.OfDouble doubleRangeData(double start, double end, double step) {
return TestData.Factory.ofDoubleSupplier("double range", () -> DoubleStream.range(start, end, step));
}
public void tesDoubleRangeReduce() {
withData(doubleRangeData(0, 10000, 1)).
terminal(s -> s.reduce(0, Double::sum)).exercise();
}
public void testDoubleInfiniteRangeLimit() {
withData(TestData.Factory.ofDoubleSupplier(
"double range", () -> DoubleStream.iterate(0, i -> i + 1).limit(10000))).
terminal(s -> s.reduce(0, Double::sum)).exercise();
}
public void testDoubleInfiniteRangeFindFirst() {
double first = DoubleStream.iterate(0, i -> i + 1).filter(i -> i > 10000).findFirst().getAsDouble();
assertEquals(first, DoubleStream.iterate(0, i -> i + 1).parallel().filter(i -> i > 10000).findFirst().getAsDouble());
}
//
private static int[] reverse(int[] a) {
int[] b = new int[a.length];
for (int i = 0; i < a.length; i++) {
......
......@@ -275,7 +275,7 @@ public class StreamBuilderTest extends OpTestCase {
@Test(dataProvider = "sizes")
public void testDoubleAfterBuilding(int size) {
StreamBuilder.OfDouble sb = DoubleStream.builder();
DoubleStream.range(0, size).forEach(sb);
IntStream.range(0, size).doubles().forEach(sb);
sb.build();
checkISE(() -> sb.accept(1));
......@@ -287,13 +287,13 @@ public class StreamBuilderTest extends OpTestCase {
public void testDoubleStreamBuilder(int size) {
testDoubleStreamBuilder(size, (s) -> {
StreamBuilder.OfDouble sb = DoubleStream.builder();
DoubleStream.range(0, s).forEach(sb);
IntStream.range(0, s).doubles().forEach(sb);
return sb.build();
});
testDoubleStreamBuilder(size, (s) -> {
StreamBuilder.OfDouble sb = DoubleStream.builder();
DoubleStream.range(0, s).forEach(i -> {
IntStream.range(0, s).doubles().forEach(i -> {
StreamBuilder.OfDouble _sb = sb.add(i);
assertTrue(sb == _sb);
});
......@@ -307,12 +307,12 @@ public class StreamBuilderTest extends OpTestCase {
withData(data).
stream(s -> s).
expectedResult(DoubleStream.range(0, size).toArray()).
expectedResult(IntStream.range(0, size).doubles().toArray()).
exercise();
withData(data).
stream(s -> s.map(i -> i)).
expectedResult(DoubleStream.range(0, size).toArray()).
expectedResult(IntStream.range(0, size).doubles().toArray()).
exercise();
}
......
......@@ -526,7 +526,7 @@ public class StreamSpliteratorTest extends OpTestCase {
for (boolean proxyEstimateSize : new boolean[]{false, true}) {
// Size is assumed to be larger than the target size for no splitting
// @@@ Need way to obtain the target size
Spliterator.OfDouble sp = intermediateOp.apply(DoubleStream.range(0, 1000)).spliterator();
Spliterator.OfDouble sp = intermediateOp.apply(IntStream.range(0, 1000).doubles()).spliterator();
ProxyNoExactSizeSpliterator.OfDouble psp = new ProxyNoExactSizeSpliterator.OfDouble(sp, proxyEstimateSize);
DoubleStream s = StreamSupport.doubleParallelStream(psp);
terminalOp.accept(s);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册