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

158
LINES

< > BotCompany Repo | #1001296 - MultiMap (include) - now synchronized (on the inner map)

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

Libraryless. Click here for Pure Java version (2755L/17K).

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

  void put(A key, B value) { synchronized(data) {
    List<B> list = data.get(key);
    if (list == null)
      data.put(key, list = _makeEmptyList());
    list.add(value);
  }}

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

  void addAll(A key, Collection<B> values) { 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) {
    if (nempty(values)) getActual(key).addAll(values);
  }}

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

  L<B> getOpt(A key) { synchronized(data) {
    ret data.get(key);
  }}

  // returns actual mutable live list
  // creates the list if not there
  List<B> getActual(A key) { synchronized(data) {
    List<B> list = data.get(key);
    if (list == null)
      data.put(key, list = _makeEmptyList());
    ret list;
  }}
 
  void clean(A key) { synchronized(data) {
    L<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) {
    List<B> list = data.get(key);
    if (list != null) {
      list.remove(value);
      if (list.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) {
    List<B> list = get(key);
    return list.isEmpty() ? null : list.get(0);
  }}
  
  void addAll(MultiMap<A, B> map) { putAll(map); }
  
  void putAll(MultiMap<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 (L l : data.values())
      n += l(l);
    ret n;
  }}
  
  // expensive operation
  L<A> reverseGet(B b) { synchronized(data) {
    new L<A> l;
    for (A key : data.keySet())
      if (data.get(key).contains(b))
        l.add(key);
    ret l;
  }}
  
  Map<A, L<B>> asMap() { synchronized(data) {
    ret cloneMap(data);
  }}
  
  bool isEmpty() { synchronized(data) { ret data.isEmpty(); }}
  
  // override in subclasses
  L<B> _makeEmptyList() {
    ret new ArrayList;
  }
  
  // returns live lists
  Collection<L<B>> allLists() {
    synchronized(data) {
      ret new L(data.values());
    }
  }
  Cl<L<B>> values() { ret allLists(); }
  
  L<B> allValues() {
    ret concatLists(data.values());
  }
  
  O mutex() { ret data; }
  
  toString { ret "mm" + str(data); }
}

download  show line numbers  debug dex   

Travelled to 19 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, ppjhyzlbdabe, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, whxojlpjdney, wtqryiryparv, xrpafgyirdlv

No comments. add comment

Snippet ID: #1001296
Snippet name: MultiMap (include) - now synchronized (on the inner map)
Eternal ID of this version: #1001296/24
Text MD5: 38cd2a58cea7fdf6a961096eafeb85d3
Transpilation MD5: 909ddba53c5037e1bd7e15b425edd81e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-23 17:55:08
Source code size: 3900 bytes / 158 lines
Pitched / IR pitched: No / No
Views / Downloads: 685 / 7792
Version history: 23 change(s)
Referenced in: [show references]

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