File: diff.c

package info (click to toggle)
wiggle 0.8%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,700 kB
  • sloc: ansic: 11,048; sh: 1,101; makefile: 161
file content (449 lines) | stat: -rw-r--r-- 9,775 bytes parent folder | download | duplicates (2)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 * wiggle - apply rejected patches
 *
 * Copyright (C) 2003 Neil Brown <neilb@cse.unsw.edu.au>
 *
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    Author: Neil Brown
 *    Email: <neilb@suse.de>
 */

/*
 * calculate longest common sequence between two sequences
 *
 * Each sequence contains strings with
 *   hash start length
 * We produce a list of tripples: a b len
 * where A and B point to elements in the two sequences, and len is the number
 * of common elements there
 *
 * To help keep matches close together (important when matching a changed fragment
 * against a whole) we track the disagonal of the first and last match on any path.
 * When choosing the best of two paths, we choose the furthest reaching unless
 * the other has a match and it's absolute diagonal difference is significantly smaller.
 * 'Significant' if the reduction in difference exceeds the loss of progress by a
 * factor of 2.
 *
 */

#include	<malloc.h>
#include	"wiggle.h"
#include	<string.h>
#include	<stdlib.h>


struct v {
	int x;	/* x location of furthest reaching path of current cost */
	int md; /* diagonal location of midline crossing */
	int l;  /* number of continuous common sequences found so far */
};


static int find_common(struct file *a, struct file *b,
		       int *alop, int *ahip,
		       int *blop, int *bhip,
		       int mid,
		       struct v *v)
{
	/* examine matrix from alo to ahi and blo to bhi
	 * finding longest subsequence.
	 * return new {a,b}{lo,hi} either side of midline.
	 * i.e. alo+blo <= mid <= ahi+bhi
	 *  and alo,blo to ahi,bhi is a common (possibly empty) subseq
	 *
	 * v is scratch space each is indexable from
	 * alo-bhi to ahi-blo inclusive
	 */

	int klo, khi;
	int k;
	int alo = *alop;
	int ahi = *ahip;
	int blo = *blop;
	int bhi = *bhip;
	int x,y;

	int best = (ahi-alo)+(bhi-blo);
	int dist;

	klo = khi = alo-blo;
	v[klo].x = alo;
	v[klo].l = 0;

	while(1) {

		for (k=klo ; k <= khi ; k+= 2) {
			int snake = 0;
			struct v vnew = v[k];
			x = v[k].x;
			y = x-k;
			if (y > bhi) abort();
			while (x < ahi && y < bhi &&
			       match(&a->list[x], &b->list[y])
				) {
				x++;
				y++;
				snake=1;
			}
			vnew.x = x;
			vnew.l += snake;
			dist = (ahi-x)+(bhi-y);
			if (dist < best) best = dist;
			if (x+y >= mid &&
			    v[k].x+v[k].x-k <= mid) {
				vnew.md = k;
			}
			v[k] = vnew;

			if (dist == 0) {
				/* OK! We have arrived.
				 * We crossed the midpoint at or after v[k].xm,v[k].ym
				 */
				if (x != ahi) abort();
				x = (v[k].md+mid)/2;
				y = x-v[k].md;
				*alop = x;
				*blop = y;

				while (x < ahi && y < bhi &&
				       match(&a->list[x], &b->list[y])
					) {
					x++;
					y++;
				}

				*ahip = x;
				*bhip = y;

				return k;
			}
		}

		for (k=klo+1; k <= khi-1 ; k+= 2) {
			if (v[k-1].x+1 >= v[k+1].x ) {
				v[k] = v[k-1];
				v[k].x++;
			} else {
				v[k] = v[k+1];
			}
		}

		x = v[klo].x; y = x -(klo-1);
		dist = abs((ahi-x)-(bhi-y));
		if (dist <= best) {
			v[klo-1] = v[klo];
			klo --;
		} else
			while (dist > best) {
				klo ++;
				x = v[klo].x; y = x -(klo-1);
				dist = abs((ahi-x)-(bhi-y));
			}

		x = v[khi].x+1; y = x - (khi+1);
		dist = abs((ahi-x)-(bhi-y));
		if (dist <= best) {
			v[khi+1] = v[khi];
			v[khi+1].x++;
			khi ++;
		} else
			while (dist > best) {
				khi --;
				x = v[khi].x+1; y = x - (khi+1);
				dist = abs((ahi-x)-(bhi-y));
			}
	}
}

