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 |
jsr166 |
1.6 |
|
9 |
dl |
1.1 |
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 ParallelArray 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 ParallelArray or its other prefix classes. |
20 |
|
|
*/ |
21 |
|
|
public abstract class ParallelArrayWithLongMapping<T> extends AbstractParallelAnyArray.OPap<T> { |
22 |
|
|
ParallelArrayWithLongMapping |
23 |
|
|
(ForkJoinPool ex, int origin, int fence, T[] array) { |
24 |
|
|
super(ex, origin, fence, array); |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
/** |
28 |
jsr166 |
1.4 |
* Applies the given procedure. |
29 |
dl |
1.1 |
* @param procedure the procedure |
30 |
|
|
*/ |
31 |
|
|
public void apply(LongProcedure procedure) { |
32 |
|
|
ex.invoke(new PAS.FJLApply(this, origin, fence, null, procedure)); |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
/** |
36 |
jsr166 |
1.4 |
* Returns reduction of mapped elements. |
37 |
dl |
1.1 |
* @param reducer the reducer |
38 |
|
|
* @param base the result for an empty array |
39 |
|
|
* @return reduction |
40 |
|
|
*/ |
41 |
|
|
public long reduce(LongReducer reducer, long base) { |
42 |
|
|
PAS.FJLReduce f = new PAS.FJLReduce |
43 |
|
|
(this, origin, fence, null, reducer, base); |
44 |
|
|
ex.invoke(f); |
45 |
|
|
return f.result; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
/** |
49 |
jsr166 |
1.4 |
* Returns the minimum element, or Long.MAX_VALUE if empty. |
50 |
dl |
1.1 |
* @return minimum element, or Long.MAX_VALUE if empty |
51 |
|
|
*/ |
52 |
|
|
public long min() { |
53 |
|
|
return reduce(CommonOps.naturalLongMinReducer(), Long.MAX_VALUE); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
/** |
57 |
jsr166 |
1.4 |
* Returns the minimum element, or Long.MAX_VALUE if empty. |
58 |
dl |
1.1 |
* @param comparator the comparator |
59 |
|
|
* @return minimum element, or Long.MAX_VALUE if empty |
60 |
|
|
*/ |
61 |
|
|
public long min(LongComparator comparator) { |
62 |
|
|
return reduce(CommonOps.longMinReducer(comparator), Long.MAX_VALUE); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
/** |
66 |
jsr166 |
1.4 |
* Returns the maximum element, or Long.MIN_VALUE if empty. |
67 |
dl |
1.1 |
* @return maximum element, or Long.MIN_VALUE if empty |
68 |
|
|
*/ |
69 |
|
|
public long max() { |
70 |
|
|
return reduce(CommonOps.naturalLongMaxReducer(), Long.MIN_VALUE); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
/** |
74 |
jsr166 |
1.4 |
* Returns the maximum element, or Long.MIN_VALUE if empty. |
75 |
dl |
1.1 |
* @param comparator the comparator |
76 |
|
|
* @return maximum element, or Long.MIN_VALUE if empty |
77 |
|
|
*/ |
78 |
|
|
public long max(LongComparator comparator) { |
79 |
|
|
return reduce(CommonOps.longMaxReducer(comparator), Long.MIN_VALUE); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
/** |
83 |
jsr166 |
1.4 |
* Returns the sum of elements. |
84 |
dl |
1.1 |
* @return the sum of elements |
85 |
|
|
*/ |
86 |
|
|
public long sum() { |
87 |
|
|
return reduce(CommonOps.longAdder(), 0L); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
/** |
91 |
jsr166 |
1.4 |
* Returns summary statistics. |
92 |
dl |
1.1 |
* @param comparator the comparator to use for |
93 |
|
|
* locating minimum and maximum elements |
94 |
jsr166 |
1.4 |
* @return the summary |
95 |
dl |
1.1 |
*/ |
96 |
|
|
public ParallelLongArray.SummaryStatistics summary |
97 |
|
|
(LongComparator comparator) { |
98 |
|
|
PAS.FJLStats f = new PAS.FJLStats |
99 |
|
|
(this, origin, fence, null, comparator); |
100 |
|
|
ex.invoke(f); |
101 |
|
|
return f; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
/** |
105 |
jsr166 |
1.4 |
* Returns summary statistics, using natural comparator. |
106 |
|
|
* @return the summary |
107 |
dl |
1.1 |
*/ |
108 |
|
|
public ParallelLongArray.SummaryStatistics summary() { |
109 |
|
|
return summary(CommonOps.naturalLongComparator()); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
/** |
113 |
jsr166 |
1.4 |
* Returns a new ParallelLongArray holding mappings. |
114 |
dl |
1.1 |
* @return a new ParallelLongArray holding mappings |
115 |
|
|
*/ |
116 |
|
|
public ParallelLongArray all() { |
117 |
|
|
return new ParallelLongArray(ex, allLongs()); |
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 ParallelArrayWithDoubleMapping<T> withMapping(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 ParallelArrayWithLongMapping<T> withMapping(LongOp op); |
135 |
|
|
|
136 |
|
|
/** |
137 |
|
|
* Returns an operation prefix that causes a method to operate |
138 |
|
|
* on mapped elements of the array using the given op. |
139 |
|
|
* @param op the op |
140 |
|
|
* @return operation prefix |
141 |
|
|
*/ |
142 |
jsr166 |
1.5 |
public abstract <U> ParallelArrayWithMapping<T,U> withMapping |
143 |
dl |
1.1 |
(LongToObject<? extends U> op); |
144 |
|
|
|
145 |
|
|
/** |
146 |
|
|
* Returns an operation prefix that causes a method to operate |
147 |
|
|
* on binary mappings of this array and the other array. |
148 |
|
|
* @param combiner the combiner |
149 |
|
|
* @param other the other array |
150 |
|
|
* @return operation prefix |
151 |
|
|
* @throws IllegalArgumentException if other array is a |
152 |
jsr166 |
1.4 |
* filtered view (all filters must precede all mappings) |
153 |
dl |
1.1 |
*/ |
154 |
|
|
public <V,W,X> ParallelArrayWithMapping<T,W> withMapping |
155 |
|
|
(LongAndObjectToObject<? super V, ? extends W> combiner, |
156 |
|
|
ParallelArrayWithMapping<X,V> other) { |
157 |
|
|
if (other.hasFilter()) throw new IllegalArgumentException(); |
158 |
|
|
return withIndexedMapping |
159 |
|
|
(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 |
jsr166 |
1.4 |
* filtered view (all filters must precede all mappings) |
170 |
dl |
1.1 |
*/ |
171 |
|
|
public <V> ParallelArrayWithMapping<T,V> withMapping |
172 |
|
|
(LongAndDoubleToObject<? extends V> combiner, |
173 |
|
|
ParallelDoubleArrayWithDoubleMapping other) { |
174 |
|
|
if (other.hasFilter()) throw new IllegalArgumentException(); |
175 |
|
|
return withIndexedMapping |
176 |
|
|
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
/** |
180 |
|
|
* Returns an operation prefix that causes a method to operate |
181 |
|
|
* on binary mappings of this array and the other array. |
182 |
|
|
* @param combiner the combiner |
183 |
|
|
* @param other the other array |
184 |
|
|
* @return operation prefix |
185 |
|
|
* @throws IllegalArgumentException if other array is a |
186 |
jsr166 |
1.4 |
* filtered view (all filters must precede all mappings) |
187 |
dl |
1.1 |
*/ |
188 |
|
|
public <V> ParallelArrayWithMapping<T,V> withMapping |
189 |
|
|
(LongAndLongToObject<? extends V> combiner, |
190 |
|
|
ParallelLongArrayWithLongMapping other) { |
191 |
|
|
if (other.hasFilter()) throw new IllegalArgumentException(); |
192 |
|
|
return withIndexedMapping |
193 |
|
|
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
/** |
197 |
|
|
* Returns an operation prefix that causes a method to operate |
198 |
|
|
* on binary mappings of this array and the other array. |
199 |
|
|
* @param combiner the combiner |
200 |
|
|
* @param other the other array |
201 |
|
|
* @return operation prefix |
202 |
|
|
* @throws IllegalArgumentException if other array is a |
203 |
jsr166 |
1.4 |
* filtered view (all filters must precede all mappings) |
204 |
dl |
1.1 |
*/ |
205 |
|
|
public <V,W> ParallelArrayWithDoubleMapping<T> withMapping |
206 |
|
|
(LongAndObjectToDouble<? super V> combiner, |
207 |
|
|
ParallelArrayWithMapping<W,V> other) { |
208 |
|
|
if (other.hasFilter()) throw new IllegalArgumentException(); |
209 |
|
|
return withIndexedMapping |
210 |
|
|
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
/** |
214 |
|
|
* Returns an operation prefix that causes a method to operate |
215 |
|
|
* on binary mappings of this array and the other array. |
216 |
|
|
* @param combiner the combiner |
217 |
|
|
* @param other the other array |
218 |
|
|
* @return operation prefix |
219 |
|
|
* @throws IllegalArgumentException if other array is a |
220 |
jsr166 |
1.4 |
* filtered view (all filters must precede all mappings) |
221 |
dl |
1.1 |
*/ |
222 |
|
|
public ParallelArrayWithDoubleMapping<T> withMapping |
223 |
|
|
(LongAndDoubleToDouble combiner, |
224 |
|
|
ParallelDoubleArrayWithDoubleMapping other) { |
225 |
|
|
if (other.hasFilter()) throw new IllegalArgumentException(); |
226 |
|
|
return withIndexedMapping |
227 |
|
|
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
228 |
|
|
} |
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 |
jsr166 |
1.4 |
* filtered view (all filters must precede all mappings) |
239 |
dl |
1.1 |
*/ |
240 |
|
|
public ParallelArrayWithDoubleMapping<T> withMapping |
241 |
|
|
(LongAndLongToDouble 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 |
jsr166 |
1.4 |
* filtered view (all filters must precede all mappings) |
256 |
dl |
1.1 |
*/ |
257 |
|
|
public <V,W> ParallelArrayWithLongMapping<T> withMapping |
258 |
|
|
(LongAndObjectToLong<? 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 |
|
|
/** |
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 |
jsr166 |
1.4 |
* filtered view (all filters must precede all mappings) |
274 |
dl |
1.1 |
*/ |
275 |
|
|
public ParallelArrayWithLongMapping<T> withMapping |
276 |
|
|
(LongAndDoubleToLong combiner, |
277 |
|
|
ParallelDoubleArrayWithDoubleMapping other) { |
278 |
|
|
if (other.hasFilter()) throw new IllegalArgumentException(); |
279 |
|
|
return withIndexedMapping |
280 |
|
|
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
281 |
|
|
} |
282 |
|
|
|
283 |
|
|
|
284 |
|
|
/** |
285 |
|
|
* Returns an operation prefix that causes a method to operate |
286 |
|
|
* on binary mappings of this array and the other array. |
287 |
|
|
* @param combiner the combiner |
288 |
|
|
* @param other the other array |
289 |
|
|
* @return operation prefix |
290 |
|
|
* @throws IllegalArgumentException if other array is a |
291 |
jsr166 |
1.4 |
* filtered view (all filters must precede all mappings) |
292 |
dl |
1.1 |
*/ |
293 |
|
|
public ParallelArrayWithLongMapping<T> withMapping |
294 |
|
|
(BinaryLongOp combiner, |
295 |
|
|
ParallelLongArrayWithLongMapping other) { |
296 |
|
|
if (other.hasFilter()) throw new IllegalArgumentException(); |
297 |
|
|
return withIndexedMapping |
298 |
|
|
(AbstractParallelAnyArray.indexedMapper(combiner, other, origin)); |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
/** |
302 |
|
|
* Returns an operation prefix that causes a method to operate |
303 |
|
|
* on mappings of this array using the given mapper that |
304 |
|
|
* accepts as arguments an element's current index and value |
305 |
|
|
* (as mapped by preceding mappings, if any), and produces a |
306 |
|
|
* new value. |
307 |
|
|
* @param mapper the mapper |
308 |
|
|
* @return operation prefix |
309 |
|
|
*/ |
310 |
|
|
public abstract <V> ParallelArrayWithMapping<T,V> withIndexedMapping |
311 |
|
|
(IntAndLongToObject<? extends V> mapper); |
312 |
|
|
|
313 |
|
|
/** |
314 |
|
|
* Returns an operation prefix that causes a method to operate |
315 |
|
|
* on mappings of this array using the given mapper that |
316 |
|
|
* accepts as arguments an element's current index and value |
317 |
|
|
* (as mapped by preceding mappings, if any), and produces a |
318 |
|
|
* new value. |
319 |
|
|
* @param mapper the mapper |
320 |
|
|
* @return operation prefix |
321 |
|
|
*/ |
322 |
|
|
public abstract ParallelArrayWithDoubleMapping<T> withIndexedMapping |
323 |
|
|
(IntAndLongToDouble mapper); |
324 |
|
|
|
325 |
|
|
/** |
326 |
|
|
* Returns an operation prefix that causes a method to operate |
327 |
|
|
* on mappings of this array using the given mapper that |
328 |
|
|
* accepts as arguments an element's current index and value |
329 |
|
|
* (as mapped by preceding mappings, if any), and produces a |
330 |
|
|
* new value. |
331 |
|
|
* @param mapper the mapper |
332 |
|
|
* @return operation prefix |
333 |
|
|
*/ |
334 |
|
|
public abstract ParallelArrayWithLongMapping<T> withIndexedMapping |
335 |
|
|
(IntAndLongToLong mapper); |
336 |
|
|
|
337 |
|
|
/** |
338 |
|
|
* Returns an Iterable view to sequentially step through mapped |
339 |
|
|
* elements also obeying bound and filter constraints, without |
340 |
jsr166 |
1.4 |
* performing computations to evaluate them in parallel. |
341 |
dl |
1.1 |
* @return the Iterable view |
342 |
|
|
*/ |
343 |
|
|
public Iterable<Long> sequentially() { |
344 |
|
|
return new SequentiallyAsLong(); |
345 |
|
|
} |
346 |
|
|
} |