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 |
|
14 |
/** |
15 |
* A prefix view of ParallelDoubleArray 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 ParallelDoubleArray or its other prefix classes. |
19 |
*/ |
20 |
public abstract class ParallelDoubleArrayWithLongMapping extends AbstractParallelAnyArray.DPap { |
21 |
ParallelDoubleArrayWithLongMapping |
22 |
(ForkJoinPool ex, int origin, int fence, double[] 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(LongProcedure procedure) { |
31 |
ex.invoke(new PAS.FJLApply(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 long reduce(LongReducer reducer, long base) { |
41 |
PAS.FJLReduce f = new PAS.FJLReduce |
42 |
(this, origin, fence, null, reducer, base); |
43 |
ex.invoke(f); |
44 |
return f.result; |
45 |
} |
46 |
|
47 |
/** |
48 |
* Returns the minimum element, or Long.MAX_VALUE if empty. |
49 |
* @return minimum element, or Long.MAX_VALUE if empty |
50 |
*/ |
51 |
public long min() { |
52 |
return reduce(CommonOps.naturalLongMinReducer(), Long.MAX_VALUE); |
53 |
} |
54 |
|
55 |
/** |
56 |
* Returns the minimum element, or Long.MAX_VALUE if empty. |
57 |
* @param comparator the comparator |
58 |
* @return minimum element, or Long.MAX_VALUE if empty |
59 |
*/ |
60 |
public long min(LongComparator comparator) { |
61 |
return reduce(CommonOps.longMinReducer(comparator), Long.MAX_VALUE); |
62 |
} |
63 |
|
64 |
/** |
65 |
* Returns the maximum element, or Long.MIN_VALUE if empty. |
66 |
* @return maximum element, or Long.MIN_VALUE if empty |
67 |
*/ |
68 |
public long max() { |
69 |
return reduce(CommonOps.naturalLongMaxReducer(), Long.MIN_VALUE); |
70 |
} |
71 |
|
72 |
/** |
73 |
* Returns the maximum element, or Long.MIN_VALUE if empty. |
74 |
* @param comparator the comparator |
75 |
* @return maximum element, or Long.MIN_VALUE if empty |
76 |
*/ |
77 |
public long max(LongComparator comparator) { |
78 |
return reduce(CommonOps.longMaxReducer(comparator), Long.MIN_VALUE); |
79 |
} |
80 |
|
81 |
/** |
82 |
* Returns the sum of elements. |
83 |
* @return the sum of elements |
84 |
*/ |
85 |
public long sum() { |
86 |
return reduce(CommonOps.longAdder(), 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 ParallelLongArray.SummaryStatistics summary |
96 |
(LongComparator comparator) { |
97 |
PAS.FJLStats f = new PAS.FJLStats |
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 ParallelLongArray.SummaryStatistics summary() { |
108 |
return summary(CommonOps.naturalLongComparator()); |
109 |
} |
110 |
|
111 |
/** |
112 |
* Returns a new ParallelLongArray holding mappings. |
113 |
* @return a new ParallelLongArray holding mappings |
114 |
*/ |
115 |
public ParallelLongArray all() { |
116 |
return new ParallelLongArray(ex, allLongs()); |
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 ParallelDoubleArrayWithDoubleMapping withMapping |
126 |
(LongToDouble 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 ParallelDoubleArrayWithLongMapping withMapping |
135 |
(LongOp 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> ParallelDoubleArrayWithMapping<U> withMapping |
144 |
(LongToObject<? 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> ParallelDoubleArrayWithMapping<W> withMapping |
156 |
(LongAndObjectToObject<? super V, ? extends W> combiner, |
157 |
ParallelArrayWithMapping<X,V> other) { |
158 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
159 |
return withIndexedMapping(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 |
(LongAndDoubleToObject<? extends V> combiner, |
173 |
ParallelDoubleArrayWithDoubleMapping other) { |
174 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
175 |
return withIndexedMapping(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
176 |
} |
177 |
|
178 |
/** |
179 |
* Returns an operation prefix that causes a method to operate |
180 |
* on binary mappings of this array and the other array. |
181 |
* @param combiner the combiner |
182 |
* @param other the other array |
183 |
* @return operation prefix |
184 |
* @throws IllegalArgumentException if other array is a |
185 |
* filtered view (all filters must precede all mappings) |
186 |
*/ |
187 |
public <V> ParallelDoubleArrayWithMapping<V> withMapping |
188 |
(LongAndLongToObject<? extends V> combiner, |
189 |
ParallelLongArrayWithLongMapping other) { |
190 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
191 |
return withIndexedMapping(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
192 |
} |
193 |
|
194 |
/** |
195 |
* Returns an operation prefix that causes a method to operate |
196 |
* on binary mappings of this array and the other array. |
197 |
* @param combiner the combiner |
198 |
* @param other the other array |
199 |
* @return operation prefix |
200 |
* @throws IllegalArgumentException if other array is a |
201 |
* filtered view (all filters must precede all mappings) |
202 |
*/ |
203 |
public <V,W> ParallelDoubleArrayWithDoubleMapping withMapping |
204 |
(LongAndObjectToDouble<? super V> combiner, |
205 |
ParallelArrayWithMapping<W,V> other) { |
206 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
207 |
return withIndexedMapping(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
208 |
} |
209 |
|
210 |
/** |
211 |
* Returns an operation prefix that causes a method to operate |
212 |
* on binary mappings of this array and the other array. |
213 |
* @param combiner the combiner |
214 |
* @param other the other array |
215 |
* @return operation prefix |
216 |
* @throws IllegalArgumentException if other array is a |
217 |
* filtered view (all filters must precede all mappings) |
218 |
*/ |
219 |
public ParallelDoubleArrayWithDoubleMapping withMapping |
220 |
(LongAndDoubleToDouble combiner, |
221 |
ParallelDoubleArrayWithDoubleMapping other) { |
222 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
223 |
return withIndexedMapping(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
224 |
} |
225 |
|
226 |
/** |
227 |
* Returns an operation prefix that causes a method to operate |
228 |
* on binary mappings of this array and the other array. |
229 |
* @param combiner the combiner |
230 |
* @param other the other array |
231 |
* @return operation prefix |
232 |
* @throws IllegalArgumentException if other array is a |
233 |
* filtered view (all filters must precede all mappings) |
234 |
*/ |
235 |
public ParallelDoubleArrayWithDoubleMapping withMapping |
236 |
(LongAndLongToDouble combiner, |
237 |
ParallelLongArrayWithLongMapping other) { |
238 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
239 |
return withIndexedMapping(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 |
* @throws IllegalArgumentException if other array is a |
249 |
* filtered view (all filters must precede all mappings) |
250 |
*/ |
251 |
public <V,W> ParallelDoubleArrayWithLongMapping withMapping |
252 |
(LongAndObjectToLong<? super V> combiner, |
253 |
ParallelArrayWithMapping<W,V> other) { |
254 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
255 |
return withIndexedMapping(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
256 |
} |
257 |
|
258 |
/** |
259 |
* Returns an operation prefix that causes a method to operate |
260 |
* on binary mappings of this array and the other array. |
261 |
* @param combiner the combiner |
262 |
* @param other the other array |
263 |
* @return operation prefix |
264 |
* @throws IllegalArgumentException if other array is a |
265 |
* filtered view (all filters must precede all mappings) |
266 |
*/ |
267 |
public ParallelDoubleArrayWithLongMapping withMapping |
268 |
(LongAndDoubleToLong combiner, |
269 |
ParallelDoubleArrayWithDoubleMapping other) { |
270 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
271 |
return withIndexedMapping(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
272 |
} |
273 |
|
274 |
/** |
275 |
* Returns an operation prefix that causes a method to operate |
276 |
* on binary mappings of this array and the other array. |
277 |
* @param combiner the combiner |
278 |
* @param other the other array |
279 |
* @return operation prefix |
280 |
* @throws IllegalArgumentException if other array is a |
281 |
* filtered view (all filters must precede all mappings) |
282 |
*/ |
283 |
public ParallelDoubleArrayWithLongMapping withMapping |
284 |
(BinaryLongOp combiner, |
285 |
ParallelLongArrayWithLongMapping other) { |
286 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
287 |
return withIndexedMapping(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
288 |
} |
289 |
|
290 |
/** |
291 |
* Returns an operation prefix that causes a method to operate |
292 |
* on mappings of this array using the given mapper that |
293 |
* accepts as arguments an element's current index and value |
294 |
* (as mapped by preceding mappings, if any), and produces a |
295 |
* new value. |
296 |
* @param mapper the mapper |
297 |
* @return operation prefix |
298 |
*/ |
299 |
public abstract <V> ParallelDoubleArrayWithMapping<V> withIndexedMapping |
300 |
(IntAndLongToObject<? extends V> mapper); |
301 |
|
302 |
/** |
303 |
* Returns an operation prefix that causes a method to operate |
304 |
* on mappings of this array using the given mapper that |
305 |
* accepts as arguments an element's current index and value |
306 |
* (as mapped by preceding mappings, if any), and produces a |
307 |
* new value. |
308 |
* @param mapper the mapper |
309 |
* @return operation prefix |
310 |
*/ |
311 |
public abstract ParallelDoubleArrayWithDoubleMapping withIndexedMapping |
312 |
(IntAndLongToDouble mapper); |
313 |
|
314 |
/** |
315 |
* Returns an operation prefix that causes a method to operate |
316 |
* on mappings of this array using the given mapper that |
317 |
* accepts as arguments an element's current index and value |
318 |
* (as mapped by preceding mappings, if any), and produces a |
319 |
* new value. |
320 |
* @param mapper the mapper |
321 |
* @return operation prefix |
322 |
*/ |
323 |
public abstract ParallelDoubleArrayWithLongMapping withIndexedMapping |
324 |
(IntAndLongToLong mapper); |
325 |
|
326 |
/** |
327 |
* Returns an Iterable view to sequentially step through mapped |
328 |
* elements also obeying bound and filter constraints, without |
329 |
* performing computations to evaluate them in parallel. |
330 |
* @return the Iterable view |
331 |
*/ |
332 |
public Iterable<Long> sequentially() { |
333 |
return new SequentiallyAsLong(); |
334 |
} |
335 |
} |