static struct csl *lcsl(struct file *a, int alo, int ahi,
		 struct file *b, int blo, int bhi,
		 struct csl *csl,
		 struct v *v)
{
	int len;
	int alo1 = alo;
	int ahi1 = ahi;
	int blo1 = blo;
	int bhi1 = bhi;
	struct csl *rv = NULL;
	int k;

	if (ahi <= alo || bhi <= blo)
		return csl;


	k = find_common(a,b,
			&alo1, &ahi1,
			&blo1, &bhi1,
			(ahi+bhi+alo+blo)/2,
			v);
	if (k != ahi-bhi) abort();

	len = v[k].l;

	if (csl == NULL) {
		rv = csl = malloc((len+1)*sizeof(*csl));
		csl->len = 0;
	}
	if (len) {
		csl = lcsl(a,alo,alo1,
			   b,blo,blo1,
			   csl, v);

		if (ahi1 > alo1) {
			/* need to add this common seq, possibly attach
			 * to last
			 */
			if (csl->len &&
			    csl->a+csl->len == alo1 &&
			    csl->b+csl->len == blo1) {
				csl->len += ahi1-alo1;
			} else {
				if (csl->len) csl++;
				csl->len = ahi1-alo1;
				csl->a = alo1;
				csl->b = blo1;
				csl[1].len = 0;
			}
		}
		csl = lcsl(a,ahi1,ahi,
			   b,bhi1,bhi,
			   csl,v);
	}
	if (rv) {
		if (csl->len)
			csl++;
		csl->a = ahi;
		csl->b = bhi;
#if 1
		if (rv+len != csl || csl->len != 0)
			abort(); /* number of runs was wrong */
#endif
		return rv;
	} else
		return csl;
}

/* If two common sequences are separated by only an add or remove,
 * and the first common ends the same as the middle text,
 * extend the second and contract the first in the hope that the
 * first might become empty.  This ameliorates against the greedyness
 * of the 'diff' algorithm.
 * We treat the final zero-length 'csl' as a common sequence which
 * can be extended so we much make sure to add a new zero-length csl
 * to the end.
 * Once this is done, repeat the process but extend the first
 * in favour of the second but only up to the last newline.  This
 * acknowledges that semantic units more often end with common
 * text ("return 0;\n}\n", "\n") than start with it.
 */
static void fixup(struct file *a, struct file *b, struct csl *list)
{
	struct csl *list1, *orig;
	int lasteol = -1;
	int found_end = 0;
	if (!list) return;
	orig = list;
	list1 = list+1;
	while (list->len) {
		if (list1->len == 0)
			found_end = 1;

		if ((list->a+list->len == list1->a &&
		     list->b+list->len != list1->b &&
		     /* text at b inserted */
		     match(&b->list[list->b+list->len-1],
			   &b->list[list1->b-1])
			    )
		    ||
		    (list->b+list->len == list1->b &&
		     list->a+list->len != list1->a &&
		     /* text at a deleted */
		     match(&a->list[list->a+list->len-1],
			   &a->list[list1->a-1])
			    )
			) {
#if 0
			printword(stderr, a->list[list1->a-1]);
			printf("fixup %d,%d %d : %d,%d %d\n",
			       list->a,list->b,list->len,
			       list1->a,list1->b,list1->len);
#endif
			if (ends_line(a->list[list->a+list->len-1])
			    && a->list[list->a+list->len-1].len==1
			    && lasteol == -1
				) {
#if 0
				printf("E\n");
#endif
				lasteol = list1->a-1;
			}
			list1->a--;
			list1->b--;
			list1->len++;
			list->len--;
			if (list->len == 0) {
				lasteol = -1;
				if (found_end) {
					*list = *list1;
					list1->a += list1->len;
					list1->b += list1->len;
					list1->len = 0;
				} else if (list > orig)
					list--;
				else {
					*list = *list1++;
/*					printf("C\n");*/
				}
			}
		} else {
			if (lasteol >= 0) {
/*				printf("seek %d\n", lasteol);*/
				while (list1->a <= lasteol && (list1->len>1 ||
							       (found_end && list1->len > 0))) {
					list1->a++;
					list1->b++;
					list1->len--;
					list->len++;
				}
				lasteol=-1;
			}
			*++list = *list1;
			if (found_end) {
				list1->a += list1->len;
				list1->b += list1->len;
				list1->len = 0;
			} else
				list1++;
		}
		if (list->len && list1 == list) abort();
	}
	// list[1] = list1[0];
}

