-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcs.js
More file actions
105 lines (100 loc) · 2.13 KB
/
Copy pathlcs.js
File metadata and controls
105 lines (100 loc) · 2.13 KB
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
function Diff(A, B) {
this.path = {};
if (A.length < B.length) {
this.a = A.split("");
this.b = B.split("");
} else {
this.a = B.split("");
this.b = A.split("");
}
// push dummy.
this.a.unshift(null);
this.b.unshift(null);
this.fp = {};
this.lcs = this.onp();
}
function max() {
var max = null;
for (var i = 0; i < arguments.length ; i++ ) {
if (max == null || max < arguments[i]) {
max = arguments[i];
}
}
return max;
}
Diff.prototype.onp = function () {
var M = this.a.length-1;
var N = this.b.length-1;
var delta = N - M;
var p = 0;
do {
for (var k = -p ; k <= delta - 1 ; k++) {
this.fp[k] = this.snake(k);
}
for (var k = delta + p ; k >= delta + 1 ; k--) {
this.fp[k] = this.snake(k);
}
// k = delta:
var k = delta;
var y = this.snake(k);
this.fp[k] = y;
p++;
} while (this.fp[delta] != N);
return this.path[delta];
}
Diff.prototype.snake = function(k) {
var i = (this.fp[k - 1] || -1) + 1;
var j = this.fp[k + 1] || -1;
var v;
if (i > j) {
if (this.path[k - 1]) {
this.path[k] = this.path[k-1].slice(0);
}
v = 1;
} else {
if (this.path[k + 1]) {
this.path[k] = this.path[k+1].slice(0);
}
v = -1;
}
if (!this.path[k])
this.path[k] = [];
this.path[k].push(v);
var y = max(i, j);
var x = y - k;
while ((x < (this.a.length-1) && y < (this.b.length-1))
&& this.a[x+1] == this.b[y+1]) {
x++;
y++;
this.path[k].push(0);
}
return y;
}
Diff.prototype.asHTML = function () {
var a = this.a.slice(1);
var b = this.b.slice(1);
var html= "";
var tags= [["<del>","</del>"], ["",""], ["<ins>","</ins>"]];
// discard first element; it's trash created when transiting to (0,0).
var which = this.lcs.slice(1);
for(var i = 0; i < which.length;) {
var j = i, t = "", w = which[i];
if (w > 0) {
for(; which[j] > 0 && j < which.length; j++) {
t += b.shift();
}
} else if (w < 0) {
for(; which[j] < 0 && j < which.length; j++) {
t += a.shift();
}
} else {
for(; which[j] == 0 && j < which.length; j++) {
t += a.shift();
b.shift();
}
}
html += tags[1+w][0]+ t +tags[1+w][1];
i = j;
}
return "<pre>" + html + "</pre>";
}