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
29 #define XDL_GUESS_NLINES1 256
30 #define XDL_GUESS_NLINES2 20
33 typedef struct s_xdlclass {
34 struct s_xdlclass *next;
41 typedef struct s_xdlclassifier {
53 static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags);
54 static void xdl_free_classifier(xdlclassifier_t *cf);
55 static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
57 static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
58 xdlclassifier_t *cf, xdfile_t *xdf);
59 static void xdl_free_ctx(xdfile_t *xdf);
60 static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
61 static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2);
62 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
63 static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2);
68 static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
71 cf->hbits = xdl_hashbits((unsigned int) size);
72 cf->hsize = 1 << cf->hbits;
74 if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) {
78 if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) {
80 xdl_cha_free(&cf->ncha);
83 memset(cf->rchash, 0, cf->hsize * sizeof(xdlclass_t *));
91 static void xdl_free_classifier(xdlclassifier_t *cf) {
94 xdl_cha_free(&cf->ncha);
98 static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
105 hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
106 for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
107 if (rcrec->ha == rec->ha &&
108 xdl_recmatch(rcrec->line, rcrec->size,
109 rec->ptr, rec->size, cf->flags))
113 if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
117 rcrec->idx = cf->count++;
119 rcrec->size = rec->size;
121 rcrec->next = cf->rchash[hi];
122 cf->rchash[hi] = rcrec;
125 rec->ha = (unsigned long) rcrec->idx;
127 hi = (long) XDL_HASHLONG(rec->ha, hbits);
128 rec->next = rhash[hi];
135 static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
136 xdlclassifier_t *cf, xdfile_t *xdf) {
138 long nrec, hsize, bsize;
140 char const *blk, *cur, *top, *prev;
142 xrecord_t **recs, **rrecs;
154 if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0)
156 if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *))))
159 if (xpp->flags & XDF_HISTOGRAM_DIFF)
162 hbits = xdl_hashbits((unsigned int) narec);
164 if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *))))
166 memset(rhash, 0, hsize * sizeof(xrecord_t *));
170 if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) {
171 for (top = blk + bsize; cur < top; ) {
173 hav = xdl_hash_record(&cur, top, xpp->flags);
176 if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *))))
180 if (!(crec = xdl_cha_alloc(&xdf->rcha)))
183 crec->size = (long) (cur - prev);
187 if (!(xpp->flags & XDF_HISTOGRAM_DIFF) &&
188 xdl_classify_record(cf, rhash, hbits, crec) < 0)
193 if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char))))
195 memset(rchg, 0, (nrec + 2) * sizeof(char));
197 if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long))))
199 if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long))))
206 xdf->rchg = rchg + 1;
207 xdf->rindex = rindex;
211 xdf->dend = nrec - 1;
221 xdl_cha_free(&xdf->rcha);
226 static void xdl_free_ctx(xdfile_t *xdf) {
228 xdl_free(xdf->rhash);
229 xdl_free(xdf->rindex);
230 xdl_free(xdf->rchg - 1);
233 xdl_cha_free(&xdf->rcha);
237 int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
239 long enl1, enl2, sample;
243 * For histogram diff, we can afford a smaller sample size and
244 * thus a poorer estimate of the number of lines, as the hash
245 * table (rhash) won't be filled up/grown. The number of lines
246 * (nrecs) will be updated correctly anyway by
249 sample = xpp->flags & XDF_HISTOGRAM_DIFF ? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1;
251 enl1 = xdl_guess_lines(mf1, sample) + 1;
252 enl2 = xdl_guess_lines(mf2, sample) + 1;
254 if (!(xpp->flags & XDF_HISTOGRAM_DIFF) &&
255 xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) {
260 if (xdl_prepare_ctx(mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
262 xdl_free_classifier(&cf);
265 if (xdl_prepare_ctx(mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
267 xdl_free_ctx(&xe->xdf1);
268 xdl_free_classifier(&cf);
272 if (!(xpp->flags & XDF_HISTOGRAM_DIFF))
273 xdl_free_classifier(&cf);
275 if (!(xpp->flags & XDF_PATIENCE_DIFF) &&
276 !(xpp->flags & XDF_HISTOGRAM_DIFF) &&
277 xdl_optimize_ctxs(&xe->xdf1, &xe->xdf2) < 0) {
279 xdl_free_ctx(&xe->xdf2);
280 xdl_free_ctx(&xe->xdf1);
288 void xdl_free_env(xdfenv_t *xe) {
290 xdl_free_ctx(&xe->xdf2);
291 xdl_free_ctx(&xe->xdf1);
295 static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
296 long r, rdis0, rpdis0, rdis1, rpdis1;
299 * Limits the window the is examined during the similar-lines
300 * scan. The loops below stops when dis[i - r] == 1 (line that
301 * has no match), but there are corner cases where the loop
302 * proceed all the way to the extremities by causing huge
303 * performance penalties in case of big files.
305 if (i - s > XDL_SIMSCAN_WINDOW)
306 s = i - XDL_SIMSCAN_WINDOW;
307 if (e - i > XDL_SIMSCAN_WINDOW)
308 e = i + XDL_SIMSCAN_WINDOW;
311 * Scans the lines before 'i' to find a run of lines that either
312 * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1).
313 * Note that we always call this function with dis[i] > 1, so the
314 * current line (i) is already a multimatch line.
316 for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
319 else if (dis[i - r] == 2)
325 * If the run before the line 'i' found only multimatch lines, we
326 * return 0 and hence we don't make the current line (i) discarded.
327 * We want to discard multimatch lines only when they appear in the
328 * middle of runs with nomatch lines (dis[j] == 0).
332 for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
335 else if (dis[i + r] == 2)
341 * If the run after the line 'i' found only multimatch lines, we
342 * return 0 and hence we don't make the current line (i) discarded.
349 return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
354 * Try to reduce the problem complexity, discard records that have no
355 * matches on the other file. Also, lines that have multiple matches
356 * might be potentially discarded if they happear in a run of discardable.
358 static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2) {
359 long i, nm, rhi, nreff, mlim;
363 char *dis, *dis1, *dis2;
365 if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
369 memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
371 dis2 = dis1 + xdf1->nrec + 1;
373 if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT)
374 mlim = XDL_MAX_EQLIMIT;
375 for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
377 rhi = (long) XDL_HASHLONG(hav, xdf2->hbits);
378 for (nm = 0, rec = xdf2->rhash[rhi]; rec; rec = rec->next)
379 if (rec->ha == hav && ++nm == mlim)
381 dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
384 if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT)
385 mlim = XDL_MAX_EQLIMIT;
386 for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
388 rhi = (long) XDL_HASHLONG(hav, xdf1->hbits);
389 for (nm = 0, rec = xdf1->rhash[rhi]; rec; rec = rec->next)
390 if (rec->ha == hav && ++nm == mlim)
392 dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
395 for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
396 i <= xdf1->dend; i++, recs++) {
398 (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
399 xdf1->rindex[nreff] = i;
400 xdf1->ha[nreff] = (*recs)->ha;
407 for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
408 i <= xdf2->dend; i++, recs++) {
410 (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
411 xdf2->rindex[nreff] = i;
412 xdf2->ha[nreff] = (*recs)->ha;
426 * Early trim initial and terminal matching records.
428 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
430 xrecord_t **recs1, **recs2;
434 for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
435 i++, recs1++, recs2++)
436 if ((*recs1)->ha != (*recs2)->ha)
439 xdf1->dstart = xdf2->dstart = i;
441 recs1 = xdf1->recs + xdf1->nrec - 1;
442 recs2 = xdf2->recs + xdf2->nrec - 1;
443 for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
444 if ((*recs1)->ha != (*recs2)->ha)
447 xdf1->dend = xdf1->nrec - i - 1;
448 xdf2->dend = xdf2->nrec - i - 1;
454 static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2) {
456 if (xdl_trim_ends(xdf1, xdf2) < 0 ||
457 xdl_cleanup_records(xdf1, xdf2) < 0) {