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.7 |
|
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 ParallelArray 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 ParallelArray or its other prefix classes. |
20 |
|
|
*/ |
21 |
|
|
public abstract class ParallelArrayWithMapping<T,U> extends AbstractParallelAnyArray.OPap<T> { |
22 |
|
|
ParallelArrayWithMapping(ForkJoinPool ex, int origin, int fence, |
23 |
|
|
T[] array) { |
24 |
|
|
super(ex, origin, fence, array); |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
/** |
28 |
jsr166 |
1.5 |
* Applies the given procedure to 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 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 some element matching bound and filter |
50 |
|
|
* constraints, or null if none. |
51 |
jsr166 |
1.5 |
* @return an 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 element, or null if empty. |
60 |
dl |
1.1 |
* @param comparator the comparator |
61 |
|
|
* @return minimum 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 element, or null if empty, |
69 |
jsr166 |
1.5 |
* assuming that all elements are Comparables. |
70 |
dl |
1.1 |
* @return minimum 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 element, or null if empty. |
79 |
dl |
1.1 |
* @param comparator the comparator |
80 |
|
|
* @return maximum 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 element, or null if empty, |
88 |
|
|
* assuming that all elements are Comparables. |
89 |
dl |
1.1 |
* @return maximum 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 |
130 |
jsr166 |
1.5 |
* holding 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 |
jsr166 |
1.6 |
public abstract <V> ParallelArrayWithMapping<T,V> withMapping |
146 |
dl |
1.1 |
(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 ParallelArrayWithDoubleMapping<T> 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 ParallelArrayWithLongMapping<T> 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> ParallelArrayWithMapping<T,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> ParallelArrayWithMapping<T,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> ParallelArrayWithMapping<T,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 |
|
|
* Returns an operation prefix that causes a method to operate |
221 |
|
|
* on binary mappings of this array and the other array. |
222 |
|
|
* @param combiner the combiner |
223 |
|
|
* @param other the other array |
224 |
|
|
* @return operation prefix |
225 |
|
|
* @throws IllegalArgumentException if other array is a |
226 |
jsr166 |
1.5 |
* filtered view (all filters must precede all mappings) |
227 |
dl |
1.1 |
*/ |
228 |
|
|
public <V,W> ParallelArrayWithDoubleMapping<T> withMapping |
229 |
|
|
(ObjectAndObjectToDouble<? super U, ? super V> combiner, |
230 |
|
|
ParallelArrayWithMapping<W,V> other) { |
231 |
|
|
if (other.hasFilter()) throw new IllegalArgumentException(); |
232 |
|
|
return withIndexedMapping |
233 |
|
|
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
/** |
237 |
|
|
* Returns an operation prefix that causes a method to operate |
238 |
|
|
* on binary mappings of this array and the other array. |
239 |
|
|
* @param combiner the combiner |
240 |
|
|
* @param other the other array |
241 |
|
|
* @return operation prefix |
242 |
|
|
* @throws IllegalArgumentException if other array is a |
243 |
jsr166 |
1.5 |
* filtered view (all filters must precede all mappings) |
244 |
dl |
1.1 |
*/ |
245 |
|
|
public ParallelArrayWithDoubleMapping<T> withMapping |
246 |
|
|
(ObjectAndDoubleToDouble<? super U> combiner, |
247 |
|
|
ParallelDoubleArrayWithDoubleMapping other) { |
248 |
|
|
if (other.hasFilter()) throw new IllegalArgumentException(); |
249 |
|
|
return withIndexedMapping |
250 |
|
|
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
251 |
|
|
} |
252 |
|
|
|
253 |
|
|
/** |
254 |
|
|
* Returns an operation prefix that causes a method to operate |
255 |
|
|
* on binary mappings of this array and the other array. |
256 |
|
|
* @param combiner the combiner |
257 |
|
|
* @param other the other array |
258 |
|
|
* @return operation prefix |
259 |
|
|
* @throws IllegalArgumentException if other array is a |
260 |
jsr166 |
1.5 |
* filtered view (all filters must precede all mappings) |
261 |
dl |
1.1 |
*/ |
262 |
|
|
public ParallelArrayWithDoubleMapping<T> withMapping |
263 |
|
|
(ObjectAndLongToDouble<? super U> combiner, |
264 |
|
|
ParallelLongArrayWithLongMapping other) { |
265 |
|
|
if (other.hasFilter()) throw new IllegalArgumentException(); |
266 |
|
|
return withIndexedMapping |
267 |
|
|
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
268 |
|
|
} |
269 |
|
|
|
270 |
|
|
/** |
271 |
|
|
* Returns an operation prefix that causes a method to operate |
272 |
|
|
* on binary mappings of this array and the other array. |
273 |
|
|
* @param combiner the combiner |
274 |
|
|
* @param other the other array |
275 |
|
|
* @return operation prefix |
276 |
|
|
* @throws IllegalArgumentException if other array is a |
277 |
jsr166 |
1.5 |
* filtered view (all filters must precede all mappings) |
278 |
dl |
1.1 |
*/ |
279 |
|
|
public <V,W> ParallelArrayWithLongMapping<T> withMapping |
280 |
|
|
(ObjectAndObjectToLong<? super U, ? super V> combiner, |
281 |
|
|
ParallelArrayWithMapping<W,V> other) { |
282 |
|
|
if (other.hasFilter()) throw new IllegalArgumentException(); |
283 |
|
|
return withIndexedMapping |
284 |
|
|
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
/** |
288 |
|
|
* Returns an operation prefix that causes a method to operate |
289 |
|
|
* on binary mappings of this array and the other array. |
290 |
|
|
* @param combiner the combiner |
291 |
|
|
* @param other the other array |
292 |
|
|
* @return operation prefix |
293 |
|
|
* @throws IllegalArgumentException if other array is a |
294 |
jsr166 |
1.5 |
* filtered view (all filters must precede all mappings) |
295 |
dl |
1.1 |
*/ |
296 |
|
|
public ParallelArrayWithLongMapping<T> withMapping |
297 |
|
|
(ObjectAndDoubleToLong<? super U> combiner, |
298 |
|
|
ParallelDoubleArrayWithDoubleMapping other) { |
299 |
|
|
if (other.hasFilter()) throw new IllegalArgumentException(); |
300 |
|
|
return withIndexedMapping |
301 |
|
|
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
302 |
|
|
} |
303 |
|
|
|
304 |
|
|
/** |
305 |
|
|
* Returns an operation prefix that causes a method to operate |
306 |
|
|
* on binary mappings of this array and the other array. |
307 |
|
|
* @param combiner the combiner |
308 |
|
|
* @param other the other array |
309 |
|
|
* @return operation prefix |
310 |
|
|
* @throws IllegalArgumentException if other array is a |
311 |
jsr166 |
1.5 |
* filtered view (all filters must precede all mappings) |
312 |
dl |
1.1 |
*/ |
313 |
|
|
public ParallelArrayWithLongMapping<T> withMapping |
314 |
|
|
(ObjectAndLongToLong<? super U> combiner, |
315 |
|
|
ParallelLongArrayWithLongMapping other) { |
316 |
|
|
if (other.hasFilter()) throw new IllegalArgumentException(); |
317 |
|
|
return withIndexedMapping |
318 |
|
|
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
319 |
|
|
} |
320 |
|
|
|
321 |
|
|
/** |
322 |
|
|
* Returns an operation prefix that causes a method to operate |
323 |
|
|
* on mappings of this array using the given mapper that |
324 |
|
|
* accepts as arguments an element's current index and value |
325 |
|
|
* (as mapped by preceding mappings, if any), and produces a |
326 |
|
|
* new value. |
327 |
|
|
* @param mapper the mapper |
328 |
|
|
* @return operation prefix |
329 |
|
|
*/ |
330 |
|
|
public abstract <V> ParallelArrayWithMapping<T,V> withIndexedMapping |
331 |
|
|
(IntAndObjectToObject<? super U, ? extends V> mapper); |
332 |
|
|
|
333 |
|
|
/** |
334 |
|
|
* Returns an operation prefix that causes a method to operate |
335 |
|
|
* on mappings of this array using the given mapper that |
336 |
|
|
* accepts as arguments an element's current index and value |
337 |
|
|
* (as mapped by preceding mappings, if any), and produces a |
338 |
|
|
* new value. |
339 |
|
|
* @param mapper the mapper |
340 |
|
|
* @return operation prefix |
341 |
|
|
*/ |
342 |
|
|
public abstract ParallelArrayWithDoubleMapping<T> withIndexedMapping |
343 |
|
|
(IntAndObjectToDouble<? super U> mapper); |
344 |
|
|
|
345 |
|
|
/** |
346 |
|
|
* Returns an operation prefix that causes a method to operate |
347 |
|
|
* on mappings of this array using the given mapper that |
348 |
|
|
* accepts as arguments an element's current index and value |
349 |
|
|
* (as mapped by preceding mappings, if any), and produces a |
350 |
|
|
* new value. |
351 |
|
|
* @param mapper the mapper |
352 |
|
|
* @return operation prefix |
353 |
|
|
*/ |
354 |
|
|
public abstract ParallelArrayWithLongMapping<T> withIndexedMapping |
355 |
|
|
(IntAndObjectToLong<? super U> mapper); |
356 |
|
|
|
357 |
|
|
/** |
358 |
|
|
* Returns an Iterable view to sequentially step through mapped |
359 |
|
|
* elements also obeying bound and filter constraints, without |
360 |
jsr166 |
1.5 |
* performing computations to evaluate them in parallel. |
361 |
dl |
1.1 |
* @return the Iterable view |
362 |
|
|
*/ |
363 |
|
|
public Iterable<U> sequentially() { |
364 |
|
|
return new Sequentially<U>(); |
365 |
|
|
} |
366 |
|
|
|
367 |
|
|
} |