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 |
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 ParallelArray 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 ParallelArray or its other prefix classes. |
19 |
*/ |
20 |
public abstract class ParallelArrayWithDoubleMapping<T> extends AbstractParallelAnyArray.OPap<T> { |
21 |
ParallelArrayWithDoubleMapping |
22 |
(ForkJoinPool ex, int origin, int fence, T[] 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.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 ParallelArrayWithDoubleMapping<T> withMapping(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 ParallelArrayWithLongMapping<T> withMapping(DoubleToLong op); |
134 |
|
135 |
/** |
136 |
* Returns an operation prefix that causes a method to operate |
137 |
* on mapped elements of the array using the given op. |
138 |
* @param op the op |
139 |
* @return operation prefix |
140 |
*/ |
141 |
public abstract <U> ParallelArrayWithMapping<T, U> withMapping |
142 |
(DoubleToObject<? extends U> op); |
143 |
|
144 |
/** |
145 |
* Returns an operation prefix that causes a method to operate |
146 |
* on binary mappings of this array and the other array. |
147 |
* @param combiner the combiner |
148 |
* @param other the other array |
149 |
* @return operation prefix |
150 |
*/ |
151 |
public <V,W,X> ParallelArrayWithMapping<T,W> withMapping |
152 |
(DoubleAndObjectToObject<? super V, ? extends W> combiner, |
153 |
ParallelArrayWithMapping<X,V> other) { |
154 |
return withIndexedMapping |
155 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
156 |
} |
157 |
|
158 |
/** |
159 |
* Returns an operation prefix that causes a method to operate |
160 |
* on binary mappings of this array and the other array. |
161 |
* @param combiner the combiner |
162 |
* @param other the other array |
163 |
* @return operation prefix |
164 |
*/ |
165 |
public <V> ParallelArrayWithMapping<T,V> withMapping |
166 |
(DoubleAndDoubleToObject<? extends V> combiner, |
167 |
ParallelDoubleArrayWithDoubleMapping other) { |
168 |
return withIndexedMapping |
169 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
170 |
} |
171 |
|
172 |
/** |
173 |
* Returns an operation prefix that causes a method to operate |
174 |
* on binary mappings of this array and the other array. |
175 |
* @param combiner the combiner |
176 |
* @param other the other array |
177 |
* @return operation prefix |
178 |
*/ |
179 |
public <V> ParallelArrayWithMapping<T,V> withMapping |
180 |
(DoubleAndLongToObject<? extends V> combiner, |
181 |
ParallelLongArrayWithLongMapping other) { |
182 |
return withIndexedMapping |
183 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
184 |
} |
185 |
|
186 |
/** |
187 |
* Returns an operation prefix that causes a method to operate |
188 |
* on binary mappings of this array and the other array. |
189 |
* @param combiner the combiner |
190 |
* @param other the other array |
191 |
* @return operation prefix |
192 |
*/ |
193 |
public <V,W> ParallelArrayWithDoubleMapping<T> withMapping |
194 |
(DoubleAndObjectToDouble<? super V> combiner, |
195 |
ParallelArrayWithMapping<W,V> other) { |
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 |
*/ |
207 |
public ParallelArrayWithDoubleMapping<T> withMapping |
208 |
(BinaryDoubleOp combiner, |
209 |
ParallelDoubleArrayWithDoubleMapping other) { |
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 |
*/ |
221 |
public ParallelArrayWithDoubleMapping<T> withMapping |
222 |
(DoubleAndLongToDouble combiner, |
223 |
ParallelLongArrayWithLongMapping other) { |
224 |
return withIndexedMapping |
225 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
226 |
} |
227 |
|
228 |
/** |
229 |
* Returns an operation prefix that causes a method to operate |
230 |
* on binary mappings of this array and the other array. |
231 |
* @param combiner the combiner |
232 |
* @param other the other array |
233 |
* @return operation prefix |
234 |
*/ |
235 |
public <V,W> ParallelArrayWithLongMapping<T> withMapping |
236 |
(DoubleAndObjectToLong<? super V> combiner, |
237 |
ParallelArrayWithMapping<W,V> other) { |
238 |
return withIndexedMapping |
239 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
240 |
} |
241 |
|
242 |
/** |
243 |
* Returns an operation prefix that causes a method to operate |
244 |
* on binary mappings of this array and the other array. |
245 |
* @param combiner the combiner |
246 |
* @param other the other array |
247 |
* @return operation prefix |
248 |
*/ |
249 |
public ParallelArrayWithLongMapping<T> withMapping |
250 |
(DoubleAndDoubleToLong combiner, |
251 |
ParallelDoubleArrayWithDoubleMapping other) { |
252 |
return withIndexedMapping |
253 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
254 |
} |
255 |
|
256 |
/** |
257 |
* Returns an operation prefix that causes a method to operate |
258 |
* on binary mappings of this array and the other array. |
259 |
* @param combiner the combiner |
260 |
* @param other the other array |
261 |
* @return operation prefix |
262 |
*/ |
263 |
public ParallelArrayWithLongMapping<T> withMapping |
264 |
(DoubleAndLongToLong combiner, |
265 |
ParallelLongArrayWithLongMapping other) { |
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 mappings of this array using the given mapper that |
273 |
* accepts as arguments an element's current index and value |
274 |
* (as mapped by preceding mappings, if any), and produces a |
275 |
* new value. |
276 |
* @param mapper the mapper |
277 |
* @return operation prefix |
278 |
*/ |
279 |
public abstract <V> ParallelArrayWithMapping<T,V> withIndexedMapping |
280 |
(IntAndDoubleToObject<? extends V> mapper); |
281 |
|
282 |
/** |
283 |
* Returns an operation prefix that causes a method to operate |
284 |
* on mappings of this array using the given mapper that |
285 |
* accepts as arguments an element's current index and value |
286 |
* (as mapped by preceding mappings, if any), and produces a |
287 |
* new value. |
288 |
* @param mapper the mapper |
289 |
* @return operation prefix |
290 |
*/ |
291 |
public abstract ParallelArrayWithDoubleMapping<T> withIndexedMapping |
292 |
(IntAndDoubleToDouble mapper); |
293 |
|
294 |
/** |
295 |
* Returns an operation prefix that causes a method to operate |
296 |
* on mappings of this array using the given mapper that |
297 |
* accepts as arguments an element's current index and value |
298 |
* (as mapped by preceding mappings, if any), and produces a |
299 |
* new value. |
300 |
* @param mapper the mapper |
301 |
* @return operation prefix |
302 |
*/ |
303 |
public abstract ParallelArrayWithLongMapping<T> withIndexedMapping |
304 |
(IntAndDoubleToLong mapper); |
305 |
|
306 |
/** |
307 |
* Returns an Iterable view to sequentially step through mapped |
308 |
* elements also obeying bound and filter constraints, without |
309 |
* performing computations to evaluate them in parallel |
310 |
* @return the Iterable view |
311 |
*/ |
312 |
public Iterable<Double> sequentially() { |
313 |
return new SequentiallyAsDouble(); |
314 |
} |
315 |
} |
316 |
|