-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
166 lines (157 loc) · 4.7 KB
/
index.html
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<head>
<title>SAM Visualizer</title>
<meta charset="UTF-8">
<script src="./viz.js"></script>
<script src="//cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.staticfile.org/semantic-ui/2.3.3/semantic.min.js"></script>
<link href="//cdn.staticfile.org/semantic-ui/2.3.3/semantic.min.css" rel="stylesheet">
</head>
<style>
</style>
<body>
<div class="ui main container" style="margin-top: 70px; margin-bottom: 70px;">
<div class="ui center aligned container" style="width: 100%;">
<div class="ui form">
<div class="ui segment stacked">
<input id="input" placeholder="Strings seperated by vertical bar (|)"/>
<div style="margin-top: 10px;">
<button onclick="visualize()" class="ui submit button">Visualize</button>
<button onclick="rawDot()" class="ui submit basic button">Raw Dot</button>
<button onclick="countDistinctSubstrings()" class="ui submit basic button">Count Distinct Substrings</button>
</div>
</div>
</div>
</div>
<div class="ui center aligned container" style="width: 100%; margin-top: 35px;">
<div class="ui form">
<div id="container" class="ui segment stacked" style="display: none;"></div>
</div>
</div>
</div>
</body>
<script>
var workerURL = "./lite.render.js";
var viz = new Viz({ workerURL });
function SAM() {
this.counter = 0;
this.son = [ null ];
this.len = [ null ];
this.pre = [ null ];
this.cnt = [ null ];
this.last = this.create(0);
}
SAM.prototype.create = function(len) {
var x = ++this.counter;
this.son.push({});
this.len.push(len);
this.pre.push(null);
this.cnt.push(0);
return x;
};
SAM.prototype.extend = function(c) {
var son = this.son, len = this.len, pre = this.pre;
var i = this.last;
var dup = !!son[i][c];
this.last = dup? son[i][c]: this.create(len[i] + 1);
while (!son[i][c]) {
son[i][c] = this.last;
if (!(i = pre[i])) {
pre[this.last] = 1;
return;
}
}
var o = son[i][c];
if (len[i] + 1 == len[o]) {
if (dup) this.last = o;
else pre[this.last] = o;
return;
}
var s = this.create(len[i] + 1);
son[s] = Object.assign({}, son[o]);
pre[s] = pre[o];
pre[o] = s;
if (dup) this.last = s;
else pre[this.last] = s;
do {
son[i][c] = s;
i = pre[i];
} while (i && son[i][c] == o);
};
SAM.prototype.finalize = function() {
var n = this.len[1];
for (var i = 2; i <= this.counter; ++i)
if (this.len[i] > n) n = this.len[i];
var sum = Array(n + 1).fill(0);
var arr = Array(this.counter + 1);
for (var i = 1; i <= this.counter; ++i) ++sum[this.len[i]];
for (var i = 1; i <= n; ++i) sum[i] += sum[i - 1];
for (var i = 1; i <= this.counter; ++i) arr[sum[this.len[i]]--] = i;
for (var i = this.counter; i > 1; --i) {
var x = arr[i];
this.cnt[this.pre[x]] += this.cnt[x];
}
};
SAM.prototype.toDot = function() {
var str =
"digraph {\n" +
" 1 [color=red];\n";
for (var i = 1; i <= this.counter; ++i) {
str += " " + i + " [label=\"" + i + "\\nlength=" + this.len[i];
if (i != 1) str += "\\ncount=" + this.cnt[i];
str += "\"];\n";
if (this.pre[i])
str += " " + i + " -> " + this.pre[i] + " [color=blue];\n";
for (var key in this.son[i])
str += " " + i + " -> " + this.son[i][key] + " [label=\"" + key + "\"];\n";
}
str += "}";
return str;
};
SAM.prototype.count = function() {
var ret = 0;
for (var i = 2; i <= this.counter; ++i) ret += this.len[i] - this.len[this.pre[i]];
return ret;
};
function buildSAM(arr) {
var sam = new SAM();
for (var i = 0; i < arr.length; ++i) {
var str = arr[i];
sam.last = 1;
for (var j = 0; j < str.length; ++j) {
sam.extend(str[j]);
++sam.cnt[sam.last];
}
}
sam.finalize();
return sam;
}
var input = document.getElementById("input");
var container = document.getElementById("container");
function setContent(element) {
var old = container.children[0];
if (old) old.remove();
container.appendChild(element);
}
function get() { return buildSAM(input.value.split("|")); }
function visualize() {
document.getElementById('container').style.display = "";
viz.renderSVGElement(get().toDot())
.then(function(element) { setContent(element); })
.catch(error => {
viz = new Viz({ workerURL });
alert("Error occurred while rendering: " + String(error));
});
}
function rawDot() {
document.getElementById('container').style.display = "";
var element = document.createElement("textarea");
element.readOnly = "readonly";
element.value = get().toDot();
element.style.width = "500px";
element.style.height = "500px";
element.style.fontFamily = "monospace";
setContent(element);
}
function countDistinctSubstrings() { alert("Number of distinct substrings: " + get().count()); }
</script>