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

39
LINES

< > BotCompany Repo | #1028215 - LineComp_PairCounts - helper for LineComp compressor

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

Libraryless. Click here for Pure Java version (3361L/21K).

sclass LineComp_PairCounts {
  new MultiSet<IntPair> counts;
  MultiSetMap<Int, IntPair> byCount = multiSetMap_innerTreeSet_outerRevTreeMap();
  
  void addAll(Iterable<IntPair> l) { fOr (IntPair p : l) add(p); }
  void removeAll(Iterable<IntPair> l) { fOr (IntPair p : l) remove(p); }
  
  void add(int a, int b) { add(IntPair(a, b)); }
  void remove(int a, int b) { remove(IntPair(a, b)); }
  
  // increment pair's count
  void add(IntPair p) {
    int count = counts.get(p);
    byCount.remove(count, p);
    counts.add(p);
    byCount.put(count+1, p);
  }
  
  // decrement pair's count
  void remove(IntPair 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);
  }
  
  IntPair mostPopularDuplicate() {
    ret toInt(firstKey(byCount)) < 2 ? null : firstValue(byCount);
  }
  
  int numberOfDuplicates() {
    ret counts.size()-l(byCount.get(1));
  }
  
  int getCount(IntPair p) {
    ret counts.get(p);
  }
}

download  show line numbers  debug dex   

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

No comments. add comment

Snippet ID: #1028215
Snippet name: LineComp_PairCounts - helper for LineComp compressor
Eternal ID of this version: #1028215/5
Text MD5: 83025cde1cc1477887661ef3531e58a4
Transpilation MD5: a1360f95a37904b360e2b74cfa85c625
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-27 22:40:22
Source code size: 1085 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 64 / 203
Version history: 4 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1028215 & http://1028215.tinybrain.de