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