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