Java Code Examples for java.util.PrimitiveIterator#OfInt

The following examples show how to use java.util.PrimitiveIterator#OfInt . 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: BitSetStreamTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "cases")
public void testBitsetStream(String name, IntStream data) {
    BitSet bs = new BitSet();
    long setBits = data.distinct()
                       .peek(i -> bs.set(i))
                       .count();

    assertEquals(bs.cardinality(), setBits);
    assertEquals(bs.cardinality(), bs.stream().reduce(0, (s, i) -> s+1));

    PrimitiveIterator.OfInt it = bs.stream().iterator();
    for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
        assertTrue(it.hasNext());
        assertEquals(it.nextInt(), i);
    }
    assertFalse(it.hasNext());
}
 
Example 2
Source File: IntStream.java    From jdk8u60 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 3
Source File: BitSetStreamTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "cases")
public void testBitsetStream(String name, IntStream data) {
    BitSet bs = new BitSet();
    long setBits = data.distinct()
                       .peek(i -> bs.set(i))
                       .count();

    assertEquals(bs.cardinality(), setBits);
    assertEquals(bs.cardinality(), bs.stream().reduce(0, (s, i) -> s+1));

    PrimitiveIterator.OfInt it = bs.stream().iterator();
    for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
        assertTrue(it.hasNext());
        assertEquals(it.nextInt(), i);
    }
    assertFalse(it.hasNext());
}
 
Example 4
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 5
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 6
Source File: NodeWalker.java    From ml-models with Apache License 2.0 5 votes vote down vote up
public Stream<int[]> internalRandomWalk(@Name(value = "steps", defaultValue = "80") int steps, org.neo4j.graphalgo.impl.walking.NodeWalker.NextNodeStrategy strategy, TerminationFlag terminationFlag,
                                        int concurrency, int limit, PrimitiveIterator.OfInt idStream) {
    int timeout = 100;
    int queueSize = 1000;
    int batchSize = ParallelUtil.adjustBatchSize(limit, concurrency, 100);
    Collection<Runnable> tasks = new ArrayList<>((limit / batchSize) + 1);

    ArrayBlockingQueue<int[]> queue = new ArrayBlockingQueue<>(queueSize);
    int[] TOMB = new int[0];

    while (idStream.hasNext()) {
        int[] ids = new int[batchSize];
        int i = 0;
        while (i < batchSize && idStream.hasNext()) {
            ids[i++] = idStream.nextInt();
        }
        int size = i;
        tasks.add(() -> {
            for (int j = 0; j < size; j++) {
                put(queue, doInternalWalk(ids[j], steps, strategy, terminationFlag));
            }
        });
    }
    new Thread(() -> {
        ParallelUtil.runWithConcurrency(concurrency, tasks, terminationFlag, Pools.DEFAULT);
        put(queue, TOMB);
    }).start();

    QueueBasedSpliterator<int[]> spliterator = new QueueBasedSpliterator<>(queue, TOMB, terminationFlag, timeout);
    return StreamSupport.stream(spliterator, false);
}
 
Example 7
Source File: IntNodeTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private Node.OfInt degenerateTree(PrimitiveIterator.OfInt it) {
    if (!it.hasNext()) {
        return Nodes.node(new int[0]);
    }

    int i = it.nextInt();
    if (it.hasNext()) {
        return new Nodes.ConcNode.OfInt(Nodes.node(new int[] {i}), degenerateTree(it));
    }
    else {
        return Nodes.node(new int[] {i});
    }
}
 
Example 8
Source File: CharSequence.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Returns a stream of {@code int} zero-extending the {@code char} values
 * from this sequence.  Any char which maps to a <a
 * href="{@docRoot}/java/lang/Character.html#unicode">surrogate code
 * point</a> is passed through uninterpreted.
 *
 * <p>If the sequence is mutated while the stream is being read, the
 * result is undefined.
 *
 * @return an IntStream of char values from this sequence
 * @since 1.8
 */
public default IntStream chars() {
    class CharIterator implements PrimitiveIterator.OfInt {
        int cur = 0;

        public boolean hasNext() {
            return cur < length();
        }

        public int nextInt() {
            if (hasNext()) {
                return charAt(cur++);
            } else {
                throw new NoSuchElementException();
            }
        }

        @Override
        public void forEachRemaining(IntConsumer block) {
            for (; cur < length(); cur++) {
                block.accept(charAt(cur));
            }
        }
    }

    return StreamSupport.intStream(() ->
            Spliterators.spliterator(
                    new CharIterator(),
                    length(),
                    Spliterator.ORDERED),
            Spliterator.SUBSIZED | Spliterator.SIZED | Spliterator.ORDERED,
            false);
}
 
