log-tree: separate major part of diff-tree.
[git/dscho.git] / xdiff / xdiffi.c
blob641362d056edb61ef38b43dd2706bbdecb2c4863
1 /*
2 * LibXDiff by Davide Libenzi ( File Differential Library )
3 * Copyright (C) 2003 Davide Libenzi
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.
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.
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
19 * Davide Libenzi <davidel@xmailserver.org>
23 #include "xinclude.h"
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
35 typedef struct s_xdpsplit {
36 long i1, i2;
37 int min_lo, min_hi;
38 } xdpsplit_t;
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);
53 * See "An O(ND) Difference Algorithm and its Variations", by Eugene Myers.
54 * Basically considers a "box" (off1, off2, lim1, lim2) and scan from both
55 * the forward diagonal starting from (off1, off2) and the backward diagonal
56 * starting from (lim1, lim2). If the K values on the same diagonal crosses
57 * returns the furthest point of reach. We might end up having to expensive
58 * cases using this algorithm is full, so a little bit of heuristic is needed
59 * to cut the search and to return a suboptimal point.
61 static long xdl_split(unsigned long const *ha1, long off1, long lim1,
62 unsigned long const *ha2, long off2, long lim2,
63 long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl,
64 xdalgoenv_t *xenv) {
65 long dmin = off1 - lim2, dmax = lim1 - off2;
66 long fmid = off1 - off2, bmid = lim1 - lim2;
67 long odd = (fmid - bmid) & 1;
68 long fmin = fmid, fmax = fmid;
69 long bmin = bmid, bmax = bmid;
70 long ec, d, i1, i2, prev1, best, dd, v, k;
73 * Set initial diagonal values for both forward and backward path.
75 kvdf[fmid] = off1;
76 kvdb[bmid] = lim1;
78 for (ec = 1;; ec++) {
79 int got_snake = 0;
82 * We need to extent the diagonal "domain" by one. If the next
83 * values exits the box boundaries we need to change it in the
84 * opposite direction because (max - min) must be a power of two.
85 * Also we initialize the extenal K value to -1 so that we can
86 * avoid extra conditions check inside the core loop.
88 if (fmin > dmin)
89 kvdf[--fmin - 1] = -1;
90 else
91 ++fmin;
92 if (fmax < dmax)
93 kvdf[++fmax + 1] = -1;
94 else
95 --fmax;
97 for (d = fmax; d >= fmin; d -= 2) {
98 if (kvdf[d - 1] >= kvdf[d + 1])
99 i1 = kvdf[d - 1] + 1;
100 else
101 i1 = kvdf[d + 1];
102 prev1 = i1;
103 i2 = i1 - d;
104 for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++);
105 if (i1 - prev1 > xenv->snake_cnt)
106 got_snake = 1;
107 kvdf[d] = i1;
108 if (odd && bmin <= d && d <= bmax && kvdb[d] <= i1) {
109 spl->i1 = i1;
110 spl->i2 = i2;
111 spl->min_lo = spl->min_hi = 1;
112 return ec;
117 * We need to extent the diagonal "domain" by one. If the next
118 * values exits the box boundaries we need to change it in the
119 * opposite direction because (max - min) must be a power of two.
120 * Also we initialize the extenal K value to -1 so that we can
121 * avoid extra conditions check inside the core loop.
123 if (bmin > dmin)
124 kvdb[--bmin - 1] = XDL_LINE_MAX;
125 else
126 ++bmin;
127 if (bmax < dmax)
128 kvdb[++bmax + 1] = XDL_LINE_MAX;
129 else
130 --bmax;
132 for (d = bmax; d >= bmin; d -= 2) {
133 if (kvdb[d - 1] < kvdb[d + 1])
134 i1 = kvdb[d - 1];
135 else
136 i1 = kvdb[d + 1] - 1;
137 prev1 = i1;
138 i2 = i1 - d;
139 for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--);
140 if (prev1 - i1 > xenv->snake_cnt)
141 got_snake = 1;
142 kvdb[d] = i1;
143 if (!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) {
144 spl->i1 = i1;
145 spl->i2 = i2;
146 spl->min_lo = spl->min_hi = 1;
147 return ec;
151 if (need_min)
152 continue;
155 * If the edit cost is above the heuristic trigger and if
156 * we got a good snake, we sample current diagonals to see
157 * if some of the, have reached an "interesting" path. Our
158 * measure is a function of the distance from the diagonal
159 * corner (i1 + i2) penalized with the distance from the
160 * mid diagonal itself. If this value is above the current
161 * edit cost times a magic factor (XDL_K_HEUR) we consider
162 * it interesting.
164 if (got_snake && ec > xenv->heur_min) {
165 for (best = 0, d = fmax; d >= fmin; d -= 2) {
166 dd = d > fmid ? d - fmid: fmid - d;
167 i1 = kvdf[d];
168 i2 = i1 - d;
169 v = (i1 - off1) + (i2 - off2) - dd;
171 if (v > XDL_K_HEUR * ec && v > best &&
172 off1 + xenv->snake_cnt <= i1 && i1 < lim1 &&
173 off2 + xenv->snake_cnt <= i2 && i2 < lim2) {
174 for (k = 1; ha1[i1 - k] == ha2[i2 - k]; k++)
175 if (k == xenv->snake_cnt) {
176 best = v;
177 spl->i1 = i1;
178 spl->i2 = i2;
179 break;
183 if (best > 0) {
184 spl->min_lo = 1;
185 spl->min_hi = 0;
186 return ec;
189 for (best = 0, d = bmax; d >= bmin; d -= 2) {
190 dd = d > bmid ? d - bmid: bmid - d;
191 i1 = kvdb[d];
192 i2 = i1 - d;
193 v = (lim1 - i1) + (lim2 - i2) - dd;
195 if (v > XDL_K_HEUR * ec && v > best &&
196 off1 < i1 && i1 <= lim1 - xenv->snake_cnt &&
197 off2 < i2 && i2 <= lim2 - xenv->snake_cnt) {
198 for (k = 0; ha1[i1 + k] == ha2[i2 + k]; k++)
199 if (k == xenv->snake_cnt - 1) {
200 best = v;
201 spl->i1 = i1;
202 spl->i2 = i2;
203 break;
207 if (best > 0) {
208 spl->min_lo = 0;
209 spl->min_hi = 1;
210 return ec;
215 * Enough is enough. We spent too much time here and now we collect
216 * the furthest reaching path using the (i1 + i2) measure.
218 if (ec >= xenv->mxcost) {
219 long fbest, fbest1, bbest, bbest1;
221 fbest = fbest1 = -1;
222 for (d = fmax; d >= fmin; d -= 2) {
223 i1 = XDL_MIN(kvdf[d], lim1);
224 i2 = i1 - d;
225 if (lim2 < i2)
226 i1 = lim2 + d, i2 = lim2;
227 if (fbest < i1 + i2) {
228 fbest = i1 + i2;
229 fbest1 = i1;
233 bbest = bbest1 = XDL_LINE_MAX;
234 for (d = bmax; d >= bmin; d -= 2) {
235 i1 = XDL_MAX(off1, kvdb[d]);
236 i2 = i1 - d;
237 if (i2 < off2)
238 i1 = off2 + d, i2 = off2;
239 if (i1 + i2 < bbest) {
240 bbest = i1 + i2;
241 bbest1 = i1;
245 if ((lim1 + lim2) - bbest < fbest - (off1 + off2)) {
246 spl->i1 = fbest1;
247 spl->i2 = fbest - fbest1;
248 spl->min_lo = 1;
249 spl->min_hi = 0;
250 } else {
251 spl->i1 = bbest1;
252 spl->i2 = bbest - bbest1;
253 spl->min_lo = 0;
254 spl->min_hi = 1;
256 return ec;
260 return -1;
265 * Rule: "Divide et Impera". Recursively split the box in sub-boxes by calling
266 * the box splitting function. Note that the real job (marking changed lines)
267 * is done in the two boundary reaching checks.
269 int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1,
270 diffdata_t *dd2, long off2, long lim2,
271 long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv) {
272 unsigned long const *ha1 = dd1->ha, *ha2 = dd2->ha;
275 * Shrink the box by walking through each diagonal snake (SW and NE).
277 for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++);
278 for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--);
281 * If one dimension is empty, then all records on the other one must
282 * be obviously changed.
284 if (off1 == lim1) {
285 char *rchg2 = dd2->rchg;
286 long *rindex2 = dd2->rindex;
288 for (; off2 < lim2; off2++)
289 rchg2[rindex2[off2]] = 1;
290 } else if (off2 == lim2) {
291 char *rchg1 = dd1->rchg;
292 long *rindex1 = dd1->rindex;
294 for (; off1 < lim1; off1++)
295 rchg1[rindex1[off1]] = 1;
296 } else {
297 long ec;
298 xdpsplit_t spl;
299 spl.i1 = spl.i2 = 0;
302 * Divide ...
304 if ((ec = xdl_split(ha1, off1, lim1, ha2, off2, lim2, kvdf, kvdb,
305 need_min, &spl, xenv)) < 0) {
307 return -1;
311 * ... et Impera.
313 if (xdl_recs_cmp(dd1, off1, spl.i1, dd2, off2, spl.i2,
314 kvdf, kvdb, spl.min_lo, xenv) < 0 ||
315 xdl_recs_cmp(dd1, spl.i1, lim1, dd2, spl.i2, lim2,
316 kvdf, kvdb, spl.min_hi, xenv) < 0) {
318 return -1;
322 return 0;
326 int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
327 xdfenv_t *xe) {
328 long ndiags;
329 long *kvd, *kvdf, *kvdb;
330 xdalgoenv_t xenv;
331 diffdata_t dd1, dd2;
333 if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) {
335 return -1;
339 * Allocate and setup K vectors to be used by the differential algorithm.
340 * One is to store the forward path and one to store the backward path.
342 ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3;
343 if (!(kvd = (long *) xdl_malloc((2 * ndiags + 2) * sizeof(long)))) {
345 xdl_free_env(xe);
346 return -1;
348 kvdf = kvd;
349 kvdb = kvdf + ndiags;
350 kvdf += xe->xdf2.nreff + 1;
351 kvdb += xe->xdf2.nreff + 1;
353 xenv.mxcost = xdl_bogosqrt(ndiags);
354 if (xenv.mxcost < XDL_MAX_COST_MIN)
355 xenv.mxcost = XDL_MAX_COST_MIN;
356 xenv.snake_cnt = XDL_SNAKE_CNT;
357 xenv.heur_min = XDL_HEUR_MIN_COST;
359 dd1.nrec = xe->xdf1.nreff;
360 dd1.ha = xe->xdf1.ha;
361 dd1.rchg = xe->xdf1.rchg;
362 dd1.rindex = xe->xdf1.rindex;
363 dd2.nrec = xe->xdf2.nreff;
364 dd2.ha = xe->xdf2.ha;
365 dd2.rchg = xe->xdf2.rchg;
366 dd2.rindex = xe->xdf2.rindex;
368 if (xdl_recs_cmp(&dd1, 0, dd1.nrec, &dd2, 0, dd2.nrec,
369 kvdf, kvdb, (xpp->flags & XDF_NEED_MINIMAL) != 0, &xenv) < 0) {
371 xdl_free(kvd);
372 xdl_free_env(xe);
373 return -1;
376 xdl_free(kvd);
378 return 0;
382 static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2) {
383 xdchange_t *xch;
385 if (!(xch = (xdchange_t *) xdl_malloc(sizeof(xdchange_t))))
386 return NULL;
388 xch->next = xscr;
389 xch->i1 = i1;
390 xch->i2 = i2;
391 xch->chg1 = chg1;
392 xch->chg2 = chg2;
394 return xch;
398 int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr) {
399 xdchange_t *cscr = NULL, *xch;
400 char *rchg1 = xe->xdf1.rchg, *rchg2 = xe->xdf2.rchg;
401 long i1, i2, l1, l2;
404 * Trivial. Collects "groups" of changes and creates an edit script.
406 for (i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >= 0 || i2 >= 0; i1--, i2--)
407 if (rchg1[i1 - 1] || rchg2[i2 - 1]) {
408 for (l1 = i1; rchg1[i1 - 1]; i1--);
409 for (l2 = i2; rchg2[i2 - 1]; i2--);
411 if (!(xch = xdl_add_change(cscr, i1, i2, l1 - i1, l2 - i2))) {
412 xdl_free_script(cscr);
413 return -1;
415 cscr = xch;
418 *xscr = cscr;
420 return 0;
424 void xdl_free_script(xdchange_t *xscr) {
425 xdchange_t *xch;
427 while ((xch = xscr) != NULL) {
428 xscr = xscr->next;
429 xdl_free(xch);
434 int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
435 xdemitconf_t const *xecfg, xdemitcb_t *ecb) {
436 xdchange_t *xscr;
437 xdfenv_t xe;
439 if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) {
441 return -1;
444 if (xdl_build_script(&xe, &xscr) < 0) {
446 xdl_free_env(&xe);
447 return -1;
450 if (xscr) {
451 if (xdl_emit_diff(&xe, xscr, ecb, xecfg) < 0) {
453 xdl_free_script(xscr);
454 xdl_free_env(&xe);
455 return -1;
458 xdl_free_script(xscr);
461 xdl_free_env(&xe);
463 return 0;