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

154
LINES

< > BotCompany Repo | #1024456 - MultiSetMap - MultiMap with a Set as value structure

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

Transpiled version (2022L) is out of date.

static class MultiSetMap<A, B> {
  Map<A, Set<B>> data = new HashMap<A, Set<B>>();
  
  *() {}
  *(bool useTreeMap) { if (useTreeMap) data = new TreeMap; }
  *(MultiSetMap<A, B> map) { putAll(map); }
  *(Map<A, Set<B>> *data) {}

  void put(A key, B value) { synchronized(data) {
    Set<B> set = data.get(key);
    if (set == null)
      data.put(key, set = _makeEmptySet());
    set.add(value);
  }}

  void add(A key, B value) { put(key, value); }

  void addAll(A key, Collection<B> values) { synchronized(data) {
    putAll(key, values);
  }}
  
  void addAllIfNotThere(A key, Collection<B> values) { synchronized(data) {
    for (B value : values)
      setPut(key, value);
  }}
  
  void setPut(A key, B value) { synchronized(data) {
    if (!containsPair(key, value))
      put(key, value);
  }}
  
  boolean containsPair(A key, B value) { synchronized(data) {
    ret get(key).contains(value);
  }}
  
  void putAll(A key, Collection<B> values) { synchronized(data) {
    for (B value : values)
      put(key, value);
  }}

  void removeAll(A key, Collection<B> values) { synchronized(data) {
    for (B value : values)
      remove(key, value);
  }}
  
  Set<B> get(A key) { synchronized(data) {
    Set<B> set = data.get(key);
    return set == null ? Collections.<B> emptySet() : set;
  }}

  // returns actual mutable live set
  // creates the set if not there
  Set<B> getActual(A key) { synchronized(data) {
    Set<B> set = data.get(key);
    if (set == null)
      data.put(key, set = _makeEmptySet());
    ret set;
  }}
 
  void clean(A key) { synchronized(data) {
    Set<B> list = data.get(key);
    if (list != null && list.isEmpty())
      data.remove(key);
  }}

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

  Set<A> keys() { synchronized(data) {
    return data.keySet();
  }}

  void remove(A key) { synchronized(data) {
    data.remove(key);
  }}

  void remove(A key, B value) { synchronized(data) {
    Set<B> set = data.get(key);
    if (set != null) {
      set.remove(value);
      if (set.isEmpty())
        data.remove(key);
    }
  }}

  void clear() { synchronized(data) {
    data.clear();
  }}

  boolean containsKey(A key) { synchronized(data) {
    return data.containsKey(key);
  }}

  B getFirst(A key) { synchronized(data) {
    return first(get(key));
  }}
  
  void addAll(MultiSetMap<A, B> map) { putAll(map); }
  
  void putAll(MultiSetMap<A, B> map) { synchronized(data) {
    for (A key : map.keySet())
      putAll(key, map.get(key));
  }}
  
  void putAll(Map<A, B> map) { synchronized(data) {
    if (map != null) for (Map.Entry<A, B> e : map.entrySet())
      put(e.getKey(), e.getValue());
  }}
  
  int keysSize() { synchronized(data) { ret l(data); }}
  
  // full size - note: expensive operation
  int size() { synchronized(data) {
    int n = 0;
    for (Set l : data.values())
      n += l(l);
    ret n;
  }}
  
  // expensive operation
  Set<A> reverseGet(B b) { synchronized(data) {
    new Set<A> l;
    for (A key : data.keySet())
      if (data.get(key).contains(b))
        l.add(key);
    ret l;
  }}
  
  Map<A, Set<B>> asMap() { synchronized(data) {
    ret cloneMap(data);
  }}
  
  bool isEmpty() { synchronized(data) { ret data.isEmpty(); }}
  
  // override in subclasses
  Set<B> _makeEmptySet() {
    ret new HashSet;
  }
  
  Collection<Set<B>> allLists() {
    synchronized(data) {
      ret new Set(data.values());
    }
  }
  
  L<B> allValues() {
    ret concatLists(values(data));
  }
  
  O mutex() { ret data; }
  
  toString { ret "mm" + str(data); }
}

Author comment

Began life as a copy of #1001296

download  show line numbers  debug dex   

Travelled to 2 computer(s): mqqgnosmbjvj, tvejysmllsmz

No comments. add comment

Snippet ID: #1024456
Snippet name: MultiSetMap - MultiMap with a Set as value structure
Eternal ID of this version: #1024456/5
Text MD5: a6445d3620bcf7c3ad6f69303b5aa2b5
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-13 13:05:49
Source code size: 3717 bytes / 154 lines
Pitched / IR pitched: No / No
Views / Downloads: 12 / 37
Version history: 4 change(s)
Referenced in: [show references]