-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathBooleanArray.java
More file actions
409 lines (361 loc) · 13.7 KB
/
BooleanArray.java
File metadata and controls
409 lines (361 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.utils;
import com.badlogic.gdx.math.MathUtils;
import java.util.BitSet;
/** A resizable, ordered or unordered boolean array. Avoids the boxing that occurs with ArrayList<Boolean>. It is less memory
* efficient than {@link BitSet}, except for very small sizes. It more CPU efficient than {@link BitSet}, except for very large
* sizes or if BitSet functionality such as and, or, xor, etc are needed. If unordered, this class avoids a memory copy when
* removing elements (the last element is moved to the removed element's position).
* @author Nathan Sweet */
public class BooleanArray {
public boolean[] items;
public int size;
public boolean ordered;
/** Creates an ordered array with a capacity of 16. */
public BooleanArray () {
this(true, 16);
}
/** Creates an ordered array with the specified capacity. */
public BooleanArray (int capacity) {
this(true, capacity);
}
/** @param ordered If false, methods that remove elements may change the order of other elements in the array, which avoids a
* memory copy.
* @param capacity Any elements added beyond this will cause the backing array to be grown. */
public BooleanArray (boolean ordered, int capacity) {
this.ordered = ordered;
items = new boolean[capacity];
}
/** Creates a new array containing the elements in the specific array. The new array will be ordered if the specific array is
* ordered. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be
* grown. */
public BooleanArray (BooleanArray array) {
this.ordered = array.ordered;
size = array.size;
items = new boolean[size];
System.arraycopy(array.items, 0, items, 0, size);
}
/** Creates a new ordered array containing the elements in the specified array. The capacity is set to the number of elements,
* so any subsequent elements added will cause the backing array to be grown. */
public BooleanArray (boolean[] array) {
this(true, array, 0, array.length);
}
/** Creates a new array containing the elements in the specified array. The capacity is set to the number of elements, so any
* subsequent elements added will cause the backing array to be grown.
* @param ordered If false, methods that remove elements may change the order of other elements in the array, which avoids a
* memory copy. */
public BooleanArray (boolean ordered, boolean[] array, int startIndex, int count) {
this(ordered, count);
size = count;
System.arraycopy(array, startIndex, items, 0, count);
}
public void add (boolean value) {
boolean[] items = this.items;
if (size == items.length) items = resize(Math.max(8, (int)(size * 1.75f)));
items[size++] = value;
}
public void add (boolean value1, boolean value2) {
boolean[] items = this.items;
if (size + 1 >= items.length) items = resize(Math.max(8, (int)(size * 1.75f)));
items[size] = value1;
items[size + 1] = value2;
size += 2;
}
public void add (boolean value1, boolean value2, boolean value3) {
boolean[] items = this.items;
if (size + 2 >= items.length) items = resize(Math.max(8, (int)(size * 1.75f)));
items[size] = value1;
items[size + 1] = value2;
items[size + 2] = value3;
size += 3;
}
public void add (boolean value1, boolean value2, boolean value3, boolean value4) {
boolean[] items = this.items;
if (size + 3 >= items.length) items = resize(Math.max(8, (int)(size * 1.8f))); // 1.75 isn't enough when size=5.
items[size] = value1;
items[size + 1] = value2;
items[size + 2] = value3;
items[size + 3] = value4;
size += 4;
}
public void addAll (BooleanArray array) {
addAll(array.items, 0, array.size);
}
public void addAll (BooleanArray array, int offset, int length) {
if (offset + length > array.size)
throw new IllegalArgumentException("offset + length must be <= size: " + offset + " + " + length + " <= " + array.size);
addAll(array.items, offset, length);
}
public void addAll (boolean... array) {
addAll(array, 0, array.length);
}
public void addAll (boolean[] array, int offset, int length) {
boolean[] items = this.items;
int sizeNeeded = size + length;
if (sizeNeeded > items.length) items = resize(Math.max(Math.max(8, sizeNeeded), (int)(size * 1.75f)));
System.arraycopy(array, offset, items, size, length);
size += length;
}
public boolean get (int index) {
if (index >= size) throw new IndexOutOfBoundsException("index can't be >= size: " + index + " >= " + size);
return items[index];
}
public void set (int index, boolean value) {
if (index >= size) throw new IndexOutOfBoundsException("index can't be >= size: " + index + " >= " + size);
items[index] = value;
}
public void insert (int index, boolean value) {
if (index > size) throw new IndexOutOfBoundsException("index can't be > size: " + index + " > " + size);
boolean[] items = this.items;
if (size == items.length) items = resize(Math.max(8, (int)(size * 1.75f)));
if (ordered)
System.arraycopy(items, index, items, index + 1, size - index);
else
items[size] = items[index];
size++;
items[index] = value;
}
/** Inserts the specified number of items at the specified index. The new items will have values equal to the values at those
* indices before the insertion. */
public void insertRange (int index, int count) {
if (index > size) throw new IndexOutOfBoundsException("index can't be > size: " + index + " > " + size);
int sizeNeeded = size + count;
if (sizeNeeded > items.length) items = resize(Math.max(Math.max(8, sizeNeeded), (int)(size * 1.75f)));
System.arraycopy(items, index, items, index + count, size - index);
size = sizeNeeded;
}
public void swap (int first, int second) {
if (first >= size) throw new IndexOutOfBoundsException("first can't be >= size: " + first + " >= " + size);
if (second >= size) throw new IndexOutOfBoundsException("second can't be >= size: " + second + " >= " + size);
boolean[] items = this.items;
boolean firstValue = items[first];
items[first] = items[second];
items[second] = firstValue;
}
/** Returns true if the specified value was replaced successfully with the replacement
* @param value the boolean to be replaced
* @param replacement the first value will be replaced by this replacement if found
* @return if value was found and replaced */
public boolean replaceFirst (boolean value, boolean replacement) {
boolean[] items = this.items;
for (int i = 0, n = size; i < n; i++) {
if (items[i] == value) {
items[i] = replacement;
return true;
}
}
return false;
}
/** Returns the number of replacements done.
* @param value the boolean to be replaced
* @param replacement all occurrences of value will be replaced by this replacement
* @return the number of replacements done */
public int replaceAll (boolean value, boolean replacement) {
boolean[] items = this.items;
int replacements = 0;
for (int i = 0, n = size; i < n; i++) {
if (items[i] == value) {
items[i] = replacement;
replacements++;
}
}
return replacements;
}
/** Removes and returns the item at the specified index. */
public boolean removeIndex (int index) {
if (index >= size) throw new IndexOutOfBoundsException("index can't be >= size: " + index + " >= " + size);
boolean[] items = this.items;
boolean value = items[index];
size--;
if (ordered)
System.arraycopy(items, index + 1, items, index, size - index);
else
items[index] = items[size];
return value;
}
/** Removes the items between the specified indices, inclusive. */
public void removeRange (int start, int end) {
int n = size;
if (end >= n) throw new IndexOutOfBoundsException("end can't be >= size: " + end + " >= " + size);
if (start > end) throw new IndexOutOfBoundsException("start can't be > end: " + start + " > " + end);
int count = end - start + 1, lastIndex = n - count;
if (ordered)
System.arraycopy(items, start + count, items, start, n - (start + count));
else {
int i = Math.max(lastIndex, end + 1);
System.arraycopy(items, i, items, start, n - i);
}
size = n - count;
}
/** Removes from this array the first instance of each element contained in the specified array.
* @return true if this array was modified. */
public boolean removeAll (BooleanArray array) {
int size = this.size;
int startSize = size;
boolean[] items = this.items;
for (int i = 0, n = array.size; i < n; i++) {
boolean item = array.get(i);
for (int ii = 0; ii < size; ii++) {
if (item == items[ii]) {
removeIndex(ii);
size--;
break;
}
}
}
return size != startSize;
}
/** Removes and returns the last item. */
public boolean pop () {
return items[--size];
}
/** Returns the last item. */
public boolean peek () {
return items[size - 1];
}
/** Returns the first item. */
public boolean first () {
if (size == 0) throw new IllegalStateException("Array is empty.");
return items[0];
}
/** Returns true if the array has one or more items. */
public boolean notEmpty () {
return size > 0;
}
/** Returns true if the array is empty. */
public boolean isEmpty () {
return size == 0;
}
public void clear () {
size = 0;
}
/** Reduces the size of the backing array to the size of the actual items. This is useful to release memory when many items
* have been removed, or if it is known that more items will not be added.
* @return {@link #items} */
public boolean[] shrink () {
if (items.length != size) resize(size);
return items;
}
/** Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many
* items to avoid multiple backing array resizes.
* @return {@link #items} */
public boolean[] ensureCapacity (int additionalCapacity) {
if (additionalCapacity < 0) throw new IllegalArgumentException("additionalCapacity must be >= 0: " + additionalCapacity);
int sizeNeeded = size + additionalCapacity;
if (sizeNeeded > items.length) resize(Math.max(Math.max(8, sizeNeeded), (int)(size * 1.75f)));
return items;
}
/** Sets the array size, leaving any values beyond the current size undefined.
* @return {@link #items} */
public boolean[] setSize (int newSize) {
if (newSize < 0) throw new IllegalArgumentException("newSize must be >= 0: " + newSize);
if (newSize > items.length) resize(Math.max(8, newSize));
size = newSize;
return items;
}
protected boolean[] resize (int newSize) {
boolean[] newItems = new boolean[newSize];
boolean[] items = this.items;
System.arraycopy(items, 0, newItems, 0, Math.min(size, newItems.length));
this.items = newItems;
return newItems;
}
public void reverse () {
boolean[] items = this.items;
for (int i = 0, lastIndex = size - 1, n = size / 2; i < n; i++) {
int ii = lastIndex - i;
boolean temp = items[i];
items[i] = items[ii];
items[ii] = temp;
}
}
public void shuffle () {
boolean[] items = this.items;
for (int i = size - 1; i >= 0; i--) {
int ii = MathUtils.random(i);
boolean temp = items[i];
items[i] = items[ii];
items[ii] = temp;
}
}
/** Reduces the size of the array to the specified size. If the array is already smaller than the specified size, no action is
* taken. */
public void truncate (int newSize) {
if (newSize < 0) throw new IllegalArgumentException("newSize must be >= 0: " + newSize);
if (size > newSize) size = newSize;
}
/** Returns a random item from the array, or false if the array is empty. */
public boolean random () {
if (size == 0) return false;
return items[MathUtils.random(0, size - 1)];
}
public boolean[] toArray () {
boolean[] array = new boolean[size];
System.arraycopy(items, 0, array, 0, size);
return array;
}
public int hashCode () {
if (!ordered) return super.hashCode();
boolean[] items = this.items;
int h = 1;
for (int i = 0, n = size; i < n; i++)
h = h * 31 + (items[i] ? 1231 : 1237);
return h;
}
/** Returns false if either array is unordered. */
public boolean equals (Object object) {
if (object == this) return true;
if (!ordered) return false;
if (!(object instanceof BooleanArray)) return false;
BooleanArray array = (BooleanArray)object;
if (!array.ordered) return false;
int n = size;
if (n != array.size) return false;
boolean[] items1 = this.items, items2 = array.items;
for (int i = 0; i < n; i++)
if (items1[i] != items2[i]) return false;
return true;
}
public String toString () {
if (size == 0) return "[]";
boolean[] items = this.items;
StringBuilder buffer = new StringBuilder(32);
buffer.append('[');
buffer.append(items[0]);
for (int i = 1; i < size; i++) {
buffer.append(", ");
buffer.append(items[i]);
}
buffer.append(']');
return buffer.toString();
}
public String toString (String separator) {
if (size == 0) return "";
boolean[] items = this.items;
StringBuilder buffer = new StringBuilder(32);
buffer.append(items[0]);
for (int i = 1; i < size; i++) {
buffer.append(separator);
buffer.append(items[i]);
}
return buffer.toString();
}
/** @see #BooleanArray(boolean[]) */
static public BooleanArray with (boolean... array) {
return new BooleanArray(array);
}
}