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

The following examples show how to use java.util.function.IntUnaryOperator#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: IntStream.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    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);
}
 
Example 2
Source File: IntStream.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: IntStream.java    From desugar_jdk_libs with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: PartitionTransforms.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Block bucketBlock(Block block, int count, IntUnaryOperator hasher)
{
    BlockBuilder builder = INTEGER.createFixedSizeBlockBuilder(block.getPositionCount());
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (block.isNull(position)) {
            builder.appendNull();
            continue;
        }
        int hash = hasher.applyAsInt(position);
        int bucket = (hash & Integer.MAX_VALUE) % count;
        INTEGER.writeLong(builder, bucket);
    }
    return builder.build();
}
 
Example 5
Source File: IEJoin.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
public static int indexOf(IntUnaryOperator a, int key, int count, boolean rev, boolean equal) {
	int lo = 0;
	int hi = count - 1;
	while (lo <= hi) {
		// Key is in a[lo..hi] or not present.
		int mid = lo + (hi - lo) / 2;
		int value = a.applyAsInt(mid);
		int comp = Integer.compare(key, value);
		if (rev) {
			if (!equal && comp >= 0 || equal && comp > 0)
				hi = mid - 1;
			else if (!equal && comp < 0 || equal && comp <= 0)
				lo = mid + 1;
			else
				return mid;
		} else {
			if (!equal && comp <= 0 || equal && comp < 0)
				hi = mid - 1;
			else if (!equal && comp > 0 || equal && comp >= 0)
				lo = mid + 1;
			else
				return mid;
		}

	}
	return lo;
}
 
Example 6
Source File: Int2IntHashMap.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Primitive specialised version of {@link #computeIfAbsent(Object, Function)}
 *
 * @param key             to search on.
 * @param mappingFunction to provide a value if the get returns null.
 * @return the value if found otherwise the missing value.
 */
public int computeIfAbsent(final int key, final IntUnaryOperator mappingFunction)
{
    int value = get(key);
    if (value == missingValue)
    {
        value = mappingFunction.applyAsInt(key);
        if (value != missingValue)
        {
            put(key, value);
        }
    }

    return value;
}
 
Example 7
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, 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.
 *
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final int getAndUpdate(IntUnaryOperator updateFunction) {
    int prev, next;
    do {
        prev = get();
        next = updateFunction.applyAsInt(prev);
    } while (!compareAndSet(prev, next));
    return prev;
}
 
Example 8
Source File: AtomicInteger.java    From openjdk-8-source 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, 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.
 *
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final int getAndUpdate(IntUnaryOperator updateFunction) {
    int prev, next;
    do {
        prev = get();
        next = updateFunction.applyAsInt(prev);
    } while (!compareAndSet(prev, next));
    return prev;
}
 
Example 9
Source File: AtomicIntegerArray.java    From openjdk-jdk8u-backup 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, 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.
 *
 * @param i the index
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
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;
}
 
Example 10
Source File: AtomicIntegerArray.java    From openjdk-jdk8u-backup 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, 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.
 *
 * @param i the index
 * @param updateFunction a side-effect-free function
 * @return the updated value
 * @since 1.8
 */
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;
}
 
Example 11
Source File: AtomicInteger.java    From Java8CN with Apache License 2.0 3 votes vote down vote up
/**
 * Atomically updates the current value with the results of
 * applying the given function, 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.
 *
 * @param updateFunction a side-effect-free function
 * @return the updated value
 * @since 1.8
 */
public final int updateAndGet(IntUnaryOperator updateFunction) {
    int prev, next;
    do {
        prev = get();
        next = updateFunction.applyAsInt(prev);
    } while (!compareAndSet(prev, next));
    return next;
}
 
Example 12
Source File: AtomicIntegerArray.java    From hottub 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, 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.
 *
 * @param i the index
 * @param updateFunction a side-effect-free function
 * @return the updated value
 * @since 1.8
 */
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;
}
 
Example 13
Source File: AtomicInteger.java    From dragonwell8_jdk 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, 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.
 *
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final int getAndUpdate(IntUnaryOperator updateFunction) {
    int prev, next;
    do {
        prev = get();
        next = updateFunction.applyAsInt(prev);
    } while (!compareAndSet(prev, next));
    return prev;
}
 
Example 14
Source File: AtomicIntegerFieldUpdater.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates (with memory effects as specified by {@link
 * VarHandle#compareAndSet}) the field of the given object managed
 * by this updater with the results of applying the given
 * function, 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.
 *
 * @param obj An object whose field to get and set
 * @param updateFunction a side-effect-free function
 * @return the updated value
 * @since 1.8
 */
public final int updateAndGet(T obj, IntUnaryOperator updateFunction) {
    int prev, next;
    do {
        prev = get(obj);
        next = updateFunction.applyAsInt(prev);
    } while (!compareAndSet(obj, prev, next));
    return next;
}
 
Example 15
Source File: AtomicIntegerArray.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
 * Atomically updates (with memory effects as specified by {@link
 * VarHandle#compareAndSet}) the element at index {@code i} with
 * the results of applying the given function, 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.
 *
 * @param i the index
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final int getAndUpdate(int i, IntUnaryOperator updateFunction) {
    int prev = get(i), next = 0;
    for (boolean haveNext = false;;) {
        if (!haveNext)
            next = updateFunction.applyAsInt(prev);
        if (weakCompareAndSetVolatile(i, prev, next))
            return prev;
        haveNext = (prev == (prev = get(i)));
    }
}
 
Example 16
Source File: AtomicIntegerFieldUpdater.java    From jdk8u60 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, 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.
 *
 * @param obj An object whose field to get and set
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final int getAndUpdate(T obj, IntUnaryOperator updateFunction) {
    int prev, next;
    do {
        prev = get(obj);
        next = updateFunction.applyAsInt(prev);
    } while (!compareAndSet(obj, 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, 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.
 *
 * @param obj An object whose field to get and set
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final int getAndUpdate(T obj, IntUnaryOperator updateFunction) {
    int prev, next;
    do {
        prev = get(obj);
        next = updateFunction.applyAsInt(prev);
    } while (!compareAndSet(obj, prev, next));
    return prev;
}
 
Example 18
Source File: AtomicInteger.java    From Java8CN with Apache License 2.0 3 votes vote down vote up
/**
 * Atomically updates the current value with the results of
 * applying the given function, 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.
 *
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final int getAndUpdate(IntUnaryOperator updateFunction) {
    int prev, next;
    do {
        prev = get();
        next = updateFunction.applyAsInt(prev);
    } while (!compareAndSet(prev, next));
    return prev;
}
 
Example 19
Source File: AtomicInteger.java    From openjdk-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, 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.
 *
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final int getAndUpdate(IntUnaryOperator updateFunction) {
    int prev, next;
    do {
        prev = get();
        next = updateFunction.applyAsInt(prev);
    } while (!compareAndSet(prev, next));
    return prev;
}
 
Example 20
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, 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.
 *
 * @param i the index
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
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;
}