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 |
/** |
16 |
* A prefix view of ParallelLongArray that causes operations to apply |
17 |
* only to elements for which a selector returns true. Instances of |
18 |
* this class may be constructed only via prefix methods of |
19 |
* ParallelLongArray or its other prefix classes. |
20 |
*/ |
21 |
public abstract class ParallelLongArrayWithFilter extends ParallelLongArrayWithLongMapping { |
22 |
ParallelLongArrayWithFilter |
23 |
(ForkJoinPool ex, int origin, int fence, long[] array) { |
24 |
super(ex, origin, fence, array); |
25 |
} |
26 |
|
27 |
/** |
28 |
* Replaces elements with the results of applying the given |
29 |
* op to their current values. |
30 |
* @param op the op |
31 |
* @return this (to simplify use in expressions) |
32 |
*/ |
33 |
public ParallelLongArrayWithFilter replaceWithMapping(LongOp op) { |
34 |
ex.invoke(new PAS.FJLTransform |
35 |
(this, origin, fence, null, op)); |
36 |
return this; |
37 |
} |
38 |
|
39 |
/** |
40 |
* Replaces elements with the results of applying the given |
41 |
* op to their indices |
42 |
* @param op the op |
43 |
* @return this (to simplify use in expressions) |
44 |
*/ |
45 |
public ParallelLongArrayWithFilter replaceWithMappedIndex(IntToLong op) { |
46 |
ex.invoke(new PAS.FJLIndexMap |
47 |
(this, origin, fence, null, op)); |
48 |
return this; |
49 |
} |
50 |
|
51 |
/** |
52 |
* Replaces elements with the results of applying the given |
53 |
* mapping to each index and current element value |
54 |
* @param op the op |
55 |
* @return this (to simplify use in expressions) |
56 |
*/ |
57 |
public ParallelLongArrayWithFilter replaceWithMappedIndex(IntAndLongToLong op) { |
58 |
ex.invoke(new PAS.FJLBinaryIndexMap |
59 |
(this, origin, fence, null, op)); |
60 |
return this; |
61 |
} |
62 |
|
63 |
/** |
64 |
* Replaces elements with results of applying the given |
65 |
* generator. |
66 |
* @param generator the generator |
67 |
* @return this (to simplify use in expressions) |
68 |
*/ |
69 |
public ParallelLongArrayWithFilter replaceWithGeneratedValue(LongGenerator generator) { |
70 |
ex.invoke(new PAS.FJLGenerate |
71 |
(this, origin, fence, null, generator)); |
72 |
return this; |
73 |
} |
74 |
|
75 |
/** |
76 |
* Replaces elements with the given value. |
77 |
* @param value the value |
78 |
* @return this (to simplify use in expressions) |
79 |
*/ |
80 |
public ParallelLongArrayWithFilter replaceWithValue(long value) { |
81 |
ex.invoke(new PAS.FJLFill |
82 |
(this, origin, fence, null, value)); |
83 |
return this; |
84 |
} |
85 |
|
86 |
/** |
87 |
* Replaces elements with results of applying |
88 |
* <tt>op(thisElement, otherElement)</tt> |
89 |
* @param other the other array |
90 |
* @param combiner the combiner |
91 |
* @return this (to simplify use in expressions) |
92 |
*/ |
93 |
public ParallelLongArrayWithFilter replaceWithMapping(BinaryLongOp combiner, |
94 |
ParallelLongArrayWithLongMapping other) { |
95 |
ex.invoke(new PAS.FJLPACombineInPlace |
96 |
(this, origin, fence, null, |
97 |
other, other.origin - origin, combiner)); |
98 |
return this; |
99 |
} |
100 |
|
101 |
/** |
102 |
* Replaces elements with results of applying |
103 |
* <tt>op(thisElement, otherElement)</tt> |
104 |
* @param other the other array |
105 |
* @param combiner the combiner |
106 |
* @return this (to simplify use in expressions) |
107 |
*/ |
108 |
public ParallelLongArrayWithFilter replaceWithMapping(BinaryLongOp combiner, |
109 |
long[] other) { |
110 |
ex.invoke(new PAS.FJLCombineInPlace |
111 |
(this, origin, fence, null, other, |
112 |
-origin, combiner)); |
113 |
return this; |
114 |
} |
115 |
|
116 |
/** |
117 |
* Returns a new ParallelLongArray containing only unique |
118 |
* elements (that is, without any duplicates). |
119 |
* @return the new ParallelLongArray |
120 |
*/ |
121 |
public ParallelLongArray allUniqueElements() { |
122 |
PAS.UniquifierTable tab = new PAS.UniquifierTable |
123 |
(fence - origin, this, false); |
124 |
PAS.FJLUniquifier f = new PAS.FJLUniquifier |
125 |
(this, origin, fence, null, tab); |
126 |
ex.invoke(f); |
127 |
long[] res = tab.uniqueLongs(f.count); |
128 |
return new ParallelLongArray(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 ParallelLongArrayWithFilter withFilter(LongPredicate selector); |
140 |
|
141 |
/** |
142 |
* Returns an operation prefix that causes a method to operate |
143 |
* only on elements for which the current selector (if |
144 |
* present) and the given binary selector returns true |
145 |
* @param selector the selector |
146 |
* @return operation prefix |
147 |
*/ |
148 |
public ParallelLongArrayWithFilter withFilter |
149 |
(BinaryLongPredicate selector, |
150 |
ParallelLongArrayWithLongMapping other) { |
151 |
return withIndexedFilter(AbstractParallelAnyArray.indexedSelector(selector, other, origin)); |
152 |
} |
153 |
|
154 |
/** |
155 |
* Returns an operation prefix that causes a method to operate |
156 |
* only on elements for which the current selector (if |
157 |
* present) and the given indexed selector returns true |
158 |
* @param selector the selector |
159 |
* @return operation prefix |
160 |
*/ |
161 |
public abstract ParallelLongArrayWithFilter withIndexedFilter |
162 |
(IntAndLongPredicate selector); |
163 |
|
164 |
/** |
165 |
* Returns true if all elements at the same relative positions |
166 |
* of this and other array are equal. |
167 |
* @param other the other array |
168 |
* @return true if equal |
169 |
*/ |
170 |
public boolean hasAllEqualElements(ParallelLongArrayWithLongMapping other) { |
171 |
return withFilter(CommonOps.longInequalityPredicate(), |
172 |
other).anyIndex() < 0; |
173 |
} |
174 |
|
175 |
final void leafTransfer(int lo, int hi, long[] dest, int offset) { |
176 |
final long[] a = this.array; |
177 |
for (int i = lo; i < hi; ++i) |
178 |
dest[offset++] = (a[i]); |
179 |
} |
180 |
|
181 |
final void leafTransferByIndex(int[] indices, int loIdx, int hiIdx, |
182 |
long[] dest, int offset) { |
183 |
final long[] a = this.array; |
184 |
for (int i = loIdx; i < hiIdx; ++i) |
185 |
dest[offset++] = (a[indices[i]]); |
186 |
} |
187 |
|
188 |
final long lget(int i) { return this.array[i]; } |
189 |
} |