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>
27 #define XDL_KPDIS_RUN 4
31 typedef struct s_xdlclass {
32 struct s_xdlclass *next;
39 typedef struct s_xdlclassifier {
50 static int xdl_init_classifier(xdlclassifier_t *cf, long size);
51 static void xdl_free_classifier(xdlclassifier_t *cf);
52 static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
54 static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
55 xdlclassifier_t *cf, xdfile_t *xdf);
56 static void xdl_free_ctx(xdfile_t *xdf);
57 static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
58 static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2);
59 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
60 static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2);
65 static int xdl_init_classifier(xdlclassifier_t *cf, long size) {
68 cf->hbits = xdl_hashbits((unsigned int) size);
69 cf->hsize = 1 << cf->hbits;
71 if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) {
75 if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) {
77 xdl_cha_free(&cf->ncha);
80 for (i = 0; i < cf->hsize; i++)
89 static void xdl_free_classifier(xdlclassifier_t *cf) {
92 xdl_cha_free(&cf->ncha);
96 static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
103 hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
104 for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
105 if (rcrec->ha == rec->ha && rcrec->size == rec->size &&
106 !memcmp(line, rcrec->line, rec->size))
110 if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
114 rcrec->idx = cf->count++;
116 rcrec->size = rec->size;
118 rcrec->next = cf->rchash[hi];
119 cf->rchash[hi] = rcrec;
122 rec->ha = (unsigned long) rcrec->idx;
124 hi = (long) XDL_HASHLONG(rec->ha, hbits);
125 rec->next = rhash[hi];
132 static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
133 xdlclassifier_t *cf, xdfile_t *xdf) {
135 long i, nrec, hsize, bsize;
137 char const *blk, *cur, *top, *prev;
139 xrecord_t **recs, **rrecs;
145 if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) {
149 if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) {
151 xdl_cha_free(&xdf->rcha);
155 hbits = xdl_hashbits((unsigned int) narec);
157 if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) {
160 xdl_cha_free(&xdf->rcha);
163 for (i = 0; i < hsize; i++)
167 if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) {
168 for (top = blk + bsize;;) {
170 if (!(cur = blk = xdl_mmfile_next(mf, &bsize)))
175 hav = xdl_hash_record(&cur, top);
178 if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) {
182 xdl_cha_free(&xdf->rcha);
187 if (!(crec = xdl_cha_alloc(&xdf->rcha))) {
191 xdl_cha_free(&xdf->rcha);
195 crec->size = (long) (cur - prev);
199 if (xdl_classify_record(cf, rhash, hbits, crec) < 0) {
203 xdl_cha_free(&xdf->rcha);
209 if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) {
213 xdl_cha_free(&xdf->rcha);
216 memset(rchg, 0, (nrec + 2) * sizeof(char));
218 if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) {
223 xdl_cha_free(&xdf->rcha);
226 if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) {
232 xdl_cha_free(&xdf->rcha);
240 xdf->rchg = rchg + 1;
241 xdf->rindex = rindex;
245 xdf->dend = nrec - 1;
251 static void xdl_free_ctx(xdfile_t *xdf) {
253 xdl_free(xdf->rhash);
254 xdl_free(xdf->rindex);
255 xdl_free(xdf->rchg - 1);
258 xdl_cha_free(&xdf->rcha);
262 int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
267 enl1 = xdl_guess_lines(mf1) + 1;
268 enl2 = xdl_guess_lines(mf2) + 1;
270 if (xdl_init_classifier(&cf, enl1 + enl2 + 1) < 0) {
275 if (xdl_prepare_ctx(mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
277 xdl_free_classifier(&cf);
280 if (xdl_prepare_ctx(mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
282 xdl_free_ctx(&xe->xdf1);
283 xdl_free_classifier(&cf);
287 xdl_free_classifier(&cf);
289 if (xdl_optimize_ctxs(&xe->xdf1, &xe->xdf2) < 0) {
291 xdl_free_ctx(&xe->xdf2);
292 xdl_free_ctx(&xe->xdf1);
300 void xdl_free_env(xdfenv_t *xe) {
302 xdl_free_ctx(&xe->xdf2);
303 xdl_free_ctx(&xe->xdf1);
307 static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
310 for (r = 1, rdis = 0, rpdis = 1; (i - r) >= s; r++) {
313 else if (dis[i - r] == 2)
318 for (r = 1; (i + r) <= e; r++) {
321 else if (dis[i + r] == 2)
327 return rpdis * XDL_KPDIS_RUN < (rpdis + rdis);
332 * Try to reduce the problem complexity, discard records that have no
333 * matches on the other file. Also, lines that have multiple matches
334 * might be potentially discarded if they happear in a run of discardable.
336 static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2) {
341 char *dis, *dis1, *dis2;
343 if (!(dis = (char *) xdl_malloc((xdf1->nrec + xdf2->nrec + 2) * sizeof(char)))) {
347 memset(dis, 0, (xdf1->nrec + xdf2->nrec + 2) * sizeof(char));
349 dis2 = dis1 + xdf1->nrec + 1;
351 for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
353 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)
359 for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
361 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)
367 for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
368 i <= xdf1->dend; i++, recs++) {
370 (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
371 xdf1->rindex[nreff] = i;
372 xdf1->ha[nreff] = (*recs)->ha;
379 for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
380 i <= xdf2->dend; i++, recs++) {
382 (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
383 xdf2->rindex[nreff] = i;
384 xdf2->ha[nreff] = (*recs)->ha;
398 * Early trim initial and terminal matching records.
400 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
402 xrecord_t **recs1, **recs2;
406 for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
407 i++, recs1++, recs2++)
408 if ((*recs1)->ha != (*recs2)->ha)
411 xdf1->dstart = xdf2->dstart = i;
413 recs1 = xdf1->recs + xdf1->nrec - 1;
414 recs2 = xdf2->recs + xdf2->nrec - 1;
415 for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
416 if ((*recs1)->ha != (*recs2)->ha)
419 xdf1->dend = xdf1->nrec - i - 1;
420 xdf2->dend = xdf2->nrec - i - 1;
426 static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2) {
428 if (xdl_trim_ends(xdf1, xdf2) < 0 ||
429 xdl_cleanup_records(xdf1, xdf2) < 0) {