下面列出了怎么用java.util.function.DoubleBinaryOperator的API类实例代码及写法,或者点击链接到github查看源代码。
public void testDoubleMethods() {
BinaryOperator<Double> sum1 = Double::sum;
DoubleBinaryOperator sum2 = Double::sum;
BinaryOperator<Double> max1 = Double::max;
DoubleBinaryOperator max2 = Double::max;
BinaryOperator<Double> min1 = Double::min;
DoubleBinaryOperator min2 = Double::min;
Comparator<Double> cmp = Double::compare;
double[] numbers = { -1, 0, 1, 100, Double.MAX_VALUE, Double.MIN_VALUE };
for (double i : numbers) {
for (double j : numbers) {
assertEquals(i+j, (double) sum1.apply(i, j));
assertEquals(i+j, sum2.applyAsDouble(i, j));
assertEquals(Math.max(i,j), (double) max1.apply(i, j));
assertEquals(Math.max(i,j), max2.applyAsDouble(i, j));
assertEquals(Math.min(i,j), (double) min1.apply(i, j));
assertEquals(Math.min(i,j), min2.applyAsDouble(i, j));
assertEquals(((Double) i).compareTo(j), cmp.compare(i, j));
}
}
}
public void testDoubleMethods() {
BinaryOperator<Double> sum1 = Double::sum;
DoubleBinaryOperator sum2 = Double::sum;
BinaryOperator<Double> max1 = Double::max;
DoubleBinaryOperator max2 = Double::max;
BinaryOperator<Double> min1 = Double::min;
DoubleBinaryOperator min2 = Double::min;
Comparator<Double> cmp = Double::compare;
double[] numbers = { -1, 0, 1, 100, Double.MAX_VALUE, Double.MIN_VALUE };
for (double i : numbers) {
for (double j : numbers) {
assertEquals(i+j, (double) sum1.apply(i, j));
assertEquals(i+j, sum2.applyAsDouble(i, j));
assertEquals(Math.max(i,j), (double) max1.apply(i, j));
assertEquals(Math.max(i,j), max2.applyAsDouble(i, j));
assertEquals(Math.min(i,j), (double) min1.apply(i, j));
assertEquals(Math.min(i,j), min2.applyAsDouble(i, j));
assertEquals(((Double) i).compareTo(j), cmp.compare(i, j));
}
}
}
public void testDoubleMethods() {
BinaryOperator<Double> sum1 = Double::sum;
DoubleBinaryOperator sum2 = Double::sum;
BinaryOperator<Double> max1 = Double::max;
DoubleBinaryOperator max2 = Double::max;
BinaryOperator<Double> min1 = Double::min;
DoubleBinaryOperator min2 = Double::min;
Comparator<Double> cmp = Double::compare;
double[] numbers = { -1, 0, 1, 100, Double.MAX_VALUE, Double.MIN_VALUE };
for (double i : numbers) {
for (double j : numbers) {
assertEquals(i+j, (double) sum1.apply(i, j));
assertEquals(i+j, sum2.applyAsDouble(i, j));
assertEquals(Math.max(i,j), (double) max1.apply(i, j));
assertEquals(Math.max(i,j), max2.applyAsDouble(i, j));
assertEquals(Math.min(i,j), (double) min1.apply(i, j));
assertEquals(Math.min(i,j), min2.applyAsDouble(i, j));
assertEquals(((Double) i).compareTo(j), cmp.compare(i, j));
}
}
}
private Matrix join(final Matrix matrix, final boolean isTwoWay,
final DoubleBinaryOperator op) {
if (!this.isSameSize(matrix)) {
throw new IllegalArgumentException("matrix sizes must be equal");
}
final Matrix result = this.create(this.getRowCount(), this.getColumnCount());
this.forEach(
(r, c, v) -> result
.setAtUnchecked(r, c, op.applyAsDouble(v, matrix.getAtUnchecked(r, c))));
if (isTwoWay) {
matrix.forEach(
(r, c, v) -> result
.setAtUnchecked(r, c, op.applyAsDouble(v, this.getAtUnchecked(r, c))));
}
return result;
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code double} values.
*
* @param identity the identity for the combining function
* @param operator the combining function
* @return a {@code TerminalOp} implementing the reduction
*/
public static TerminalOp<Double, Double>
makeDouble(double identity, DoubleBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink
implements AccumulatingSink<Double, Double, ReducingSink>, Sink.OfDouble {
private double state;
@Override
public void begin(long size) {
state = identity;
}
@Override
public void accept(double t) {
state = operator.applyAsDouble(state, t);
}
@Override
public Double get() {
return state;
}
@Override
public void combine(ReducingSink other) {
accept(other.state);
}
}
return new ReduceOp<Double, Double, ReducingSink>(StreamShape.DOUBLE_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
@Test(dataProvider="doubleSet")
public void testParallelPrefixForDouble(double[] data, int fromIndex, int toIndex, DoubleBinaryOperator op) {
double[] sequentialResult = data.clone();
for (int index = fromIndex + 1; index < toIndex; index++) {
sequentialResult[index ] = op.applyAsDouble(sequentialResult[index - 1], sequentialResult[index]);
}
double[] parallelResult = data.clone();
Arrays.parallelPrefix(parallelResult, fromIndex, toIndex, op);
assertArraysEqual(parallelResult, sequentialResult);
double[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex);
Arrays.parallelPrefix(parallelRangeResult, op);
assertArraysEqual(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex));
}
/** Root task constructor */
public DoubleCumulateTask(DoubleCumulateTask parent,
DoubleBinaryOperator function,
double[] array, int lo, int hi) {
super(parent);
this.function = function; this.array = array;
this.lo = this.origin = lo; this.hi = this.fence = hi;
int p;
this.threshold =
(p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
<= MIN_PARTITION ? MIN_PARTITION : p;
}
/** Subtask constructor */
DoubleCumulateTask(DoubleCumulateTask parent, DoubleBinaryOperator function,
double[] array, int origin, int fence, int threshold,
int lo, int hi) {
super(parent);
this.function = function; this.array = array;
this.origin = origin; this.fence = fence;
this.threshold = threshold;
this.lo = lo; this.hi = hi;
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code double} values.
*
* @param identity the identity for the combining function
* @param operator the combining function
* @return a {@code TerminalOp} implementing the reduction
*/
public static TerminalOp<Double, Double>
makeDouble(double identity, DoubleBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink
implements AccumulatingSink<Double, Double, ReducingSink>, Sink.OfDouble {
private double state;
@Override
public void begin(long size) {
state = identity;
}
@Override
public void accept(double t) {
state = operator.applyAsDouble(state, t);
}
@Override
public Double get() {
return state;
}
@Override
public void combine(ReducingSink other) {
accept(other.state);
}
}
return new ReduceOp<Double, Double, ReducingSink>(StreamShape.DOUBLE_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/** Subtask constructor */
DoubleCumulateTask(DoubleCumulateTask parent, DoubleBinaryOperator function,
double[] array, int origin, int fence, int threshold,
int lo, int hi) {
super(parent);
this.function = function; this.array = array;
this.origin = origin; this.fence = fence;
this.threshold = threshold;
this.lo = lo; this.hi = hi;
}
/** Subtask constructor */
DoubleCumulateTask(DoubleCumulateTask parent, DoubleBinaryOperator function,
double[] array, int origin, int fence, int threshold,
int lo, int hi) {
super(parent);
this.function = function; this.array = array;
this.origin = origin; this.fence = fence;
this.threshold = threshold;
this.lo = lo; this.hi = hi;
}
@Test(dataProvider="doubleSet")
public void testParallelPrefixForDouble(double[] data, int fromIndex, int toIndex, DoubleBinaryOperator op) {
double[] sequentialResult = data.clone();
for (int index = fromIndex + 1; index < toIndex; index++) {
sequentialResult[index ] = op.applyAsDouble(sequentialResult[index - 1], sequentialResult[index]);
}
double[] parallelResult = data.clone();
Arrays.parallelPrefix(parallelResult, fromIndex, toIndex, op);
assertEquals(parallelResult, sequentialResult);
double[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex);
Arrays.parallelPrefix(parallelRangeResult, op);
assertEquals(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex));
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code double} values.
*
* @param identity the identity for the combining function
* @param operator the combining function
* @return a {@code TerminalOp} implementing the reduction
*/
public static TerminalOp<Double, Double>
makeDouble(double identity, DoubleBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink
implements AccumulatingSink<Double, Double, ReducingSink>, Sink.OfDouble {
private double state;
@Override
public void begin(long size) {
state = identity;
}
@Override
public void accept(double t) {
state = operator.applyAsDouble(state, t);
}
@Override
public Double get() {
return state;
}
@Override
public void combine(ReducingSink other) {
accept(other.state);
}
}
return new ReduceOp<Double, Double, ReducingSink>(StreamShape.DOUBLE_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/** Subtask constructor */
DoubleCumulateTask(DoubleCumulateTask parent, DoubleBinaryOperator function,
double[] array, int origin, int fence, int threshold,
int lo, int hi) {
super(parent);
this.function = function; this.array = array;
this.origin = origin; this.fence = fence;
this.threshold = threshold;
this.lo = lo; this.hi = hi;
}
@DataProvider
public static Object[][] doubleSet(){
return genericData(size -> IntStream.range(0, size).mapToDouble(i -> (double)i).toArray(),
new DoubleBinaryOperator[]{
Double::sum,
Double::min});
}
static void testDoubleAccumulator() {
DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
a.accumulate(17.5d);
DoubleAccumulator result = echo(a);
if (result.get() != a.get())
throw new RuntimeException("Unexpected value");
a.reset();
result.reset();
if (result.get() != a.get())
throw new RuntimeException("Unexpected value after reset");
checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
SerializationProxy(double value,
DoubleBinaryOperator function,
long identity) {
this.value = value;
this.function = function;
this.identity = identity;
}
/** Subtask constructor */
DoubleCumulateTask(DoubleCumulateTask parent, DoubleBinaryOperator function,
double[] array, int origin, int fence, int threshold,
int lo, int hi) {
super(parent);
this.function = function; this.array = array;
this.origin = origin; this.fence = fence;
this.threshold = threshold;
this.lo = lo; this.hi = hi;
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code double} values.
*
* @param identity the identity for the combining function
* @param operator the combining function
* @return a {@code TerminalOp} implementing the reduction
*/
public static TerminalOp<Double, Double>
makeDouble(double identity, DoubleBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink
implements AccumulatingSink<Double, Double, ReducingSink>, Sink.OfDouble {
private double state;
@Override
public void begin(long size) {
state = identity;
}
@Override
public void accept(double t) {
state = operator.applyAsDouble(state, t);
}
@Override
public Double get() {
return state;
}
@Override
public void combine(ReducingSink other) {
accept(other.state);
}
}
return new ReduceOp<Double, Double, ReducingSink>(StreamShape.DOUBLE_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
private static double meetBounds(double a, double b, DoubleBinaryOperator op) {
if (Double.isNaN(a)) {
return b;
} else if (Double.isNaN(b)) {
return a;
} else {
return op.applyAsDouble(a, b);
}
}
@Test(dataProvider="doubleSet")
public void testParallelPrefixForDouble(double[] data, int fromIndex, int toIndex, DoubleBinaryOperator op) {
double[] sequentialResult = data.clone();
for (int index = fromIndex + 1; index < toIndex; index++) {
sequentialResult[index ] = op.applyAsDouble(sequentialResult[index - 1], sequentialResult[index]);
}
double[] parallelResult = data.clone();
Arrays.parallelPrefix(parallelResult, fromIndex, toIndex, op);
assertEquals(parallelResult, sequentialResult);
double[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex);
Arrays.parallelPrefix(parallelRangeResult, op);
assertEquals(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex));
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code double} values.
*
* @param identity the identity for the combining function
* @param operator the combining function
* @return a {@code TerminalOp} implementing the reduction
*/
public static TerminalOp<Double, Double>
makeDouble(double identity, DoubleBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink
implements AccumulatingSink<Double, Double, ReducingSink>, Sink.OfDouble {
private double state;
@Override
public void begin(long size) {
state = identity;
}
@Override
public void accept(double t) {
state = operator.applyAsDouble(state, t);
}
@Override
public Double get() {
return state;
}
@Override
public void combine(ReducingSink other) {
accept(other.state);
}
}
return new ReduceOp<Double, Double, ReducingSink>(StreamShape.DOUBLE_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/** Root task constructor */
public DoubleCumulateTask(DoubleCumulateTask parent,
DoubleBinaryOperator function,
double[] array, int lo, int hi) {
super(parent);
this.function = function; this.array = array;
this.lo = this.origin = lo; this.hi = this.fence = hi;
int p;
this.threshold =
(p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
<= MIN_PARTITION ? MIN_PARTITION : p;
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code double} values.
*
* @param identity the identity for the combining function
* @param operator the combining function
* @return a {@code TerminalOp} implementing the reduction
*/
public static TerminalOp<Double, Double>
makeDouble(double identity, DoubleBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink
implements AccumulatingSink<Double, Double, ReducingSink>, Sink.OfDouble {
private double state;
@Override
public void begin(long size) {
state = identity;
}
@Override
public void accept(double t) {
state = operator.applyAsDouble(state, t);
}
@Override
public Double get() {
return state;
}
@Override
public void combine(ReducingSink other) {
accept(other.state);
}
}
return new ReduceOp<Double, Double, ReducingSink>(StreamShape.DOUBLE_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code double} values.
*
* @param identity the identity for the combining function
* @param operator the combining function
* @return a {@code TerminalOp} implementing the reduction
*/
public static TerminalOp<Double, Double>
makeDouble(double identity, DoubleBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink
implements AccumulatingSink<Double, Double, ReducingSink>, Sink.OfDouble {
private double state;
@Override
public void begin(long size) {
state = identity;
}
@Override
public void accept(double t) {
state = operator.applyAsDouble(state, t);
}
@Override
public Double get() {
return state;
}
@Override
public void combine(ReducingSink other) {
accept(other.state);
}
}
return new ReduceOp<Double, Double, ReducingSink>(StreamShape.DOUBLE_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/** Subtask constructor */
DoubleCumulateTask(DoubleCumulateTask parent, DoubleBinaryOperator function,
double[] array, int origin, int fence, int threshold,
int lo, int hi) {
super(parent);
this.function = function; this.array = array;
this.origin = origin; this.fence = fence;
this.threshold = threshold;
this.lo = lo; this.hi = hi;
}
/** Subtask constructor */
DoubleCumulateTask(DoubleCumulateTask parent, DoubleBinaryOperator function,
double[] array, int origin, int fence, int threshold,
int lo, int hi) {
super(parent);
this.function = function; this.array = array;
this.origin = origin; this.fence = fence;
this.threshold = threshold;
this.lo = lo; this.hi = hi;
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code double} values.
*
* @param identity the identity for the combining function
* @param operator the combining function
* @return a {@code TerminalOp} implementing the reduction
*/
public static TerminalOp<Double, Double>
makeDouble(double identity, DoubleBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink
implements AccumulatingSink<Double, Double, ReducingSink>, Sink.OfDouble {
private double state;
@Override
public void begin(long size) {
state = identity;
}
@Override
public void accept(double t) {
state = operator.applyAsDouble(state, t);
}
@Override
public Double get() {
return state;
}
@Override
public void combine(ReducingSink other) {
accept(other.state);
}
}
return new ReduceOp<Double, Double, ReducingSink>(StreamShape.DOUBLE_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code double} values.
*
* @param identity the identity for the combining function
* @param operator the combining function
* @return a {@code TerminalOp} implementing the reduction
*/
public static TerminalOp<Double, Double>
makeDouble(double identity, DoubleBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink
implements AccumulatingSink<Double, Double, ReducingSink>, Sink.OfDouble {
private double state;
@Override
public void begin(long size) {
state = identity;
}
@Override
public void accept(double t) {
state = operator.applyAsDouble(state, t);
}
@Override
public Double get() {
return state;
}
@Override
public void combine(ReducingSink other) {
accept(other.state);
}
}
return new ReduceOp<Double, Double, ReducingSink>(StreamShape.DOUBLE_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}