Example 9
Source File: CharSequence.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a stream of {@code int} zero-extending the {@code char} values
 * from this sequence.  Any char which maps to a <a
 * href="{@docRoot}/java/lang/Character.html#unicode">surrogate code
 * point</a> is passed through uninterpreted.
 *
 * <p>If the sequence is mutated while the stream is being read, the
 * result is undefined.
 *
 * @return an IntStream of char values from this sequence
 * @since 1.8
 */
public default IntStream chars() {
    class CharIterator implements PrimitiveIterator.OfInt {
        int cur = 0;

        public boolean hasNext() {
            return cur < length();
        }

        public int nextInt() {
            if (hasNext()) {
                return charAt(cur++);
            } else {
                throw new NoSuchElementException();
            }
        }

        @Override
        public void forEachRemaining(IntConsumer block) {
            for (; cur < length(); cur++) {
                block.accept(charAt(cur));
            }
        }
    }

    return StreamSupport.intStream(() ->
            Spliterators.spliterator(
                    new CharIterator(),
                    length(),
                    Spliterator.ORDERED),
            Spliterator.SUBSIZED | Spliterator.SIZED | Spliterator.ORDERED,
            false);
}
 
Example 10
Source File: StringLiteral.java    From sylph with Apache License 2.0 5 votes vote down vote up
static String formatStringLiteral(String s)
    {
        s = s.replace("'", "''");
//        if (CharMatcher.inRange((char) 0x20, (char) 0x7E).matchesAllOf(s)) {
//            return "'" + s + "'";
//        }
        if (charMatches((char) 0x20, (char) 0x7E, s)) {
            return "'" + s + "'";
        }

        StringBuilder builder = new StringBuilder();
        builder.append("U&'");
        PrimitiveIterator.OfInt iterator = s.codePoints().iterator();
        while (iterator.hasNext()) {
            int codePoint = iterator.nextInt();
            checkArgument(codePoint >= 0, "Invalid UTF-8 encoding in characters: %s", s);
            if (isAsciiPrintable(codePoint)) {
                char ch = (char) codePoint;
                if (ch == '\\') {
                    builder.append(ch);
                }
                builder.append(ch);
            }
            else if (codePoint <= 0xFFFF) {
                builder.append('\\');
                builder.append(String.format("%04X", codePoint));
            }
            else {
                builder.append("\\+");
                builder.append(String.format("%06X", codePoint));
            }
        }
        builder.append("'");
        return builder.toString();
    }
 
Example 11
Source File: IntPipeline.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public final PrimitiveIterator.OfInt iterator() {
    return Spliterators.iterator(spliterator());
}
 
Example 12
Source File: SpinedBuffer.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
@Override
public PrimitiveIterator.OfInt iterator() {
    return Spliterators.iterator(spliterator());
}
 
Example 13
Source File: SpinedBuffer.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public PrimitiveIterator.OfInt iterator() {
    return Spliterators.iterator(spliterator());
}
 
Example 14
Source File: DefaultTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NoSuchElementException.class)
public void testEmptyCodePoints() {
    PrimitiveIterator.OfInt s = "".codePoints().iterator();
    assertFalse(s.hasNext());
    int cp = s.nextInt();
}
 
Example 15
Source File: IntStream.java    From desugar_jdk_libs with GNU General Public License v2.0 4 votes vote down vote up
@Override
PrimitiveIterator.OfInt iterator();
 
Example 16
Source File: IntPipeline.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public final PrimitiveIterator.OfInt iterator() {
    return Spliterators.iterator(spliterator());
}
 
Example 17
Source File: DefaultTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NoSuchElementException.class)
public void testEmptyCodePoints() {
    PrimitiveIterator.OfInt s = "".codePoints().iterator();
    assertFalse(s.hasNext());
    int cp = s.nextInt();
}
 
Example 18
Source File: IntStream.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
PrimitiveIterator.OfInt iterator();
 
Example 19
Source File: IntStream.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
@Override
PrimitiveIterator.OfInt iterator();
 
Example 20
Source File: DefaultTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NoSuchElementException.class)
public void testEmptyChars() {
    PrimitiveIterator.OfInt s = "".chars().iterator();
    assertFalse(s.hasNext());
    int ch = s.nextInt();
}