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

101
LINES

< > BotCompany Repo | #1029108 // OptimizedMultiSet - MultiSet that always also holds a list of all items sorted by popularity

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

Libraryless. Click here for Pure Java version (10518L/58K).

// not synchronized yet
persistable sclass OptimizedMultiSet<A> is IMultiSet<A> {
  new Map<A, Int> map;
  // outer tree map for sorting
  // inner linked hash set for reproducability (HashSet ordering sucks)
  transient MultiSetMap<Int, A> byCount = multiSetMap_innerLinkedHashSet_outerRevTreeMap();
  int size;
  
  public int add(A a) { ret add(a, 1); }
  
  int add(A a, int count) {
    if (count <= 0) ret 0;
    size += count;
    Int i = map.get(a);
    if (i != null)
      byCount.remove(i += count, a);
    else
      i = count;
    map.put(a, i);
    byCount.add(i, a);
    ret i;
  }
  
  // remove one occurrence of a (decrease its count)
  void remove(A a) {
    Int i = map.get(a);
    if (i != null) {
      --size;
      byCount.remove(i, a);
      if (--i > 0) {
        map.put(a, i);
        byCount.add(i, a);
      } else
        map.remove(a);
    }
  }
  
  // remove all occurrences of a
  void removeAll(A a) {
    Int i = map.get(a);
    if (i == null) ret;
    map.remove(a);
    byCount.remove(i, a);
    size -= i;
  }
  
  public bool isEmpty() {
    ret map.isEmpty();
  }
  
  toString {
    ret str(map);
  }
  
  A getMostPopularEntry() {
    ret firstValue(byCount);
  }
  
  // returns first of least popular entries if there is a tie
  A leastPopularEntry() {
    ret first(lastValue(castMultiSetMapToNavigableMap(byCount));
  }
  
  void _doneLoading {
    rebuildByCount();
  }
  
  // internal
  void rebuildByCount {
    byCount.clear();
    for (A entry, int count : map)
      byCount.put(count, entry);
  }
  
  public int get(A a) {
    ret map.get(a);
  }
  
  public int size() {
    ret size;
  }
  
  int uniqueSize() {
    ret map.size();
  }
  
  L<A> highestFirst aka getSortedListDescending() {
    ret asList(multiSetMapValuesIterator(byCount));
  }
  
  // drop least popular items until at or below maxSize of (distinct) entries
  void truncate(int maxSize) {
    assertTrue(maxSize >= 0);
    while (uniqueSize() > maxSize)
      removeAll(leastPopularEntry());
  }
  
  public synchronized Set<A> keySet() {
    return map.keySet();
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 9 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1029108
Snippet name: OptimizedMultiSet - MultiSet that always also holds a list of all items sorted by popularity
Eternal ID of this version: #1029108/37
Text MD5: daebedf1d3870425c3e85e71ce84da62
Transpilation MD5: 5b4d8c6cedaeff4646b5f8db832a8222
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-03-20 20:56:46
Source code size: 2198 bytes / 101 lines
Pitched / IR pitched: No / No
Views / Downloads: 282 / 692
Version history: 36 change(s)
Referenced in: [show references]