1 | // a persistent list using a clever combination of persisting and logging |
2 | // (well, only logging as of now) |
3 | // Note: don't put in static initializer (programID not set yet) |
4 | |
5 | static class PersistentList<A> extends AbstractList<A> {
|
6 | new List<A> m; |
7 | File file; |
8 | |
9 | PersistentList(S fileName) {
|
10 | this(getProgramFile(fileName)); |
11 | } |
12 | |
13 | PersistentList(S progID, S fileName) {
|
14 | this(getProgramFile(progID, fileName)); |
15 | } |
16 | |
17 | *(File *file) {
|
18 | for (S s : scanLog(file)) pcall {
|
19 | L l = cast unstructure(s); |
20 | if (eq(l.get(0), "add")) |
21 | m.put((A) l.get(1), (B) l.get(2)); |
22 | else if (eq(l.get(0), "remove")) |
23 | m.remove((A) l.get(1)); |
24 | else |
25 | print("Unknown command in log: " + l.get(0));
|
26 | } |
27 | } |
28 | |
29 | // just delegates |
30 | |
31 | public int size() {
|
32 | ret m.size(); |
33 | } |
34 | |
35 | public B get(int i) {
|
36 | ret m.get(i); |
37 | } |
38 | |
39 | // TODO: calling remove() in the iterator will have unpersisted |
40 | // effects. |
41 | public Set<Map.Entry<A,B>> entrySet() {
|
42 | ret m.entrySet(); |
43 | } |
44 | |
45 | // delegates with logging |
46 | |
47 | public B put(A a, B b) {
|
48 | B result = m.put(a, b); |
49 | logQuoted(file, structure(litlist("add", a, b)));
|
50 | ret result; |
51 | } |
52 | |
53 | public B remove(O a) {
|
54 | B result = m.remove(a); |
55 | logQuoted(file, structure(litlist("remove", a)));
|
56 | ret result; |
57 | } |
58 | } |
Began life as a copy of #1002063
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: | #1002363 |
| Snippet name: | PersistentList (developing) |
| Eternal ID of this version: | #1002363/1 |
| Text MD5: | c716b0c1c35951ffe4a0b6b8f5b92548 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2016-01-09 18:42:24 |
| Source code size: | 1358 bytes / 58 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 889 / 800 |
| Referenced in: | [show references] |