java.util.function.IntBinaryOperator Java Examples

The following examples show how to use java.util.function.IntBinaryOperator. 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: PrimitiveSumMinMaxTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void testIntMethods() {
    BinaryOperator<Integer> sum1 = Integer::sum;
    IntBinaryOperator sum2 = Integer::sum;
    BinaryOperator<Integer> max1 = Integer::max;
    IntBinaryOperator max2 = Integer::max;
    BinaryOperator<Integer> min1 = Integer::min;
    IntBinaryOperator min2 = Integer::min;
    Comparator<Integer> cmp = Integer::compare;

    int[] numbers = { -1, 0, 1, 100, Integer.MAX_VALUE, Integer.MIN_VALUE };
    for (int i : numbers) {
        for (int j : numbers) {
            assertEquals(i+j, (int) sum1.apply(i, j));
            assertEquals(i+j, sum2.applyAsInt(i, j));
            assertEquals(Math.max(i,j), (int) max1.apply(i, j));
            assertEquals(Math.max(i,j), max2.applyAsInt(i, j));
            assertEquals(Math.min(i,j), (int) min1.apply(i, j));
            assertEquals(Math.min(i,j), min2.applyAsInt(i, j));
            assertEquals(((Integer) i).compareTo(j), cmp.compare(i, j));
        }
    }
}
 
Example #2
Source File: PrimitiveSumMinMaxTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void testIntMethods() {
    BinaryOperator<Integer> sum1 = Integer::sum;
    IntBinaryOperator sum2 = Integer::sum;
    BinaryOperator<Integer> max1 = Integer::max;
    IntBinaryOperator max2 = Integer::max;
    BinaryOperator<Integer> min1 = Integer::min;
    IntBinaryOperator min2 = Integer::min;
    Comparator<Integer> cmp = Integer::compare;

    int[] numbers = { -1, 0, 1, 100, Integer.MAX_VALUE, Integer.MIN_VALUE };
    for (int i : numbers) {
        for (int j : numbers) {
            assertEquals(i+j, (int) sum1.apply(i, j));
            assertEquals(i+j, sum2.applyAsInt(i, j));
            assertEquals(Math.max(i,j), (int) max1.apply(i, j));
            assertEquals(Math.max(i,j), max2.applyAsInt(i, j));
            assertEquals(Math.min(i,j), (int) min1.apply(i, j));
            assertEquals(Math.min(i,j), min2.applyAsInt(i, j));
            assertEquals(((Integer) i).compareTo(j), cmp.compare(i, j));
        }
    }
}
 
Example #3
Source File: PrimitiveSumMinMaxTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public void testIntMethods() {
    BinaryOperator<Integer> sum1 = Integer::sum;
    IntBinaryOperator sum2 = Integer::sum;
    BinaryOperator<Integer> max1 = Integer::max;
    IntBinaryOperator max2 = Integer::max;
    BinaryOperator<Integer> min1 = Integer::min;
    IntBinaryOperator min2 = Integer::min;
    Comparator<Integer> cmp = Integer::compare;

    int[] numbers = { -1, 0, 1, 100, Integer.MAX_VALUE, Integer.MIN_VALUE };
    for (int i : numbers) {
        for (int j : numbers) {
            assertEquals(i+j, (int) sum1.apply(i, j));
            assertEquals(i+j, sum2.applyAsInt(i, j));
            assertEquals(Math.max(i,j), (int) max1.apply(i, j));
            assertEquals(Math.max(i,j), max2.applyAsInt(i, j));
            assertEquals(Math.min(i,j), (int) min1.apply(i, j));
            assertEquals(Math.min(i,j), min2.applyAsInt(i, j));
            assertEquals(((Integer) i).compareTo(j), cmp.compare(i, j));
        }
    }
}
 
