Libraryless. Click here for Pure Java version (9395L/52K).
1 | sclass IntBuffer implements Iterable<Int> { |
2 | int[] data; |
3 | int size; |
4 | |
5 | *() {} |
6 | *(int size) { if (size != 0) data = new int[size]; } |
7 | *(Iterable<Int> l) { |
8 | if (l cast Cl) allocate(l.size()); |
9 | addAll(l); |
10 | } |
11 | |
12 | void add(int i) { |
13 | if (size >= lIntArray(data)) { |
14 | data = resizeIntArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lIntArray(data)*2L)))); |
15 | if (size >= data.length) fail("IntBuffer too large: " + size); |
16 | } |
17 | data[size++] = i; |
18 | } |
19 | |
20 | void allocate(int n) { |
21 | data = resizeIntArray(data, max(n, size())); |
22 | } |
23 | |
24 | void setSize(int n) { |
25 | data = resizeIntArray(data, n); |
26 | size = min(l(data), size); |
27 | } |
28 | |
29 | void addAll(Iterable<Int> l) { |
30 | if (l != null) for (int i : l) add(i); |
31 | } |
32 | |
33 | void addAll(int... l) { |
34 | if (l != null) for (int i : l) add(i); |
35 | } |
36 | |
37 | // Note: may return the internal array |
38 | int[] toArray aka toIntArray() { |
39 | ret size == 0 ? null : resizeIntArray(data, size); |
40 | } |
41 | |
42 | int[] subArray(int from, int to) { |
43 | ret subIntArray(data, from, min(to, size)); |
44 | } |
45 | |
46 | int[] toArrayNonNull() { |
47 | ret unnull(toArray()); |
48 | } |
49 | |
50 | // abandoned version |
51 | /*L<Int> toList() { |
52 | ret intArrayToList(data, 0, size); |
53 | }*/ |
54 | |
55 | // now all these return a virtual list |
56 | L<Int> toList aka asList aka asVirtualList() { |
57 | ret new RandomAccessAbstractList<Int> { |
58 | public int size() { ret size; } |
59 | public Int get(int i) { ret IntBuffer.this.get(i); } |
60 | public Int set(int i, Int val) { |
61 | Int a = get(i); |
62 | data[i] = val; |
63 | ret a; |
64 | } |
65 | }; |
66 | } |
67 | |
68 | void reset { size = 0; } |
69 | void clear { reset(); } |
70 | |
71 | int size() { ret size; } |
72 | bool isEmpty() { ret size == 0; } |
73 | |
74 | int get(int idx) { |
75 | if (idx >= size) fail("Index out of range: " + idx + "/" + size); |
76 | ret data[idx]; |
77 | } |
78 | |
79 | void set(int idx, int value) { |
80 | if (idx >= size) fail("Index out of range: " + idx + "/" + size); |
81 | data[idx] = value; |
82 | } |
83 | |
84 | int popLast() { |
85 | if (size == 0) fail("empty buffer"); |
86 | ret data[--size]; |
87 | } |
88 | |
89 | int last() { ret data[size-1]; } |
90 | int nextToLast() { ret data[size-2]; } |
91 | |
92 | toString { ret squareBracket(joinWithSpace(toList())); } |
93 | |
94 | public Iterator<Int> iterator() { |
95 | ret new ItIt<Int> { |
96 | int i = 0; |
97 | |
98 | public bool hasNext() { ret i < size; } |
99 | public Int next() { |
100 | //if (!hasNext()) fail("Index out of bounds: " + i); |
101 | ret data[i++]; |
102 | } |
103 | }; |
104 | } |
105 | |
106 | public IntegerIterator integerIterator() { |
107 | ret new IntegerIterator { |
108 | int i = 0; |
109 | |
110 | public bool hasNext() { ret i < size; } |
111 | public int next() { |
112 | //if (!hasNext()) fail("Index out of bounds: " + i); |
113 | ret data[i++]; |
114 | } |
115 | toString { ret "Iterator@" + i + " over " + IntBuffer.this; } |
116 | }; |
117 | } |
118 | |
119 | void trimToSize { |
120 | data = resizeIntArray(data, size); |
121 | } |
122 | } |
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1028793 |
Snippet name: | IntBuffer |
Eternal ID of this version: | #1028793/42 |
Text MD5: | fd6c2b2c65a573658d1ff2c927e7dc8c |
Transpilation MD5: | f103fdfb56c7636f912ff44ecebd8bbc |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-08-14 19:34:21 |
Source code size: | 2956 bytes / 122 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 580 / 1137 |
Version history: | 41 change(s) |
Referenced in: | [show references] |