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).

1  
sclass DoubleKeyedMap<A, B, C> {
2  
  new Map<A, Map<B, C>> map1;
3  
  new Map<B, Map<A, C>> map2;
4  
  
5  
  Map<B, C> getA(A a) { ret map1.get(a); }
6  
  Map<A, C> getB(B b) { ret map2.get(b); }
7  
  
8  
  Cl<C> valuesForA(A a) { ret values(getA(a)); }
9  
  Cl<C> valuesForB(B b) { ret values(getB(b)); }
10  
  
11  
  Set<B> bsForA(A a) { ret keys(getA(a)); }
12  
  Set<A> asForB(B b) { ret keys(getB(b)); }
13  
  
14  
  C get(A a, B b) { ret mapGet(map1.get(a), b); }
15  
  C get(Pair<A, B> p) { ret p == null ? null : get(p.a, p.b); }
16  
  
17  
  C put(A a, B b, C c) {
18  
    mapGetOrCreateHashMap(map1, a).put(b, c);
19  
    ret mapGetOrCreateHashMap(map2, b).put(a, c);
20  
  }
21  
  
22  
  C put(Pair<A, B> p, C c) {
23  
    ret put(p.a, p.b, c);
24  
  }
25  
  
26  
  C remove(A a, B b) {
27  
    nestedMapRemove(map1, a, b);
28  
    ret nestedMapRemove(map2, b, a);
29  
  }
30  
  
31  
  Set<A> aKeys() { ret keys(map1); }
32  
  Set<B> bKeys() { ret keys(map2); }
33  
  
34  
  void removeAllA(Iterable<A> l) { fOr (A a : l) removeA(a); }
35  
  
36  
  void removeA(A a) {
37  
    fOr (B b : cloneList(bsForA(a))) remove(a, b);
38  
  }
39  
}

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: 237 / 625
Version history: 12 change(s)
Referenced in: [show references]