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 |
* A prefix view of ParallelLongArray that causes operations to apply |
16 |
* only to elements for which a selector returns true. Instances of |
17 |
* this class may be constructed only via prefix methods of |
18 |
* ParallelLongArray or its other prefix classes. |
19 |
*/ |
20 |
public abstract class ParallelLongArrayWithFilter extends ParallelLongArrayWithLongMapping { |
21 |
ParallelLongArrayWithFilter |
22 |
(ForkJoinPool ex, int origin, int fence, long[] 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 ParallelLongArrayWithFilter replaceWithMapping(LongOp op) { |
33 |
ex.invoke(new PAS.FJLTransform |
34 |
(this, origin, 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 ParallelLongArrayWithFilter replaceWithMappedIndex(IntToLong op) { |
45 |
ex.invoke(new PAS.FJLIndexMap |
46 |
(this, origin, fence, 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 ParallelLongArrayWithFilter replaceWithMappedIndex(IntAndLongToLong op) { |
57 |
ex.invoke(new PAS.FJLBinaryIndexMap |
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 ParallelLongArrayWithFilter replaceWithGeneratedValue(LongGenerator generator) { |
69 |
ex.invoke(new PAS.FJLGenerate |
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 ParallelLongArrayWithFilter replaceWithValue(long value) { |
80 |
ex.invoke(new PAS.FJLFill |
81 |
(this, origin, fence, null, value)); |
82 |
return this; |
83 |
} |
84 |
|
85 |
/** |
86 |
* Replaces elements with results of applying |
87 |
* {@code op(thisElement, otherElement)}. |
88 |
* @param other the other array |
89 |
* @param combiner the combiner |
90 |
* @return this (to simplify use in expressions) |
91 |
*/ |
92 |
public ParallelLongArrayWithFilter replaceWithMapping(BinaryLongOp combiner, |
93 |
ParallelLongArrayWithLongMapping other) { |
94 |
ex.invoke(new PAS.FJLPACombineInPlace |
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 |
* {@code op(thisElement, otherElement)}. |
103 |
* @param other the other array |
104 |
* @param combiner the combiner |
105 |
* @return this (to simplify use in expressions) |
106 |
*/ |
107 |
public ParallelLongArrayWithFilter replaceWithMapping(BinaryLongOp combiner, |
108 |
long[] other) { |
109 |
ex.invoke(new PAS.FJLCombineInPlace |
110 |
(this, origin, fence, null, other, |
111 |
-origin, combiner)); |
112 |
return this; |
113 |
} |
114 |
|
115 |
/** |
116 |
* Returns a new ParallelLongArray containing only unique |
117 |
* elements (that is, without any duplicates). |
118 |
* @return the new ParallelLongArray |
119 |
*/ |
120 |
public ParallelLongArray allUniqueElements() { |
121 |
PAS.UniquifierTable tab = new PAS.UniquifierTable |
122 |
(fence - origin, this, false); |
123 |
PAS.FJLUniquifier f = new PAS.FJLUniquifier |
124 |
(this, origin, fence, null, tab); |
125 |
ex.invoke(f); |
126 |
long[] res = tab.uniqueLongs(f.count); |
127 |
return new ParallelLongArray(ex, res); |
128 |
} |
129 |
|
130 |
|
131 |
/** |
132 |
* Returns an operation prefix that causes a method to operate |
133 |
* only on elements for which the current selector (if |
134 |
* present) and the given selector returns true. |
135 |
* @param selector the selector |
136 |
* @return operation prefix |
137 |
*/ |
138 |
public abstract ParallelLongArrayWithFilter withFilter(LongPredicate selector); |
139 |
|
140 |
/** |
141 |
* Returns an operation prefix that causes a method to operate |
142 |
* only on elements for which the current selector (if |
143 |
* present) and the given binary selector returns true. |
144 |
* @param selector the selector |
145 |
* @return operation prefix |
146 |
*/ |
147 |
public ParallelLongArrayWithFilter withFilter |
148 |
(BinaryLongPredicate selector, |
149 |
ParallelLongArrayWithLongMapping other) { |
150 |
return withIndexedFilter(AbstractParallelAnyArray.indexedSelector(selector, other, origin)); |
151 |
} |
152 |
|
153 |
/** |
154 |
* Returns an operation prefix that causes a method to operate |
155 |
* only on elements for which the current selector (if |
156 |
* present) and the given indexed selector returns true. |
157 |
* @param selector the selector |
158 |
* @return operation prefix |
159 |
*/ |
160 |
public abstract ParallelLongArrayWithFilter withIndexedFilter |
161 |
(IntAndLongPredicate selector); |
162 |
|
163 |
/** |
164 |
* Returns true if all elements at the same relative positions |
165 |
* of this and other array are equal. |
166 |
* @param other the other array |
167 |
* @return true if equal |
168 |
*/ |
169 |
public boolean hasAllEqualElements(ParallelLongArrayWithLongMapping other) { |
170 |
return withFilter(CommonOps.longInequalityPredicate(), |
171 |
other).anyIndex() < 0; |
172 |
} |
173 |
|
174 |
final void leafTransfer(int lo, int hi, long[] dest, int offset) { |
175 |
final long[] a = this.array; |
176 |
for (int i = lo; i < hi; ++i) |
177 |
dest[offset++] = (a[i]); |
178 |
} |
179 |
|
180 |
final void leafTransferByIndex(int[] indices, int loIdx, int hiIdx, |
181 |
long[] dest, int offset) { |
182 |
final long[] a = this.array; |
183 |
for (int i = loIdx; i < hiIdx; ++i) |
184 |
dest[offset++] = (a[indices[i]]); |
185 |
} |
186 |
|
187 |
final long lget(int i) { return this.array[i]; } |
188 |
} |