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>
26 #define XDL_KPDIS_RUN 4
27 #define XDL_MAX_EQLIMIT 1024
28 #define XDL_SIMSCAN_WINDOW 100
31 typedef struct s_xdlclass {
32 struct s_xdlclass *next;
40 typedef struct s_xdlclassifier {
54 static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags);
55 static void xdl_free_classifier(xdlclassifier_t *cf);
56 static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
57 unsigned int hbits, xrecord_t *rec);
58 static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
59 xdlclassifier_t *cf, xdfile_t *xdf);
60 static void xdl_free_ctx(xdfile_t *xdf);
61 static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
62 static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
63 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
64 static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
69 static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
74 cf->hbits = xdl_hashbits((unsigned int) size);
75 cf->hsize = 1 << cf->hbits;
77 if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) {
81 if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) {
83 xdl_cha_free(&cf->ncha);
86 for (i = 0; i < cf->hsize; i++)
90 if (!(cf->rcrecs = (xdlclass_t **) xdl_malloc(cf->alloc * sizeof(xdlclass_t *)))) {
93 xdl_cha_free(&cf->ncha);
103 static void xdl_free_classifier(xdlclassifier_t *cf) {
105 xdl_free(cf->rcrecs);
106 xdl_free(cf->rchash);
107 xdl_cha_free(&cf->ncha);
111 static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
112 unsigned int hbits, xrecord_t *rec) {
119 hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
120 for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
121 if (rcrec->ha == rec->ha &&
122 xdl_recmatch(rcrec->line, rcrec->size,
123 rec->ptr, rec->size, cf->flags))
127 if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
131 rcrec->idx = cf->count++;
132 if (cf->count > cf->alloc) {
134 if (!(rcrecs = (xdlclass_t **) xdl_realloc(cf->rcrecs, cf->alloc * sizeof(xdlclass_t *)))) {
140 cf->rcrecs[rcrec->idx] = rcrec;
142 rcrec->size = rec->size;
144 rcrec->len1 = rcrec->len2 = 0;
145 rcrec->next = cf->rchash[hi];
146 cf->rchash[hi] = rcrec;
149 (pass == 1) ? rcrec->len1++ : rcrec->len2++;
151 rec->ha = (unsigned long) rcrec->idx;
153 hi = (long) XDL_HASHLONG(rec->ha, hbits);
154 rec->next = rhash[hi];
161 static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
162 xdlclassifier_t *cf, xdfile_t *xdf) {
164 long i, nrec, hsize, bsize;
166 char const *blk, *cur, *top, *prev;
168 xrecord_t **recs, **rrecs;
174 if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) {
178 if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) {
180 xdl_cha_free(&xdf->rcha);
184 hbits = xdl_hashbits((unsigned int) narec);
186 if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) {
189 xdl_cha_free(&xdf->rcha);
192 for (i = 0; i < hsize; i++)
196 if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) {
197 for (top = blk + bsize;;) {
199 if (!(cur = blk = xdl_mmfile_next(mf, &bsize)))
204 hav = xdl_hash_record(&cur, top, xpp->flags);
207 if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) {
211 xdl_cha_free(&xdf->rcha);
216 if (!(crec = xdl_cha_alloc(&xdf->rcha))) {
220 xdl_cha_free(&xdf->rcha);
224 crec->size = (long) (cur - prev);
228 if (xdl_classify_record(pass, cf, rhash, hbits, crec) < 0) {
232 xdl_cha_free(&xdf->rcha);
238 if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) {
242 xdl_cha_free(&xdf->rcha);
245 memset(rchg, 0, (nrec + 2) * sizeof(char));
247 if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) {
252 xdl_cha_free(&xdf->rcha);
255 if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) {
261 xdl_cha_free(&xdf->rcha);
269 xdf->rchg = rchg + 1;
270 xdf->rindex = rindex;
274 xdf->dend = nrec - 1;
280 static void xdl_free_ctx(xdfile_t *xdf) {
282 xdl_free(xdf->rhash);
283 xdl_free(xdf->rindex);
284 xdl_free(xdf->rchg - 1);
287 xdl_cha_free(&xdf->rcha);
291 int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
296 enl1 = xdl_guess_lines(mf1) + 1;
297 enl2 = xdl_guess_lines(mf2) + 1;
299 if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) {
304 if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
306 xdl_free_classifier(&cf);
309 if (xdl_prepare_ctx(2, mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
311 xdl_free_ctx(&xe->xdf1);
312 xdl_free_classifier(&cf);
316 if (!(xpp->flags & XDF_PATIENCE_DIFF) &&
317 xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) {
319 xdl_free_ctx(&xe->xdf2);
320 xdl_free_ctx(&xe->xdf1);
324 xdl_free_classifier(&cf);
330 void xdl_free_env(xdfenv_t *xe) {
332 xdl_free_ctx(&xe->xdf2);
333 xdl_free_ctx(&xe->xdf1);
337 static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
338 long r, rdis0, rpdis0, rdis1, rpdis1;
341 * Limits the window the is examined during the similar-lines
342 * scan. The loops below stops when dis[i - r] == 1 (line that
343 * has no match), but there are corner cases where the loop
344 * proceed all the way to the extremities by causing huge
345 * performance penalties in case of big files.
347 if (i - s > XDL_SIMSCAN_WINDOW)
348 s = i - XDL_SIMSCAN_WINDOW;
349 if (e - i > XDL_SIMSCAN_WINDOW)
350 e = i + XDL_SIMSCAN_WINDOW;
353 * Scans the lines before 'i' to find a run of lines that either
354 * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1).
355 * Note that we always call this function with dis[i] > 1, so the
356 * current line (i) is already a multimatch line.
358 for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
361 else if (dis[i - r] == 2)
367 * If the run before the line 'i' found only multimatch lines, we
368 * return 0 and hence we don't make the current line (i) discarded.
369 * We want to discard multimatch lines only when they appear in the
370 * middle of runs with nomatch lines (dis[j] == 0).
374 for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
377 else if (dis[i + r] == 2)
383 * If the run after the line 'i' found only multimatch lines, we
384 * return 0 and hence we don't make the current line (i) discarded.
391 return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
396 * Try to reduce the problem complexity, discard records that have no
397 * matches on the other file. Also, lines that have multiple matches
398 * might be potentially discarded if they happear in a run of discardable.
400 static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
401 long i, nm, nreff, mlim;
404 char *dis, *dis1, *dis2;
406 if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
410 memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
412 dis2 = dis1 + xdf1->nrec + 1;
414 if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT)
415 mlim = XDL_MAX_EQLIMIT;
416 for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
417 rcrec = cf->rcrecs[(*recs)->ha];
418 nm = rcrec ? rcrec->len2 : 0;
419 dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
422 if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT)
423 mlim = XDL_MAX_EQLIMIT;
424 for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
425 rcrec = cf->rcrecs[(*recs)->ha];
426 nm = rcrec ? rcrec->len1 : 0;
427 dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
430 for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
431 i <= xdf1->dend; i++, recs++) {
433 (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
434 xdf1->rindex[nreff] = i;
435 xdf1->ha[nreff] = (*recs)->ha;
442 for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
443 i <= xdf2->dend; i++, recs++) {
445 (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
446 xdf2->rindex[nreff] = i;
447 xdf2->ha[nreff] = (*recs)->ha;
461 * Early trim initial and terminal matching records.
463 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
465 xrecord_t **recs1, **recs2;
469 for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
470 i++, recs1++, recs2++)
471 if ((*recs1)->ha != (*recs2)->ha)
474 xdf1->dstart = xdf2->dstart = i;
476 recs1 = xdf1->recs + xdf1->nrec - 1;
477 recs2 = xdf2->recs + xdf2->nrec - 1;
478 for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
479 if ((*recs1)->ha != (*recs2)->ha)
482 xdf1->dend = xdf1->nrec - i - 1;
483 xdf2->dend = xdf2->nrec - i - 1;
489 static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
491 if (xdl_trim_ends(xdf1, xdf2) < 0 ||
492 xdl_cleanup_records(cf, xdf1, xdf2) < 0) {