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