-
Notifications
You must be signed in to change notification settings - Fork 924
/
hunt-mcilroy.js
134 lines (114 loc) · 3.73 KB
/
hunt-mcilroy.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
/* Copyright (c) 2006 Tony Garnock-Jones <tonyg@lshift.net>
* Copyright (c) 2006 LShift Ltd. <query@lshift.net>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
function diff(file1, file2) {
/* Text diff algorithm following Hunt and McIlroy 1976.
* J. W. Hunt and M. D. McIlroy, An algorithm for differential file
* comparison, Bell Telephone Laboratories CSTR #41 (1976)
* http://www.cs.dartmouth.edu/~doug/
*
* Expects two arrays of strings.
*/
var equivalenceClasses = {};
for (var j = 0; j < file2.length; j++) {
var line = file2[j];
if (equivalenceClasses[line]) {
equivalenceClasses[line].push(j);
} else {
equivalenceClasses[line] = [j];
}
}
var candidates = [{file1index: -1,
file2index: -1,
chain: null}];
for (var i = 0; i < file1.length; i++) {
var line = file1[i];
var file2indices = equivalenceClasses[line] || [];
var r = 0;
var c = candidates[0];
for (var jX = 0; jX < file2indices.length; jX++) {
var j = file2indices[jX];
for (var s = 0; s < candidates.length; s++) {
if ((candidates[s].file2index < j) &&
((s == candidates.length - 1) ||
(candidates[s + 1].file2index > j)))
break;
}
if (s < candidates.length) {
var newCandidate = {file1index: i,
file2index: j,
chain: candidates[s]};
if (r == candidates.length) {
candidates.push(c);
} else {
candidates[r] = c;
}
r = s + 1;
c = newCandidate;
if (r == candidates.length) {
break; // no point in examining further (j)s
}
}
}
candidates[r] = c;
}
// At this point, we know the LCS: it's in the reverse of the
// linked-list through .chain of
// candidates[candidates.length - 1].
// We now apply the LCS to build a "comm"-style picture of the
// differences between file1 and file2.
var result = [];
var tail1 = file1.length;
var tail2 = file2.length;
var common = {common: []};
function processCommon() {
if (common.common.length) {
common.common.reverse();
result.push(common);
common = {common: []};
}
}
for (var candidate = candidates[candidates.length - 1];
candidate != null;
candidate = candidate.chain) {
var different = {file1: [], file2: []};
while (--tail1 > candidate.file1index) {
different.file1.push(file1[tail1]);
}
while (--tail2 > candidate.file2index) {
different.file2.push(file2[tail2]);
}
if (different.file1.length || different.file2.length) {
processCommon();
different.file1.reverse();
different.file2.reverse();
result.push(different);
}
if (tail1 >= 0) {
common.common.push(file1[tail1]);
}
}
processCommon();
result.reverse();
return result;
}