/** * Returns an infinite sequential ordered {@code IntStream} produced by iterative * application of a function {@code f} to an initial element {@code seed}, * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, * {@code f(f(seed))}, etc. * * <p>The first element (position {@code 0}) in the {@code IntStream} will be * the provided {@code seed}. For {@code n > 0}, the element at position * {@code n}, will be the result of applying the function {@code f} to the * element at position {@code n - 1}. * * @param seed the initial element * @param f a function to be applied to to the previous element to produce * a new element * @return A new sequential {@code IntStream} */ public static IntStream iterate(final int seed, final IntUnaryOperator f) { Objects.requireNonNull(f); final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() { int t = seed; @Override public boolean hasNext() { return true; } @Override public int nextInt() { int v = t; t = f.applyAsInt(t); return v; } }; return StreamSupport.intStream(Spliterators.spliteratorUnknownSize( iterator, Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false); }
@Override public final IntStream map(IntUnaryOperator mapper) { Objects.requireNonNull(mapper); return new StatelessOp<Integer>(this, StreamShape.INT_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { @Override Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) { return new Sink.ChainedInt<Integer>(sink) { @Override public void accept(int t) { downstream.accept(mapper.applyAsInt(t)); } }; } }; }
/** * Fill the inputs of the PhiNode corresponding to one {@link JavaKind#Object} entry in the * virtual object. * * @return true if materialization happened during the merge, false otherwise */ private boolean mergeObjectEntry(IntUnaryOperator objectIdFunc, PartialEscapeBlockState<?>[] states, PhiNode phi, int entryIndex) { boolean materialized = false; for (int i = 0; i < states.length; i++) { int object = objectIdFunc.applyAsInt(i); ObjectState objectState = states[i].getObjectState(object); if (!objectState.isVirtual()) { break; } ValueNode entry = objectState.getEntry(entryIndex); if (entry instanceof VirtualObjectNode) { VirtualObjectNode entryVirtual = (VirtualObjectNode) entry; Block predecessor = getPredecessor(i); materialized |= ensureMaterialized(states[i], entryVirtual.getObjectId(), predecessor.getEndNode(), blockEffects.get(predecessor), COUNTER_MATERIALIZATIONS_MERGE); objectState = states[i].getObjectState(object); if (objectState.isVirtual()) { states[i].setEntry(object, entryIndex, entry = states[i].getObjectState(entryVirtual.getObjectId()).getMaterializedValue()); } } setPhiInput(phi, i, entry); } return materialized; }
/** * Returns an infinite sequential ordered {@code IntStream} produced by iterative * application of a function {@code f} to an initial element {@code seed}, * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, * {@code f(f(seed))}, etc. * * <p>The first element (position {@code 0}) in the {@code IntStream} will be * the provided {@code seed}. For {@code n > 0}, the element at position * {@code n}, will be the result of applying the function {@code f} to the * element at position {@code n - 1}. * * <p>The action of applying {@code f} for one element * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a> * the action of applying {@code f} for subsequent elements. For any given * element the action may be performed in whatever thread the library * chooses. * * @param seed the initial element * @param f a function to be applied to the previous element to produce * a new element * @return a new sequential {@code IntStream} */ public static IntStream iterate(final int seed, final IntUnaryOperator f) { Objects.requireNonNull(f); Spliterator.OfInt spliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE, Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) { int prev; boolean started; @Override public boolean tryAdvance(IntConsumer action) { Objects.requireNonNull(action); int t; if (started) t = f.applyAsInt(prev); else { t = seed; started = true; } action.accept(prev = t); return true; } }; return StreamSupport.intStream(spliterator, false); }
private void tick() { int time = this.time.updateAndGet( new IntUnaryOperator() { @Override public int applyAsInt( int operand ) { return operand < maxWorldTicks ? ( operand + 1 ) : 0; } } ); int age = worldAge.incrementAndGet(); if ( timeCounter == 20 ) { timeCounter = 0; for ( FlexPlayer player : playerSet ) { player.getConnectionHandler().sendMessage( new MessageS47TimeUpdate( age, time ) ); } } else { timeCounter++; } }
/** * Returns an infinite sequential ordered {@code IntStream} produced by iterative * application of a function {@code f} to an initial element {@code seed}, * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, * {@code f(f(seed))}, etc. * * <p>The first element (position {@code 0}) in the {@code IntStream} will be * the provided {@code seed}. For {@code n > 0}, the element at position * {@code n}, will be the result of applying the function {@code f} to the * element at position {@code n - 1}. * * @param seed the initial element * @param f a function to be applied to the previous element to produce * a new element * @return a new sequential {@code IntStream} */ public static IntStream iterate(final int seed, final IntUnaryOperator f) { Objects.requireNonNull(f); Spliterator.OfInt spliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE, Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) { int prev; boolean started; @Override public boolean tryAdvance(IntConsumer action) { Objects.requireNonNull(action); int t; if (started) t = f.applyAsInt(prev); else { t = seed; started = true; } action.accept(prev = t); return true; } }; return StreamSupport.intStream(spliterator, false); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<Integer, Integer> cache = new ConcurrentHashMap<>(); final IntUnaryOperator operator = input -> input; final IntFunction<Integer> keyFunction = Integer::valueOf; // when final ConcurrentMapBasedIntUnaryOperatorMemoizer<Integer> memoizer = new ConcurrentMapBasedIntUnaryOperatorMemoizer<>( cache, keyFunction, operator); // then memoizer.applyAsInt(123); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", 123, memoizer.viewCacheForTest().keySet().iterator().next().intValue()); Assert.assertEquals("Memoization value does not match expectations", 123, memoizer.viewCacheForTest().values().iterator().next().intValue()); }
/** * */ @Test public void shouldUseCallWrappedOperator() { // given final ConcurrentMap<Integer, Integer> cache = new ConcurrentHashMap<>(); final IntUnaryOperator operator = Mockito.mock(IntUnaryOperator.class); final IntFunction<Integer> keyFunction = Integer::valueOf; // when final ConcurrentMapBasedIntUnaryOperatorMemoizer<Integer> memoizer = new ConcurrentMapBasedIntUnaryOperatorMemoizer<>( cache, keyFunction, operator); // then memoizer.applyAsInt(123); Mockito.verify(operator).applyAsInt(123); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<Integer, Integer> cache = null; final IntUnaryOperator operator = input -> input; final IntFunction<Integer> keyFunction = Integer::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedIntUnaryOperatorMemoizer<>(cache, keyFunction, operator); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullOperator() { // given final ConcurrentMap<Integer, Integer> cache = new ConcurrentHashMap<>(); final IntUnaryOperator operator = null; final IntFunction<Integer> keyFunction = Integer::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Cannot memoize a NULL IntUnaryOperator - provide an actual IntUnaryOperator to fix this."); // then new ConcurrentMapBasedIntUnaryOperatorMemoizer<>(cache, keyFunction, operator); }
public BinaryVarVal(IntVar[] variables, IntUnaryOperator varCost, IntUnaryOperator valSelector) { this.variables = variables; this.unassigned = Array.makeInt(variables.length, i -> i); this.nUnassignedT = new TrailedInt(variables[0].trail(), variables.length); this.varCost = varCost; this.valSelector = valSelector; }
public static int[] makeInt(int n, IntUnaryOperator f) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = f.applyAsInt(i); } return array; }
protected static int luhnChecksum(IntUnaryOperator provider, int length) { return IntStream.range(0, length) .map(condition(i -> i % 2 == length % 2, i -> provider.applyAsInt(i)*2, provider)) .map(condition(v -> v > 9, v -> v - 9)) .sum() % 10; }
@NonNull @Override public MuVector4i map(@NonNull final IntUnaryOperator operator) { this.x = operator.applyAsInt(this.x); this.y = operator.applyAsInt(this.y); this.z = operator.applyAsInt(this.z); this.w = operator.applyAsInt(this.w); return this; }
@NonNull @Override public MuVector3i map(@NonNull final IntUnaryOperator operator) { this.x = operator.applyAsInt(this.x); this.y = operator.applyAsInt(this.y); this.z = operator.applyAsInt(this.z); return this; }
@NonNull @Override public MuVector2i map(@NonNull final IntUnaryOperator operator) { this.x = operator.applyAsInt(this.x); this.y = operator.applyAsInt(this.y); return this; }
public final int getAndUpdate(int i, IntUnaryOperator updateFunction) { long offset = checkedByteOffset(i); int prev, next; do { prev = getRaw(offset); next = updateFunction.applyAsInt(prev); } while (!compareAndSetRaw(offset, prev, next)); return prev; }
public final int updateAndGet(int i, IntUnaryOperator updateFunction) { long offset = checkedByteOffset(i); int prev, next; do { prev = getRaw(offset); next = updateFunction.applyAsInt(prev); } while (!compareAndSetRaw(offset, prev, next)); return next; }
public final int getAndUpdate(IntUnaryOperator updateFunction) { int prev, next; do { prev = value; next = updateFunction.applyAsInt(prev); } while (!compareAndSet(prev, next)); return prev; }
public final int updateAndGet(IntUnaryOperator updateFunction) { int prev, next; do { prev = value; next = updateFunction.applyAsInt(prev); } while (!compareAndSet(prev, next)); return next; }
@Test(dataProvider = "int") public void testSetAllInt(String name, int size, IntUnaryOperator generator, int[] expected) { int[] result = new int[size]; Arrays.setAll(result, generator); assertEquals(result, expected, "setAll(int[], IntUnaryOperator) case " + name + " failed."); // ensure fresh array result = new int[size]; Arrays.parallelSetAll(result, generator); assertEquals(result, expected, "parallelSetAll(int[], IntUnaryOperator) case " + name + " failed."); }
private void checkAndSetOrder(IntPredicate expectedValue, IntUnaryOperator newValue) { if (!expectedValue.test(invocationOrder)) { throw new TestSupport.AssertionFailedException( expectedValue + " -> " + newValue); } invocationOrder = newValue.applyAsInt(invocationOrder); }
private int nextEntityId() { return this.entityIdCounter.updateAndGet( new IntUnaryOperator() { @Override public int applyAsInt( int operand ) { return operand < Integer.MAX_VALUE ? ( operand + 1 ) : 0; // just in case :P } } ); }
/** * Apply an int -> int function to this range, producing an int[] * * @param lambda the int -> int function */ public int[] mapToInteger(final IntUnaryOperator lambda) { JointCallingUtils.nonNull(lambda, "the lambda function cannot be null"); final int[] result = new int[size()]; for (int i = from; i < to; i++) { result[i - from] = lambda.applyAsInt(i); } return result; }
/** * Returns a 1-dimensional array of integers, obtained after mapping every integer in this range in a value given by the specified unary operator. * * @param op * a unary operator that converts an {@code int} into another {@code int} * @return a 1-dimensional array of integers */ public int[] map(IntUnaryOperator op) { // return IntStream.iterate(minIncluded, n -> n + step).takeWhile(n -> n <= maxIncluded).toArray(); // WAIT FOR JDK9 List<Integer> list = new ArrayList<>(); for (int i : this) list.add(op.applyAsInt(i)); return list.stream().mapToInt(i -> i).toArray(); }
/** * */ @Test public void shouldUseReturnOperatorResult() { // given final ConcurrentMap<Integer, Integer> cache = new ConcurrentHashMap<>(); final IntUnaryOperator operator = input -> input; final IntFunction<Integer> keyFunction = Integer::valueOf; // when final ConcurrentMapBasedIntUnaryOperatorMemoizer<Integer> memoizer = new ConcurrentMapBasedIntUnaryOperatorMemoizer<>( cache, keyFunction, operator); // then Assert.assertEquals(123, memoizer.applyAsInt(123)); }
@Test public void iterate() { IntUnaryOperator operator = a -> a; IntStream stream = ParallelIntStreamSupport.iterate(42, operator, this.workerPool); assertThat(stream, instanceOf(ParallelIntStreamSupport.class)); assertTrue(stream.isParallel()); assertEquals(OptionalInt.of(42), stream.findAny()); }