ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/extra166y/ParallelDoubleArrayWithMapping.java
Revision: 1.6
Committed: Sun Jan 18 20:17:32 2015 UTC (9 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +1 -0 lines
Log Message:
exactly one blank line before and after package statements

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 jsr166 1.6
9 dl 1.1 import jsr166y.*;
10     import static extra166y.Ops.*;
11     import java.util.*;
12     import java.util.concurrent.atomic.*;
13     import java.lang.reflect.Array;
14    
15     /**
16     * A prefix view of ParallelDoubleArray that causes operations to apply
17     * to mappings of elements, not to the elements themselves.
18     * Instances of this class may be constructed only via prefix
19     * methods of ParallelDoubleArray or its other prefix classes.
20     */
21     public abstract class ParallelDoubleArrayWithMapping<U> extends AbstractParallelAnyArray.DPap {
22     ParallelDoubleArrayWithMapping
23     (ForkJoinPool ex, int origin, int fence, double[] array) {
24     super(ex, origin, fence, array);
25     }
26    
27     /**
28 jsr166 1.5 * Applies the given procedure to mapped elements.
29 dl 1.1 * @param procedure the procedure
30     */
31     public void apply(Procedure<? super U> procedure) {
32     ex.invoke(new PAS.FJOApply(this, origin, fence, null, procedure));
33     }
34    
35     /**
36 jsr166 1.5 * Returns reduction of mapped elements.
37 dl 1.1 * @param reducer the reducer
38     * @param base the result for an empty array
39     * @return reduction
40     */
41     public U reduce(Reducer<U> reducer, U base) {
42     PAS.FJOReduce f = new PAS.FJOReduce
43     (this, origin, fence, null, reducer, base);
44     ex.invoke(f);
45     return (U)(f.result);
46     }
47    
48     /**
49     * Returns mapping of some element matching bound and filter
50     * constraints, or null if none.
51 jsr166 1.5 * @return mapping of matching element, or null if none
52 dl 1.1 */
53     public U any() {
54     int i = anyIndex();
55 jsr166 1.2 return (i < 0) ? null : (U)oget(i);
56 dl 1.1 }
57    
58     /**
59 jsr166 1.5 * Returns the minimum mapped element, or null if empty.
60 dl 1.1 * @param comparator the comparator
61     * @return minimum mapped element, or null if empty
62     */
63     public U min(Comparator<? super U> comparator) {
64     return reduce(CommonOps.<U>minReducer(comparator), null);
65     }
66    
67     /**
68     * Returns the minimum mapped element, or null if empty,
69 jsr166 1.5 * assuming that all elements are Comparables.
70 dl 1.1 * @return minimum mapped element, or null if empty
71 jsr166 1.5 * @throws ClassCastException if any element is not Comparable
72 dl 1.1 */
73     public U min() {
74     return reduce((Reducer<U>)(CommonOps.castedMinReducer()), null);
75     }
76    
77     /**
78 jsr166 1.5 * Returns the maximum mapped element, or null if empty.
79 dl 1.1 * @param comparator the comparator
80     * @return maximum mapped element, or null if empty
81     */
82     public U max(Comparator<? super U> comparator) {
83     return reduce(CommonOps.<U>maxReducer(comparator), null);
84     }
85    
86     /**
87 jsr166 1.5 * Returns the maximum mapped element, or null if empty,
88     * assuming that all elements are Comparables.
89 dl 1.1 * @return maximum mapped element, or null if empty
90 jsr166 1.5 * @throws ClassCastException if any element is not Comparable
91 dl 1.1 */
92     public U max() {
93     return reduce((Reducer<U>)(CommonOps.castedMaxReducer()), null);
94     }
95    
96     /**
97     * Returns summary statistics, using the given comparator
98     * to locate minimum and maximum elements.
99     * @param comparator the comparator to use for
100     * locating minimum and maximum elements
101 jsr166 1.5 * @return the summary
102 dl 1.1 */
103     public ParallelArray.SummaryStatistics<U> summary
104     (Comparator<? super U> comparator) {
105     PAS.FJOStats f = new PAS.FJOStats
106     (this, origin, fence, null, comparator);
107     ex.invoke(f);
108     return (ParallelArray.SummaryStatistics<U>)f;
109     }
110    
111     /**
112     * Returns summary statistics, assuming that all elements are
113 jsr166 1.5 * Comparables.
114     * @return the summary
115 dl 1.1 */
116     public ParallelArray.SummaryStatistics<U> summary() {
117     return summary((Comparator<? super U>)(CommonOps.castedComparator()));
118     }
119    
120     /**
121 jsr166 1.5 * Returns a new ParallelArray holding elements.
122 dl 1.1 * @return a new ParallelArray holding elements
123     */
124     public ParallelArray<U> all() {
125     return new ParallelArray<U>(ex, (U[])allObjects(null));
126     }
127    
128     /**
129     * Returns a new ParallelArray with the given element type holding
130 jsr166 1.5 * elements.
131 dl 1.1 * @param elementType the type of the elements
132     * @return a new ParallelArray holding elements
133     */
134     public ParallelArray<U> all(Class<? super U> elementType) {
135     return new ParallelArray<U>(ex, (U[])allObjects(elementType));
136     }
137    
138     /**
139     * Returns an operation prefix that causes a method to operate
140     * on mapped elements of the array using the given op
141 jsr166 1.5 * applied to current op's results.
142 dl 1.1 * @param op the op
143     * @return operation prefix
144     */
145     public abstract <V> ParallelDoubleArrayWithMapping<V> withMapping
146     (Op<? super U, ? extends V> op);
147    
148     /**
149     * Returns an operation prefix that causes a method to operate
150     * on mapped elements of the array using the given op
151 jsr166 1.5 * applied to current op's results.
152 dl 1.1 * @param op the op
153     * @return operation prefix
154     */
155     public abstract ParallelDoubleArrayWithDoubleMapping withMapping
156     (ObjectToDouble<? super U> op);
157    
158     /**
159     * Returns an operation prefix that causes a method to operate
160     * on mapped elements of the array using the given op
161 jsr166 1.5 * applied to current op's results.
162 dl 1.1 * @param op the op
163     * @return operation prefix
164     */
165     public abstract ParallelDoubleArrayWithLongMapping withMapping
166     (ObjectToLong<? super U> op);
167    
168     /**
169     * Returns an operation prefix that causes a method to operate
170     * on binary mappings of this array and the other array.
171     * @param combiner the combiner
172     * @param other the other array
173     * @return operation prefix
174     * @throws IllegalArgumentException if other array is a
175 jsr166 1.5 * filtered view (all filters must precede all mappings)
176 dl 1.1 */
177     public <V,W,X> ParallelDoubleArrayWithMapping<W> withMapping
178     (BinaryOp<? super U, ? super V, ? extends W> combiner,
179     ParallelArrayWithMapping<X,V> other) {
180     if (other.hasFilter()) throw new IllegalArgumentException();
181     return withIndexedMapping
182     (AbstractParallelAnyArray.indexedMapper(combiner, other, origin));
183     }
184    
185     /**
186     * Returns an operation prefix that causes a method to operate
187     * on binary mappings of this array and the other array.
188     * @param combiner the combiner
189     * @param other the other array
190     * @return operation prefix
191     * @throws IllegalArgumentException if other array is a
192 jsr166 1.5 * filtered view (all filters must precede all mappings)
193 dl 1.1 */
194     public <V> ParallelDoubleArrayWithMapping<V> withMapping
195     (ObjectAndDoubleToObject<? super U, ? extends V> combiner,
196     ParallelDoubleArrayWithDoubleMapping other) {
197     if (other.hasFilter()) throw new IllegalArgumentException();
198     return withIndexedMapping
199     (AbstractParallelAnyArray.indexedMapper(combiner, other, origin));
200     }
201    
202     /**
203     * Returns an operation prefix that causes a method to operate
204     * on binary mappings of this array and the other array.
205     * @param combiner the combiner
206     * @param other the other array
207     * @return operation prefix
208     * @throws IllegalArgumentException if other array is a
209 jsr166 1.5 * filtered view (all filters must precede all mappings)
210 dl 1.1 */
211     public <V> ParallelDoubleArrayWithMapping<V> withMapping
212     (ObjectAndLongToObject<? super U, ? extends V> combiner,
213     ParallelLongArrayWithLongMapping other) {
214     if (other.hasFilter()) throw new IllegalArgumentException();
215     return withIndexedMapping
216     (AbstractParallelAnyArray.indexedMapper(combiner, other, origin));
217     }
218    
219    
220     /**
221     * Returns an operation prefix that causes a method to operate
222     * on binary mappings of this array and the other array.
223     * @param combiner the combiner
224     * @param other the other array
225     * @return operation prefix
226     * @throws IllegalArgumentException if other array is a
227 jsr166 1.5 * filtered view (all filters must precede all mappings)
228 dl 1.1 */
229     public <V,W> ParallelDoubleArrayWithDoubleMapping withMapping
230     (ObjectAndObjectToDouble<? super U, ? super V> combiner,
231     ParallelArrayWithMapping<W,V> other) {
232     if (other.hasFilter()) throw new IllegalArgumentException();
233     return withIndexedMapping
234     (AbstractParallelAnyArray.indexedMapper(combiner, other, origin));
235     }
236    
237     /**
238     * Returns an operation prefix that causes a method to operate
239     * on binary mappings of this array and the other array.
240     * @param combiner the combiner
241     * @param other the other array
242     * @return operation prefix
243     * @throws IllegalArgumentException if other array is a
244 jsr166 1.5 * filtered view (all filters must precede all mappings)
245 dl 1.1 */
246     public ParallelDoubleArrayWithDoubleMapping withMapping
247     (ObjectAndDoubleToDouble<? super U> combiner,
248     ParallelDoubleArrayWithDoubleMapping other) {
249     if (other.hasFilter()) throw new IllegalArgumentException();
250     return withIndexedMapping
251     (AbstractParallelAnyArray.indexedMapper(combiner, other, origin));
252     }
253    
254     /**
255     * Returns an operation prefix that causes a method to operate
256     * on binary mappings of this array and the other array.
257     * @param combiner the combiner
258     * @param other the other array
259     * @return operation prefix
260     */
261     public ParallelDoubleArrayWithDoubleMapping 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 jsr166 1.5 * filtered view (all filters must precede all mappings)
277 dl 1.1 */
278     public <V,W> ParallelDoubleArrayWithLongMapping 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 jsr166 1.5 * filtered view (all filters must precede all mappings)
294 dl 1.1 */
295     public ParallelDoubleArrayWithLongMapping 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 jsr166 1.5 * filtered view (all filters must precede all mappings)
311 dl 1.1 */
312     public ParallelDoubleArrayWithLongMapping 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> ParallelDoubleArrayWithMapping<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 ParallelDoubleArrayWithDoubleMapping 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 ParallelDoubleArrayWithLongMapping 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 jsr166 1.5 * performing computations to evaluate them in parallel.
360 dl 1.1 * @return the Iterable view
361     */
362     public Iterable<U> sequentially() {
363     return new Sequentially<U>();
364     }
365    
366     }