-
Notifications
You must be signed in to change notification settings - Fork 391
/
IntArrayList.java
682 lines (591 loc) · 16.6 KB
/
IntArrayList.java
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
/*
* Copyright 2014-2021 Real Logic Limited.
*
* 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
*
* https://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 org.agrona.collections;
import org.agrona.generation.DoNotSub;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;
/**
* A {@link List} implementation that stores int values with the ability to not have them boxed.
*/
public class IntArrayList extends AbstractList<Integer> implements List<Integer>, RandomAccess
{
/**
* The default value that will be used in place of null for an element.
*/
public static final int DEFAULT_NULL_VALUE = Integer.MIN_VALUE;
/**
* Initial capacity to which the array will be sized.
*/
@DoNotSub public static final int INITIAL_CAPACITY = 10;
private final int nullValue;
@DoNotSub private int size = 0;
private int[] elements;
/**
* Constructs a new list with the {@link #INITIAL_CAPACITY} using {@link #DEFAULT_NULL_VALUE}.
*/
public IntArrayList()
{
this(INITIAL_CAPACITY, DEFAULT_NULL_VALUE);
}
/**
* Construct a new list.
*
* @param initialCapacity for the backing array.
* @param nullValue to be used to represent a null element.
*/
public IntArrayList(
@DoNotSub final int initialCapacity,
final int nullValue)
{
this.nullValue = nullValue;
elements = new int[Math.max(initialCapacity, INITIAL_CAPACITY)];
}
/**
* Create a new list that wraps an existing arrays without copying it.
*
* @param initialElements to be wrapped.
* @param initialSize of the array to wrap.
* @param nullValue to be used to represent a null element.
*/
public IntArrayList(
final int[] initialElements,
@DoNotSub final int initialSize,
final int nullValue)
{
wrap(initialElements, initialSize);
this.nullValue = nullValue;
}
/**
* Wrap an existing array without copying it.
*
* @param initialElements to be wrapped.
* @param initialSize of the array to wrap.
* @throws IllegalArgumentException if the initialSize is less than 0 or greater than the length of the
* initial array.
*/
public void wrap(
final int[] initialElements,
final @DoNotSub int initialSize)
{
if (initialSize < 0 || initialSize > initialElements.length)
{
throw new IllegalArgumentException(
"illegal initial size " + initialSize + " for array length of " + initialElements.length);
}
elements = initialElements;
size = initialSize;
}
/**
* The value representing a null element.
*
* @return value representing a null element.
*/
public int nullValue()
{
return nullValue;
}
/**
* {@inheritDoc}
*/
public @DoNotSub int size()
{
return size;
}
/**
* The current capacity for the collection.
*
* @return the current capacity for the collection.
*/
@DoNotSub public int capacity()
{
return elements.length;
}
/**
* {@inheritDoc}
*/
public void clear()
{
size = 0;
}
/**
* Trim the underlying array to be the current size, or {@link #INITIAL_CAPACITY} if size is less.
*/
public void trimToSize()
{
if (elements.length != size && elements.length > INITIAL_CAPACITY)
{
elements = Arrays.copyOf(elements, Math.max(INITIAL_CAPACITY, size));
}
}
/**
* {@inheritDoc}
*/
public Integer get(
@DoNotSub final int index)
{
final int value = getInt(index);
return value == nullValue ? null : value;
}
/**
* Get the element at a given index without boxing.
*
* @param index to get.
* @return the unboxed element.
*/
public int getInt(
@DoNotSub final int index)
{
checkIndex(index);
return elements[index];
}
/**
* {@inheritDoc}
*/
public boolean add(final Integer element)
{
return addInt(null == element ? nullValue : element);
}
/**
* Add an element without boxing.
*
* @param element to be added.
* @return true
*/
public boolean addInt(final int element)
{
ensureCapacityPrivate(size + 1);
elements[size] = element;
size++;
return true;
}
/**
* {@inheritDoc}
*/
public void add(
@DoNotSub final int index,
final Integer element)
{
addInt(index, null == element ? nullValue : element);
}
/**
* Add an element without boxing at a given index.
*
* @param index at which the element should be added.
* @param element to be added.
*/
public void addInt(
@DoNotSub final int index,
final int element)
{
checkIndexForAdd(index);
@DoNotSub final int requiredSize = size + 1;
ensureCapacityPrivate(requiredSize);
if (index < size)
{
System.arraycopy(elements, index, elements, index + 1, size - index);
}
elements[index] = element;
size++;
}
/**
* {@inheritDoc}
*/
public Integer set(
@DoNotSub final int index,
final Integer element)
{
final int previous = setInt(index, null == element ? nullValue : element);
return nullValue == previous ? null : previous;
}
/**
* Set an element at a given index without boxing.
*
* @param index at which to set the element.
* @param element to be added.
* @return the previous element at the index.
*/
public int setInt(
@DoNotSub final int index,
final int element)
{
checkIndex(index);
final int previous = elements[index];
elements[index] = element;
return previous;
}
/**
* Does the list contain this element value.
*
* @param value of the element.
* @return true if present otherwise false.
*/
public boolean containsInt(final int value)
{
return -1 != indexOf(value);
}
/**
* Index of the first element with this value.
*
* @param value for the element.
* @return the index if found otherwise -1.
*/
public @DoNotSub int indexOf(
final int value)
{
for (@DoNotSub int i = 0; i < size; i++)
{
if (value == elements[i])
{
return i;
}
}
return -1;
}
/**
* Index of the last element with this value.
*
* @param value for the element.
* @return the index if found otherwise -1.
*/
public @DoNotSub int lastIndexOf(
final int value)
{
for (@DoNotSub int i = size - 1; i >= 0; i--)
{
if (value == elements[i])
{
return i;
}
}
return -1;
}
/**
* Remove at a given index.
*
* @param index of the element to be removed.
* @return the existing value at this index.
*/
public Integer remove(
@DoNotSub final int index)
{
checkIndex(index);
final int value = elements[index];
@DoNotSub final int moveCount = size - index - 1;
if (moveCount > 0)
{
System.arraycopy(elements, index + 1, elements, index, moveCount);
}
size--;
return value;
}
/**
* Removes element at index, but instead of copying all elements to the left,
* it replaces the item in the slot with the last item in the list. This avoids the copy
* costs at the expense of preserving list order. If index is the last element it is just removed.
*
* @param index of the element to be removed.
* @return the existing value at this index.
* @throws IndexOutOfBoundsException if index is out of bounds.
*/
public int fastUnorderedRemove(
@DoNotSub final int index)
{
checkIndex(index);
final int value = elements[index];
elements[index] = elements[--size];
return value;
}
/**
* Remove the first instance of a value if found in the list.
*
* @param value to be removed.
* @return true if successful otherwise false.
*/
public boolean removeInt(final int value)
{
@DoNotSub final int index = indexOf(value);
if (-1 != index)
{
remove(index);
return true;
}
return false;
}
/**
* Remove the first instance of a value if found in the list and replaces it with the last item
* in the list. This saves a copy down of all items at the expense of not preserving list order.
*
* @param value to be removed.
* @return true if successful otherwise false.
*/
public boolean fastUnorderedRemoveInt(final int value)
{
@DoNotSub final int index = indexOf(value);
if (-1 != index)
{
elements[index] = elements[--size];
return true;
}
return false;
}
/**
* Push an element onto the end of the array like a stack.
*
* @param element to be pushed onto the end of the array.
*/
public void pushInt(final int element)
{
ensureCapacityPrivate(size + 1);
elements[size] = element;
size++;
}
/**
* Pop a value off the end of the array as a stack operation.
*
* @return the value at the end of the array.
* @throws NoSuchElementException if the array is empty.
*/
public int popInt()
{
if (isEmpty())
{
throw new NoSuchElementException();
}
return elements[--size];
}
/**
* For each element in order provide the int value to a {@link IntConsumer}.
*
* @param action to be taken for each element.
*/
public void forEachOrderedInt(final IntConsumer action)
{
for (@DoNotSub int i = 0; i < size; i++)
{
action.accept(elements[i]);
}
}
/**
* Create a {@link IntStream} over the elements of underlying array.
*
* @return a {@link IntStream} over the elements of underlying array.
*/
public IntStream intStream()
{
return Arrays.stream(elements, 0, size);
}
/**
* Create a new array that is a copy of the elements.
*
* @return a copy of the elements.
*/
public int[] toIntArray()
{
return Arrays.copyOf(elements, size);
}
/**
* Create a new array that is a copy of the elements.
*
* @param dst destination array for the copy if it is the correct size.
* @return a copy of the elements.
*/
public int[] toIntArray(final int[] dst)
{
if (dst.length == size)
{
System.arraycopy(elements, 0, dst, 0, dst.length);
return dst;
}
else
{
return Arrays.copyOf(elements, size);
}
}
/**
* Ensure the backing array has a required capacity.
*
* @param requiredCapacity for the backing array.
*/
public void ensureCapacity(@DoNotSub final int requiredCapacity)
{
ensureCapacityPrivate(Math.max(requiredCapacity, INITIAL_CAPACITY));
}
/**
* Type-safe overload of the {@link #equals(Object)} method.
*
* @param that other list.
* @return {@code true} if lists are equal.
*/
public boolean equals(final IntArrayList that)
{
if (that == this)
{
return true;
}
boolean isEqual = false;
if (this.size == that.size)
{
isEqual = true;
for (@DoNotSub int i = 0; i < size; i++)
{
final int thisValue = this.elements[i];
final int thatValue = that.elements[i];
if (thisValue != thatValue)
{
if (thisValue != this.nullValue || thatValue != that.nullValue)
{
isEqual = false;
break;
}
}
}
}
return isEqual;
}
/**
* {@inheritDoc}
*/
public boolean equals(final Object other)
{
if (other == this)
{
return true;
}
boolean isEqual = false;
if (other instanceof IntArrayList)
{
return equals((IntArrayList)other);
}
else if (other instanceof List)
{
final List<?> that = (List<?>)other;
if (this.size == that.size())
{
isEqual = true;
@DoNotSub int i = 0;
for (final Object o : that)
{
if (o == null || o instanceof Integer)
{
final Integer thisValue = get(i++);
final Integer thatValue = (Integer)o;
if (Objects.equals(thisValue, thatValue))
{
continue;
}
}
isEqual = false;
break;
}
}
}
return isEqual;
}
/**
* {@inheritDoc}
*/
@DoNotSub public int hashCode()
{
@DoNotSub int hashCode = 1;
for (@DoNotSub int i = 0; i < size; i++)
{
final int value = elements[i];
hashCode = 31 * hashCode + (value == nullValue ? 0 : Integer.hashCode(value));
}
return hashCode;
}
/**
* {@inheritDoc}
*/
public void forEach(final Consumer<? super Integer> action)
{
for (@DoNotSub int i = 0; i < size; i++)
{
final int value = elements[i];
action.accept(value != nullValue ? value : null);
}
}
/**
* Iterate over the collection without boxing.
*
* @param action to be taken for each element.
*/
public void forEachInt(final IntConsumer action)
{
for (@DoNotSub int i = 0; i < size; i++)
{
action.accept(elements[i]);
}
}
/**
* {@inheritDoc}
*/
public String toString()
{
final StringBuilder sb = new StringBuilder();
sb.append('[');
for (@DoNotSub int i = 0; i < size; i++)
{
final int value = elements[i];
sb.append(value != nullValue ? value : null).append(", ");
}
if (sb.length() > 1)
{
sb.setLength(sb.length() - 2);
}
sb.append(']');
return sb.toString();
}
private void ensureCapacityPrivate(@DoNotSub final int requiredCapacity)
{
@DoNotSub final int currentCapacity = elements.length;
if (requiredCapacity > currentCapacity)
{
if (requiredCapacity > ArrayUtil.MAX_CAPACITY)
{
throw new IllegalStateException("max capacity: " + ArrayUtil.MAX_CAPACITY);
}
@DoNotSub int newCapacity = currentCapacity > INITIAL_CAPACITY ? currentCapacity : INITIAL_CAPACITY;
while (newCapacity < requiredCapacity)
{
newCapacity = newCapacity + (newCapacity >> 1);
if (newCapacity < 0 || newCapacity >= ArrayUtil.MAX_CAPACITY)
{
newCapacity = ArrayUtil.MAX_CAPACITY;
}
}
final int[] newElements = new int[newCapacity];
System.arraycopy(elements, 0, newElements, 0, currentCapacity);
elements = newElements;
}
}
private void checkIndex(@DoNotSub final int index)
{
if (index >= size || index < 0)
{
throw new IndexOutOfBoundsException("index=" + index + " size=" + size);
}
}
private void checkIndexForAdd(@DoNotSub final int index)
{
if (index > size || index < 0)
{
throw new IndexOutOfBoundsException("index=" + index + " size=" + size);
}
}
}