/** * 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); }
/** * Returns an infinite sequential ordered {@code LongStream} 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 LongStream} 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 LongStream} */ public static LongStream iterate(final long seed, final LongUnaryOperator f) { Objects.requireNonNull(f); final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() { long t = seed; @Override public boolean hasNext() { return true; } @Override public long nextLong() { long v = t; t = f.applyAsLong(t); return v; } }; return StreamSupport.longStream(Spliterators.spliteratorUnknownSize( iterator, Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false); }
/** * Returns an infinite sequential ordered {@code DoubleStream} 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 DoubleStream} * 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 DoubleStream} */ public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) { Objects.requireNonNull(f); final PrimitiveIterator.OfDouble iterator = new PrimitiveIterator.OfDouble() { double t = seed; @Override public boolean hasNext() { return true; } @Override public double nextDouble() { double v = t; t = f.applyAsDouble(t); return v; } }; return StreamSupport.doubleStream(Spliterators.spliteratorUnknownSize( iterator, Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false); }
public void testIntForEachRemainingWithNull() { PrimitiveIterator.OfInt i = new PrimitiveIterator.OfInt() { @Override public int nextInt() { return 0; } @Override public boolean hasNext() { return false; } }; executeAndCatch(() -> i.forEachRemaining((IntConsumer) null)); executeAndCatch(() -> i.forEachRemaining((Consumer<Integer>) null)); }
public void testLongForEachRemainingWithNull() { PrimitiveIterator.OfLong i = new PrimitiveIterator.OfLong() { @Override public long nextLong() { return 0; } @Override public boolean hasNext() { return false; } }; executeAndCatch(() -> i.forEachRemaining((LongConsumer) null)); executeAndCatch(() -> i.forEachRemaining((Consumer<Long>) null)); }
public void testDoubleForEachRemainingWithNull() { PrimitiveIterator.OfDouble i = new PrimitiveIterator.OfDouble() { @Override public double nextDouble() { return 0; } @Override public boolean hasNext() { return false; } }; executeAndCatch(() -> i.forEachRemaining((DoubleConsumer) null)); executeAndCatch(() -> i.forEachRemaining((Consumer<Double>) null)); }
@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()); }
public void testIntForEachRemainingWithNull() { PrimitiveIterator.OfInt i = new PrimitiveIterator.OfInt() { @Override public int nextInt() { return 0; } @Override public boolean hasNext() { return false; } }; assertThrowsNPE(() -> i.forEachRemaining((IntConsumer) null)); assertThrowsNPE(() -> i.forEachRemaining((Consumer<Integer>) null)); }
public void testLongForEachRemainingWithNull() { PrimitiveIterator.OfLong i = new PrimitiveIterator.OfLong() { @Override public long nextLong() { return 0; } @Override public boolean hasNext() { return false; } }; assertThrowsNPE(() -> i.forEachRemaining((LongConsumer) null)); assertThrowsNPE(() -> i.forEachRemaining((Consumer<Long>) null)); }
public void testDoubleForEachRemainingWithNull() { PrimitiveIterator.OfDouble i = new PrimitiveIterator.OfDouble() { @Override public double nextDouble() { return 0; } @Override public boolean hasNext() { return false; } }; assertThrowsNPE(() -> i.forEachRemaining((DoubleConsumer) null)); assertThrowsNPE(() -> i.forEachRemaining((Consumer<Double>) null)); }
@Test(dataProvider = "cases") public void testBitsetStream(String name, IntStream data) { BitSet bs = data.collect(BitSet::new, BitSet::set, BitSet::or); assertEquals(bs.cardinality(), bs.stream().count()); int[] indexHolder = new int[] { -1 }; bs.stream().forEach(i -> { int ei = indexHolder[0]; indexHolder[0] = bs.nextSetBit(ei + 1); assertEquals(i, indexHolder[0]); }); 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); if (i == Integer.MAX_VALUE) break; // or (i + 1) would overflow } assertFalse(it.hasNext()); }
/** {@inheritDoc} */ @Override public VarianceData calculateRegionInfo(DoubleStream s, int size) { PrimitiveIterator.OfDouble itr = s.iterator(); int i = 0; double mean = 0.0; double m2 = 0.0; // Here we calculate variance and mean by incremental computation. while (itr.hasNext()) { i++; double x = itr.next(); double delta = x - mean; mean += delta / i; double delta2 = x - mean; m2 += delta * delta2; } return new VarianceData(m2 / i, size, mean); }
/** * Returns a primitive iterator for this range * @return the primitive iterator */ private PrimitiveIterator.OfInt iteratorOfInt() { return new PrimitiveIterator.OfInt() { private int value = start(); @Override public boolean hasNext() { if (excludes != null) { while (excludes.test(value) && inBounds(value)) { value = ascend ? value + step : value - step; } } return inBounds(value); } @Override public int nextInt() { final int next = value; value = ascend ? value + step : value - step; return next; } }; }
/** * Returns a primitive iterator for this range * @return the primitive iterator */ private PrimitiveIterator.OfDouble iteratorOfDouble() { return new PrimitiveIterator.OfDouble() { private double value = start; @Override public boolean hasNext() { if (excludes != null) { while (excludes.test(value) && inBounds(value)) { value = ascend ? value + step : value - step; } } return inBounds(value); } @Override public double nextDouble() { final double next = value; value = ascend ? value + step : value - step; return next; } }; }
/** * Returns a primitive iterator for this range * @return the primitive iterator */ private PrimitiveIterator.OfLong iteratorOfLong() { return new PrimitiveIterator.OfLong() { private long value = start; @Override public boolean hasNext() { if (excludes != null) { while (excludes.test(value) && inBounds(value)) { value = ascend ? value + step : value - step; } } return inBounds(value); } @Override public long nextLong() { final long next = value; value = ascend ? value + step : value - step; return next; } }; }
@Test() public void testRowIteratorWithPredicate() throws IOException { final DataFrame<String,String> frame = TestDataFrames.random(double.class, 20, 20); frame.applyValues(v -> Math.random() * 10); frame.col("C5").applyDoubles(v -> Double.NaN); frame.col("C10").applyDoubles(v -> Double.NaN); frame.rows().forEach(row -> { int count = 0; final PrimitiveIterator.OfDouble iterator1 = row.toDoubleStream().filter(v -> !Double.isNaN(v)).iterator(); final Iterator<DataFrameValue<String,String>> iterator2 = row.iterator(v -> !Double.isNaN(v.getDouble())); while (iterator1.hasNext()) { ++count; final double v1 = iterator1.next(); Assert.assertTrue(iterator2.hasNext(), "Row iterator has next"); final DataFrameValue<String,String> value = iterator2.next(); final double v2 = value.getDouble(); Assert.assertEquals(v2, v1, "Value match at " + value.rowKey() + ", " + value.colKey()); } final int colCount = row.size()-2; Assert.assertEquals(colCount, count, "The column count matches"); }); }
@Test() public void testColumnIteratorWithPredicate() throws IOException { final DataFrame<String, String> frame = TestDataFrames.random(double.class, 20, 20); frame.applyValues(v -> Math.random() * 10); frame.row("R5").applyDoubles(v -> Double.NaN); frame.row("R10").applyDoubles(v -> Double.NaN); frame.cols().forEach(column -> { int count = 0; final PrimitiveIterator.OfDouble iterator1 = column.values().filter(v -> !v.isNull()).mapToDouble(DataFrameValue::getDouble).iterator(); final Iterator<DataFrameValue<String, String>> iterator2 = column.iterator(v -> !Double.isNaN(v.getDouble())); while (iterator1.hasNext()) { ++count; final double v1 = iterator1.next(); Assert.assertTrue(iterator2.hasNext(), "Column iterator has next"); final DataFrameValue<String, String> value = iterator2.next(); final double v2 = value.getDouble(); Assert.assertEquals(v2, v1, "Value match at " + value.rowKey() + ", " + value.colKey()); } final int colCount = column.size() - 2; Assert.assertEquals(colCount, count, "The row count matches"); }); }
@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); if (i == Integer.MAX_VALUE) break; // or (i+1) would overflow } assertFalse(it.hasNext()); }
@Override protected PrimitiveIterator.OfInt createIterator(PieceStatistics pieceStatistics) { return new PrimitiveIterator.OfInt() { int i = 0; @Override public int nextInt() { return i++; } @Override public boolean hasNext() { while (i < pieceStatistics.getPiecesTotal() && pieceStatistics.getCount(i) == 0) { i++; } return i < pieceStatistics.getPiecesTotal(); } }; }
@Override protected PrimitiveIterator.OfInt createIterator(PieceStatistics pieceStatistics) { LinkedList<Integer> queue = orderedQueue(pieceStatistics); return new PrimitiveIterator.OfInt() { @Override public int nextInt() { if (random.isPresent()) { int i = Math.min(RANDOMIZED_SELECTION_SIZE, queue.size()); return queue.remove(random.get().nextInt(i)); } else { return queue.poll(); } } @Override public boolean hasNext() { return !queue.isEmpty(); } }; }
static PrimitiveIterator.OfInt masks(int bitmap) { if (bitmap == 0) { return EMPTY_INT; } long start = 1L << startIndex(bitmap); long end = 1L << (endIndex(bitmap) + 1); return new PrimitiveIterator.OfInt() { long mask = start; @Override public int nextInt() { long result = mask; mask <<= 1; return (int) result; } @Override public boolean hasNext() { return mask < end; } }; }
static PrimitiveIterator.OfInt reverseMasks(int bitmap) { if (bitmap == 0) { return EMPTY_INT; } long start = 1L << startIndex(bitmap); long end = 1L << endIndex(bitmap); return new PrimitiveIterator.OfInt() { long mask = end; @Override public int nextInt() { long result = mask; mask >>= 1; return (int) result; } @Override public boolean hasNext() { return mask >= start; } }; }
public IEntry<Long, V> nth(int idx) { PrimitiveIterator.OfInt masks = masks(); while (masks.hasNext()) { int mask = masks.nextInt(); if (isEntry(mask)) { if (idx-- == 0) { int entryIdx = entryIndex(mask); return new Maps.Entry<>(keys[entryIdx], (V) content[entryIdx]); } } else if (isNode(mask)) { Node<V> node = node(mask); if (idx < node.size()) { return node.nth(idx); } else { idx -= node.size(); } } } throw new IndexOutOfBoundsException(); }
PrimitiveIterator.OfLong iteratorVarLong(final int len) { return new PrimitiveIterator.OfLong() { private int i; private long x; @Override public boolean hasNext() { return i < len; } @Override public long nextLong() { remapIfNeeded(); x += readVarLong(buff); return x; } }; }