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

57
LINES

< > BotCompany Repo | #1009455 // MultiSetAndTopTen v2 (indexed top 10, long counts, LIVE)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2939L/19K).

sclass MultiSetAndTopTen<A> {
  int maxEntries = 100000, topTenTargetLength = 10;
  Map<A, Long> ms;
  L<A> topTen;
  Set<A> topTenSet;
  transient Lock lock = lock();
  
  *() {}
  *(Map<A, Long> *ms, L<A> *topTen) { topTenSet = new TreeSet(topTen); }
  
  void add(A a) {
    lock lock;
    long count = longMultiSet_add(ms, a);
    insertToTopTen(a, count);
    if (l(ms) >= maxEntries)
      longMultiSet_dropBottomPart(ms, l(ms)-iround(maxEntries*0.9));
  }
  
  void insertToTopTen(A a, long count) {
    int originalIndex = topTenSet.contains(a) ? indexOf(topTen, a) : -1;
    int i = originalIndex < 0 ? l(topTen) : originalIndex;
    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
      --i;
    if (i >= topTenTargetLength || i == originalIndex) ret;
    if (originalIndex >= 0) topTen.remove(originalIndex);
    topTen.add(i, a); topTenSet.add(a);
    while (l(topTen) > topTenTargetLength)
      topTenSet.remove(popLast(topTen));
  }
  
  void printTopTen {
    printAsciiHeading("TOP TEN (OF " + l(ms) + ")");
    pnl(map(topTen, func(S s) { s + " [" + ms.get(s) + "]" }));
  }
  
  void setTopTenSize(int size) {
    topTenTargetLength = size;
    if (l(topTen) != size) newTopTen(size);
  }
  
  void newTopTen(int size) {
    topTenTargetLength = size;
    replaceCollection(topTen, takeFirst(size, sortByDescScore(keys(ms), ms)));
    topTenSet = new TreeSet(topTen);
  }
  
  int numEntries() { ret l(ms); }
  long numOccurrences() { ret longMultiSet_l(ms); }
  
  bool contains(A a) { ret ms.containsKey(a); }
  long get(A a) { ret toLong(ms.get(a)); }
  
  L<A> topTen() { lock lock; ret cloneList(topTen); }
  L<A> entries() { lock lock; ret asList(keys(ms)); }
  
  void clear { lock lock; ms.clear(); topTen.clear(); topTenSet.clear(); }
}

Author comment

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: 561 / 1247
Version history: 20 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)