ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/extra166y/ParallelArrayWithMapping.java
Revision: 1.4
Committed: Wed Jul 4 20:13:53 2012 UTC (11 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.3: +0 -1 lines
Log Message:
trailing newlines

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.3 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     package extra166y;
8     import jsr166y.*;
9     import static extra166y.Ops.*;
10     import java.util.*;
11     import java.util.concurrent.atomic.*;
12     import java.lang.reflect.Array;
13    
14     /**
15     * A prefix view of ParallelArray that causes operations to apply
16     * to mappings of elements, not to the elements themselves.
17     * Instances of this class may be constructed only via prefix
18     * methods of ParallelArray or its other prefix classes.
19     */
20     public abstract class ParallelArrayWithMapping<T,U> extends AbstractParallelAnyArray.OPap<T> {
21     ParallelArrayWithMapping(ForkJoinPool ex, int origin, int fence,
22     T[] array) {
23     super(ex, origin, fence, array);
24     }
25    
26     /**
27     * Applies the given procedure to elements
28     * @param procedure the procedure
29     */
30     public void apply(Procedure<? super U> procedure) {
31     ex.invoke(new PAS.FJOApply(this, origin, fence, null, procedure));
32     }
33    
34     /**
35     * Returns reduction of elements
36     * @param reducer the reducer
37     * @param base the result for an empty array
38     * @return reduction
39     */
40     public U reduce(Reducer<U> reducer, U base) {
41     PAS.FJOReduce f = new PAS.FJOReduce
42     (this, origin, fence, null, reducer, base);
43     ex.invoke(f);
44     return (U)(f.result);
45     }
46    
47     /**
48     * Returns some element matching bound and filter
49     * constraints, or null if none.
50     * @return an element, or null if none.
51     */
52     public U any() {
53     int i = anyIndex();
54 jsr166 1.2 return (i < 0) ? null : (U)oget(i);
55 dl 1.1 }
56    
57     /**
58     * Returns the minimum element, or null if empty
59     * @param comparator the comparator
60     * @return minimum element, or null if empty
61     */
62     public U min(Comparator<? super U> comparator) {
63     return reduce(CommonOps.<U>minReducer(comparator), null);
64     }
65    
66     /**
67     * Returns the minimum element, or null if empty,
68     * assuming that all elements are Comparables
69     * @return minimum element, or null if empty
70     * @throws ClassCastException if any element is not Comparable.
71     */
72     public U min() {
73     return reduce((Reducer<U>)(CommonOps.castedMinReducer()), null);
74     }
75    
76     /**
77     * Returns the maximum element, or null if empty
78     * @param comparator the comparator
79     * @return maximum element, or null if empty
80     */
81     public U max(Comparator<? super U> comparator) {
82     return reduce(CommonOps.<U>maxReducer(comparator), null);
83     }
84    
85     /**
86     * Returns the maximum element, or null if empty
87     * assuming that all elements are Comparables
88     * @return maximum element, or null if empty
89     * @throws ClassCastException if any element is not Comparable.
90     */
91     public U max() {
92     return reduce((Reducer<U>)(CommonOps.castedMaxReducer()), null);
93     }
94    
95     /**
96     * Returns summary statistics, using the given comparator
97     * to locate minimum and maximum elements.
98     * @param comparator the comparator to use for
99     * locating minimum and maximum elements
100     * @return the summary.
101     */
102     public ParallelArray.SummaryStatistics<U> summary
103     (Comparator<? super U> comparator) {
104     PAS.FJOStats f = new PAS.FJOStats
105     (this, origin, fence, null, comparator);
106     ex.invoke(f);
107     return (ParallelArray.SummaryStatistics<U>)f;
108     }
109    
110     /**
111     * Returns summary statistics, assuming that all elements are
112     * Comparables
113     * @return the summary.
114     */
115     public ParallelArray.SummaryStatistics<U> summary() {
116     return summary((Comparator<? super U>)(CommonOps.castedComparator()));
117     }
118    
119     /**
120     * Returns a new ParallelArray holding elements
121     * @return a new ParallelArray holding elements
122     */
123     public ParallelArray<U> all() {
124     return new ParallelArray<U>(ex, (U[])allObjects(null));
125     }
126    
127     /**
128     * Returns a new ParallelArray with the given element type
129     * holding elements
130     * @param elementType the type of the elements
131     * @return a new ParallelArray holding elements
132     */
133     public ParallelArray<U> all(Class<? super U> elementType) {
134     return new ParallelArray<U>(ex, (U[])allObjects(elementType));
135     }
136    
137     /**
138     * Returns an operation prefix that causes a method to operate
139     * on mapped elements of the array using the given op
140     * applied to current op's results
141     * @param op the op
142     * @return operation prefix
143     */
144     public abstract <V> ParallelArrayWithMapping<T, V> withMapping
145     (Op<? super U, ? extends V> op);
146    
147     /**
148     * Returns an operation prefix that causes a method to operate
149     * on mapped elements of the array using the given op
150     * applied to current op's results
151     * @param op the op
152     * @return operation prefix
153     */
154     public abstract ParallelArrayWithDoubleMapping<T> withMapping
155     (ObjectToDouble<? super U> op);
156    
157     /**
158     * Returns an operation prefix that causes a method to operate
159     * on mapped elements of the array using the given op
160     * applied to current op's results
161     * @param op the op
162     * @return operation prefix
163     */
164     public abstract ParallelArrayWithLongMapping<T> withMapping
165     (ObjectToLong<? super U> op);
166    
167     /**
168     * Returns an operation prefix that causes a method to operate
169     * on binary mappings of this array and the other array.
170     * @param combiner the combiner
171     * @param other the other array
172     * @return operation prefix
173     * @throws IllegalArgumentException if other array is a
174     * filtered view (all filters must precede all mappings).
175     */
176     public <V,W,X> ParallelArrayWithMapping<T,W> withMapping
177     (BinaryOp<? super U, ? super V, ? extends W> combiner,
178     ParallelArrayWithMapping<X,V> other) {
179     if (other.hasFilter()) throw new IllegalArgumentException();
180     return withIndexedMapping
181     (AbstractParallelAnyArray.indexedMapper(combiner, other, origin));
182     }
183    
184     /**
185     * Returns an operation prefix that causes a method to operate
186     * on binary mappings of this array and the other array.
187     * @param combiner the combiner
188     * @param other the other array
189     * @return operation prefix
190     * @throws IllegalArgumentException if other array is a
191     * filtered view (all filters must precede all mappings).
192     */
193     public <V> ParallelArrayWithMapping<T,V> withMapping
194     (ObjectAndDoubleToObject<? super U, ? extends V> combiner,
195     ParallelDoubleArrayWithDoubleMapping other) {
196     if (other.hasFilter()) throw new IllegalArgumentException();
197     return withIndexedMapping
198     (AbstractParallelAnyArray.indexedMapper(combiner, other, origin));
199     }
200    
201     /**
202     * Returns an operation prefix that causes a method to operate
203     * on binary mappings of this array and the other array.
204     * @param combiner the combiner
205     * @param other the other array
206     * @return operation prefix
207     * @throws IllegalArgumentException if other array is a
208     * filtered view (all filters must precede all mappings).
209     */
210     public <V> ParallelArrayWithMapping<T,V> withMapping
211     (ObjectAndLongToObject<? super U, ? extends V> combiner,
212     ParallelLongArrayWithLongMapping other) {
213     if (other.hasFilter()) throw new IllegalArgumentException();
214     return withIndexedMapping
215     (AbstractParallelAnyArray.indexedMapper(combiner, other, origin));
216     }
217    
218     /**
219     * Returns an operation prefix that causes a method to operate
220     * on binary mappings of this array and the other array.
221     * @param combiner the combiner
222     * @param other the other array
223     * @return operation prefix
224     * @throws IllegalArgumentException if other array is a
225     * filtered view (all filters must precede all mappings).
226     */
227     public <V,W> ParallelArrayWithDoubleMapping<T> withMapping
228     (ObjectAndObjectToDouble<? super U, ? super V> combiner,
229     ParallelArrayWithMapping<W,V> other) {
230     if (other.hasFilter()) throw new IllegalArgumentException();
231     return withIndexedMapping
232     (AbstractParallelAnyArray.indexedMapper(combiner, other, origin));
233     }
234    
235     /**
236     * Returns an operation prefix that causes a method to operate
237     * on binary mappings of this array and the other array.
238     * @param combiner the combiner
239     * @param other the other array
240     * @return operation prefix
241     * @throws IllegalArgumentException if other array is a
242     * filtered view (all filters must precede all mappings).
243     */
244     public ParallelArrayWithDoubleMapping<T> withMapping
245     (ObjectAndDoubleToDouble<? super U> combiner,
246     ParallelDoubleArrayWithDoubleMapping other) {
247     if (other.hasFilter()) throw new IllegalArgumentException();
248     return withIndexedMapping
249     (AbstractParallelAnyArray.indexedMapper(combiner, other, origin));
250     }
251    
252     /**
253     * Returns an operation prefix that causes a method to operate
254     * on binary mappings of this array and the other array.
255     * @param combiner the combiner
256     * @param other the other array
257     * @return operation prefix
258     * @throws IllegalArgumentException if other array is a
259     * filtered view (all filters must precede all mappings).
260     */
261     public ParallelArrayWithDoubleMapping<T> withMapping
262     (ObjectAndLongToDouble<? super U> combiner,
263     ParallelLongArrayWithLongMapping other) {
264     if (other.hasFilter()) throw new IllegalArgumentException();
265     return withIndexedMapping
266     (AbstractParallelAnyArray.indexedMapper(combiner, other, origin));
267     }
268    
269     /**
270     * Returns an operation prefix that causes a method to operate
271     * on binary mappings of this array and the other array.
272     * @param combiner the combiner
273     * @param other the other array
274     * @return operation prefix
275     * @throws IllegalArgumentException if other array is a
276     * filtered view (all filters must precede all mappings).
277     */
278     public <V,W> ParallelArrayWithLongMapping<T> withMapping
279     (ObjectAndObjectToLong<? super U, ? super V> combiner,
280     ParallelArrayWithMapping<W,V> other) {
281     if (other.hasFilter()) throw new IllegalArgumentException();
282     return withIndexedMapping
283     (AbstractParallelAnyArray.indexedMapper(combiner, other, origin));
284     }
285    
286     /**
287     * Returns an operation prefix that causes a method to operate
288     * on binary mappings of this array and the other array.
289     * @param combiner the combiner
290     * @param other the other array
291     * @return operation prefix
292     * @throws IllegalArgumentException if other array is a
293     * filtered view (all filters must precede all mappings).
294     */
295     public ParallelArrayWithLongMapping<T> withMapping
296     (ObjectAndDoubleToLong<? super U> combiner,
297     ParallelDoubleArrayWithDoubleMapping other) {
298     if (other.hasFilter()) throw new IllegalArgumentException();
299     return withIndexedMapping
300     (AbstractParallelAnyArray.indexedMapper(combiner, other, origin));
301     }
302    
303     /**
304     * Returns an operation prefix that causes a method to operate
305     * on binary mappings of this array and the other array.
306     * @param combiner the combiner
307     * @param other the other array
308     * @return operation prefix
309     * @throws IllegalArgumentException if other array is a
310     * filtered view (all filters must precede all mappings).
311     */
312     public ParallelArrayWithLongMapping<T> withMapping
313     (ObjectAndLongToLong<? super U> combiner,
314     ParallelLongArrayWithLongMapping other) {
315     if (other.hasFilter()) throw new IllegalArgumentException();
316     return withIndexedMapping
317     (AbstractParallelAnyArray.indexedMapper(combiner, other, origin));
318     }
319    
320     /**
321     * Returns an operation prefix that causes a method to operate
322     * on mappings of this array using the given mapper that
323     * accepts as arguments an element's current index and value
324     * (as mapped by preceding mappings, if any), and produces a
325     * new value.
326     * @param mapper the mapper
327     * @return operation prefix
328     */
329     public abstract <V> ParallelArrayWithMapping<T,V> withIndexedMapping
330     (IntAndObjectToObject<? super U, ? extends V> mapper);
331    
332     /**
333     * Returns an operation prefix that causes a method to operate
334     * on mappings of this array using the given mapper that
335     * accepts as arguments an element's current index and value
336     * (as mapped by preceding mappings, if any), and produces a
337     * new value.
338     * @param mapper the mapper
339     * @return operation prefix
340     */
341     public abstract ParallelArrayWithDoubleMapping<T> withIndexedMapping
342     (IntAndObjectToDouble<? super U> mapper);
343    
344     /**
345     * Returns an operation prefix that causes a method to operate
346     * on mappings of this array using the given mapper that
347     * accepts as arguments an element's current index and value
348     * (as mapped by preceding mappings, if any), and produces a
349     * new value.
350     * @param mapper the mapper
351     * @return operation prefix
352     */
353     public abstract ParallelArrayWithLongMapping<T> withIndexedMapping
354     (IntAndObjectToLong<? super U> mapper);
355    
356     /**
357     * Returns an Iterable view to sequentially step through mapped
358     * elements also obeying bound and filter constraints, without
359     * performing computations to evaluate them in parallel
360     * @return the Iterable view
361     */
362     public Iterable<U> sequentially() {
363     return new Sequentially<U>();
364     }
365    
366     }