@Override public final Array<T> forEachDouble(DoubleConsumer consumer) { final int length = length(); if (isParallel() && length > 0) { final int processors = Runtime.getRuntime().availableProcessors(); final int splitThreshold = parallel ? Math.max(length() / processors, 10000) : Integer.MAX_VALUE; final ForEach action = new ForEach(0, length - 1, splitThreshold, consumer); ForkJoinPool.commonPool().invoke(action); } else { for (int i=0; i<length; ++i) { final double value = getDouble(i); consumer.accept(value); } } return this; }
public void autoMatchAll(DoubleConsumer progressReceiver) { if (autoMatchClasses(ClassifierLevel.Initial, absClassAutoMatchThreshold, relClassAutoMatchThreshold, progressReceiver)) { autoMatchClasses(ClassifierLevel.Initial, absClassAutoMatchThreshold, relClassAutoMatchThreshold, progressReceiver); } autoMatchLevel(ClassifierLevel.Intermediate, progressReceiver); autoMatchLevel(ClassifierLevel.Full, progressReceiver); autoMatchLevel(ClassifierLevel.Extra, progressReceiver); boolean matchedAny; do { matchedAny = autoMatchMethodArgs(ClassifierLevel.Full, absMethodArgAutoMatchThreshold, relMethodArgAutoMatchThreshold, progressReceiver); } while (matchedAny); env.getCache().clear(); }
@Override public final DoubleStream peek(DoubleConsumer action) { Objects.requireNonNull(action); return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, 0) { @Override Sink<Double> opWrapSink(int flags, Sink<Double> sink) { return new Sink.ChainedDouble<Double>(sink) { @Override public void accept(double t) { action.accept(t); downstream.accept(t); } }; } }; }
private <T, C> void runInParallel(List<T> workSet, Consumer<T> worker, DoubleConsumer progressReceiver) { if (workSet.isEmpty()) return; AtomicInteger itemsDone = new AtomicInteger(); int updateRate = Math.max(1, workSet.size() / 200); workSet.parallelStream().forEach(workItem -> { worker.accept(workItem); int cItemsDone = itemsDone.incrementAndGet(); if ((cItemsDone % updateRate) == 0) { progressReceiver.accept((double) cItemsDone / workSet.size()); } }); }
/** * 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}. * * <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 DoubleStream} */ public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) { Objects.requireNonNull(f); Spliterator.OfDouble spliterator = new Spliterators.AbstractDoubleSpliterator(Long.MAX_VALUE, Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) { double prev; boolean started; @Override public boolean tryAdvance(DoubleConsumer action) { Objects.requireNonNull(action); double t; if (started) t = f.applyAsDouble(prev); else { t = seed; started = true; } action.accept(prev = t); return true; } }; return StreamSupport.doubleStream(spliterator, false); }
@Test(timeOut = 20_000, groups = "non-deterministic") public void testNextGaussianStatistically() throws SeedException { final BaseRandom rng = createRng(); final int iterations = 10000; final SynchronizedDescriptiveStatistics stats = new SynchronizedDescriptiveStatistics(); rng.gaussians(iterations).spliterator().forEachRemaining((DoubleConsumer) stats::addValue); final double observedSD = stats.getStandardDeviation(); Reporter.log("Expected SD for Gaussians: 1, observed SD: " + observedSD); assertGreaterOrEqual(observedSD, 0.97); assertLessOrEqual(observedSD, 1.03); assertGreaterOrEqual(stats.getMax(), 2.0); assertLessOrEqual(stats.getMin(), -2.0); assertGreaterOrEqual(stats.getMean(), -0.1); assertLessOrEqual(stats.getMean(), 0.1); final double median = stats.getPercentile(50); assertGreaterOrEqual(median, -0.1); assertLessOrEqual(median, 0.1); }
@Override public boolean tryAdvance(DoubleConsumer action) { boolean test = true; if (takeOrDrop && // If can take checkCancelOnCount() && // and if not cancelled s.tryAdvance(this) && // and if advanced one element (test = p.test(t))) { // and test on element passes action.accept(t); // then accept element return true; } else { // Taking is finished takeOrDrop = false; // Cancel all further traversal and splitting operations // only if test of element failed (short-circuited) if (!test) cancel.set(true); return false; } }
@Test(dataProvider = "DoubleSpinedBuffer", groups = { "serialization-hostile" }) public void testLongLastSplit(double[] array, SpinedBuffer.OfDouble sb) { Spliterator.OfDouble spliterator = sb.spliterator(); Spliterator.OfDouble split = spliterator.trySplit(); long splitSizes = (split == null) ? 0 : split.getExactSizeIfKnown(); long lastSplitSize = spliterator.getExactSizeIfKnown(); splitSizes += lastSplitSize; assertEquals(splitSizes, array.length); List<Double> contentOfLastSplit = new ArrayList<>(); spliterator.forEachRemaining((DoubleConsumer) contentOfLastSplit::add); assertEquals(contentOfLastSplit.size(), lastSplitSize); List<Double> end = Arrays.stream(array) .boxed() .skip(array.length - lastSplitSize) .collect(Collectors.toList()); assertEquals(contentOfLastSplit, end); }
@Test(dataProvider = "Node.Builder<Double>") public void testDoubleIteration(List<Double> l, Function<Integer, Node.Builder.OfDouble> m) { Node.Builder.OfDouble nb = m.apply(l.size()); nb.begin(l.size()); for (Double i : l) { nb.accept((double) i); } nb.end(); Node.OfDouble n = nb.build(); assertEquals(n.count(), l.size()); { List<Double> _l = new ArrayList<>(); n.forEach((DoubleConsumer) _l::add); assertContents(_l, l); } }
@Override default Node.OfDouble truncate(long from, long to, IntFunction<Double[]> generator) { if (from == 0 && to == count()) return this; long size = to - from; Spliterator.OfDouble spliterator = spliterator(); Node.Builder.OfDouble nodeBuilder = Nodes.doubleBuilder(size); nodeBuilder.begin(size); for (int i = 0; i < from && spliterator.tryAdvance((DoubleConsumer) e -> { }); i++) { } if (to == count()) { spliterator.forEachRemaining((DoubleConsumer) nodeBuilder); } else { for (int i = 0; i < size && spliterator.tryAdvance((DoubleConsumer) nodeBuilder); i++) { } } nodeBuilder.end(); return nodeBuilder.build(); }
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)); }
private void animateValue(double from, double to, Duration duration, Interpolator interpolator, DoubleConsumer consumer, EventHandler<ActionEvent> l) { timeline.stop(); timeline.getKeyFrames().clear(); KeyValue keyValue = new KeyValue(new WritableValue<Double>() { @Override public Double getValue() { return from; } @Override public void setValue(Double value) { consumer.accept(value); } }, to, interpolator == null ? Interpolator.LINEAR : interpolator); timeline.getKeyFrames().add(new KeyFrame(duration, keyValue)); timeline.setOnFinished(l); timeline.play(); }
public void forEachRemaining(DoubleConsumer consumer) { if (consumer == null) throw new NullPointerException(); long i = index, f = fence; if (i < f) { index = f; Random r = rng; double o = origin, b = bound; do { consumer.accept(r.internalNextDouble(o, b)); } while (++i < f); } }
@Override public void forEachOrdered(DoubleConsumer consumer) { if (!isParallel()) { adapt(sourceStageSpliterator()).forEachRemaining(consumer); } else { super.forEachOrdered(consumer); } }
public boolean tryAdvance(DoubleConsumer consumer) { if (consumer == null) throw new NullPointerException(); long i = index, f = fence; if (i < f) { consumer.accept(ThreadLocalRandom.current().internalNextDouble(origin, bound)); index = i + 1; return true; } return false; }
@Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) public void testDoubleOps(String name, final TestData.OfDouble data) { class RecordingConsumer extends AbstractRecordingConsumer<Double> implements DoubleConsumer { public void accept(double t) { list.add(t); } } final RecordingConsumer b = new RecordingConsumer(); withData(data) .stream(s -> s.peek(b)) .before(b::before) .after(b::after) .exercise(); }
/** * Creates an {@code PrimitiveIterator.OfDouble} from a * {@code Spliterator.OfDouble}. * * <p>Traversal of elements should be accomplished through the iterator. * The behaviour of traversal is undefined if the spliterator is operated * after the iterator is returned. * * @param spliterator The spliterator * @return An iterator * @throws NullPointerException if the given spliterator is {@code null} */ public static PrimitiveIterator.OfDouble iterator(Spliterator.OfDouble spliterator) { Objects.requireNonNull(spliterator); class Adapter implements PrimitiveIterator.OfDouble, DoubleConsumer { boolean valueReady = false; double nextElement; @Override public void accept(double t) { valueReady = true; nextElement = t; } @Override public boolean hasNext() { if (!valueReady) spliterator.tryAdvance(this); return valueReady; } @Override public double nextDouble() { if (!valueReady && !hasNext()) throw new NoSuchElementException(); else { valueReady = false; return nextElement; } } } return new Adapter(); }
public boolean autoMatchFields(ClassifierLevel level, double absThreshold, double relThreshold, DoubleConsumer progressReceiver) { AtomicInteger totalUnmatched = new AtomicInteger(); Map<FieldInstance, FieldInstance> matches = match(level, absThreshold, relThreshold, cls -> cls.getFields(), FieldClassifier::rank, progressReceiver, totalUnmatched); for (Map.Entry<FieldInstance, FieldInstance> entry : matches.entrySet()) { match(entry.getKey(), entry.getValue()); } System.out.println("Auto matched "+matches.size()+" fields ("+totalUnmatched.get()+" unmatched)"); return !matches.isEmpty(); }
@Override public boolean tryAdvance(DoubleConsumer action) { if (action == null) throw new NullPointerException(); if (index >= 0 && index < fence) { action.accept(array[index++]); return true; } return false; }
private long run_benchmark(long iterations, DoubleConsumer test) { long seed = 12345; Random rand = new Random(); rand.setSeed(seed); long startTime = System.nanoTime(); for(int i = 0; i < iterations; i++) { test.accept(rand.nextDouble()); } long endTime = System.nanoTime(); return TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS); }
@Override public boolean tryAdvance(DoubleConsumer action) { Objects.requireNonNull(action); if (count == -2) { action.accept(first); count = -1; return true; } else { return false; } }
public boolean tryAdvance(DoubleConsumer consumer) { if (consumer == null) throw new NullPointerException(); long i = index, f = fence; if (i < f) { consumer.accept(rng.internalNextDouble(origin, bound)); index = i + 1; return true; } return false; }
@Override public void forEach(DoubleConsumer consumer) { if (!isParallel()) { adapt(sourceStageSpliterator()).forEachRemaining(consumer); } else { super.forEach(consumer); } }
@Override public void forEach(Consumer<? super Double> consumer) { if (consumer instanceof DoubleConsumer) { forEach((DoubleConsumer) consumer); } else { if (Tripwire.ENABLED) Tripwire.trip(getClass(), "{0} calling SpinedBuffer.OfDouble.forEach(Consumer)"); spliterator().forEachRemaining(consumer); } }
@Override protected void arrayForEach(double[] array, int from, int to, DoubleConsumer consumer) { for (int i = from; i < to; i++) consumer.accept(array[i]); }
@Override public boolean tryAdvance(DoubleConsumer consumer) { Objects.requireNonNull(consumer); boolean hasNext = doAdvance(); if (hasNext) consumer.accept(buffer.get(nextToConsume)); return hasNext; }
@Override public boolean tryAdvance(DoubleConsumer action) { Objects.requireNonNull(action); action.accept(s.getAsDouble()); return true; }
@Override default Node.OfDouble truncate(long from, long to, IntFunction<Double[]> generator) { if (from == 0 && to == count()) return this; long size = to - from; Spliterator.OfDouble spliterator = spliterator(); Node.Builder.OfDouble nodeBuilder = Nodes.doubleBuilder(size); nodeBuilder.begin(size); for (int i = 0; i < from && spliterator.tryAdvance((DoubleConsumer) e -> { }); i++) { } for (int i = 0; (i < size) && spliterator.tryAdvance((DoubleConsumer) nodeBuilder); i++) { } nodeBuilder.end(); return nodeBuilder.build(); }
/** * {@inheritDoc} * @implSpec * If the action is an instance of {@code DoubleConsumer} then it is * cast to {@code DoubleConsumer} and passed to * {@link #forEachRemaining}; otherwise the action is adapted to * an instance of {@code DoubleConsumer}, by boxing the argument of * {@code DoubleConsumer}, and then passed to * {@link #forEachRemaining}. */ @Override default void forEachRemaining(Consumer<? super Double> action) { if (action instanceof DoubleConsumer) { forEachRemaining((DoubleConsumer) action); } else { // The method reference action::accept is never null Objects.requireNonNull(action); if (Tripwire.ENABLED) Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfDouble.forEachRemainingDouble(action::accept)"); forEachRemaining((DoubleConsumer) action::accept); } }
/** * {@inheritDoc} * @implSpec * If the action is an instance of {@code DoubleConsumer} then it is * cast to {@code DoubleConsumer} and passed to * {@link #tryAdvance(java.util.function.DoubleConsumer)}; otherwise * the action is adapted to an instance of {@code DoubleConsumer}, by * boxing the argument of {@code DoubleConsumer}, and then passed to * {@link #tryAdvance(java.util.function.DoubleConsumer)}. */ @Override default boolean tryAdvance(Consumer<? super Double> action) { if (action instanceof DoubleConsumer) { return tryAdvance((DoubleConsumer) action); } else { if (Tripwire.ENABLED) Tripwire.trip(getClass(), "{0} calling Spliterator.OfDouble.tryAdvance((DoubleConsumer) action::accept)"); return tryAdvance((DoubleConsumer) action::accept); } }
/** * {@inheritDoc} * @implSpec * If the action is an instance of {@code DoubleConsumer} then it is * cast to {@code DoubleConsumer} and passed to * {@link #forEachRemaining(java.util.function.DoubleConsumer)}; * otherwise the action is adapted to an instance of * {@code DoubleConsumer}, by boxing the argument of * {@code DoubleConsumer}, and then passed to * {@link #forEachRemaining(java.util.function.DoubleConsumer)}. */ @Override default void forEachRemaining(Consumer<? super Double> action) { if (action instanceof DoubleConsumer) { forEachRemaining((DoubleConsumer) action); } else { if (Tripwire.ENABLED) Tripwire.trip(getClass(), "{0} calling Spliterator.OfDouble.forEachRemaining((DoubleConsumer) action::accept)"); forEachRemaining((DoubleConsumer) action::accept); } }