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
;
39 typedef struct s_xdlclassifier
{
51 static int xdl_init_classifier(xdlclassifier_t
*cf
, long size
, long flags
);
52 static void xdl_free_classifier(xdlclassifier_t
*cf
);
53 static int xdl_classify_record(xdlclassifier_t
*cf
, xrecord_t
**rhash
, unsigned int hbits
,
55 static int xdl_prepare_ctx(mmfile_t
*mf
, long narec
, xpparam_t
const *xpp
,
56 xdlclassifier_t
*cf
, xdfile_t
*xdf
);
57 static void xdl_free_ctx(xdfile_t
*xdf
);
58 static int xdl_clean_mmatch(char const *dis
, long i
, long s
, long e
);
59 static int xdl_cleanup_records(xdfile_t
*xdf1
, xdfile_t
*xdf2
);
60 static int xdl_trim_ends(xdfile_t
*xdf1
, xdfile_t
*xdf2
);
61 static int xdl_optimize_ctxs(xdfile_t
*xdf1
, xdfile_t
*xdf2
);
66 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 for (i
= 0; i
< cf
->hsize
; i
++)
92 static void xdl_free_classifier(xdlclassifier_t
*cf
) {
95 xdl_cha_free(&cf
->ncha
);
99 static int xdl_classify_record(xdlclassifier_t
*cf
, xrecord_t
**rhash
, unsigned int hbits
,
106 hi
= (long) XDL_HASHLONG(rec
->ha
, cf
->hbits
);
107 for (rcrec
= cf
->rchash
[hi
]; rcrec
; rcrec
= rcrec
->next
)
108 if (rcrec
->ha
== rec
->ha
&&
109 xdl_recmatch(rcrec
->line
, rcrec
->size
,
110 rec
->ptr
, rec
->size
, cf
->flags
))
114 if (!(rcrec
= xdl_cha_alloc(&cf
->ncha
))) {
118 rcrec
->idx
= cf
->count
++;
120 rcrec
->size
= rec
->size
;
122 rcrec
->next
= cf
->rchash
[hi
];
123 cf
->rchash
[hi
] = rcrec
;
126 rec
->ha
= (unsigned long) rcrec
->idx
;
128 hi
= (long) XDL_HASHLONG(rec
->ha
, hbits
);
129 rec
->next
= rhash
[hi
];
136 static int xdl_prepare_ctx(mmfile_t
*mf
, long narec
, xpparam_t
const *xpp
,
137 xdlclassifier_t
*cf
, xdfile_t
*xdf
) {
139 long i
, nrec
, hsize
, bsize
;
141 char const *blk
, *cur
, *top
, *prev
;
143 xrecord_t
**recs
, **rrecs
;
149 if (xdl_cha_init(&xdf
->rcha
, sizeof(xrecord_t
), narec
/ 4 + 1) < 0) {
153 if (!(recs
= (xrecord_t
**) xdl_malloc(narec
* sizeof(xrecord_t
*)))) {
155 xdl_cha_free(&xdf
->rcha
);
159 hbits
= xdl_hashbits((unsigned int) narec
);
161 if (!(rhash
= (xrecord_t
**) xdl_malloc(hsize
* sizeof(xrecord_t
*)))) {
164 xdl_cha_free(&xdf
->rcha
);
167 for (i
= 0; i
< hsize
; i
++)
171 if ((cur
= blk
= xdl_mmfile_first(mf
, &bsize
)) != NULL
) {
172 for (top
= blk
+ bsize
;;) {
174 if (!(cur
= blk
= xdl_mmfile_next(mf
, &bsize
)))
179 hav
= xdl_hash_record(&cur
, top
, xpp
->flags
);
182 if (!(rrecs
= (xrecord_t
**) xdl_realloc(recs
, narec
* sizeof(xrecord_t
*)))) {
186 xdl_cha_free(&xdf
->rcha
);
191 if (!(crec
= xdl_cha_alloc(&xdf
->rcha
))) {
195 xdl_cha_free(&xdf
->rcha
);
199 crec
->size
= (long) (cur
- prev
);
203 if (xdl_classify_record(cf
, rhash
, hbits
, crec
) < 0) {
207 xdl_cha_free(&xdf
->rcha
);
213 if (!(rchg
= (char *) xdl_malloc((nrec
+ 2) * sizeof(char)))) {
217 xdl_cha_free(&xdf
->rcha
);
220 memset(rchg
, 0, (nrec
+ 2) * sizeof(char));
222 if (!(rindex
= (long *) xdl_malloc((nrec
+ 1) * sizeof(long)))) {
227 xdl_cha_free(&xdf
->rcha
);
230 if (!(ha
= (unsigned long *) xdl_malloc((nrec
+ 1) * sizeof(unsigned long)))) {
236 xdl_cha_free(&xdf
->rcha
);
244 xdf
->rchg
= rchg
+ 1;
245 xdf
->rindex
= rindex
;
249 xdf
->dend
= nrec
- 1;
255 static void xdl_free_ctx(xdfile_t
*xdf
) {
257 xdl_free(xdf
->rhash
);
258 xdl_free(xdf
->rindex
);
259 xdl_free(xdf
->rchg
- 1);
262 xdl_cha_free(&xdf
->rcha
);
266 int xdl_prepare_env(mmfile_t
*mf1
, mmfile_t
*mf2
, xpparam_t
const *xpp
,
271 enl1
= xdl_guess_lines(mf1
) + 1;
272 enl2
= xdl_guess_lines(mf2
) + 1;
274 if (xdl_init_classifier(&cf
, enl1
+ enl2
+ 1, xpp
->flags
) < 0) {
279 if (xdl_prepare_ctx(mf1
, enl1
, xpp
, &cf
, &xe
->xdf1
) < 0) {
281 xdl_free_classifier(&cf
);
284 if (xdl_prepare_ctx(mf2
, enl2
, xpp
, &cf
, &xe
->xdf2
) < 0) {
286 xdl_free_ctx(&xe
->xdf1
);
287 xdl_free_classifier(&cf
);
291 xdl_free_classifier(&cf
);
293 if (xdl_optimize_ctxs(&xe
->xdf1
, &xe
->xdf2
) < 0) {
295 xdl_free_ctx(&xe
->xdf2
);
296 xdl_free_ctx(&xe
->xdf1
);
304 void xdl_free_env(xdfenv_t
*xe
) {
306 xdl_free_ctx(&xe
->xdf2
);
307 xdl_free_ctx(&xe
->xdf1
);
311 static int xdl_clean_mmatch(char const *dis
, long i
, long s
, long e
) {
312 long r
, rdis0
, rpdis0
, rdis1
, rpdis1
;
315 * Limits the window the is examined during the similar-lines
316 * scan. The loops below stops when dis[i - r] == 1 (line that
317 * has no match), but there are corner cases where the loop
318 * proceed all the way to the extremities by causing huge
319 * performance penalties in case of big files.
321 if (i
- s
> XDL_SIMSCAN_WINDOW
)
322 s
= i
- XDL_SIMSCAN_WINDOW
;
323 if (e
- i
> XDL_SIMSCAN_WINDOW
)
324 e
= i
+ XDL_SIMSCAN_WINDOW
;
327 * Scans the lines before 'i' to find a run of lines that either
328 * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1).
329 * Note that we always call this function with dis[i] > 1, so the
330 * current line (i) is already a multimatch line.
332 for (r
= 1, rdis0
= 0, rpdis0
= 1; (i
- r
) >= s
; r
++) {
335 else if (dis
[i
- r
] == 2)
341 * If the run before the line 'i' found only multimatch lines, we
342 * return 0 and hence we don't make the current line (i) discarded.
343 * We want to discard multimatch lines only when they appear in the
344 * middle of runs with nomatch lines (dis[j] == 0).
348 for (r
= 1, rdis1
= 0, rpdis1
= 1; (i
+ r
) <= e
; r
++) {
351 else if (dis
[i
+ r
] == 2)
357 * If the run after the line 'i' found only multimatch lines, we
358 * return 0 and hence we don't make the current line (i) discarded.
365 return rpdis1
* XDL_KPDIS_RUN
< (rpdis1
+ rdis1
);
370 * Try to reduce the problem complexity, discard records that have no
371 * matches on the other file. Also, lines that have multiple matches
372 * might be potentially discarded if they happear in a run of discardable.
374 static int xdl_cleanup_records(xdfile_t
*xdf1
, xdfile_t
*xdf2
) {
375 long i
, nm
, rhi
, nreff
, mlim
;
379 char *dis
, *dis1
, *dis2
;
381 if (!(dis
= (char *) xdl_malloc(xdf1
->nrec
+ xdf2
->nrec
+ 2))) {
385 memset(dis
, 0, xdf1
->nrec
+ xdf2
->nrec
+ 2);
387 dis2
= dis1
+ xdf1
->nrec
+ 1;
389 if ((mlim
= xdl_bogosqrt(xdf1
->nrec
)) > XDL_MAX_EQLIMIT
)
390 mlim
= XDL_MAX_EQLIMIT
;
391 for (i
= xdf1
->dstart
, recs
= &xdf1
->recs
[xdf1
->dstart
]; i
<= xdf1
->dend
; i
++, recs
++) {
393 rhi
= (long) XDL_HASHLONG(hav
, xdf2
->hbits
);
394 for (nm
= 0, rec
= xdf2
->rhash
[rhi
]; rec
; rec
= rec
->next
)
395 if (rec
->ha
== hav
&& ++nm
== mlim
)
397 dis1
[i
] = (nm
== 0) ? 0: (nm
>= mlim
) ? 2: 1;
400 if ((mlim
= xdl_bogosqrt(xdf2
->nrec
)) > XDL_MAX_EQLIMIT
)
401 mlim
= XDL_MAX_EQLIMIT
;
402 for (i
= xdf2
->dstart
, recs
= &xdf2
->recs
[xdf2
->dstart
]; i
<= xdf2
->dend
; i
++, recs
++) {
404 rhi
= (long) XDL_HASHLONG(hav
, xdf1
->hbits
);
405 for (nm
= 0, rec
= xdf1
->rhash
[rhi
]; rec
; rec
= rec
->next
)
406 if (rec
->ha
== hav
&& ++nm
== mlim
)
408 dis2
[i
] = (nm
== 0) ? 0: (nm
>= mlim
) ? 2: 1;
411 for (nreff
= 0, i
= xdf1
->dstart
, recs
= &xdf1
->recs
[xdf1
->dstart
];
412 i
<= xdf1
->dend
; i
++, recs
++) {
414 (dis1
[i
] == 2 && !xdl_clean_mmatch(dis1
, i
, xdf1
->dstart
, xdf1
->dend
))) {
415 xdf1
->rindex
[nreff
] = i
;
416 xdf1
->ha
[nreff
] = (*recs
)->ha
;
423 for (nreff
= 0, i
= xdf2
->dstart
, recs
= &xdf2
->recs
[xdf2
->dstart
];
424 i
<= xdf2
->dend
; i
++, recs
++) {
426 (dis2
[i
] == 2 && !xdl_clean_mmatch(dis2
, i
, xdf2
->dstart
, xdf2
->dend
))) {
427 xdf2
->rindex
[nreff
] = i
;
428 xdf2
->ha
[nreff
] = (*recs
)->ha
;
442 * Early trim initial and terminal matching records.
444 static int xdl_trim_ends(xdfile_t
*xdf1
, xdfile_t
*xdf2
) {
446 xrecord_t
**recs1
, **recs2
;
450 for (i
= 0, lim
= XDL_MIN(xdf1
->nrec
, xdf2
->nrec
); i
< lim
;
451 i
++, recs1
++, recs2
++)
452 if ((*recs1
)->ha
!= (*recs2
)->ha
)
455 xdf1
->dstart
= xdf2
->dstart
= i
;
457 recs1
= xdf1
->recs
+ xdf1
->nrec
- 1;
458 recs2
= xdf2
->recs
+ xdf2
->nrec
- 1;
459 for (lim
-= i
, i
= 0; i
< lim
; i
++, recs1
--, recs2
--)
460 if ((*recs1
)->ha
!= (*recs2
)->ha
)
463 xdf1
->dend
= xdf1
->nrec
- i
- 1;
464 xdf2
->dend
= xdf2
->nrec
- i
- 1;
470 static int xdl_optimize_ctxs(xdfile_t
*xdf1
, xdfile_t
*xdf2
) {
472 if (xdl_trim_ends(xdf1
, xdf2
) < 0 ||
473 xdl_cleanup_records(xdf1
, xdf2
) < 0) {