/** * Flatten, in parallel, a {@link Node.OfInt}. A flattened node is one that * has no children. If the node is already flat, it is simply returned. * * <p><b>Implementation Requirements:</b><br> * If a new node is to be created, a new int[] array is created whose length * is {@link Node#count()}. Then the node tree is traversed and leaf node * elements are placed in the array concurrently by leaf tasks at the * correct offsets. * * @param node the node to flatten * @return a flat {@code Node.OfInt} */ public static Node.OfInt flattenInt(Node.OfInt node) { if (node.getChildCount() > 0) { long size = node.count(); if (size >= MAX_ARRAY_SIZE) throw new IllegalArgumentException(BAD_SIZE); int[] array = new int[(int) size]; new ToArrayTask.OfInt(node, array, 0).invoke(); return node(array); } else { return node; } }
Node.OfInt n = (Node.OfInt) helper.evaluate(spliterator, true, generator); int[] content = n.asPrimitiveArray();
/** * Flatten, in parallel, a {@link Node.OfInt}. A flattened node is one that * has no children. If the node is already flat, it is simply returned. * * <p><b>Implementation Requirements:</b><br> * If a new node is to be created, a new int[] array is created whose length * is {@link Node#count()}. Then the node tree is traversed and leaf node * elements are placed in the array concurrently by leaf tasks at the * correct offsets. * * @param node the node to flatten * @return a flat {@code Node.OfInt} */ public static Node.OfInt flattenInt(Node.OfInt node) { if (node.getChildCount() > 0) { long size = node.count(); if (size >= MAX_ARRAY_SIZE) throw new IllegalArgumentException(BAD_SIZE); int[] array = new int[(int) size]; new ToArrayTask.OfInt(node, array, 0).invoke(); return node(array); } else { return node; } }
/** * {@inheritDoc} * * @param consumer a {@code Consumer} that is to be invoked with each * element in this {@code Node}. If this is an * {@code IntConsumer}, it is cast to {@code IntConsumer} so the * elements may be processed without boxing. */ static void forEach(Node.OfInt this_, Consumer<? super Integer> consumer) { if (consumer instanceof IntConsumer) { this_.forEach((IntConsumer) consumer); } else { this_.spliterator().forEachRemaining(consumer); } }
@Override public <P_IN> Node<Integer> opEvaluateParallel(PipelineHelper<Integer> helper, Spliterator<P_IN> spliterator, IntFunction<Integer[]> generator) { if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) { return helper.evaluate(spliterator, false, generator); } else { Node.OfInt n = (Node.OfInt) helper.evaluate(spliterator, true, generator); int[] content = n.asPrimitiveArray(); java8.util.J8Arrays.parallelSort(content); return Nodes.node(content); } } }
static Node.OfInt truncate(Node.OfInt this_, long from, long to, IntFunction<Integer[]> generator) { if (from == 0 && to == this_.count()) return this_; long size = to - from; Spliterator.OfInt spliterator = this_.spliterator(); Node.Builder.OfInt nodeBuilder = Nodes.intBuilder(size); nodeBuilder.begin(size); for (int i = 0; i < from && spliterator.tryAdvance((IntConsumer) e -> { }); i++) { } if (to == this_.count()) { spliterator.forEachRemaining((IntConsumer) nodeBuilder); } else { for (int i = 0; i < size && spliterator.tryAdvance((IntConsumer) nodeBuilder); i++) { } } nodeBuilder.end(); return nodeBuilder.build(); }
static Node.OfInt truncate(Node.OfInt this_, long from, long to, IntFunction<Integer[]> generator) { if (from == 0 && to == this_.count()) return this_; long size = to - from; Spliterator.OfInt spliterator = this_.spliterator(); Node.Builder.OfInt nodeBuilder = Nodes.intBuilder(size); nodeBuilder.begin(size); for (int i = 0; i < from && spliterator.tryAdvance((IntConsumer) e -> { }); i++) { } if (to == this_.count()) { spliterator.forEachRemaining((IntConsumer) nodeBuilder); } else { for (int i = 0; i < size && spliterator.tryAdvance((IntConsumer) nodeBuilder); i++) { } } nodeBuilder.end(); return nodeBuilder.build(); }
/** * {@inheritDoc} * * @param consumer a {@code Consumer} that is to be invoked with each * element in this {@code Node}. If this is an * {@code IntConsumer}, it is cast to {@code IntConsumer} so the * elements may be processed without boxing. */ static void forEach(Node.OfInt this_, Consumer<? super Integer> consumer) { if (consumer instanceof IntConsumer) { this_.forEach((IntConsumer) consumer); } else { this_.spliterator().forEachRemaining(consumer); } }
@Override public <P_IN> Node<Integer> opEvaluateParallel(PipelineHelper<Integer> helper, Spliterator<P_IN> spliterator, IntFunction<Integer[]> generator) { if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) { return helper.evaluate(spliterator, false, generator); } else { Node.OfInt n = (Node.OfInt) helper.evaluate(spliterator, true, generator); int[] content = n.asPrimitiveArray(); java8.util.J8Arrays.parallelSort(content); return Nodes.node(content); } } }
/** * {@inheritDoc} * * <p><b>Implementation Requirements:</b><br> the default implementation invokes {@link Node.OfInt#asPrimitiveArray()} to * obtain an int[] array then and copies the elements from that int[] * array into the boxed Integer[] array. This is not efficient and it * is recommended to invoke {@link #copyInto(Object, int)}. */ static void copyInto(Node.OfInt this_, Integer[] boxed, int offset) { int[] array = this_.asPrimitiveArray(); for (int i = 0; i < array.length; i++) { boxed[offset + i] = array[i]; } }
/** * {@inheritDoc} * * <p><b>Implementation Requirements:</b><br> the default implementation invokes {@link Node.OfInt#asPrimitiveArray()} to * obtain an int[] array then and copies the elements from that int[] * array into the boxed Integer[] array. This is not efficient and it * is recommended to invoke {@link #copyInto(Object, int)}. */ static void copyInto(Node.OfInt this_, Integer[] boxed, int offset) { int[] array = this_.asPrimitiveArray(); for (int i = 0; i < array.length; i++) { boxed[offset + i] = array[i]; } }
@Override public final int[] toArray() { return Nodes.flattenInt((Node.OfInt) evaluateToArrayNode(WhileOps.INT_ARR_GEN)) .asPrimitiveArray(); }
@Override public final int[] toArray() { return Nodes.flattenInt((Node.OfInt) evaluateToArrayNode(WhileOps.INT_ARR_GEN)) .asPrimitiveArray(); }