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

66
LINES

< > BotCompany Repo | #1003331 - class OccTree<A> - with element in relation

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

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

sclass OccTree<A> {
  int count;
  new HashMap<A, OccTree> followUp;
  OccTree<A> parent;
  
  void add(L<A> script) {
    OccTree node = this;
    ++count;
    for (A c : script)
      (node = node.getOrMakeFollowUp(c)).count++;
  }
  
  OccTree getOrMakeFollowUp(A c) {
    OccTree node = followUp.get(c);
    if (node == null)
      followUp.put(c, node = newChild());
    ret node;
  }
  
  OccTree followUp(A a) {
    ret followUp.get(a);
  }
  
  Set<A> followUpKeys() {
    ret keys(followUp);
  }
  
  A randomChoice() {
    MultiSet<A> ms = new MultiSet(false);
    for (A c : keys(followUp))
      ms.add(c, followUp.get(c).count);
    ret msOneOf(ms);
  }
  
  OccTree<A> newChild() {
    new OccTree<A> child;
    child.parent = this;
    ret child;
  }
  
  // size including all nodes
  int size() {
    int n = 1;
    for (OccTree t : values(followUp))
      n += t.size();
    ret n;
  }
  
  L<OccTree<A>> allLeaves() {
    new L l;
    collectLeaves(l);
    ret l;
  }
  
  void collectLeaves(L<OccTree> l) {
    if (isLeaf())
      l.add(this);
    else
      for (OccTree t : values(followUp))
        t.collectLeaves(l);
  }
  
  bool isLeaf() {
    ret isEmpty(followUp);
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1003331
Snippet name: class OccTree<A> - with element in relation
Eternal ID of this version: #1003331/2
Text MD5: 7c3c4f7d445a55eee0264d1f3d328661
Transpilation MD5: 5ce504be1db4eb12cc5c84d6435f1488
Author: stefan
Category: javax / talking robots
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-09-07 18:07:19
Source code size: 1264 bytes / 66 lines
Pitched / IR pitched: No / No
Views / Downloads: 699 / 1757
Version history: 1 change(s)
Referenced in: [show references]