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;
42
typedef struct s_xdlclassifier {
56
static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags);
57
static void xdl_free_classifier(xdlclassifier_t *cf);
58
static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
59
unsigned int hbits, xrecord_t *rec);
60
static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
61
xdlclassifier_t *cf, xdfile_t *xdf);
62
static void xdl_free_ctx(xdfile_t *xdf);
63
static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
64
static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
65
static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
66
static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
71
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 (!XDL_CALLOC_ARRAY(cf->rchash, cf->hsize)) {
83
xdl_cha_free(&cf->ncha);
88
if (!XDL_ALLOC_ARRAY(cf->rcrecs, cf->alloc)) {
91
xdl_cha_free(&cf->ncha);
101
static void xdl_free_classifier(xdlclassifier_t *cf) {
103
xdl_free(cf->rcrecs);
104
xdl_free(cf->rchash);
105
xdl_cha_free(&cf->ncha);
109
static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
110
unsigned int hbits, xrecord_t *rec) {
116
hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
117
for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
118
if (rcrec->ha == rec->ha &&
119
xdl_recmatch(rcrec->line, rcrec->size,
120
rec->ptr, rec->size, cf->flags))
124
if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
128
rcrec->idx = cf->count++;
129
if (XDL_ALLOC_GROW(cf->rcrecs, cf->count, cf->alloc))
131
cf->rcrecs[rcrec->idx] = rcrec;
133
rcrec->size = rec->size;
135
rcrec->len1 = rcrec->len2 = 0;
136
rcrec->next = cf->rchash[hi];
137
cf->rchash[hi] = rcrec;
140
(pass == 1) ? rcrec->len1++ : rcrec->len2++;
142
rec->ha = (unsigned long) rcrec->idx;
144
hi = (long) XDL_HASHLONG(rec->ha, hbits);
145
rec->next = rhash[hi];
152
static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
153
xdlclassifier_t *cf, xdfile_t *xdf) {
155
long nrec, hsize, bsize;
157
char const *blk, *cur, *top, *prev;
171
if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0)
173
if (!XDL_ALLOC_ARRAY(recs, narec))
176
hbits = xdl_hashbits((unsigned int) narec);
178
if (!XDL_CALLOC_ARRAY(rhash, hsize))
182
if ((cur = blk = xdl_mmfile_first(mf, &bsize))) {
183
for (top = blk + bsize; cur < top; ) {
185
hav = xdl_hash_record(&cur, top, xpp->flags);
186
if (XDL_ALLOC_GROW(recs, nrec + 1, narec))
188
if (!(crec = xdl_cha_alloc(&xdf->rcha)))
191
crec->size = (long) (cur - prev);
194
if (xdl_classify_record(pass, cf, rhash, hbits, crec) < 0)
199
if (!XDL_CALLOC_ARRAY(rchg, nrec + 2))
202
if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
203
(XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) {
204
if (!XDL_ALLOC_ARRAY(rindex, nrec + 1))
206
if (!XDL_ALLOC_ARRAY(ha, nrec + 1))
214
xdf->rchg = rchg + 1;
215
xdf->rindex = rindex;
219
xdf->dend = nrec - 1;
229
xdl_cha_free(&xdf->rcha);
234
static void xdl_free_ctx(xdfile_t *xdf) {
236
xdl_free(xdf->rhash);
237
xdl_free(xdf->rindex);
238
xdl_free(xdf->rchg - 1);
241
xdl_cha_free(&xdf->rcha);
245
int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
247
long enl1, enl2, sample;
250
memset(&cf, 0, sizeof(cf));
259
sample = (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF
260
? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1);
262
enl1 = xdl_guess_lines(mf1, sample) + 1;
263
enl2 = xdl_guess_lines(mf2, sample) + 1;
265
if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0)
268
if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
270
xdl_free_classifier(&cf);
273
if (xdl_prepare_ctx(2, mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
275
xdl_free_ctx(&xe->xdf1);
276
xdl_free_classifier(&cf);
280
if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
281
(XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) &&
282
xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) {
284
xdl_free_ctx(&xe->xdf2);
285
xdl_free_ctx(&xe->xdf1);
286
xdl_free_classifier(&cf);
290
xdl_free_classifier(&cf);
296
void xdl_free_env(xdfenv_t *xe) {
298
xdl_free_ctx(&xe->xdf2);
299
xdl_free_ctx(&xe->xdf1);
303
static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
304
long r, rdis0, rpdis0, rdis1, rpdis1;
313
if (i - s > XDL_SIMSCAN_WINDOW)
314
s = i - XDL_SIMSCAN_WINDOW;
315
if (e - i > XDL_SIMSCAN_WINDOW)
316
e = i + XDL_SIMSCAN_WINDOW;
324
for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
327
else if (dis[i - r] == 2)
340
for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
343
else if (dis[i + r] == 2)
357
return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
366
static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
367
long i, nm, nreff, mlim;
370
char *dis, *dis1, *dis2;
372
if (!XDL_CALLOC_ARRAY(dis, xdf1->nrec + xdf2->nrec + 2))
375
dis2 = dis1 + xdf1->nrec + 1;
377
if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT)
378
mlim = XDL_MAX_EQLIMIT;
379
for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
380
rcrec = cf->rcrecs[(*recs)->ha];
381
nm = rcrec ? rcrec->len2 : 0;
382
dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
385
if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT)
386
mlim = XDL_MAX_EQLIMIT;
387
for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
388
rcrec = cf->rcrecs[(*recs)->ha];
389
nm = rcrec ? rcrec->len1 : 0;
390
dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
393
for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
394
i <= xdf1->dend; i++, recs++) {
396
(dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
397
xdf1->rindex[nreff] = i;
398
xdf1->ha[nreff] = (*recs)->ha;
405
for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
406
i <= xdf2->dend; i++, recs++) {
408
(dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
409
xdf2->rindex[nreff] = i;
410
xdf2->ha[nreff] = (*recs)->ha;
426
static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
428
xrecord_t **recs1, **recs2;
432
for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
433
i++, recs1++, recs2++)
434
if ((*recs1)->ha != (*recs2)->ha)
437
xdf1->dstart = xdf2->dstart = i;
439
recs1 = xdf1->recs + xdf1->nrec - 1;
440
recs2 = xdf2->recs + xdf2->nrec - 1;
441
for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
442
if ((*recs1)->ha != (*recs2)->ha)
445
xdf1->dend = xdf1->nrec - i - 1;
446
xdf2->dend = xdf2->nrec - i - 1;
452
static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
454
if (xdl_trim_ends(xdf1, xdf2) < 0 ||
455
xdl_cleanup_records(cf, xdf1, xdf2) < 0) {