struct csl *diff(struct file a, struct file b)
{
	struct v *v;
	struct csl *csl;
	v = malloc(sizeof(struct v)*(a.elcnt+b.elcnt+2));
	v += b.elcnt+1;

	csl = lcsl(&a, 0, a.elcnt,
		   &b, 0, b.elcnt,
		   NULL, v);
	free(v-(b.elcnt+1));
	fixup(&a, &b, csl);
	if (!csl) {
		csl = malloc(sizeof(*csl));
		csl->len = 0;
		csl->a = a.elcnt;
		csl->b = b.elcnt;
	}
	return csl;
}

struct csl *diff_partial(struct file a, struct file b,
			 int alo, int ahi, int blo, int bhi)
{
	struct v *v;
	struct csl *csl;
	v = malloc(sizeof(struct v)*(ahi-alo+bhi-blo+2));
	v += bhi-alo+1;

	csl = lcsl(&a, alo, ahi,
		   &b, blo, bhi,
		   NULL, v);
	free(v-(bhi-alo+1));
	fixup(&a, &b, csl);
	return csl;
}


#ifdef MAIN

main(int argc, char *argv[])
{
	struct file a, b;
	struct csl *csl;
	struct elmnt *lst = malloc(argc*sizeof(*lst));
	int arg;
	int alo, ahi, blo, bhi;
	struct v *v;
	int ln;

	arg = 1;
	a.elcnt = 0;
	a.list = lst;
	while (argv[arg] && strcmp(argv[arg],"--")) {
		lst->hash = 0;
		lst->start = argv[arg];
		lst->len = strlen(argv[arg]);
		a.elcnt++;
		lst++;
		arg++;
	}
	if (!argv[arg]) {
		printf("AARGH\n");
		exit(1);
	}
	arg++;
	b.elcnt = 0;
	b.list = lst;
	while (argv[arg] && strcmp(argv[arg],"--")) {
		lst->hash = 0;
		lst->start = argv[arg];
		lst->len = strlen(argv[arg]);
		b.elcnt++;
		lst++;
		arg++;
	}

	v = malloc(sizeof(struct v)*(a.elcnt+b.elcnt+2));
	v += b.elcnt+1;
	alo = blo = 0;
	ahi = a.elcnt;
	bhi = b.elcnt;
#if 0
	ln = find_common(&a, &b,
			 &alo, &ahi, &blo, &bhi,
			 (ahi+bhi)/2,
			 v);

	printf("ln=%d (%d,%d) -> (%d,%d)\n", ln,
	       alo,blo,ahi,bhi);
#else
	csl = lcsl(&a, 0, a.elcnt,
		   &b, 0, b.elcnt,
		   NULL, v);
	fixup(&a, &b, csl);
	while (csl && csl->len) {
		int i;
		printf("%d,%d for %d:\n", csl->a,csl->b,csl->len);
		for (i=0; i<csl->len; i++) {
			printf("  %.*s (%.*s)\n",
			       a.list[csl->a+i].len, a.list[csl->a+i].start,
			       b.list[csl->b+i].len, b.list[csl->b+i].start);
		}
		csl++;
	}
#endif

	exit(0);
}

#endif