Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

36
LINES

< > BotCompany Repo | #1009449 // MultiSetAndTopTen

JavaX fragment (include)

1  
sclass MultiSetAndTopTen<A> {
2  
  int maxEntries = 100000, topTenTargetLength = 10;
3  
  Map<A, Long> ms;
4  
  L<A> topTen;
5  
  
6  
  *() {}
7  
  *(Map<A, Long> *ms, L<A> *topTen) {}
8  
  
9  
  void add(A a) {
10  
    long count = longMultiSet_add(ms, a);
11  
    insertToTopTen(a, count);
12  
    if (l(ms) >= maxEntries)
13  
      longMultiSet_dropBottomPart(ms, l(ms)-iround(maxEntries*0.9));
14  
  }
15  
  
16  
  void insertToTopTen(A a, long count) {
17  
    int originalIndex = indexOf(topTen, a);
18  
    int i = originalIndex < 0 ? l(topTen) : originalIndex;
19  
    while (i > 0 && count >= toLong(ms.get(topTen.get(i-1))))
20  
      --i;
21  
    if (i >= topTenTargetLength || i == originalIndex) ret;
22  
    if (originalIndex >= 0) topTen.remove(originalIndex);
23  
    topTen.add(i, a);
24  
    truncateList(topTen, topTenTargetLength);
25  
  }
26  
  
27  
  void printTopTen {
28  
    printAsciiHeading("TOP TEN (OF " + l(ms) + ")");
29  
    pnl(map(topTen, func(S s) { s + " - " + ms.get(s) }));
30  
  }
31  
  
32  
  void newTopTen(int size) {
33  
    topTenTargetLength = size;
34  
    replaceCollection(topTen, takeFirst(size, sortByDescScore(keys(ms), ms)));
35  
  }
36  
}

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: #1009449
Snippet name: MultiSetAndTopTen
Eternal ID of this version: #1009449/9
Text MD5: 4a3d380fd5bfb876dafdbf11adf050fb
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-08-02 00:55:58
Source code size: 1094 bytes / 36 lines
Pitched / IR pitched: No / No
Views / Downloads: 442 / 481
Version history: 8 change(s)
Referenced in: [show references]