xdiff/xprepare: use memset()
[git] / xdiff / xprepare.c
1 /*
2  *  LibXDiff by Davide Libenzi ( File Differential Library )
3  *  Copyright (C) 2003  Davide Libenzi
4  *
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.
9  *
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.
14  *
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
18  *
19  *  Davide Libenzi <davidel@xmailserver.org>
20  *
21  */
22
23 #include "xinclude.h"
24
25
26 #define XDL_KPDIS_RUN 4
27 #define XDL_MAX_EQLIMIT 1024
28 #define XDL_SIMSCAN_WINDOW 100
29
30
31 typedef struct s_xdlclass {
32         struct s_xdlclass *next;
33         unsigned long ha;
34         char const *line;
35         long size;
36         long idx;
37 } xdlclass_t;
38
39 typedef struct s_xdlclassifier {
40         unsigned int hbits;
41         long hsize;
42         xdlclass_t **rchash;
43         chastore_t ncha;
44         long count;
45         long flags;
46 } xdlclassifier_t;
47
48
49
50
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,
54                                xrecord_t *rec);
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);
62
63
64
65
66 static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
67         cf->flags = flags;
68
69         cf->hbits = xdl_hashbits((unsigned int) size);
70         cf->hsize = 1 << cf->hbits;
71
72         if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) {
73
74                 return -1;
75         }
76         if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) {
77
78                 xdl_cha_free(&cf->ncha);
79                 return -1;
80         }
81         memset(cf->rchash, 0, cf->hsize * sizeof(xdlclass_t *));
82
83         cf->count = 0;
84
85         return 0;
86 }
87
88
89 static void xdl_free_classifier(xdlclassifier_t *cf) {
90
91         xdl_free(cf->rchash);
92         xdl_cha_free(&cf->ncha);
93 }
94
95
96 static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
97                                xrecord_t *rec) {
98         long hi;
99         char const *line;
100         xdlclass_t *rcrec;
101
102         line = rec->ptr;
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 &&
106                                 xdl_recmatch(rcrec->line, rcrec->size,
107                                         rec->ptr, rec->size, cf->flags))
108                         break;
109
110         if (!rcrec) {
111                 if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
112
113                         return -1;
114                 }
115                 rcrec->idx = cf->count++;
116                 rcrec->line = line;
117                 rcrec->size = rec->size;
118                 rcrec->ha = rec->ha;
119                 rcrec->next = cf->rchash[hi];
120                 cf->rchash[hi] = rcrec;
121         }
122
123         rec->ha = (unsigned long) rcrec->idx;
124
125         hi = (long) XDL_HASHLONG(rec->ha, hbits);
126         rec->next = rhash[hi];
127         rhash[hi] = rec;
128
129         return 0;
130 }
131
132
133 static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
134                            xdlclassifier_t *cf, xdfile_t *xdf) {
135         unsigned int hbits;
136         long nrec, hsize, bsize;
137         unsigned long hav;
138         char const *blk, *cur, *top, *prev;
139         xrecord_t *crec;
140         xrecord_t **recs, **rrecs;
141         xrecord_t **rhash;
142         unsigned long *ha;
143         char *rchg;
144         long *rindex;
145
146         if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) {
147
148                 return -1;
149         }
150         if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) {
151
152                 xdl_cha_free(&xdf->rcha);
153                 return -1;
154         }
155
156         hbits = xdl_hashbits((unsigned int) narec);
157         hsize = 1 << hbits;
158         if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) {
159
160                 xdl_free(recs);
161                 xdl_cha_free(&xdf->rcha);
162                 return -1;
163         }
164         memset(rhash, 0, hsize * sizeof(xrecord_t *));
165
166         nrec = 0;
167         if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) {
168                 for (top = blk + bsize;;) {
169                         if (cur >= top) {
170                                 if (!(cur = blk = xdl_mmfile_next(mf, &bsize)))
171                                         break;
172                                 top = blk + bsize;
173                         }
174                         prev = cur;
175                         hav = xdl_hash_record(&cur, top, xpp->flags);
176                         if (nrec >= narec) {
177                                 narec *= 2;
178                                 if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) {
179
180                                         xdl_free(rhash);
181                                         xdl_free(recs);
182                                         xdl_cha_free(&xdf->rcha);
183                                         return -1;
184                                 }
185                                 recs = rrecs;
186                         }
187                         if (!(crec = xdl_cha_alloc(&xdf->rcha))) {
188
189                                 xdl_free(rhash);
190                                 xdl_free(recs);
191                                 xdl_cha_free(&xdf->rcha);
192                                 return -1;
193                         }
194                         crec->ptr = prev;
195                         crec->size = (long) (cur - prev);
196                         crec->ha = hav;
197                         recs[nrec++] = crec;
198
199                         if (xdl_classify_record(cf, rhash, hbits, crec) < 0) {
200
201                                 xdl_free(rhash);
202                                 xdl_free(recs);
203                                 xdl_cha_free(&xdf->rcha);
204                                 return -1;
205                         }
206                 }
207         }
208
209         if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) {
210
211                 xdl_free(rhash);
212                 xdl_free(recs);
213                 xdl_cha_free(&xdf->rcha);
214                 return -1;
215         }
216         memset(rchg, 0, (nrec + 2) * sizeof(char));
217
218         if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) {
219
220                 xdl_free(rchg);
221                 xdl_free(rhash);
222                 xdl_free(recs);
223                 xdl_cha_free(&xdf->rcha);
224                 return -1;
225         }
226         if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) {
227
228                 xdl_free(rindex);
229                 xdl_free(rchg);
230                 xdl_free(rhash);
231                 xdl_free(recs);
232                 xdl_cha_free(&xdf->rcha);
233                 return -1;
234         }
235
236         xdf->nrec = nrec;
237         xdf->recs = recs;
238         xdf->hbits = hbits;
239         xdf->rhash = rhash;
240         xdf->rchg = rchg + 1;
241         xdf->rindex = rindex;
242         xdf->nreff = 0;
243         xdf->ha = ha;
244         xdf->dstart = 0;
245         xdf->dend = nrec - 1;
246
247         return 0;
248 }
249
250
251 static void xdl_free_ctx(xdfile_t *xdf) {
252
253         xdl_free(xdf->rhash);
254         xdl_free(xdf->rindex);
255         xdl_free(xdf->rchg - 1);
256         xdl_free(xdf->ha);
257         xdl_free(xdf->recs);
258         xdl_cha_free(&xdf->rcha);
259 }
260
261
262 int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
263                     xdfenv_t *xe) {
264         long enl1, enl2;
265         xdlclassifier_t cf;
266
267         enl1 = xdl_guess_lines(mf1) + 1;
268         enl2 = xdl_guess_lines(mf2) + 1;
269
270         if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) {
271
272                 return -1;
273         }
274
275         if (xdl_prepare_ctx(mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
276
277                 xdl_free_classifier(&cf);
278                 return -1;
279         }
280         if (xdl_prepare_ctx(mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
281
282                 xdl_free_ctx(&xe->xdf1);
283                 xdl_free_classifier(&cf);
284                 return -1;
285         }
286
287         xdl_free_classifier(&cf);
288
289         if (!(xpp->flags & XDF_PATIENCE_DIFF) &&
290                         xdl_optimize_ctxs(&xe->xdf1, &xe->xdf2) < 0) {
291
292                 xdl_free_ctx(&xe->xdf2);
293                 xdl_free_ctx(&xe->xdf1);
294                 return -1;
295         }
296
297         return 0;
298 }
299
300
301 void xdl_free_env(xdfenv_t *xe) {
302
303         xdl_free_ctx(&xe->xdf2);
304         xdl_free_ctx(&xe->xdf1);
305 }
306
307
308 static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
309         long r, rdis0, rpdis0, rdis1, rpdis1;
310
311         /*
312          * Limits the window the is examined during the similar-lines
313          * scan. The loops below stops when dis[i - r] == 1 (line that
314          * has no match), but there are corner cases where the loop
315          * proceed all the way to the extremities by causing huge
316          * performance penalties in case of big files.
317          */
318         if (i - s > XDL_SIMSCAN_WINDOW)
319                 s = i - XDL_SIMSCAN_WINDOW;
320         if (e - i > XDL_SIMSCAN_WINDOW)
321                 e = i + XDL_SIMSCAN_WINDOW;
322
323         /*
324          * Scans the lines before 'i' to find a run of lines that either
325          * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1).
326          * Note that we always call this function with dis[i] > 1, so the
327          * current line (i) is already a multimatch line.
328          */
329         for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
330                 if (!dis[i - r])
331                         rdis0++;
332                 else if (dis[i - r] == 2)
333                         rpdis0++;
334                 else
335                         break;
336         }
337         /*
338          * If the run before the line 'i' found only multimatch lines, we
339          * return 0 and hence we don't make the current line (i) discarded.
340          * We want to discard multimatch lines only when they appear in the
341          * middle of runs with nomatch lines (dis[j] == 0).
342          */
343         if (rdis0 == 0)
344                 return 0;
345         for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
346                 if (!dis[i + r])
347                         rdis1++;
348                 else if (dis[i + r] == 2)
349                         rpdis1++;
350                 else
351                         break;
352         }
353         /*
354          * If the run after the line 'i' found only multimatch lines, we
355          * return 0 and hence we don't make the current line (i) discarded.
356          */
357         if (rdis1 == 0)
358                 return 0;
359         rdis1 += rdis0;
360         rpdis1 += rpdis0;
361
362         return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
363 }
364
365
366 /*
367  * Try to reduce the problem complexity, discard records that have no
368  * matches on the other file. Also, lines that have multiple matches
369  * might be potentially discarded if they happear in a run of discardable.
370  */
371 static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2) {
372         long i, nm, rhi, nreff, mlim;
373         unsigned long hav;
374         xrecord_t **recs;
375         xrecord_t *rec;
376         char *dis, *dis1, *dis2;
377
378         if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
379
380                 return -1;
381         }
382         memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
383         dis1 = dis;
384         dis2 = dis1 + xdf1->nrec + 1;
385
386         if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT)
387                 mlim = XDL_MAX_EQLIMIT;
388         for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
389                 hav = (*recs)->ha;
390                 rhi = (long) XDL_HASHLONG(hav, xdf2->hbits);
391                 for (nm = 0, rec = xdf2->rhash[rhi]; rec; rec = rec->next)
392                         if (rec->ha == hav && ++nm == mlim)
393                                 break;
394                 dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
395         }
396
397         if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT)
398                 mlim = XDL_MAX_EQLIMIT;
399         for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
400                 hav = (*recs)->ha;
401                 rhi = (long) XDL_HASHLONG(hav, xdf1->hbits);
402                 for (nm = 0, rec = xdf1->rhash[rhi]; rec; rec = rec->next)
403                         if (rec->ha == hav && ++nm == mlim)
404                                 break;
405                 dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
406         }
407
408         for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
409              i <= xdf1->dend; i++, recs++) {
410                 if (dis1[i] == 1 ||
411                     (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
412                         xdf1->rindex[nreff] = i;
413                         xdf1->ha[nreff] = (*recs)->ha;
414                         nreff++;
415                 } else
416                         xdf1->rchg[i] = 1;
417         }
418         xdf1->nreff = nreff;
419
420         for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
421              i <= xdf2->dend; i++, recs++) {
422                 if (dis2[i] == 1 ||
423                     (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
424                         xdf2->rindex[nreff] = i;
425                         xdf2->ha[nreff] = (*recs)->ha;
426                         nreff++;
427                 } else
428                         xdf2->rchg[i] = 1;
429         }
430         xdf2->nreff = nreff;
431
432         xdl_free(dis);
433
434         return 0;
435 }
436
437
438 /*
439  * Early trim initial and terminal matching records.
440  */
441 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
442         long i, lim;
443         xrecord_t **recs1, **recs2;
444
445         recs1 = xdf1->recs;
446         recs2 = xdf2->recs;
447         for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
448              i++, recs1++, recs2++)
449                 if ((*recs1)->ha != (*recs2)->ha)
450                         break;
451
452         xdf1->dstart = xdf2->dstart = i;
453
454         recs1 = xdf1->recs + xdf1->nrec - 1;
455         recs2 = xdf2->recs + xdf2->nrec - 1;
456         for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
457                 if ((*recs1)->ha != (*recs2)->ha)
458                         break;
459
460         xdf1->dend = xdf1->nrec - i - 1;
461         xdf2->dend = xdf2->nrec - i - 1;
462
463         return 0;
464 }
465
466
467 static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2) {
468
469         if (xdl_trim_ends(xdf1, xdf2) < 0 ||
470             xdl_cleanup_records(xdf1, xdf2) < 0) {
471
472                 return -1;
473         }
474
475         return 0;
476 }