Transpiled version (9189L) is out of date.
1 | // records its full size (total value count) in a field now |
2 | sclass MultiMap<A,B> is IMultiMap<A, B> { |
3 | Map<A, L<B>> data = new HashMap<A, L<B>>(); |
4 | int fullSize; |
5 | |
6 | MultiMap() {} |
7 | MultiMap(bool useTreeMap) { if (useTreeMap) data = new TreeMap; } |
8 | MultiMap(MultiMap<A, B> map) { putAll(map); } |
9 | *(Map<A, L<B>> *data) {} |
10 | |
11 | void put(A key, B value) { synchronized(data) { |
12 | L<B> list = data.get(key); |
13 | if (list == null) |
14 | data.put(key, list = _makeEmptyList()); |
15 | list.add(value); |
16 | ++fullSize; |
17 | }} |
18 | |
19 | void add(A key, B value) { put(key, value); } |
20 | |
21 | void addAll(A key, Collection<B> values) { putAll(key, values); } |
22 | |
23 | void addAllIfNotThere(A key, Collection<B> values) { synchronized(data) { |
24 | for (B value : values) |
25 | setPut(key, value); |
26 | }} |
27 | |
28 | void setPut(A key, B value) { synchronized(data) { |
29 | if (!containsPair(key, value)) |
30 | put(key, value); |
31 | }} |
32 | |
33 | boolean containsPair(A key, B value) { synchronized(data) { |
34 | ret get(key).contains(value); |
35 | }} |
36 | |
37 | void putAll(Cl<A> keys, B value) { synchronized(data) { |
38 | fOr (A key : keys) |
39 | put(key, value); |
40 | }} |
41 | |
42 | void putAll(A key, Collection<B> values) { synchronized(data) { |
43 | if (nempty(values)) getActual(key).addAll(values); |
44 | }} |
45 | |
46 | void putAll(Iterable<Pair<A, B>> pairs) { synchronized(data) { |
47 | fOr (Pair<A, B> p : pairs) |
48 | put(p.a, p.b); |
49 | }} |
50 | |
51 | void removeAll(A key, Cl<B> values) { synchronized(data) { |
52 | for (B value : values) |
53 | remove(key, value); |
54 | }} |
55 | |
56 | public L<B> get(A key) { synchronized(data) { |
57 | List<B> list = data.get(key); |
58 | return list == null ? Collections.<B> emptyList() : list; |
59 | }} |
60 | |
61 | L<B> getOpt(A key) { synchronized(data) { |
62 | ret data.get(key); |
63 | }} |
64 | |
65 | L<B> getAndClear(A key) { synchronized(data) { |
66 | L<B> l = cloneList(data.get(key)); |
67 | remove(key); |
68 | ret l; |
69 | }} |
70 | |
71 | // returns actual mutable live list |
72 | // creates the list if not there |
73 | List<B> getActual(A key) { synchronized(data) { |
74 | List<B> list = data.get(key); |
75 | if (list == null) |
76 | data.put(key, list = _makeEmptyList()); |
77 | ret list; |
78 | }} |
79 | |
80 | void clean(A key) { synchronized(data) { |
81 | L<B> list = data.get(key); |
82 | if (list != null && list.isEmpty()) { |
83 | fullSize -= l(list); |
84 | data.remove(key); |
85 | } |
86 | }} |
87 | |
88 | public Set<A> keySet aka keys() { synchronized(data) { |
89 | return data.keySet(); |
90 | }} |
91 | |
92 | void remove(A key) { synchronized(data) { |
93 | fullSize -= l(this.getOpt(key)); |
94 | data.remove(key); |
95 | }} |
96 | |
97 | void removePair aka remove(Pair<A, B> p) { |
98 | if (p != null) remove(p.a, p.b); |
99 | } |
100 | |
101 | void remove(A key, B value) { synchronized(data) { |
102 | L<B> list = data.get(key); |
103 | if (list != null) { |
104 | if (list.remove(value)) |
105 | fullSize--; |
106 | if (list.isEmpty()) |
107 | data.remove(key); |
108 | } |
109 | }} |
110 | |
111 | void clear() { synchronized(data) { |
112 | data.clear(); |
113 | }} |
114 | |
115 | boolean containsKey(A key) { synchronized(data) { |
116 | return data.containsKey(key); |
117 | }} |
118 | |
119 | B getFirst(A key) { synchronized(data) { |
120 | L<B> list = get(key); |
121 | return list.isEmpty() ? null : list.get(0); |
122 | }} |
123 | |
124 | void addAll(MultiMap<A, B> map) { putAll(map); } |
125 | |
126 | void putAll(MultiMap<A, B> map) { synchronized(data) { |
127 | for (A key : map.keySet()) |
128 | putAll(key, map.get(key)); |
129 | }} |
130 | |
131 | void putAll(Map<A, B> map) { synchronized(data) { |
132 | if (map != null) for (Map.Entry<A, B> e : map.entrySet()) |
133 | put(e.getKey(), e.getValue()); |
134 | }} |
135 | |
136 | public int keysSize aka keyCount() { synchronized(data) { ret l(data); }} |
137 | |
138 | public int size aka fullSize() { synchronized(data) { |
139 | ret fullSize; |
140 | }} |
141 | |
142 | // expensive operation |
143 | L<A> reverseGet(B b) { synchronized(data) { |
144 | new L<A> l; |
145 | for (A key : data.keySet()) |
146 | if (data.get(key).contains(b)) |
147 | l.add(key); |
148 | ret l; |
149 | }} |
150 | |
151 | Map<A, L<B>> asMap() { synchronized(data) { |
152 | ret cloneMap(data); |
153 | }} |
154 | |
155 | public bool isEmpty() { synchronized(data) { ret data.isEmpty(); }} |
156 | |
157 | // override in subclasses |
158 | L<B> _makeEmptyList() { |
159 | ret new ArrayList; |
160 | } |
161 | |
162 | // returns live lists |
163 | Collection<L<B>> allLists() { |
164 | synchronized(data) { |
165 | ret new L(data.values()); |
166 | } |
167 | } |
168 | Cl<L<B>> values() { ret allLists(); } |
169 | |
170 | L<B> allValues() { |
171 | ret concatLists(data.values()); |
172 | } |
173 | |
174 | O mutex() { ret data; } |
175 | |
176 | toString { ret "mm" + str(data); } |
177 | |
178 | Map<A, L<B>> innerMap() { ret data; } |
179 | } |
download show line numbers debug dex old transpilations
Travelled to 23 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, ekrmjmnbrukm, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, ppjhyzlbdabe, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, wtqryiryparv, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1001296 |
Snippet name: | MultiMap - synchronized multi-map data structure (Key => L<Value>) |
Eternal ID of this version: | #1001296/43 |
Text MD5: | a12d6efef1915b5bd527470b5160ce37 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2023-02-14 12:46:34 |
Source code size: | 4496 bytes / 179 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 1171 / 9166 |
Version history: | 42 change(s) |
Referenced in: | [show references] |