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