ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/extra166y/ParallelDoubleArray.java
Revision: 1.15
Committed: Thu Feb 26 06:53:34 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.14: +0 -1 lines
Log Message:
delete unused imports

File Contents

# Content
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/publicdomain/zero/1.0/
5 */
6
7 package extra166y;
8
9 import jsr166y.*;
10 import static extra166y.Ops.*;
11 import java.util.*;
12 import java.util.concurrent.atomic.*;
13
14 /**
15 * An array of doubles supporting parallel operations. This class
16 * provides methods supporting the same operations as {@link
17 * ParallelArray}, but specialized for scalar doubles. It additionally
18 * provides a few methods specific to numerical values.
19 */
20 public class ParallelDoubleArray extends AbstractParallelAnyArray.DUPap {
21 // Same internals as ParallelArray, but specialized for doubles
22 AsList listView; // lazily constructed
23
24 /**
25 * Returns a common default executor for use in ParallelArrays.
26 * This executor arranges enough parallelism to use most, but not
27 * necessarily all, of the available processors on this system.
28 * @return the executor
29 */
30 public static ForkJoinPool defaultExecutor() {
31 return PAS.defaultExecutor();
32 }
33
34 /**
35 * Constructor for use by subclasses to create a new ParallelDoubleArray
36 * using the given executor, and initially using the supplied
37 * array, with effective size bound by the given limit. This
38 * constructor is designed to enable extensions via
39 * subclassing. To create a ParallelDoubleArray, use {@link #create},
40 * {@link #createEmpty}, {@link #createUsingHandoff} or {@link
41 * #createFromCopy}.
42 * @param executor the executor
43 * @param array the array
44 * @param limit the upper bound limit
45 */
46 protected ParallelDoubleArray(ForkJoinPool executor, double[] array,
47 int limit) {
48 super(executor, 0, limit, array);
49 if (executor == null || array == null)
50 throw new NullPointerException();
51 if (limit < 0 || limit > array.length)
52 throw new IllegalArgumentException();
53 }
54
55 /**
56 * Trusted internal version of protected constructor.
57 */
58 ParallelDoubleArray(ForkJoinPool executor, double[] array) {
59 super(executor, 0, array.length, array);
60 }
61
62 /**
63 * Creates a new ParallelDoubleArray using the given executor and
64 * an array of the given size.
65 * @param size the array size
66 * @param executor the executor
67 */
68 public static ParallelDoubleArray create
69 (int size, ForkJoinPool executor) {
70 double[] array = new double[size];
71 return new ParallelDoubleArray(executor, array, size);
72 }
73
74 /**
75 * Creates a new ParallelDoubleArray initially using the given array and
76 * executor. In general, the handed off array should not be used
77 * for other purposes once constructing this ParallelDoubleArray. The
78 * given array may be internally replaced by another array in the
79 * course of methods that add or remove elements.
80 * @param handoff the array
81 * @param executor the executor
82 */
83 public static ParallelDoubleArray createUsingHandoff
84 (double[] handoff, ForkJoinPool executor) {
85 return new ParallelDoubleArray(executor, handoff, handoff.length);
86 }
87
88 /**
89 * Creates a new ParallelDoubleArray using the given executor and
90 * initially holding copies of the given
91 * source elements.
92 * @param source the source of initial elements
93 * @param executor the executor
94 */
95 public static ParallelDoubleArray createFromCopy
96 (double[] source, ForkJoinPool executor) {
97 // For now, avoid copyOf so people can compile with Java5
98 int size = source.length;
99 double[] array = new double[size];
100 System.arraycopy(source, 0, array, 0, size);
101 return new ParallelDoubleArray(executor, array, size);
102 }
103
104 /**
105 * Creates a new ParallelDoubleArray using an array of the given size,
106 * initially holding copies of the given source truncated or
107 * padded with zeros to obtain the specified length.
108 * @param source the source of initial elements
109 * @param size the array size
110 * @param executor the executor
111 */
112 public static ParallelDoubleArray createFromCopy
113 (int size, double[] source, ForkJoinPool executor) {
114 // For now, avoid copyOf so people can compile with Java5
115 double[] array = new double[size];
116 System.arraycopy(source, 0, array, 0,
117 Math.min(source.length, size));
118 return new ParallelDoubleArray(executor, array, size);
119 }
120
121 /**
122 * Creates a new ParallelDoubleArray using the given executor and
123 * an array of the given size, but with an initial effective size
124 * of zero, enabling incremental insertion via {@link
125 * ParallelDoubleArray#asList} operations.
126 * @param size the array size
127 * @param executor the executor
128 */
129 public static ParallelDoubleArray createEmpty
130 (int size, ForkJoinPool executor) {
131 double[] array = new double[size];
132 return new ParallelDoubleArray(executor, array, 0);
133 }
134
135 /**
136 * Summary statistics for a possibly bounded, filtered, and/or
137 * mapped ParallelDoubleArray.
138 */
139 public static interface SummaryStatistics {
140 /** Returns the number of elements */
141 public int size();
142 /** Returns the minimum element, or Double.MAX_VALUE if empty */
143 public double min();
144 /** Returns the maximum element, or -Double.MAX_VALUE if empty */
145 public double max();
146 /** Returns the index of the minimum element, or -1 if empty */
147 public int indexOfMin();
148 /** Returns the index of the maximum element, or -1 if empty */
149 public int indexOfMax();
150 /** Returns the sum of all elements */
151 public double sum();
152 /** Returns the arithmetic average of all elements */
153 public double average();
154 }
155
156 /**
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.
164 * @param procedure the procedure
165 */
166 public void apply(DoubleProcedure procedure) {
167 super.apply(procedure);
168 }
169
170 /**
171 * Returns reduction of elements.
172 * @param reducer the reducer
173 * @param base the result for an empty array
174 * @return reduction
175 */
176 public double reduce(DoubleReducer reducer, double base) {
177 return super.reduce(reducer, base);
178 }
179
180 /**
181 * Returns a new ParallelDoubleArray holding all elements.
182 * @return a new ParallelDoubleArray holding all elements
183 */
184 public ParallelDoubleArray all() {
185 return super.all();
186 }
187
188 /**
189 * Replaces elements with the results of applying the given op
190 * to their current values.
191 * @param op the op
192 * @return this (to simplify use in expressions)
193 */
194 public ParallelDoubleArray replaceWithMapping(DoubleOp op) {
195 super.replaceWithMapping(op);
196 return this;
197 }
198
199 /**
200 * Replaces elements with the results of applying the given
201 * op to their indices.
202 * @param op the op
203 * @return this (to simplify use in expressions)
204 */
205 public ParallelDoubleArray replaceWithMappedIndex(IntToDouble op) {
206 super.replaceWithMappedIndex(op);
207 return this;
208 }
209
210 /**
211 * Replaces elements with the results of applying the given
212 * mapping to each index and current element value.
213 * @param op the op
214 * @return this (to simplify use in expressions)
215 */
216 public ParallelDoubleArray replaceWithMappedIndex(IntAndDoubleToDouble op) {
217 super.replaceWithMappedIndex(op);
218 return this;
219 }
220
221 /**
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 * {@code replaceWithGeneratedValue(Ops.doubleRandom())}.
226 * @param generator the generator
227 * @return this (to simplify use in expressions)
228 */
229 public ParallelDoubleArray replaceWithGeneratedValue(DoubleGenerator generator) {
230 super.replaceWithGeneratedValue(generator);
231 return this;
232 }
233
234 /**
235 * Replaces elements with the given value.
236 * @param value the value
237 * @return this (to simplify use in expressions)
238 */
239 public ParallelDoubleArray replaceWithValue(double value) {
240 super.replaceWithValue(value);
241 return this;
242 }
243
244 /**
245 * Replaces elements with results of applying
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
252 */
253 public ParallelDoubleArray replaceWithMapping
254 (BinaryDoubleOp combiner, ParallelDoubleArrayWithDoubleMapping other) {
255 super.replaceWithMapping(combiner, other);
256 return this;
257 }
258
259 /**
260 * Replaces elements with results of applying
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
267 */
268 public ParallelDoubleArray replaceWithMapping(BinaryDoubleOp combiner,
269 double[] other) {
270 super.replaceWithMapping(combiner, other);
271 return this;
272 }
273
274 /**
275 * Returns the index of some element equal to given target, or -1
276 * if not present.
277 * @param target the element to search for
278 * @return the index or -1 if not present
279 */
280 public int indexOf(double target) {
281 return super.indexOf(target);
282 }
283
284 /**
285 * Assuming this array is sorted, returns the index of an element
286 * equal to given target, or -1 if not present. If the array
287 * is not sorted, the results are undefined.
288 * @param target the element to search for
289 * @return the index or -1 if not present
290 */
291 public int binarySearch(double target) {
292 return super.binarySearch(target);
293 }
294
295 /**
296 * Assuming this array is sorted with respect to the given
297 * comparator, returns the index of an element equal to given
298 * target, or -1 if not present. If the array is not sorted, the
299 * results are undefined.
300 * @param target the element to search for
301 * @param comparator the comparator
302 * @return the index or -1 if not present
303 */
304 public int binarySearch(double target, DoubleComparator comparator) {
305 return super.binarySearch(target, comparator);
306 }
307
308 /**
309 * Returns summary statistics, using the given comparator
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
314 */
315 public ParallelDoubleArray.SummaryStatistics summary
316 (DoubleComparator comparator) {
317 return super.summary(comparator);
318 }
319
320 /**
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.
330 * @param comparator the comparator
331 * @return minimum element, or Double.MAX_VALUE if empty
332 */
333 public double min(DoubleComparator comparator) {
334 return super.min(comparator);
335 }
336
337 /**
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() {
342 return super.min();
343 }
344
345 /**
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 */
350 public double max(DoubleComparator comparator) {
351 return super.max(comparator);
352 }
353
354 /**
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() {
359 return super.max();
360 }
361
362 /**
363 * Replaces each element with the running cumulation of applying
364 * the given reducer. For example, if the contents are the numbers
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)
371 */
372 public ParallelDoubleArray cumulate(DoubleReducer reducer, double base) {
373 super.cumulate(reducer, base);
374 return this;
375 }
376
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 {@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
388 */
389 public double precumulate(DoubleReducer reducer, double base) {
390 return super.precumulate(reducer, base);
391 }
392
393 /**
394 * Sorts the array. Unlike Arrays.sort, this sort does
395 * not guarantee that elements with equal keys maintain their
396 * relative position in the array.
397 * @param comparator the comparator to use
398 * @return this (to simplify use in expressions)
399 */
400 public ParallelDoubleArray sort(DoubleComparator comparator) {
401 super.sort(comparator);
402 return this;
403 }
404
405 /**
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 * @return this (to simplify use in expressions)
410 * @throws ClassCastException if any element is not Comparable
411 */
412 public ParallelDoubleArray sort() {
413 super.sort();
414 return this;
415 }
416
417 /**
418 * Removes consecutive elements that are equal,
419 * shifting others leftward, and possibly decreasing size. This
420 * method may be used after sorting to ensure that this
421 * ParallelDoubleArray contains a set of unique elements.
422 * @return this (to simplify use in expressions)
423 */
424 public ParallelDoubleArray removeConsecutiveDuplicates() {
425 // Sequential implementation for now
426 int k = 0;
427 int n = fence;
428 if (k < n) {
429 double[] arr = this.array;
430 double last = arr[k++];
431 for (int i = k; i < n; ++i) {
432 double x = arr[i];
433 if (last != x)
434 arr[k++] = last = x;
435 }
436 removeSlotsAt(k, n);
437 }
438 return this;
439 }
440
441 /**
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)
446 */
447 public ParallelDoubleArray addAll(double[] other) {
448 int csize = other.length;
449 int end = fence;
450 insertSlotsAt(end, csize);
451 System.arraycopy(other, 0, array, end, csize);
452 return this;
453 }
454
455 /**
456 * Appends all (possibly bounded, filtered, or mapped) elements of
457 * the given ParallelDoubleArray, resizing and/or reallocating this
458 * array if necessary.
459 * @param other the elements to add
460 * @return this (to simplify use in expressions)
461 */
462 public ParallelDoubleArray addAll(ParallelDoubleArrayWithDoubleMapping other) {
463 int end = fence;
464 if (other.hasFilter()) {
465 PAS.FJDAppendAllDriver r = new PAS.FJDAppendAllDriver
466 (other, end, array);
467 ex.invoke(r);
468 array = r.results;
469 fence = end + r.resultSize;
470 }
471 else {
472 int csize = other.size();
473 insertSlotsAt(end, csize);
474 if (other.hasMap())
475 ex.invoke(new PAS.FJDMap(other, other.origin, other.fence,
476 null, array, end - other.origin));
477 else
478 System.arraycopy(other.array, 0, array, end, csize);
479 }
480 return this;
481 }
482
483 /**
484 * Returns a new ParallelDoubleArray containing only the unique
485 * elements of this array (that is, without any duplicates).
486 * @return the new ParallelDoubleArray
487 */
488 public ParallelDoubleArray allUniqueElements() {
489 return super.allUniqueElements();
490 }
491
492 /**
493 * Removes from the array all elements for which the given
494 * selector holds.
495 * @param selector the selector
496 * @return this (to simplify use in expressions)
497 */
498 public ParallelDoubleArray removeAll(DoublePredicate selector) {
499 DFPap v = new DFPap(ex, 0, fence, array, selector);
500 PAS.FJRemoveAllDriver f = new PAS.FJRemoveAllDriver(v, 0, fence);
501 ex.invoke(f);
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.
509 * @param other the other array
510 * @return true if equal
511 */
512 public boolean hasAllEqualElements
513 (ParallelDoubleArrayWithDoubleMapping other) {
514 return super.hasAllEqualElements(other);
515 }
516
517 /**
518 * Returns the sum of elements.
519 * @return the sum of elements
520 */
521 public double sum() {
522 return super.sum();
523 }
524
525 /**
526 * Replaces each element with the running sum.
527 * @return this (to simplify use in expressions)
528 */
529 public ParallelDoubleArray cumulateSum() {
530 super.cumulateSum();
531 return this;
532 }
533
534 /**
535 * Replaces each element with its prefix sum.
536 * @return the total sum
537 */
538 public double precumulateSum() {
539 return super.precumulateSum();
540 }
541
542 /**
543 * Returns an operation prefix that causes a method to
544 * operate only on the elements of the array between
545 * firstIndex (inclusive) and upperBound (exclusive).
546 * @param firstIndex the lower bound (inclusive)
547 * @param upperBound the upper bound (exclusive)
548 * @return operation prefix
549 */
550 public ParallelDoubleArrayWithBounds withBounds(int firstIndex,
551 int upperBound) {
552 return super.withBounds(firstIndex, upperBound);
553 }
554
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.
559 * @param selector the selector
560 * @return operation prefix
561 */
562 public ParallelDoubleArrayWithFilter withFilter(DoublePredicate selector) {
563 return super.withFilter(selector);
564 }
565
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.
570 * @param selector the selector
571 * @return operation prefix
572 */
573 public ParallelDoubleArrayWithFilter withFilter
574 (BinaryDoublePredicate selector,
575 ParallelDoubleArrayWithDoubleMapping other) {
576 return super.withFilter(selector, other);
577 }
578
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.
583 * @param selector the selector
584 * @return operation prefix
585 */
586 public ParallelDoubleArrayWithFilter withIndexedFilter
587 (IntAndDoublePredicate selector) {
588 return super.withIndexedFilter(selector);
589 }
590
591 /**
592 * Returns an operation prefix that causes a method to operate
593 * on mapped elements of the array using the given op.
594 * @param op the op
595 * @return operation prefix
596 */
597 public <U> ParallelDoubleArrayWithMapping<U> withMapping
598 (DoubleToObject<? extends U> op) {
599 return super.withMapping(op);
600 }
601
602 /**
603 * Returns an operation prefix that causes a method to operate
604 * on mapped elements of the array using the given op.
605 * @param op the op
606 * @return operation prefix
607 */
608 public ParallelDoubleArrayWithDoubleMapping withMapping(DoubleOp op) {
609 return super.withMapping(op);
610 }
611
612 /**
613 * Returns an operation prefix that causes a method to operate
614 * on mapped elements of the array using the given op.
615 * @param op the op
616 * @return operation prefix
617 */
618 public ParallelDoubleArrayWithLongMapping withMapping(DoubleToLong op) {
619 return super.withMapping(op);
620 }
621
622 /**
623 * Returns an operation prefix that causes a method to operate
624 * on binary mappings of this array and the other array.
625 * @param combiner the combiner
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)
630 */
631 public <V,W,X> ParallelDoubleArrayWithMapping<W> withMapping
632 (DoubleAndObjectToObject<? super V, ? extends W> combiner,
633 ParallelArrayWithMapping<X,V> other) {
634 return super.withMapping(combiner, other);
635 }
636
637 /**
638 * Returns an operation prefix that causes a method to operate
639 * on binary mappings of this array and the other array.
640 * @param combiner the 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)
645 */
646 public <V> ParallelDoubleArrayWithMapping<V> withMapping
647 (DoubleAndDoubleToObject<? extends V> combiner,
648 ParallelDoubleArrayWithDoubleMapping other) {
649 return super.withMapping(combiner, other);
650 }
651
652 /**
653 * Returns an operation prefix that causes a method to operate
654 * on binary mappings of this array and the other array.
655 * @param combiner the 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)
660 */
661 public <V> ParallelDoubleArrayWithMapping<V> withMapping
662 (DoubleAndLongToObject<? extends V> combiner,
663 ParallelLongArrayWithLongMapping other) {
664 return super.withMapping(combiner, other);
665 }
666
667 /**
668 * Returns an operation prefix that causes a method to operate
669 * on binary mappings of this array and the other array.
670 * @param combiner the 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)
675 */
676 public <V,W> ParallelDoubleArrayWithDoubleMapping withMapping
677 (DoubleAndObjectToDouble<? super V> combiner,
678 ParallelArrayWithMapping<W,V> other) {
679 return super.withMapping(combiner, other);
680 }
681
682 /**
683 * Returns an operation prefix that causes a method to operate
684 * on binary mappings of this array and the other array.
685 * @param combiner the 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)
690 */
691 public ParallelDoubleArrayWithDoubleMapping withMapping
692 (BinaryDoubleOp combiner,
693 ParallelDoubleArrayWithDoubleMapping other) {
694 return super.withMapping(combiner, other);
695 }
696
697 /**
698 * Returns an operation prefix that causes a method to operate
699 * on binary mappings of this array and the other array.
700 * @param combiner the 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)
705 */
706 public ParallelDoubleArrayWithDoubleMapping withMapping
707 (DoubleAndLongToDouble combiner,
708 ParallelLongArrayWithLongMapping other) {
709 return super.withMapping(combiner, other);
710 }
711
712 /**
713 * Returns an operation prefix that causes a method to operate
714 * on binary mappings of this array and the other array.
715 * @param combiner the 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)
720 */
721 public <V,W> ParallelDoubleArrayWithLongMapping withMapping
722 (DoubleAndObjectToLong<? super V> combiner,
723 ParallelArrayWithMapping<W,V> other) {
724 return super.withMapping(combiner, other);
725 }
726
727 /**
728 * Returns an operation prefix that causes a method to operate
729 * on binary mappings of this array and the other array.
730 * @param combiner the 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)
735 */
736 public ParallelDoubleArrayWithLongMapping withMapping
737 (DoubleAndDoubleToLong combiner,
738 ParallelDoubleArrayWithDoubleMapping other) {
739 return super.withMapping(combiner, other);
740 }
741
742 /**
743 * Returns an operation prefix that causes a method to operate
744 * on binary mappings of this array and the other array.
745 * @param combiner the 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)
750 */
751 public ParallelDoubleArrayWithLongMapping withMapping
752 (DoubleAndLongToLong combiner,
753 ParallelLongArrayWithLongMapping other) {
754 return super.withMapping(combiner, other);
755 }
756
757 /**
758 * Returns an operation prefix that causes a method to operate on
759 * mappings of this array using the given mapper that accepts as
760 * arguments an element's current index and value, and produces a
761 * new value.
762 * @param mapper the mapper
763 * @return operation prefix
764 */
765 public <U> ParallelDoubleArrayWithMapping<U> withIndexedMapping
766 (IntAndDoubleToObject<? extends U> mapper) {
767 return super.withIndexedMapping(mapper);
768 }
769
770 /**
771 * Returns an operation prefix that causes a method to operate on
772 * mappings of this array using the given mapper that accepts as
773 * arguments an element's current index and value, and produces a
774 * new value.
775 * @param mapper the mapper
776 * @return operation prefix
777 */
778 public ParallelDoubleArrayWithDoubleMapping withIndexedMapping
779 (IntAndDoubleToDouble mapper) {
780 return super.withIndexedMapping(mapper);
781 }
782
783 /**
784 * Returns an operation prefix that causes a method to operate on
785 * mappings of this array using the given mapper that accepts as
786 * arguments an element's current index and value, and produces a
787 * new value.
788 * @param mapper the mapper
789 * @return operation prefix
790 */
791 public ParallelDoubleArrayWithLongMapping withIndexedMapping
792 (IntAndDoubleToLong mapper) {
793 return super.withIndexedMapping(mapper);
794 }
795
796 /**
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 * {@code ListIterator} supporting add, remove, and set
801 * operations is available via {@link #asList}.
802 * @return an iterator stepping through each element
803 */
804 public Iterator<Double> iterator() {
805 return new ParallelDoubleArrayIterator(array, fence);
806 }
807
808 static final class ParallelDoubleArrayIterator
809 implements Iterator<Double> {
810 int cursor;
811 final double[] arr;
812 final int hi;
813 ParallelDoubleArrayIterator(double[] a, int limit) { arr = a; hi = limit; }
814 public boolean hasNext() { return cursor < hi; }
815 public Double next() {
816 if (cursor >= hi)
817 throw new NoSuchElementException();
818 return Double.valueOf(arr[cursor++]);
819 }
820 public void remove() {
821 throw new UnsupportedOperationException();
822 }
823 }
824
825 // List support
826
827 /**
828 * Returns a view of this ParallelDoubleArray as a List. This List
829 * has the same structural and performance characteristics as
830 * {@link ArrayList}, and may be used to modify, replace or extend
831 * the bounds of the array underlying this ParallelDoubleArray.
832 * The methods supported by this list view are <em>not</em> in
833 * general implemented as parallel operations. This list is also
834 * not itself thread-safe. In particular, performing list updates
835 * while other parallel operations are in progress has undefined
836 * (and surely undesired) effects.
837 * @return a list view
838 */
839 public List<Double> asList() {
840 AsList lv = listView;
841 if (lv == null)
842 listView = lv = new AsList();
843 return lv;
844 }
845
846 /**
847 * Returns the effective size of the underlying array. The
848 * effective size is the current limit, if used (see {@link
849 * #setLimit}), or the length of the array otherwise.
850 * @return the effective size of array
851 */
852 public int size() { return fence; }
853
854 /**
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.
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.
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 {@code asList().toString()}.
876 * @return a string representation
877 */
878 public String toString() {
879 return asList().toString();
880 }
881
882 /**
883 * Ensures that the underlying array can be accessed up to the
884 * given upper bound, reallocating and copying the underlying
885 * array to expand if necessary. Or, if the given limit is less
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
890 */
891 public final void setLimit(int newLimit) {
892 if (newLimit < 0)
893 throw new IllegalArgumentException();
894 int cap = array.length;
895 if (newLimit > cap)
896 resizeArray(newLimit);
897 fence = newLimit;
898 }
899
900 final void resizeArray(int newCap) {
901 int cap = array.length;
902 if (newCap > cap) {
903 double[] a = new double[newCap];
904 System.arraycopy(array, 0, a, 0, cap);
905 array = a;
906 }
907 }
908
909 final void insertElementAt(int index, double e) {
910 int hi = fence++;
911 if (hi >= array.length)
912 resizeArray((hi * 3)/2 + 1);
913 if (hi > index)
914 System.arraycopy(array, index, array, index+1, hi - index);
915 array[index] = e;
916 }
917
918 final void appendElement(double e) {
919 int hi = fence++;
920 if (hi >= array.length)
921 resizeArray((hi * 3)/2 + 1);
922 array[hi] = e;
923 }
924
925 /**
926 * Makes len slots available at index.
927 */
928 final void insertSlotsAt(int index, int len) {
929 if (len <= 0)
930 return;
931 int cap = array.length;
932 int newSize = fence + len;
933 if (cap < newSize) {
934 cap = (cap * 3)/2 + 1;
935 if (cap < newSize)
936 cap = newSize;
937 resizeArray(cap);
938 }
939 if (index < fence)
940 System.arraycopy(array, index, array, index + len, fence - index);
941 fence = newSize;
942 }
943
944 final void removeSlotAt(int index) {
945 System.arraycopy(array, index + 1, array, index, fence - index - 1);
946 --fence;
947 }
948
949 final void removeSlotsAt(int fromIndex, int toIndex) {
950 if (fromIndex < toIndex) {
951 int size = fence;
952 System.arraycopy(array, toIndex, array, fromIndex, size - toIndex);
953 int newSize = size - (toIndex - fromIndex);
954 fence = newSize;
955 }
956 }
957
958 final int seqIndexOf(double target) {
959 double[] arr = array;
960 int end = fence;
961 for (int i = 0; i < end; i++)
962 if (target == arr[i])
963 return i;
964 return -1;
965 }
966
967 final int seqLastIndexOf(double target) {
968 double[] arr = array;
969 for (int i = fence - 1; i >= 0; i--)
970 if (target == arr[i])
971 return i;
972 return -1;
973 }
974
975 final class ListIter implements ListIterator<Double> {
976 int cursor;
977 int lastRet;
978 double[] arr; // cache array and bound
979 int hi;
980 ListIter(int lo) {
981 this.cursor = lo;
982 this.lastRet = -1;
983 this.arr = ParallelDoubleArray.this.array;
984 this.hi = ParallelDoubleArray.this.fence;
985 }
986
987 public boolean hasNext() {
988 return cursor < hi;
989 }
990
991 public Double next() {
992 int i = cursor;
993 if (i < 0 || i >= hi)
994 throw new NoSuchElementException();
995 double next = arr[i];
996 lastRet = i;
997 cursor = i + 1;
998 return Double.valueOf(next);
999 }
1000
1001 public void remove() {
1002 int k = lastRet;
1003 if (k < 0)
1004 throw new IllegalStateException();
1005 ParallelDoubleArray.this.removeSlotAt(k);
1006 hi = ParallelDoubleArray.this.fence;
1007 if (lastRet < cursor)
1008 cursor--;
1009 lastRet = -1;
1010 }
1011
1012 public boolean hasPrevious() {
1013 return cursor > 0;
1014 }
1015
1016 public Double previous() {
1017 int i = cursor - 1;
1018 if (i < 0 || i >= hi)
1019 throw new NoSuchElementException();
1020 double previous = arr[i];
1021 lastRet = cursor = i;
1022 return Double.valueOf(previous);
1023 }
1024
1025 public int nextIndex() {
1026 return cursor;
1027 }
1028
1029 public int previousIndex() {
1030 return cursor - 1;
1031 }
1032
1033 public void set(Double e) {
1034 int i = lastRet;
1035 if (i < 0 || i >= hi)
1036 throw new NoSuchElementException();
1037 arr[i] = e.doubleValue();
1038 }
1039
1040 public void add(Double e) {
1041 int i = cursor;
1042 ParallelDoubleArray.this.insertElementAt(i, e.doubleValue());
1043 arr = ParallelDoubleArray.this.array;
1044 hi = ParallelDoubleArray.this.fence;
1045 lastRet = -1;
1046 cursor = i + 1;
1047 }
1048 }
1049
1050 final class AsList extends AbstractList<Double> implements RandomAccess {
1051 public Double get(int i) {
1052 if (i >= fence)
1053 throw new IndexOutOfBoundsException();
1054 return Double.valueOf(array[i]);
1055 }
1056
1057 public Double set(int i, Double x) {
1058 if (i >= fence)
1059 throw new IndexOutOfBoundsException();
1060 double[] arr = array;
1061 Double t = Double.valueOf(arr[i]);
1062 arr[i] = x.doubleValue();
1063 return t;
1064 }
1065
1066 public boolean isEmpty() {
1067 return fence == 0;
1068 }
1069
1070 public int size() {
1071 return fence;
1072 }
1073
1074 public Iterator<Double> iterator() {
1075 return new ListIter(0);
1076 }
1077
1078 public ListIterator<Double> listIterator() {
1079 return new ListIter(0);
1080 }
1081
1082 public ListIterator<Double> listIterator(int index) {
1083 if (index < 0 || index > fence)
1084 throw new IndexOutOfBoundsException();
1085 return new ListIter(index);
1086 }
1087
1088 public boolean add(Double e) {
1089 appendElement(e.doubleValue());
1090 return true;
1091 }
1092
1093 public void add(int index, Double e) {
1094 if (index < 0 || index > fence)
1095 throw new IndexOutOfBoundsException();
1096 insertElementAt(index, e.doubleValue());
1097 }
1098
1099 public boolean addAll(Collection<? extends Double> c) {
1100 int csize = c.size();
1101 if (csize == 0)
1102 return false;
1103 int hi = fence;
1104 setLimit(hi + csize);
1105 double[] arr = array;
1106 for (Double e : c)
1107 arr[hi++] = e.doubleValue();
1108 return true;
1109 }
1110
1111 public boolean addAll(int index, Collection<? extends Double> c) {
1112 if (index < 0 || index > fence)
1113 throw new IndexOutOfBoundsException();
1114 int csize = c.size();
1115 if (csize == 0)
1116 return false;
1117 insertSlotsAt(index, csize);
1118 double[] arr = array;
1119 for (Double e : c)
1120 arr[index++] = e.doubleValue();
1121 return true;
1122 }
1123
1124 public void clear() {
1125 fence = 0;
1126 }
1127
1128 public boolean remove(Object o) {
1129 if (!(o instanceof Double))
1130 return false;
1131 int idx = seqIndexOf(((Double)o).doubleValue());
1132 if (idx < 0)
1133 return false;
1134 removeSlotAt(idx);
1135 return true;
1136 }
1137
1138 public Double remove(int index) {
1139 Double oldValue = get(index);
1140 removeSlotAt(index);
1141 return oldValue;
1142 }
1143
1144 public void removeRange(int fromIndex, int toIndex) {
1145 removeSlotsAt(fromIndex, toIndex);
1146 }
1147
1148 public boolean contains(Object o) {
1149 if (!(o instanceof Double))
1150 return false;
1151 return seqIndexOf(((Double)o).doubleValue()) >= 0;
1152 }
1153
1154 public int indexOf(Object o) {
1155 if (!(o instanceof Double))
1156 return -1;
1157 return seqIndexOf(((Double)o).doubleValue());
1158 }
1159
1160 public int lastIndexOf(Object o) {
1161 if (!(o instanceof Double))
1162 return -1;
1163 return seqLastIndexOf(((Double)o).doubleValue());
1164 }
1165 }
1166
1167 }