1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.giraph.types.ops.collections.array;
1920import java.io.DataInput;
21import java.io.DataOutput;
22import java.io.IOException;
23import java.util.Arrays;
2425import org.apache.giraph.function.Consumer;
26import org.apache.giraph.function.Predicate;
27import org.apache.giraph.function.primitive.BooleanConsumer;
28import org.apache.giraph.function.primitive.BooleanPredicate;
29import org.apache.giraph.types.ops.BooleanTypeOps;
30import org.apache.giraph.types.ops.PrimitiveTypeOps;
31import org.apache.giraph.types.ops.collections.ResettableIterator;
32import org.apache.giraph.types.ops.collections.WBooleanCollection;
33import org.apache.hadoop.io.BooleanWritable;
34import org.apache.giraph.utils.Varint;
3536import it.unimi.dsi.fastutil.booleans.BooleanArrayList;
37import it.unimi.dsi.fastutil.booleans.BooleanArrays;
38import it.unimi.dsi.fastutil.booleans.BooleanCollection;
39import it.unimi.dsi.fastutil.booleans.BooleanList;
4041// AUTO-GENERATED class via class:42// org.apache.giraph.generate.GeneratePrimitiveClasses4344/**45 * Writable extension of BooleanArrayList, as well as46 * BooleanWritable implementation of WArrayList.47 */48publicclassWBooleanArrayList49extends BooleanArrayList
50implements WArrayList<BooleanWritable>, WBooleanCollection {
51/**52 * Creates a new array list with {@link #DEFAULT_INITIAL_CAPACITY} capacity.53 */54publicWBooleanArrayList() {
55super();
56 }
5758/**59 * Creates a new array list with given capacity.60 *61 * @param capacity the initial capacity of the array list (may be 0).62 */63publicWBooleanArrayList(int capacity) {
64super(capacity);
65 }
6667/**68 * Creates a new array list and fills it with a given type-specific69 * collection.70 *71 * @param c a type-specific collection that will be used to fill the array72 * list.73 */74publicWBooleanArrayList(BooleanCollection c) {
75super(c);
76 }
7778/**79 * Creates a new array list and fills it with a given type-specific list.80 *81 * @param l a type-specific list that will be used to fill the array list.82 */83publicWBooleanArrayList(BooleanList l) {
84super(l);
85 }
8687 @Override
88public PrimitiveTypeOps<BooleanWritable> getElementTypeOps() {
89return BooleanTypeOps.INSTANCE;
90 }
9192 @Override
93publicint capacity() {
94return elements().length;
95 }
9697 @Override
98publicvoid setCapacity(int n) {
99if (n >= capacity()) {
100 ensureCapacity(n);
101 } else {
102 trim(n);
103 }
104 }
105106 @Override
107publicvoid addW(BooleanWritable value) {
108 add(value.get());
109 }
110111 @Override
112publicvoid getIntoW(int index, BooleanWritable to) {
113 to.set(getBoolean(index));
114 }
115116 @Override
117publicvoid popIntoW(BooleanWritable to) {
118 to.set(popBoolean());
119 }
120121 @Override
122publicvoid setW(int index, BooleanWritable value) {
123 set(index, value.get());
124 }
125126 @Override
127publicvoid fillW(int from, int to, BooleanWritable value) {
128if (to > size()) {
129thrownew ArrayIndexOutOfBoundsException(
130"End index (" + to + ") is greater than array length (" +
131 size() + ")");
132 }
133 Arrays.fill(elements(), from, to, value.get());
134 }
135136 @Override
137public ResettableIterator<BooleanWritable> fastIteratorW() {
138return fastIteratorW(getElementTypeOps().create());
139 }
140141 @Override
142public ResettableIterator<BooleanWritable> fastIteratorW(
143 BooleanWritable iterationValue) {
144return WArrayListPrivateUtils.fastIterator(this, iterationValue);
145 }
146147 @Override
148publicvoid fastForEachW(Consumer<BooleanWritable> f) {
149 WArrayListPrivateUtils.fastForEach(this, f, getElementTypeOps().create());
150 }
151152 @Override
153publicboolean fastForEachWhileW(Predicate<BooleanWritable> f) {
154return WArrayListPrivateUtils.fastForEachWhile(
155this, f, getElementTypeOps().create());
156 }
157158/**159 * Traverse all elements of the array list, calling given function on each160 * element, or until predicate returns false.161 *162 * @param f Function to call on each element.163 */164publicvoid forEachBoolean(BooleanConsumer f) {
165for (int i = 0; i < size(); ++i) {
166 f.apply(getBoolean(i));
167 }
168 }
169170/**171 * Traverse all elements of the array list, calling given function on each172 * element.173 *174 * @param f Function to call on each element.175 * @return true if the predicate returned true for all elements,176 * false if it returned false for some element.177 */178publicboolean forEachWhileBoolean(BooleanPredicate f) {
179for (int i = 0; i < size(); ++i) {
180if (!f.apply(getBoolean(i))) {
181return false;
182 }
183 }
184returntrue;
185 }
186187 @Override
188publicvoid sort() {
189 BooleanArrays.quickSort(elements(), 0, size());
190 }
191192 @Override
193publicvoid writeElements(DataOutput out) throws IOException {
194for (int i = 0; i < size; i++) {
195 out.writeBoolean(a[i]);
196 }
197 }
198199 @Override
200publicvoid write(DataOutput out) throws IOException {
201 Varint.writeUnsignedVarInt(size, out);
202 writeElements(out);
203 }
204205 @Override
206publicvoid readElements(DataInput in, int size) throws IOException {
207this.size = size;
208 resizeArrayForRead(size);
209for (int i = 0; i < size; i++) {
210 a[i] = in.readBoolean();
211 }
212 }
213214/**215 * Resize array for reading given number of elements.216 * @param size Number of elements that will be read.217 */218protectedvoid resizeArrayForRead(int size) {
219if (size != a.length) {
220 a = newboolean[size];
221 }
222 }
223224 @Override
225publicvoid readFields(DataInput in) throws IOException {
226 readElements(in, Varint.readUnsignedVarInt(in));
227 }
228229/**230 * Write a potentially null list to a DataOutput. Null list is written231 * equivalently as an empty list. Use WritableUtils.writeIfNotNullAndObject232 * if distinction is needed.233 *234 * @param list Array list to be written235 * @param out Data output236 */237publicstaticvoid writeOrNull(WBooleanArrayList list, DataOutput out)
238throws IOException {
239if (list == null) {
240 Varint.writeUnsignedVarInt(0, out);
241 } else {
242 list.write(out);
243 }
244 }
245246/**247 * Read array list from the DataInput stream into a newly created object.248 *249 * @param in Data input250 * @return New read array list object.251 */252publicstaticWBooleanArrayList readNew(DataInput in) throws IOException {
253int size = Varint.readSignedVarInt(in);
254WBooleanArrayList list = newWBooleanArrayList(size);
255 list.readElements(in, size);
256return list;
257 }
258259/**260 * Variant of WBooleanArrayList that doesn't reallocate smaller backing261 * array on consecutive readFields/readElements calls, and so is suitable for262 * reusable use.263 * (and backing array will only grow on readFields/readElements calls)264 */265publicstaticclassWReusableBooleanArrayList266extendsWBooleanArrayList {
267/** Constructor */268publicWReusableBooleanArrayList() {
269super();
270 }
271272/**273 * Constructor274 * @param capacity Capacity275 */276publicWReusableBooleanArrayList(int capacity) {
277super(capacity);
278 }
279280 @Override
281protectedvoid resizeArrayForRead(int size) {
282if (size > a.length) {
283 a = newboolean[size];
284 }
285 }
286287/**288 * Read array list from DataInput stream, into a given list if not null,289 * or creating a new list if given list is null.290 *291 * @param list Array list to be written292 * @param in Data input293 * @return Passed array list, or a new one if null is passed294 */295publicstaticWReusableBooleanArrayList readIntoOrCreate(
296WReusableBooleanArrayList list, DataInput in) throws IOException {
297int size = Varint.readUnsignedVarInt(in);
298if (list == null) {
299 list = newWReusableBooleanArrayList(size);
300 }
301 list.readElements(in, size);
302return list;
303 }
304 }
305 }