1 |
dl |
1.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 |
jsr166 |
1.2 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
package extra166y; |
8 |
jsr166 |
1.7 |
|
9 |
dl |
1.1 |
import jsr166y.*; |
10 |
|
|
import static extra166y.Ops.*; |
11 |
|
|
import java.util.*; |
12 |
|
|
import java.util.concurrent.atomic.*; |
13 |
|
|
|
14 |
|
|
/** |
15 |
|
|
* A prefix view of ParallelArray 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 ParallelArray or its other prefix classes. |
19 |
|
|
*/ |
20 |
|
|
public abstract class ParallelArrayWithFilter<T> |
21 |
jsr166 |
1.3 |
extends ParallelArrayWithMapping<T,T> { |
22 |
dl |
1.1 |
ParallelArrayWithFilter(ForkJoinPool ex, int origin, int fence, T[] 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 ParallelArrayWithFilter<T> replaceWithMapping |
33 |
|
|
(Op<? super T, ? extends T> op) { |
34 |
|
|
ex.invoke(new PAS.FJOTransform(this, origin, fence, |
35 |
|
|
null, op)); |
36 |
|
|
return this; |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
/** |
40 |
|
|
* Replaces elements with the results of applying the given |
41 |
jsr166 |
1.5 |
* op to their indices. |
42 |
dl |
1.1 |
* @param op the op |
43 |
|
|
* @return this (to simplify use in expressions) |
44 |
|
|
*/ |
45 |
|
|
public ParallelArrayWithFilter<T> replaceWithMappedIndex |
46 |
|
|
(IntToObject<? extends T> op) { |
47 |
|
|
ex.invoke(new PAS.FJOIndexMap(this, origin, fence, |
48 |
|
|
null, op)); |
49 |
|
|
return this; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
/** |
53 |
|
|
* Replaces elements with the results of applying the given |
54 |
jsr166 |
1.5 |
* mapping to each index and current element value. |
55 |
dl |
1.1 |
* @param op the op |
56 |
|
|
* @return this (to simplify use in expressions) |
57 |
|
|
*/ |
58 |
|
|
public ParallelArrayWithFilter<T> replaceWithMappedIndex |
59 |
|
|
(IntAndObjectToObject<? super T, ? extends T> op) { |
60 |
|
|
ex.invoke(new PAS.FJOBinaryIndexMap |
61 |
|
|
(this, origin, fence, null, op)); |
62 |
|
|
return this; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
/** |
66 |
jsr166 |
1.5 |
* Replaces elements with results of applying the given generator. |
67 |
dl |
1.1 |
* @param generator the generator |
68 |
|
|
* @return this (to simplify use in expressions) |
69 |
|
|
*/ |
70 |
|
|
public ParallelArrayWithFilter<T> replaceWithGeneratedValue |
71 |
|
|
(Generator<? extends T> generator) { |
72 |
|
|
ex.invoke(new PAS.FJOGenerate |
73 |
|
|
(this, origin, fence, null, generator)); |
74 |
|
|
return this; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
/** |
78 |
|
|
* Replaces elements with the given value. |
79 |
|
|
* @param value the value |
80 |
|
|
* @return this (to simplify use in expressions) |
81 |
|
|
*/ |
82 |
|
|
public ParallelArrayWithFilter<T> replaceWithValue(T value) { |
83 |
|
|
ex.invoke(new PAS.FJOFill(this, origin, fence, |
84 |
|
|
null, value)); |
85 |
|
|
return this; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
/** |
89 |
|
|
* Replaces elements with results of applying |
90 |
jsr166 |
1.6 |
* {@code op(thisElement, otherElement)}. |
91 |
dl |
1.1 |
* @param other the other array |
92 |
|
|
* @param combiner the combiner |
93 |
|
|
* @return this (to simplify use in expressions) |
94 |
|
|
*/ |
95 |
|
|
public <V,W> ParallelArrayWithFilter<T> replaceWithMapping |
96 |
|
|
(BinaryOp<? super T, ? super V, ? extends T> combiner, |
97 |
|
|
ParallelArrayWithMapping<W,V> other) { |
98 |
|
|
ex.invoke(new PAS.FJOPACombineInPlace |
99 |
|
|
(this, origin, fence, null, |
100 |
|
|
other, other.origin - origin, combiner)); |
101 |
|
|
return this; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
/** |
105 |
|
|
* Replaces elements with results of applying |
106 |
jsr166 |
1.6 |
* {@code op(thisElement, otherElement)}. |
107 |
dl |
1.1 |
* @param other the other array |
108 |
|
|
* @param combiner the combiner |
109 |
|
|
* @return this (to simplify use in expressions) |
110 |
|
|
*/ |
111 |
|
|
public ParallelArrayWithFilter<T> replaceWithMapping |
112 |
|
|
(BinaryOp<T,T,T> combiner, T[] other) { |
113 |
|
|
ex.invoke(new PAS.FJOCombineInPlace |
114 |
|
|
(this, origin, fence, null, other, |
115 |
|
|
-origin, combiner)); |
116 |
|
|
return this; |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
/** |
120 |
|
|
* Returns a new ParallelArray containing only non-null unique |
121 |
|
|
* elements (that is, without any duplicates). This method |
122 |
jsr166 |
1.6 |
* uses each element's {@code equals} method to test for |
123 |
dl |
1.1 |
* duplication. |
124 |
|
|
* @return the new ParallelArray |
125 |
|
|
*/ |
126 |
|
|
public ParallelArray<T> allUniqueElements() { |
127 |
|
|
PAS.UniquifierTable tab = new PAS.UniquifierTable |
128 |
|
|
(fence - origin, this, false); |
129 |
|
|
PAS.FJOUniquifier f = new PAS.FJOUniquifier |
130 |
|
|
(this, origin, fence, null, tab); |
131 |
|
|
ex.invoke(f); |
132 |
|
|
T[] res = (T[])(tab.uniqueObjects(f.count)); |
133 |
|
|
return new ParallelArray<T>(ex, res); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
/** |
137 |
|
|
* Returns a new ParallelArray containing only non-null unique |
138 |
|
|
* elements (that is, without any duplicates). This method |
139 |
|
|
* uses reference identity to test for duplication. |
140 |
|
|
* @return the new ParallelArray |
141 |
|
|
*/ |
142 |
|
|
public ParallelArray<T> allNonidenticalElements() { |
143 |
|
|
PAS.UniquifierTable tab = new PAS.UniquifierTable |
144 |
|
|
(fence - origin, this, true); |
145 |
|
|
PAS.FJOUniquifier f = new PAS.FJOUniquifier |
146 |
|
|
(this, origin, fence, null, tab); |
147 |
|
|
ex.invoke(f); |
148 |
|
|
T[] res = (T[])(tab.uniqueObjects(f.count)); |
149 |
|
|
return new ParallelArray<T>(ex, res); |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
/** |
153 |
|
|
* Returns an operation prefix that causes a method to operate |
154 |
|
|
* only on elements for which the current selector (if |
155 |
jsr166 |
1.5 |
* present) and the given selector returns true. |
156 |
dl |
1.1 |
* @param selector the selector |
157 |
|
|
* @return operation prefix |
158 |
|
|
*/ |
159 |
|
|
public abstract ParallelArrayWithFilter<T> withFilter |
160 |
|
|
(Predicate<? super T> selector); |
161 |
|
|
|
162 |
|
|
/** |
163 |
|
|
* Returns an operation prefix that causes a method to operate |
164 |
|
|
* only on elements for which the current selector (if |
165 |
jsr166 |
1.5 |
* present) and the given binary selector returns true. |
166 |
dl |
1.1 |
* @param selector the selector |
167 |
|
|
* @return operation prefix |
168 |
|
|
*/ |
169 |
|
|
public <V,W> ParallelArrayWithFilter<T> withFilter |
170 |
|
|
(BinaryPredicate<? super T, ? super V> selector, |
171 |
|
|
ParallelArrayWithMapping<W,V> other) { |
172 |
|
|
return withIndexedFilter(AbstractParallelAnyArray.indexedSelector(selector, other, origin)); |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
/** |
176 |
|
|
* Returns an operation prefix that causes a method to operate |
177 |
|
|
* only on elements for which the current selector (if |
178 |
jsr166 |
1.5 |
* present) and the given indexed selector returns true. |
179 |
dl |
1.1 |
* @param selector the selector |
180 |
|
|
* @return operation prefix |
181 |
|
|
*/ |
182 |
|
|
public abstract ParallelArrayWithFilter<T> withIndexedFilter |
183 |
|
|
(IntAndObjectPredicate<? super T> selector); |
184 |
|
|
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
/** |
188 |
|
|
* Returns true if all elements at the same relative positions |
189 |
|
|
* of this and other array are equal. |
190 |
|
|
* @param other the other array |
191 |
|
|
* @return true if equal |
192 |
|
|
*/ |
193 |
|
|
public <U,V> boolean hasAllEqualElements |
194 |
|
|
(ParallelArrayWithMapping<U,V> other) { |
195 |
|
|
return withFilter(CommonOps.inequalityPredicate(), |
196 |
|
|
other).anyIndex() < 0; |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
/** |
200 |
|
|
* Returns true if all elements at the same relative positions |
201 |
|
|
* of this and other array are identical. |
202 |
|
|
* @param other the other array |
203 |
|
|
* @return true if equal |
204 |
|
|
*/ |
205 |
|
|
public <U,V> boolean hasAllIdenticalElements |
206 |
|
|
(ParallelArrayWithMapping<U,V> other) { |
207 |
|
|
return withFilter(CommonOps.nonidentityPredicate(), |
208 |
|
|
other).anyIndex() < 0; |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
final void leafTransfer(int lo, int hi, Object[] dest, int offset) { |
212 |
|
|
final Object[] a = this.array; |
213 |
|
|
for (int i = lo; i < hi; ++i) |
214 |
|
|
dest[offset++] = (a[i]); |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
final void leafTransferByIndex(int[] indices, int loIdx, int hiIdx, |
218 |
|
|
Object[] dest, int offset) { |
219 |
|
|
final Object[] a = this.array; |
220 |
|
|
for (int i = loIdx; i < hiIdx; ++i) |
221 |
|
|
dest[offset++] = (a[indices[i]]); |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
final Object oget(int i) { return this.array[i]; } |
225 |
|
|
} |