/** * Returns the parent of this task, or null if this task is the root * * @return the parent of this task, or null if this task is the root */ @SuppressWarnings("unchecked") protected K getParent() { return (K) getCompleter(); }
/** * Returns a suggested target leaf size based on the initial size estimate. * * @return suggested target leaf size */ public static long suggestTargetSize(long sizeEstimate) { long est = sizeEstimate / getLeafTarget(); return est > 0L ? est : 1L; }
/** * Indicates whether this task is the root node * * @return {@code true} if this task is the root node. */ protected boolean isRoot() { return getParent() == null; }
long sizeThreshold = getTargetSize(sizeEstimate); boolean forkRight = false; @SuppressWarnings("unchecked") K task = (K) this; while (sizeEstimate > sizeThreshold && (ls = rs.trySplit()) != null) { K leftChild, rightChild, taskToFork; task.leftChild = leftChild = task.makeChild(ls); task.rightChild = rightChild = task.makeChild(rs); task.setPendingCount(1); if (forkRight) { forkRight = false; taskToFork = leftChild; taskToFork.fork(); sizeEstimate = rs.estimateSize(); task.setLocalResult(task.doLeaf()); task.tryComplete();
protected ForEachOrderedTask(PipelineHelper<T> helper, Spliterator<S> spliterator, Sink<T> action) { super(null); this.helper = helper; this.spliterator = spliterator; this.targetSize = AbstractTask.suggestTargetSize(spliterator.estimateSize()); // Size map to avoid concurrent re-sizes this.completionMap = new ConcurrentHashMap<>(Math.max(16, AbstractTask.getLeafTarget() << 1), 0.75f, ForkJoinPool.getCommonPoolParallelism() + 1); this.action = action; this.leftPredecessor = null; }
@Override public void onCompletion(CountedCompleter<?> caller) { if (!isLeaf()) setLocalResult(concFactory.apply(leftChild.getLocalResult(), rightChild.getLocalResult())); super.onCompletion(caller); }
/** * Retrieves the local result for this task. If this task is the root, * retrieves the shared result instead. */ @Override public R getLocalResult() { if (isRoot()) { R answer = sharedResult.get(); return (answer == null) ? getEmptyResult() : answer; } else return super.getLocalResult(); }
/** * Sets a local result for this task. If this task is the root, set the * shared result instead (if not already set). * * @param localResult The result to set for this task */ @Override protected void setLocalResult(R localResult) { if (isRoot()) { if (localResult != null) sharedResult.compareAndSet(null, localResult); } else super.setLocalResult(localResult); }
long sizeThreshold = getTargetSize(sizeEstimate); boolean forkRight = false; @SuppressWarnings("unchecked") K task = (K) this; while (sizeEstimate > sizeThreshold && (ls = rs.trySplit()) != null) { K leftChild, rightChild, taskToFork; task.leftChild = leftChild = task.makeChild(ls); task.rightChild = rightChild = task.makeChild(rs); task.setPendingCount(1); if (forkRight) { forkRight = false; taskToFork = leftChild; taskToFork.fork(); sizeEstimate = rs.estimateSize(); task.setLocalResult(task.doLeaf()); task.tryComplete();
protected ForEachOrderedTask(PipelineHelper<T> helper, Spliterator<S> spliterator, Sink<T> action) { super(null); this.helper = helper; this.spliterator = spliterator; this.targetSize = AbstractTask.suggestTargetSize(spliterator.estimateSize()); // Size map to avoid concurrent re-sizes this.completionMap = new ConcurrentHashMap<>(Math.max(16, AbstractTask.getLeafTarget() << 1), 0.75f, ForkJoinPool.getCommonPoolParallelism() + 1); this.action = action; this.leftPredecessor = null; }
@Override public void onCompletion(CountedCompleter<?> caller) { if (!isLeaf()) setLocalResult(concFactory.apply(leftChild.getLocalResult(), rightChild.getLocalResult())); super.onCompletion(caller); }
/** * Retrieves the local result for this task. If this task is the root, * retrieves the shared result instead. */ @Override public R getLocalResult() { if (isRoot()) { R answer = sharedResult.get(); return (answer == null) ? getEmptyResult() : answer; } else return super.getLocalResult(); }
/** * Sets a local result for this task. If this task is the root, set the * shared result instead (if not already set). * * @param localResult The result to set for this task */ @Override protected void setLocalResult(R localResult) { if (isRoot()) { if (localResult != null) sharedResult.compareAndSet(null, localResult); } else super.setLocalResult(localResult); }
@Override public void onCompletion(CountedCompleter<?> caller) { if (!isLeaf()) { S leftResult = leftChild.getLocalResult(); leftResult.combine(rightChild.getLocalResult()); setLocalResult(leftResult); } // GC spliterator, left and right child super.onCompletion(caller); } }
/** * Returns a suggested target leaf size based on the initial size estimate. * * @return suggested target leaf size */ public static long suggestTargetSize(long sizeEstimate) { long est = sizeEstimate / getLeafTarget(); return est > 0L ? est : 1L; }
/** * Indicates whether this task is the root node * * @return {@code true} if this task is the root node. */ protected boolean isRoot() { return getParent() == null; }
/** * Returns the parent of this task, or null if this task is the root * * @return the parent of this task, or null if this task is the root */ @SuppressWarnings("unchecked") protected K getParent() { return (K) getCompleter(); }
@Override public void onCompletion(CountedCompleter<?> caller) { if (!isLeaf()) { S leftResult = leftChild.getLocalResult(); leftResult.combine(rightChild.getLocalResult()); setLocalResult(leftResult); } // GC spliterator, left and right child super.onCompletion(caller); } }
UnorderedSliceSpliterator(T_SPLITR s, long skip, long limit) { this.s = s; this.unlimited = limit < 0; this.skipThreshold = limit >= 0 ? limit : 0; this.chunkSize = limit >= 0 ? (int) Math.min(CHUNK_SIZE, ((skip + limit) / AbstractTask.getLeafTarget()) + 1) : CHUNK_SIZE; this.permits = new AtomicLong(limit >= 0 ? skip + limit : skip); }
/** * Returns whether this node is a "leftmost" node -- whether the path from * the root to this node involves only traversing leftmost child links. For * a leaf node, this means it is the first leaf node in the encounter order. * * @return {@code true} if this node is a "leftmost" node */ protected boolean isLeftmostNode() { @SuppressWarnings("unchecked") K node = (K) this; while (node != null) { K parent = node.getParent(); if (parent != null && parent.leftChild != node) return false; node = parent; } return true; } }