-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathArray.java
More file actions
796 lines (709 loc) · 26.9 KB
/
Array.java
File metadata and controls
796 lines (709 loc) · 26.9 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/*******************************************************************************
* 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 java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.reflect.ArrayReflection;
/** A resizable, ordered or unordered array of objects. 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 Array<T> implements Iterable<T> {
/** Provides direct access to the underlying array. If the Array's generic type is not Object, this field may only be accessed
* if the {@link Array#Array(boolean, int, ArraySupplier)} constructor was used. */
public T[] items;
public int size;
public boolean ordered;
private transient ArrayIterable<T> iterable;
private transient Predicate.PredicateIterable<T> predicateIterable;
/** Creates an ordered array with a capacity of 16. */
public Array () {
this(true, 16);
}
/** Creates an ordered array with the specified capacity. */
public Array (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 Array (boolean ordered, int capacity) {
this(ordered, capacity, ArraySupplier.object());
}
/** Creates a new array with {@link #items} with the specified supplier.
* @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 Array (boolean ordered, int capacity, ArraySupplier<T[]> arraySupplier) {
this.ordered = ordered;
items = arraySupplier.get(capacity);
}
/** Creates an ordered array with {@link #items} with the specified supplier and a capacity of 16. */
public Array (ArraySupplier<T[]> arraySupplier) {
this(true, 16, arraySupplier);
}
/** Creates a new array with {@link #items} of the specified type.
* @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.
*
* @deprecated Use {@link Array#Array(boolean, int, ArraySupplier)} instead */
@Deprecated
public Array (boolean ordered, int capacity, Class arrayType) {
this(ordered, capacity, size -> (T[])ArrayReflection.newInstance(arrayType, size));
}
/** Creates an ordered array with {@link #items} of the specified type and a capacity of 16.
*
* @deprecated Use {@link Array#Array(ArraySupplier)} instead */
@Deprecated
public Array (Class arrayType) {
this(true, 16, arrayType);
}
/** Creates a new array containing the elements in the specified array. The new array will have the same type of backing array
* and will be ordered if the specified 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 Array (Array<? extends T> array) {
items = Arrays.copyOf(array.items, array.size);
ordered = array.ordered;
size = array.size;
}
/** Creates a new ordered array containing the elements in the specified array. The new array will have the same type of
* backing array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array
* to be grown. */
public Array (T[] array) {
this(true, array, 0, array.length);
}
/** Creates a new array containing the elements in the specified array. The new array will have the same type of backing 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 Array (boolean ordered, T[] array, int start, int count) {
items = Arrays.copyOfRange(array, start, start + count);
this.ordered = ordered;
size = count;
}
public void add (T value) {
T[] items = this.items;
if (size == items.length) items = resize(Math.max(8, (int)(size * 1.75f)));
items[size++] = value;
}
public void add (T value1, T value2) {
T[] 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 (T value1, T value2, T value3) {
T[] 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 (T value1, T value2, T value3, T value4) {
T[] 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 (Array<? extends T> array) {
addAll(array.items, 0, array.size);
}
public void addAll (Array<? extends T> array, int start, int count) {
if (start + count > array.size)
throw new IllegalArgumentException("start + count must be <= size: " + start + " + " + count + " <= " + array.size);
addAll(array.items, start, count);
}
public void addAll (T... array) {
addAll(array, 0, array.length);
}
public void addAll (T[] array, int start, int count) {
T[] items = this.items;
int sizeNeeded = size + count;
if (sizeNeeded > items.length) items = resize(Math.max(Math.max(8, sizeNeeded), (int)(size * 1.75f)));
System.arraycopy(array, start, items, size, count);
size = sizeNeeded;
}
public T get (int index) {
if (index >= size) throw new IndexOutOfBoundsException("index can't be >= size: " + index + " >= " + size);
return items[index];
}
public void set (int index, T value) {
if (index >= size) throw new IndexOutOfBoundsException("index can't be >= size: " + index + " >= " + size);
items[index] = value;
}
public void insert (int index, T value) {
if (index > size) throw new IndexOutOfBoundsException("index can't be > size: " + index + " > " + size);
T[] 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);
T[] items = this.items;
T firstValue = items[first];
items[first] = items[second];
items[second] = firstValue;
}
/** Returns true if the specified value was replaced successfully with the replacement
* @param value May be null.
* @param identity If true, == comparison will be used. If false, .equals() comparison will be used.
* @param replacement the first value will be replaced by this replacement if found
* @return if value was found and replaced */
public boolean replaceFirst (@Null T value, boolean identity, T replacement) {
T[] items = this.items;
if (identity || value == null) {
for (int i = 0, n = size; i < n; i++) {
if (items[i] == value) {
items[i] = replacement;
return true;
}
}
} else {
for (int i = 0, n = size; i < n; i++) {
if (value.equals(items[i])) {
items[i] = replacement;
return true;
}
}
}
return false;
}
/** Returns the number of replacements done.
* @param value May be null.
* @param identity If true, == comparison will be used. If false, .equals() comparison will be used.
* @param replacement all occurrences of value will be replaced by this replacement
* @return the number of replacements done */
public int replaceAll (@Null T value, boolean identity, @Null T replacement) {
T[] items = this.items;
int replacements = 0;
if (identity || value == null) {
for (int i = 0, n = size; i < n; i++) {
if (items[i] == value) {
items[i] = replacement;
replacements++;
}
}
} else {
for (int i = 0, n = size; i < n; i++) {
if (value.equals(items[i])) {
items[i] = replacement;
replacements++;
}
}
}
return replacements;
}
/** Returns true if this array contains the specified value.
* @param value May be null.
* @param identity If true, == comparison will be used. If false, .equals() comparison will be used. */
public boolean contains (@Null T value, boolean identity) {
T[] items = this.items;
int i = size - 1;
if (identity || value == null) {
while (i >= 0)
if (items[i--] == value) return true;
} else {
while (i >= 0)
if (value.equals(items[i--])) return true;
}
return false;
}
/** Returns true if this array contains all the specified values.
* @param values May contains nulls.
* @param identity If true, == comparison will be used. If false, .equals() comparison will be used. */
public boolean containsAll (Array<? extends T> values, boolean identity) {
T[] items = values.items;
for (int i = 0, n = values.size; i < n; i++)
if (!contains(items[i], identity)) return false;
return true;
}
/** Returns true if this array contains any the specified values.
* @param values May contains nulls.
* @param identity If true, == comparison will be used. If false, .equals() comparison will be used. */
public boolean containsAny (Array<? extends T> values, boolean identity) {
T[] items = values.items;
for (int i = 0, n = values.size; i < n; i++)
if (contains(items[i], identity)) return true;
return false;
}
/** Returns the index of first occurrence of value in the array, or -1 if no such value exists.
* @param value May be null.
* @param identity If true, == comparison will be used. If false, .equals() comparison will be used.
* @return An index of first occurrence of value in array or -1 if no such value exists */
public int indexOf (@Null T value, boolean identity) {
T[] items = this.items;
if (identity || value == null) {
for (int i = 0, n = size; i < n; i++)
if (items[i] == value) return i;
} else {
for (int i = 0, n = size; i < n; i++)
if (value.equals(items[i])) return i;
}
return -1;
}
/** Returns an index of last occurrence of value in array or -1 if no such value exists. Search is started from the end of an
* array.
* @param value May be null.
* @param identity If true, == comparison will be used. If false, .equals() comparison will be used.
* @return An index of last occurrence of value in array or -1 if no such value exists */
public int lastIndexOf (@Null T value, boolean identity) {
T[] items = this.items;
if (identity || value == null) {
for (int i = size - 1; i >= 0; i--)
if (items[i] == value) return i;
} else {
for (int i = size - 1; i >= 0; i--)
if (value.equals(items[i])) return i;
}
return -1;
}
/** Removes the first instance of the specified value in the array.
* @param value May be null.
* @param identity If true, == comparison will be used. If false, .equals() comparison will be used.
* @return true if value was found and removed, false otherwise */
public boolean removeValue (@Null T value, boolean identity) {
T[] items = this.items;
if (identity || value == null) {
for (int i = 0, n = size; i < n; i++) {
if (items[i] == value) {
removeIndex(i);
return true;
}
}
} else {
for (int i = 0, n = size; i < n; i++) {
if (value.equals(items[i])) {
removeIndex(i);
return true;
}
}
}
return false;
}
/** Removes and returns the item at the specified index. */
public T removeIndex (int index) {
if (index >= size) throw new IndexOutOfBoundsException("index can't be >= size: " + index + " >= " + size);
T[] items = this.items;
T value = items[index];
size--;
if (ordered)
System.arraycopy(items, index + 1, items, index, size - index);
else
items[index] = items[size];
items[size] = null;
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);
T[] items = this.items;
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);
}
for (int i = lastIndex; i < n; i++)
items[i] = null;
size = n - count;
}
/** Removes from this array the first instance of each element contained in the specified array.
* @param identity True to use ==, false to use .equals().
* @return true if this array was modified. */
public boolean removeAll (Array<? extends T> array, boolean identity) {
int size = this.size;
int startSize = size;
T[] items = this.items;
if (identity) {
for (int i = 0, n = array.size; i < n; i++) {
T item = array.get(i);
for (int ii = 0; ii < size; ii++) {
if (item == items[ii]) {
removeIndex(ii);
size--;
break;
}
}
}
} else {
for (int i = 0, n = array.size; i < n; i++) {
T item = array.get(i);
for (int ii = 0; ii < size; ii++) {
if (item.equals(items[ii])) {
removeIndex(ii);
size--;
break;
}
}
}
}
return size != startSize;
}
/** Removes and returns the last item. */
public T pop () {
if (size == 0) throw new IllegalStateException("Array is empty.");
--size;
T item = items[size];
items[size] = null;
return item;
}
/** Returns the last item. */
public T peek () {
if (size == 0) throw new IllegalStateException("Array is empty.");
return items[size - 1];
}
/** Returns the first item. */
public T 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 () {
Arrays.fill(items, 0, size, null);
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 T[] 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 T[] 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 null.
* @return {@link #items} */
public T[] setSize (int newSize) {
truncate(newSize);
if (newSize > items.length) resize(Math.max(8, newSize));
size = newSize;
return items;
}
/** Creates a new backing array with the specified size containing the current items. */
protected T[] resize (int newSize) {
items = Arrays.copyOf(items, newSize);
return items;
}
/** Sorts this array. The array elements must implement {@link Comparable}. This method is not thread safe (uses
* {@link Sort#instance()}). */
public void sort () {
Sort.instance().sort(items, 0, size);
}
/** Sorts the array. This method is not thread safe (uses {@link Sort#instance()}). */
public void sort (Comparator<? super T> comparator) {
Sort.instance().sort(items, comparator, 0, size);
}
/** Selects the nth-lowest element from the Array according to Comparator ranking. This might partially sort the Array. The
* array must have a size greater than 0, or a {@link com.badlogic.gdx.utils.GdxRuntimeException} will be thrown.
* @see Select
* @param comparator used for comparison
* @param kthLowest rank of desired object according to comparison, n is based on ordinal numbers, not array indices. for min
* value use 1, for max value use size of array, using 0 results in runtime exception.
* @return the value of the Nth lowest ranked object. */
public T selectRanked (Comparator<T> comparator, int kthLowest) {
if (kthLowest < 1) {
throw new GdxRuntimeException("nth_lowest must be greater than 0, 1 = first, 2 = second...");
}
return Select.instance().select(items, comparator, kthLowest, size);
}
/** @see Array#selectRanked(java.util.Comparator, int)
* @param comparator used for comparison
* @param kthLowest rank of desired object according to comparison, n is based on ordinal numbers, not array indices. for min
* value use 1, for max value use size of array, using 0 results in runtime exception.
* @return the index of the Nth lowest ranked object. */
public int selectRankedIndex (Comparator<T> comparator, int kthLowest) {
if (kthLowest < 1) {
throw new GdxRuntimeException("nth_lowest must be greater than 0, 1 = first, 2 = second...");
}
return Select.instance().selectIndex(items, comparator, kthLowest, size);
}
public void reverse () {
T[] items = this.items;
for (int i = 0, lastIndex = size - 1, n = size / 2; i < n; i++) {
int ii = lastIndex - i;
T temp = items[i];
items[i] = items[ii];
items[ii] = temp;
}
}
public void shuffle () {
T[] items = this.items;
for (int i = size - 1; i >= 0; i--) {
int ii = MathUtils.random(i);
T temp = items[i];
items[i] = items[ii];
items[ii] = temp;
}
}
/** Returns an iterator for the items in the array. Remove is supported.
* <p>
* If {@link Collections#allocateIterators} is false, the same iterator instance is returned each time this method is called.
* Use the {@link ArrayIterator} constructor for nested or multithreaded iteration. */
public ArrayIterator<T> iterator () {
if (Collections.allocateIterators) return new ArrayIterator<T>(this, true);
if (iterable == null) iterable = new ArrayIterable<T>(this);
return iterable.iterator();
}
/** Returns an iterable for the selected items in the array. Remove is supported, but not between hasNext() and next().
* <p>
* If {@link Collections#allocateIterators} is false, the same iterable instance is returned each time this method is called.
* Use the {@link Predicate.PredicateIterable} constructor for nested or multithreaded iteration. */
public Iterable<T> select (Predicate<T> predicate) {
if (Collections.allocateIterators) return new Predicate.PredicateIterable<T>(this, predicate);
if (predicateIterable == null)
predicateIterable = new Predicate.PredicateIterable<T>(this, predicate);
else
predicateIterable.set(this, predicate);
return predicateIterable;
}
/** 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) return;
for (int i = newSize; i < size; i++)
items[i] = null;
size = newSize;
}
/** Returns a random item from the array, or null if the array is empty. */
public @Null T random () {
if (size == 0) return null;
return items[MathUtils.random(0, size - 1)];
}
/** Returns the items as an array. Note the array is typed, so the {@link #Array(ArraySupplier)} constructor must have been
* used. Otherwise use {@link #toArray(ArraySupplier)} to specify the array type. */
public T[] toArray () {
return Arrays.copyOf(items, size);
}
public T[] toArray (ArraySupplier<T[]> arraySupplier) {
T[] result = arraySupplier.get(size);
System.arraycopy(items, 0, result, 0, size);
return result;
}
/** @deprecated Use {@link Array#toArray(ArraySupplier)} instead */
@Deprecated
public <V> V[] toArray (Class<V> type) {
V[] result = (V[])ArrayReflection.newInstance(type, size);
System.arraycopy(items, 0, result, 0, size);
return result;
}
public int hashCode () {
if (!ordered) return super.hashCode();
Object[] items = this.items;
int h = 1;
for (int i = 0, n = size; i < n; i++) {
h *= 31;
Object item = items[i];
if (item != null) h += item.hashCode();
}
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 Array)) return false;
Array array = (Array)object;
if (!array.ordered) return false;
int n = size;
if (n != array.size) return false;
Object[] items1 = this.items, items2 = array.items;
for (int i = 0; i < n; i++) {
Object o1 = items1[i], o2 = items2[i];
if (!(o1 == null ? o2 == null : o1.equals(o2))) return false;
}
return true;
}
/** Uses == for comparison of each item. Returns false if either array is unordered. */
public boolean equalsIdentity (Object object) {
if (object == this) return true;
if (!ordered) return false;
if (!(object instanceof Array)) return false;
Array array = (Array)object;
if (!array.ordered) return false;
int n = size;
if (n != array.size) return false;
Object[] 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 "[]";
T[] 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 "";
T[] 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 #Array(ArraySupplier) */
static public <T> Array<T> of (ArraySupplier<T[]> arraySupplier) {
return new Array<>(arraySupplier);
}
/** @see #Array(boolean, int, ArraySupplier) */
static public <T> Array<T> of (boolean ordered, int capacity, ArraySupplier<T[]> arraySupplier) {
return new Array<>(ordered, capacity, arraySupplier);
}
/** @see #Array(Class)
*
* @deprecated Use {@link Array#of(ArraySupplier)} */
@Deprecated
static public <T> Array<T> of (Class<T> arrayType) {
return new Array(arrayType);
}
/** @see #Array(boolean, int, Class)
*
* @deprecated Use {@link Array#of(boolean, int, ArraySupplier)} */
@Deprecated
static public <T> Array<T> of (boolean ordered, int capacity, Class<T> arrayType) {
return new Array(ordered, capacity, arrayType);
}
/** @see #Array(Object[]) */
static public <T> Array<T> with (T... array) {
return new Array(array);
}
static public class ArrayIterator<T> implements Iterator<T>, Iterable<T> {
private final Array<T> array;
private final boolean allowRemove;
int index;
boolean valid = true;
// ArrayIterable<T> iterable;
public ArrayIterator (Array<T> array) {
this(array, true);
}
public ArrayIterator (Array<T> array, boolean allowRemove) {
this.array = array;
this.allowRemove = allowRemove;
}
public boolean hasNext () {
if (!valid) {
// System.out.println(iterable.lastAcquire);
throw new GdxRuntimeException("#iterator() cannot be used nested.");
}
return index < array.size;
}
public T next () {
if (index >= array.size) throw new NoSuchElementException(String.valueOf(index));
if (!valid) {
// System.out.println(iterable.lastAcquire);
throw new GdxRuntimeException("#iterator() cannot be used nested.");
}
return array.items[index++];
}
public void remove () {
if (!allowRemove) throw new GdxRuntimeException("Remove not allowed.");
index--;
array.removeIndex(index);
}
public void reset () {
index = 0;
}
public ArrayIterator<T> iterator () {
return this;
}
}
static public class ArrayIterable<T> implements Iterable<T> {
private final Array<T> array;
private final boolean allowRemove;
private transient ArrayIterator<T> iterator1, iterator2;
// java.io.StringWriter lastAcquire = new java.io.StringWriter();
public ArrayIterable (Array<T> array) {
this(array, true);
}
public ArrayIterable (Array<T> array, boolean allowRemove) {
this.array = array;
this.allowRemove = allowRemove;
}
/** @see Collections#allocateIterators */
public ArrayIterator<T> iterator () {
if (Collections.allocateIterators) return new ArrayIterator<T>(array, allowRemove);
// lastAcquire.getBuffer().setLength(0);
// new Throwable().printStackTrace(new java.io.PrintWriter(lastAcquire));
if (iterator1 == null) {
iterator1 = new ArrayIterator<T>(array, allowRemove);
iterator2 = new ArrayIterator<T>(array, allowRemove);
// iterator1.iterable = this;
// iterator2.iterable = this;
}
if (!iterator1.valid) {
iterator1.index = 0;
iterator1.valid = true;
iterator2.valid = false;
return iterator1;
}
iterator2.index = 0;
iterator2.valid = true;
iterator1.valid = false;
return iterator2;
}
}
}