5 |
|
*/ |
6 |
|
|
7 |
|
package extra166y; |
8 |
+ |
|
9 |
|
import jsr166y.*; |
10 |
|
import static extra166y.Ops.*; |
11 |
|
import java.util.*; |
47 |
|
* public long op(int i) { return i + 2; } }; |
48 |
|
* static LongPredicate notDivisibleBy(final long p) { |
49 |
|
* return new LongPredicate() { |
50 |
< |
* public boolean op(long n) { return n <= p || (n % p) != 0; } |
50 |
> |
* public boolean op(long n) { return n <= p || (n % p) != 0; } |
51 |
|
* }; } |
52 |
|
* static LongPredicate notProbablePrime = new LongPredicate() { |
53 |
|
* private static final int CERTAINTY = 8; |
102 |
|
|
103 |
|
/** |
104 |
|
* Creates a new ParallelLongArray using the given executor and |
105 |
< |
* an array of the given size |
105 |
> |
* an array of the given size. |
106 |
|
* @param size the array size |
107 |
|
* @param executor the executor |
108 |
|
*/ |
178 |
|
* mapped ParallelLongArray. |
179 |
|
*/ |
180 |
|
public static interface SummaryStatistics { |
181 |
< |
/** Return the number of elements */ |
181 |
> |
/** Returns the number of elements */ |
182 |
|
public int size(); |
183 |
< |
/** Return the minimum element, or Long.MAX_VALUE if empty */ |
183 |
> |
/** Returns the minimum element, or Long.MAX_VALUE if empty */ |
184 |
|
public long min(); |
185 |
< |
/** Return the maximum element, or Long.MIN_VALUE if empty */ |
185 |
> |
/** Returns the maximum element, or Long.MIN_VALUE if empty */ |
186 |
|
public long max(); |
187 |
< |
/** Return the index of the minimum element, or -1 if empty */ |
187 |
> |
/** Returns the index of the minimum element, or -1 if empty */ |
188 |
|
public int indexOfMin(); |
189 |
< |
/** Return the index of the maximum element, or -1 if empty */ |
189 |
> |
/** Returns the index of the maximum element, or -1 if empty */ |
190 |
|
public int indexOfMax(); |
191 |
< |
/** Return the sum of all elements */ |
191 |
> |
/** Returns the sum of all elements */ |
192 |
|
public long sum(); |
193 |
< |
/** Return the arithmetic average of all elements */ |
193 |
> |
/** Returns the arithmetic average of all elements */ |
194 |
|
public double average(); |
195 |
|
} |
196 |
|
|
197 |
|
/** |
198 |
< |
* Returns the executor used for computations |
198 |
> |
* Returns the executor used for computations. |
199 |
|
* @return the executor |
200 |
|
*/ |
201 |
|
public ForkJoinPool getExecutor() { return ex; } |
202 |
|
|
203 |
|
/** |
204 |
< |
* Applies the given procedure to elements |
204 |
> |
* Applies the given procedure to elements. |
205 |
|
* @param procedure the procedure |
206 |
|
*/ |
207 |
|
public void apply(LongProcedure procedure) { |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
< |
* Returns reduction of elements |
212 |
> |
* Returns reduction of elements. |
213 |
|
* @param reducer the reducer |
214 |
|
* @param base the result for an empty array |
215 |
|
* @return reduction |
219 |
|
} |
220 |
|
|
221 |
|
/** |
222 |
< |
* Returns a new ParallelLongArray holding all elements |
222 |
> |
* Returns a new ParallelLongArray holding all elements. |
223 |
|
* @return a new ParallelLongArray holding all elements |
224 |
|
*/ |
225 |
|
public ParallelLongArray all() { |
232 |
|
* @param op the op |
233 |
|
* @return this (to simplify use in expressions) |
234 |
|
*/ |
235 |
< |
public ParallelLongArray replaceWithMapping(LongOp op) { |
235 |
> |
public ParallelLongArray replaceWithMapping(LongOp op) { |
236 |
|
super.replaceWithMapping(op); |
237 |
|
return this; |
238 |
|
} |
250 |
|
|
251 |
|
/** |
252 |
|
* Replaces elements with the results of applying the given |
253 |
< |
* mapping to each index and current element value |
253 |
> |
* mapping to each index and current element value. |
254 |
|
* @param op the op |
255 |
|
* @return this (to simplify use in expressions) |
256 |
|
*/ |
263 |
|
* Replaces elements with the results of applying the given |
264 |
|
* generator. For example, to fill the array with uniform random |
265 |
|
* values, use |
266 |
< |
* <tt>replaceWithGeneratedValue(Ops.longRandom())</tt> |
266 |
> |
* {@code replaceWithGeneratedValue(Ops.longRandom())}. |
267 |
|
* @param generator the generator |
268 |
|
* @return this (to simplify use in expressions) |
269 |
|
*/ |
284 |
|
|
285 |
|
/** |
286 |
|
* Replaces elements with results of applying |
287 |
< |
* <tt>op(thisElement, otherElement)</tt> |
287 |
> |
* {@code op(thisElement, otherElement)}. |
288 |
|
* @param other the other array |
289 |
|
* @param combiner the combiner |
290 |
|
* @return this (to simplify use in expressions) |
298 |
|
|
299 |
|
/** |
300 |
|
* Replaces elements with results of applying |
301 |
< |
* <tt>op(thisElement, otherElement)</tt> |
301 |
> |
* {@code op(thisElement, otherElement)}. |
302 |
|
* @param other the other array |
303 |
|
* @param combiner the combiner |
304 |
|
* @return this (to simplify use in expressions) |
305 |
|
* @throws ArrayIndexOutOfBoundsException if other array has |
306 |
< |
* fewer elements than this array. |
306 |
> |
* fewer elements than this array |
307 |
|
*/ |
308 |
|
public ParallelLongArray replaceWithMapping(BinaryLongOp combiner, |
309 |
|
long[] other) { |
313 |
|
|
314 |
|
/** |
315 |
|
* Returns the index of some element equal to given target, or -1 |
316 |
< |
* if not present |
316 |
> |
* if not present. |
317 |
|
* @param target the element to search for |
318 |
|
* @return the index or -1 if not present |
319 |
|
*/ |
350 |
|
* to locate minimum and maximum elements. |
351 |
|
* @param comparator the comparator to use for |
352 |
|
* locating minimum and maximum elements |
353 |
< |
* @return the summary. |
353 |
> |
* @return the summary |
354 |
|
*/ |
355 |
|
public ParallelLongArray.SummaryStatistics summary |
356 |
|
(LongComparator comparator) { |
358 |
|
} |
359 |
|
|
360 |
|
/** |
361 |
< |
* Returns summary statistics, using natural comparator |
362 |
< |
* @return the summary. |
361 |
> |
* Returns summary statistics, using natural comparator. |
362 |
> |
* @return the summary |
363 |
|
*/ |
364 |
|
public ParallelLongArray.SummaryStatistics summary() { |
365 |
|
return super.summary(); |
366 |
|
} |
367 |
|
|
368 |
|
/** |
369 |
< |
* Returns the minimum element, or Long.MAX_VALUE if empty |
369 |
> |
* Returns the minimum element, or Long.MAX_VALUE if empty. |
370 |
|
* @param comparator the comparator |
371 |
|
* @return minimum element, or Long.MAX_VALUE if empty |
372 |
|
*/ |
375 |
|
} |
376 |
|
|
377 |
|
/** |
378 |
< |
* Returns the minimum element, or Long.MAX_VALUE if empty, |
378 |
> |
* Returns the minimum element, or Long.MAX_VALUE if empty. |
379 |
|
* @return minimum element, or Long.MAX_VALUE if empty |
380 |
|
*/ |
381 |
|
public long min() { |
383 |
|
} |
384 |
|
|
385 |
|
/** |
386 |
< |
* Returns the maximum element, or Long.MIN_VALUE if empty |
386 |
> |
* Returns the maximum element, or Long.MIN_VALUE if empty. |
387 |
|
* @param comparator the comparator |
388 |
|
* @return maximum element, or Long.MIN_VALUE if empty |
389 |
|
*/ |
392 |
|
} |
393 |
|
|
394 |
|
/** |
395 |
< |
* Returns the maximum element, or Long.MIN_VALUE if empty |
395 |
> |
* Returns the maximum element, or Long.MIN_VALUE if empty. |
396 |
|
* @return maximum element, or Long.MIN_VALUE if empty |
397 |
|
*/ |
398 |
|
public long max() { |
402 |
|
/** |
403 |
|
* Replaces each element with the running cumulation of applying |
404 |
|
* the given reducer. For example, if the contents are the numbers |
405 |
< |
* <tt>1, 2, 3</tt>, and the reducer operation adds numbers, then |
406 |
< |
* after invocation of this method, the contents would be <tt>1, |
407 |
< |
* 3, 6</tt> (that is, <tt>1, 1+2, 1+2+3</tt>); |
405 |
> |
* {@code 1, 2, 3}, and the reducer operation adds numbers, then |
406 |
> |
* after invocation of this method, the contents would be {@code 1, |
407 |
> |
* 3, 6} (that is, {@code 1, 1+2, 1+2+3}). |
408 |
|
* @param reducer the reducer |
409 |
|
* @param base the result for an empty array |
410 |
|
* @return this (to simplify use in expressions) |
417 |
|
/** |
418 |
|
* Replaces each element with the cumulation of applying the given |
419 |
|
* reducer to all previous values, and returns the total |
420 |
< |
* reduction. For example, if the contents are the numbers <tt>1, |
421 |
< |
* 2, 3</tt>, and the reducer operation adds numbers, then after |
422 |
< |
* invocation of this method, the contents would be <tt>0, 1, |
423 |
< |
* 3</tt> (that is, <tt>0, 0+1, 0+1+2</tt>, and the return value |
424 |
< |
* would be 6 (that is, <tt> 1+2+3</tt>); |
420 |
> |
* reduction. For example, if the contents are the numbers {@code 1, |
421 |
> |
* 2, 3}, and the reducer operation adds numbers, then after |
422 |
> |
* invocation of this method, the contents would be {@code 0, 1, |
423 |
> |
* 3} (that is, {@code 0, 0+1, 0+1+2}, and the return value |
424 |
> |
* would be 6 (that is, {@code 1+2+3}). |
425 |
|
* @param reducer the reducer |
426 |
|
* @param base the result for an empty array |
427 |
|
* @return the total reduction |
446 |
|
* Sorts the array, assuming all elements are Comparable. Unlike |
447 |
|
* Arrays.sort, this sort does not guarantee that elements |
448 |
|
* with equal keys maintain their relative position in the array. |
448 |
– |
* @throws ClassCastException if any element is not Comparable. |
449 |
|
* @return this (to simplify use in expressions) |
450 |
+ |
* @throws ClassCastException if any element is not Comparable |
451 |
|
*/ |
452 |
|
public ParallelLongArray sort() { |
453 |
|
super.sort(); |
479 |
|
} |
480 |
|
|
481 |
|
/** |
482 |
< |
* Equivalent to <tt>asList().addAll</tt> but specialized for array |
482 |
> |
* Equivalent to {@code asList().addAll} but specialized for array |
483 |
|
* arguments and likely to be more efficient. |
484 |
|
* @param other the elements to add |
485 |
|
* @return this (to simplify use in expressions) |
553 |
|
} |
554 |
|
|
555 |
|
/** |
556 |
< |
* Returns the sum of elements |
556 |
> |
* Returns the sum of elements. |
557 |
|
* @return the sum of elements |
558 |
|
*/ |
559 |
|
public long sum() { |
561 |
|
} |
562 |
|
|
563 |
|
/** |
564 |
< |
* Replaces each element with the running sum |
564 |
> |
* Replaces each element with the running sum. |
565 |
|
* @return this (to simplify use in expressions) |
566 |
|
*/ |
567 |
|
public ParallelLongArray cumulateSum() { |
570 |
|
} |
571 |
|
|
572 |
|
/** |
573 |
< |
* Replaces each element with its prefix sum |
573 |
> |
* Replaces each element with its prefix sum. |
574 |
|
* @return the total sum |
575 |
|
*/ |
576 |
|
public long precumulateSum() { |
593 |
|
/** |
594 |
|
* Returns an operation prefix that causes a method to operate |
595 |
|
* only on the elements of the array for which the given selector |
596 |
< |
* returns true |
596 |
> |
* returns true. |
597 |
|
* @param selector the selector |
598 |
|
* @return operation prefix |
599 |
|
*/ |
604 |
|
/** |
605 |
|
* Returns an operation prefix that causes a method to operate |
606 |
|
* only on elements for which the given binary selector returns |
607 |
< |
* true |
607 |
> |
* true. |
608 |
|
* @param selector the selector |
609 |
|
* @return operation prefix |
610 |
|
*/ |
617 |
|
/** |
618 |
|
* Returns an operation prefix that causes a method to operate |
619 |
|
* only on elements for which the given indexed selector returns |
620 |
< |
* true |
620 |
> |
* true. |
621 |
|
* @param selector the selector |
622 |
|
* @return operation prefix |
623 |
|
*/ |
665 |
|
* @param other the other array |
666 |
|
* @return operation prefix |
667 |
|
* @throws IllegalArgumentException if other array is a |
668 |
< |
* filtered view (all filters must precede all mappings). |
668 |
> |
* filtered view (all filters must precede all mappings) |
669 |
|
*/ |
670 |
|
public <V,W,X> ParallelLongArrayWithMapping<W> withMapping |
671 |
|
(LongAndObjectToObject<? super V, ? extends W> combiner, |
680 |
|
* @param other the other array |
681 |
|
* @return operation prefix |
682 |
|
* @throws IllegalArgumentException if other array is a |
683 |
< |
* filtered view (all filters must precede all mappings). |
683 |
> |
* filtered view (all filters must precede all mappings) |
684 |
|
*/ |
685 |
|
public <V> ParallelLongArrayWithMapping<V> withMapping |
686 |
|
(LongAndDoubleToObject<? extends V> combiner, |
695 |
|
* @param other the other array |
696 |
|
* @return operation prefix |
697 |
|
* @throws IllegalArgumentException if other array is a |
698 |
< |
* filtered view (all filters must precede all mappings). |
698 |
> |
* filtered view (all filters must precede all mappings) |
699 |
|
*/ |
700 |
|
public <V> ParallelLongArrayWithMapping<V> withMapping |
701 |
|
(LongAndLongToObject<? extends V> combiner, |
710 |
|
* @param other the other array |
711 |
|
* @return operation prefix |
712 |
|
* @throws IllegalArgumentException if other array is a |
713 |
< |
* filtered view (all filters must precede all mappings). |
713 |
> |
* filtered view (all filters must precede all mappings) |
714 |
|
*/ |
715 |
|
public <V,W> ParallelLongArrayWithDoubleMapping withMapping |
716 |
|
(LongAndObjectToDouble<? super V> combiner, |
725 |
|
* @param other the other array |
726 |
|
* @return operation prefix |
727 |
|
* @throws IllegalArgumentException if other array is a |
728 |
< |
* filtered view (all filters must precede all mappings). |
728 |
> |
* filtered view (all filters must precede all mappings) |
729 |
|
*/ |
730 |
|
public ParallelLongArrayWithDoubleMapping withMapping |
731 |
|
(LongAndDoubleToDouble combiner, |
740 |
|
* @param other the other array |
741 |
|
* @return operation prefix |
742 |
|
* @throws IllegalArgumentException if other array is a |
743 |
< |
* filtered view (all filters must precede all mappings). |
743 |
> |
* filtered view (all filters must precede all mappings) |
744 |
|
*/ |
745 |
|
public ParallelLongArrayWithDoubleMapping withMapping |
746 |
|
(LongAndLongToDouble combiner, |
755 |
|
* @param other the other array |
756 |
|
* @return operation prefix |
757 |
|
* @throws IllegalArgumentException if other array is a |
758 |
< |
* filtered view (all filters must precede all mappings). |
758 |
> |
* filtered view (all filters must precede all mappings) |
759 |
|
*/ |
760 |
|
public <V,W> ParallelLongArrayWithLongMapping withMapping |
761 |
|
(LongAndObjectToLong<? super V> combiner, |
770 |
|
* @param other the other array |
771 |
|
* @return operation prefix |
772 |
|
* @throws IllegalArgumentException if other array is a |
773 |
< |
* filtered view (all filters must precede all mappings). |
773 |
> |
* filtered view (all filters must precede all mappings) |
774 |
|
*/ |
775 |
|
public ParallelLongArrayWithLongMapping withMapping |
776 |
|
(LongAndDoubleToLong combiner, |
785 |
|
* @param other the other array |
786 |
|
* @return operation prefix |
787 |
|
* @throws IllegalArgumentException if other array is a |
788 |
< |
* filtered view (all filters must precede all mappings). |
788 |
> |
* filtered view (all filters must precede all mappings) |
789 |
|
*/ |
790 |
|
public ParallelLongArrayWithLongMapping withMapping |
791 |
|
(BinaryLongOp combiner, |
836 |
|
* Returns an iterator stepping through each element of the array |
837 |
|
* up to the current limit. This iterator does <em>not</em> |
838 |
|
* support the remove operation. However, a full |
839 |
< |
* <tt>ListIterator</tt> supporting add, remove, and set |
839 |
> |
* {@code ListIterator} supporting add, remove, and set |
840 |
|
* operations is available via {@link #asList}. |
841 |
< |
* @return an iterator stepping through each element. |
841 |
> |
* @return an iterator stepping through each element |
842 |
|
*/ |
843 |
|
public Iterator<Long> iterator() { |
844 |
|
return new ParallelLongArrayIterator(array, fence); |
890 |
|
public int size() { return fence; } |
891 |
|
|
892 |
|
/** |
893 |
< |
* Returns the underlying array used for computations |
893 |
> |
* Returns the underlying array used for computations. |
894 |
|
* @return the array |
895 |
|
*/ |
896 |
|
public long[] getArray() { return array; } |
897 |
|
|
898 |
|
/** |
899 |
< |
* Returns the element of the array at the given index |
899 |
> |
* Returns the element of the array at the given index. |
900 |
|
* @param i the index |
901 |
|
* @return the element of the array at the given index |
902 |
|
*/ |
903 |
|
public long get(int i) { return array[i]; } |
904 |
|
|
905 |
|
/** |
906 |
< |
* Sets the element of the array at the given index to the given value |
906 |
> |
* Sets the element of the array at the given index to the given value. |
907 |
|
* @param i the index |
908 |
|
* @param x the value |
909 |
|
*/ |
910 |
|
public void set(int i, long x) { array[i] = x; } |
911 |
|
|
912 |
|
/** |
913 |
< |
* Equivalent to <tt>asList().toString()</tt> |
913 |
> |
* Equivalent to {@code asList().toString()}. |
914 |
|
* @return a string representation |
915 |
|
*/ |
916 |
|
public String toString() { |
924 |
|
* than the length of the underlying array, causes computations to |
925 |
|
* ignore elements past the given limit. |
926 |
|
* @param newLimit the new upper bound |
927 |
< |
* @throws IllegalArgumentException if newLimit less than zero. |
927 |
> |
* @throws IllegalArgumentException if newLimit less than zero |
928 |
|
*/ |
929 |
|
public final void setLimit(int newLimit) { |
930 |
|
if (newLimit < 0) |
966 |
|
} |
967 |
|
|
968 |
|
/** |
969 |
< |
* Make len slots available at index |
969 |
> |
* Makes len slots available at index. |
970 |
|
*/ |
971 |
|
final void insertSlotsAt(int index, int len) { |
972 |
|
if (len <= 0) |
1208 |
|
} |
1209 |
|
|
1210 |
|
} |
1210 |
– |
|