Merge branch 'maint'
[git] / xdiff / xdiffi.c
1 /*
2  *  LibXDiff by Davide Libenzi ( File Differential Library )
3  *  Copyright (C) 2003  Davide Libenzi
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2.1 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Davide Libenzi <davidel@xmailserver.org>
20  *
21  */
22
23 #include "xinclude.h"
24
25
26
27 #define XDL_MAX_COST_MIN 256
28 #define XDL_HEUR_MIN_COST 256
29 #define XDL_LINE_MAX (long)((1UL << (8 * sizeof(long) - 1)) - 1)
30 #define XDL_SNAKE_CNT 20
31 #define XDL_K_HEUR 4
32
33
34
35 typedef struct s_xdpsplit {
36         long i1, i2;
37         int min_lo, min_hi;
38 } xdpsplit_t;
39
40
41
42
43 static long xdl_split(unsigned long const *ha1, long off1, long lim1,
44                       unsigned long const *ha2, long off2, long lim2,
45                       long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl,
46                       xdalgoenv_t *xenv);
47 static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2);
48
49
50
51
52
53 /*
54  * See "An O(ND) Difference Algorithm and its Variations", by Eugene Myers.
55  * Basically considers a "box" (off1, off2, lim1, lim2) and scan from both
56  * the forward diagonal starting from (off1, off2) and the backward diagonal
57  * starting from (lim1, lim2). If the K values on the same diagonal crosses
58  * returns the furthest point of reach. We might end up having to expensive
59  * cases using this algorithm is full, so a little bit of heuristic is needed
60  * to cut the search and to return a suboptimal point.
61  */
62 static long xdl_split(unsigned long const *ha1, long off1, long lim1,
63                       unsigned long const *ha2, long off2, long lim2,
64                       long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl,
65                       xdalgoenv_t *xenv) {
66         long dmin = off1 - lim2, dmax = lim1 - off2;
67         long fmid = off1 - off2, bmid = lim1 - lim2;
68         long odd = (fmid - bmid) & 1;
69         long fmin = fmid, fmax = fmid;
70         long bmin = bmid, bmax = bmid;
71         long ec, d, i1, i2, prev1, best, dd, v, k;
72
73         /*
74          * Set initial diagonal values for both forward and backward path.
75          */
76         kvdf[fmid] = off1;
77         kvdb[bmid] = lim1;
78
79         for (ec = 1;; ec++) {
80                 int got_snake = 0;
81
82                 /*
83                  * We need to extent the diagonal "domain" by one. If the next
84                  * values exits the box boundaries we need to change it in the
85                  * opposite direction because (max - min) must be a power of two.
86                  * Also we initialize the external K value to -1 so that we can
87                  * avoid extra conditions check inside the core loop.
88                  */
89                 if (fmin > dmin)
90                         kvdf[--fmin - 1] = -1;
91                 else
92                         ++fmin;
93                 if (fmax < dmax)
94                         kvdf[++fmax + 1] = -1;
95                 else
96                         --fmax;
97
98                 for (d = fmax; d >= fmin; d -= 2) {
99                         if (kvdf[d - 1] >= kvdf[d + 1])
100                                 i1 = kvdf[d - 1] + 1;
101                         else
102                                 i1 = kvdf[d + 1];
103                         prev1 = i1;
104                         i2 = i1 - d;
105                         for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++);
106                         if (i1 - prev1 > xenv->snake_cnt)
107                                 got_snake = 1;
108                         kvdf[d] = i1;
109                         if (odd && bmin <= d && d <= bmax && kvdb[d] <= i1) {
110                                 spl->i1 = i1;
111                                 spl->i2 = i2;
112                                 spl->min_lo = spl->min_hi = 1;
113                                 return ec;
114                         }
115                 }
116
117                 /*
118                  * We need to extent the diagonal "domain" by one. If the next
119                  * values exits the box boundaries we need to change it in the
120                  * opposite direction because (max - min) must be a power of two.
121                  * Also we initialize the external K value to -1 so that we can
122                  * avoid extra conditions check inside the core loop.
123                  */
124                 if (bmin > dmin)
125                         kvdb[--bmin - 1] = XDL_LINE_MAX;
126                 else
127                         ++bmin;
128                 if (bmax < dmax)
129                         kvdb[++bmax + 1] = XDL_LINE_MAX;
130                 else
131                         --bmax;
132
133                 for (d = bmax; d >= bmin; d -= 2) {
134                         if (kvdb[d - 1] < kvdb[d + 1])
135                                 i1 = kvdb[d - 1];
136                         else
137                                 i1 = kvdb[d + 1] - 1;
138                         prev1 = i1;
139                         i2 = i1 - d;
140                         for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--);
141                         if (prev1 - i1 > xenv->snake_cnt)
142                                 got_snake = 1;
143                         kvdb[d] = i1;
144                         if (!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) {
145                                 spl->i1 = i1;
146                                 spl->i2 = i2;
147                                 spl->min_lo = spl->min_hi = 1;
148                                 return ec;
149                         }
150                 }
151
152                 if (need_min)
153                         continue;
154
155                 /*
156                  * If the edit cost is above the heuristic trigger and if
157                  * we got a good snake, we sample current diagonals to see
158                  * if some of the, have reached an "interesting" path. Our
159                  * measure is a function of the distance from the diagonal
160                  * corner (i1 + i2) penalized with the distance from the
161                  * mid diagonal itself. If this value is above the current
162                  * edit cost times a magic factor (XDL_K_HEUR) we consider
163                  * it interesting.
164                  */
165                 if (got_snake && ec > xenv->heur_min) {
166                         for (best = 0, d = fmax; d >= fmin; d -= 2) {
167                                 dd = d > fmid ? d - fmid: fmid - d;
168                                 i1 = kvdf[d];
169                                 i2 = i1 - d;
170                                 v = (i1 - off1) + (i2 - off2) - dd;
171
172                                 if (v > XDL_K_HEUR * ec && v > best &&
173                                     off1 + xenv->snake_cnt <= i1 && i1 < lim1 &&
174                                     off2 + xenv->snake_cnt <= i2 && i2 < lim2) {
175                                         for (k = 1; ha1[i1 - k] == ha2[i2 - k]; k++)
176                                                 if (k == xenv->snake_cnt) {
177                                                         best = v;
178                                                         spl->i1 = i1;
179                                                         spl->i2 = i2;
180                                                         break;
181                                                 }
182                                 }
183                         }
184                         if (best > 0) {
185                                 spl->min_lo = 1;
186                                 spl->min_hi = 0;
187                                 return ec;
188                         }
189
190                         for (best = 0, d = bmax; d >= bmin; d -= 2) {
191                                 dd = d > bmid ? d - bmid: bmid - d;
192                                 i1 = kvdb[d];
193                                 i2 = i1 - d;
194                                 v = (lim1 - i1) + (lim2 - i2) - dd;
195
196                                 if (v > XDL_K_HEUR * ec && v > best &&
197                                     off1 < i1 && i1 <= lim1 - xenv->snake_cnt &&
198                                     off2 < i2 && i2 <= lim2 - xenv->snake_cnt) {
199                                         for (k = 0; ha1[i1 + k] == ha2[i2 + k]; k++)
200                                                 if (k == xenv->snake_cnt - 1) {
201                                                         best = v;
202                                                         spl->i1 = i1;
203                                                         spl->i2 = i2;
204                                                         break;
205                                                 }
206                                 }
207                         }
208                         if (best > 0) {
209                                 spl->min_lo = 0;
210                                 spl->min_hi = 1;
211                                 return ec;
212                         }
213                 }
214
215                 /*
216                  * Enough is enough. We spent too much time here and now we collect
217                  * the furthest reaching path using the (i1 + i2) measure.
218                  */
219                 if (ec >= xenv->mxcost) {
220                         long fbest, fbest1, bbest, bbest1;
221
222                         fbest = fbest1 = -1;
223                         for (d = fmax; d >= fmin; d -= 2) {
224                                 i1 = XDL_MIN(kvdf[d], lim1);
225                                 i2 = i1 - d;
226                                 if (lim2 < i2)
227                                         i1 = lim2 + d, i2 = lim2;
228                                 if (fbest < i1 + i2) {
229                                         fbest = i1 + i2;
230                                         fbest1 = i1;
231                                 }
232                         }
233
234                         bbest = bbest1 = XDL_LINE_MAX;
235                         for (d = bmax; d >= bmin; d -= 2) {
236                                 i1 = XDL_MAX(off1, kvdb[d]);
237                                 i2 = i1 - d;
238                                 if (i2 < off2)
239                                         i1 = off2 + d, i2 = off2;
240                                 if (i1 + i2 < bbest) {
241                                         bbest = i1 + i2;
242                                         bbest1 = i1;
243                                 }
244                         }
245
246                         if ((lim1 + lim2) - bbest < fbest - (off1 + off2)) {
247                                 spl->i1 = fbest1;
248                                 spl->i2 = fbest - fbest1;
249                                 spl->min_lo = 1;
250                                 spl->min_hi = 0;
251                         } else {
252                                 spl->i1 = bbest1;
253                                 spl->i2 = bbest - bbest1;
254                                 spl->min_lo = 0;
255                                 spl->min_hi = 1;
256                         }
257                         return ec;
258                 }
259         }
260
261         return -1;
262 }
263
264
265 /*
266  * Rule: "Divide et Impera". Recursively split the box in sub-boxes by calling
267  * the box splitting function. Note that the real job (marking changed lines)
268  * is done in the two boundary reaching checks.
269  */
270 int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1,
271                  diffdata_t *dd2, long off2, long lim2,
272                  long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv) {
273         unsigned long const *ha1 = dd1->ha, *ha2 = dd2->ha;
274
275         /*
276          * Shrink the box by walking through each diagonal snake (SW and NE).
277          */
278         for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++);
279         for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--);
280
281         /*
282          * If one dimension is empty, then all records on the other one must
283          * be obviously changed.
284          */
285         if (off1 == lim1) {
286                 char *rchg2 = dd2->rchg;
287                 long *rindex2 = dd2->rindex;
288
289                 for (; off2 < lim2; off2++)
290                         rchg2[rindex2[off2]] = 1;
291         } else if (off2 == lim2) {
292                 char *rchg1 = dd1->rchg;
293                 long *rindex1 = dd1->rindex;
294
295                 for (; off1 < lim1; off1++)
296                         rchg1[rindex1[off1]] = 1;
297         } else {
298                 long ec;
299                 xdpsplit_t spl;
300                 spl.i1 = spl.i2 = 0;
301
302                 /*
303                  * Divide ...
304                  */
305                 if ((ec = xdl_split(ha1, off1, lim1, ha2, off2, lim2, kvdf, kvdb,
306                                     need_min, &spl, xenv)) < 0) {
307
308                         return -1;
309                 }
310
311                 /*
312                  * ... et Impera.
313                  */
314                 if (xdl_recs_cmp(dd1, off1, spl.i1, dd2, off2, spl.i2,
315                                  kvdf, kvdb, spl.min_lo, xenv) < 0 ||
316                     xdl_recs_cmp(dd1, spl.i1, lim1, dd2, spl.i2, lim2,
317                                  kvdf, kvdb, spl.min_hi, xenv) < 0) {
318
319                         return -1;
320                 }
321         }
322
323         return 0;
324 }
325
326
327 int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
328                 xdfenv_t *xe) {
329         long ndiags;
330         long *kvd, *kvdf, *kvdb;
331         xdalgoenv_t xenv;
332         diffdata_t dd1, dd2;
333
334         if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) {
335
336                 return -1;
337         }
338
339         /*
340          * Allocate and setup K vectors to be used by the differential algorithm.
341          * One is to store the forward path and one to store the backward path.
342          */
343         ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3;
344         if (!(kvd = (long *) xdl_malloc((2 * ndiags + 2) * sizeof(long)))) {
345
346                 xdl_free_env(xe);
347                 return -1;
348         }
349         kvdf = kvd;
350         kvdb = kvdf + ndiags;
351         kvdf += xe->xdf2.nreff + 1;
352         kvdb += xe->xdf2.nreff + 1;
353
354         xenv.mxcost = xdl_bogosqrt(ndiags);
355         if (xenv.mxcost < XDL_MAX_COST_MIN)
356                 xenv.mxcost = XDL_MAX_COST_MIN;
357         xenv.snake_cnt = XDL_SNAKE_CNT;
358         xenv.heur_min = XDL_HEUR_MIN_COST;
359
360         dd1.nrec = xe->xdf1.nreff;
361         dd1.ha = xe->xdf1.ha;
362         dd1.rchg = xe->xdf1.rchg;
363         dd1.rindex = xe->xdf1.rindex;
364         dd2.nrec = xe->xdf2.nreff;
365         dd2.ha = xe->xdf2.ha;
366         dd2.rchg = xe->xdf2.rchg;
367         dd2.rindex = xe->xdf2.rindex;
368
369         if (xdl_recs_cmp(&dd1, 0, dd1.nrec, &dd2, 0, dd2.nrec,
370                          kvdf, kvdb, (xpp->flags & XDF_NEED_MINIMAL) != 0, &xenv) < 0) {
371
372                 xdl_free(kvd);
373                 xdl_free_env(xe);
374                 return -1;
375         }
376
377         xdl_free(kvd);
378
379         return 0;
380 }
381
382
383 static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2) {
384         xdchange_t *xch;
385
386         if (!(xch = (xdchange_t *) xdl_malloc(sizeof(xdchange_t))))
387                 return NULL;
388
389         xch->next = xscr;
390         xch->i1 = i1;
391         xch->i2 = i2;
392         xch->chg1 = chg1;
393         xch->chg2 = chg2;
394
395         return xch;
396 }
397
398
399 int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
400         long ix, ixo, ixs, ixref, grpsiz, nrec = xdf->nrec;
401         char *rchg = xdf->rchg, *rchgo = xdfo->rchg;
402         xrecord_t **recs = xdf->recs;
403
404         /*
405          * This is the same of what GNU diff does. Move back and forward
406          * change groups for a consistent and pretty diff output. This also
407          * helps in finding joinable change groups and reduce the diff size.
408          */
409         for (ix = ixo = 0;;) {
410                 /*
411                  * Find the first changed line in the to-be-compacted file.
412                  * We need to keep track of both indexes, so if we find a
413                  * changed lines group on the other file, while scanning the
414                  * to-be-compacted file, we need to skip it properly. Note
415                  * that loops that are testing for changed lines on rchg* do
416                  * not need index bounding since the array is prepared with
417                  * a zero at position -1 and N.
418                  */
419                 for (; ix < nrec && !rchg[ix]; ix++)
420                         while (rchgo[ixo++]);
421                 if (ix == nrec)
422                         break;
423
424                 /*
425                  * Record the start of a changed-group in the to-be-compacted file
426                  * and find the end of it, on both to-be-compacted and other file
427                  * indexes (ix and ixo).
428                  */
429                 ixs = ix;
430                 for (ix++; rchg[ix]; ix++);
431                 for (; rchgo[ixo]; ixo++);
432
433                 do {
434                         grpsiz = ix - ixs;
435
436                         /*
437                          * If the line before the current change group, is equal to
438                          * the last line of the current change group, shift backward
439                          * the group.
440                          */
441                         while (ixs > 0 && recs[ixs - 1]->ha == recs[ix - 1]->ha &&
442                                xdl_recmatch(recs[ixs - 1]->ptr, recs[ixs - 1]->size, recs[ix - 1]->ptr, recs[ix - 1]->size, flags)) {
443                                 rchg[--ixs] = 1;
444                                 rchg[--ix] = 0;
445
446                                 /*
447                                  * This change might have joined two change groups,
448                                  * so we try to take this scenario in account by moving
449                                  * the start index accordingly (and so the other-file
450                                  * end-of-group index).
451                                  */
452                                 for (; rchg[ixs - 1]; ixs--);
453                                 while (rchgo[--ixo]);
454                         }
455
456                         /*
457                          * Record the end-of-group position in case we are matched
458                          * with a group of changes in the other file (that is, the
459                          * change record before the enf-of-group index in the other
460                          * file is set).
461                          */
462                         ixref = rchgo[ixo - 1] ? ix: nrec;
463
464                         /*
465                          * If the first line of the current change group, is equal to
466                          * the line next of the current change group, shift forward
467                          * the group.
468                          */
469                         while (ix < nrec && recs[ixs]->ha == recs[ix]->ha &&
470                                xdl_recmatch(recs[ixs]->ptr, recs[ixs]->size, recs[ix]->ptr, recs[ix]->size, flags)) {
471                                 rchg[ixs++] = 0;
472                                 rchg[ix++] = 1;
473
474                                 /*
475                                  * This change might have joined two change groups,
476                                  * so we try to take this scenario in account by moving
477                                  * the start index accordingly (and so the other-file
478                                  * end-of-group index). Keep tracking the reference
479                                  * index in case we are shifting together with a
480                                  * corresponding group of changes in the other file.
481                                  */
482                                 for (; rchg[ix]; ix++);
483                                 while (rchgo[++ixo])
484                                         ixref = ix;
485                         }
486                 } while (grpsiz != ix - ixs);
487
488                 /*
489                  * Try to move back the possibly merged group of changes, to match
490                  * the recorded postion in the other file.
491                  */
492                 while (ixref < ix) {
493                         rchg[--ixs] = 1;
494                         rchg[--ix] = 0;
495                         while (rchgo[--ixo]);
496                 }
497         }
498
499         return 0;
500 }
501
502
503 int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr) {
504         xdchange_t *cscr = NULL, *xch;
505         char *rchg1 = xe->xdf1.rchg, *rchg2 = xe->xdf2.rchg;
506         long i1, i2, l1, l2;
507
508         /*
509          * Trivial. Collects "groups" of changes and creates an edit script.
510          */
511         for (i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >= 0 || i2 >= 0; i1--, i2--)
512                 if (rchg1[i1 - 1] || rchg2[i2 - 1]) {
513                         for (l1 = i1; rchg1[i1 - 1]; i1--);
514                         for (l2 = i2; rchg2[i2 - 1]; i2--);
515
516                         if (!(xch = xdl_add_change(cscr, i1, i2, l1 - i1, l2 - i2))) {
517                                 xdl_free_script(cscr);
518                                 return -1;
519                         }
520                         cscr = xch;
521                 }
522
523         *xscr = cscr;
524
525         return 0;
526 }
527
528
529 void xdl_free_script(xdchange_t *xscr) {
530         xdchange_t *xch;
531
532         while ((xch = xscr) != NULL) {
533                 xscr = xscr->next;
534                 xdl_free(xch);
535         }
536 }
537
538
539 int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
540              xdemitconf_t const *xecfg, xdemitcb_t *ecb) {
541         xdchange_t *xscr;
542         xdfenv_t xe;
543
544         if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) {
545
546                 return -1;
547         }
548         if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 ||
549             xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 ||
550             xdl_build_script(&xe, &xscr) < 0) {
551
552                 xdl_free_env(&xe);
553                 return -1;
554         }
555         if (xscr) {
556                 if (xdl_emit_diff(&xe, xscr, ecb, xecfg) < 0) {
557
558                         xdl_free_script(xscr);
559                         xdl_free_env(&xe);
560                         return -1;
561                 }
562                 xdl_free_script(xscr);
563         }
564         xdl_free_env(&xe);
565
566         return 0;
567 }