]> git.ipfire.org Git - thirdparty/git.git/blame - xdiff/xprepare.c
git-commit: allow -e option anywhere on command line
[thirdparty/git.git] / xdiff / xprepare.c
CommitLineData
3443546f
LT
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
27#define XDL_KPDIS_RUN 4
ca557aff 28#define XDL_MAX_EQLIMIT 1024
3443546f
LT
29
30
31
32typedef struct s_xdlclass {
33 struct s_xdlclass *next;
34 unsigned long ha;
35 char const *line;
36 long size;
37 long idx;
38} xdlclass_t;
39
40typedef struct s_xdlclassifier {
41 unsigned int hbits;
42 long hsize;
43 xdlclass_t **rchash;
44 chastore_t ncha;
45 long count;
46} xdlclassifier_t;
47
48
49
50
51static int xdl_init_classifier(xdlclassifier_t *cf, long size);
52static void xdl_free_classifier(xdlclassifier_t *cf);
53static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
54 xrecord_t *rec);
55static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
56 xdlclassifier_t *cf, xdfile_t *xdf);
57static void xdl_free_ctx(xdfile_t *xdf);
58static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
59static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2);
60static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
61static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2);
62
63
64
65
66static int xdl_init_classifier(xdlclassifier_t *cf, long size) {
67 long i;
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 for (i = 0; i < cf->hsize; i++)
82 cf->rchash[i] = NULL;
83
84 cf->count = 0;
85
86 return 0;
87}
88
89
90static void xdl_free_classifier(xdlclassifier_t *cf) {
91
92 xdl_free(cf->rchash);
93 xdl_cha_free(&cf->ncha);
94}
95
96
97static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
98 xrecord_t *rec) {
99 long hi;
100 char const *line;
101 xdlclass_t *rcrec;
102
103 line = rec->ptr;
104 hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
105 for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
106 if (rcrec->ha == rec->ha && rcrec->size == rec->size &&
107 !memcmp(line, rcrec->line, rec->size))
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
133static 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 i, 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 for (i = 0; i < hsize; i++)
165 rhash[i] = NULL;
166
167 nrec = 0;
168 if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) {
169 for (top = blk + bsize;;) {
170 if (cur >= top) {
171 if (!(cur = blk = xdl_mmfile_next(mf, &bsize)))
172 break;
173 top = blk + bsize;
174 }
175 prev = cur;
176 hav = xdl_hash_record(&cur, top);
177 if (nrec >= narec) {
178 narec *= 2;
179 if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) {
180
181 xdl_free(rhash);
182 xdl_free(recs);
183 xdl_cha_free(&xdf->rcha);
184 return -1;
185 }
186 recs = rrecs;
187 }
188 if (!(crec = xdl_cha_alloc(&xdf->rcha))) {
189
190 xdl_free(rhash);
191 xdl_free(recs);
192 xdl_cha_free(&xdf->rcha);
193 return -1;
194 }
195 crec->ptr = prev;
196 crec->size = (long) (cur - prev);
197 crec->ha = hav;
198 recs[nrec++] = crec;
199
200 if (xdl_classify_record(cf, rhash, hbits, crec) < 0) {
201
202 xdl_free(rhash);
203 xdl_free(recs);
204 xdl_cha_free(&xdf->rcha);
205 return -1;
206 }
207 }
208 }
209
210 if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) {
211
212 xdl_free(rhash);
213 xdl_free(recs);
214 xdl_cha_free(&xdf->rcha);
215 return -1;
216 }
217 memset(rchg, 0, (nrec + 2) * sizeof(char));
218
219 if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) {
220
221 xdl_free(rchg);
222 xdl_free(rhash);
223 xdl_free(recs);
224 xdl_cha_free(&xdf->rcha);
225 return -1;
226 }
227 if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) {
228
229 xdl_free(rindex);
230 xdl_free(rchg);
231 xdl_free(rhash);
232 xdl_free(recs);
233 xdl_cha_free(&xdf->rcha);
234 return -1;
235 }
236
237 xdf->nrec = nrec;
238 xdf->recs = recs;
239 xdf->hbits = hbits;
240 xdf->rhash = rhash;
241 xdf->rchg = rchg + 1;
242 xdf->rindex = rindex;
243 xdf->nreff = 0;
244 xdf->ha = ha;
245 xdf->dstart = 0;
246 xdf->dend = nrec - 1;
247
248 return 0;
249}
250
251
252static void xdl_free_ctx(xdfile_t *xdf) {
253
254 xdl_free(xdf->rhash);
255 xdl_free(xdf->rindex);
256 xdl_free(xdf->rchg - 1);
257 xdl_free(xdf->ha);
258 xdl_free(xdf->recs);
259 xdl_cha_free(&xdf->rcha);
260}
261
262
263int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
264 xdfenv_t *xe) {
265 long enl1, enl2;
266 xdlclassifier_t cf;
267
268 enl1 = xdl_guess_lines(mf1) + 1;
269 enl2 = xdl_guess_lines(mf2) + 1;
270
271 if (xdl_init_classifier(&cf, enl1 + enl2 + 1) < 0) {
272
273 return -1;
274 }
275
276 if (xdl_prepare_ctx(mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
277
278 xdl_free_classifier(&cf);
279 return -1;
280 }
281 if (xdl_prepare_ctx(mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
282
283 xdl_free_ctx(&xe->xdf1);
284 xdl_free_classifier(&cf);
285 return -1;
286 }
287
288 xdl_free_classifier(&cf);
289
290 if (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
301void xdl_free_env(xdfenv_t *xe) {
302
303 xdl_free_ctx(&xe->xdf2);
304 xdl_free_ctx(&xe->xdf1);
305}
306
307
308static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
ca557aff
DL
309 long r, rdis0, rpdis0, rdis1, rpdis1;
310
311 /*
312 * Scans the lines before 'i' to find a run of lines that either
313 * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1).
314 * Note that we always call this function with dis[i] > 1, so the
315 * current line (i) is already a multimatch line.
316 */
317 for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) {
3443546f 318 if (!dis[i - r])
ca557aff 319 rdis0++;
3443546f 320 else if (dis[i - r] == 2)
ca557aff 321 rpdis0++;
3443546f
LT
322 else
323 break;
324 }
ca557aff
DL
325 /*
326 * If the run before the line 'i' found only multimatch lines, we
327 * return 0 and hence we don't make the current line (i) discarded.
328 * We want to discard multimatch lines only when they appear in the
329 * middle of runs with nomatch lines (dis[j] == 0).
330 */
331 if (rdis0 == 0)
332 return 0;
333 for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) {
3443546f 334 if (!dis[i + r])
ca557aff 335 rdis1++;
3443546f 336 else if (dis[i + r] == 2)
ca557aff 337 rpdis1++;
3443546f
LT
338 else
339 break;
340 }
ca557aff
DL
341 /*
342 * If the run after the line 'i' found only multimatch lines, we
343 * return 0 and hence we don't make the current line (i) discarded.
344 */
345 if (rdis1 == 0)
346 return 0;
347 rdis1 += rdis0;
348 rpdis1 += rpdis0;
349
350 return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
3443546f
LT
351}
352
353
354/*
355 * Try to reduce the problem complexity, discard records that have no
356 * matches on the other file. Also, lines that have multiple matches
357 * might be potentially discarded if they happear in a run of discardable.
358 */
359static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2) {
ca557aff 360 long i, nm, rhi, nreff, mlim;
3443546f
LT
361 unsigned long hav;
362 xrecord_t **recs;
363 xrecord_t *rec;
364 char *dis, *dis1, *dis2;
365
ca557aff 366 if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
3443546f
LT
367
368 return -1;
369 }
ca557aff 370 memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
3443546f
LT
371 dis1 = dis;
372 dis2 = dis1 + xdf1->nrec + 1;
373
ca557aff
DL
374 if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT)
375 mlim = XDL_MAX_EQLIMIT;
3443546f
LT
376 for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) {
377 hav = (*recs)->ha;
378 rhi = (long) XDL_HASHLONG(hav, xdf2->hbits);
ca557aff
DL
379 for (nm = 0, rec = xdf2->rhash[rhi]; rec; rec = rec->next)
380 if (rec->ha == hav && ++nm == mlim)
3443546f 381 break;
ca557aff 382 dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
3443546f
LT
383 }
384
ca557aff
DL
385 if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT)
386 mlim = XDL_MAX_EQLIMIT;
3443546f
LT
387 for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) {
388 hav = (*recs)->ha;
389 rhi = (long) XDL_HASHLONG(hav, xdf1->hbits);
ca557aff
DL
390 for (nm = 0, rec = xdf1->rhash[rhi]; rec; rec = rec->next)
391 if (rec->ha == hav && ++nm == mlim)
3443546f 392 break;
ca557aff 393 dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1;
3443546f
LT
394 }
395
396 for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart];
397 i <= xdf1->dend; i++, recs++) {
398 if (dis1[i] == 1 ||
399 (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) {
400 xdf1->rindex[nreff] = i;
401 xdf1->ha[nreff] = (*recs)->ha;
402 nreff++;
403 } else
404 xdf1->rchg[i] = 1;
405 }
406 xdf1->nreff = nreff;
407
408 for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart];
409 i <= xdf2->dend; i++, recs++) {
410 if (dis2[i] == 1 ||
411 (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) {
412 xdf2->rindex[nreff] = i;
413 xdf2->ha[nreff] = (*recs)->ha;
414 nreff++;
415 } else
416 xdf2->rchg[i] = 1;
417 }
418 xdf2->nreff = nreff;
419
420 xdl_free(dis);
421
422 return 0;
423}
424
425
426/*
427 * Early trim initial and terminal matching records.
428 */
429static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
430 long i, lim;
431 xrecord_t **recs1, **recs2;
432
433 recs1 = xdf1->recs;
434 recs2 = xdf2->recs;
435 for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
436 i++, recs1++, recs2++)
437 if ((*recs1)->ha != (*recs2)->ha)
438 break;
439
440 xdf1->dstart = xdf2->dstart = i;
441
442 recs1 = xdf1->recs + xdf1->nrec - 1;
443 recs2 = xdf2->recs + xdf2->nrec - 1;
444 for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)
445 if ((*recs1)->ha != (*recs2)->ha)
446 break;
447
448 xdf1->dend = xdf1->nrec - i - 1;
449 xdf2->dend = xdf2->nrec - i - 1;
450
451 return 0;
452}
453
454
455static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2) {
456
457 if (xdl_trim_ends(xdf1, xdf2) < 0 ||
458 xdl_cleanup_records(xdf1, xdf2) < 0) {
459
460 return -1;
461 }
462
463 return 0;
464}
465