Libraryless. Click here for Pure Java version (2676L/16K).
1 | sclass OccTree<A> {
|
2 | int count; |
3 | new HashMap<A, OccTree> followUp; |
4 | OccTree<A> parent; |
5 | |
6 | void add(L<A> script) {
|
7 | OccTree node = this; |
8 | ++count; |
9 | for (A c : script) |
10 | (node = node.getOrMakeFollowUp(c)).count++; |
11 | } |
12 | |
13 | OccTree getOrMakeFollowUp(A c) {
|
14 | OccTree node = followUp.get(c); |
15 | if (node == null) |
16 | followUp.put(c, node = newChild()); |
17 | ret node; |
18 | } |
19 | |
20 | OccTree followUp(A a) {
|
21 | ret followUp.get(a); |
22 | } |
23 | |
24 | Set<A> followUpKeys() {
|
25 | ret keys(followUp); |
26 | } |
27 | |
28 | A randomChoice() {
|
29 | MultiSet<A> ms = new MultiSet(false); |
30 | for (A c : keys(followUp)) |
31 | ms.add(c, followUp.get(c).count); |
32 | ret msOneOf(ms); |
33 | } |
34 | |
35 | OccTree<A> newChild() {
|
36 | new OccTree<A> child; |
37 | child.parent = this; |
38 | ret child; |
39 | } |
40 | |
41 | // size including all nodes |
42 | int size() {
|
43 | int n = 1; |
44 | for (OccTree t : values(followUp)) |
45 | n += t.size(); |
46 | ret n; |
47 | } |
48 | |
49 | L<OccTree<A>> allLeaves() {
|
50 | new L l; |
51 | collectLeaves(l); |
52 | ret l; |
53 | } |
54 | |
55 | void collectLeaves(L<OccTree> l) {
|
56 | if (isLeaf()) |
57 | l.add(this); |
58 | else |
59 | for (OccTree t : values(followUp)) |
60 | t.collectLeaves(l); |
61 | } |
62 | |
63 | bool isLeaf() {
|
64 | ret isEmpty(followUp); |
65 | } |
66 | } |
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: | 1161 / 2349 |
| Version history: | 1 change(s) |
| Referenced in: | [show references] |