Java Code Examples for java.util.function.DoubleBinaryOperator#applyAsDouble()

The following examples show how to use java.util.function.DoubleBinaryOperator#applyAsDouble() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParallelPrefix.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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));
}
 
Example 2
Source File: ReduceOps.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
        }
    };
}
 
Example 3
Source File: ReduceOps.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
        }
    };
}
 
Example 4
Source File: ParallelPrefix.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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));
}
 
Example 5
Source File: FloatStamp.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example 6
Source File: ParallelPrefix.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@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));
}
 
Example 7
Source File: ReduceOps.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
        }
    };
}
 
Example 8
Source File: ReduceOps.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
        }
    };
}
 
Example 9
Source File: ReduceOps.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
        }
    };
}
 
Example 10
Source File: ReduceOps.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
        }
    };
}
 
Example 11
Source File: ReduceOps.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * 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();
        }
    };
}
 
Example 12
Source File: NodeEvaluator.java    From gyro with Apache License 2.0 5 votes vote down vote up
private static Object doArithmetic(
    Object left,
    Object right,
    DoubleBinaryOperator doubleOperator,
    LongBinaryOperator longOperator) {
    if (left == null || right == null) {
        throw new GyroException("Can't do arithmetic with a null!");
    }

    Number leftNumber = NumberUtils.createNumber(left.toString());

    if (leftNumber == null) {
        throw new GyroException(String.format(
            "Can't do arithmetic on @|bold %s|@, an instance of @|bold %s|@, because it's not a number!",
            left,
            left.getClass().getName()));
    }

    Number rightNumber = NumberUtils.createNumber(right.toString());

    if (rightNumber == null) {
        throw new GyroException(String.format(
            "Can't do arithmetic on @|bold %s|@, an instance of @|bold %s|@, because it's not a number!",
            right,
            right.getClass().getName()));
    }

    if (leftNumber instanceof Float
        || leftNumber instanceof Double
        || rightNumber instanceof Float
        || rightNumber instanceof Double) {

        return doubleOperator.applyAsDouble(leftNumber.doubleValue(), rightNumber.doubleValue());

    } else {
        return longOperator.applyAsLong(leftNumber.longValue(), rightNumber.longValue());
    }
}
 
Example 13
Source File: ReduceOps.java    From desugar_jdk_libs with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
        }
    };
}
 
Example 14
Source File: Striped64.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static long apply(DoubleBinaryOperator fn, long v, double x) {
    double d = Double.longBitsToDouble(v);
    d = (fn == null) ? d + x : fn.applyAsDouble(d, x);
    return Double.doubleToRawLongBits(d);
}
 
Example 15
Source File: ReduceOps.java    From desugar_jdk_libs with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs a {@code TerminalOp} that implements a functional reduce on
 * {@code double} values, producing an optional double result.
 *
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Double, OptionalDouble>
makeDouble(DoubleBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Double, OptionalDouble, ReducingSink>, Sink.OfDouble {
        private boolean empty;
        private double state;

        public void begin(long size) {
            empty = true;
            state = 0;
        }

        @Override
        public void accept(double t) {
            if (empty) {
                empty = false;
                state = t;
            }
            else {
                state = operator.applyAsDouble(state, t);
            }
        }

        @Override
        public OptionalDouble get() {
            return empty ? OptionalDouble.empty() : OptionalDouble.of(state);
        }

        @Override
        public void combine(ReducingSink other) {
            if (!other.empty)
                accept(other.state);
        }
    }
    return new ReduceOp<Double, OptionalDouble, ReducingSink>(StreamShape.DOUBLE_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example 16
Source File: ReduceOps.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Constructs a {@code TerminalOp} that implements a functional reduce on
 * {@code double} values, producing an optional double result.
 *
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Double, OptionalDouble>
makeDouble(DoubleBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Double, OptionalDouble, ReducingSink>, Sink.OfDouble {
        private boolean empty;
        private double state;

        public void begin(long size) {
            empty = true;
            state = 0;
        }

        @Override
        public void accept(double t) {
            if (empty) {
                empty = false;
                state = t;
            }
            else {
                state = operator.applyAsDouble(state, t);
            }
        }

        @Override
        public OptionalDouble get() {
            return empty ? OptionalDouble.empty() : OptionalDouble.of(state);
        }

        @Override
        public void combine(ReducingSink other) {
            if (!other.empty)
                accept(other.state);
        }
    }
    return new ReduceOp<Double, OptionalDouble, ReducingSink>(StreamShape.DOUBLE_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example 17
Source File: ReduceOps.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs a {@code TerminalOp} that implements a functional reduce on
 * {@code double} values, producing an optional double result.
 *
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Double, OptionalDouble>
makeDouble(DoubleBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Double, OptionalDouble, ReducingSink>, Sink.OfDouble {
        private boolean empty;
        private double state;

        public void begin(long size) {
            empty = true;
            state = 0;
        }

        @Override
        public void accept(double t) {
            if (empty) {
                empty = false;
                state = t;
            }
            else {
                state = operator.applyAsDouble(state, t);
            }
        }

        @Override
        public OptionalDouble get() {
            return empty ? OptionalDouble.empty() : OptionalDouble.of(state);
        }

        @Override
        public void combine(ReducingSink other) {
            if (!other.empty)
                accept(other.state);
        }
    }
    return new ReduceOp<Double, OptionalDouble, ReducingSink>(StreamShape.DOUBLE_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example 18
Source File: ReduceOps.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs a {@code TerminalOp} that implements a functional reduce on
 * {@code double} values, producing an optional double result.
 *
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Double, OptionalDouble>
makeDouble(DoubleBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Double, OptionalDouble, ReducingSink>, Sink.OfDouble {
        private boolean empty;
        private double state;

        public void begin(long size) {
            empty = true;
            state = 0;
        }

        @Override
        public void accept(double t) {
            if (empty) {
                empty = false;
                state = t;
            }
            else {
                state = operator.applyAsDouble(state, t);
            }
        }

        @Override
        public OptionalDouble get() {
            return empty ? OptionalDouble.empty() : OptionalDouble.of(state);
        }

        @Override
        public void combine(ReducingSink other) {
            if (!other.empty)
                accept(other.state);
        }
    }
    return new ReduceOp<Double, OptionalDouble, ReducingSink>(StreamShape.DOUBLE_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example 19
Source File: Striped64.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private static long apply(DoubleBinaryOperator fn, long v, double x) {
    double d = Double.longBitsToDouble(v);
    d = (fn == null) ? d + x : fn.applyAsDouble(d, x);
    return Double.doubleToRawLongBits(d);
}
 
Example 20
Source File: ReduceOps.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs a {@code TerminalOp} that implements a functional reduce on
 * {@code double} values, producing an optional double result.
 *
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Double, OptionalDouble>
makeDouble(DoubleBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Double, OptionalDouble, ReducingSink>, Sink.OfDouble {
        private boolean empty;
        private double state;

        public void begin(long size) {
            empty = true;
            state = 0;
        }

        @Override
        public void accept(double t) {
            if (empty) {
                empty = false;
                state = t;
            }
            else {
                state = operator.applyAsDouble(state, t);
            }
        }

        @Override
        public OptionalDouble get() {
            return empty ? OptionalDouble.empty() : OptionalDouble.of(state);
        }

        @Override
        public void combine(ReducingSink other) {
            if (!other.empty)
                accept(other.state);
        }
    }
    return new ReduceOp<Double, OptionalDouble, ReducingSink>(StreamShape.DOUBLE_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}