Transpiled version (4786L) is out of date.
1 | // uses HashMap by default |
2 | static class MultiSet<A> implements IMultiSet<A> { |
3 | Map<A, Integer> map = new HashMap; |
4 | int size; // now maintaining a size counter |
5 | |
6 | *(bool useTreeMap) { |
7 | if (useTreeMap) map = new TreeMap; |
8 | } |
9 | *(TreeMap *map) {} |
10 | |
11 | *() {} |
12 | *(Iterable<A> c) { addAll(c); } |
13 | *(MultiSet<A> ms) { synchronized(ms) { |
14 | for (A a : ms.keySet()) add(a, ms.get(a)); |
15 | }} |
16 | |
17 | // returns new count |
18 | public synchronized int add(A key) { ret add(key, 1); } |
19 | |
20 | synchronized void addAll(Iterable<A> c) { |
21 | if (c != null) for (A a : c) add(a); |
22 | } |
23 | |
24 | synchronized void addAll(MultiSet<A> ms) { |
25 | for (A a : ms.keySet()) add(a, ms.get(a)); |
26 | } |
27 | |
28 | synchronized int add(A key, int count) { |
29 | if (count <= 0) ret 0; // don't calculate return value in this case |
30 | size += count; |
31 | Int i = map.get(key); |
32 | map.put(key, i != null ? (count += i) : count); |
33 | ret count; |
34 | } |
35 | |
36 | synchronized void put(A key, int count) { |
37 | int oldCount = get(key); |
38 | if (count == oldCount) ret; |
39 | size += count-oldCount; |
40 | if (count != 0) |
41 | map.put(key, count); |
42 | else |
43 | map.remove(key); |
44 | } |
45 | |
46 | public synchronized int get(A key) { |
47 | Int i = map.get(key); |
48 | ret i != null ? i : 0; |
49 | } |
50 | |
51 | synchronized bool contains(A key) { |
52 | ret map.containsKey(key); |
53 | } |
54 | |
55 | synchronized void remove(A key) { |
56 | Integer i = map.get(key); |
57 | if (i != null) { |
58 | --size; |
59 | if (i > 1) |
60 | map.put(key, i - 1); |
61 | else |
62 | map.remove(key); |
63 | } |
64 | } |
65 | |
66 | synchronized List<A> topTen() { ret getTopTen(); } |
67 | |
68 | synchronized List<A> getTopTen() { ret getTopTen(10); } |
69 | synchronized List<A> getTopTen(int maxSize) { |
70 | List<A> list = getSortedListDescending(); |
71 | return list.size() > maxSize ? list.subList(0, maxSize) : list; |
72 | } |
73 | |
74 | synchronized L<A> highestFirst() { |
75 | ret getSortedListDescending(); |
76 | } |
77 | |
78 | synchronized L<A> lowestFirst() { |
79 | ret reversedList(getSortedListDescending()); |
80 | } |
81 | |
82 | synchronized L<A> getSortedListDescending() { |
83 | List<A> list = new ArrayList<A>(map.keySet()); |
84 | Collections.sort(list, new Comparator<A>() { |
85 | public int compare(A a, A b) { |
86 | return map.get(b).compareTo(map.get(a)); |
87 | } |
88 | }); |
89 | ret list; |
90 | } |
91 | |
92 | synchronized int getNumberOfUniqueElements() { |
93 | return map.size(); |
94 | } |
95 | |
96 | synchronized int uniqueSize() { |
97 | ret map.size(); |
98 | } |
99 | |
100 | synchronized Set<A> asSet() { |
101 | return map.keySet(); |
102 | } |
103 | |
104 | synchronized NavigableSet<A> navigableSet() { |
105 | return navigableKeys((NavigableMap) map); |
106 | } |
107 | |
108 | public synchronized Set<A> keySet() { |
109 | return map.keySet(); |
110 | } |
111 | |
112 | synchronized A getMostPopularEntry aka mostPopular() { |
113 | int max = 0; |
114 | A a = null; |
115 | for (Map.Entry<A,Integer> entry : map.entrySet()) { |
116 | if (entry.getValue() > max) { |
117 | max = entry.getValue(); |
118 | a = entry.getKey(); |
119 | } |
120 | } |
121 | return a; |
122 | } |
123 | |
124 | synchronized void removeAll(A key) { |
125 | size -= get(key); |
126 | map.remove(key); |
127 | } |
128 | |
129 | public synchronized int size() { |
130 | ret size; |
131 | } |
132 | |
133 | synchronized MultiSet<A> mergeWith(MultiSet<A> set) { |
134 | MultiSet<A> result = new MultiSet<A>(); |
135 | for (A a : set.asSet()) { |
136 | result.add(a, set.get(a)); |
137 | } |
138 | return result; |
139 | } |
140 | |
141 | public synchronized boolean isEmpty() { |
142 | return map.isEmpty(); |
143 | } |
144 | |
145 | synchronized public String toString() { // hmm. sync this? |
146 | return str(map); |
147 | } |
148 | |
149 | synchronized void clear() { |
150 | map.clear(); |
151 | size = 0; |
152 | } |
153 | |
154 | synchronized Map<A, Int> asMap aka toMap() { |
155 | ret cloneMap(map); |
156 | } |
157 | } |
download show line numbers debug dex old transpilations
Travelled to 22 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, ekrmjmnbrukm, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, lulzaavyztxj, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, podlckwnjdmb, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1000988 |
Snippet name: | MultiSet - now synchronized |
Eternal ID of this version: | #1000988/27 |
Text MD5: | 0884f427e1fafa168cc87efd7984b657 |
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:49:27 |
Source code size: | 3645 bytes / 157 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 1066 / 8252 |
Version history: | 26 change(s) |
Referenced in: | [show references] |