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

36
LINES

< > BotCompany Repo | #1028236 // LineComp_PairCounts - helper for LineComp compressor (backup before IntPair)

JavaX fragment (include)

1  
sclass LineComp_PairCounts {
2  
  new MultiSet<Pair<Int>> counts;
3  
  MultiSetMap<Int, Pair<Int>> byCount = multiSetMap_innerTreeSet_outerRevTreeMap();
4  
  
5  
  void addAll(Iterable<Pair<Int>> l) { fOr (Pair<Int> p : l) add(p); }
6  
  void removeAll(Iterable<Pair<Int>> l) { fOr (Pair<Int> p : l) remove(p); }
7  
  
8  
  // increment pair's count
9  
  void add(Pair<Int> p) {
10  
    int count = counts.get(p);
11  
    byCount.remove(count, p);
12  
    counts.add(p);
13  
    byCount.put(count+1, p);
14  
  }
15  
  
16  
  // decrement pair's count
17  
  void remove(Pair<Int> p) {
18  
    int count = counts.get(p);
19  
    if (count == 0) fail("Can't remove pair " + p);
20  
    byCount.remove(count, p);
21  
    counts.remove(p);
22  
    if (count-- > 0) byCount.put(count, p);
23  
  }
24  
  
25  
  Pair<Int> mostPopularDuplicate() {
26  
    ret toInt(firstKey(byCount)) < 2 ? null : firstValue(byCount);
27  
  }
28  
  
29  
  int numberOfDuplicates() {
30  
    ret counts.size()-l(byCount.get(1));
31  
  }
32  
  
33  
  int getCount(Pair<Int> p) {
34  
    ret counts.get(p);
35  
  }
36  
}

Author comment

Began life as a copy of #1028215

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028236
Snippet name: LineComp_PairCounts - helper for LineComp compressor (backup before IntPair)
Eternal ID of this version: #1028236/1
Text MD5: 0c13aea5c1234a3a99df7bfa3e05605a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-27 14:52:53
Source code size: 995 bytes / 36 lines
Pitched / IR pitched: No / No
Views / Downloads: 149 / 187
Referenced in: [show references]