1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.util; |
7 | |
8 | import org.h2.engine.SysProperties; |
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 (SysProperties.CHECK) { |
65 | if (index >= size) { |
66 | throw new ArrayIndexOutOfBoundsException("i=" + index + " size=" + size); |
67 | } |
68 | } |
69 | return data[index]; |
70 | } |
71 | |
72 | /** |
73 | * Remove the value at the given index. |
74 | * |
75 | * @param index the index |
76 | */ |
77 | public void remove(int index) { |
78 | if (SysProperties.CHECK) { |
79 | if (index >= size) { |
80 | throw new ArrayIndexOutOfBoundsException("i=" + index + " size=" + size); |
81 | } |
82 | } |
83 | System.arraycopy(data, index + 1, data, index, size - index - 1); |
84 | size--; |
85 | } |
86 | |
87 | /** |
88 | * Ensure the the underlying array is large enough for the given number of |
89 | * entries. |
90 | * |
91 | * @param minCapacity the minimum capacity |
92 | */ |
93 | public void ensureCapacity(int minCapacity) { |
94 | minCapacity = Math.max(4, minCapacity); |
95 | if (minCapacity >= data.length) { |
96 | int[] d = new int[minCapacity]; |
97 | System.arraycopy(data, 0, d, 0, data.length); |
98 | data = d; |
99 | } |
100 | } |
101 | |
102 | @Override |
103 | public boolean equals(Object obj) { |
104 | if (!(obj instanceof IntArray)) { |
105 | return false; |
106 | } |
107 | IntArray other = (IntArray) obj; |
108 | if (hashCode() != other.hashCode() || size != other.size) { |
109 | return false; |
110 | } |
111 | for (int i = 0; i < size; i++) { |
112 | if (data[i] != other.data[i]) { |
113 | return false; |
114 | } |
115 | } |
116 | return true; |
117 | } |
118 | |
119 | @Override |
120 | public int hashCode() { |
121 | if (hash != 0) { |
122 | return hash; |
123 | } |
124 | int h = size + 1; |
125 | for (int i = 0; i < size; i++) { |
126 | h = h * 31 + data[i]; |
127 | } |
128 | hash = h; |
129 | return h; |
130 | } |
131 | |
132 | /** |
133 | * Get the size of the list. |
134 | * |
135 | * @return the size |
136 | */ |
137 | public int size() { |
138 | return size; |
139 | } |
140 | |
141 | /** |
142 | * Convert this list to an array. The target array must be big enough. |
143 | * |
144 | * @param array the target array |
145 | */ |
146 | public void toArray(int[] array) { |
147 | System.arraycopy(data, 0, array, 0, size); |
148 | } |
149 | |
150 | @Override |
151 | public String toString() { |
152 | StatementBuilder buff = new StatementBuilder("{"); |
153 | for (int i = 0; i < size; i++) { |
154 | buff.appendExceptFirst(", "); |
155 | buff.append(data[i]); |
156 | } |
157 | return buff.append('}').toString(); |
158 | } |
159 | |
160 | /** |
161 | * Remove a number of elements. |
162 | * |
163 | * @param fromIndex the index of the first item to remove |
164 | * @param toIndex upper bound (exclusive) |
165 | */ |
166 | public void removeRange(int fromIndex, int toIndex) { |
167 | if (SysProperties.CHECK) { |
168 | if (fromIndex > toIndex || toIndex > size) { |
169 | throw new ArrayIndexOutOfBoundsException("from=" + fromIndex + |
170 | " to=" + toIndex + " size=" + size); |
171 | } |
172 | } |
173 | System.arraycopy(data, toIndex, data, fromIndex, size - toIndex); |
174 | size -= toIndex - fromIndex; |
175 | } |
176 | |
177 | } |