/** * @param it an iterator * @param f a function which transforms values into iterators * @return an iterator which yields the concatenation of the iterators */ public static <U> OfInt flatMap(Iterator<U> it, Function<U, OfInt> f) { return new OfInt() { OfInt curr = EMPTY; private void prime() { while (!curr.hasNext() && it.hasNext()) { curr = f.apply(it.next()); } } @Override public boolean hasNext() { prime(); return curr.hasNext(); } @Override public int nextInt() { prime(); return curr.nextInt(); } }; }
/** * @param min an inclusive start of the range * @param max an exclusive end of the range * @param f a function which transforms a number in the range into a value * @return an iterator which yields the values returned by {@code f} */ public static OfInt range(long min, long max, LongToIntFunction f) { return new OfInt() { long i = min; @Override public boolean hasNext() { return i < max; } @Override public int nextInt() { if (hasNext()) { return f.applyAsInt(i++); } else { throw new NoSuchElementException(); } } }; }
public static OfInt codePointIterator(byte[] chunk) { return new OfInt() { private int idx = 2; @Override public int nextInt() { int codePoint = decode(chunk, idx); idx += prefixLength(chunk[idx]); return codePoint; } @Override public boolean hasNext() { return idx < chunk.length; } }; }
public static OfInt reverseCodePointIterator(byte[] chunk) { return new OfInt() { int idx = chunk.length; @Override public int nextInt() { while ((chunk[idx] & 0b11000000) == 0b10000000) { idx--; } int codePoint = decode(chunk, idx); idx--; return codePoint; } @Override public boolean hasNext() { return idx > 2; } }; }
private <A> A collectSized(Supplier<A> supplier, ObjIntConsumer<A> accumulator, BiConsumer<A, A> combiner, IntFunction<A> sizedSupplier, ObjIntConsumer<A> sizedAccumulator) { if (isParallel()) return collect(supplier, accumulator, combiner); java.util.Spliterator.OfInt spliterator = spliterator(); long size = spliterator.getExactSizeIfKnown(); A intermediate; if (size >= 0 && size <= Integer.MAX_VALUE) { intermediate = sizedSupplier.apply((int) size); spliterator.forEachRemaining((IntConsumer) i -> sizedAccumulator.accept(intermediate, i)); } else { intermediate = supplier.get(); spliterator.forEachRemaining((IntConsumer) i -> accumulator.accept(intermediate, i)); } return intermediate; }
@Test public void compatibility() { final TestData data = TestData.of("/io/jenetics/util/IndexStream.Random"); for (String[] line : data) { final Random random = new Random(0); final double p = Double.parseDouble(line[0]); final OfInt it = indexes(random, 500, p).iterator(); for (int i = 1; i < line.length; ++i) { final int index = Integer.parseInt(line[i]); Assert.assertEquals(it.nextInt(), index); } Assert.assertFalse(it.hasNext()); } }
@Test public void reference() { final int size = 5000; final double p = 0.5; final Random random1 = new Random(0); final Random random2 = new Random(0); for (int j = 0; j < 1; ++j) { final OfInt it = indexes(random1, size, p).iterator(); final IndexStream stream2 = ReferenceRandomStream( size, p, random2 ); while (it.hasNext()) { Assert.assertEquals(it.nextInt(), stream2.next()); } Assert.assertFalse(it.hasNext()); Assert.assertEquals(stream2.next(), -1); } }
public static boolean equals(OfInt a, OfInt b) { while (a.hasNext()) { if (a.nextInt() != b.nextInt()) { return false; } } return true; }
/** * @param it an iterator * @param f a predicate * @return an iterator which only yields values that satisfy the predicate */ public static OfInt filter(OfInt it, IntPredicate f) { return new OfInt() { private int next = 0; private boolean primed = false; private boolean done = false; private void prime() { if (!primed && !done) { while (it.hasNext()) { next = it.nextInt(); if (f.test(next)) { primed = true; return; } } done = true; } } @Override public boolean hasNext() { prime(); return !done; } @Override public int nextInt() { prime(); if (!primed) { throw new NoSuchElementException(); } primed = false; return next; } }; }
/** * @param it an iterator * @param f a function which transforms values * @return an iterator which yields the transformed values */ public static OfInt map(OfInt it, IntUnaryOperator f) { return new OfInt() { @Override public boolean hasNext() { return it.hasNext(); } @Override public int nextInt() { return f.applyAsInt(it.nextInt()); } }; }
public static OfInt codeUnitIterator(byte[] chunk) { return new OfInt() { OfInt it = codePointIterator(chunk); short low = -1; @Override public int nextInt() { if (low == -1) { int codePoint = it.nextInt(); if (codePoint < 0x10000) { return codePoint; } else { low = (short) Character.lowSurrogate(codePoint); return Character.highSurrogate(codePoint); } } else { int val = low; low = -1; return val; } } @Override public boolean hasNext() { return low != -1 || it.hasNext(); } }; }
public static OfInt reverseCodeUnitIterator(byte[] chunk) { return new OfInt() { OfInt it = codePointIterator(chunk); short high = -1; @Override public int nextInt() { if (high == -1) { int codePoint = it.nextInt(); if (codePoint < 0x10000) { return codePoint; } else { high = (short) Character.highSurrogate(codePoint); return Character.lowSurrogate(codePoint); } } else { int val = high; high = -1; return val; } } @Override public boolean hasNext() { return high != -1 || it.hasNext(); } }; }
/** * Helper method matching a string. Returns false if the string could not be * found. */ private boolean matchString(String expected) { OfInt it = expected.codePoints().iterator(); while (it.hasNext() && ctx.hasNext()) { if (it.nextInt() != ctx.peek()) { return false; } ctx.next(); } return !it.hasNext(); }
@Test public void testDropWhile() { assertArrayEquals(new int[] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, IntStreamEx.range(100).dropWhile( i -> i % 10 < 5).limit(10).toArray()); assertEquals(100, IntStreamEx.range(100).dropWhile(i -> i % 10 < 0).count()); assertEquals(0, IntStreamEx.range(100).dropWhile(i -> i % 10 < 10).count()); assertEquals(OptionalInt.of(0), IntStreamEx.range(100).dropWhile(i -> i % 10 < 0).findFirst()); assertEquals(OptionalInt.empty(), IntStreamEx.range(100).dropWhile(i -> i % 10 < 10).findFirst()); java.util.Spliterator.OfInt spltr = IntStreamEx.range(100).dropWhile(i -> i % 10 < 1).spliterator(); assertTrue(spltr.tryAdvance((int x) -> assertEquals(1, x))); Builder builder = IntStream.builder(); spltr.forEachRemaining(builder); assertArrayEquals(IntStreamEx.range(2, 100).toArray(), builder.build().toArray()); }
private void appendString(final String string) throws IOException { writer.write('"'); OfInt iter = string.codePoints().iterator(); while (iter.hasNext()) { appendEncode(iter.nextInt()); } writer.write('"'); }
public static IntStream toStream(OfInt it, long estimatedSize) { return StreamSupport.intStream(Spliterators.spliterator(it, estimatedSize, Spliterator.ORDERED), false); }
@VisibleForTesting protected List<EmojiEntity> getEmojisFromMessage(FatMessage fatMessage) { String message = fatMessage.getMessage().getMessage(); if (message == null) { return ImmutableList.of(); } Map<String, EmojiEntity> emojis = Maps.newHashMap(); OfInt charIterator = message.chars().iterator(); Room room = fatMessage.getRoom(); String roomName = null; if (room != null) { roomName = room.getName(); } boolean capturingEmoji = false; StringBuilder emojiStrBuilder = new StringBuilder(); while (charIterator.hasNext()) { int asciiValue = charIterator.next(); char ch = (char) asciiValue; if (ch == ':') { // done capturing if (capturingEmoji) { // There's another : right after so continue capturing from there but skip this if (emojiStrBuilder.length() <= 0) { emojiStrBuilder = new StringBuilder(); continue; } String emoji = emojiStrBuilder.toString(); EmojiEntity existingEmoji = emojis.remove(emoji); int occurrences; if (existingEmoji == null) { occurrences = 1; } else { occurrences = existingEmoji.getOccurrences() + 1; } emojis.put(emoji, new EmojiEntity(fatMessage.getUser().getMentionName(), roomName, fatMessage.getMessage().getDate(), emoji, occurrences, fatMessage.getUser().isBot())); emojiStrBuilder = new StringBuilder(); } capturingEmoji = !capturingEmoji; continue; } if (capturingEmoji) { if (BLACKLISTED_CHARS.contains(ch)) { capturingEmoji = !capturingEmoji; emojiStrBuilder = new StringBuilder(); continue; } emojiStrBuilder.append(ch); } } LOG.debug("Extracted {} emojis", emojis.size()); return Lists.newArrayList(emojis.values()); }
public NoSQLSchema build(Map<String, List<SchemaComponent>> rawEntities) { // TODO: Identify objects that are references in the form of MongoDB // references: https://docs.mongodb.org/manual/reference/database-references/#dbrefs // { "$ref" : <type>, "$id" : <value>, "$db" : <value> } // Build reverse indices & Create Entities & Populate with EntityVersions rawEntities.forEach((entityName, schemas) -> { // Entity creation Entity e = factory.createEntity(); e.setName(entityName); mEntities.add(e); OfInt n = IntStream.iterate(1, i -> i+1).iterator(); schemas.forEach(schema -> { EntityVersion theEV = factory.createEntityVersion(); theEV.setVersionId(n.next()); // Set the root flag. It is needed to know which // entities can be destination of a reference ObjectSC obj = (ObjectSC)schema; theEV.setRoot(obj.isRoot); e.getEntityversions().add(theEV); mEntityVersions.put(schema, theEV); }); }); // Consider as reference matcher only those Entities of which // at least one version is root rm = createReferenceMatcher(); // Populate empty EntityVersions mEntityVersions.forEach((schema, ev) -> fillEV(schema, ev)); // Opposite references mEntities.forEach(eFrom -> { eFrom.getEntityversions().forEach(ev -> { ev.getProperties().stream().filter(p -> p instanceof Reference).forEach(r -> { Reference ref = (Reference)r; Entity eTo = ref.getRefTo(); // Find a EntityVersion of eTo that has a reference to the // current Entity eFrom Optional<Property> refTo = eTo.getEntityversions().stream().flatMap(evTo -> evTo.getProperties().stream().filter(pTo -> pTo instanceof Reference)) .filter(rTo -> ((Reference)rTo).getRefTo() == eFrom).findFirst(); refTo.ifPresent(r_ -> ref.setOpposite((Reference)r_)); }); }); }); NoSQLSchema finalSchema = factory.createNoSQLSchema(); finalSchema.setName(name); finalSchema.getEntities().addAll(mEntities); return finalSchema; }
@Override public OfInt iterator() { return this.delegate.iterator(); }
@Override public java.util.Spliterator.OfInt spliterator() { return this.delegate.spliterator(); }
IntStreamEx(Spliterator.OfInt spliterator, StreamContext context) { super(spliterator, context); }
final IntStreamEx delegate(Spliterator.OfInt spliterator) { return new IntStreamEx(spliterator, context); }
/** * Returns an {@code InputStream} lazily populated from the current * {@code IntStreamEx}. * * <p> * Note that only the least-significant byte of every number encountered in * this stream is preserved in the resulting {@code InputStream}, other * bytes are silently lost. Thus it's a caller responsibility to check * whether this may cause problems. * * <p> * This is a terminal operation. * * <p> * When the resulting {@code InputStream} is closed, this * {@code IntStreamEx} is closed as well. * * @return a new {@code InputStream}. * @see #of(InputStream) * @since 0.6.1 */ public InputStream asByteInputStream() { Spliterator.OfInt spltr = spliterator(); return new InputStream() { private int last; @Override public int read() { return spltr.tryAdvance((int val) -> last = val) ? (last & 0xFF) : -1; } @Override public void close() { IntStreamEx.this.close(); } }; }
@Override public OfInt iterator() { return Spliterators.iterator(spliterator()); }
@Test public void testBasics() { assertFalse(IntStreamEx.of(1).isParallel()); assertTrue(IntStreamEx.of(1).parallel().isParallel()); assertFalse(IntStreamEx.of(1).parallel().sequential().isParallel()); AtomicInteger i = new AtomicInteger(); try (IntStreamEx s = IntStreamEx.of(1).onClose(i::incrementAndGet)) { assertEquals(1, s.count()); } assertEquals(1, i.get()); assertEquals(6, IntStreamEx.range(0, 4).sum()); assertEquals(3, IntStreamEx.range(0, 4).max().getAsInt()); assertEquals(0, IntStreamEx.range(0, 4).min().getAsInt()); assertEquals(1.5, IntStreamEx.range(0, 4).average().getAsDouble(), 0.000001); assertEquals(4, IntStreamEx.range(0, 4).summaryStatistics().getCount()); assertArrayEquals(new int[] { 1, 2, 3 }, IntStreamEx.range(0, 5).skip(1).limit(3).toArray()); assertArrayEquals(new int[] { 1, 2, 3 }, IntStreamEx.of(3, 1, 2).sorted().toArray()); assertArrayEquals(new int[] { 1, 2, 3 }, IntStreamEx.of(1, 2, 1, 3, 2).distinct().toArray()); assertArrayEquals(new int[] { 2, 4, 6 }, IntStreamEx.range(1, 4).map(x -> x * 2).toArray()); assertArrayEquals(new long[] { 2, 4, 6 }, IntStreamEx.range(1, 4).mapToLong(x -> x * 2).toArray()); assertArrayEquals(new double[] { 2, 4, 6 }, IntStreamEx.range(1, 4).mapToDouble(x -> x * 2).toArray(), 0.0); assertArrayEquals(new int[] { 1, 3 }, IntStreamEx.range(0, 5).filter(x -> x % 2 == 1).toArray()); assertEquals(6, IntStreamEx.of(1, 2, 3).reduce(Integer::sum).getAsInt()); assertEquals(Integer.MAX_VALUE, IntStreamEx.rangeClosed(1, Integer.MAX_VALUE).spliterator() .getExactSizeIfKnown()); assertTrue(IntStreamEx.of(1, 2, 3).spliterator().hasCharacteristics(Spliterator.ORDERED)); assertFalse(IntStreamEx.of(1, 2, 3).unordered().spliterator().hasCharacteristics(Spliterator.ORDERED)); OfInt iterator = IntStreamEx.of(1, 2, 3).iterator(); assertEquals(1, iterator.nextInt()); assertEquals(2, iterator.nextInt()); assertEquals(3, iterator.nextInt()); assertFalse(iterator.hasNext()); List<Integer> list = new ArrayList<>(); IntStreamEx.range(10).parallel().forEachOrdered(list::add); assertEquals(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), list); assertTrue(IntStreamEx.empty().noneMatch(x -> true)); assertFalse(IntStreamEx.of(1).noneMatch(x -> true)); assertTrue(IntStreamEx.of(1).noneMatch(x -> false)); }
@Override public OfInt iterator() { return ThrowingBridge.of(getDelegate().iterator(), getExceptionClass()); }
@Override public Spliterator.OfInt spliterator() { return ThrowingBridge.of(getDelegate().spliterator(), getExceptionClass()); }
/** * Produces an array containing cumulative results of applying the * accumulation function going left to right. * * <p> * This is a terminal operation. * * <p> * For parallel stream it's not guaranteed that accumulator will always be * executed in the same thread. * * <p> * This method cannot take all the advantages of parallel streams as it must * process elements strictly left to right. * * @param accumulator a * <a href="package-summary.html#NonInterference">non-interfering * </a>, <a href="package-summary.html#Statelessness">stateless</a> * function for incorporating an additional element into a result * @return the array where the first element is the first element of this * stream and every successor element is the result of applying * accumulator function to the previous array element and the * corresponding stream element. The resulting array has the same * length as this stream. * @see #foldLeft(IntBinaryOperator) * @since 0.5.1 */ public int[] scanLeft(IntBinaryOperator accumulator) { Spliterator.OfInt spliterator = spliterator(); long size = spliterator.getExactSizeIfKnown(); IntBuffer buf = new IntBuffer(size >= 0 && size <= Integer.MAX_VALUE ? (int) size : INITIAL_SIZE); delegate(spliterator).forEachOrdered(i -> buf.add(buf.size == 0 ? i : accumulator.applyAsInt(buf.data[buf.size - 1], i))); return buf.toArray(); }
/** * Represents a range implicitly starting at 0. * * @param max an exclusive end of the range. * @param f a function which transforms a number in the range into a value. * @return an iterator which yields the values returned by {@code f} */ public static OfInt range(long max, LongToIntFunction f) { return range(0, max, f); }
/** * Creates a <strong>parallel</strong> {@code int} stream from the given Spliterator. This operation is similar to * calling {@code StreamSupport.intStream(spliterator, true)} with the difference that a parallel * <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html#StreamOps">terminal * operation</a> will be executed in the given {@link ForkJoinPool}. * * @param spliterator A {@code Spliterator.OfInt} describing the stream elements. Must not be {@code null}. * @param workerPool Thread pool for parallel execution of a terminal operation. Must not be {@code null}. * @return A parallel {@code int} stream that executes a terminal operation in the given {@link ForkJoinPool}. * @see StreamSupport#intStream(Spliterator.OfInt, boolean) */ public static IntStream parallelStream(Spliterator.OfInt spliterator, ForkJoinPool workerPool) { requireNonNull(spliterator, "Spliterator must not be null"); return new ParallelIntStreamSupport(intStream(spliterator, true), workerPool); }
/** * Creates a <strong>parallel</strong> {@code int} stream from the given Spliterator supplier. This operation is * similar to calling {@code StreamSupport.intStream(supplier, characteristics, true)} with the difference that a * parallel * <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html#StreamOps">terminal * operation</a> will be executed in the given {@link ForkJoinPool}. * * @param supplier A {@code Supplier} of a {@code Spliterator.OfInt}. Must not be {@code null}. * @param characteristics Spliterator characteristics of the supplied {@code Spliterator}. The characteristics must * be equal to {@code supplier.get().characteristics()}, otherwise undefined behavior may occur when terminal * operation commences. * @param workerPool Thread pool for parallel execution of a terminal operation. Must not be {@code null}. * @return A parallel {@code int} stream that executes a terminal operation in the given {@link ForkJoinPool}. * @see StreamSupport#intStream(Supplier, int, boolean) */ public static IntStream parallelStream(Supplier<? extends Spliterator.OfInt> supplier, int characteristics, ForkJoinPool workerPool) { requireNonNull(supplier, "Supplier must not be null"); return new ParallelIntStreamSupport(intStream(supplier, characteristics, true), workerPool); }
/** * Returns a stream consisting of the remaining elements of this stream * after discarding the first {@code n} elements of the stream. If this * stream contains fewer than {@code n} elements then an empty stream will * be returned. * * <p> * This is a stateful quasi-intermediate operation. Unlike * {@link #skip(long)} it skips the first elements even if the stream is * unordered. The main purpose of this method is to workaround the problem * of skipping the first elements from non-sized source with further * parallel processing and unordered terminal operation (such as * {@link #forEach(IntConsumer)}). This problem was fixed in OracleJDK 8u60. * * <p> * Also it behaves much better with infinite streams processed in parallel. * For example, * {@code IntStreamEx.iterate(0, i->i+1).skip(1).limit(100).parallel().toArray()} * will likely to fail with {@code OutOfMemoryError}, but will work nicely * if {@code skip} is replaced with {@code skipOrdered}. * * <p> * For sequential streams this method behaves exactly like * {@link #skip(long)}. * * @param n the number of leading elements to skip * @return the new stream * @throws IllegalArgumentException if {@code n} is negative * @see #skip(long) * @since 0.3.2 */ public IntStreamEx skipOrdered(long n) { Spliterator.OfInt spliterator = (isParallel() ? StreamSupport.intStream(spliterator(), false) : stream()).skip( n).spliterator(); return delegate(spliterator); }
/** * Returns a stream containing cumulative results of applying the * accumulation function going left to right. * * <p> * This is a stateful * <a href="package-summary.html#StreamOps">quasi-intermediate</a> * operation. * * <p> * This operation resembles {@link #scanLeft(IntBinaryOperator)}, but unlike * {@code scanLeft} this operation is intermediate and accumulation function * must be associative. * * <p> * This method cannot take all the advantages of parallel streams as it must * process elements strictly left to right. Using an unordered source or * removing the ordering constraint with {@link #unordered()} may improve * the parallel processing speed. * * @param op an <a href="package-summary.html#Associativity">associative</a> * , <a href="package-summary.html#NonInterference">non-interfering * </a>, <a href="package-summary.html#Statelessness">stateless</a> * function for computing the next element based on the previous one * @return the new stream. * @see #scanLeft(IntBinaryOperator) * @since 0.6.1 */ public IntStreamEx prefix(IntBinaryOperator op) { return delegate(new PrefixOps.OfInt(spliterator(), op)); }
/** * Returns a sequential {@code IntStreamEx} containing a single element. * * @param element the single element * @return a singleton sequential stream */ public static IntStreamEx of(int element) { return of(new ConstSpliterator.OfInt(element, 1, true)); }
/** * Returns a sequential {@link IntStreamEx} created from given * {@link java.util.Spliterator.OfInt}. * * @param spliterator a spliterator to create the stream from. * @return the new stream * @since 0.3.4 */ public static IntStreamEx of(Spliterator.OfInt spliterator) { return new IntStreamEx(spliterator, StreamContext.SEQUENTIAL); }
/** * Returns a sequential, ordered {@link IntStreamEx} created from given * {@link java.util.PrimitiveIterator.OfInt}. * * <p> * This method is roughly equivalent to * {@code IntStreamEx.of(Spliterators.spliteratorUnknownSize(iterator, ORDERED))} * , but may show better performance for parallel processing. * * <p> * Use this method only if you cannot provide better Stream source. * * @param iterator an iterator to create the stream from. * @return the new stream * @since 0.5.1 */ public static IntStreamEx of(PrimitiveIterator.OfInt iterator) { return of(new UnknownSizeSpliterator.USOfInt(iterator)); }
/** * Returns a sequential unordered {@code IntStreamEx} of given length which * elements are equal to supplied value. * * @param value the constant value * @param length the length of the stream * @return a new {@code IntStreamEx} * @since 0.1.2 */ public static IntStreamEx constant(int value, long length) { return of(new ConstSpliterator.OfInt(value, length, false)); }
/** * Returns the spliterator which covers all the elements emitted by this * emitter. * * @return the new spliterator */ default Spliterator.OfInt spliterator() { return new EmitterSpliterator.OfInt(this); }