@matheusbsbs / matheus-bagliano / commits / ca557afff9
Fork 0

Clean-up trivially redundant diff.

Also corrects the line numbers in unified output when using zero lines context.

Davide Libenzi committed Apr 3, 2006 at 18:47 UTC ca557afff9f7dad7a8739cd193ac0730d872e282
4 files changed +63 -25
xdiff/xdiffi.c
+1 -6
@@ -349,12 +349,7 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
349 kvdf += xe->xdf2.nreff + 1;
350 kvdb += xe->xdf2.nreff + 1;
351
352 - /*
353 - * Classical integer square root approximation using shifts.
354 - */
355 - xenv.mxcost = 1;
356 - for (; ndiags; ndiags >>= 2)
357 - xenv.mxcost <<= 1;
352 + xenv.mxcost = xdl_bogosqrt(ndiags);
353 if (xenv.mxcost < XDL_MAX_COST_MIN)
354 xenv.mxcost = XDL_MAX_COST_MIN;
355 xenv.snake_cnt = XDL_SNAKE_CNT;
xdiff/xprepare.c
+46 -17
@@ -25,6 +25,7 @@
25
26
27 #define XDL_KPDIS_RUN 4
28 +#define XDL_MAX_EQLIMIT 1024
29
30
31
@@ -305,26 +306,48 @@ void xdl_free_env(xdfenv_t *xe) {
306
307
308 static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
308 - long r, rdis, rpdis;
309 -
310 - for (r = 1, rdis = 0, rpdis = 1; (i - r) >= s; r++) {
309 + long r, rdis0, rpdis0, rdis1, rpdis1;
310 +
311 + /*
312 + * Scans the lines before 'i' to find a run of lines that either
313 + * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1).
314 + * Note that we always call this function with dis[i] > 1, so the
315 + * current line (i) is already a multimatch line.
316 + */
317 + for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
318 if (!dis[i - r])
312 - rdis++;
319 + rdis0++;
320 else if (dis[i - r] == 2)
314 - rpdis++;
321 + rpdis0++;
322 else
323 break;
324 }
318 - for (r = 1; (i + r) <= e; r++) {
325 + /*
326 + * If the run before the line 'i' found only multimatch lines, we
327 + * return 0 and hence we don't make the current line (i) discarded.
328 + * We want to discard multimatch lines only when they appear in the
329 + * middle of runs with nomatch lines (dis[j] == 0).
330 + */
331 + if (rdis0 == 0)
332 + return 0;
333 + for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
334 if (!dis[i + r])
320 - rdis++;
335 + rdis1++;
336 else if (dis[i + r] == 2)
322 - rpdis++;
337 + rpdis1++;
338 else
339 break;
340 }
326 -
327 - return rpdis * XDL_KPDIS_RUN < (rpdis + rdis);
341 + /*
342 + * If the run after the line 'i' found only multimatch lines, we
343 + * return 0 and hence we don't make the current line (i) discarded.
344 + */
345 + if (rdis1 == 0)
346 + return 0;
347 + rdis1 += rdis0;
348 + rpdis1 += rpdis0;
349 +
350 + return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
351 }
352
353
@@ -334,34 +357,40 @@ static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
357 * might be potentially discarded if they happear in a run of discardable.
358 */
359 static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2) {
337 - long i, rhi, nreff;
360 + long i, nm, rhi, nreff, mlim;
361 unsigned long hav;
362 xrecord_t **recs;
363 xrecord_t *rec;
364 char *dis, *dis1, *dis2;
365
343 - if (!(dis = (char *) xdl_malloc((xdf1->nrec + xdf2->nrec + 2) * sizeof(char)))) {
366 + if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
367
368 return -1;
369 }
347 - memset(dis, 0, (xdf1->nrec + xdf2->nrec + 2) * sizeof(char));
370 + memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
371 dis1 = dis;
372 dis2 = dis1 + xdf1->nrec + 1;
373
374 + if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT)
375 + mlim = XDL_MAX_EQLIMIT;
376 for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
377 hav = (*recs)->ha;
378 rhi = (long) XDL_HASHLONG(hav, xdf2->hbits);
354 - for (rec = xdf2->rhash[rhi]; rec; rec = rec->next)
355 - if (rec->ha == hav && ++dis1[i] == 2)
379 + for (nm = 0, rec = xdf2->rhash[rhi]; rec; rec = rec->next)
380 + if (rec->ha == hav && ++nm == mlim)
381 break;
382 + dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
383 }
384
385 + if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT)
386 + mlim = XDL_MAX_EQLIMIT;
387 for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
388 hav = (*recs)->ha;
389 rhi = (long) XDL_HASHLONG(hav, xdf1->hbits);
362 - for (rec = xdf1->rhash[rhi]; rec; rec = rec->next)
363 - if (rec->ha == hav && ++dis2[i] == 2)
390 + for (nm = 0, rec = xdf1->rhash[rhi]; rec; rec = rec->next)
391 + if (rec->ha == hav && ++nm == mlim)
392 break;
393 + dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
394 }
395
396 for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
xdiff/xutils.c
+15 -2
@@ -29,6 +29,19 @@
29
30
31
32 +long xdl_bogosqrt(long n) {
33 + long i;
34 +
35 + /*
36 + * Classical integer square root approximation using shifts.
37 + */
38 + for (i = 1; n > 0; n >>= 2)
39 + i <<= 1;
40 +
41 + return i;
42 +}
43 +
44 +
45 int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize,
46 xdemitcb_t *ecb) {
47 mmbuffer_t mb[3];
@@ -244,7 +257,7 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2,
257 memcpy(buf, "@@ -", 4);
258 nb += 4;
259
247 - nb += xdl_num_out(buf + nb, c1 ? s1: 0);
260 + nb += xdl_num_out(buf + nb, c1 ? s1: s1 - 1);
261
262 if (c1 != 1) {
263 memcpy(buf + nb, ",", 1);
@@ -256,7 +269,7 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2,
269 memcpy(buf + nb, " +", 2);
270 nb += 2;
271
259 - nb += xdl_num_out(buf + nb, c2 ? s2: 0);
272 + nb += xdl_num_out(buf + nb, c2 ? s2: s2 - 1);
273
274 if (c2 != 1) {
275 memcpy(buf + nb, ",", 1);
xdiff/xutils.h
+1
@@ -24,6 +24,7 @@
24 #define XUTILS_H
25
26
27 +long xdl_bogosqrt(long n);
28 int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize,
29 xdemitcb_t *ecb);
30 int xdl_cha_init(chastore_t *cha, long isize, long icount);