Example #4
Source File: ReduceOps.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a {@code TerminalOp} that implements a functional reduce on
 * {@code int} values.
 *
 * @param identity the identity for the combining function
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Integer, Integer>
makeInt(int identity, IntBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Integer, Integer, ReducingSink>, Sink.OfInt {
        private int state;

        @Override
        public void begin(long size) {
            state = identity;
        }

        @Override
        public void accept(int t) {
            state = operator.applyAsInt(state, t);
        }

        @Override
        public Integer get() {
            return state;
        }

        @Override
        public void combine(ReducingSink other) {
            accept(other.state);
        }
    }
    return new ReduceOp<Integer, Integer, ReducingSink>(StreamShape.INT_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example #5
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 int} values.
 *
 * @param identity the identity for the combining function
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Integer, Integer>
makeInt(int identity, IntBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Integer, Integer, ReducingSink>, Sink.OfInt {
        private int state;

        @Override
        public void begin(long size) {
            state = identity;
        }

        @Override
        public void accept(int t) {
            state = operator.applyAsInt(state, t);
        }

        @Override
        public Integer get() {
            return state;
        }

        @Override
        public void combine(ReducingSink other) {
            accept(other.state);
        }
    }
    return new ReduceOp<Integer, Integer, ReducingSink>(StreamShape.INT_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example #6
Source File: ArrayPrefixHelpers.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/** Root task constructor */
public IntCumulateTask(IntCumulateTask parent,
                       IntBinaryOperator function,
                       int[] 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;
}
 
Example #7
Source File: ArrayPrefixHelpers.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/** Subtask constructor */
IntCumulateTask(IntCumulateTask parent, IntBinaryOperator function,
                int[] 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;
}
 
Example #8
Source File: ParallelPrefix.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="intSet")
public void testParallelPrefixForInt(int[] data, int fromIndex, int toIndex, IntBinaryOperator op) {
    int[] sequentialResult = data.clone();
    for (int index = fromIndex + 1; index < toIndex; index++) {
        sequentialResult[index ] = op.applyAsInt(sequentialResult[index  - 1], sequentialResult[index]);
    }

    int[] parallelResult = data.clone();
    Arrays.parallelPrefix(parallelResult, fromIndex, toIndex, op);
    assertEquals(parallelResult, sequentialResult);

    int[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex);
    Arrays.parallelPrefix(parallelRangeResult, op);
    assertEquals(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex));
}
 
Example #9
Source File: ParallelPrefix.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider
public static Object[][] intSet(){
    return genericData(size -> IntStream.range(0, size).toArray(),
            new IntBinaryOperator[]{
                Integer::sum,
                Integer::min});
}
 
Example #10
Source File: ArrayPrefixHelpers.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/** Root task constructor */
public IntCumulateTask(IntCumulateTask parent,
                       IntBinaryOperator function,
                       int[] 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;
}
 
Example #11
Source File: ArrayPrefixHelpers.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/** Subtask constructor */
IntCumulateTask(IntCumulateTask parent, IntBinaryOperator function,
                int[] 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;
}
 
Example #12
Source File: ParallelPrefix.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider
public static Object[][] intSet(){
    return genericData(size -> IntStream.range(0, size).toArray(),
            new IntBinaryOperator[]{
                Integer::sum,
                Integer::min});
}
 
Example #13
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 int} values.
 *
 * @param identity the identity for the combining function
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Integer, Integer>
makeInt(int identity, IntBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Integer, Integer, ReducingSink>, Sink.OfInt {
        private int state;

        @Override
        public void begin(long size) {
            state = identity;
        }

        @Override
        public void accept(int t) {
            state = operator.applyAsInt(state, t);
        }

        @Override
        public Integer get() {
            return state;
        }

        @Override
        public void combine(ReducingSink other) {
            accept(other.state);
        }
    }
    return new ReduceOp<Integer, Integer, ReducingSink>(StreamShape.INT_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example #14
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 int} values.
 *
 * @param identity the identity for the combining function
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Integer, Integer>
makeInt(int identity, IntBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Integer, Integer, ReducingSink>, Sink.OfInt {
        private int state;

        @Override
        public void begin(long size) {
            state = identity;
        }

        @Override
        public void accept(int t) {
            state = operator.applyAsInt(state, t);
        }

        @Override
        public Integer get() {
            return state;
        }

        @Override
        public void combine(ReducingSink other) {
            accept(other.state);
        }
    }
    return new ReduceOp<Integer, Integer, ReducingSink>(StreamShape.INT_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example #15
Source File: ArrayPrefixHelpers.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/** Root task constructor */
public IntCumulateTask(IntCumulateTask parent,
                       IntBinaryOperator function,
                       int[] 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;
}
 
Example #16
Source File: ParallelPrefix.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="intSet")
public void testParallelPrefixForInt(int[] data, int fromIndex, int toIndex, IntBinaryOperator op) {
    int[] sequentialResult = data.clone();
    for (int index = fromIndex + 1; index < toIndex; index++) {
        sequentialResult[index ] = op.applyAsInt(sequentialResult[index  - 1], sequentialResult[index]);
    }

    int[] parallelResult = data.clone();
    Arrays.parallelPrefix(parallelResult, fromIndex, toIndex, op);
    assertEquals(parallelResult, sequentialResult);

    int[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex);
    Arrays.parallelPrefix(parallelRangeResult, op);
    assertEquals(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex));
}
 
Example #17
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 int} values.
 *
 * @param identity the identity for the combining function
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Integer, Integer>
makeInt(int identity, IntBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Integer, Integer, ReducingSink>, Sink.OfInt {
        private int state;

        @Override
        public void begin(long size) {
            state = identity;
        }

        @Override
        public void accept(int t) {
            state = operator.applyAsInt(state, t);
        }

        @Override
        public Integer get() {
            return state;
        }

        @Override
        public void combine(ReducingSink other) {
            accept(other.state);
        }
    }
    return new ReduceOp<Integer, Integer, ReducingSink>(StreamShape.INT_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example #18
Source File: ArrayPrefixHelpers.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/** Root task constructor */
public IntCumulateTask(IntCumulateTask parent,
                       IntBinaryOperator function,
                       int[] 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;
}
 
Example #19
Source File: ArrayPrefixHelpers.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/** Subtask constructor */
IntCumulateTask(IntCumulateTask parent, IntBinaryOperator function,
                int[] 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;
}
 
Example #20
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 int} values.
 *
 * @param identity the identity for the combining function
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Integer, Integer>
makeInt(int identity, IntBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Integer, Integer, ReducingSink>, Sink.OfInt {
        private int state;

        @Override
        public void begin(long size) {
            state = identity;
        }

        @Override
        public void accept(int t) {
            state = operator.applyAsInt(state, t);
        }

        @Override
        public Integer get() {
            return state;
        }

        @Override
        public void combine(ReducingSink other) {
            accept(other.state);
        }
    }
    return new ReduceOp<Integer, Integer, ReducingSink>(StreamShape.INT_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example #21
Source File: ArrayPrefixHelpers.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/** Subtask constructor */
IntCumulateTask(IntCumulateTask parent, IntBinaryOperator function,
                int[] 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;
}
 
Example #22
Source File: ArrayPrefixHelpers.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/** Subtask constructor */
IntCumulateTask(IntCumulateTask parent, IntBinaryOperator function,
                int[] 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;
}
 
Example #23
Source File: ParallelPrefix.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider
public static Object[][] intSet(){
    return genericData(size -> IntStream.range(0, size).toArray(),
            new IntBinaryOperator[]{
                Integer::sum,
                Integer::min});
}
 
Example #24
Source File: ParallelPrefix.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="intSet")
public void testParallelPrefixForInt(int[] data, int fromIndex, int toIndex, IntBinaryOperator op) {
    int[] sequentialResult = data.clone();
    for (int index = fromIndex + 1; index < toIndex; index++) {
        sequentialResult[index ] = op.applyAsInt(sequentialResult[index  - 1], sequentialResult[index]);
    }

    int[] parallelResult = data.clone();
    Arrays.parallelPrefix(parallelResult, fromIndex, toIndex, op);
    assertEquals(parallelResult, sequentialResult);

    int[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex);
    Arrays.parallelPrefix(parallelRangeResult, op);
    assertEquals(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex));
}
 
Example #25
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 int} values, producing an optional integer result.
 *
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Integer, OptionalInt>
makeInt(IntBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Integer, OptionalInt, ReducingSink>, Sink.OfInt {
        private boolean empty;
        private int state;

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

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

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

        @Override
        public void combine(ReducingSink other) {
            if (!other.empty)
                accept(other.state);
        }
    }
    return new ReduceOp<Integer, OptionalInt, ReducingSink>(StreamShape.INT_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example #26
Source File: IntPipeline.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public final int reduce(int identity, IntBinaryOperator op) {
    return evaluate(ReduceOps.makeInt(identity, op));
}
 
Example #27
Source File: IntPipeline.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public final OptionalInt reduce(IntBinaryOperator op) {
    return evaluate(ReduceOps.makeInt(op));
}
 
Example #28
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 int} values, producing an optional integer result.
 *
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Integer, OptionalInt>
makeInt(IntBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Integer, OptionalInt, ReducingSink>, Sink.OfInt {
        private boolean empty;
        private int state;

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

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

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

        @Override
        public void combine(ReducingSink other) {
            if (!other.empty)
                accept(other.state);
        }
    }
    return new ReduceOp<Integer, OptionalInt, ReducingSink>(StreamShape.INT_VALUE) {
        @Override
        public ReducingSink makeSink() {
            return new ReducingSink();
        }
    };
}
 
Example #29
Source File: IntPipeline.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
@Override
public final OptionalInt reduce(IntBinaryOperator op) {
    return evaluate(ReduceOps.makeInt(op));
}
 
Example #30
Source File: ReduceOps.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs a {@code TerminalOp} that implements a functional reduce on
 * {@code int} values, producing an optional integer result.
 *
 * @param operator the combining function
 * @return a {@code TerminalOp} implementing the reduction
 */
public static TerminalOp<Integer, OptionalInt>
makeInt(IntBinaryOperator operator) {
    Objects.requireNonNull(operator);
    class ReducingSink
            implements AccumulatingSink<Integer, OptionalInt, ReducingSink>, Sink.OfInt {
        private boolean empty;
        private int state;

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

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

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

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