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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
package extra166y; |
61 |
|
|
62 |
|
/** |
63 |
|
* Creates a new ParallelDoubleArray using the given executor and |
64 |
< |
* an array of the given size |
64 |
> |
* an array of the given size. |
65 |
|
* @param size the array size |
66 |
|
* @param executor the executor |
67 |
|
*/ |
137 |
|
* mapped ParallelDoubleArray. |
138 |
|
*/ |
139 |
|
public static interface SummaryStatistics { |
140 |
< |
/** Return the number of elements */ |
140 |
> |
/** Returns the number of elements */ |
141 |
|
public int size(); |
142 |
< |
/** Return the minimum element, or Double.MAX_VALUE if empty */ |
142 |
> |
/** Returns the minimum element, or Double.MAX_VALUE if empty */ |
143 |
|
public double min(); |
144 |
< |
/** Return the maximum element, or -Double.MAX_VALUE if empty */ |
144 |
> |
/** Returns the maximum element, or -Double.MAX_VALUE if empty */ |
145 |
|
public double max(); |
146 |
< |
/** Return the index of the minimum element, or -1 if empty */ |
146 |
> |
/** Returns the index of the minimum element, or -1 if empty */ |
147 |
|
public int indexOfMin(); |
148 |
< |
/** Return the index of the maximum element, or -1 if empty */ |
148 |
> |
/** Returns the index of the maximum element, or -1 if empty */ |
149 |
|
public int indexOfMax(); |
150 |
< |
/** Return the sum of all elements */ |
150 |
> |
/** Returns the sum of all elements */ |
151 |
|
public double sum(); |
152 |
< |
/** Return the arithmetic average of all elements */ |
152 |
> |
/** Returns the arithmetic average of all elements */ |
153 |
|
public double average(); |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
< |
* Returns the executor used for computations |
157 |
> |
* Returns the executor used for computations. |
158 |
|
* @return the executor |
159 |
|
*/ |
160 |
|
public ForkJoinPool getExecutor() { return ex; } |
161 |
|
|
162 |
|
/** |
163 |
< |
* Applies the given procedure to elements |
163 |
> |
* Applies the given procedure to elements. |
164 |
|
* @param procedure the procedure |
165 |
|
*/ |
166 |
|
public void apply(DoubleProcedure procedure) { |
168 |
|
} |
169 |
|
|
170 |
|
/** |
171 |
< |
* Returns reduction of elements |
171 |
> |
* Returns reduction of elements. |
172 |
|
* @param reducer the reducer |
173 |
|
* @param base the result for an empty array |
174 |
|
* @return reduction |
178 |
|
} |
179 |
|
|
180 |
|
/** |
181 |
< |
* Returns a new ParallelDoubleArray holding all elements |
181 |
> |
* Returns a new ParallelDoubleArray holding all elements. |
182 |
|
* @return a new ParallelDoubleArray holding all elements |
183 |
|
*/ |
184 |
|
public ParallelDoubleArray all() { |
191 |
|
* @param op the op |
192 |
|
* @return this (to simplify use in expressions) |
193 |
|
*/ |
194 |
< |
public ParallelDoubleArray replaceWithMapping(DoubleOp op) { |
194 |
> |
public ParallelDoubleArray replaceWithMapping(DoubleOp op) { |
195 |
|
super.replaceWithMapping(op); |
196 |
|
return this; |
197 |
|
} |
209 |
|
|
210 |
|
/** |
211 |
|
* Replaces elements with the results of applying the given |
212 |
< |
* mapping to each index and current element value |
212 |
> |
* mapping to each index and current element value. |
213 |
|
* @param op the op |
214 |
|
* @return this (to simplify use in expressions) |
215 |
|
*/ |
222 |
|
* Replaces elements with the results of applying the given |
223 |
|
* generator. For example, to fill the array with uniform random |
224 |
|
* values, use |
225 |
< |
* <tt>replaceWithGeneratedValue(Ops.doubleRandom())</tt> |
225 |
> |
* {@code replaceWithGeneratedValue(Ops.doubleRandom())}. |
226 |
|
* @param generator the generator |
227 |
|
* @return this (to simplify use in expressions) |
228 |
|
*/ |
243 |
|
|
244 |
|
/** |
245 |
|
* Replaces elements with results of applying |
246 |
< |
* <tt>op(thisElement, otherElement)</tt> |
246 |
> |
* {@code op(thisElement, otherElement)}. |
247 |
|
* @param other the other array |
248 |
|
* @param combiner the combiner |
249 |
|
* @return this (to simplify use in expressions) |
250 |
|
* @throws ArrayIndexOutOfBoundsException if other array has |
251 |
< |
* fewer elements than this array. |
251 |
> |
* fewer elements than this array |
252 |
|
*/ |
253 |
|
public ParallelDoubleArray replaceWithMapping |
254 |
|
(BinaryDoubleOp combiner, ParallelDoubleArrayWithDoubleMapping other) { |
258 |
|
|
259 |
|
/** |
260 |
|
* Replaces elements with results of applying |
261 |
< |
* <tt>op(thisElement, otherElement)</tt> |
261 |
> |
* {@code op(thisElement, otherElement)}. |
262 |
|
* @param other the other array |
263 |
|
* @param combiner the combiner |
264 |
|
* @return this (to simplify use in expressions) |
265 |
|
* @throws ArrayIndexOutOfBoundsException if other array has |
266 |
< |
* fewer elements than this array. |
266 |
> |
* fewer elements than this array |
267 |
|
*/ |
268 |
|
public ParallelDoubleArray replaceWithMapping(BinaryDoubleOp combiner, |
269 |
|
double[] other) { |
273 |
|
|
274 |
|
/** |
275 |
|
* Returns the index of some element equal to given target, or -1 |
276 |
< |
* if not present |
276 |
> |
* if not present. |
277 |
|
* @param target the element to search for |
278 |
|
* @return the index or -1 if not present |
279 |
|
*/ |
310 |
|
* to locate minimum and maximum elements. |
311 |
|
* @param comparator the comparator to use for |
312 |
|
* locating minimum and maximum elements |
313 |
< |
* @return the summary. |
313 |
> |
* @return the summary |
314 |
|
*/ |
315 |
|
public ParallelDoubleArray.SummaryStatistics summary |
316 |
|
(DoubleComparator comparator) { |
318 |
|
} |
319 |
|
|
320 |
|
/** |
321 |
< |
* Returns summary statistics, using natural comparator |
322 |
< |
* @return the summary. |
321 |
> |
* Returns summary statistics, using natural comparator. |
322 |
> |
* @return the summary |
323 |
|
*/ |
324 |
|
public ParallelDoubleArray.SummaryStatistics summary() { |
325 |
|
return super.summary(); |
326 |
|
} |
327 |
|
|
328 |
|
/** |
329 |
< |
* Returns the minimum element, or Double.MAX_VALUE if empty |
329 |
> |
* Returns the minimum element, or Double.MAX_VALUE if empty. |
330 |
|
* @param comparator the comparator |
331 |
|
* @return minimum element, or Double.MAX_VALUE if empty |
332 |
|
*/ |
335 |
|
} |
336 |
|
|
337 |
|
/** |
338 |
< |
* Returns the minimum element, or Double.MAX_VALUE if empty, |
338 |
> |
* Returns the minimum element, or Double.MAX_VALUE if empty. |
339 |
|
* @return minimum element, or Double.MAX_VALUE if empty |
340 |
|
*/ |
341 |
|
public double min() { |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
< |
* Returns the maximum element, or -Double.MAX_VALUE if empty |
346 |
> |
* Returns the maximum element, or -Double.MAX_VALUE if empty. |
347 |
|
* @param comparator the comparator |
348 |
|
* @return maximum element, or -Double.MAX_VALUE if empty |
349 |
|
*/ |
352 |
|
} |
353 |
|
|
354 |
|
/** |
355 |
< |
* Returns the maximum element, or -Double.MAX_VALUE if empty |
355 |
> |
* Returns the maximum element, or -Double.MAX_VALUE if empty. |
356 |
|
* @return maximum element, or -Double.MAX_VALUE if empty |
357 |
|
*/ |
358 |
|
public double max() { |
362 |
|
/** |
363 |
|
* Replaces each element with the running cumulation of applying |
364 |
|
* the given reducer. For example, if the contents are the numbers |
365 |
< |
* <tt>1, 2, 3</tt>, and the reducer operation adds numbers, then |
366 |
< |
* after invocation of this method, the contents would be <tt>1, |
367 |
< |
* 3, 6</tt> (that is, <tt>1, 1+2, 1+2+3</tt>); |
365 |
> |
* {@code 1, 2, 3}, and the reducer operation adds numbers, then |
366 |
> |
* after invocation of this method, the contents would be {@code 1, |
367 |
> |
* 3, 6} (that is, {@code 1, 1+2, 1+2+3}). |
368 |
|
* @param reducer the reducer |
369 |
|
* @param base the result for an empty array |
370 |
|
* @return this (to simplify use in expressions) |
377 |
|
/** |
378 |
|
* Replaces each element with the cumulation of applying the given |
379 |
|
* reducer to all previous values, and returns the total |
380 |
< |
* reduction. For example, if the contents are the numbers <tt>1, |
381 |
< |
* 2, 3</tt>, and the reducer operation adds numbers, then after |
382 |
< |
* invocation of this method, the contents would be <tt>0, 1, |
383 |
< |
* 3</tt> (that is, <tt>0, 0+1, 0+1+2</tt>, and the return value |
384 |
< |
* would be 6 (that is, <tt> 1+2+3</tt>); |
380 |
> |
* reduction. For example, if the contents are the numbers {@code 1, |
381 |
> |
* 2, 3}, and the reducer operation adds numbers, then after |
382 |
> |
* invocation of this method, the contents would be {@code 0, 1, |
383 |
> |
* 3} (that is, {@code 0, 0+1, 0+1+2}, and the return value |
384 |
> |
* would be 6 (that is, {@code 1+2+3}). |
385 |
|
* @param reducer the reducer |
386 |
|
* @param base the result for an empty array |
387 |
|
* @return the total reduction |
406 |
|
* Sorts the array, assuming all elements are Comparable. Unlike |
407 |
|
* Arrays.sort, this sort does not guarantee that elements |
408 |
|
* with equal keys maintain their relative position in the array. |
409 |
< |
* @throws ClassCastException if any element is not Comparable. |
409 |
> |
* @throws ClassCastException if any element is not Comparable |
410 |
|
* @return this (to simplify use in expressions) |
411 |
|
*/ |
412 |
|
public ParallelDoubleArray sort() { |
439 |
|
} |
440 |
|
|
441 |
|
/** |
442 |
< |
* Equivalent to <tt>asList().addAll</tt> but specialized for |
442 |
> |
* Equivalent to {@code asList().addAll} but specialized for |
443 |
|
* array arguments and likely to be more efficient. |
444 |
|
* @param other the elements to add |
445 |
|
* @return this (to simplify use in expressions) |
502 |
|
removeSlotsAt(f.offset, fence); |
503 |
|
return this; |
504 |
|
} |
505 |
+ |
|
506 |
|
/** |
507 |
|
* Returns true if all elements at the same relative positions |
508 |
|
* of this and other array are equal. |
515 |
|
} |
516 |
|
|
517 |
|
/** |
518 |
< |
* Returns the sum of elements |
518 |
> |
* Returns the sum of elements. |
519 |
|
* @return the sum of elements |
520 |
|
*/ |
521 |
|
public double sum() { |
523 |
|
} |
524 |
|
|
525 |
|
/** |
526 |
< |
* Replaces each element with the running sum |
526 |
> |
* Replaces each element with the running sum. |
527 |
|
* @return this (to simplify use in expressions) |
528 |
|
*/ |
529 |
|
public ParallelDoubleArray cumulateSum() { |
532 |
|
} |
533 |
|
|
534 |
|
/** |
535 |
< |
* Replaces each element with its prefix sum |
535 |
> |
* Replaces each element with its prefix sum. |
536 |
|
* @return the total sum |
537 |
|
*/ |
538 |
|
public double precumulateSum() { |
555 |
|
/** |
556 |
|
* Returns an operation prefix that causes a method to operate |
557 |
|
* only on the elements of the array for which the given selector |
558 |
< |
* returns true |
558 |
> |
* returns true. |
559 |
|
* @param selector the selector |
560 |
|
* @return operation prefix |
561 |
|
*/ |
566 |
|
/** |
567 |
|
* Returns an operation prefix that causes a method to operate |
568 |
|
* only on elements for which the given binary selector returns |
569 |
< |
* true |
569 |
> |
* true. |
570 |
|
* @param selector the selector |
571 |
|
* @return operation prefix |
572 |
|
*/ |
579 |
|
/** |
580 |
|
* Returns an operation prefix that causes a method to operate |
581 |
|
* only on elements for which the given indexed selector returns |
582 |
< |
* true |
582 |
> |
* true. |
583 |
|
* @param selector the selector |
584 |
|
* @return operation prefix |
585 |
|
*/ |
626 |
|
* @param other the other array |
627 |
|
* @return operation prefix |
628 |
|
* @throws IllegalArgumentException if other array is a |
629 |
< |
* filtered view (all filters must precede all mappings). |
629 |
> |
* filtered view (all filters must precede all mappings) |
630 |
|
*/ |
631 |
|
public <V,W,X> ParallelDoubleArrayWithMapping<W> withMapping |
632 |
|
(DoubleAndObjectToObject<? super V, ? extends W> combiner, |
641 |
|
* @param other the other array |
642 |
|
* @return operation prefix |
643 |
|
* @throws IllegalArgumentException if other array is a |
644 |
< |
* filtered view (all filters must precede all mappings). |
644 |
> |
* filtered view (all filters must precede all mappings) |
645 |
|
*/ |
646 |
|
public <V> ParallelDoubleArrayWithMapping<V> withMapping |
647 |
|
(DoubleAndDoubleToObject<? extends V> combiner, |
656 |
|
* @param other the other array |
657 |
|
* @return operation prefix |
658 |
|
* @throws IllegalArgumentException if other array is a |
659 |
< |
* filtered view (all filters must precede all mappings). |
659 |
> |
* filtered view (all filters must precede all mappings) |
660 |
|
*/ |
661 |
|
public <V> ParallelDoubleArrayWithMapping<V> withMapping |
662 |
|
(DoubleAndLongToObject<? extends V> combiner, |
671 |
|
* @param other the other array |
672 |
|
* @return operation prefix |
673 |
|
* @throws IllegalArgumentException if other array is a |
674 |
< |
* filtered view (all filters must precede all mappings). |
674 |
> |
* filtered view (all filters must precede all mappings) |
675 |
|
*/ |
676 |
|
public <V,W> ParallelDoubleArrayWithDoubleMapping withMapping |
677 |
|
(DoubleAndObjectToDouble<? super V> combiner, |
686 |
|
* @param other the other array |
687 |
|
* @return operation prefix |
688 |
|
* @throws IllegalArgumentException if other array is a |
689 |
< |
* filtered view (all filters must precede all mappings). |
689 |
> |
* filtered view (all filters must precede all mappings) |
690 |
|
*/ |
691 |
|
public ParallelDoubleArrayWithDoubleMapping withMapping |
692 |
|
(BinaryDoubleOp combiner, |
701 |
|
* @param other the other array |
702 |
|
* @return operation prefix |
703 |
|
* @throws IllegalArgumentException if other array is a |
704 |
< |
* filtered view (all filters must precede all mappings). |
704 |
> |
* filtered view (all filters must precede all mappings) |
705 |
|
*/ |
706 |
|
public ParallelDoubleArrayWithDoubleMapping withMapping |
707 |
|
(DoubleAndLongToDouble combiner, |
716 |
|
* @param other the other array |
717 |
|
* @return operation prefix |
718 |
|
* @throws IllegalArgumentException if other array is a |
719 |
< |
* filtered view (all filters must precede all mappings). |
719 |
> |
* filtered view (all filters must precede all mappings) |
720 |
|
*/ |
721 |
|
public <V,W> ParallelDoubleArrayWithLongMapping withMapping |
722 |
|
(DoubleAndObjectToLong<? super V> combiner, |
731 |
|
* @param other the other array |
732 |
|
* @return operation prefix |
733 |
|
* @throws IllegalArgumentException if other array is a |
734 |
< |
* filtered view (all filters must precede all mappings). |
734 |
> |
* filtered view (all filters must precede all mappings) |
735 |
|
*/ |
736 |
|
public ParallelDoubleArrayWithLongMapping withMapping |
737 |
|
(DoubleAndDoubleToLong combiner, |
746 |
|
* @param other the other array |
747 |
|
* @return operation prefix |
748 |
|
* @throws IllegalArgumentException if other array is a |
749 |
< |
* filtered view (all filters must precede all mappings). |
749 |
> |
* filtered view (all filters must precede all mappings) |
750 |
|
*/ |
751 |
|
public ParallelDoubleArrayWithLongMapping withMapping |
752 |
|
(DoubleAndLongToLong combiner, |
797 |
|
* Returns an iterator stepping through each element of the array |
798 |
|
* up to the current limit. This iterator does <em>not</em> |
799 |
|
* support the remove operation. However, a full |
800 |
< |
* <tt>ListIterator</tt> supporting add, remove, and set |
800 |
> |
* {@code ListIterator} supporting add, remove, and set |
801 |
|
* operations is available via {@link #asList}. |
802 |
< |
* @return an iterator stepping through each element. |
802 |
> |
* @return an iterator stepping through each element |
803 |
|
*/ |
804 |
|
public Iterator<Double> iterator() { |
805 |
|
return new ParallelDoubleArrayIterator(array, fence); |
852 |
|
public int size() { return fence; } |
853 |
|
|
854 |
|
/** |
855 |
< |
* Returns the underlying array used for computations |
855 |
> |
* Returns the underlying array used for computations. |
856 |
|
* @return the array |
857 |
|
*/ |
858 |
|
public double[] getArray() { return array; } |
859 |
|
|
860 |
|
/** |
861 |
< |
* Returns the element of the array at the given index |
861 |
> |
* Returns the element of the array at the given index. |
862 |
|
* @param i the index |
863 |
|
* @return the element of the array at the given index |
864 |
|
*/ |
865 |
|
public double get(int i) { return array[i]; } |
866 |
|
|
867 |
|
/** |
868 |
< |
* Sets the element of the array at the given index to the given value |
868 |
> |
* Sets the element of the array at the given index to the given value. |
869 |
|
* @param i the index |
870 |
|
* @param x the value |
871 |
|
*/ |
872 |
|
public void set(int i, double x) { array[i] = x; } |
873 |
|
|
874 |
|
/** |
875 |
< |
* Equivalent to <tt>asList().toString()</tt> |
875 |
> |
* Equivalent to {@code asList().toString()}. |
876 |
|
* @return a string representation |
877 |
|
*/ |
878 |
|
public String toString() { |
886 |
|
* than the length of the underlying array, causes computations to |
887 |
|
* ignore elements past the given limit. |
888 |
|
* @param newLimit the new upper bound |
889 |
< |
* @throws IllegalArgumentException if newLimit less than zero. |
889 |
> |
* @throws IllegalArgumentException if newLimit less than zero |
890 |
|
*/ |
891 |
|
public final void setLimit(int newLimit) { |
892 |
|
if (newLimit < 0) |
923 |
|
} |
924 |
|
|
925 |
|
/** |
926 |
< |
* Make len slots available at index |
926 |
> |
* Makes len slots available at index. |
927 |
|
*/ |
928 |
|
final void insertSlotsAt(int index, int len) { |
929 |
|
if (len <= 0) |
1165 |
|
} |
1166 |
|
|
1167 |
|
} |
1167 |
– |
|