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