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)

sclass LineComp_PairCounts {
  new MultiSet<Pair<Int>> counts;
  MultiSetMap<Int, Pair<Int>> byCount = multiSetMap_innerTreeSet_outerRevTreeMap();
  
  void addAll(Iterable<Pair<Int>> l) { fOr (Pair<Int> p : l) add(p); }
  void removeAll(Iterable<Pair<Int>> l) { fOr (Pair<Int> p : l) remove(p); }
  
  // increment pair's count
  void add(Pair<Int> p) {
    int count = counts.get(p);
    byCount.remove(count, p);
    counts.add(p);
    byCount.put(count+1, p);
  }
  
  // decrement pair's count
  void remove(Pair<Int> p) {
    int count = counts.get(p);
    if (count == 0) fail("Can't remove pair " + p);
    byCount.remove(count, p);
    counts.remove(p);
    if (count-- > 0) byCount.put(count, p);
  }
  
  Pair<Int> mostPopularDuplicate() {
    ret toInt(firstKey(byCount)) < 2 ? null : firstValue(byCount);
  }
  
  int numberOfDuplicates() {
    ret counts.size()-l(byCount.get(1));
  }
  
  int getCount(Pair<Int> p) {
    ret counts.get(p);
  }
}

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: 148 / 185
Referenced in: [show references]