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)

sclass WebNode {
  Web web;
  new L<S> labels;
  
  *() {}
  *(Web *web) { web.fireNewNode(this); }
  
  void addLabel(S label) {
    if (setAdd(labels, label) && web != null) {
      web.index(label, this);
      ++web.size;
    }
  }
  
  bool hasLabel(S label) { ret labels.contains(label); }

  toString { ret str(labels); }
  
  int count() { ret 1 + l(labels); }
}

sclass Web {
  new L<WebNode> nodes;
  Map<Pair<WebNode>, WebNode> relations = new HashMap;
  new MultiMap<S, WebNode> index; // label -> node
  Map<S, L<WebNode>> pots = new Map;
  transient Lock lock = reentrantLock(true);
  int size;
  static new L onNewNode; // L<voidfunc(WebNode)>
  static new L onNewLabel; // L<voidfunc(WebNode, S)>
  
  bool potNotEmpty(S pot) { ret nempty(getPot(pot)); }
  
  L<WebNode> clearPot(S pot) {
    L<WebNode> l = getPot(pot);
    L<WebNode> l2 = cloneList(l);
    l.clear();
    ret l2;
  }
  
  L<WebNode> getPot(S pot) {
    L<WebNode> l = pots.get(pot);
    if (l == null)
      pots.put(pot, l = cloneList(nodes));
    ret l;
  }
  
  void relation(S a, S arrow, S b) {
    getRelation(a, b).addLabel(arrow);
  }
  
  WebNode getRelation(S a, S b) {
    ret getRelation(findNode(a), findNode(b));
  }
  
  WebNode getRelation(WebNode a, WebNode b) {
    Pair<WebNode> p = pair(a, b);
    WebNode r = relations.get(p);
    if (r == null) relations.put(p, r = newNode());
    ret r;
  }
  
  WebNode newNode() {
    WebNode node = new WebNode(this);
    nodes.add(node); ++size;
    for (L<WebNode> l : values(pots)) l.add(node);
    ret node;
  }
  
  WebNode node(S s) { ret findNode(s); }

  WebNode findNode(S s) {
    WebNode n = findNodeOpt(s);
    ret n != null ? n : newNode(s);
  }
  
  WebNode findNodeOpt(S s) {
    ret first(index.get(s));
  }
  
  WebNode newNode(S... labels) {
    WebNode n = newNode();
    for (S label : labels) n.addLabel(label);
    ret n;
  }
  
  toString { ret webToString(this); }
  
  int count() {
    ret size;
    /*int count = 0;
    for (WebNode n : nodes) count += n.count();
    for (WebNode n : values(relations)) count += n.count();
    ret count;*/
  }
  
  void index(S label, WebNode n) {
    index.put(label, n);
    fireNewLabel(n, label);
  }
  
  void clear {
    size = 0;
    clearAll(nodes, relations, index, pots);
  }
  
  void fireNewNode(WebNode node) { for (O f : onNewNode) pcallF(f, node); }
  void fireNewLabel(WebNode node, S label) { for (O f : onNewLabel) pcallF(f, node, label); }
}

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: 404 / 435
Version history: 2 change(s)
Referenced in: