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

39
LINES

< > BotCompany Repo | #1027832 // DoubleKeyedMap - map with 2 keys & efficient lookup from each key

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

Libraryless. Click here for Pure Java version (2626L/16K).

sclass DoubleKeyedMap<A, B, C> {
  new Map<A, Map<B, C>> map1;
  new Map<B, Map<A, C>> map2;
  
  Map<B, C> getA(A a) { ret map1.get(a); }
  Map<A, C> getB(B b) { ret map2.get(b); }
  
  Cl<C> valuesForA(A a) { ret values(getA(a)); }
  Cl<C> valuesForB(B b) { ret values(getB(b)); }
  
  Set<B> bsForA(A a) { ret keys(getA(a)); }
  Set<A> asForB(B b) { ret keys(getB(b)); }
  
  C get(A a, B b) { ret mapGet(map1.get(a), b); }
  C get(Pair<A, B> p) { ret p == null ? null : get(p.a, p.b); }
  
  C put(A a, B b, C c) {
    mapGetOrCreateHashMap(map1, a).put(b, c);
    ret mapGetOrCreateHashMap(map2, b).put(a, c);
  }
  
  C put(Pair<A, B> p, C c) {
    ret put(p.a, p.b, c);
  }
  
  C remove(A a, B b) {
    nestedMapRemove(map1, a, b);
    ret nestedMapRemove(map2, b, a);
  }
  
  Set<A> aKeys() { ret keys(map1); }
  Set<B> bKeys() { ret keys(map2); }
  
  void removeAllA(Iterable<A> l) { fOr (A a : l) removeA(a); }
  
  void removeA(A a) {
    fOr (B b : cloneList(bsForA(a))) remove(a, b);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027832
Snippet name: DoubleKeyedMap - map with 2 keys & efficient lookup from each key
Eternal ID of this version: #1027832/13
Text MD5: 96df098e083e811097332ee26e8fd493
Transpilation MD5: 1186389fbfd29562f764cdd25de260eb
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-06-07 19:26:54
Source code size: 1043 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 236 / 623
Version history: 12 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)