ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/extra166y/ParallelArrayWithFilter.java
Revision: 1.7
Committed: Sun Jan 18 20:17:32 2015 UTC (9 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.6: +1 -0 lines
Log Message:
exactly one blank line before and after package statements

File Contents

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