1 | sclass OccTree2 { |
2 | OccTree2 parent; |
3 | E e; |
4 | new L<OccTree2> next; |
5 | int count; // This isn't accurate after replaceWith and stuff |
6 | |
7 | *() {} |
8 | *(E *e) {} |
9 | |
10 | void addScript(L<E> script) { |
11 | OccTree2 node = this; |
12 | ++count; |
13 | for (E c : script) |
14 | (node = node.getOrMakeFollowUp(c)).count++; |
15 | } |
16 | |
17 | OccTree2 getOrMakeFollowUp(E e) { |
18 | OccTree2 node = followUp(e); |
19 | if (node == null) |
20 | next.add(node = newChild(e)); |
21 | ret node; |
22 | } |
23 | |
24 | OccTree2 followUp(E e) { |
25 | for (OccTree2 t : next) |
26 | if (eq(t.e, e)) |
27 | ret t; |
28 | null; |
29 | } |
30 | |
31 | L<E> followUpKeys() { |
32 | ret collect(next, "e"); |
33 | } |
34 | |
35 | OccTree2 newChild(E e) { |
36 | OccTree2 child = new OccTree2(e); |
37 | child.parent = this; |
38 | ret child; |
39 | } |
40 | |
41 | // size including all nodes |
42 | int size() { |
43 | int n = 1; |
44 | for (OccTree2 t : next) |
45 | n += t.size(); |
46 | ret n; |
47 | } |
48 | |
49 | L<OccTree2> allLeaves() { new L l; collectLeaves(l); ret l; } |
50 | |
51 | // does not include root node as it has no input line associated |
52 | L<OccTree2> allNodes() { new L l; collectNodes(l); ret l; } |
53 | |
54 | void collectLeaves(L<OccTree2> l) { |
55 | if (isLeaf()) |
56 | l.add(this); |
57 | else |
58 | for (OccTree2 t : next) |
59 | t.collectLeaves(l); |
60 | } |
61 | |
62 | void collectNodes(L<OccTree2> l) { |
63 | if (!isRoot()) |
64 | l.add(this); |
65 | for (OccTree2 t : next) |
66 | t.collectNodes(l); |
67 | } |
68 | |
69 | bool isRoot() { ret e == null; } |
70 | |
71 | bool isLeaf() { |
72 | ret !isRoot() && isEmpty(next); |
73 | } |
74 | |
75 | int level() { |
76 | int n = 0; |
77 | OccTree2 node = this; |
78 | while (node.parent != null) { |
79 | ++n; |
80 | node = node.parent; |
81 | } |
82 | ret n; |
83 | } |
84 | |
85 | void replaceWith(OccTree2 n) { |
86 | int i = parent.next.indexOf(this); |
87 | parent.next.set(i, n); |
88 | n.parent = parent; |
89 | parent = null; |
90 | } |
91 | } |
Began life as a copy of #1003331
download show line numbers debug dex old transpilations
Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv
No comments. add comment
Snippet ID: | #1003407 |
Snippet name: | class OccTree2 - with element in node, fixed element class |
Eternal ID of this version: | #1003407/2 |
Text MD5: | 5ce6d734daf3c2889bd45d808f409315 |
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:05 |
Source code size: | 1836 bytes / 91 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 697 / 3597 |
Version history: | 1 change(s) |
Referenced in: | [show references] |