public void testLongMethods() { BinaryOperator<Long> sum1 = Long::sum; LongBinaryOperator sum2 = Long::sum; BinaryOperator<Long> max1 = Long::max; LongBinaryOperator max2 = Long::max; BinaryOperator<Long> min1 = Long::min; LongBinaryOperator min2 = Long::min; Comparator<Long> cmp = Long::compare; long[] numbers = { -1, 0, 1, 100, Long.MAX_VALUE, Long.MIN_VALUE }; for (long i : numbers) { for (long j : numbers) { assertEquals(i+j, (long) sum1.apply(i, j)); assertEquals(i+j, sum2.applyAsLong(i, j)); assertEquals(Math.max(i,j), (long) max1.apply(i, j)); assertEquals(Math.max(i,j), max2.applyAsLong(i, j)); assertEquals(Math.min(i,j), (long) min1.apply(i, j)); assertEquals(Math.min(i,j), min2.applyAsLong(i, j)); assertEquals(((Long) i).compareTo(j), cmp.compare(i, j)); } } }
@NonNull @Override public MuVector2l map(@NonNull final Vector2l that, @NonNull final LongBinaryOperator operator) { this.x = operator.applyAsLong(this.x, that.x()); this.y = operator.applyAsLong(this.y, that.y()); return this; }
@NonNull @Override public MuVector4l map(@NonNull final Vector4l that, @NonNull final LongBinaryOperator operator) { this.x = operator.applyAsLong(this.x, that.x()); this.y = operator.applyAsLong(this.y, that.y()); this.z = operator.applyAsLong(this.z, that.z()); this.w = operator.applyAsLong(this.w, that.w()); return this; }
@NonNull @Override public MuVector3l map(@NonNull final Vector3l that, @NonNull final LongBinaryOperator operator) { this.x = operator.applyAsLong(this.x, that.x()); this.y = operator.applyAsLong(this.y, that.y()); this.z = operator.applyAsLong(this.z, that.z()); return this; }
public final long getAndAccumulate(long x, LongBinaryOperator accumulatorFunction) { long prev, next; do { prev = value; next = accumulatorFunction.applyAsLong(prev, x); } while (!compareAndSet(prev, next)); return prev; }
public final long accumulateAndGet(long x, LongBinaryOperator accumulatorFunction) { long prev, next; do { prev = value; next = accumulatorFunction.applyAsLong(prev, x); } while (!compareAndSet(prev, next)); return next; }
public final long getAndAccumulate(int i, long x, LongBinaryOperator accumulatorFunction) { long offset = checkedByteOffset(i); long prev, next; do { prev = getRaw(offset); next = accumulatorFunction.applyAsLong(prev, x); } while (!compareAndSetRaw(offset, prev, next)); return prev; }
public final long accumulateAndGet(int i, long x, LongBinaryOperator accumulatorFunction) { long offset = checkedByteOffset(i); long prev, next; do { prev = getRaw(offset); next = accumulatorFunction.applyAsLong(prev, x); } while (!compareAndSetRaw(offset, prev, next)); return next; }
/** * Constructs a {@code TerminalOp} that implements a functional reduce on * {@code long} values. * * @param identity the identity for the combining function * @param operator the combining function * @return a {@code TerminalOp} implementing the reduction */ public static TerminalOp<Long, Long> makeLong(long identity, LongBinaryOperator operator) { Objects.requireNonNull(operator); class ReducingSink implements AccumulatingSink<Long, Long, ReducingSink>, Sink.OfLong { private long state; @Override public void begin(long size) { state = identity; } @Override public void accept(long t) { state = operator.applyAsLong(state, t); } @Override public Long get() { return state; } @Override public void combine(ReducingSink other) { accept(other.state); } } return new ReduceOp<Long, Long, ReducingSink>(StreamShape.LONG_VALUE) { @Override public ReducingSink makeSink() { return new ReducingSink(); } }; }
public final void compute() { final ToLongFunction<? super V> transformer; final LongBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { long r = this.basis; for (int i = baseIndex, f, h; batch > 0 && (h = ((f = baseLimit) + i) >>> 1) > i;) { addToPendingCount(1); (rights = new MapReduceValuesToLongTask<K,V> (this, batch >>>= 1, baseLimit = h, f, tab, rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = advance()) != null; ) r = reducer.applyAsLong(r, transformer.applyAsLong(p.val)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceValuesToLongTask<K,V> t = (MapReduceValuesToLongTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsLong(t.result, s.result); s = t.rights = s.nextRight; } } } }
/** Root task constructor */ public LongCumulateTask(LongCumulateTask parent, LongBinaryOperator function, long[] 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 */ LongCumulateTask(LongCumulateTask parent, LongBinaryOperator function, long[] 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; }
MapReduceEntriesToLongTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceEntriesToLongTask<K,V> nextRight, ToLongFunction<Map.Entry<K,V>> transformer, long basis, LongBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
MapReduceKeysToLongTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceKeysToLongTask<K,V> nextRight, ToLongFunction<? super K> transformer, long basis, LongBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
MapReduceValuesToLongTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceValuesToLongTask<K,V> nextRight, ToLongFunction<? super V> transformer, long basis, LongBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
public final void compute() { final ToLongFunction<Map.Entry<K,V>> transformer; final LongBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { long r = this.basis; for (int i = baseIndex, f, h; batch > 0 && (h = ((f = baseLimit) + i) >>> 1) > i;) { addToPendingCount(1); (rights = new MapReduceEntriesToLongTask<K,V> (this, batch >>>= 1, baseLimit = h, f, tab, rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = advance()) != null; ) r = reducer.applyAsLong(r, transformer.applyAsLong(p)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceEntriesToLongTask<K,V> t = (MapReduceEntriesToLongTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsLong(t.result, s.result); s = t.rights = s.nextRight; } } } }
MapReduceMappingsToLongTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceMappingsToLongTask<K,V> nextRight, ToLongBiFunction<? super K, ? super V> transformer, long basis, LongBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
public final void compute() { final ToLongBiFunction<? super K, ? super V> transformer; final LongBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { long r = this.basis; for (int i = baseIndex, f, h; batch > 0 && (h = ((f = baseLimit) + i) >>> 1) > i;) { addToPendingCount(1); (rights = new MapReduceMappingsToLongTask<K,V> (this, batch >>>= 1, baseLimit = h, f, tab, rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = advance()) != null; ) r = reducer.applyAsLong(r, transformer.applyAsLong(p.key, p.val)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceMappingsToLongTask<K,V> t = (MapReduceMappingsToLongTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsLong(t.result, s.result); s = t.rights = s.nextRight; } } } }
@Test(dataProvider="longSet") public void testParallelPrefixForLong(long[] data, int fromIndex, int toIndex, LongBinaryOperator op) { long[] sequentialResult = data.clone(); for (int index = fromIndex + 1; index < toIndex; index++) { sequentialResult[index ] = op.applyAsLong(sequentialResult[index - 1], sequentialResult[index]); } long[] parallelResult = data.clone(); Arrays.parallelPrefix(parallelResult, fromIndex, toIndex, op); assertArraysEqual(parallelResult, sequentialResult); long[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex); Arrays.parallelPrefix(parallelRangeResult, op); assertArraysEqual(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex)); }
@DataProvider(name = "longSet") public static Object[][] longSet(){ return genericData(size -> LongStream.range(0, size).toArray(), new LongBinaryOperator[]{ Long::sum, Long::min}); }
public final void compute() { final ToLongFunction<? super K> transformer; final LongBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { long r = this.basis; for (int i = baseIndex, f, h; batch > 0 && (h = ((f = baseLimit) + i) >>> 1) > i;) { addToPendingCount(1); (rights = new MapReduceKeysToLongTask<K,V> (this, batch >>>= 1, baseLimit = h, f, tab, rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = advance()) != null; ) r = reducer.applyAsLong(r, transformer.applyAsLong(p.key)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceKeysToLongTask<K,V> t = (MapReduceKeysToLongTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsLong(t.result, s.result); s = t.rights = s.nextRight; } } } }
@DataProvider public static Object[][] longSet(){ return genericData(size -> LongStream.range(0, size).toArray(), new LongBinaryOperator[]{ Long::sum, Long::min}); }
@Test(dataProvider="longSet") public void testParallelPrefixForLong(long[] data, int fromIndex, int toIndex, LongBinaryOperator op) { long[] sequentialResult = data.clone(); for (int index = fromIndex + 1; index < toIndex; index++) { sequentialResult[index ] = op.applyAsLong(sequentialResult[index - 1], sequentialResult[index]); } long[] parallelResult = data.clone(); Arrays.parallelPrefix(parallelResult, fromIndex, toIndex, op); assertEquals(parallelResult, sequentialResult); long[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex); Arrays.parallelPrefix(parallelRangeResult, op); assertEquals(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex)); }
static void testLongAccumulator() { LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y; LongAccumulator a = new LongAccumulator(plus, -2); a.accumulate(34); LongAccumulator 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.LongAccumulator$SerializationProxy"); }