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

File Contents

# User Rev Content
1 dl 1.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 jsr166 1.2 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     package extra166y;
8 jsr166 1.8
9 dl 1.1 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 jsr166 1.6 public ParallelLongArrayWithFilter replaceWithMapping(LongOp op) {
35 dl 1.1 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 jsr166 1.5 * op to their indices.
43 dl 1.1 * @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 jsr166 1.4 /**
53     * Replaces elements with the results of applying the given
54 jsr166 1.5 * mapping to each index and current element value.
55 jsr166 1.4 * @param op the op
56     * @return this (to simplify use in expressions)
57     */
58 jsr166 1.3 public ParallelLongArrayWithFilter replaceWithMappedIndex(IntAndLongToLong op) {
59 dl 1.1 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 jsr166 1.7 * {@code op(thisElement, otherElement)}.
90 dl 1.1 * @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 jsr166 1.7 * {@code op(thisElement, otherElement)}.
105 dl 1.1 * @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 jsr166 1.5 * present) and the given selector returns true.
137 dl 1.1 * @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 jsr166 1.5 * present) and the given binary selector returns true.
146 dl 1.1 * @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 jsr166 1.5 * present) and the given indexed selector returns true.
159 dl 1.1 * @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     }