receive-pack: do not insist on fast-forward outside refs/heads/
[git/mingw/4msysgit/wingit-dll.git] / xdiff / xprepare.c
blob1be7b3195059bebffe83d17301f3ff0fb261799e
1 /*
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>
23 #include "xinclude.h"
27 #define XDL_KPDIS_RUN 4
28 #define XDL_MAX_EQLIMIT 1024
32 typedef 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;
40 typedef struct s_xdlclassifier {
41 unsigned int hbits;
42 long hsize;
43 xdlclass_t **rchash;
44 chastore_t ncha;
45 long count;
46 long flags;
47 } xdlclassifier_t;
52 static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags);
53 static void xdl_free_classifier(xdlclassifier_t *cf);
54 static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
55 xrecord_t *rec);
56 static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
57 xdlclassifier_t *cf, xdfile_t *xdf);
58 static void xdl_free_ctx(xdfile_t *xdf);
59 static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
60 static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2);
61 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
62 static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2);
67 static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
68 long i;
70 cf->flags = flags;
72 cf->hbits = xdl_hashbits((unsigned int) size);
73 cf->hsize = 1 << cf->hbits;
75 if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) {
77 return -1;
79 if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) {
81 xdl_cha_free(&cf->ncha);
82 return -1;
84 for (i = 0; i < cf->hsize; i++)
85 cf->rchash[i] = NULL;
87 cf->count = 0;
89 return 0;
93 static void xdl_free_classifier(xdlclassifier_t *cf) {
95 xdl_free(cf->rchash);
96 xdl_cha_free(&cf->ncha);
100 static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
101 xrecord_t *rec) {
102 long hi;
103 char const *line;
104 xdlclass_t *rcrec;
106 line = rec->ptr;
107 hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
108 for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
109 if (rcrec->ha == rec->ha &&
110 xdl_recmatch(rcrec->line, rcrec->size,
111 rec->ptr, rec->size, cf->flags))
112 break;
114 if (!rcrec) {
115 if (!(rcrec = xdl_cha_alloc(&cf->ncha))) {
117 return -1;
119 rcrec->idx = cf->count++;
120 rcrec->line = line;
121 rcrec->size = rec->size;
122 rcrec->ha = rec->ha;
123 rcrec->next = cf->rchash[hi];
124 cf->rchash[hi] = rcrec;
127 rec->ha = (unsigned long) rcrec->idx;
129 hi = (long) XDL_HASHLONG(rec->ha, hbits);
130 rec->next = rhash[hi];
131 rhash[hi] = rec;
133 return 0;
137 static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
138 xdlclassifier_t *cf, xdfile_t *xdf) {
139 unsigned int hbits;
140 long i, nrec, hsize, bsize;
141 unsigned long hav;
142 char const *blk, *cur, *top, *prev;
143 xrecord_t *crec;
144 xrecord_t **recs, **rrecs;
145 xrecord_t **rhash;
146 unsigned long *ha;
147 char *rchg;
148 long *rindex;
150 if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) {
152 return -1;
154 if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) {
156 xdl_cha_free(&xdf->rcha);
157 return -1;
160 hbits = xdl_hashbits((unsigned int) narec);
161 hsize = 1 << hbits;
162 if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) {
164 xdl_free(recs);
165 xdl_cha_free(&xdf->rcha);
166 return -1;
168 for (i = 0; i < hsize; i++)
169 rhash[i] = NULL;
171 nrec = 0;
172 if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) {
173 for (top = blk + bsize;;) {
174 if (cur >= top) {
175 if (!(cur = blk = xdl_mmfile_next(mf, &bsize)))
176 break;
177 top = blk + bsize;
179 prev = cur;
180 hav = xdl_hash_record(&cur, top, xpp->flags);
181 if (nrec >= narec) {
182 narec *= 2;
183 if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) {
185 xdl_free(rhash);
186 xdl_free(recs);
187 xdl_cha_free(&xdf->rcha);
188 return -1;
190 recs = rrecs;
192 if (!(crec = xdl_cha_alloc(&xdf->rcha))) {
194 xdl_free(rhash);
195 xdl_free(recs);
196 xdl_cha_free(&xdf->rcha);
197 return -1;
199 crec->ptr = prev;
200 crec->size = (long) (cur - prev);
201 crec->ha = hav;
202 recs[nrec++] = crec;
204 if (xdl_classify_record(cf, rhash, hbits, crec) < 0) {
206 xdl_free(rhash);
207 xdl_free(recs);
208 xdl_cha_free(&xdf->rcha);
209 return -1;
214 if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) {
216 xdl_free(rhash);
217 xdl_free(recs);
218 xdl_cha_free(&xdf->rcha);
219 return -1;
221 memset(rchg, 0, (nrec + 2) * sizeof(char));
223 if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) {
225 xdl_free(rchg);
226 xdl_free(rhash);
227 xdl_free(recs);
228 xdl_cha_free(&xdf->rcha);
229 return -1;
231 if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) {
233 xdl_free(rindex);
234 xdl_free(rchg);
235 xdl_free(rhash);
236 xdl_free(recs);
237 xdl_cha_free(&xdf->rcha);
238 return -1;
241 xdf->nrec = nrec;
242 xdf->recs = recs;
243 xdf->hbits = hbits;
244 xdf->rhash = rhash;
245 xdf->rchg = rchg + 1;
246 xdf->rindex = rindex;
247 xdf->nreff = 0;
248 xdf->ha = ha;
249 xdf->dstart = 0;
250 xdf->dend = nrec - 1;
252 return 0;
256 static void xdl_free_ctx(xdfile_t *xdf) {
258 xdl_free(xdf->rhash);
259 xdl_free(xdf->rindex);
260 xdl_free(xdf->rchg - 1);
261 xdl_free(xdf->ha);
262 xdl_free(xdf->recs);
263 xdl_cha_free(&xdf->rcha);
267 int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
268 xdfenv_t *xe) {
269 long enl1, enl2;
270 xdlclassifier_t cf;
272 enl1 = xdl_guess_lines(mf1) + 1;
273 enl2 = xdl_guess_lines(mf2) + 1;
275 if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) {
277 return -1;
280 if (xdl_prepare_ctx(mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
282 xdl_free_classifier(&cf);
283 return -1;
285 if (xdl_prepare_ctx(mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
287 xdl_free_ctx(&xe->xdf1);
288 xdl_free_classifier(&cf);
289 return -1;
292 xdl_free_classifier(&cf);
294 if (xdl_optimize_ctxs(&xe->xdf1, &xe->xdf2) < 0) {
296 xdl_free_ctx(&xe->xdf2);
297 xdl_free_ctx(&xe->xdf1);
298 return -1;
301 return 0;
305 void xdl_free_env(xdfenv_t *xe) {
307 xdl_free_ctx(&xe->xdf2);
308 xdl_free_ctx(&xe->xdf1);
312 static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
313 long r, rdis0, rpdis0, rdis1, rpdis1;
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.
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;
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).
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;
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.
349 if (rdis1 == 0)
350 return 0;
351 rdis1 += rdis0;
352 rpdis1 += rpdis0;
354 return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1);
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.
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;
370 if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) {
372 return -1;
374 memset(dis, 0, xdf1->nrec + xdf2->nrec + 2);
375 dis1 = dis;
376 dis2 = dis1 + xdf1->nrec + 1;
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;
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;
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;
410 xdf1->nreff = nreff;
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;
422 xdf2->nreff = nreff;
424 xdl_free(dis);
426 return 0;
431 * Early trim initial and terminal matching records.
433 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
434 long i, lim;
435 xrecord_t **recs1, **recs2;
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;
444 xdf1->dstart = xdf2->dstart = i;
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;
452 xdf1->dend = xdf1->nrec - i - 1;
453 xdf2->dend = xdf2->nrec - i - 1;
455 return 0;
459 static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2) {
461 if (xdl_trim_ends(xdf1, xdf2) < 0 ||
462 xdl_cleanup_records(xdf1, xdf2) < 0) {
464 return -1;
467 return 0;