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

157
LINES

< > BotCompany Repo | #1000988 // MultiSet - now synchronized

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

Transpiled version (4786L) is out of date.

// uses HashMap by default
static class MultiSet<A> implements IMultiSet<A> {
  Map<A, Integer> map = new HashMap;
  int size; // now maintaining a size counter
  
  *(bool useTreeMap) {
    if (useTreeMap) map = new TreeMap;
  }
  *(TreeMap *map) {}
  
  *() {}
  *(Iterable<A> c) { addAll(c); }
  *(MultiSet<A> ms) { synchronized(ms) {
    for (A a : ms.keySet()) add(a, ms.get(a));
  }}
  
  // returns new count
  public synchronized int add(A key) { ret add(key, 1); }
  
  synchronized void addAll(Iterable<A> c) {
    if (c != null) for (A a : c) add(a);
  }

  synchronized void addAll(MultiSet<A> ms) {
    for (A a : ms.keySet()) add(a, ms.get(a));
  }
  
  synchronized int add(A key, int count) {
    if (count <= 0) ret 0; // don't calculate return value in this case
    size += count;
    Int i = map.get(key);
    map.put(key, i != null ? (count += i) : count);
    ret count;
  }

  synchronized void put(A key, int count) {
    int oldCount = get(key);
    if (count == oldCount) ret;
    size += count-oldCount;
    if (count != 0)
      map.put(key, count);
    else
      map.remove(key);
  }

  public synchronized int get(A key) {
    Int i = map.get(key);
    ret i != null ? i : 0;
  }
  
  synchronized bool contains(A key) {
    ret map.containsKey(key);
  }

  synchronized void remove(A key) {
    Integer i = map.get(key);
    if (i != null) {
      --size;
      if (i > 1)
        map.put(key, i - 1);
      else
        map.remove(key);
    }
  }

  synchronized List<A> topTen() { ret getTopTen(); }
  
  synchronized List<A> getTopTen() { ret getTopTen(10); }
  synchronized List<A> getTopTen(int maxSize) {
    List<A> list = getSortedListDescending();
    return list.size() > maxSize ? list.subList(0, maxSize) : list;
  }
  
  synchronized L<A> highestFirst() {
    ret getSortedListDescending();
  }

  synchronized L<A> lowestFirst() {
    ret reversedList(getSortedListDescending());
  }

  synchronized L<A> getSortedListDescending() {
    List<A> list = new ArrayList<A>(map.keySet());
    Collections.sort(list, new Comparator<A>() {
      public int compare(A a, A b) {
        return map.get(b).compareTo(map.get(a));
      }
    });
    ret list;
  }

  synchronized int getNumberOfUniqueElements() {
    return map.size();
  }
  
  synchronized int uniqueSize() {
    ret map.size();
  }

  synchronized Set<A> asSet() {
    return map.keySet();
  }

  synchronized NavigableSet<A> navigableSet() {
    return navigableKeys((NavigableMap) map);
  }

  public synchronized Set<A> keySet() {
    return map.keySet();
  }
  
  synchronized A getMostPopularEntry aka mostPopular() {
    int max = 0;
    A a = null;
    for (Map.Entry<A,Integer> entry : map.entrySet()) {
      if (entry.getValue() > max) {
        max = entry.getValue();
        a = entry.getKey();
      }
    }
    return a;
  }

  synchronized void removeAll(A key) {
    size -= get(key);
    map.remove(key);
  }

  public synchronized int size() {
    ret size;
  }

  synchronized MultiSet<A> mergeWith(MultiSet<A> set) {
    MultiSet<A> result = new MultiSet<A>();
    for (A a : set.asSet()) {
      result.add(a, set.get(a));
    }
    return result;
  }
  
  public synchronized boolean isEmpty() {
    return map.isEmpty();
  }
  
  synchronized public String toString() { // hmm. sync this?
    return str(map);
  }
  
  synchronized void clear() {
    map.clear();
    size = 0;
  }
  
  synchronized Map<A, Int> asMap aka toMap() {
    ret cloneMap(map);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 22 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, ekrmjmnbrukm, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, lulzaavyztxj, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, podlckwnjdmb, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1000988
Snippet name: MultiSet - now synchronized
Eternal ID of this version: #1000988/27
Text MD5: 0884f427e1fafa168cc87efd7984b657
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-02-14 12:49:27
Source code size: 3645 bytes / 157 lines
Pitched / IR pitched: No / No
Views / Downloads: 1065 / 8250
Version history: 26 change(s)
Referenced in: #1000987 - Multiset matching for prediction
#1000990 - Predict snippet continuation according to previous snippets (developing)
#1001816 - Translator for #1001814 ("Hit Log")
#1003597 - MultiHashSet
#1003872 - Integrating #759 in One Program
#1003874 - Backup of #759 Before Integration
#1004091 - 759 with new loadClasses (spike)
#1006723 - Higher-Level-Transpiled #759 Spike [old]
#1006724 - Backup of #759 before switching to higher-level-translation
#1007595 - "Super-Edgy" JavaX Translator (Extension of #752) [dev., broken]
#1010083 - Backup of "Super-Edgy" JavaX Translator (Extension of #7)
#1010084 - Speeding up "Super-Edgy" JavaX Translator (Extension of #7)
#1010087 - Speeding up "Super-Edgy" JavaX Translator (Extension of #7) 2
#1031874 - IMultiSet - interface to MultiSet
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)