1 | // Copyright (C) 2002-2017 Sebastiano Vigna |
2 | |
3 | public class IntArrayList extends AbstractIntList implements RandomAccess, Cloneable, java.io.Serializable { |
4 | public static final int DEFAULT_INITIAL_CAPACITY = 16; |
5 | protected transient int a[]; |
6 | protected int size; |
7 | private static final boolean ASSERTS = false; |
8 | |
9 | protected *(final int a[], @SuppressWarnings("unused") boolean dummy) { |
10 | this.a = a; |
11 | } |
12 | |
13 | *(final int capacity) { |
14 | if (capacity < 0) |
15 | throw new IllegalArgumentException("Initial capacity (" + capacity + ") is negative"); |
16 | a = new int[capacity]; |
17 | } |
18 | |
19 | *() { |
20 | this(DEFAULT_INITIAL_CAPACITY); |
21 | } |
22 | |
23 | *(final Collection<? extends Integer> c) { |
24 | this(c.size()); |
25 | size = IntIterators.unwrap(IntIterators.asIntIterator(c.iterator()), a); |
26 | } |
27 | |
28 | *(final IntCollection c) { |
29 | this(c.size()); |
30 | size = IntIterators.unwrap(c.iterator(), a); |
31 | } |
32 | |
33 | *(final IntList l) { |
34 | this(l.size()); |
35 | l.getElements(0, a, 0, size = l.size()); |
36 | } |
37 | |
38 | *(final int a[]) { |
39 | this(a, 0, a.length); |
40 | } |
41 | |
42 | *(final int a[], final int offset, final int length) { |
43 | this(length); |
44 | System.arraycopy(a, offset, this.a, 0, length); |
45 | size = length; |
46 | } |
47 | |
48 | public int[] elements() { |
49 | return a; |
50 | } |
51 | |
52 | static IntArrayList wrap(final int a[], final int length) { |
53 | if (length > a.length) |
54 | throw new IllegalArgumentException( |
55 | "The specified length (" + length + ") is greater than the array size (" + a.length + ")"); |
56 | final IntArrayList l = new IntArrayList(a, false); |
57 | l.size = length; |
58 | return l; |
59 | } |
60 | |
61 | static IntArrayList wrap(final int a[]) { |
62 | return wrap(a, a.length); |
63 | } |
64 | |
65 | public void ensureCapacity(final int capacity) { |
66 | a = IntArrays.ensureCapacity(a, capacity, size); |
67 | if (ASSERTS) |
68 | assert size <= a.length; |
69 | } |
70 | |
71 | private void grow(final int capacity) { |
72 | a = IntArrays.grow(a, capacity, size); |
73 | if (ASSERTS) |
74 | assert size <= a.length; |
75 | } |
76 | @Override |
77 | public void add(final int index, final int k) { |
78 | ensureIndex(index); |
79 | grow(size + 1); |
80 | if (index != size) |
81 | System.arraycopy(a, index, a, index + 1, size - index); |
82 | a[index] = k; |
83 | size++; |
84 | if (ASSERTS) |
85 | assert size <= a.length; |
86 | } |
87 | @Override |
88 | public boolean add(final int k) { |
89 | grow(size + 1); |
90 | a[size++] = k; |
91 | if (ASSERTS) |
92 | assert size <= a.length; |
93 | return true; |
94 | } |
95 | @Override |
96 | public int getInt(final int index) { |
97 | if (index >= size) |
98 | throw new IndexOutOfBoundsException( |
99 | "Index (" + index + ") is greater than or equal to list size (" + size + ")"); |
100 | return a[index]; |
101 | } |
102 | @Override |
103 | public int indexOf(final int k) { |
104 | for (int i = 0; i < size; i++) |
105 | if (((k) == (a[i]))) |
106 | return i; |
107 | return -1; |
108 | } |
109 | @Override |
110 | public int lastIndexOf(final int k) { |
111 | for (int i = size; i-- != 0;) |
112 | if (((k) == (a[i]))) |
113 | return i; |
114 | return -1; |
115 | } |
116 | @Override |
117 | public int removeInt(final int index) { |
118 | if (index >= size) |
119 | throw new IndexOutOfBoundsException( |
120 | "Index (" + index + ") is greater than or equal to list size (" + size + ")"); |
121 | final int old = a[index]; |
122 | size--; |
123 | if (index != size) |
124 | System.arraycopy(a, index + 1, a, index, size - index); |
125 | if (ASSERTS) |
126 | assert size <= a.length; |
127 | return old; |
128 | } |
129 | @Override |
130 | public boolean rem(final int k) { |
131 | int index = indexOf(k); |
132 | if (index == -1) |
133 | return false; |
134 | removeInt(index); |
135 | if (ASSERTS) |
136 | assert size <= a.length; |
137 | return true; |
138 | } |
139 | @Override |
140 | public int set(final int index, final int k) { |
141 | if (index >= size) |
142 | throw new IndexOutOfBoundsException( |
143 | "Index (" + index + ") is greater than or equal to list size (" + size + ")"); |
144 | int old = a[index]; |
145 | a[index] = k; |
146 | return old; |
147 | } |
148 | @Override |
149 | public void clear() { |
150 | size = 0; |
151 | if (ASSERTS) |
152 | assert size <= a.length; |
153 | } |
154 | @Override |
155 | public int size() { |
156 | return size; |
157 | } |
158 | @Override |
159 | public void size(final int size) { |
160 | if (size > a.length) |
161 | ensureCapacity(size); |
162 | if (size > this.size) |
163 | Arrays.fill(a, this.size, size, (0)); |
164 | this.size = size; |
165 | } |
166 | @Override |
167 | public boolean isEmpty() { |
168 | return size == 0; |
169 | } |
170 | /** |
171 | * Trims this array list so that the capacity is equal to the size. |
172 | * |
173 | * @see java.util.ArrayList#trimToSize() |
174 | */ |
175 | public void trim() { |
176 | trim(0); |
177 | } |
178 | |
179 | public void trim(final int n) { |
180 | // TODO: use Arrays.trim() and preserve type only if necessary |
181 | if (n >= a.length || size == a.length) |
182 | return; |
183 | final int t[] = new int[Math.max(n, size)]; |
184 | System.arraycopy(a, 0, t, 0, size); |
185 | a = t; |
186 | if (ASSERTS) |
187 | assert size <= a.length; |
188 | } |
189 | |
190 | public void getElements(final int from, final int[] a, final int offset, final int length) { |
191 | IntArrays.ensureOffsetLength(a, offset, length); |
192 | System.arraycopy(this.a, from, a, offset, length); |
193 | } |
194 | |
195 | public void removeElements(final int from, final int to) { |
196 | it.unimi.dsi.fastutil.Arrays.ensureFromTo(size, from, to); |
197 | System.arraycopy(a, to, a, from, size - to); |
198 | size -= (to - from); |
199 | } |
200 | |
201 | public void addElements(final int index, final int a[], final int offset, final int length) { |
202 | ensureIndex(index); |
203 | IntArrays.ensureOffsetLength(a, offset, length); |
204 | grow(size + length); |
205 | System.arraycopy(this.a, index, this.a, index + length, size - index); |
206 | System.arraycopy(a, offset, this.a, index, length); |
207 | size += length; |
208 | } |
209 | |
210 | @Override |
211 | public int[] toArray(int a[]) { |
212 | if (a == null || a.length < size) |
213 | a = new int[size]; |
214 | System.arraycopy(this.a, 0, a, 0, size); |
215 | return a; |
216 | } |
217 | @Override |
218 | public boolean addAll(int index, final IntCollection c) { |
219 | ensureIndex(index); |
220 | int n = c.size(); |
221 | if (n == 0) |
222 | return false; |
223 | grow(size + n); |
224 | if (index != size) |
225 | System.arraycopy(a, index, a, index + n, size - index); |
226 | final IntIterator i = c.iterator(); |
227 | size += n; |
228 | while (n-- != 0) |
229 | a[index++] = i.nextInt(); |
230 | if (ASSERTS) |
231 | assert size <= a.length; |
232 | return true; |
233 | } |
234 | |
235 | @Override |
236 | public boolean addAll(final int index, final IntList l) { |
237 | ensureIndex(index); |
238 | final int n = l.size(); |
239 | if (n == 0) |
240 | return false; |
241 | grow(size + n); |
242 | if (index != size) |
243 | System.arraycopy(a, index, a, index + n, size - index); |
244 | l.getElements(0, a, index, n); |
245 | size += n; |
246 | if (ASSERTS) |
247 | assert size <= a.length; |
248 | return true; |
249 | } |
250 | |
251 | @Override |
252 | public boolean removeAll(final IntCollection c) { |
253 | final int[] a = this.a; |
254 | int j = 0; |
255 | for (int i = 0; i < size; i++) |
256 | if (!c.contains(a[i])) |
257 | a[j++] = a[i]; |
258 | final boolean modified = size != j; |
259 | size = j; |
260 | return modified; |
261 | } |
262 | |
263 | @Override |
264 | public boolean removeAll(final Collection<?> c) { |
265 | final int[] a = this.a; |
266 | int j = 0; |
267 | for (int i = 0; i < size; i++) |
268 | if (!c.contains(Integer.valueOf(a[i]))) |
269 | a[j++] = a[i]; |
270 | final boolean modified = size != j; |
271 | size = j; |
272 | return modified; |
273 | } |
274 | |
275 | @Override |
276 | public IntListIterator listIterator(final int index) { |
277 | ensureIndex(index); |
278 | return new IntListIterator() { |
279 | int pos = index, last = -1; |
280 | @Override |
281 | public boolean hasNext() { |
282 | return pos < size; |
283 | } |
284 | @Override |
285 | public boolean hasPrevious() { |
286 | return pos > 0; |
287 | } |
288 | @Override |
289 | public int nextInt() { |
290 | if (!hasNext()) |
291 | throw new NoSuchElementException(); |
292 | return a[last = pos++]; |
293 | } |
294 | @Override |
295 | public int previousInt() { |
296 | if (!hasPrevious()) |
297 | throw new NoSuchElementException(); |
298 | return a[last = --pos]; |
299 | } |
300 | @Override |
301 | public int nextIndex() { |
302 | return pos; |
303 | } |
304 | @Override |
305 | public int previousIndex() { |
306 | return pos - 1; |
307 | } |
308 | @Override |
309 | public void add(int k) { |
310 | IntArrayList.this.add(pos++, k); |
311 | last = -1; |
312 | } |
313 | @Override |
314 | public void set(int k) { |
315 | if (last == -1) |
316 | throw new IllegalStateException(); |
317 | IntArrayList.this.set(last, k); |
318 | } |
319 | @Override |
320 | public void remove() { |
321 | if (last == -1) |
322 | throw new IllegalStateException(); |
323 | IntArrayList.this.removeInt(last); |
324 | /* |
325 | * If the last operation was a next(), we are removing an element *before* us, |
326 | * and we must decrease pos correspondingly. |
327 | */ |
328 | if (last < pos) |
329 | pos--; |
330 | last = -1; |
331 | } |
332 | }; |
333 | } |
334 | |
335 | @Override |
336 | public IntArrayList clone() { |
337 | IntArrayList c = new IntArrayList(size); |
338 | System.arraycopy(a, 0, c.a, 0, size); |
339 | c.size = size; |
340 | return c; |
341 | } |
342 | } |
/** * A type-specific array-based list; provides some additional methods that use * polymorphism to avoid (un)boxing. * * <p> * This class implements a lightweight, fast, open, optimized, reuse-oriented * version of array-based lists. Instances of this class represent a list with * an array that is enlarged as needed when new entries are created (by doubling * its current length), but is <em>never</em> made smaller (even on a * {@link #clear()}). A family of {@linkplain #trim() trimming methods} lets you * control the size of the backing array; this is particularly useful if you * reuse instances of this class. Range checks are equivalent to those of * {@link java.util}'s classes, but they are delayed as much as possible. The * backing array is exposed by the {@link #elements()} method. * * <p> * This class implements the bulk methods {@code removeElements()}, * {@code addElements()} and {@code getElements()} using high-performance system * calls (e.g., {@link System#arraycopy(Object,int,Object,int,int) * System.arraycopy()} instead of expensive loops. * * @see java.util.ArrayList */
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1012237 |
Snippet name: | IntArrayList (dev.) |
Eternal ID of this version: | #1012237/2 |
Text MD5: | b28a338fd3aa74a8ad02d1bad32a2da1 |
Author: | stefan |
Category: | javax / primitive collections |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-11-26 11:42:54 |
Source code size: | 8355 bytes / 342 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 386 / 418 |
Version history: | 1 change(s) |
Referenced in: | [show references] |