6 /* diff - differential file comparison
8 * Uses an algorithm due to Harold Stone, which finds
9 * a pair of longest identical subsequences in the two
12 * The major goal is to generate the match vector J.
13 * J[i] is the index of the line in file1 corresponding
14 * to line i file0. J[i] = 0 if there is no
17 * Lines are hashed so as to work in core. All potential
18 * matches are located by sorting the lines of each file
19 * on the hash (called value). In particular, this
20 * collects the equivalence classes in file1 together.
21 * Subroutine equiv replaces the value of each line in
22 * file0 by the index of the first element of its
23 * matching equivalence in (the reordered) file1.
24 * To save space equiv squeezes file1 into a single
25 * array member in which the equivalence classes
26 * are simply concatenated, except that their first
27 * members are flagged by changing sign.
29 * Next the indices that point into member are unsorted into
30 * array class according to the original order of file0.
32 * The cleverness lies in routine stone. This marches
33 * through the lines of file0, developing a vector klist
34 * of "k-candidates". At step i a k-candidate is a matched
35 * pair of lines x,y (x in file0 y in file1) such that
36 * there is a common subsequence of lenght k
37 * between the first i lines of file0 and the first y
38 * lines of file1, but there is no such subsequence for
39 * any smaller y. x is the earliest possible mate to y
40 * that occurs in such a subsequence.
42 * Whenever any of the members of the equivalence class of
43 * lines in file1 matable to a line in file0 has serial number
44 * less than the y of some k-candidate, that k-candidate
45 * with the smallest such y is replaced. The new
46 * k-candidate is chained (via pred) to the current
47 * k-1 candidate so that the actual subsequence can
48 * be recovered. When a member has serial number greater
49 * that the y of all k-candidates, the klist is extended.
50 * At the end, the longest subsequence is pulled out
51 * and placed in the array J by unravel.
53 * With J in hand, the matches there recorded are
54 * check'ed against reality to assure that no spurious
55 * matches have crept in due to hashing. If they have,
56 * they are broken, and "jackpot " is recorded--a harmless
57 * matter except that a true match for a spuriously
58 * mated line may now be unnecessarily reported as a change.
60 * Much of the complexity of the program comes simply
61 * from trying to minimize core utilization and
62 * maximize the range of doable problems by dynamically
63 * allocating what is needed and reusing what is not.
64 * The core requirements for problems larger than somewhat
65 * are (in words) 2*length(file0) + length(file1) +
66 * 3*(number of k-candidates installed), typically about
67 * 6n words for files of length n.
81 struct line *sfile[2]; /*shortened by pruning common prefix and suffix*/
83 int pref, suff; /*length of prefix and suffix*/
84 int *class; /*will be overlaid on file[0]*/
85 int *member; /*will be overlaid on file[1]*/
86 int *klist; /*will be overlaid on file[0] after class*/
87 struct cand *clist; /* merely a free storage pot for candidates */
89 int *J; /*will be overlaid on class*/
90 long *ixold; /*will be overlaid on klist*/
91 long *ixnew; /*will be overlaid on file[1]*/
92 /* END OF SOME TIDYING */
95 sort(struct line *a, int n) /*shellsort CACM #201*/
98 struct line *ai, *aim, *j, *k;
103 for (i = 1; i <= n; i *= 2)
105 for (m /= 2; m != 0; m /= 2) {
107 for (j = a+1; j <= k; j++) {
111 if (aim->value > ai->value ||
112 aim->value == ai->value &&
113 aim->serial > ai->serial)
121 } while (ai > a && aim >= ai);
127 unsort(struct line *f, int l, int *b)
132 a = MALLOC(int, (l+1));
134 a[f[i].serial] = f[i].value;
145 for(pref=0;pref<len[0]&&pref<len[1]&&
146 file[0][pref+1].value==file[1][pref+1].value;
148 for(suff=0;suff<len[0]-pref&&suff<len[1]-pref&&
149 file[0][len[0]-suff].value==file[1][len[1]-suff].value;
152 sfile[j] = file[j]+pref;
153 slen[j] = len[j]-pref-suff;
154 for(i=0;i<=slen[j];i++)
155 sfile[j][i].serial = i;
160 equiv(struct line *a, int n, struct line *b, int m, int *c)
165 while(i<=n && j<=m) {
166 if(a[i].value < b[j].value)
168 else if(a[i].value == b[j].value)
179 while(b[j+1].value == b[j].value) {
188 newcand(int x, int y, int pred)
192 clist = REALLOC(clist, struct cand, (clen+1));
201 search(int *c, int k, int y)
206 if(clist[c[k]].y < y) /*quick look for typical case*/
210 while((l=(i+j)/2) > i) {
223 stone(int *a, int n, int *b, int *c)
231 c[0] = newcand(0,0,0);
232 for(i=1; i<=n; i++) {
240 if(y <= clist[oldc].y)
246 if(clist[c[l]].y <= y)
249 c[l] = newcand(i,y,oldc);
253 c[l] = newcand(i,y,oldc);
257 } while((y=b[++j]) > 0);
268 for(i=0; i<=len[0]; i++) {
271 else if (i > len[0]-suff)
272 J[i] = i+len[1]-len[0];
276 for(q=clist+p;q->y!=0;q=clist+q->pred)
277 J[q->x+pref] = q->y+pref;
283 int m, i0, i1, j0, j1;
289 for (i0 = 1; i0 <= m; i0 = i1+1) {
290 while (i0 <= m && J[i0] == J[i0-1]+1)
294 while (i1 < m && J[i1+1] == 0)
298 change(i0, i1, j0, j1);
302 for (i0 = m; i0 >= 1; i0 = i1-1) {
303 while (i0 >= 1 && J[i0] == J[i0+1]-1 && J[i0])
307 while (i1 > 1 && J[i1-1] == 0)
311 change(i1 , i0, j1, j0);
315 change(1, 0, 1, len[1]);
321 cmp(Biobuf* b1, Biobuf* b2)
324 uchar buf1[BUF], buf2[BUF];
327 uchar *b1s, *b1e, *b2s, *b2e;
337 if(b1s >= &buf1[BUF])
339 n = read(f1, b1s, &buf1[BUF] - b1s);
343 if(b2s >= &buf2[BUF])
345 n = read(f2, b2s, &buf2[BUF] - b2s);
353 if(memcmp((void *)b1s, (void *)b2s, n) != 0){
360 if(b1e - b1s == b2e - b2s)
366 diffreg(char *f, char *t)
382 // could use b0 and b1 but this is simpler.
384 print("binary files %s %s differ\n", f, t);
391 sort(sfile[0], slen[0]);
392 sort(sfile[1], slen[1]);
394 member = (int *)file[1];
395 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
396 member = REALLOC(member, int, slen[1]+2);
398 class = (int *)file[0];
399 unsort(sfile[0], slen[0], class);
400 class = REALLOC(class, int, slen[0]+2);
402 klist = MALLOC(int, slen[0]+2);
403 clist = MALLOC(struct cand, 1);
404 k = stone(class, slen[0], member, klist);
408 J = MALLOC(int, len[0]+2);
413 ixold = MALLOC(long, len[0]+2);
414 ixnew = MALLOC(long, len[1]+2);
415 Bseek(b0, 0, 0); Bseek(b1, 0, 0);
418 FREE(J); FREE(ixold); FREE(ixnew);
419 Bterm(b0); Bterm(b1); /* ++++ */