1 | static final class SmallestListMultiMap<A,B> { |
2 | Map<A, O> data = new HashMap<A, O>(); // values are smallestLists |
3 | |
4 | *() {} |
5 | *(bool useTreeMap) { if (useTreeMap) data = new TreeMap; } |
6 | *(MultiMap<A, B> map) { putAll(map); } |
7 | |
8 | void put(A key, B value) { synchronized(data) { |
9 | O list = data.get(key); |
10 | data.put(key, smallestList_add(list, value)); |
11 | }} |
12 | |
13 | void addAll(A key, Collection<B> values) { synchronized(data) { |
14 | putAll(key, values); |
15 | }} |
16 | |
17 | void addAllIfNotThere(A key, Collection<B> values) { synchronized(data) { |
18 | for (B value : values) |
19 | setPut(key, value); |
20 | }} |
21 | |
22 | void setPut(A key, B value) { synchronized(data) { |
23 | if (!containsPair(key, value)) |
24 | put(key, value); |
25 | }} |
26 | |
27 | boolean containsPair(A key, B value) { synchronized(data) { |
28 | ret smallestList_contains(get(key), value); |
29 | }} |
30 | |
31 | void putAll(A key, Collection<B> values) { synchronized(data) { |
32 | for (B value : values) |
33 | put(key, value); |
34 | }} |
35 | |
36 | void removeAll(A key, Collection<B> values) { synchronized(data) { |
37 | for (B value : values) |
38 | remove(key, value); |
39 | }} |
40 | |
41 | List<B> get(A key) { synchronized(data) { |
42 | ret smallestList_toList(data.get(key)); |
43 | }} |
44 | |
45 | Set<A> keySet() { synchronized(data) { |
46 | return data.keySet(); |
47 | }} |
48 | |
49 | Set<A> keys() { synchronized(data) { |
50 | return data.keySet(); |
51 | }} |
52 | |
53 | void remove(A key) { synchronized(data) { |
54 | data.remove(key); |
55 | }} |
56 | |
57 | void remove(A key, B value) { synchronized(data) { |
58 | O list = data.get(key); |
59 | if (list != null) { |
60 | O list2 = smallestList_remove(list, value); |
61 | if (list2 != list) |
62 | if (list2 == null) |
63 | data.remove(key); |
64 | else |
65 | data.put(key, list2); |
66 | } |
67 | }} |
68 | |
69 | void clear() { synchronized(data) { |
70 | data.clear(); |
71 | }} |
72 | |
73 | boolean containsKey(A key) { synchronized(data) { |
74 | return data.containsKey(key); |
75 | }} |
76 | |
77 | B getFirst(A key) { synchronized(data) { |
78 | ret (B) smallestList_get(data.get(key), 0); |
79 | }} |
80 | |
81 | void putAll(MultiMap<A, B> map) { synchronized(data) { |
82 | for (A key : map.keySet()) |
83 | putAll(key, map.get(key)); |
84 | }} |
85 | |
86 | int keysSize() { synchronized(data) { ret l(data); }} |
87 | |
88 | // full size - note: expensive operation |
89 | int size() { synchronized(data) { |
90 | int n = 0; |
91 | for (O l : data.values()) |
92 | n += smallestList_l(l); |
93 | ret n; |
94 | }} |
95 | |
96 | // expensive operation |
97 | L<A> reverseGet(B b) { synchronized(data) { |
98 | new L<A> l; |
99 | for (A key : data.keySet()) |
100 | if (smallestList_contains(data.get(key), b)) |
101 | l.add(key); |
102 | ret l; |
103 | }} |
104 | |
105 | Map<A, L<B>> asMap() { synchronized(data) { |
106 | new HashMap<A, L<B>> map; |
107 | for (A key : data.keySet()) |
108 | map.put(key, smallestList_toList(data.get(key))); |
109 | ret map; |
110 | }} |
111 | |
112 | bool isEmpty() { synchronized(data) { ret data.isEmpty(); }} |
113 | } |
Began life as a copy of #1001296
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: | #1012280 |
Snippet name: | SmallestListMultiMap - very space-saving MultiMap |
Eternal ID of this version: | #1012280/7 |
Text MD5: | 0888741cfcb19df31f7ccbb8e48cb064 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-11-27 11:03:01 |
Source code size: | 2875 bytes / 113 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 489 / 1093 |
Version history: | 6 change(s) |
Referenced in: | [show references] |