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

109
LINES

< > BotCompany Repo | #1009835 // Web + WebNode v2 with strings

JavaX fragment (include)

1  
sclass WebNode {
2  
  Web web;
3  
  new L<S> labels;
4  
  
5  
  *() {}
6  
  *(Web *web) { web.fireNewNode(this); }
7  
  
8  
  void addLabel(S label) {
9  
    if (setAdd(labels, label) && web != null) {
10  
      web.index(label, this);
11  
      ++web.size;
12  
    }
13  
  }
14  
  
15  
  bool hasLabel(S label) { ret labels.contains(label); }
16  
17  
  toString { ret str(labels); }
18  
  
19  
  int count() { ret 1 + l(labels); }
20  
}
21  
22  
sclass Web {
23  
  new L<WebNode> nodes;
24  
  Map<Pair<WebNode>, WebNode> relations = new HashMap;
25  
  new MultiMap<S, WebNode> index; // label -> node
26  
  Map<S, L<WebNode>> pots = new Map;
27  
  transient Lock lock = reentrantLock(true);
28  
  int size;
29  
  static new L onNewNode; // L<voidfunc(WebNode)>
30  
  static new L onNewLabel; // L<voidfunc(WebNode, S)>
31  
  
32  
  bool potNotEmpty(S pot) { ret nempty(getPot(pot)); }
33  
  
34  
  L<WebNode> clearPot(S pot) {
35  
    L<WebNode> l = getPot(pot);
36  
    L<WebNode> l2 = cloneList(l);
37  
    l.clear();
38  
    ret l2;
39  
  }
40  
  
41  
  L<WebNode> getPot(S pot) {
42  
    L<WebNode> l = pots.get(pot);
43  
    if (l == null)
44  
      pots.put(pot, l = cloneList(nodes));
45  
    ret l;
46  
  }
47  
  
48  
  void relation(S a, S arrow, S b) {
49  
    getRelation(a, b).addLabel(arrow);
50  
  }
51  
  
52  
  WebNode getRelation(S a, S b) {
53  
    ret getRelation(findNode(a), findNode(b));
54  
  }
55  
  
56  
  WebNode getRelation(WebNode a, WebNode b) {
57  
    Pair<WebNode> p = pair(a, b);
58  
    WebNode r = relations.get(p);
59  
    if (r == null) relations.put(p, r = newNode());
60  
    ret r;
61  
  }
62  
  
63  
  WebNode newNode() {
64  
    WebNode node = new WebNode(this);
65  
    nodes.add(node); ++size;
66  
    for (L<WebNode> l : values(pots)) l.add(node);
67  
    ret node;
68  
  }
69  
  
70  
  WebNode node(S s) { ret findNode(s); }
71  
72  
  WebNode findNode(S s) {
73  
    WebNode n = findNodeOpt(s);
74  
    ret n != null ? n : newNode(s);
75  
  }
76  
  
77  
  WebNode findNodeOpt(S s) {
78  
    ret first(index.get(s));
79  
  }
80  
  
81  
  WebNode newNode(S... labels) {
82  
    WebNode n = newNode();
83  
    for (S label : labels) n.addLabel(label);
84  
    ret n;
85  
  }
86  
  
87  
  toString { ret webToString(this); }
88  
  
89  
  int count() {
90  
    ret size;
91  
    /*int count = 0;
92  
    for (WebNode n : nodes) count += n.count();
93  
    for (WebNode n : values(relations)) count += n.count();
94  
    ret count;*/
95  
  }
96  
  
97  
  void index(S label, WebNode n) {
98  
    index.put(label, n);
99  
    fireNewLabel(n, label);
100  
  }
101  
  
102  
  void clear {
103  
    size = 0;
104  
    clearAll(nodes, relations, index, pots);
105  
  }
106  
  
107  
  void fireNewNode(WebNode node) { for (O f : onNewNode) pcallF(f, node); }
108  
  void fireNewLabel(WebNode node, S label) { for (O f : onNewLabel) pcallF(f, node, label); }
109  
}

Author comment

Began life as a copy of #1007689

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1009835
Snippet name: Web + WebNode v2 with strings
Eternal ID of this version: #1009835/3
Text MD5: 7286c6db9d0085df81713a46bb8933d0
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-08-21 01:55:05
Source code size: 2575 bytes / 109 lines
Pitched / IR pitched: No / No
Views / Downloads: 405 / 436
Version history: 2 change(s)
Referenced in: [show references]