Libraryless. Click here for Pure Java version (2816L/18K).
1 | final sclass ChunkedIntList extends RandomAccessAbstractList<Int> { |
2 | int chunkShift = 16; // => chunkSize = 65536 |
3 | new ArrayList<int[]> chunks; |
4 | int actualChunks; // roughly keep track of chunk list's internal capacity so we can shrink in time |
5 | int size; |
6 | |
7 | public void add(int i) { |
8 | ensureCapacity(size+1); |
9 | set(size, i); |
10 | ++size; |
11 | } |
12 | |
13 | public Int set(int i, Int value) { |
14 | ret set(i, (int) value); |
15 | } |
16 | |
17 | public int set(int i, int value) { |
18 | int[] chunk = chunks.get(i >> chunkShift); |
19 | int idx = i & (chunkSize()-1); |
20 | int old = chunk[idx]; |
21 | chunk[idx] = value; |
22 | ret old; |
23 | } |
24 | |
25 | public bool add(Int i) { |
26 | add((int) i); |
27 | true; |
28 | } |
29 | |
30 | public int size() { ret size; } |
31 | |
32 | public Int get(int i) { |
33 | ret getPrimitive(i); |
34 | } |
35 | |
36 | public int getPrimitive(int i) { |
37 | if (i >= 0 && i < size) ret chunks.get(i >> chunkShift)[i & (chunkSize()-1)]; |
38 | throw new IndexOutOfBoundsException(i + "/" + size); |
39 | } |
40 | |
41 | void ensureCapacity(int minCapacity) { |
42 | while (l(chunks) << chunkShift < minCapacity) { |
43 | chunks.add(new int[chunkSize()]); |
44 | actualChunks = max(actualChunks, l(chunks)); |
45 | } |
46 | } |
47 | |
48 | int[] toIntArray() { |
49 | int[] a = new int[size]; |
50 | int i = 0; |
51 | for (int[] chunk : chunks) { |
52 | System.arraycopy(chunk, 0, a, i, min(chunk.length, size-i)); |
53 | i += chunk.length; |
54 | } |
55 | ret a; |
56 | } |
57 | |
58 | int chunkSize() { ret 1 << chunkShift; } |
59 | |
60 | void truncateAt(int idx) { |
61 | size = idx; |
62 | while (size <= (l(chunks)-1) << chunkShift) |
63 | popLast(chunks); |
64 | if (l(chunks)*2 < actualChunks) { |
65 | chunks.trimToSize(); |
66 | actualChunks = l(chunks); |
67 | } |
68 | } |
69 | } |
Began life as a copy of #1012238
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: | #1028525 |
Snippet name: | ChunkedIntList - stored in fixed-size chunks |
Eternal ID of this version: | #1028525/9 |
Text MD5: | 309d949f1ffd6a6e6a3b04bd3cbcb191 |
Transpilation MD5: | 29a46e930c5a95ae5633faaa9cbbdbae |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-07-26 13:49:57 |
Source code size: | 1705 bytes / 69 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 250 / 585 |
Version history: | 8 change(s) |
Referenced in: | [show references] |