ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/extra166y/ParallelArrayWithFilter.java
Revision: 1.4
Committed: Wed Jul 4 20:13:53 2012 UTC (11 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.3: +0 -1 lines
Log Message:
trailing newlines

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 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 * 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 extends ParallelArrayWithMapping<T,T> {
22 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 * op to their indices
42 * @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 * mapping to each index and current element value
55 * @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 * Replaces elements with results of applying the given
67 * 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 * <tt>op(thisElement, otherElement)</tt>
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 * <tt>op(thisElement, otherElement)</tt>
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 <tt>equals</tt> 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 }