/* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ package extra166y; import jsr166y.*; import static extra166y.Ops.*; import java.util.*; import java.util.concurrent.atomic.*; import java.lang.reflect.Array; /** * A prefix view of ParallelArray that causes operations to apply * to mappings of elements, not to the elements themselves. * Instances of this class may be constructed only via prefix * methods of ParallelArray or its other prefix classes. */ public abstract class ParallelArrayWithMapping extends AbstractParallelAnyArray.OPap { ParallelArrayWithMapping(ForkJoinPool ex, int origin, int fence, T[] array) { super(ex, origin, fence, array); } /** * Applies the given procedure to elements. * @param procedure the procedure */ public void apply(Procedure procedure) { ex.invoke(new PAS.FJOApply(this, origin, fence, null, procedure)); } /** * Returns reduction of elements. * @param reducer the reducer * @param base the result for an empty array * @return reduction */ public U reduce(Reducer reducer, U base) { PAS.FJOReduce f = new PAS.FJOReduce (this, origin, fence, null, reducer, base); ex.invoke(f); return (U)(f.result); } /** * Returns some element matching bound and filter * constraints, or null if none. * @return an element, or null if none */ public U any() { int i = anyIndex(); return (i < 0) ? null : (U)oget(i); } /** * Returns the minimum element, or null if empty. * @param comparator the comparator * @return minimum element, or null if empty */ public U min(Comparator comparator) { return reduce(CommonOps.minReducer(comparator), null); } /** * Returns the minimum element, or null if empty, * assuming that all elements are Comparables. * @return minimum element, or null if empty * @throws ClassCastException if any element is not Comparable */ public U min() { return reduce((Reducer)(CommonOps.castedMinReducer()), null); } /** * Returns the maximum element, or null if empty. * @param comparator the comparator * @return maximum element, or null if empty */ public U max(Comparator comparator) { return reduce(CommonOps.maxReducer(comparator), null); } /** * Returns the maximum element, or null if empty, * assuming that all elements are Comparables. * @return maximum element, or null if empty * @throws ClassCastException if any element is not Comparable */ public U max() { return reduce((Reducer)(CommonOps.castedMaxReducer()), null); } /** * Returns summary statistics, using the given comparator * to locate minimum and maximum elements. * @param comparator the comparator to use for * locating minimum and maximum elements * @return the summary */ public ParallelArray.SummaryStatistics summary (Comparator comparator) { PAS.FJOStats f = new PAS.FJOStats (this, origin, fence, null, comparator); ex.invoke(f); return (ParallelArray.SummaryStatistics)f; } /** * Returns summary statistics, assuming that all elements are * Comparables. * @return the summary */ public ParallelArray.SummaryStatistics summary() { return summary((Comparator)(CommonOps.castedComparator())); } /** * Returns a new ParallelArray holding elements. * @return a new ParallelArray holding elements */ public ParallelArray all() { return new ParallelArray(ex, (U[])allObjects(null)); } /** * Returns a new ParallelArray with the given element type * holding elements. * @param elementType the type of the elements * @return a new ParallelArray holding elements */ public ParallelArray all(Class elementType) { return new ParallelArray(ex, (U[])allObjects(elementType)); } /** * Returns an operation prefix that causes a method to operate * on mapped elements of the array using the given op * applied to current op's results. * @param op the op * @return operation prefix */ public abstract ParallelArrayWithMapping withMapping (Op op); /** * Returns an operation prefix that causes a method to operate * on mapped elements of the array using the given op * applied to current op's results. * @param op the op * @return operation prefix */ public abstract ParallelArrayWithDoubleMapping withMapping (ObjectToDouble op); /** * Returns an operation prefix that causes a method to operate * on mapped elements of the array using the given op * applied to current op's results. * @param op the op * @return operation prefix */ public abstract ParallelArrayWithLongMapping withMapping (ObjectToLong op); /** * Returns an operation prefix that causes a method to operate * on binary mappings of this array and the other array. * @param combiner the combiner * @param other the other array * @return operation prefix * @throws IllegalArgumentException if other array is a * filtered view (all filters must precede all mappings) */ public ParallelArrayWithMapping withMapping (BinaryOp combiner, ParallelArrayWithMapping other) { if (other.hasFilter()) throw new IllegalArgumentException(); return withIndexedMapping (AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); } /** * Returns an operation prefix that causes a method to operate * on binary mappings of this array and the other array. * @param combiner the combiner * @param other the other array * @return operation prefix * @throws IllegalArgumentException if other array is a * filtered view (all filters must precede all mappings) */ public ParallelArrayWithMapping withMapping (ObjectAndDoubleToObject combiner, ParallelDoubleArrayWithDoubleMapping other) { if (other.hasFilter()) throw new IllegalArgumentException(); return withIndexedMapping (AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); } /** * Returns an operation prefix that causes a method to operate * on binary mappings of this array and the other array. * @param combiner the combiner * @param other the other array * @return operation prefix * @throws IllegalArgumentException if other array is a * filtered view (all filters must precede all mappings) */ public ParallelArrayWithMapping withMapping (ObjectAndLongToObject combiner, ParallelLongArrayWithLongMapping other) { if (other.hasFilter()) throw new IllegalArgumentException(); return withIndexedMapping (AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); } /** * Returns an operation prefix that causes a method to operate * on binary mappings of this array and the other array. * @param combiner the combiner * @param other the other array * @return operation prefix * @throws IllegalArgumentException if other array is a * filtered view (all filters must precede all mappings) */ public ParallelArrayWithDoubleMapping withMapping (ObjectAndObjectToDouble combiner, ParallelArrayWithMapping other) { if (other.hasFilter()) throw new IllegalArgumentException(); return withIndexedMapping (AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); } /** * Returns an operation prefix that causes a method to operate * on binary mappings of this array and the other array. * @param combiner the combiner * @param other the other array * @return operation prefix * @throws IllegalArgumentException if other array is a * filtered view (all filters must precede all mappings) */ public ParallelArrayWithDoubleMapping withMapping (ObjectAndDoubleToDouble combiner, ParallelDoubleArrayWithDoubleMapping other) { if (other.hasFilter()) throw new IllegalArgumentException(); return withIndexedMapping (AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); } /** * Returns an operation prefix that causes a method to operate * on binary mappings of this array and the other array. * @param combiner the combiner * @param other the other array * @return operation prefix * @throws IllegalArgumentException if other array is a * filtered view (all filters must precede all mappings) */ public ParallelArrayWithDoubleMapping withMapping (ObjectAndLongToDouble combiner, ParallelLongArrayWithLongMapping other) { if (other.hasFilter()) throw new IllegalArgumentException(); return withIndexedMapping (AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); } /** * Returns an operation prefix that causes a method to operate * on binary mappings of this array and the other array. * @param combiner the combiner * @param other the other array * @return operation prefix * @throws IllegalArgumentException if other array is a * filtered view (all filters must precede all mappings) */ public ParallelArrayWithLongMapping withMapping (ObjectAndObjectToLong combiner, ParallelArrayWithMapping other) { if (other.hasFilter()) throw new IllegalArgumentException(); return withIndexedMapping (AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); } /** * Returns an operation prefix that causes a method to operate * on binary mappings of this array and the other array. * @param combiner the combiner * @param other the other array * @return operation prefix * @throws IllegalArgumentException if other array is a * filtered view (all filters must precede all mappings) */ public ParallelArrayWithLongMapping withMapping (ObjectAndDoubleToLong combiner, ParallelDoubleArrayWithDoubleMapping other) { if (other.hasFilter()) throw new IllegalArgumentException(); return withIndexedMapping (AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); } /** * Returns an operation prefix that causes a method to operate * on binary mappings of this array and the other array. * @param combiner the combiner * @param other the other array * @return operation prefix * @throws IllegalArgumentException if other array is a * filtered view (all filters must precede all mappings) */ public ParallelArrayWithLongMapping withMapping (ObjectAndLongToLong combiner, ParallelLongArrayWithLongMapping other) { if (other.hasFilter()) throw new IllegalArgumentException(); return withIndexedMapping (AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); } /** * Returns an operation prefix that causes a method to operate * on mappings of this array using the given mapper that * accepts as arguments an element's current index and value * (as mapped by preceding mappings, if any), and produces a * new value. * @param mapper the mapper * @return operation prefix */ public abstract ParallelArrayWithMapping withIndexedMapping (IntAndObjectToObject mapper); /** * Returns an operation prefix that causes a method to operate * on mappings of this array using the given mapper that * accepts as arguments an element's current index and value * (as mapped by preceding mappings, if any), and produces a * new value. * @param mapper the mapper * @return operation prefix */ public abstract ParallelArrayWithDoubleMapping withIndexedMapping (IntAndObjectToDouble mapper); /** * Returns an operation prefix that causes a method to operate * on mappings of this array using the given mapper that * accepts as arguments an element's current index and value * (as mapped by preceding mappings, if any), and produces a * new value. * @param mapper the mapper * @return operation prefix */ public abstract ParallelArrayWithLongMapping withIndexedMapping (IntAndObjectToLong mapper); /** * Returns an Iterable view to sequentially step through mapped * elements also obeying bound and filter constraints, without * performing computations to evaluate them in parallel. * @return the Iterable view */ public Iterable sequentially() { return new Sequentially(); } }