1 | // a persistent map using a clever combination of persisting and logging |
2 | // and with case-insensitive keys |
3 | // (well, only logging as of now) |
4 | // Note: don't put in static initializer (programID not set yet) |
5 | static class PersistentCIMap<B> extends AbstractMap<S, B> { |
6 | new Map<S, B> m; |
7 | File file; |
8 | |
9 | PersistentCIMap(S fileName) { |
10 | this(getProgramFile(fileName)); |
11 | } |
12 | |
13 | PersistentCIMap(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(toLowerCase((S) l.get(1)), (B) l.get(2)); |
22 | else if (eq(l.get(0), "remove")) |
23 | m.remove(toLowerCase((S) 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(O a) { |
36 | ret m.get(toLowerCase((S) a)); |
37 | } |
38 | |
39 | public boolean containsKey(O a) { |
40 | ret m.containsKey(toLowerCase((S) a)); |
41 | } |
42 | |
43 | // TODO: calling remove() in the iterator will have unpersisted |
44 | // effects. |
45 | public Set<Map.Entry<S, B>> entrySet() { |
46 | ret m.entrySet(); |
47 | } |
48 | |
49 | // delegates with logging |
50 | |
51 | public B put(S a, B b) { |
52 | a = toLowerCase(a); |
53 | B c = m.get(a); |
54 | if (neq(b, c)) { |
55 | m.put(a, b); |
56 | logQuoted(file, structure(litlist("add", a, b))); |
57 | } |
58 | ret c; |
59 | } |
60 | |
61 | public B remove(O a) { |
62 | a = toLowerCase((S) a); |
63 | B result = m.remove(a); |
64 | logQuoted(file, structure(litlist("remove", a))); |
65 | ret result; |
66 | } |
67 | } |
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: | #1002872 |
Snippet name: | PersistentCIMap |
Eternal ID of this version: | #1002872/1 |
Text MD5: | bca8a24cba7d14bd7b34b87cacec29f4 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-03-10 19:01:43 |
Source code size: | 1621 bytes / 67 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 564 / 1544 |
Referenced in: | [show references] |