Java Code Examples for java.util.function.IntBinaryOperator#applyAsInt()

The following examples show how to use java.util.function.IntBinaryOperator#applyAsInt() . 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 TencentKona-8 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 2
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 3
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 4
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 5
Source File: ReduceOps.java    From TencentKona-8 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 6
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 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 7
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 8
Source File: ReduceOps.java    From openjdk-jdk8u 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 9
Source File: AtomicIntegerFieldUpdater.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Atomically updates the field of the given object managed by this
 * updater with the results of applying the given function to the
 * current and given values, returning the updated value. The
 * function should be side-effect-free, since it may be re-applied
 * when attempted updates fail due to contention among threads.  The
 * function is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param obj An object whose field to get and set
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the updated value
 * @since 1.8
 */
public final int accumulateAndGet(T obj, int x,
                                  IntBinaryOperator accumulatorFunction) {
    int prev, next;
    do {
        prev = get(obj);
        next = accumulatorFunction.applyAsInt(prev, x);
    } while (!compareAndSet(obj, prev, next));
    return next;
}
 
Example 10
Source File: AtomicIntegerArray.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the element at index {@code i} with the
 * results of applying the given function to the current and
 * given values, returning the previous value. The function should
 * be side-effect-free, since it may be re-applied when attempted
 * updates fail due to contention among threads.  The function is
 * applied with the current value at index {@code i} as its first
 * argument, and the given update as the second argument.
 *
 * @param i the index
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the previous value
 * @since 1.8
 */
public final int getAndAccumulate(int i, int x,
                                  IntBinaryOperator accumulatorFunction) {
    long offset = checkedByteOffset(i);
    int prev, next;
    do {
        prev = getRaw(offset);
        next = accumulatorFunction.applyAsInt(prev, x);
    } while (!compareAndSetRaw(offset, prev, next));
    return prev;
}
 
Example 11
Source File: AtomicIntegerFieldUpdater.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the field of the given object managed by this
 * updater with the results of applying the given function to the
 * current and given values, returning the updated value. The
 * function should be side-effect-free, since it may be re-applied
 * when attempted updates fail due to contention among threads.  The
 * function is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param obj An object whose field to get and set
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the updated value
 * @since 1.8
 */
public final int accumulateAndGet(T obj, int x,
                                  IntBinaryOperator accumulatorFunction) {
    int prev, next;
    do {
        prev = get(obj);
        next = accumulatorFunction.applyAsInt(prev, x);
    } while (!compareAndSet(obj, prev, next));
    return next;
}
 
Example 12
Source File: AtomicIntegerFieldUpdater.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the field of the given object managed by this
 * updater with the results of applying the given function to the
 * current and given values, returning the updated value. The
 * function should be side-effect-free, since it may be re-applied
 * when attempted updates fail due to contention among threads.  The
 * function is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param obj An object whose field to get and set
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the updated value
 * @since 1.8
 */
public final int accumulateAndGet(T obj, int x,
                                  IntBinaryOperator accumulatorFunction) {
    int prev, next;
    do {
        prev = get(obj);
        next = accumulatorFunction.applyAsInt(prev, x);
    } while (!compareAndSet(obj, prev, next));
    return next;
}
 
Example 13
Source File: AtomicIntegerArray.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Atomically updates the element at index {@code i} with the
 * results of applying the given function to the current and
 * given values, returning the updated value. The function should
 * be side-effect-free, since it may be re-applied when attempted
 * updates fail due to contention among threads.  The function is
 * applied with the current value at index {@code i} as its first
 * argument, and the given update as the second argument.
 *
 * @param i the index
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the updated value
 * @since 1.8
 */
public final int accumulateAndGet(int i, int x,
                                  IntBinaryOperator accumulatorFunction) {
    long offset = checkedByteOffset(i);
    int prev, next;
    do {
        prev = getRaw(offset);
        next = accumulatorFunction.applyAsInt(prev, x);
    } while (!compareAndSetRaw(offset, prev, next));
    return next;
}
 
Example 14
Source File: AtomicIntegerFieldUpdater.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the field of the given object managed by this
 * updater with the results of applying the given function to the
 * current and given values, returning the previous value. The
 * function should be side-effect-free, since it may be re-applied
 * when attempted updates fail due to contention among threads.  The
 * function is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param obj An object whose field to get and set
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the previous value
 * @since 1.8
 */
