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

147
LINES

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

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

Libraryless. Click here for Pure Java version (2974L/18K).

// uses HashMap by default
static class MultiSet<A> {
  Map<A, Integer> map = new HashMap;
  int size; // now maintaining a size counter
  
  *(bool useTreeMap) {
    if (useTreeMap) map = new TreeMap;
  }
  *() {}
  *(Iterable<A> c) { addAll(c); }
  *(MultiSet<A> ms) { synchronized(ms) {
    for (A a : ms.keySet()) add(a, ms.get(a));
  }}
  
  // returns new count
  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;
    if (map.containsKey(key))
      map.put(key, count += map.get(key));
    else
      map.put(key, count);
    ret count;
  }

  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);
  }

  synchronized Set<A> keySet() {
    return map.keySet();
  }
  
  synchronized A getMostPopularEntry() {
    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);
  }

  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;
  }
  
  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() {
    ret cloneMap(map);
  }
}

download  show line numbers  debug dex   

Travelled to 16 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, lulzaavyztxj, mqqgnosmbjvj, onxytkatvevr, podlckwnjdmb, sawdedvomwva, tslmcundralx, tvejysmllsmz, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1000988
Snippet name: MultiSet - now synchronized
Eternal ID of this version: #1000988/17
Text MD5: 5264668a00b19d0600ef9ee86bb5d9aa
Transpilation MD5: 1e176833daca38b48082bca2a610dacd
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-28 01:45:09
Source code size: 3340 bytes / 147 lines
Pitched / IR pitched: No / No
Views / Downloads: 566 / 6733
Version history: 16 change(s)
Referenced in: [show references]

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