Libraryless. Click here for Pure Java version (10901L/60K).
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 | int getOpt(int idx) {
|
80 | ret idx >= 0 && idx < size ? data[idx] : 0; |
81 | } |
82 | |
83 | void set(int idx, int value) {
|
84 | if (idx >= size) fail("Index out of range: " + idx + "/" + size);
|
85 | data[idx] = value; |
86 | } |
87 | |
88 | int popLast() {
|
89 | if (size == 0) fail("empty buffer");
|
90 | ret data[--size]; |
91 | } |
92 | |
93 | int last() { ret data[size-1]; }
|
94 | int nextToLast() { ret data[size-2]; }
|
95 | |
96 | toString { ret squareBracket(joinWithSpace(toList())); }
|
97 | |
98 | public Iterator<Int> iterator() {
|
99 | ret new ItIt<Int> {
|
100 | int i = 0; |
101 | |
102 | public bool hasNext() { ret i < size; }
|
103 | public Int next() {
|
104 | //if (!hasNext()) fail("Index out of bounds: " + i);
|
105 | ret data[i++]; |
106 | } |
107 | }; |
108 | } |
109 | |
110 | public IntegerIterator integerIterator() {
|
111 | ret new IntegerIterator {
|
112 | int i = 0; |
113 | |
114 | public bool hasNext() { ret i < size; }
|
115 | public int next() {
|
116 | //if (!hasNext()) fail("Index out of bounds: " + i);
|
117 | ret data[i++]; |
118 | } |
119 | toString { ret "Iterator@" + i + " over " + IntBuffer.this; }
|
120 | }; |
121 | } |
122 | |
123 | void trimToSize {
|
124 | data = resizeIntArray(data, size); |
125 | } |
126 | } |
download show line numbers debug dex old transpilations
Travelled to 8 computer(s): bhatertpkbcr, elmgxqgtpvxh, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
| Snippet ID: | #1028793 |
| Snippet name: | IntBuffer |
| Eternal ID of this version: | #1028793/45 |
| Text MD5: | 9f04831a88c86b2346f21e1a3603625f |
| Transpilation MD5: | 9e78d94a8412c33785b7994c2a9ac207 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2025-01-06 11:20:35 |
| Source code size: | 3037 bytes / 126 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 3402 / 4320 |
| Version history: | 44 change(s) |
| Referenced in: | [show references] |