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 |
import jsr166y.*; |
9 |
import static extra166y.Ops.*; |
10 |
import java.util.*; |
11 |
import java.util.concurrent.atomic.*; |
12 |
import java.lang.reflect.Array; |
13 |
|
14 |
/** |
15 |
* A prefix view of ParallelDoubleArray that causes operations to apply |
16 |
* only to elements for which a selector returns true. |
17 |
* Instances of this class may be constructed only via prefix |
18 |
* methods of ParallelDoubleArray or its other prefix classes. |
19 |
*/ |
20 |
public abstract class ParallelDoubleArrayWithFilter extends ParallelDoubleArrayWithDoubleMapping { |
21 |
ParallelDoubleArrayWithFilter |
22 |
(ForkJoinPool ex, int origin, int fence, double[] array) { |
23 |
super(ex, origin, fence, array); |
24 |
} |
25 |
|
26 |
/** |
27 |
* Replaces elements with the results of applying the given |
28 |
* op to their current values. |
29 |
* @param op the op |
30 |
* @return this (to simplify use in expressions) |
31 |
*/ |
32 |
public ParallelDoubleArrayWithFilter replaceWithMapping(DoubleOp op) { |
33 |
ex.invoke(new PAS.FJDTransform(this, origin, |
34 |
fence, null, op)); |
35 |
return this; |
36 |
} |
37 |
|
38 |
/** |
39 |
* Replaces elements with the results of applying the given |
40 |
* op to their indices |
41 |
* @param op the op |
42 |
* @return this (to simplify use in expressions) |
43 |
*/ |
44 |
public ParallelDoubleArrayWithFilter replaceWithMappedIndex(IntToDouble op) { |
45 |
ex.invoke(new PAS.FJDIndexMap(this, origin, fence, |
46 |
null, op)); |
47 |
return this; |
48 |
} |
49 |
|
50 |
/** |
51 |
* Replaces elements with the results of applying the given |
52 |
* mapping to each index and current element value |
53 |
* @param op the op |
54 |
* @return this (to simplify use in expressions) |
55 |
*/ |
56 |
public ParallelDoubleArrayWithFilter replaceWithMappedIndex(IntAndDoubleToDouble op) { |
57 |
ex.invoke(new PAS.FJDBinaryIndexMap |
58 |
(this, origin, fence, null, op)); |
59 |
return this; |
60 |
} |
61 |
|
62 |
/** |
63 |
* Replaces elements with results of applying the given |
64 |
* generator. |
65 |
* @param generator the generator |
66 |
* @return this (to simplify use in expressions) |
67 |
*/ |
68 |
public ParallelDoubleArrayWithFilter replaceWithGeneratedValue(DoubleGenerator generator) { |
69 |
ex.invoke(new PAS.FJDGenerate |
70 |
(this, origin, fence, null, generator)); |
71 |
return this; |
72 |
} |
73 |
|
74 |
/** |
75 |
* Replaces elements with the given value. |
76 |
* @param value the value |
77 |
* @return this (to simplify use in expressions) |
78 |
*/ |
79 |
public ParallelDoubleArrayWithFilter replaceWithValue(double value) { |
80 |
ex.invoke(new PAS.FJDFill(this, origin, fence, |
81 |
null, value)); |
82 |
return this; |
83 |
} |
84 |
|
85 |
/** |
86 |
* Replaces elements with results of applying |
87 |
* <tt>op(thisElement, otherElement)</tt> |
88 |
* @param other the other array |
89 |
* @param combiner the combiner |
90 |
* @return this (to simplify use in expressions) |
91 |
*/ |
92 |
public ParallelDoubleArrayWithFilter replaceWithMapping(BinaryDoubleOp combiner, |
93 |
ParallelDoubleArrayWithDoubleMapping other) { |
94 |
ex.invoke(new PAS.FJDPACombineInPlace |
95 |
(this, origin, fence, null, |
96 |
other, other.origin - origin, combiner)); |
97 |
return this; |
98 |
} |
99 |
|
100 |
/** |
101 |
* Replaces elements with results of applying |
102 |
* <tt>op(thisElement, otherElement)</tt> |
103 |
* @param other the other array |
104 |
* @param combiner the combiner |
105 |
* @return this (to simplify use in expressions) |
106 |
*/ |
107 |
public ParallelDoubleArrayWithFilter replaceWithMapping |
108 |
(BinaryDoubleOp combiner, |
109 |
double[] other) { |
110 |
ex.invoke(new PAS.FJDCombineInPlace |
111 |
(this, origin, fence, null, other, |
112 |
-origin, combiner)); |
113 |
return this; |
114 |
} |
115 |
|
116 |
/** |
117 |
* Returns a new ParallelDoubleArray containing only unique |
118 |
* elements (that is, without any duplicates). |
119 |
* @return the new ParallelDoubleArray |
120 |
*/ |
121 |
public ParallelDoubleArray allUniqueElements() { |
122 |
PAS.UniquifierTable tab = new PAS.UniquifierTable |
123 |
(fence - origin, this, false); |
124 |
PAS.FJDUniquifier f = new PAS.FJDUniquifier |
125 |
(this, origin, fence, null, tab); |
126 |
ex.invoke(f); |
127 |
double[] res = tab.uniqueDoubles(f.count); |
128 |
return new ParallelDoubleArray(ex, res); |
129 |
} |
130 |
|
131 |
|
132 |
/** |
133 |
* Returns an operation prefix that causes a method to operate |
134 |
* only on elements for which the current selector (if |
135 |
* present) and the given selector returns true |
136 |
* @param selector the selector |
137 |
* @return operation prefix |
138 |
*/ |
139 |
public abstract ParallelDoubleArrayWithFilter withFilter |
140 |
(DoublePredicate selector); |
141 |
|
142 |
/** |
143 |
* Returns an operation prefix that causes a method to operate |
144 |
* only on elements for which the current selector (if |
145 |
* present) and the given binary selector returns true |
146 |
* @param selector the selector |
147 |
* @return operation prefix |
148 |
*/ |
149 |
public ParallelDoubleArrayWithFilter withFilter |
150 |
(BinaryDoublePredicate selector, |
151 |
ParallelDoubleArrayWithDoubleMapping other) { |
152 |
return withIndexedFilter(AbstractParallelAnyArray.indexedSelector(selector, other, origin)); |
153 |
} |
154 |
|
155 |
/** |
156 |
* Returns an operation prefix that causes a method to operate |
157 |
* only on elements for which the current selector (if |
158 |
* present) and the given indexed selector returns true |
159 |
* @param selector the selector |
160 |
* @return operation prefix |
161 |
*/ |
162 |
public abstract ParallelDoubleArrayWithFilter withIndexedFilter |
163 |
(IntAndDoublePredicate selector); |
164 |
|
165 |
/** |
166 |
* Returns true if all elements at the same relative positions |
167 |
* of this and other array are equal. |
168 |
* @param other the other array |
169 |
* @return true if equal |
170 |
*/ |
171 |
public boolean hasAllEqualElements |
172 |
(ParallelDoubleArrayWithDoubleMapping other) { |
173 |
return withFilter(CommonOps.doubleInequalityPredicate(), |
174 |
other).anyIndex() < 0; |
175 |
} |
176 |
|
177 |
final void leafTransfer(int lo, int hi, double[] dest, int offset) { |
178 |
final double[] a = this.array; |
179 |
for (int i = lo; i < hi; ++i) |
180 |
dest[offset++] = (a[i]); |
181 |
} |
182 |
|
183 |
final void leafTransferByIndex(int[] indices, int loIdx, int hiIdx, |
184 |
double[] dest, int offset) { |
185 |
final double[] a = this.array; |
186 |
for (int i = loIdx; i < hiIdx; ++i) |
187 |
dest[offset++] = (a[indices[i]]); |
188 |
} |
189 |
|
190 |
final double dget(int i) { return this.array[i]; } |
191 |
|
192 |
} |