Libraryless. Click here for Pure Java version (2939L/19K).
1 | sclass MultiSetAndTopTen<A> { |
2 | int maxEntries = 100000, topTenTargetLength = 10; |
3 | Map<A, Long> ms; |
4 | L<A> topTen; |
5 | Set<A> topTenSet; |
6 | transient Lock lock = lock(); |
7 | |
8 | *() {} |
9 | *(Map<A, Long> *ms, L<A> *topTen) { topTenSet = new TreeSet(topTen); } |
10 | |
11 | void add(A a) { |
12 | lock lock; |
13 | long count = longMultiSet_add(ms, a); |
14 | insertToTopTen(a, count); |
15 | if (l(ms) >= maxEntries) |
16 | longMultiSet_dropBottomPart(ms, l(ms)-iround(maxEntries*0.9)); |
17 | } |
18 | |
19 | void insertToTopTen(A a, long count) { |
20 | int originalIndex = topTenSet.contains(a) ? indexOf(topTen, a) : -1; |
21 | int i = originalIndex < 0 ? l(topTen) : originalIndex; |
22 | while (i > 0 && count >= toLong(ms.get(topTen.get(i-1)))) // maybe this is even better than binary search as we will usually exit immediately |
23 | --i; |
24 | if (i >= topTenTargetLength || i == originalIndex) ret; |
25 | if (originalIndex >= 0) topTen.remove(originalIndex); |
26 | topTen.add(i, a); topTenSet.add(a); |
27 | while (l(topTen) > topTenTargetLength) |
28 | topTenSet.remove(popLast(topTen)); |
29 | } |
30 | |
31 | void printTopTen { |
32 | printAsciiHeading("TOP TEN (OF " + l(ms) + ")"); |
33 | pnl(map(topTen, func(S s) { s + " [" + ms.get(s) + "]" })); |
34 | } |
35 | |
36 | void setTopTenSize(int size) { |
37 | topTenTargetLength = size; |
38 | if (l(topTen) != size) newTopTen(size); |
39 | } |
40 | |
41 | void newTopTen(int size) { |
42 | topTenTargetLength = size; |
43 | replaceCollection(topTen, takeFirst(size, sortByDescScore(keys(ms), ms))); |
44 | topTenSet = new TreeSet(topTen); |
45 | } |
46 | |
47 | int numEntries() { ret l(ms); } |
48 | long numOccurrences() { ret longMultiSet_l(ms); } |
49 | |
50 | bool contains(A a) { ret ms.containsKey(a); } |
51 | long get(A a) { ret toLong(ms.get(a)); } |
52 | |
53 | L<A> topTen() { lock lock; ret cloneList(topTen); } |
54 | L<A> entries() { lock lock; ret asList(keys(ms)); } |
55 | |
56 | void clear { lock lock; ms.clear(); topTen.clear(); topTenSet.clear(); } |
57 | } |
Began life as a copy of #1009449
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1009455 |
Snippet name: | MultiSetAndTopTen v2 (indexed top 10, long counts, LIVE) |
Eternal ID of this version: | #1009455/21 |
Text MD5: | 655a6f0862acf483e117794732e3383d |
Transpilation MD5: | 32f3e521aff341ba0540574a2a2a572c |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-03-22 14:12:16 |
Source code size: | 1914 bytes / 57 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 562 / 1248 |
Version history: | 20 change(s) |
Referenced in: | [show references] |