Debian Codegraph @

src/main/org/h2/util/IntArray.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.util;
7
8 import java.util.Arrays;
9
10 /**
11 * An array with integer element.
12 */
13 public class IntArray {
14
15 private int[] data;
16 private int size;
17 private int hash;
18
19 /**
20 * Create an int array with the default initial capacity.
21 */
22 public IntArray() {
23 this(10);
24 }
25
26 /**
27 * Create an int array with specified initial capacity.
28 *
29 * @param capacity the initial capacity
30 */
31 public IntArray(int capacity) {
32 data = new int[capacity];
33 }
34
35 /**
36 * Create an int array with the given values and size.
37 *
38 * @param data the int array
39 */
40 public IntArray(int[] data) {
41 this.data = data;
42 size = data.length;
43 }
44
45 /**
46 * Append a value.
47 *
48 * @param value the value to append
49 */
50 public void add(int value) {
51 if (size >= data.length) {
52 ensureCapacity(size + size);
53 }
54 data[size++] = value;
55 }
56
57 /**
58 * Get the value at the given index.
59 *
60 * @param index the index
61 * @return the value
62 */
63 public int get(int index) {
64 if (index >= size) {
65 throw new ArrayIndexOutOfBoundsException("i=" + index + " size=" + size);
66 }
67 return data[index];
68 }
69
70 /**
71 * Remove the value at the given index.
72 *
73 * @param index the index
74 */
75 public void remove(int index) {
76 if (index >= size) {
77 throw new ArrayIndexOutOfBoundsException("i=" + index + " size=" + size);
78 }
79 System.arraycopy(data, index + 1, data, index, size - index - 1);
80 size--;
81 }
82
83 /**
84 * Ensure the underlying array is large enough for the given number of
85 * entries.
86 *
87 * @param minCapacity the minimum capacity
88 */
89 public void ensureCapacity(int minCapacity) {
90 minCapacity = Math.max(4, minCapacity);
91 if (minCapacity >= data.length) {
92 data = Arrays.copyOf(data, minCapacity);
93 }
94 }
95
96 @Override
97 public boolean equals(Object obj) {
98 if (!(obj instanceof IntArray)) {
99 return false;
100 }
101 IntArray other = (IntArray) obj;
102 if (hashCode() != other.hashCode() || size != other.size) {
103 return false;
104 }
105 for (int i = 0; i < size; i++) {
106 if (data[i] != other.data[i]) {
107 return false;
108 }
109 }
110 return true;
111 }
112
113 @Override
114 public int hashCode() {
115 if (hash != 0) {
116 return hash;
117 }
118 int h = size + 1;
119 for (int i = 0; i < size; i++) {
120 h = h * 31 + data[i];
121 }
122 hash = h;
123 return h;
124 }
125
126 /**
127 * Get the size of the list.
128 *
129 * @return the size
130 */
131 public int size() {
132 return size;
133 }
134
135 /**
136 * Convert this list to an array. The target array must be big enough.
137 *
138 * @param array the target array
139 */
140 public void toArray(int[] array) {
141 System.arraycopy(data, 0, array, 0, size);
142 }
143
144 @Override
145 public String toString() {
146 StringBuilder builder = new StringBuilder("{");
147 for (int i = 0; i < size; i++) {
148 if (i > 0) {
149 builder.append(", ");
150 }
151 builder.append(data[i]);
152 }
153 return builder.append('}').toString();
154 }
155
156 /**
157 * Remove a number of elements.
158 *
159 * @param fromIndex the index of the first item to remove
160 * @param toIndex upper bound (exclusive)
161 */
162 public void removeRange(int fromIndex, int toIndex) {
163 if (fromIndex > toIndex || toIndex > size) {
164 throw new ArrayIndexOutOfBoundsException("from=" + fromIndex + " to=" + toIndex + " size=" + size);
165 }
166 System.arraycopy(data, toIndex, data, fromIndex, size - toIndex);
167 size -= toIndex - fromIndex;
168 }
169
170 }