Libraryless. Click here for Pure Java version (3385L/19K).
1 | // analogous to OptimizedMultiSet |
2 | static class MultiSetMapWithTopTen<A, B> extends MultiSetMap<A, B> { |
3 | // outer tree map for sorting |
4 | // inner linked hash set for reproducability (HashSet ordering sucks) |
5 | MultiSetMap<Int, A> byCount = multiSetMap_innerLinkedHashSet_outerRevTreeMap(); |
6 | |
7 | *() {} |
8 | *(bool useTreeMap) { super(useTreeMap); } |
9 | *(MultiSetMap<A, B> map) { super(map); } |
10 | *(Map<A, Set<B>> data) { super(data); } |
11 | |
12 | private void unindex(A key, Set<B> values) { |
13 | if (nempty(values)) byCount.remove(l(values), key); |
14 | } |
15 | |
16 | private void index(A key, Set<B> values) { |
17 | if (nempty(values)) byCount.add(l(values), key); |
18 | } |
19 | |
20 | bool put(A key, B value) { synchronized(data) { |
21 | Set<B> set = data.get(key); |
22 | unindex(key, set); |
23 | if (set == null) |
24 | data.put(key, set = _makeEmptySet()); |
25 | bool changed = set.add(value); |
26 | index(key, set); |
27 | if (!changed) false; |
28 | ret true with ++size; |
29 | }} |
30 | |
31 | void remove(A key) { synchronized(data) { |
32 | Set<B> set = data.get(key); |
33 | if (set == null) ret; |
34 | size -= l(set); |
35 | unindex(key, set); |
36 | data.remove(key); |
37 | }} |
38 | |
39 | void remove(A key, B value) { synchronized(data) { |
40 | Set<B> set = data.get(key); |
41 | if (set == null) ret; |
42 | unindex(key, set); |
43 | bool changed = set.remove(value); |
44 | index(key, set); |
45 | if (changed) { |
46 | --size; |
47 | if (set.isEmpty()) |
48 | data.remove(key); |
49 | } |
50 | }} |
51 | |
52 | void clear() { synchronized(data) { |
53 | data.clear(); |
54 | byCount.clear(); |
55 | size = 0; |
56 | }} |
57 | |
58 | // keys are the values in the byCount multimap |
59 | ItIt<A> keysByPopularityIterator() { |
60 | ret multiSetMapValuesIterator(byCount); |
61 | } |
62 | } |
Began life as a copy of #1024456
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt
No comments. add comment
Snippet ID: | #1031457 |
Snippet name: | MultiSetMapWithTopTen - MultiSetMap with a list of most "popular" keys [dev.] |
Eternal ID of this version: | #1031457/6 |
Text MD5: | fc7ae6ecf52b24d01d998911aed339af |
Transpilation MD5: | 100b3e273920b5a705341f5db3fd2843 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-06-14 01:03:43 |
Source code size: | 1682 bytes / 62 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 229 / 390 |
Version history: | 5 change(s) |
Referenced in: | [show references] |