1 | static class Grid { |
2 | int[] mem = new int[0]; |
3 | |
4 | *() {} |
5 | *(int[] data) { set(0, data); } |
6 | |
7 | void set(int addr, int x) { |
8 | if (addr < 0) ret; |
9 | expandTo(addr); |
10 | mem[addr] = x; |
11 | } |
12 | |
13 | void set(int addr, int[] x) { |
14 | for (int i = l(x)-1; i >= 0; i--) |
15 | set(addr+i, x[i]); |
16 | } |
17 | |
18 | int get(int addr) { |
19 | if (addr < 0 || addr >= l(mem)) |
20 | ret 0; |
21 | ret mem[addr]; |
22 | } |
23 | |
24 | void expandTo(int n) { |
25 | if (n >= l(mem)) { |
26 | int[] m = new int[max(n+1, l(mem)*2)]; |
27 | arraycopy(mem, 0, m, 0, l(mem)); |
28 | mem = m; |
29 | } |
30 | } |
31 | |
32 | int lastNonZero() { |
33 | int i = l(mem)-1; |
34 | while (i > 0 && get(i) == 0) --i; |
35 | ret i; |
36 | } |
37 | |
38 | int[] getRange(int a, int b) { |
39 | ret subArray(mem, a, b); |
40 | } |
41 | |
42 | void swap(int a, int b) { |
43 | int i = get(a); |
44 | set(a, get(b)); |
45 | set(b, i); |
46 | } |
47 | } |
Began life as a copy of #1003139
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1004583 |
Snippet name: | class Grid - basically a growable int[]. used for simple virtual machines |
Eternal ID of this version: | #1004583/2 |
Text MD5: | 581b3bc5db363a78a889355d0b41b1ce |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-09-16 21:15:47 |
Source code size: | 856 bytes / 47 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 626 / 1222 |
Version history: | 1 change(s) |
Referenced in: | [show references] |