-
Notifications
You must be signed in to change notification settings - Fork 155
/
IntGrowableArray.java
59 lines (50 loc) · 1.44 KB
/
IntGrowableArray.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
/*
* HPPC
*
* Copyright (C) 2010-2022 Carrot Search s.c.
* All rights reserved.
*
* Refer to the full license file "LICENSE.txt":
* https://github.com/carrotsearch/hppc/blob/master/LICENSE.txt
*/
package com.carrotsearch.hppc;
import java.util.Arrays;
/**
* Basic growable int array helper for HPPC templates (so before {@code IntArrayList} is generated).
*/
public class IntGrowableArray implements Accountable {
public int[] buffer;
public int size;
public IntGrowableArray(int initialCapacity) {
buffer = new int[initialCapacity];
}
public void add(int e) {
ensureBufferSpace(1);
buffer[size++] = e;
}
public int[] toArray() {
return buffer.length == size ? buffer : Arrays.copyOf(buffer, size);
}
private void ensureBufferSpace(int expectedAdditions) {
if (size + expectedAdditions > buffer.length) {
int newSize =
BoundedProportionalArraySizingStrategy.DEFAULT_INSTANCE.grow(
buffer.length, size, expectedAdditions);
buffer = Arrays.copyOf(buffer, newSize);
}
}
@Override
public long ramBytesAllocated() {
// int: size
return RamUsageEstimator.NUM_BYTES_OBJECT_HEADER
+ Integer.BYTES
+ RamUsageEstimator.shallowSizeOfArray(buffer);
}
@Override
public long ramBytesUsed() {
// int: size
return RamUsageEstimator.NUM_BYTES_OBJECT_HEADER
+ Integer.BYTES
+ RamUsageEstimator.shallowUsedSizeOfArray(buffer, size);
}
}