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 ParallelArray 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 ParallelDoubleArrayWithDoubleMapping extends AbstractParallelAnyArray.DPap { |
20 |
ParallelDoubleArrayWithDoubleMapping |
21 |
(ForkJoinExecutor ex, int origin, int fence, double[] array) { |
22 |
super(ex, origin, fence, array); |
23 |
} |
24 |
|
25 |
/** |
26 |
* Applies the given procedure to elements |
27 |
* @param procedure the procedure |
28 |
*/ |
29 |
public void apply(DoubleProcedure procedure) { |
30 |
ex.invoke(new PAS.FJDApply(this, origin, fence, null, procedure)); |
31 |
} |
32 |
|
33 |
/** |
34 |
* Returns reduction of elements |
35 |
* @param reducer the reducer |
36 |
* @param base the result for an empty array |
37 |
* @return reduction |
38 |
*/ |
39 |
public double reduce(DoubleReducer reducer, double base) { |
40 |
PAS.FJDReduce f = new PAS.FJDReduce |
41 |
(this, origin, fence, null, reducer, base); |
42 |
ex.invoke(f); |
43 |
return f.result; |
44 |
} |
45 |
|
46 |
/** |
47 |
* Returns the minimum element, or Double.MAX_VALUE if empty |
48 |
* @return minimum element, or Double.MAX_VALUE if empty |
49 |
*/ |
50 |
public double min() { |
51 |
return reduce(CommonOps.naturalDoubleMinReducer(), Double.MAX_VALUE); |
52 |
} |
53 |
|
54 |
/** |
55 |
* Returns the minimum element, or Double.MAX_VALUE if empty |
56 |
* @param comparator the comparator |
57 |
* @return minimum element, or Double.MAX_VALUE if empty |
58 |
*/ |
59 |
public double min(DoubleComparator comparator) { |
60 |
return reduce(CommonOps.doubleMinReducer(comparator), Double.MAX_VALUE); |
61 |
} |
62 |
|
63 |
/** |
64 |
* Returns the maximum element, or -Double.MAX_VALUE if empty |
65 |
* @return maximum element, or -Double.MAX_VALUE if empty |
66 |
*/ |
67 |
public double max() { |
68 |
return reduce(CommonOps.naturalDoubleMaxReducer(), -Double.MAX_VALUE); |
69 |
} |
70 |
|
71 |
/** |
72 |
* Returns the maximum element, or -Double.MAX_VALUE if empty |
73 |
* @param comparator the comparator |
74 |
* @return maximum element, or -Double.MAX_VALUE if empty |
75 |
*/ |
76 |
public double max(DoubleComparator comparator) { |
77 |
return reduce(CommonOps.doubleMaxReducer(comparator), -Double.MAX_VALUE); |
78 |
} |
79 |
|
80 |
/** |
81 |
* Returns the sum of elements |
82 |
* @return the sum of elements |
83 |
*/ |
84 |
public double sum() { |
85 |
return reduce(CommonOps.doubleAdder(), 0); |
86 |
} |
87 |
|
88 |
/** |
89 |
* Returns summary statistics |
90 |
* @param comparator the comparator to use for |
91 |
* locating minimum and maximum elements |
92 |
* @return the summary. |
93 |
*/ |
94 |
public ParallelDoubleArray.SummaryStatistics summary |
95 |
(DoubleComparator comparator) { |
96 |
PAS.FJDStats f = new PAS.FJDStats |
97 |
(this, origin, fence, null, comparator); |
98 |
ex.invoke(f); |
99 |
return f; |
100 |
} |
101 |
|
102 |
/** |
103 |
* Returns summary statistics, using natural comparator |
104 |
* @return the summary. |
105 |
*/ |
106 |
public ParallelDoubleArray.SummaryStatistics summary() { |
107 |
return summary(CommonOps.naturalDoubleComparator()); |
108 |
} |
109 |
|
110 |
/** |
111 |
* Returns a new ParallelDoubleArray holding elements |
112 |
* @return a new ParallelDoubleArray holding elements |
113 |
*/ |
114 |
public ParallelDoubleArray all() { |
115 |
return new ParallelDoubleArray(ex, allDoubles()); |
116 |
} |
117 |
|
118 |
/** |
119 |
* Returns an operation prefix that causes a method to operate |
120 |
* on mapped elements of the array using the given op. |
121 |
* @param op the op |
122 |
* @return operation prefix |
123 |
*/ |
124 |
public abstract ParallelDoubleArrayWithDoubleMapping withMapping |
125 |
(DoubleOp op); |
126 |
|
127 |
/** |
128 |
* Returns an operation prefix that causes a method to operate |
129 |
* on mapped elements of the array using the given op. |
130 |
* @param op the op |
131 |
* @return operation prefix |
132 |
*/ |
133 |
public abstract ParallelDoubleArrayWithLongMapping withMapping |
134 |
(DoubleToLong op); |
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 |
* @param op the op |
140 |
* @return operation prefix |
141 |
*/ |
142 |
public abstract <U> ParallelDoubleArrayWithMapping<U> withMapping |
143 |
(DoubleToObject<? extends U> op); |
144 |
|
145 |
/** |
146 |
* Returns an operation prefix that causes a method to operate |
147 |
* on binary mappings of this array and the other array. |
148 |
* @param combiner the combiner |
149 |
* @param other the other array |
150 |
* @return operation prefix |
151 |
* @throws IllegalArgumentException if other array is a |
152 |
* filtered view (all filters must precede all mappings). |
153 |
*/ |
154 |
public <V,W,X> ParallelDoubleArrayWithMapping<W> withMapping |
155 |
(DoubleAndObjectToObject<? super V, ? extends W> combiner, |
156 |
ParallelArrayWithMapping<X,V> other) { |
157 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
158 |
return withIndexedMapping |
159 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
160 |
} |
161 |
|
162 |
/** |
163 |
* Returns an operation prefix that causes a method to operate |
164 |
* on binary mappings of this array and the other array. |
165 |
* @param combiner the combiner |
166 |
* @param other the other array |
167 |
* @return operation prefix |
168 |
* @throws IllegalArgumentException if other array is a |
169 |
* filtered view (all filters must precede all mappings). |
170 |
*/ |
171 |
public <V> ParallelDoubleArrayWithMapping<V> withMapping |
172 |
(DoubleAndDoubleToObject<? extends V> combiner, |
173 |
ParallelDoubleArrayWithDoubleMapping other) { |
174 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
175 |
return withIndexedMapping |
176 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
177 |
} |
178 |
|
179 |
/** |
180 |
* Returns an operation prefix that causes a method to operate |
181 |
* on binary mappings of this array and the other array. |
182 |
* @param combiner the combiner |
183 |
* @param other the other array |
184 |
* @return operation prefix |
185 |
* @throws IllegalArgumentException if other array is a |
186 |
* filtered view (all filters must precede all mappings). |
187 |
*/ |
188 |
public <V> ParallelDoubleArrayWithMapping<V> withMapping |
189 |
(DoubleAndLongToObject<? extends V> combiner, |
190 |
ParallelLongArrayWithLongMapping other) { |
191 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
192 |
return withIndexedMapping |
193 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
194 |
} |
195 |
|
196 |
/** |
197 |
* Returns an operation prefix that causes a method to operate |
198 |
* on binary mappings of this array and the other array. |
199 |
* @param combiner the combiner |
200 |
* @param other the other array |
201 |
* @return operation prefix |
202 |
* @throws IllegalArgumentException if other array is a |
203 |
* filtered view (all filters must precede all mappings). |
204 |
*/ |
205 |
public <V,W> ParallelDoubleArrayWithDoubleMapping withMapping |
206 |
(DoubleAndObjectToDouble<? super V> combiner, |
207 |
ParallelArrayWithMapping<W,V> other) { |
208 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
209 |
return withIndexedMapping |
210 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
211 |
} |
212 |
|
213 |
/** |
214 |
* Returns an operation prefix that causes a method to operate |
215 |
* on binary mappings of this array and the other array. |
216 |
* @param combiner the combiner |
217 |
* @param other the other array |
218 |
* @return operation prefix |
219 |
* @throws IllegalArgumentException if other array is a |
220 |
* filtered view (all filters must precede all mappings). |
221 |
*/ |
222 |
public ParallelDoubleArrayWithDoubleMapping withMapping |
223 |
(BinaryDoubleOp combiner, |
224 |
ParallelDoubleArrayWithDoubleMapping other) { |
225 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
226 |
return withIndexedMapping |
227 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
228 |
} |
229 |
|
230 |
/** |
231 |
* Returns an operation prefix that causes a method to operate |
232 |
* on binary mappings of this array and the other array. |
233 |
* @param combiner the combiner |
234 |
* @param other the other array |
235 |
* @return operation prefix |
236 |
* @throws IllegalArgumentException if other array is a |
237 |
* filtered view (all filters must precede all mappings). |
238 |
*/ |
239 |
public ParallelDoubleArrayWithDoubleMapping withMapping |
240 |
(DoubleAndLongToDouble combiner, |
241 |
ParallelLongArrayWithLongMapping other) { |
242 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
243 |
return withIndexedMapping |
244 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
245 |
} |
246 |
|
247 |
/** |
248 |
* Returns an operation prefix that causes a method to operate |
249 |
* on binary mappings of this array and the other array. |
250 |
* @param combiner the combiner |
251 |
* @param other the other array |
252 |
* @return operation prefix |
253 |
* @throws IllegalArgumentException if other array is a |
254 |
* filtered view (all filters must precede all mappings). |
255 |
*/ |
256 |
public <V,W> ParallelDoubleArrayWithLongMapping withMapping |
257 |
(DoubleAndObjectToLong<? super V> combiner, |
258 |
ParallelArrayWithMapping<W,V> other) { |
259 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
260 |
return withIndexedMapping |
261 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
262 |
} |
263 |
|
264 |
/** |
265 |
* Returns an operation prefix that causes a method to operate |
266 |
* on binary mappings of this array and the other array. |
267 |
* @param combiner the combiner |
268 |
* @param other the other array |
269 |
* @return operation prefix |
270 |
* @throws IllegalArgumentException if other array is a |
271 |
* filtered view (all filters must precede all mappings). |
272 |
*/ |
273 |
public ParallelDoubleArrayWithLongMapping withMapping |
274 |
(DoubleAndDoubleToLong combiner, |
275 |
ParallelDoubleArrayWithDoubleMapping other) { |
276 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
277 |
return withIndexedMapping |
278 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
279 |
} |
280 |
|
281 |
/** |
282 |
* Returns an operation prefix that causes a method to operate |
283 |
* on binary mappings of this array and the other array. |
284 |
* @param combiner the combiner |
285 |
* @param other the other array |
286 |
* @return operation prefix |
287 |
* @throws IllegalArgumentException if other array is a |
288 |
* filtered view (all filters must precede all mappings). |
289 |
*/ |
290 |
public ParallelDoubleArrayWithLongMapping withMapping |
291 |
(DoubleAndLongToLong combiner, |
292 |
ParallelLongArrayWithLongMapping other) { |
293 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
294 |
return withIndexedMapping |
295 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
296 |
} |
297 |
|
298 |
/** |
299 |
* Returns an operation prefix that causes a method to operate |
300 |
* on mappings of this array using the given mapper that |
301 |
* accepts as arguments an element's current index and value |
302 |
* (as mapped by preceding mappings, if any), and produces a |
303 |
* new value. |
304 |
* @param mapper the mapper |
305 |
* @return operation prefix |
306 |
*/ |
307 |
public abstract <V> ParallelDoubleArrayWithMapping<V> withIndexedMapping |
308 |
(IntAndDoubleToObject<? extends V> mapper); |
309 |
|
310 |
/** |
311 |
* Returns an operation prefix that causes a method to operate |
312 |
* on mappings of this array using the given mapper that |
313 |
* accepts as arguments an element's current index and value |
314 |
* (as mapped by preceding mappings, if any), and produces a |
315 |
* new value. |
316 |
* @param mapper the mapper |
317 |
* @return operation prefix |
318 |
*/ |
319 |
public abstract ParallelDoubleArrayWithDoubleMapping withIndexedMapping |
320 |
(IntAndDoubleToDouble mapper); |
321 |
|
322 |
/** |
323 |
* Returns an operation prefix that causes a method to operate |
324 |
* on mappings of this array using the given mapper that |
325 |
* accepts as arguments an element's current index and value |
326 |
* (as mapped by preceding mappings, if any), and produces a |
327 |
* new value. |
328 |
* @param mapper the mapper |
329 |
* @return operation prefix |
330 |
*/ |
331 |
public abstract ParallelDoubleArrayWithLongMapping withIndexedMapping |
332 |
(IntAndDoubleToLong mapper); |
333 |
|
334 |
/** |
335 |
* Returns an Iterable view to sequentially step through mapped |
336 |
* elements also obeying bound and filter constraints, without |
337 |
* performing computations to evaluate them in parallel |
338 |
* @return the Iterable view |
339 |
*/ |
340 |
public Iterable<Double> sequentially() { |
341 |
return new SequentiallyAsDouble(); |
342 |
} |
343 |
} |
344 |
|