xdiff / xdiffi.con commit xdiff/xdiffi.c: remove unneeded function declarations (21c770b)
   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, see
  17 *  <http://www.gnu.org/licenses/>.
  18 *
  19 *  Davide Libenzi <davidel@xmailserver.org>
  20 *
  21 */
  22
  23#include "xinclude.h"
  24
  25#define XDL_MAX_COST_MIN 256
  26#define XDL_HEUR_MIN_COST 256
  27#define XDL_LINE_MAX (long)((1UL << (CHAR_BIT * sizeof(long) - 1)) - 1)
  28#define XDL_SNAKE_CNT 20
  29#define XDL_K_HEUR 4
  30
  31typedef struct s_xdpsplit {
  32        long i1, i2;
  33        int min_lo, min_hi;
  34} xdpsplit_t;
  35
  36/*
  37 * See "An O(ND) Difference Algorithm and its Variations", by Eugene Myers.
  38 * Basically considers a "box" (off1, off2, lim1, lim2) and scan from both
  39 * the forward diagonal starting from (off1, off2) and the backward diagonal
  40 * starting from (lim1, lim2). If the K values on the same diagonal crosses
  41 * returns the furthest point of reach. We might end up having to expensive
  42 * cases using this algorithm is full, so a little bit of heuristic is needed
  43 * to cut the search and to return a suboptimal point.
  44 */
  45static long xdl_split(unsigned long const *ha1, long off1, long lim1,
  46                      unsigned long const *ha2, long off2, long lim2,
  47                      long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl,
  48                      xdalgoenv_t *xenv) {
  49        long dmin = off1 - lim2, dmax = lim1 - off2;
  50        long fmid = off1 - off2, bmid = lim1 - lim2;
  51        long odd = (fmid - bmid) & 1;
  52        long fmin = fmid, fmax = fmid;
  53        long bmin = bmid, bmax = bmid;
  54        long ec, d, i1, i2, prev1, best, dd, v, k;
  55
  56        /*
  57         * Set initial diagonal values for both forward and backward path.
  58         */
  59        kvdf[fmid] = off1;
  60        kvdb[bmid] = lim1;
  61
  62        for (ec = 1;; ec++) {
  63                int got_snake = 0;
  64
  65                /*
  66                 * We need to extent the diagonal "domain" by one. If the next
  67                 * values exits the box boundaries we need to change it in the
  68                 * opposite direction because (max - min) must be a power of two.
  69                 * Also we initialize the external K value to -1 so that we can
  70                 * avoid extra conditions check inside the core loop.
  71                 */
  72                if (fmin > dmin)
  73                        kvdf[--fmin - 1] = -1;
  74                else
  75                        ++fmin;
  76                if (fmax < dmax)
  77                        kvdf[++fmax + 1] = -1;
  78                else
  79                        --fmax;
  80
  81                for (d = fmax; d >= fmin; d -= 2) {
  82                        if (kvdf[d - 1] >= kvdf[d + 1])
  83                                i1 = kvdf[d - 1] + 1;
  84                        else
  85                                i1 = kvdf[d + 1];
  86                        prev1 = i1;
  87                        i2 = i1 - d;
  88                        for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++);
  89                        if (i1 - prev1 > xenv->snake_cnt)
  90                                got_snake = 1;
  91                        kvdf[d] = i1;
  92                        if (odd && bmin <= d && d <= bmax && kvdb[d] <= i1) {
  93                                spl->i1 = i1;
  94                                spl->i2 = i2;
  95                                spl->min_lo = spl->min_hi = 1;
  96                                return ec;
  97                        }
  98                }
  99
 100                /*
 101                 * We need to extent the diagonal "domain" by one. If the next
 102                 * values exits the box boundaries we need to change it in the
 103                 * opposite direction because (max - min) must be a power of two.
 104                 * Also we initialize the external K value to -1 so that we can
 105                 * avoid extra conditions check inside the core loop.
 106                 */
 107                if (bmin > dmin)
 108                        kvdb[--bmin - 1] = XDL_LINE_MAX;
 109                else
 110                        ++bmin;
 111                if (bmax < dmax)
 112                        kvdb[++bmax + 1] = XDL_LINE_MAX;
 113                else
 114                        --bmax;
 115
 116                for (d = bmax; d >= bmin; d -= 2) {
 117                        if (kvdb[d - 1] < kvdb[d + 1])
 118                                i1 = kvdb[d - 1];
 119                        else
 120                                i1 = kvdb[d + 1] - 1;
 121                        prev1 = i1;
 122                        i2 = i1 - d;
 123                        for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--);
 124                        if (prev1 - i1 > xenv->snake_cnt)
 125                                got_snake = 1;
 126                        kvdb[d] = i1;
 127                        if (!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) {
 128                                spl->i1 = i1;
 129                                spl->i2 = i2;
 130                                spl->min_lo = spl->min_hi = 1;
 131                                return ec;
 132                        }
 133                }
 134
 135                if (need_min)
 136                        continue;
 137
 138                /*
 139                 * If the edit cost is above the heuristic trigger and if
 140                 * we got a good snake, we sample current diagonals to see
 141                 * if some of the, have reached an "interesting" path. Our
 142                 * measure is a function of the distance from the diagonal
 143                 * corner (i1 + i2) penalized with the distance from the
 144                 * mid diagonal itself. If this value is above the current
 145                 * edit cost times a magic factor (XDL_K_HEUR) we consider
 146                 * it interesting.
 147                 */
 148                if (got_snake && ec > xenv->heur_min) {
 149                        for (best = 0, d = fmax; d >= fmin; d -= 2) {
 150                                dd = d > fmid ? d - fmid: fmid - d;
 151                                i1 = kvdf[d];
 152                                i2 = i1 - d;
 153                                v = (i1 - off1) + (i2 - off2) - dd;
 154
 155                                if (v > XDL_K_HEUR * ec && v > best &&
 156                                    off1 + xenv->snake_cnt <= i1 && i1 < lim1 &&
 157                                    off2 + xenv->snake_cnt <= i2 && i2 < lim2) {
 158                                        for (k = 1; ha1[i1 - k] == ha2[i2 - k]; k++)
 159                                                if (k == xenv->snake_cnt) {
 160                                                        best = v;
 161                                                        spl->i1 = i1;
 162                                                        spl->i2 = i2;
 163                                                        break;
 164                                                }
 165                                }
 166                        }
 167                        if (best > 0) {
 168                                spl->min_lo = 1;
 169                                spl->min_hi = 0;
 170                                return ec;
 171                        }
 172
 173                        for (best = 0, d = bmax; d >= bmin; d -= 2) {
 174                                dd = d > bmid ? d - bmid: bmid - d;
 175                                i1 = kvdb[d];
 176                                i2 = i1 - d;
 177                                v = (lim1 - i1) + (lim2 - i2) - dd;
 178
 179                                if (v > XDL_K_HEUR * ec && v > best &&
 180                                    off1 < i1 && i1 <= lim1 - xenv->snake_cnt &&
 181                                    off2 < i2 && i2 <= lim2 - xenv->snake_cnt) {
 182                                        for (k = 0; ha1[i1 + k] == ha2[i2 + k]; k++)
 183                                                if (k == xenv->snake_cnt - 1) {
 184                                                        best = v;
 185                                                        spl->i1 = i1;
 186                                                        spl->i2 = i2;
 187                                                        break;
 188                                                }
 189                                }
 190                        }
 191                        if (best > 0) {
 192                                spl->min_lo = 0;
 193                                spl->min_hi = 1;
 194                                return ec;
 195                        }
 196                }
 197
 198                /*
 199                 * Enough is enough. We spent too much time here and now we collect
 200                 * the furthest reaching path using the (i1 + i2) measure.
 201                 */
 202                if (ec >= xenv->mxcost) {
 203                        long fbest, fbest1, bbest, bbest1;
 204
 205                        fbest = fbest1 = -1;
 206                        for (d = fmax; d >= fmin; d -= 2) {
 207                                i1 = XDL_MIN(kvdf[d], lim1);
 208                                i2 = i1 - d;
 209                                if (lim2 < i2)
 210                                        i1 = lim2 + d, i2 = lim2;
 211                                if (fbest < i1 + i2) {
 212                                        fbest = i1 + i2;
 213                                        fbest1 = i1;
 214                                }
 215                        }
 216
 217                        bbest = bbest1 = XDL_LINE_MAX;
 218                        for (d = bmax; d >= bmin; d -= 2) {
 219                                i1 = XDL_MAX(off1, kvdb[d]);
 220                                i2 = i1 - d;
 221                                if (i2 < off2)
 222                                        i1 = off2 + d, i2 = off2;
 223                                if (i1 + i2 < bbest) {
 224                                        bbest = i1 + i2;
 225                                        bbest1 = i1;
 226                                }
 227                        }
 228
 229                        if ((lim1 + lim2) - bbest < fbest - (off1 + off2)) {
 230                                spl->i1 = fbest1;
 231                                spl->i2 = fbest - fbest1;
 232                                spl->min_lo = 1;
 233                                spl->min_hi = 0;
 234                        } else {
 235                                spl->i1 = bbest1;
 236                                spl->i2 = bbest - bbest1;
 237                                spl->min_lo = 0;
 238                                spl->min_hi = 1;
 239                        }
 240                        return ec;
 241                }
 242        }
 243}
 244
 245
 246/*
 247 * Rule: "Divide et Impera". Recursively split the box in sub-boxes by calling
 248 * the box splitting function. Note that the real job (marking changed lines)
 249 * is done in the two boundary reaching checks.
 250 */
 251int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1,
 252                 diffdata_t *dd2, long off2, long lim2,
 253                 long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv) {
 254        unsigned long const *ha1 = dd1->ha, *ha2 = dd2->ha;
 255
 256        /*
 257         * Shrink the box by walking through each diagonal snake (SW and NE).
 258         */
 259        for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++);
 260        for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--);
 261
 262        /*
 263         * If one dimension is empty, then all records on the other one must
 264         * be obviously changed.
 265         */
 266        if (off1 == lim1) {
 267                char *rchg2 = dd2->rchg;
 268                long *rindex2 = dd2->rindex;
 269
 270                for (; off2 < lim2; off2++)
 271                        rchg2[rindex2[off2]] = 1;
 272        } else if (off2 == lim2) {
 273                char *rchg1 = dd1->rchg;
 274                long *rindex1 = dd1->rindex;
 275
 276                for (; off1 < lim1; off1++)
 277                        rchg1[rindex1[off1]] = 1;
 278        } else {
 279                xdpsplit_t spl;
 280                spl.i1 = spl.i2 = 0;
 281
 282                /*
 283                 * Divide ...
 284                 */
 285                if (xdl_split(ha1, off1, lim1, ha2, off2, lim2, kvdf, kvdb,
 286                              need_min, &spl, xenv) < 0) {
 287
 288                        return -1;
 289                }
 290
 291                /*
 292                 * ... et Impera.
 293                 */
 294                if (xdl_recs_cmp(dd1, off1, spl.i1, dd2, off2, spl.i2,
 295                                 kvdf, kvdb, spl.min_lo, xenv) < 0 ||
 296                    xdl_recs_cmp(dd1, spl.i1, lim1, dd2, spl.i2, lim2,
 297                                 kvdf, kvdb, spl.min_hi, xenv) < 0) {
 298
 299                        return -1;
 300                }
 301        }
 302
 303        return 0;
 304}
 305
 306
 307int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
 308                xdfenv_t *xe) {
 309        long ndiags;
 310        long *kvd, *kvdf, *kvdb;
 311        xdalgoenv_t xenv;
 312        diffdata_t dd1, dd2;
 313
 314        if (XDF_DIFF_ALG(xpp->flags) == XDF_PATIENCE_DIFF)
 315                return xdl_do_patience_diff(mf1, mf2, xpp, xe);
 316
 317        if (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF)
 318                return xdl_do_histogram_diff(mf1, mf2, xpp, xe);
 319
 320        if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) {
 321
 322                return -1;
 323        }
 324
 325        /*
 326         * Allocate and setup K vectors to be used by the differential algorithm.
 327         * One is to store the forward path and one to store the backward path.
 328         */
 329        ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3;
 330        if (!(kvd = (long *) xdl_malloc((2 * ndiags + 2) * sizeof(long)))) {
 331
 332                xdl_free_env(xe);
 333                return -1;
 334        }
 335        kvdf = kvd;
 336        kvdb = kvdf + ndiags;
 337        kvdf += xe->xdf2.nreff + 1;
 338        kvdb += xe->xdf2.nreff + 1;
 339
 340        xenv.mxcost = xdl_bogosqrt(ndiags);
 341        if (xenv.mxcost < XDL_MAX_COST_MIN)
 342                xenv.mxcost = XDL_MAX_COST_MIN;
 343        xenv.snake_cnt = XDL_SNAKE_CNT;
 344        xenv.heur_min = XDL_HEUR_MIN_COST;
 345
 346        dd1.nrec = xe->xdf1.nreff;
 347        dd1.ha = xe->xdf1.ha;
 348        dd1.rchg = xe->xdf1.rchg;
 349        dd1.rindex = xe->xdf1.rindex;
 350        dd2.nrec = xe->xdf2.nreff;
 351        dd2.ha = xe->xdf2.ha;
 352        dd2.rchg = xe->xdf2.rchg;
 353        dd2.rindex = xe->xdf2.rindex;
 354
 355        if (xdl_recs_cmp(&dd1, 0, dd1.nrec, &dd2, 0, dd2.nrec,
 356                         kvdf, kvdb, (xpp->flags & XDF_NEED_MINIMAL) != 0, &xenv) < 0) {
 357
 358                xdl_free(kvd);
 359                xdl_free_env(xe);
 360                return -1;
 361        }
 362
 363        xdl_free(kvd);
 364
 365        return 0;
 366}
 367
 368
 369static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2) {
 370        xdchange_t *xch;
 371
 372        if (!(xch = (xdchange_t *) xdl_malloc(sizeof(xdchange_t))))
 373                return NULL;
 374
 375        xch->next = xscr;
 376        xch->i1 = i1;
 377        xch->i2 = i2;
 378        xch->chg1 = chg1;
 379        xch->chg2 = chg2;
 380        xch->ignore = 0;
 381
 382        return xch;
 383}
 384
 385
 386static int recs_match(xrecord_t *rec1, xrecord_t *rec2, long flags)
 387{
 388        return (rec1->ha == rec2->ha &&
 389                xdl_recmatch(rec1->ptr, rec1->size,
 390                             rec2->ptr, rec2->size,
 391                             flags));
 392}
 393
 394/*
 395 * If a line is indented more than this, get_indent() just returns this value.
 396 * This avoids having to do absurd amounts of work for data that are not
 397 * human-readable text, and also ensures that the output of get_indent fits within
 398 * an int.
 399 */
 400#define MAX_INDENT 200
 401
 402/*
 403 * Return the amount of indentation of the specified line, treating TAB as 8
 404 * columns. Return -1 if line is empty or contains only whitespace. Clamp the
 405 * output value at MAX_INDENT.
 406 */
 407static int get_indent(xrecord_t *rec)
 408{
 409        long i;
 410        int ret = 0;
 411
 412        for (i = 0; i < rec->size; i++) {
 413                char c = rec->ptr[i];
 414
 415                if (!XDL_ISSPACE(c))
 416                        return ret;
 417                else if (c == ' ')
 418                        ret += 1;
 419                else if (c == '\t')
 420                        ret += 8 - ret % 8;
 421                /* ignore other whitespace characters */
 422
 423                if (ret >= MAX_INDENT)
 424                        return MAX_INDENT;
 425        }
 426
 427        /* The line contains only whitespace. */
 428        return -1;
 429}
 430
 431/*
 432 * If more than this number of consecutive blank rows are found, just return this
 433 * value. This avoids requiring O(N^2) work for pathological cases, and also
 434 * ensures that the output of score_split fits in an int.
 435 */
 436#define MAX_BLANKS 20
 437
 438/* Characteristics measured about a hypothetical split position. */
 439struct split_measurement {
 440        /*
 441         * Is the split at the end of the file (aside from any blank lines)?
 442         */
 443        int end_of_file;
 444
 445        /*
 446         * How much is the line immediately following the split indented (or -1 if
 447         * the line is blank):
 448         */
 449        int indent;
 450
 451        /*
 452         * How many consecutive lines above the split are blank?
 453         */
 454        int pre_blank;
 455
 456        /*
 457         * How much is the nearest non-blank line above the split indented (or -1
 458         * if there is no such line)?
 459         */
 460        int pre_indent;
 461
 462        /*
 463         * How many lines after the line following the split are blank?
 464         */
 465        int post_blank;
 466
 467        /*
 468         * How much is the nearest non-blank line after the line following the
 469         * split indented (or -1 if there is no such line)?
 470         */
 471        int post_indent;
 472};
 473
 474struct split_score {
 475        /* The effective indent of this split (smaller is preferred). */
 476        int effective_indent;
 477
 478        /* Penalty for this split (smaller is preferred). */
 479        int penalty;
 480};
 481
 482/*
 483 * Fill m with information about a hypothetical split of xdf above line split.
 484 */
 485static void measure_split(const xdfile_t *xdf, long split,
 486                          struct split_measurement *m)
 487{
 488        long i;
 489
 490        if (split >= xdf->nrec) {
 491                m->end_of_file = 1;
 492                m->indent = -1;
 493        } else {
 494                m->end_of_file = 0;
 495                m->indent = get_indent(xdf->recs[split]);
 496        }
 497
 498        m->pre_blank = 0;
 499        m->pre_indent = -1;
 500        for (i = split - 1; i >= 0; i--) {
 501                m->pre_indent = get_indent(xdf->recs[i]);
 502                if (m->pre_indent != -1)
 503                        break;
 504                m->pre_blank += 1;
 505                if (m->pre_blank == MAX_BLANKS) {
 506                        m->pre_indent = 0;
 507                        break;
 508                }
 509        }
 510
 511        m->post_blank = 0;
 512        m->post_indent = -1;
 513        for (i = split + 1; i < xdf->nrec; i++) {
 514                m->post_indent = get_indent(xdf->recs[i]);
 515                if (m->post_indent != -1)
 516                        break;
 517                m->post_blank += 1;
 518                if (m->post_blank == MAX_BLANKS) {
 519                        m->post_indent = 0;
 520                        break;
 521                }
 522        }
 523}
 524
 525/*
 526 * The empirically-determined weight factors used by score_split() below.
 527 * Larger values means that the position is a less favorable place to split.
 528 *
 529 * Note that scores are only ever compared against each other, so multiplying
 530 * all of these weight/penalty values by the same factor wouldn't change the
 531 * heuristic's behavior. Still, we need to set that arbitrary scale *somehow*.
 532 * In practice, these numbers are chosen to be large enough that they can be
 533 * adjusted relative to each other with sufficient precision despite using
 534 * integer math.
 535 */
 536
 537/* Penalty if there are no non-blank lines before the split */
 538#define START_OF_FILE_PENALTY 1
 539
 540/* Penalty if there are no non-blank lines after the split */
 541#define END_OF_FILE_PENALTY 21
 542
 543/* Multiplier for the number of blank lines around the split */
 544#define TOTAL_BLANK_WEIGHT (-30)
 545
 546/* Multiplier for the number of blank lines after the split */
 547#define POST_BLANK_WEIGHT 6
 548
 549/*
 550 * Penalties applied if the line is indented more than its predecessor
 551 */
 552#define RELATIVE_INDENT_PENALTY (-4)
 553#define RELATIVE_INDENT_WITH_BLANK_PENALTY 10
 554
 555/*
 556 * Penalties applied if the line is indented less than both its predecessor and
 557 * its successor
 558 */
 559#define RELATIVE_OUTDENT_PENALTY 24
 560#define RELATIVE_OUTDENT_WITH_BLANK_PENALTY 17
 561
 562/*
 563 * Penalties applied if the line is indented less than its predecessor but not
 564 * less than its successor
 565 */
 566#define RELATIVE_DEDENT_PENALTY 23
 567#define RELATIVE_DEDENT_WITH_BLANK_PENALTY 17
 568
 569/*
 570 * We only consider whether the sum of the effective indents for splits are
 571 * less than (-1), equal to (0), or greater than (+1) each other. The resulting
 572 * value is multiplied by the following weight and combined with the penalty to
 573 * determine the better of two scores.
 574 */
 575#define INDENT_WEIGHT 60
 576
 577/*
 578 * Compute a badness score for the hypothetical split whose measurements are
 579 * stored in m. The weight factors were determined empirically using the tools and
 580 * corpus described in
 581 *
 582 *     https://github.com/mhagger/diff-slider-tools
 583 *
 584 * Also see that project if you want to improve the weights based on, for example,
 585 * a larger or more diverse corpus.
 586 */
 587static void score_add_split(const struct split_measurement *m, struct split_score *s)
 588{
 589        /*
 590         * A place to accumulate penalty factors (positive makes this index more
 591         * favored):
 592         */
 593        int post_blank, total_blank, indent, any_blanks;
 594
 595        if (m->pre_indent == -1 && m->pre_blank == 0)
 596                s->penalty += START_OF_FILE_PENALTY;
 597
 598        if (m->end_of_file)
 599                s->penalty += END_OF_FILE_PENALTY;
 600
 601        /*
 602         * Set post_blank to the number of blank lines following the split,
 603         * including the line immediately after the split:
 604         */
 605        post_blank = (m->indent == -1) ? 1 + m->post_blank : 0;
 606        total_blank = m->pre_blank + post_blank;
 607
 608        /* Penalties based on nearby blank lines: */
 609        s->penalty += TOTAL_BLANK_WEIGHT * total_blank;
 610        s->penalty += POST_BLANK_WEIGHT * post_blank;
 611
 612        if (m->indent != -1)
 613                indent = m->indent;
 614        else
 615                indent = m->post_indent;
 616
 617        any_blanks = (total_blank != 0);
 618
 619        /* Note that the effective indent is -1 at the end of the file: */
 620        s->effective_indent += indent;
 621
 622        if (indent == -1) {
 623                /* No additional adjustments needed. */
 624        } else if (m->pre_indent == -1) {
 625                /* No additional adjustments needed. */
 626        } else if (indent > m->pre_indent) {
 627                /*
 628                 * The line is indented more than its predecessor.
 629                 */
 630                s->penalty += any_blanks ?
 631                        RELATIVE_INDENT_WITH_BLANK_PENALTY :
 632                        RELATIVE_INDENT_PENALTY;
 633        } else if (indent == m->pre_indent) {
 634                /*
 635                 * The line has the same indentation level as its predecessor.
 636                 * No additional adjustments needed.
 637                 */
 638        } else {
 639                /*
 640                 * The line is indented less than its predecessor. It could be
 641                 * the block terminator of the previous block, but it could
 642                 * also be the start of a new block (e.g., an "else" block, or
 643                 * maybe the previous block didn't have a block terminator).
 644                 * Try to distinguish those cases based on what comes next:
 645                 */
 646                if (m->post_indent != -1 && m->post_indent > indent) {
 647                        /*
 648                         * The following line is indented more. So it is likely
 649                         * that this line is the start of a block.
 650                         */
 651                        s->penalty += any_blanks ?
 652                                RELATIVE_OUTDENT_WITH_BLANK_PENALTY :
 653                                RELATIVE_OUTDENT_PENALTY;
 654                } else {
 655                        /*
 656                         * That was probably the end of a block.
 657                         */
 658                        s->penalty += any_blanks ?
 659                                RELATIVE_DEDENT_WITH_BLANK_PENALTY :
 660                                RELATIVE_DEDENT_PENALTY;
 661                }
 662        }
 663}
 664
 665static int score_cmp(struct split_score *s1, struct split_score *s2)
 666{
 667        /* -1 if s1.effective_indent < s2->effective_indent, etc. */
 668        int cmp_indents = ((s1->effective_indent > s2->effective_indent) -
 669                           (s1->effective_indent < s2->effective_indent));
 670
 671        return INDENT_WEIGHT * cmp_indents + (s1->penalty - s2->penalty);
 672}
 673
 674/*
 675 * Represent a group of changed lines in an xdfile_t (i.e., a contiguous group
 676 * of lines that was inserted or deleted from the corresponding version of the
 677 * file). We consider there to be such a group at the beginning of the file, at
 678 * the end of the file, and between any two unchanged lines, though most such
 679 * groups will usually be empty.
 680 *
 681 * If the first line in a group is equal to the line following the group, then
 682 * the group can be slid down. Similarly, if the last line in a group is equal
 683 * to the line preceding the group, then the group can be slid up. See
 684 * group_slide_down() and group_slide_up().
 685 *
 686 * Note that loops that are testing for changed lines in xdf->rchg do not need
 687 * index bounding since the array is prepared with a zero at position -1 and N.
 688 */
 689struct xdlgroup {
 690        /*
 691         * The index of the first changed line in the group, or the index of
 692         * the unchanged line above which the (empty) group is located.
 693         */
 694        long start;
 695
 696        /*
 697         * The index of the first unchanged line after the group. For an empty
 698         * group, end is equal to start.
 699         */
 700        long end;
 701};
 702
 703/*
 704 * Initialize g to point at the first group in xdf.
 705 */
 706static void group_init(xdfile_t *xdf, struct xdlgroup *g)
 707{
 708        g->start = g->end = 0;
 709        while (xdf->rchg[g->end])
 710                g->end++;
 711}
 712
 713/*
 714 * Move g to describe the next (possibly empty) group in xdf and return 0. If g
 715 * is already at the end of the file, do nothing and return -1.
 716 */
 717static inline int group_next(xdfile_t *xdf, struct xdlgroup *g)
 718{
 719        if (g->end == xdf->nrec)
 720                return -1;
 721
 722        g->start = g->end + 1;
 723        for (g->end = g->start; xdf->rchg[g->end]; g->end++)
 724                ;
 725
 726        return 0;
 727}
 728
 729/*
 730 * Move g to describe the previous (possibly empty) group in xdf and return 0.
 731 * If g is already at the beginning of the file, do nothing and return -1.
 732 */
 733static inline int group_previous(xdfile_t *xdf, struct xdlgroup *g)
 734{
 735        if (g->start == 0)
 736                return -1;
 737
 738        g->end = g->start - 1;
 739        for (g->start = g->end; xdf->rchg[g->start - 1]; g->start--)
 740                ;
 741
 742        return 0;
 743}
 744
 745/*
 746 * If g can be slid toward the end of the file, do so, and if it bumps into a
 747 * following group, expand this group to include it. Return 0 on success or -1
 748 * if g cannot be slid down.
 749 */
 750static int group_slide_down(xdfile_t *xdf, struct xdlgroup *g, long flags)
 751{
 752        if (g->end < xdf->nrec &&
 753            recs_match(xdf->recs[g->start], xdf->recs[g->end], flags)) {
 754                xdf->rchg[g->start++] = 0;
 755                xdf->rchg[g->end++] = 1;
 756
 757                while (xdf->rchg[g->end])
 758                        g->end++;
 759
 760                return 0;
 761        } else {
 762                return -1;
 763        }
 764}
 765
 766/*
 767 * If g can be slid toward the beginning of the file, do so, and if it bumps
 768 * into a previous group, expand this group to include it. Return 0 on success
 769 * or -1 if g cannot be slid up.
 770 */
 771static int group_slide_up(xdfile_t *xdf, struct xdlgroup *g, long flags)
 772{
 773        if (g->start > 0 &&
 774            recs_match(xdf->recs[g->start - 1], xdf->recs[g->end - 1], flags)) {
 775                xdf->rchg[--g->start] = 1;
 776                xdf->rchg[--g->end] = 0;
 777
 778                while (xdf->rchg[g->start - 1])
 779                        g->start--;
 780
 781                return 0;
 782        } else {
 783                return -1;
 784        }
 785}
 786
 787static void xdl_bug(const char *msg)
 788{
 789        fprintf(stderr, "BUG: %s\n", msg);
 790        exit(1);
 791}
 792
 793/*
 794 * Move back and forward change groups for a consistent and pretty diff output.
 795 * This also helps in finding joinable change groups and reducing the diff
 796 * size.
 797 */
 798int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
 799        struct xdlgroup g, go;
 800        long earliest_end, end_matching_other;
 801        long groupsize;
 802
 803        group_init(xdf, &g);
 804        group_init(xdfo, &go);
 805
 806        while (1) {
 807                /* If the group is empty in the to-be-compacted file, skip it: */
 808                if (g.end == g.start)
 809                        goto next;
 810
 811                /*
 812                 * Now shift the change up and then down as far as possible in
 813                 * each direction. If it bumps into any other changes, merge them.
 814                 */
 815                do {
 816                        groupsize = g.end - g.start;
 817
 818                        /*
 819                         * Keep track of the last "end" index that causes this
 820                         * group to align with a group of changed lines in the
 821                         * other file. -1 indicates that we haven't found such
 822                         * a match yet:
 823                         */
 824                        end_matching_other = -1;
 825
 826                        /* Shift the group backward as much as possible: */
 827                        while (!group_slide_up(xdf, &g, flags))
 828                                if (group_previous(xdfo, &go))
 829                                        xdl_bug("group sync broken sliding up");
 830
 831                        /*
 832                         * This is this highest that this group can be shifted.
 833                         * Record its end index:
 834                         */
 835                        earliest_end = g.end;
 836
 837                        if (go.end > go.start)
 838                                end_matching_other = g.end;
 839
 840                        /* Now shift the group forward as far as possible: */
 841                        while (1) {
 842                                if (group_slide_down(xdf, &g, flags))
 843                                        break;
 844                                if (group_next(xdfo, &go))
 845                                        xdl_bug("group sync broken sliding down");
 846
 847                                if (go.end > go.start)
 848                                        end_matching_other = g.end;
 849                        }
 850                } while (groupsize != g.end - g.start);
 851
 852                /*
 853                 * If the group can be shifted, then we can possibly use this
 854                 * freedom to produce a more intuitive diff.
 855                 *
 856                 * The group is currently shifted as far down as possible, so the
 857                 * heuristics below only have to handle upwards shifts.
 858                 */
 859
 860                if (g.end == earliest_end) {
 861                        /* no shifting was possible */
 862                } else if (end_matching_other != -1) {
 863                        /*
 864                         * Move the possibly merged group of changes back to line
 865                         * up with the last group of changes from the other file
 866                         * that it can align with.
 867                         */
 868                        while (go.end == go.start) {
 869                                if (group_slide_up(xdf, &g, flags))
 870                                        xdl_bug("match disappeared");
 871                                if (group_previous(xdfo, &go))
 872                                        xdl_bug("group sync broken sliding to match");
 873                        }
 874                } else if (flags & XDF_INDENT_HEURISTIC) {
 875                        /*
 876                         * Indent heuristic: a group of pure add/delete lines
 877                         * implies two splits, one between the end of the "before"
 878                         * context and the start of the group, and another between
 879                         * the end of the group and the beginning of the "after"
 880                         * context. Some splits are aesthetically better and some
 881                         * are worse. We compute a badness "score" for each split,
 882                         * and add the scores for the two splits to define a
 883                         * "score" for each position that the group can be shifted
 884                         * to. Then we pick the shift with the lowest score.
 885                         */
 886                        long shift, best_shift = -1;
 887                        struct split_score best_score;
 888
 889                        for (shift = earliest_end; shift <= g.end; shift++) {
 890                                struct split_measurement m;
 891                                struct split_score score = {0, 0};
 892
 893                                measure_split(xdf, shift, &m);
 894                                score_add_split(&m, &score);
 895                                measure_split(xdf, shift - groupsize, &m);
 896                                score_add_split(&m, &score);
 897                                if (best_shift == -1 ||
 898                                    score_cmp(&score, &best_score) <= 0) {
 899                                        best_score.effective_indent = score.effective_indent;
 900                                        best_score.penalty = score.penalty;
 901                                        best_shift = shift;
 902                                }
 903                        }
 904
 905                        while (g.end > best_shift) {
 906                                if (group_slide_up(xdf, &g, flags))
 907                                        xdl_bug("best shift unreached");
 908                                if (group_previous(xdfo, &go))
 909                                        xdl_bug("group sync broken sliding to blank line");
 910                        }
 911                }
 912
 913        next:
 914                /* Move past the just-processed group: */
 915                if (group_next(xdf, &g))
 916                        break;
 917                if (group_next(xdfo, &go))
 918                        xdl_bug("group sync broken moving to next group");
 919        }
 920
 921        if (!group_next(xdfo, &go))
 922                xdl_bug("group sync broken at end of file");
 923
 924        return 0;
 925}
 926
 927
 928int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr) {
 929        xdchange_t *cscr = NULL, *xch;
 930        char *rchg1 = xe->xdf1.rchg, *rchg2 = xe->xdf2.rchg;
 931        long i1, i2, l1, l2;
 932
 933        /*
 934         * Trivial. Collects "groups" of changes and creates an edit script.
 935         */
 936        for (i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >= 0 || i2 >= 0; i1--, i2--)
 937                if (rchg1[i1 - 1] || rchg2[i2 - 1]) {
 938                        for (l1 = i1; rchg1[i1 - 1]; i1--);
 939                        for (l2 = i2; rchg2[i2 - 1]; i2--);
 940
 941                        if (!(xch = xdl_add_change(cscr, i1, i2, l1 - i1, l2 - i2))) {
 942                                xdl_free_script(cscr);
 943                                return -1;
 944                        }
 945                        cscr = xch;
 946                }
 947
 948        *xscr = cscr;
 949
 950        return 0;
 951}
 952
 953
 954void xdl_free_script(xdchange_t *xscr) {
 955        xdchange_t *xch;
 956
 957        while ((xch = xscr) != NULL) {
 958                xscr = xscr->next;
 959                xdl_free(xch);
 960        }
 961}
 962
 963static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
 964                              xdemitconf_t const *xecfg)
 965{
 966        xdchange_t *xch, *xche;
 967
 968        for (xch = xscr; xch; xch = xche->next) {
 969                xche = xdl_get_hunk(&xch, xecfg);
 970                if (!xch)
 971                        break;
 972                if (xecfg->hunk_func(xch->i1, xche->i1 + xche->chg1 - xch->i1,
 973                                     xch->i2, xche->i2 + xche->chg2 - xch->i2,
 974                                     ecb->priv) < 0)
 975                        return -1;
 976        }
 977        return 0;
 978}
 979
 980static void xdl_mark_ignorable(xdchange_t *xscr, xdfenv_t *xe, long flags)
 981{
 982        xdchange_t *xch;
 983
 984        for (xch = xscr; xch; xch = xch->next) {
 985                int ignore = 1;
 986                xrecord_t **rec;
 987                long i;
 988
 989                rec = &xe->xdf1.recs[xch->i1];
 990                for (i = 0; i < xch->chg1 && ignore; i++)
 991                        ignore = xdl_blankline(rec[i]->ptr, rec[i]->size, flags);
 992
 993                rec = &xe->xdf2.recs[xch->i2];
 994                for (i = 0; i < xch->chg2 && ignore; i++)
 995                        ignore = xdl_blankline(rec[i]->ptr, rec[i]->size, flags);
 996
 997                xch->ignore = ignore;
 998        }
 999}
1000
1001int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
1002             xdemitconf_t const *xecfg, xdemitcb_t *ecb) {
1003        xdchange_t *xscr;
1004        xdfenv_t xe;
1005        emit_func_t ef = xecfg->hunk_func ? xdl_call_hunk_func : xdl_emit_diff;
1006
1007        if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) {
1008
1009                return -1;
1010        }
1011        if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 ||
1012            xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 ||
1013            xdl_build_script(&xe, &xscr) < 0) {
1014
1015                xdl_free_env(&xe);
1016                return -1;
1017        }
1018        if (xscr) {
1019                if (xpp->flags & XDF_IGNORE_BLANK_LINES)
1020                        xdl_mark_ignorable(xscr, &xe, xpp->flags);
1021
1022                if (ef(&xe, xscr, ecb, xecfg) < 0) {
1023
1024                        xdl_free_script(xscr);
1025                        xdl_free_env(&xe);
1026                        return -1;
1027                }
1028                xdl_free_script(xscr);
1029        }
1030        xdl_free_env(&xe);
1031
1032        return 0;
1033}