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