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 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 ParallelArrayWithLongMapping<T> extends AbstractParallelAnyArray.OPap<T> { |
21 |
ParallelArrayWithLongMapping |
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(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(), 0L); |
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 ParallelArrayWithDoubleMapping<T> withMapping(LongToDouble 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(LongOp 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 |
(LongToObject<? 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 |
* @throws IllegalArgumentException if other array is a |
151 |
* filtered view (all filters must precede all mappings). |
152 |
*/ |
153 |
public <V,W,X> ParallelArrayWithMapping<T,W> withMapping |
154 |
(LongAndObjectToObject<? super V, ? extends W> combiner, |
155 |
ParallelArrayWithMapping<X,V> other) { |
156 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
157 |
return withIndexedMapping |
158 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
159 |
} |
160 |
|
161 |
/** |
162 |
* Returns an operation prefix that causes a method to operate |
163 |
* on binary mappings of this array and the other array. |
164 |
* @param combiner the combiner |
165 |
* @param other the other array |
166 |
* @return operation prefix |
167 |
* @throws IllegalArgumentException if other array is a |
168 |
* filtered view (all filters must precede all mappings). |
169 |
*/ |
170 |
public <V> ParallelArrayWithMapping<T,V> withMapping |
171 |
(LongAndDoubleToObject<? extends V> combiner, |
172 |
ParallelDoubleArrayWithDoubleMapping other) { |
173 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
174 |
return withIndexedMapping |
175 |
(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> ParallelArrayWithMapping<T,V> withMapping |
188 |
(LongAndLongToObject<? extends V> combiner, |
189 |
ParallelLongArrayWithLongMapping other) { |
190 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
191 |
return withIndexedMapping |
192 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
193 |
} |
194 |
|
195 |
/** |
196 |
* Returns an operation prefix that causes a method to operate |
197 |
* on binary mappings of this array and the other array. |
198 |
* @param combiner the combiner |
199 |
* @param other the other array |
200 |
* @return operation prefix |
201 |
* @throws IllegalArgumentException if other array is a |
202 |
* filtered view (all filters must precede all mappings). |
203 |
*/ |
204 |
public <V,W> ParallelArrayWithDoubleMapping<T> withMapping |
205 |
(LongAndObjectToDouble<? super V> combiner, |
206 |
ParallelArrayWithMapping<W,V> other) { |
207 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
208 |
return withIndexedMapping |
209 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
210 |
} |
211 |
|
212 |
/** |
213 |
* Returns an operation prefix that causes a method to operate |
214 |
* on binary mappings of this array and the other array. |
215 |
* @param combiner the combiner |
216 |
* @param other the other array |
217 |
* @return operation prefix |
218 |
* @throws IllegalArgumentException if other array is a |
219 |
* filtered view (all filters must precede all mappings). |
220 |
*/ |
221 |
public ParallelArrayWithDoubleMapping<T> withMapping |
222 |
(LongAndDoubleToDouble combiner, |
223 |
ParallelDoubleArrayWithDoubleMapping other) { |
224 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
225 |
return withIndexedMapping |
226 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
227 |
} |
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 ParallelArrayWithDoubleMapping<T> withMapping |
240 |
(LongAndLongToDouble 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> ParallelArrayWithLongMapping<T> withMapping |
257 |
(LongAndObjectToLong<? 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 |
/** |
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 ParallelArrayWithLongMapping<T> withMapping |
275 |
(LongAndDoubleToLong combiner, |
276 |
ParallelDoubleArrayWithDoubleMapping other) { |
277 |
if (other.hasFilter()) throw new IllegalArgumentException(); |
278 |
return withIndexedMapping |
279 |
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
280 |
} |
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 ParallelArrayWithLongMapping<T> withMapping |
293 |
(BinaryLongOp 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> ParallelArrayWithMapping<T,V> withIndexedMapping |
310 |
(IntAndLongToObject<? 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 ParallelArrayWithDoubleMapping<T> withIndexedMapping |
322 |
(IntAndLongToDouble 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 ParallelArrayWithLongMapping<T> withIndexedMapping |
334 |
(IntAndLongToLong 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<Long> sequentially() { |
343 |
return new SequentiallyAsLong(); |
344 |
} |
345 |
} |
346 |
|