public final int getAndAccumulate(T obj, int x,
                                  IntBinaryOperator accumulatorFunction) {
    int prev, next;
    do {
        prev = get(obj);
        next = accumulatorFunction.applyAsInt(prev, x);
    } while (!compareAndSet(obj, prev, next));
    return prev;
}
 
Example 15
Source File: AtomicInteger.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the current value with the results of
 * applying the given function to the current and given values,
 * returning the updated value. The function should be
 * side-effect-free, since it may be re-applied when attempted
 * updates fail due to contention among threads.  The function
 * is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the updated value
 * @since 1.8
 */
public final int accumulateAndGet(int x,
                                  IntBinaryOperator accumulatorFunction) {
    int prev, next;
    do {
        prev = get();
        next = accumulatorFunction.applyAsInt(prev, x);
    } while (!compareAndSet(prev, next));
    return next;
}
 
Example 16
Source File: DesugarAtomicInteger.java    From desugar_jdk_libs with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the current value with the results of
 * applying the given function to the current and given values,
 * returning the previous value. The function should be
 * side-effect-free, since it may be re-applied when attempted
 * updates fail due to contention among threads.  The function
 * is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the previous value
 * @since 1.8
 */
// For desugar: made static so it can exist outside original class
public static int getAndAccumulate(AtomicInteger atomic, int x,
                                   IntBinaryOperator accumulatorFunction) {
    int prev, next;
    do {
        prev = atomic.get();
        next = accumulatorFunction.applyAsInt(prev, x);
    } while (!atomic.compareAndSet(prev, next));
    return prev;
}
 
Example 17
Source File: AtomicIntegerFieldUpdater.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Atomically updates the field of the given object managed by this
 * updater with the results of applying the given function to the
 * current and given values, returning the updated value. The
 * function should be side-effect-free, since it may be re-applied
 * when attempted updates fail due to contention among threads.  The
 * function is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param obj An object whose field to get and set
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the updated value
 * @since 1.8
 */
public final int accumulateAndGet(T obj, int x,
                                  IntBinaryOperator accumulatorFunction) {
    int prev, next;
    do {
        prev = get(obj);
        next = accumulatorFunction.applyAsInt(prev, x);
    } while (!compareAndSet(obj, prev, next));
    return next;
}
 
Example 18
Source File: AtomicInteger.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the current value with the results of
 * applying the given function to the current and given values,
 * returning the previous value. The function should be
 * side-effect-free, since it may be re-applied when attempted
 * updates fail due to contention among threads.  The function
 * is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the previous value
 * @since 1.8
 */
public final int getAndAccumulate(int x,
                                  IntBinaryOperator accumulatorFunction) {
    int prev, next;
    do {
        prev = get();
        next = accumulatorFunction.applyAsInt(prev, x);
    } while (!compareAndSet(prev, next));
    return prev;
}
 
Example 19
Source File: AtomicIntegerArray.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Atomically updates the element at index {@code i} with the
 * results of applying the given function to the current and
 * given values, returning the previous value. The function should
 * be side-effect-free, since it may be re-applied when attempted
 * updates fail due to contention among threads.  The function is
 * applied with the current value at index {@code i} as its first
 * argument, and the given update as the second argument.
 *
 * @param i the index
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the previous value
 * @since 1.8
 */
public final int getAndAccumulate(int i, int x,
                                  IntBinaryOperator accumulatorFunction) {
    long offset = checkedByteOffset(i);
    int prev, next;
    do {
        prev = getRaw(offset);
        next = accumulatorFunction.applyAsInt(prev, x);
    } while (!compareAndSetRaw(offset, prev, next));
    return prev;
}
 
Example 20
Source File: AtomicInteger.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the current value with the results of
 * applying the given function to the current and given values,
 * returning the previous value. The function should be
 * side-effect-free, since it may be re-applied when attempted
 * updates fail due to contention among threads.  The function
 * is applied with the current value as its first argument,
 * and the given update as the second argument.
 *
 * @param x the update value
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @return the previous value
 * @since 1.8
 */
public final int getAndAccumulate(int x,
                                  IntBinaryOperator accumulatorFunction) {
    int prev, next;
    do {
        prev = get();
        next = accumulatorFunction.applyAsInt(prev, x);
    } while (!compareAndSet(prev, next));
    return prev;
}