下面列出了java.util.function.DoubleFunction#apply ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedDouble<Double>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(double t) {
try (DoubleStream result = mapper.apply(t)) {
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
if (result != null)
result.sequential().forEach(i -> downstream.accept(i));
}
}
};
}
};
}
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedDouble<Double>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(double t) {
try (DoubleStream result = mapper.apply(t)) {
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
if (result != null)
result.sequential().forEach(i -> downstream.accept(i));
}
}
};
}
};
}
public static double binarySearch(
double lo, double hi, double target, DoubleFunction<Double> function) {
if (hi <= lo) throw new IllegalArgumentException("hi should be greater than lo");
double mid;
do {
// Find the middle point
mid = (hi + lo) / 2.0;
// Compute the value of our function for the middle point
// Note that f can be any function not just the square root function
double value = function.apply(mid);
if (value > target) {
hi = mid;
} else {
lo = mid;
}
} while ((hi - lo) > EPS);
return mid;
}
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedDouble<Double>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(double t) {
try (DoubleStream result = mapper.apply(t)) {
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
if (result != null)
result.sequential().forEach(i -> downstream.accept(i));
}
}
};
}
};
}
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedDouble<Double>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(double t) {
try (DoubleStream result = mapper.apply(t)) {
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
if (result != null)
result.sequential().forEach(i -> downstream.accept(i));
}
}
};
}
};
}
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedDouble<Double>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(double t) {
try (DoubleStream result = mapper.apply(t)) {
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
if (result != null)
result.sequential().forEach(i -> downstream.accept(i));
}
}
};
}
};
}
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedDouble<Double>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(double t) {
try (DoubleStream result = mapper.apply(t)) {
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
if (result != null)
result.sequential().forEach(i -> downstream.accept(i));
}
}
};
}
};
}
private static <T> void readDoubleColumn(Object[] vals, int fieldIdx, DoubleColumnVector vector,
int childCount, DoubleFunction<T> reader) {
if (vector.isRepeating) { // fill complete column with first value
if (vector.isNull[0]) {
// fill vals with null values
fillColumnWithRepeatingValue(vals, fieldIdx, null, childCount);
} else {
// read repeating non-null value by forwarding call
readNonNullDoubleColumn(vals, fieldIdx, vector, childCount, reader);
}
} else {
boolean[] isNullVector = vector.isNull;
if (fieldIdx == -1) { // set as an object
for (int i = 0; i < childCount; i++) {
if (isNullVector[i]) {
vals[i] = null;
} else {
vals[i] = reader.apply(vector.vector[i]);
}
}
} else { // set as a field of Row
Row[] rows = (Row[]) vals;
for (int i = 0; i < childCount; i++) {
if (isNullVector[i]) {
rows[i].setField(fieldIdx, null);
} else {
rows[i].setField(fieldIdx, reader.apply(vector.vector[i]));
}
}
}
}
}
private static void addDPToSeries(XYSeries series, int[] bins, int i, double binwidth, double min,
double max, DoubleFunction<Double> function) {
// adds a data point to the series
double x = min + (binwidth / 2.0) + i * binwidth;
if (function != null)
x = function.apply(x);
series.add(x, bins[i]);
}
private static void addDPToList(List<DataPoint> list, int[] bins, int i, double binwidth,
double min, double max, DoubleFunction<Double> function) {
// adds a data point to the series
double x = min + (binwidth / 2.0) + i * binwidth;
if (function != null)
x = function.apply(x);
list.add(new SimpleDataPoint(x, bins[i]));
}
/**
* Creates an DOUBLE Printer that wraps the function provided
* @param function the function to wrap
* @return the newly created function Printer
*/
public static Printer<Double> forDouble(DoubleFunction<String> function) {
return new Printer<Double>(FunctionStyle.DOUBLE, DEFAULT_NULL) {
@Override
public final String apply(double input) {
return function.apply(input);
}
};
}
private static <T> void readDoubleColumn(Object[] vals, int fieldIdx, DoubleColumnVector vector,
int childCount, DoubleFunction<T> reader) {
if (vector.isRepeating) { // fill complete column with first value
if (vector.isNull[0]) {
// fill vals with null values
fillColumnWithRepeatingValue(vals, fieldIdx, null, childCount);
} else {
// read repeating non-null value by forwarding call
readNonNullDoubleColumn(vals, fieldIdx, vector, childCount, reader);
}
} else {
boolean[] isNullVector = vector.isNull;
if (fieldIdx == -1) { // set as an object
for (int i = 0; i < childCount; i++) {
if (isNullVector[i]) {
vals[i] = null;
} else {
vals[i] = reader.apply(vector.vector[i]);
}
}
} else { // set as a field of Row
Row[] rows = (Row[]) vals;
for (int i = 0; i < childCount; i++) {
if (isNullVector[i]) {
rows[i].setField(fieldIdx, null);
} else {
rows[i].setField(fieldIdx, reader.apply(vector.vector[i]));
}
}
}
}
}
/**
* Creates an LONG function that wraps to function provided
* @param function the function to wrap
* @param <O> the output type
* @return the newly created function wrapper
*/
public static <O> Function2<Double,O> fromDouble(DoubleFunction<O> function) {
return new Function2<Double,O>(FunctionStyle.DOUBLE) {
@Override
public final O apply(double input) {
return function.apply(input);
}
};
}
/** Check a rotation transform for consistency against a variety of points and rotation angles.
* @param factory function used to create a rotation transform from an input angle
* @param transformFn function that accepts the transform and a point and returns
* the transformed point
*/
private static <T extends EuclideanTransform<Vector2D>> void checkRotate(
DoubleFunction<T> factory, BiFunction<T, Vector2D, Vector2D> transformFn) {
// check zero
T transform = factory.apply(0);
EuclideanTestUtils.assertCoordinatesEqual(Vector2D.ZERO, transformFn.apply(transform, Vector2D.ZERO), TEST_EPS);
// check a variety of non-zero points
EuclideanTestUtils.permuteSkipZero(-2, -2, 1, (x, y) -> {
checkRotatePoint(Vector2D.of(x, y), factory, transformFn);
});
}
/** Check a rotation transform for consistency when transforming a single point against a
* variety of rotation angles.
* @param pt point to transform
* @param factory function used to create a rotation transform from an input angle
* @param transformFn function that accepts the transform and a point and returns
* the transformed point
*/
private static <T extends EuclideanTransform<Vector2D>> void checkRotatePoint(
Vector2D pt, DoubleFunction<T> factory, BiFunction<T, Vector2D, Vector2D> transformFn) {
// arrange
double limit = 4 * Math.PI;
double inc = 0.25;
Line line = Lines.fromPointAndDirection(Vector2D.ZERO, pt, TEST_PRECISION);
T transform;
Vector2D resultPt;
Line resultLine;
for (double angle = -limit; angle < limit; angle += inc) {
transform = factory.apply(angle);
// act
resultPt = transformFn.apply(transform, pt);
// assert
// check that the norm is unchanged
Assert.assertEquals(pt.norm(), resultPt.norm(), TEST_EPS);
resultLine = Lines.fromPointAndDirection(Vector2D.ZERO, resultPt, TEST_PRECISION);
double lineAngle = line.angle(resultLine);
// check that the angle is what we expect
Assert.assertEquals(PlaneAngleRadians.normalizeBetweenMinusPiAndPi(angle), lineAngle, TEST_EPS);
}
}
public LinearlyScaledFunction(final DoubleFunction<Double> baseFunction, final double x1, final double y1, final double x2, final double y2) {
super();
this.baseFunction = baseFunction;
this.mapping = new AffineFunction(baseFunction.apply(x1), y1, baseFunction.apply(x2), y2);
}
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedDouble<Double>(sink) {
// true if cancellationRequested() has been called
boolean cancellationRequestedCalled;
// cache the consumer to avoid creation on every accepted element
DoubleConsumer downstreamAsDouble = downstream::accept;
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(double t) {
try (DoubleStream result = mapper.apply(t)) {
if (result != null) {
if (!cancellationRequestedCalled) {
result.sequential().forEach(downstreamAsDouble);
}
else {
Spliterator.OfDouble s = result.sequential().spliterator();
do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsDouble));
}
}
}
}
@Override
public boolean cancellationRequested() {
// If this method is called then an operation within the stream
// pipeline is short-circuiting (see AbstractPipeline.copyInto).
// Note that we cannot differentiate between an upstream or
// downstream operation
cancellationRequestedCalled = true;
return downstream.cancellationRequested();
}
};
}
};
}
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedDouble<Double>(sink) {
// true if cancellationRequested() has been called
boolean cancellationRequestedCalled;
// cache the consumer to avoid creation on every accepted element
DoubleConsumer downstreamAsDouble = downstream::accept;
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(double t) {
try (DoubleStream result = mapper.apply(t)) {
if (result != null) {
if (!cancellationRequestedCalled) {
result.sequential().forEach(downstreamAsDouble);
}
else {
Spliterator.OfDouble s = result.sequential().spliterator();
do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsDouble));
}
}
}
}
@Override
public boolean cancellationRequested() {
// If this method is called then an operation within the stream
// pipeline is short-circuiting (see AbstractPipeline.copyInto).
// Note that we cannot differentiate between an upstream or
// downstream operation
cancellationRequestedCalled = true;
return downstream.cancellationRequested();
}
};
}
};
}
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedDouble<Double>(sink) {
// true if cancellationRequested() has been called
boolean cancellationRequestedCalled;
// cache the consumer to avoid creation on every accepted element
DoubleConsumer downstreamAsDouble = downstream::accept;
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(double t) {
try (DoubleStream result = mapper.apply(t)) {
if (result != null) {
if (!cancellationRequestedCalled) {
result.sequential().forEach(downstreamAsDouble);
}
else {
Spliterator.OfDouble s = result.sequential().spliterator();
do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsDouble));
}
}
}
}
@Override
public boolean cancellationRequested() {
// If this method is called then an operation within the stream
// pipeline is short-circuiting (see AbstractPipeline.copyInto).
// Note that we cannot differentiate between an upstream or
// downstream operation
cancellationRequestedCalled = true;
return downstream.cancellationRequested();
}
};
}
};
}
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedDouble<Double>(sink) {
// true if cancellationRequested() has been called
boolean cancellationRequestedCalled;
// cache the consumer to avoid creation on every accepted element
DoubleConsumer downstreamAsDouble = downstream::accept;
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(double t) {
try (DoubleStream result = mapper.apply(t)) {
if (result != null) {
if (!cancellationRequestedCalled) {
result.sequential().forEach(downstreamAsDouble);
}
else {
var s = result.sequential().spliterator();
do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsDouble));
}
}
}
}
@Override
public boolean cancellationRequested() {
// If this method is called then an operation within the stream
// pipeline is short-circuiting (see AbstractPipeline.copyInto).
// Note that we cannot differentiate between an upstream or
// downstream operation
cancellationRequestedCalled = true;
return downstream.cancellationRequested();
}
};
}
};
}