This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tree.js
135 lines (131 loc) · 3.75 KB
/
tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"use strict";
/* this script relies on digi.js */
function next(mon) {
return digi[mon].next;
}
function prev(mon) {
if (typeof digi[mon].prev == "undefined") { // memoization
digi[mon].prev = [];
for (var prevmon in digi) {
if (next(prevmon).includes(mon)) {
digi[mon].prev.push(parseInt(prevmon));
}
}
}
return digi[mon].prev;
}
function Gemel(roots) { // gemels are unions of trees
if (typeof roots == "string" || typeof roots == "number") { // single-root gemels are just trees
if (typeof digi[roots].tree == "undefined") { // memoization
roots = [roots];
}
else {
return digi[roots].tree;
}
}
if (typeof roots != "undefined") {
var parsedRoots = [];
for (var root of roots) {
parsedRoots.push(parseInt(root));
}
roots = parsedRoots;
}
this.roots = new Set(roots);
this.nodes = new Set();
this.JSONedges = new Set(); // stringify edges because [] != [] but "" == ""
// pointers for initialization
var nodes = this.nodes;
var JSONedges = this.JSONedges;
function edgeBack(mon, JSONedges) {
nodes.add(mon);
for (var prevmon of prev(mon)) {
var edge = [prevmon, mon];
var JSONedge = JSON.stringify(edge);
if (!JSONedges.has(JSONedge)) {
JSONedges.add(JSONedge);
edgeBack(prevmon, JSONedges)
}
}
}
function edgeFore(mon, JSONedges) {
nodes.add(mon);
for (var nextmon of next(mon)) {
var edge = [mon, nextmon];
var JSONedge = JSON.stringify(edge);
if (!JSONedges.has(JSONedge)) {
JSONedges.add(JSONedge);
edgeFore(nextmon, JSONedges)
}
}
}
function initEdges(mon) {
var visitedBack = new Set();
var visitedFore = new Set();
edgeBack(mon, visitedBack);
edgeFore(mon, visitedFore);
for (var edge of visitedBack) {
JSONedges.add(edge);
}
for (var edge of visitedFore) {
JSONedges.add(edge);
}
}
if (typeof roots == "object") {
// initialization
for (var root of roots) {
initEdges(parseInt(root));
}
}
if (this.roots.size == 1) { // memoization
for (var root of this.roots) {
digi[root].tree = this;
}
}
}
Gemel.prototype.forEachEdge = function (f) {
for (var JSONedge of this.JSONedges) {
var edge = JSON.parse(JSONedge);
f(edge, JSONedge);
}
};
Gemel.prototype.clone = function () {
var clone = new Gemel();
clone.roots = new Set(this.roots);
clone.nodes = new Set(this.nodes);
clone.JSONedges = new Set(this.JSONedges);
return clone;
}
Gemel.prototype.intersect = function (that) {
for (var root of this.roots) {
if (!that.roots.has(root)) {
this.roots.delete(root);
}
}
for (var node of this.nodes) {
if (!that.nodes.has(node)) {
this.nodes.delete(node);
}
}
for (var JSONedge of this.JSONedges) {
if (!that.JSONedges.has(JSONedge)) {
this.JSONedges.delete(JSONedge);
}
}
};
Gemel.prototype.intersection = function () {
var clone = this.clone();
for (var root of this.roots) {
var tree = new Gemel(root);
clone.intersect(tree);
}
for (var root of this.roots) {
clone.roots.add(root);
clone.nodes.add(root);
}
this.forEachEdge(function (edge, JSONedge) {
if (clone.nodes.has(edge[0]) && clone.nodes.has(edge[1])) {
clone.JSONedges.add(JSONedge);
}
});
return clone;
};