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