1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.giraph.types.ops.collections.array;
19
20 import java.io.DataInput;
21 import java.io.DataOutput;
22 import java.io.IOException;
23 import java.util.Arrays;
24
25 import org.apache.giraph.function.Consumer;
26 import org.apache.giraph.function.Predicate;
27 import org.apache.giraph.function.primitive.BooleanConsumer;
28 import org.apache.giraph.function.primitive.BooleanPredicate;
29 import org.apache.giraph.types.ops.BooleanTypeOps;
30 import org.apache.giraph.types.ops.PrimitiveTypeOps;
31 import org.apache.giraph.types.ops.collections.ResettableIterator;
32 import org.apache.giraph.types.ops.collections.WBooleanCollection;
33 import org.apache.hadoop.io.BooleanWritable;
34 import org.apache.giraph.utils.Varint;
35
36 import it.unimi.dsi.fastutil.booleans.BooleanArrayList;
37 import it.unimi.dsi.fastutil.booleans.BooleanArrays;
38 import it.unimi.dsi.fastutil.booleans.BooleanCollection;
39 import it.unimi.dsi.fastutil.booleans.BooleanList;
40
41
42
43
44
45
46
47
48 public class WBooleanArrayList
49 extends BooleanArrayList
50 implements WArrayList<BooleanWritable>, WBooleanCollection {
51
52
53
54 public WBooleanArrayList() {
55 super();
56 }
57
58
59
60
61
62
63 public WBooleanArrayList(int capacity) {
64 super(capacity);
65 }
66
67
68
69
70
71
72
73
74 public WBooleanArrayList(BooleanCollection c) {
75 super(c);
76 }
77
78
79
80
81
82
83 public WBooleanArrayList(BooleanList l) {
84 super(l);
85 }
86
87 @Override
88 public PrimitiveTypeOps<BooleanWritable> getElementTypeOps() {
89 return BooleanTypeOps.INSTANCE;
90 }
91
92 @Override
93 public int capacity() {
94 return elements().length;
95 }
96
97 @Override
98 public void setCapacity(int n) {
99 if (n >= capacity()) {
100 ensureCapacity(n);
101 } else {
102 trim(n);
103 }
104 }
105
106 @Override
107 public void addW(BooleanWritable value) {
108 add(value.get());
109 }
110
111 @Override
112 public void getIntoW(int index, BooleanWritable to) {
113 to.set(getBoolean(index));
114 }
115
116 @Override
117 public void popIntoW(BooleanWritable to) {
118 to.set(popBoolean());
119 }
120
121 @Override
122 public void setW(int index, BooleanWritable value) {
123 set(index, value.get());
124 }
125
126 @Override
127 public void fillW(int from, int to, BooleanWritable value) {
128 if (to > size()) {
129 throw new ArrayIndexOutOfBoundsException(
130 "End index (" + to + ") is greater than array length (" +
131 size() + ")");
132 }
133 Arrays.fill(elements(), from, to, value.get());
134 }
135
136 @Override
137 public ResettableIterator<BooleanWritable> fastIteratorW() {
138 return fastIteratorW(getElementTypeOps().create());
139 }
140
141 @Override
142 public ResettableIterator<BooleanWritable> fastIteratorW(
143 BooleanWritable iterationValue) {
144 return WArrayListPrivateUtils.fastIterator(this, iterationValue);
145 }
146
147 @Override
148 public void fastForEachW(Consumer<BooleanWritable> f) {
149 WArrayListPrivateUtils.fastForEach(this, f, getElementTypeOps().create());
150 }
151
152 @Override
153 public boolean fastForEachWhileW(Predicate<BooleanWritable> f) {
154 return WArrayListPrivateUtils.fastForEachWhile(
155 this, f, getElementTypeOps().create());
156 }
157
158
159
160
161
162
163
164 public void forEachBoolean(BooleanConsumer f) {
165 for (int i = 0; i < size(); ++i) {
166 f.apply(getBoolean(i));
167 }
168 }
169
170
171
172
173
174
175
176
177
178 public boolean forEachWhileBoolean(BooleanPredicate f) {
179 for (int i = 0; i < size(); ++i) {
180 if (!f.apply(getBoolean(i))) {
181 return false;
182 }
183 }
184 return true;
185 }
186
187 @Override
188 public void sort() {
189 BooleanArrays.quickSort(elements(), 0, size());
190 }
191
192 @Override
193 public void writeElements(DataOutput out) throws IOException {
194 for (int i = 0; i < size; i++) {
195 out.writeBoolean(a[i]);
196 }
197 }
198
199 @Override
200 public void write(DataOutput out) throws IOException {
201 Varint.writeUnsignedVarInt(size, out);
202 writeElements(out);
203 }
204
205 @Override
206 public void readElements(DataInput in, int size) throws IOException {
207 this.size = size;
208 resizeArrayForRead(size);
209 for (int i = 0; i < size; i++) {
210 a[i] = in.readBoolean();
211 }
212 }
213
214
215
216
217
218 protected void resizeArrayForRead(int size) {
219 if (size != a.length) {
220 a = new boolean[size];
221 }
222 }
223
224 @Override
225 public void readFields(DataInput in) throws IOException {
226 readElements(in, Varint.readUnsignedVarInt(in));
227 }
228
229
230
231
232
233
234
235
236
237 public static void writeOrNull(WBooleanArrayList list, DataOutput out)
238 throws IOException {
239 if (list == null) {
240 Varint.writeUnsignedVarInt(0, out);
241 } else {
242 list.write(out);
243 }
244 }
245
246
247
248
249
250
251
252 public static WBooleanArrayList readNew(DataInput in) throws IOException {
253 int size = Varint.readSignedVarInt(in);
254 WBooleanArrayList list = new WBooleanArrayList(size);
255 list.readElements(in, size);
256 return list;
257 }
258
259
260
261
262
263
264
265 public static class WReusableBooleanArrayList
266 extends WBooleanArrayList {
267
268 public WReusableBooleanArrayList() {
269 super();
270 }
271
272
273
274
275
276 public WReusableBooleanArrayList(int capacity) {
277 super(capacity);
278 }
279
280 @Override
281 protected void resizeArrayForRead(int size) {
282 if (size > a.length) {
283 a = new boolean[size];
284 }
285 }
286
287
288
289
290
291
292
293
294
295 public static WReusableBooleanArrayList readIntoOrCreate(
296 WReusableBooleanArrayList list, DataInput in) throws IOException {
297 int size = Varint.readUnsignedVarInt(in);
298 if (list == null) {
299 list = new WReusableBooleanArrayList(size);
300 }
301 list.readElements(in, size);
302 return list;
303 }
304 }
305 }