Git fork

Merge branch 'en/xdiff-cleanup'

A lot of code clean-up of xdiff.
Split out of a larger topic.

* en/xdiff-cleanup:
xdiff: change type of xdfile_t.changed from char to bool
xdiff: add macros DISCARD(0), KEEP(1), INVESTIGATE(2) in xprepare.c
xdiff: rename rchg -> changed in xdfile_t
xdiff: delete chastore from xdfile_t
xdiff: delete fields ha, line, size in xdlclass_t in favor of an xrecord_t
xdiff: delete redundant array xdfile_t.ha
xdiff: delete struct diffdata_t
xdiff: delete local variables that alias fields in xrecord_t
xdiff: delete superfluous function xdl_get_rec() in xemit
xdiff: delete unnecessary fields from xrecord_t and xdfile_t
xdiff: delete local variables and initialize/free xdfile_t directly
xdiff: delete static forward declarations in xprepare

+268 -336
+45 -57
xdiff/xdiffi.c
··· 22 22 23 23 #include "xinclude.h" 24 24 25 + static unsigned long get_hash(xdfile_t *xdf, long index) 26 + { 27 + return xdf->recs[xdf->rindex[index]].ha; 28 + } 29 + 25 30 #define XDL_MAX_COST_MIN 256 26 31 #define XDL_HEUR_MIN_COST 256 27 32 #define XDL_LINE_MAX (long)((1UL << (CHAR_BIT * sizeof(long) - 1)) - 1) ··· 42 47 * using this algorithm, so a little bit of heuristic is needed to cut the 43 48 * search and to return a suboptimal point. 44 49 */ 45 - static long xdl_split(unsigned long const *ha1, long off1, long lim1, 46 - unsigned long const *ha2, long off2, long lim2, 50 + static long xdl_split(xdfile_t *xdf1, long off1, long lim1, 51 + xdfile_t *xdf2, long off2, long lim2, 47 52 long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl, 48 53 xdalgoenv_t *xenv) { 49 54 long dmin = off1 - lim2, dmax = lim1 - off2; ··· 87 92 i1 = kvdf[d + 1]; 88 93 prev1 = i1; 89 94 i2 = i1 - d; 90 - for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++); 95 + for (; i1 < lim1 && i2 < lim2 && get_hash(xdf1, i1) == get_hash(xdf2, i2); i1++, i2++); 91 96 if (i1 - prev1 > xenv->snake_cnt) 92 97 got_snake = 1; 93 98 kvdf[d] = i1; ··· 124 129 i1 = kvdb[d + 1] - 1; 125 130 prev1 = i1; 126 131 i2 = i1 - d; 127 - for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--); 132 + for (; i1 > off1 && i2 > off2 && get_hash(xdf1, i1 - 1) == get_hash(xdf2, i2 - 1); i1--, i2--); 128 133 if (prev1 - i1 > xenv->snake_cnt) 129 134 got_snake = 1; 130 135 kvdb[d] = i1; ··· 159 164 if (v > XDL_K_HEUR * ec && v > best && 160 165 off1 + xenv->snake_cnt <= i1 && i1 < lim1 && 161 166 off2 + xenv->snake_cnt <= i2 && i2 < lim2) { 162 - for (k = 1; ha1[i1 - k] == ha2[i2 - k]; k++) 167 + for (k = 1; get_hash(xdf1, i1 - k) == get_hash(xdf2, i2 - k); k++) 163 168 if (k == xenv->snake_cnt) { 164 169 best = v; 165 170 spl->i1 = i1; ··· 183 188 if (v > XDL_K_HEUR * ec && v > best && 184 189 off1 < i1 && i1 <= lim1 - xenv->snake_cnt && 185 190 off2 < i2 && i2 <= lim2 - xenv->snake_cnt) { 186 - for (k = 0; ha1[i1 + k] == ha2[i2 + k]; k++) 191 + for (k = 0; get_hash(xdf1, i1 + k) == get_hash(xdf2, i2 + k); k++) 187 192 if (k == xenv->snake_cnt - 1) { 188 193 best = v; 189 194 spl->i1 = i1; ··· 257 262 * sub-boxes by calling the box splitting function. Note that the real job 258 263 * (marking changed lines) is done in the two boundary reaching checks. 259 264 */ 260 - int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1, 261 - diffdata_t *dd2, long off2, long lim2, 265 + int xdl_recs_cmp(xdfile_t *xdf1, long off1, long lim1, 266 + xdfile_t *xdf2, long off2, long lim2, 262 267 long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv) { 263 - unsigned long const *ha1 = dd1->ha, *ha2 = dd2->ha; 264 268 265 269 /* 266 270 * Shrink the box by walking through each diagonal snake (SW and NE). 267 271 */ 268 - for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++); 269 - for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--); 272 + for (; off1 < lim1 && off2 < lim2 && get_hash(xdf1, off1) == get_hash(xdf2, off2); off1++, off2++); 273 + for (; off1 < lim1 && off2 < lim2 && get_hash(xdf1, lim1 - 1) == get_hash(xdf2, lim2 - 1); lim1--, lim2--); 270 274 271 275 /* 272 276 * If one dimension is empty, then all records on the other one must 273 277 * be obviously changed. 274 278 */ 275 279 if (off1 == lim1) { 276 - char *rchg2 = dd2->rchg; 277 - long *rindex2 = dd2->rindex; 278 - 279 280 for (; off2 < lim2; off2++) 280 - rchg2[rindex2[off2]] = 1; 281 + xdf2->changed[xdf2->rindex[off2]] = true; 281 282 } else if (off2 == lim2) { 282 - char *rchg1 = dd1->rchg; 283 - long *rindex1 = dd1->rindex; 284 - 285 283 for (; off1 < lim1; off1++) 286 - rchg1[rindex1[off1]] = 1; 284 + xdf1->changed[xdf1->rindex[off1]] = true; 287 285 } else { 288 286 xdpsplit_t spl; 289 287 spl.i1 = spl.i2 = 0; ··· 291 289 /* 292 290 * Divide ... 293 291 */ 294 - if (xdl_split(ha1, off1, lim1, ha2, off2, lim2, kvdf, kvdb, 292 + if (xdl_split(xdf1, off1, lim1, xdf2, off2, lim2, kvdf, kvdb, 295 293 need_min, &spl, xenv) < 0) { 296 294 297 295 return -1; ··· 300 298 /* 301 299 * ... et Impera. 302 300 */ 303 - if (xdl_recs_cmp(dd1, off1, spl.i1, dd2, off2, spl.i2, 301 + if (xdl_recs_cmp(xdf1, off1, spl.i1, xdf2, off2, spl.i2, 304 302 kvdf, kvdb, spl.min_lo, xenv) < 0 || 305 - xdl_recs_cmp(dd1, spl.i1, lim1, dd2, spl.i2, lim2, 303 + xdl_recs_cmp(xdf1, spl.i1, lim1, xdf2, spl.i2, lim2, 306 304 kvdf, kvdb, spl.min_hi, xenv) < 0) { 307 305 308 306 return -1; ··· 318 316 long ndiags; 319 317 long *kvd, *kvdf, *kvdb; 320 318 xdalgoenv_t xenv; 321 - diffdata_t dd1, dd2; 322 319 int res; 323 320 324 321 if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) ··· 357 354 xenv.snake_cnt = XDL_SNAKE_CNT; 358 355 xenv.heur_min = XDL_HEUR_MIN_COST; 359 356 360 - dd1.nrec = xe->xdf1.nreff; 361 - dd1.ha = xe->xdf1.ha; 362 - dd1.rchg = xe->xdf1.rchg; 363 - dd1.rindex = xe->xdf1.rindex; 364 - dd2.nrec = xe->xdf2.nreff; 365 - dd2.ha = xe->xdf2.ha; 366 - dd2.rchg = xe->xdf2.rchg; 367 - dd2.rindex = xe->xdf2.rindex; 368 - 369 - res = xdl_recs_cmp(&dd1, 0, dd1.nrec, &dd2, 0, dd2.nrec, 357 + res = xdl_recs_cmp(&xe->xdf1, 0, xe->xdf1.nreff, &xe->xdf2, 0, xe->xdf2.nreff, 370 358 kvdf, kvdb, (xpp->flags & XDF_NEED_MINIMAL) != 0, 371 359 &xenv); 372 360 xdl_free(kvd); ··· 501 489 m->indent = -1; 502 490 } else { 503 491 m->end_of_file = 0; 504 - m->indent = get_indent(xdf->recs[split]); 492 + m->indent = get_indent(&xdf->recs[split]); 505 493 } 506 494 507 495 m->pre_blank = 0; 508 496 m->pre_indent = -1; 509 497 for (i = split - 1; i >= 0; i--) { 510 - m->pre_indent = get_indent(xdf->recs[i]); 498 + m->pre_indent = get_indent(&xdf->recs[i]); 511 499 if (m->pre_indent != -1) 512 500 break; 513 501 m->pre_blank += 1; ··· 520 508 m->post_blank = 0; 521 509 m->post_indent = -1; 522 510 for (i = split + 1; i < xdf->nrec; i++) { 523 - m->post_indent = get_indent(xdf->recs[i]); 511 + m->post_indent = get_indent(&xdf->recs[i]); 524 512 if (m->post_indent != -1) 525 513 break; 526 514 m->post_blank += 1; ··· 720 708 static void group_init(xdfile_t *xdf, struct xdlgroup *g) 721 709 { 722 710 g->start = g->end = 0; 723 - while (xdf->rchg[g->end]) 711 + while (xdf->changed[g->end]) 724 712 g->end++; 725 713 } 726 714 ··· 734 722 return -1; 735 723 736 724 g->start = g->end + 1; 737 - for (g->end = g->start; xdf->rchg[g->end]; g->end++) 725 + for (g->end = g->start; xdf->changed[g->end]; g->end++) 738 726 ; 739 727 740 728 return 0; ··· 750 738 return -1; 751 739 752 740 g->end = g->start - 1; 753 - for (g->start = g->end; xdf->rchg[g->start - 1]; g->start--) 741 + for (g->start = g->end; xdf->changed[g->start - 1]; g->start--) 754 742 ; 755 743 756 744 return 0; ··· 764 752 static int group_slide_down(xdfile_t *xdf, struct xdlgroup *g) 765 753 { 766 754 if (g->end < xdf->nrec && 767 - recs_match(xdf->recs[g->start], xdf->recs[g->end])) { 768 - xdf->rchg[g->start++] = 0; 769 - xdf->rchg[g->end++] = 1; 755 + recs_match(&xdf->recs[g->start], &xdf->recs[g->end])) { 756 + xdf->changed[g->start++] = false; 757 + xdf->changed[g->end++] = true; 770 758 771 - while (xdf->rchg[g->end]) 759 + while (xdf->changed[g->end]) 772 760 g->end++; 773 761 774 762 return 0; ··· 785 773 static int group_slide_up(xdfile_t *xdf, struct xdlgroup *g) 786 774 { 787 775 if (g->start > 0 && 788 - recs_match(xdf->recs[g->start - 1], xdf->recs[g->end - 1])) { 789 - xdf->rchg[--g->start] = 1; 790 - xdf->rchg[--g->end] = 0; 776 + recs_match(&xdf->recs[g->start - 1], &xdf->recs[g->end - 1])) { 777 + xdf->changed[--g->start] = true; 778 + xdf->changed[--g->end] = false; 791 779 792 - while (xdf->rchg[g->start - 1]) 780 + while (xdf->changed[g->start - 1]) 793 781 g->start--; 794 782 795 783 return 0; ··· 944 932 945 933 int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr) { 946 934 xdchange_t *cscr = NULL, *xch; 947 - char *rchg1 = xe->xdf1.rchg, *rchg2 = xe->xdf2.rchg; 935 + bool *changed1 = xe->xdf1.changed, *changed2 = xe->xdf2.changed; 948 936 long i1, i2, l1, l2; 949 937 950 938 /* 951 939 * Trivial. Collects "groups" of changes and creates an edit script. 952 940 */ 953 941 for (i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >= 0 || i2 >= 0; i1--, i2--) 954 - if (rchg1[i1 - 1] || rchg2[i2 - 1]) { 955 - for (l1 = i1; rchg1[i1 - 1]; i1--); 956 - for (l2 = i2; rchg2[i2 - 1]; i2--); 942 + if (changed1[i1 - 1] || changed2[i2 - 1]) { 943 + for (l1 = i1; changed1[i1 - 1]; i1--); 944 + for (l2 = i2; changed2[i2 - 1]; i2--); 957 945 958 946 if (!(xch = xdl_add_change(cscr, i1, i2, l1 - i1, l2 - i2))) { 959 947 xdl_free_script(cscr); ··· 1000 988 1001 989 for (xch = xscr; xch; xch = xch->next) { 1002 990 int ignore = 1; 1003 - xrecord_t **rec; 991 + xrecord_t *rec; 1004 992 long i; 1005 993 1006 994 rec = &xe->xdf1.recs[xch->i1]; 1007 995 for (i = 0; i < xch->chg1 && ignore; i++) 1008 - ignore = xdl_blankline(rec[i]->ptr, rec[i]->size, flags); 996 + ignore = xdl_blankline(rec[i].ptr, rec[i].size, flags); 1009 997 1010 998 rec = &xe->xdf2.recs[xch->i2]; 1011 999 for (i = 0; i < xch->chg2 && ignore; i++) 1012 - ignore = xdl_blankline(rec[i]->ptr, rec[i]->size, flags); 1000 + ignore = xdl_blankline(rec[i].ptr, rec[i].size, flags); 1013 1001 1014 1002 xch->ignore = ignore; 1015 1003 } ··· 1033 1021 xdchange_t *xch; 1034 1022 1035 1023 for (xch = xscr; xch; xch = xch->next) { 1036 - xrecord_t **rec; 1024 + xrecord_t *rec; 1037 1025 int ignore = 1; 1038 1026 long i; 1039 1027 ··· 1045 1033 1046 1034 rec = &xe->xdf1.recs[xch->i1]; 1047 1035 for (i = 0; i < xch->chg1 && ignore; i++) 1048 - ignore = record_matches_regex(rec[i], xpp); 1036 + ignore = record_matches_regex(&rec[i], xpp); 1049 1037 1050 1038 rec = &xe->xdf2.recs[xch->i2]; 1051 1039 for (i = 0; i < xch->chg2 && ignore; i++) 1052 - ignore = record_matches_regex(rec[i], xpp); 1040 + ignore = record_matches_regex(&rec[i], xpp); 1053 1041 1054 1042 xch->ignore = ignore; 1055 1043 }
+2 -9
xdiff/xdiffi.h
··· 24 24 #define XDIFFI_H 25 25 26 26 27 - typedef struct s_diffdata { 28 - long nrec; 29 - unsigned long const *ha; 30 - long *rindex; 31 - char *rchg; 32 - } diffdata_t; 33 - 34 27 typedef struct s_xdalgoenv { 35 28 long mxcost; 36 29 long snake_cnt; ··· 46 39 47 40 48 41 49 - int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1, 50 - diffdata_t *dd2, long off2, long lim2, 42 + int xdl_recs_cmp(xdfile_t *xdf1, long off1, long lim1, 43 + xdfile_t *xdf2, long off2, long lim2, 51 44 long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv); 52 45 int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, 53 46 xdfenv_t *xe);
+13 -25
xdiff/xemit.c
··· 22 22 23 23 #include "xinclude.h" 24 24 25 - static long xdl_get_rec(xdfile_t *xdf, long ri, char const **rec) { 26 25 27 - *rec = xdf->recs[ri]->ptr; 28 - 29 - return xdf->recs[ri]->size; 30 - } 31 - 32 - 33 - static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *ecb) { 34 - long size, psize = strlen(pre); 35 - char const *rec; 26 + static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *ecb) 27 + { 28 + xrecord_t *rec = &xdf->recs[ri]; 36 29 37 - size = xdl_get_rec(xdf, ri, &rec); 38 - if (xdl_emit_diffrec(rec, size, pre, psize, ecb) < 0) { 39 - 30 + if (xdl_emit_diffrec(rec->ptr, rec->size, pre, strlen(pre), ecb) < 0) 40 31 return -1; 41 - } 42 32 43 33 return 0; 44 34 } ··· 120 110 static long match_func_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri, 121 111 char *buf, long sz) 122 112 { 123 - const char *rec; 124 - long len = xdl_get_rec(xdf, ri, &rec); 113 + xrecord_t *rec = &xdf->recs[ri]; 114 + 125 115 if (!xecfg->find_func) 126 - return def_ff(rec, len, buf, sz); 127 - return xecfg->find_func(rec, len, buf, sz, xecfg->find_func_priv); 116 + return def_ff(rec->ptr, rec->size, buf, sz); 117 + return xecfg->find_func(rec->ptr, rec->size, buf, sz, xecfg->find_func_priv); 128 118 } 129 119 130 120 static int is_func_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri) ··· 160 150 161 151 static int is_empty_rec(xdfile_t *xdf, long ri) 162 152 { 163 - const char *rec; 164 - long len = xdl_get_rec(xdf, ri, &rec); 153 + xrecord_t *rec = &xdf->recs[ri]; 154 + long i = 0; 165 155 166 - while (len > 0 && XDL_ISSPACE(*rec)) { 167 - rec++; 168 - len--; 169 - } 170 - return !len; 156 + for (; i < rec->size && XDL_ISSPACE(rec->ptr[i]); i++); 157 + 158 + return i == rec->size; 171 159 } 172 160 173 161 int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
+5 -5
xdiff/xhistogram.c
··· 86 86 ((LINE_MAP(index, ptr))->cnt) 87 87 88 88 #define REC(env, s, l) \ 89 - (env->xdf##s.recs[l - 1]) 89 + (&env->xdf##s.recs[l - 1]) 90 90 91 91 static int cmp_recs(xrecord_t *r1, xrecord_t *r2) 92 92 { ··· 318 318 319 319 if (!count1) { 320 320 while(count2--) 321 - env->xdf2.rchg[line2++ - 1] = 1; 321 + env->xdf2.changed[line2++ - 1] = true; 322 322 return 0; 323 323 } else if (!count2) { 324 324 while(count1--) 325 - env->xdf1.rchg[line1++ - 1] = 1; 325 + env->xdf1.changed[line1++ - 1] = true; 326 326 return 0; 327 327 } 328 328 ··· 335 335 else { 336 336 if (lcs.begin1 == 0 && lcs.begin2 == 0) { 337 337 while (count1--) 338 - env->xdf1.rchg[line1++ - 1] = 1; 338 + env->xdf1.changed[line1++ - 1] = true; 339 339 while (count2--) 340 - env->xdf2.rchg[line2++ - 1] = 1; 340 + env->xdf2.changed[line2++ - 1] = true; 341 341 result = 0; 342 342 } else { 343 343 result = histogram_diff(xpp, env,
+28 -28
xdiff/xmerge.c
··· 97 97 int line_count, long flags) 98 98 { 99 99 int i; 100 - xrecord_t **rec1 = xe1->xdf2.recs + i1; 101 - xrecord_t **rec2 = xe2->xdf2.recs + i2; 100 + xrecord_t *rec1 = xe1->xdf2.recs + i1; 101 + xrecord_t *rec2 = xe2->xdf2.recs + i2; 102 102 103 103 for (i = 0; i < line_count; i++) { 104 - int result = xdl_recmatch(rec1[i]->ptr, rec1[i]->size, 105 - rec2[i]->ptr, rec2[i]->size, flags); 104 + int result = xdl_recmatch(rec1[i].ptr, rec1[i].size, 105 + rec2[i].ptr, rec2[i].size, flags); 106 106 if (!result) 107 107 return -1; 108 108 } ··· 111 111 112 112 static int xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest) 113 113 { 114 - xrecord_t **recs; 114 + xrecord_t *recs; 115 115 int size = 0; 116 116 117 117 recs = (use_orig ? xe->xdf1.recs : xe->xdf2.recs) + i; ··· 119 119 if (count < 1) 120 120 return 0; 121 121 122 - for (i = 0; i < count; size += recs[i++]->size) 122 + for (i = 0; i < count; size += recs[i++].size) 123 123 if (dest) 124 - memcpy(dest + size, recs[i]->ptr, recs[i]->size); 124 + memcpy(dest + size, recs[i].ptr, recs[i].size); 125 125 if (add_nl) { 126 - i = recs[count - 1]->size; 127 - if (i == 0 || recs[count - 1]->ptr[i - 1] != '\n') { 126 + i = recs[count - 1].size; 127 + if (i == 0 || recs[count - 1].ptr[i - 1] != '\n') { 128 128 if (needs_cr) { 129 129 if (dest) 130 130 dest[size] = '\r'; ··· 160 160 161 161 if (i < file->nrec - 1) 162 162 /* All lines before the last *must* end in LF */ 163 - return (size = file->recs[i]->size) > 1 && 164 - file->recs[i]->ptr[size - 2] == '\r'; 163 + return (size = file->recs[i].size) > 1 && 164 + file->recs[i].ptr[size - 2] == '\r'; 165 165 if (!file->nrec) 166 166 /* Cannot determine eol style from empty file */ 167 167 return -1; 168 - if ((size = file->recs[i]->size) && 169 - file->recs[i]->ptr[size - 1] == '\n') 168 + if ((size = file->recs[i].size) && 169 + file->recs[i].ptr[size - 1] == '\n') 170 170 /* Last line; ends in LF; Is it CR/LF? */ 171 171 return size > 1 && 172 - file->recs[i]->ptr[size - 2] == '\r'; 172 + file->recs[i].ptr[size - 2] == '\r'; 173 173 if (!i) 174 174 /* The only line has no eol */ 175 175 return -1; 176 176 /* Determine eol from second-to-last line */ 177 - return (size = file->recs[i - 1]->size) > 1 && 178 - file->recs[i - 1]->ptr[size - 2] == '\r'; 177 + return (size = file->recs[i - 1].size) > 1 && 178 + file->recs[i - 1].ptr[size - 2] == '\r'; 179 179 } 180 180 181 181 static int is_cr_needed(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m) ··· 334 334 static void xdl_refine_zdiff3_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m, 335 335 xpparam_t const *xpp) 336 336 { 337 - xrecord_t **rec1 = xe1->xdf2.recs, **rec2 = xe2->xdf2.recs; 337 + xrecord_t *rec1 = xe1->xdf2.recs, *rec2 = xe2->xdf2.recs; 338 338 for (; m; m = m->next) { 339 339 /* let's handle just the conflicts */ 340 340 if (m->mode) 341 341 continue; 342 342 343 343 while(m->chg1 && m->chg2 && 344 - recmatch(rec1[m->i1], rec2[m->i2], xpp->flags)) { 344 + recmatch(&rec1[m->i1], &rec2[m->i2], xpp->flags)) { 345 345 m->chg1--; 346 346 m->chg2--; 347 347 m->i1++; 348 348 m->i2++; 349 349 } 350 350 while (m->chg1 && m->chg2 && 351 - recmatch(rec1[m->i1 + m->chg1 - 1], 352 - rec2[m->i2 + m->chg2 - 1], xpp->flags)) { 351 + recmatch(&rec1[m->i1 + m->chg1 - 1], 352 + &rec2[m->i2 + m->chg2 - 1], xpp->flags)) { 353 353 m->chg1--; 354 354 m->chg2--; 355 355 } ··· 381 381 * This probably does not work outside git, since 382 382 * we have a very simple mmfile structure. 383 383 */ 384 - t1.ptr = (char *)xe1->xdf2.recs[m->i1]->ptr; 385 - t1.size = xe1->xdf2.recs[m->i1 + m->chg1 - 1]->ptr 386 - + xe1->xdf2.recs[m->i1 + m->chg1 - 1]->size - t1.ptr; 387 - t2.ptr = (char *)xe2->xdf2.recs[m->i2]->ptr; 388 - t2.size = xe2->xdf2.recs[m->i2 + m->chg2 - 1]->ptr 389 - + xe2->xdf2.recs[m->i2 + m->chg2 - 1]->size - t2.ptr; 384 + t1.ptr = (char *)xe1->xdf2.recs[m->i1].ptr; 385 + t1.size = xe1->xdf2.recs[m->i1 + m->chg1 - 1].ptr 386 + + xe1->xdf2.recs[m->i1 + m->chg1 - 1].size - t1.ptr; 387 + t2.ptr = (char *)xe2->xdf2.recs[m->i2].ptr; 388 + t2.size = xe2->xdf2.recs[m->i2 + m->chg2 - 1].ptr 389 + + xe2->xdf2.recs[m->i2 + m->chg2 - 1].size - t2.ptr; 390 390 if (xdl_do_diff(&t1, &t2, xpp, &xe) < 0) 391 391 return -1; 392 392 if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 || ··· 440 440 static int lines_contain_alnum(xdfenv_t *xe, int i, int chg) 441 441 { 442 442 for (; chg; chg--, i++) 443 - if (line_contains_alnum(xe->xdf2.recs[i]->ptr, 444 - xe->xdf2.recs[i]->size)) 443 + if (line_contains_alnum(xe->xdf2.recs[i].ptr, 444 + xe->xdf2.recs[i].size)) 445 445 return 1; 446 446 return 0; 447 447 }
+9 -9
xdiff/xpatience.c
··· 88 88 static void insert_record(xpparam_t const *xpp, int line, struct hashmap *map, 89 89 int pass) 90 90 { 91 - xrecord_t **records = pass == 1 ? 91 + xrecord_t *records = pass == 1 ? 92 92 map->env->xdf1.recs : map->env->xdf2.recs; 93 - xrecord_t *record = records[line - 1]; 93 + xrecord_t *record = &records[line - 1]; 94 94 /* 95 95 * After xdl_prepare_env() (or more precisely, due to 96 96 * xdl_classify_record()), the "ha" member of the records (AKA lines) ··· 121 121 return; 122 122 map->entries[index].line1 = line; 123 123 map->entries[index].hash = record->ha; 124 - map->entries[index].anchor = is_anchor(xpp, map->env->xdf1.recs[line - 1]->ptr); 124 + map->entries[index].anchor = is_anchor(xpp, map->env->xdf1.recs[line - 1].ptr); 125 125 if (!map->first) 126 126 map->first = map->entries + index; 127 127 if (map->last) { ··· 246 246 247 247 static int match(struct hashmap *map, int line1, int line2) 248 248 { 249 - xrecord_t *record1 = map->env->xdf1.recs[line1 - 1]; 250 - xrecord_t *record2 = map->env->xdf2.recs[line2 - 1]; 249 + xrecord_t *record1 = &map->env->xdf1.recs[line1 - 1]; 250 + xrecord_t *record2 = &map->env->xdf2.recs[line2 - 1]; 251 251 return record1->ha == record2->ha; 252 252 } 253 253 ··· 331 331 /* trivial case: one side is empty */ 332 332 if (!count1) { 333 333 while(count2--) 334 - env->xdf2.rchg[line2++ - 1] = 1; 334 + env->xdf2.changed[line2++ - 1] = true; 335 335 return 0; 336 336 } else if (!count2) { 337 337 while(count1--) 338 - env->xdf1.rchg[line1++ - 1] = 1; 338 + env->xdf1.changed[line1++ - 1] = true; 339 339 return 0; 340 340 } 341 341 ··· 347 347 /* are there any matching lines at all? */ 348 348 if (!map.has_matches) { 349 349 while(count1--) 350 - env->xdf1.rchg[line1++ - 1] = 1; 350 + env->xdf1.changed[line1++ - 1] = true; 351 351 while(count2--) 352 - env->xdf2.rchg[line2++ - 1] = 1; 352 + env->xdf2.changed[line2++ - 1] = true; 353 353 xdl_free(map.entries); 354 354 return 0; 355 355 }
+156 -188
xdiff/xprepare.c
··· 29 29 #define XDL_GUESS_NLINES1 256 30 30 #define XDL_GUESS_NLINES2 20 31 31 32 + #define DISCARD 0 33 + #define KEEP 1 34 + #define INVESTIGATE 2 32 35 33 36 typedef struct s_xdlclass { 34 37 struct s_xdlclass *next; 35 - unsigned long ha; 36 - char const *line; 37 - long size; 38 + xrecord_t rec; 38 39 long idx; 39 40 long len1, len2; 40 41 } xdlclass_t; ··· 49 50 long count; 50 51 long flags; 51 52 } xdlclassifier_t; 52 - 53 - 54 - 55 - 56 - static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags); 57 - static void xdl_free_classifier(xdlclassifier_t *cf); 58 - static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash, 59 - unsigned int hbits, xrecord_t *rec); 60 - static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp, 61 - xdlclassifier_t *cf, xdfile_t *xdf); 62 - static void xdl_free_ctx(xdfile_t *xdf); 63 - static int xdl_clean_mmatch(char const *dis, long i, long s, long e); 64 - static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2); 65 - static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2); 66 - static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2); 67 53 68 54 69 55 ··· 106 92 } 107 93 108 94 109 - static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash, 110 - unsigned int hbits, xrecord_t *rec) { 95 + static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t *rec) { 111 96 long hi; 112 - char const *line; 113 97 xdlclass_t *rcrec; 114 98 115 - line = rec->ptr; 116 99 hi = (long) XDL_HASHLONG(rec->ha, cf->hbits); 117 100 for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next) 118 - if (rcrec->ha == rec->ha && 119 - xdl_recmatch(rcrec->line, rcrec->size, 101 + if (rcrec->rec.ha == rec->ha && 102 + xdl_recmatch(rcrec->rec.ptr, rcrec->rec.size, 120 103 rec->ptr, rec->size, cf->flags)) 121 104 break; 122 105 ··· 129 112 if (XDL_ALLOC_GROW(cf->rcrecs, cf->count, cf->alloc)) 130 113 return -1; 131 114 cf->rcrecs[rcrec->idx] = rcrec; 132 - rcrec->line = line; 133 - rcrec->size = rec->size; 134 - rcrec->ha = rec->ha; 115 + rcrec->rec = *rec; 135 116 rcrec->len1 = rcrec->len2 = 0; 136 117 rcrec->next = cf->rchash[hi]; 137 118 cf->rchash[hi] = rcrec; ··· 141 122 142 123 rec->ha = (unsigned long) rcrec->idx; 143 124 144 - hi = (long) XDL_HASHLONG(rec->ha, hbits); 145 - rec->next = rhash[hi]; 146 - rhash[hi] = rec; 147 - 148 125 return 0; 149 126 } 150 127 151 128 129 + static void xdl_free_ctx(xdfile_t *xdf) 130 + { 131 + xdl_free(xdf->rindex); 132 + xdl_free(xdf->changed - 1); 133 + xdl_free(xdf->recs); 134 + } 135 + 136 + 152 137 static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp, 153 138 xdlclassifier_t *cf, xdfile_t *xdf) { 154 - unsigned int hbits; 155 - long nrec, hsize, bsize; 139 + long bsize; 156 140 unsigned long hav; 157 141 char const *blk, *cur, *top, *prev; 158 142 xrecord_t *crec; 159 - xrecord_t **recs; 160 - xrecord_t **rhash; 161 - unsigned long *ha; 162 - char *rchg; 163 - long *rindex; 164 143 165 - ha = NULL; 166 - rindex = NULL; 167 - rchg = NULL; 168 - rhash = NULL; 169 - recs = NULL; 144 + xdf->rindex = NULL; 145 + xdf->changed = NULL; 146 + xdf->recs = NULL; 170 147 171 - if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) 172 - goto abort; 173 - if (!XDL_ALLOC_ARRAY(recs, narec)) 148 + if (!XDL_ALLOC_ARRAY(xdf->recs, narec)) 174 149 goto abort; 175 150 176 - hbits = xdl_hashbits((unsigned int) narec); 177 - hsize = 1 << hbits; 178 - if (!XDL_CALLOC_ARRAY(rhash, hsize)) 179 - goto abort; 180 - 181 - nrec = 0; 151 + xdf->nrec = 0; 182 152 if ((cur = blk = xdl_mmfile_first(mf, &bsize))) { 183 153 for (top = blk + bsize; cur < top; ) { 184 154 prev = cur; 185 155 hav = xdl_hash_record(&cur, top, xpp->flags); 186 - if (XDL_ALLOC_GROW(recs, nrec + 1, narec)) 156 + if (XDL_ALLOC_GROW(xdf->recs, xdf->nrec + 1, narec)) 187 157 goto abort; 188 - if (!(crec = xdl_cha_alloc(&xdf->rcha))) 189 - goto abort; 158 + crec = &xdf->recs[xdf->nrec++]; 190 159 crec->ptr = prev; 191 160 crec->size = (long) (cur - prev); 192 161 crec->ha = hav; 193 - recs[nrec++] = crec; 194 - if (xdl_classify_record(pass, cf, rhash, hbits, crec) < 0) 162 + if (xdl_classify_record(pass, cf, crec) < 0) 195 163 goto abort; 196 164 } 197 165 } 198 166 199 - if (!XDL_CALLOC_ARRAY(rchg, nrec + 2)) 167 + if (!XDL_CALLOC_ARRAY(xdf->changed, xdf->nrec + 2)) 200 168 goto abort; 201 169 202 170 if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) && 203 171 (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) { 204 - if (!XDL_ALLOC_ARRAY(rindex, nrec + 1)) 205 - goto abort; 206 - if (!XDL_ALLOC_ARRAY(ha, nrec + 1)) 172 + if (!XDL_ALLOC_ARRAY(xdf->rindex, xdf->nrec + 1)) 207 173 goto abort; 208 174 } 209 175 210 - xdf->nrec = nrec; 211 - xdf->recs = recs; 212 - xdf->hbits = hbits; 213 - xdf->rhash = rhash; 214 - xdf->rchg = rchg + 1; 215 - xdf->rindex = rindex; 176 + xdf->changed += 1; 216 177 xdf->nreff = 0; 217 - xdf->ha = ha; 218 178 xdf->dstart = 0; 219 - xdf->dend = nrec - 1; 179 + xdf->dend = xdf->nrec - 1; 220 180 221 181 return 0; 222 182 223 183 abort: 224 - xdl_free(ha); 225 - xdl_free(rindex); 226 - xdl_free(rchg); 227 - xdl_free(rhash); 228 - xdl_free(recs); 229 - xdl_cha_free(&xdf->rcha); 184 + xdl_free_ctx(xdf); 230 185 return -1; 231 186 } 232 187 233 188 234 - static void xdl_free_ctx(xdfile_t *xdf) { 235 - 236 - xdl_free(xdf->rhash); 237 - xdl_free(xdf->rindex); 238 - xdl_free(xdf->rchg - 1); 239 - xdl_free(xdf->ha); 240 - xdl_free(xdf->recs); 241 - xdl_cha_free(&xdf->rcha); 242 - } 243 - 244 - 245 - int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, 246 - xdfenv_t *xe) { 247 - long enl1, enl2, sample; 248 - xdlclassifier_t cf; 249 - 250 - memset(&cf, 0, sizeof(cf)); 251 - 252 - /* 253 - * For histogram diff, we can afford a smaller sample size and 254 - * thus a poorer estimate of the number of lines, as the hash 255 - * table (rhash) won't be filled up/grown. The number of lines 256 - * (nrecs) will be updated correctly anyway by 257 - * xdl_prepare_ctx(). 258 - */ 259 - sample = (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF 260 - ? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1); 261 - 262 - enl1 = xdl_guess_lines(mf1, sample) + 1; 263 - enl2 = xdl_guess_lines(mf2, sample) + 1; 264 - 265 - if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) 266 - return -1; 267 - 268 - if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) { 269 - 270 - xdl_free_classifier(&cf); 271 - return -1; 272 - } 273 - if (xdl_prepare_ctx(2, mf2, enl2, xpp, &cf, &xe->xdf2) < 0) { 274 - 275 - xdl_free_ctx(&xe->xdf1); 276 - xdl_free_classifier(&cf); 277 - return -1; 278 - } 279 - 280 - if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) && 281 - (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) && 282 - xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) { 283 - 284 - xdl_free_ctx(&xe->xdf2); 285 - xdl_free_ctx(&xe->xdf1); 286 - xdl_free_classifier(&cf); 287 - return -1; 288 - } 289 - 290 - xdl_free_classifier(&cf); 291 - 292 - return 0; 293 - } 294 - 295 - 296 189 void xdl_free_env(xdfenv_t *xe) { 297 190 298 191 xdl_free_ctx(&xe->xdf2); ··· 300 193 } 301 194 302 195 303 - static int xdl_clean_mmatch(char const *dis, long i, long s, long e) { 196 + static bool xdl_clean_mmatch(uint8_t const *action, long i, long s, long e) { 304 197 long r, rdis0, rpdis0, rdis1, rpdis1; 305 198 306 199 /* 307 - * Limits the window the is examined during the similar-lines 308 - * scan. The loops below stops when dis[i - r] == 1 (line that 309 - * has no match), but there are corner cases where the loop 310 - * proceed all the way to the extremities by causing huge 311 - * performance penalties in case of big files. 200 + * Limits the window that is examined during the similar-lines 201 + * scan. The loops below stops when action[i - r] == KEEP 202 + * (line that has no match), but there are corner cases where 203 + * the loop proceed all the way to the extremities by causing 204 + * huge performance penalties in case of big files. 312 205 */ 313 206 if (i - s > XDL_SIMSCAN_WINDOW) 314 207 s = i - XDL_SIMSCAN_WINDOW; ··· 317 210 318 211 /* 319 212 * Scans the lines before 'i' to find a run of lines that either 320 - * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1). 321 - * Note that we always call this function with dis[i] > 1, so the 322 - * current line (i) is already a multimatch line. 213 + * have no match (action[j] == DISCARD) or have multiple matches 214 + * (action[j] == INVESTIGATE). Note that we always call this 215 + * function with action[i] == INVESTIGATE, so the current line 216 + * (i) is already a multimatch line. 323 217 */ 324 218 for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) { 325 - if (!dis[i - r]) 219 + if (action[i - r] == DISCARD) 326 220 rdis0++; 327 - else if (dis[i - r] == 2) 221 + else if (action[i - r] == INVESTIGATE) 328 222 rpdis0++; 223 + else if (action[i - r] == KEEP) 224 + break; 329 225 else 330 - break; 226 + BUG("Illegal value for action[i - r]"); 331 227 } 332 228 /* 333 - * If the run before the line 'i' found only multimatch lines, we 334 - * return 0 and hence we don't make the current line (i) discarded. 335 - * We want to discard multimatch lines only when they appear in the 336 - * middle of runs with nomatch lines (dis[j] == 0). 229 + * If the run before the line 'i' found only multimatch lines, 230 + * we return false and hence we don't make the current line (i) 231 + * discarded. We want to discard multimatch lines only when 232 + * they appear in the middle of runs with nomatch lines 233 + * (action[j] == DISCARD). 337 234 */ 338 235 if (rdis0 == 0) 339 236 return 0; 340 237 for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) { 341 - if (!dis[i + r]) 238 + if (action[i + r] == DISCARD) 342 239 rdis1++; 343 - else if (dis[i + r] == 2) 240 + else if (action[i + r] == INVESTIGATE) 344 241 rpdis1++; 345 - else 242 + else if (action[i + r] == KEEP) 346 243 break; 244 + else 245 + BUG("Illegal value for action[i + r]"); 347 246 } 348 247 /* 349 - * If the run after the line 'i' found only multimatch lines, we 350 - * return 0 and hence we don't make the current line (i) discarded. 248 + * If the run after the line 'i' found only multimatch lines, 249 + * we return false and hence we don't make the current line (i) 250 + * discarded. 351 251 */ 352 252 if (rdis1 == 0) 353 - return 0; 253 + return false; 354 254 rdis1 += rdis0; 355 255 rpdis1 += rpdis0; 356 256 ··· 361 261 /* 362 262 * Try to reduce the problem complexity, discard records that have no 363 263 * matches on the other file. Also, lines that have multiple matches 364 - * might be potentially discarded if they happear in a run of discardable. 264 + * might be potentially discarded if they appear in a run of discardable. 365 265 */ 366 266 static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) { 367 267 long i, nm, nreff, mlim; 368 - xrecord_t **recs; 268 + xrecord_t *recs; 369 269 xdlclass_t *rcrec; 370 - char *dis, *dis1, *dis2; 371 - int need_min = !!(cf->flags & XDF_NEED_MINIMAL); 270 + uint8_t *action1 = NULL, *action2 = NULL; 271 + bool need_min = !!(cf->flags & XDF_NEED_MINIMAL); 272 + int ret = 0; 372 273 373 - if (!XDL_CALLOC_ARRAY(dis, xdf1->nrec + xdf2->nrec + 2)) 374 - return -1; 375 - dis1 = dis; 376 - dis2 = dis1 + xdf1->nrec + 1; 274 + /* 275 + * Create temporary arrays that will help us decide if 276 + * changed[i] should remain false, or become true. 277 + */ 278 + if (!XDL_CALLOC_ARRAY(action1, xdf1->nrec + 1)) { 279 + ret = -1; 280 + goto cleanup; 281 + } 282 + if (!XDL_CALLOC_ARRAY(action2, xdf2->nrec + 1)) { 283 + ret = -1; 284 + goto cleanup; 285 + } 377 286 287 + /* 288 + * Initialize temporary arrays with DISCARD, KEEP, or INVESTIGATE. 289 + */ 378 290 if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT) 379 291 mlim = XDL_MAX_EQLIMIT; 380 292 for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) { 381 - rcrec = cf->rcrecs[(*recs)->ha]; 293 + rcrec = cf->rcrecs[recs->ha]; 382 294 nm = rcrec ? rcrec->len2 : 0; 383 - dis1[i] = (nm == 0) ? 0: (nm >= mlim && !need_min) ? 2: 1; 295 + action1[i] = (nm == 0) ? DISCARD: (nm >= mlim && !need_min) ? INVESTIGATE: KEEP; 384 296 } 385 297 386 298 if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT) 387 299 mlim = XDL_MAX_EQLIMIT; 388 300 for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) { 389 - rcrec = cf->rcrecs[(*recs)->ha]; 301 + rcrec = cf->rcrecs[recs->ha]; 390 302 nm = rcrec ? rcrec->len1 : 0; 391 - dis2[i] = (nm == 0) ? 0: (nm >= mlim && !need_min) ? 2: 1; 303 + action2[i] = (nm == 0) ? DISCARD: (nm >= mlim && !need_min) ? INVESTIGATE: KEEP; 392 304 } 393 305 306 + /* 307 + * Use temporary arrays to decide if changed[i] should remain 308 + * false, or become true. 309 + */ 394 310 for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; 395 311 i <= xdf1->dend; i++, recs++) { 396 - if (dis1[i] == 1 || 397 - (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) { 398 - xdf1->rindex[nreff] = i; 399 - xdf1->ha[nreff] = (*recs)->ha; 400 - nreff++; 312 + if (action1[i] == KEEP || 313 + (action1[i] == INVESTIGATE && !xdl_clean_mmatch(action1, i, xdf1->dstart, xdf1->dend))) { 314 + xdf1->rindex[nreff++] = i; 315 + /* changed[i] remains false, i.e. keep */ 401 316 } else 402 - xdf1->rchg[i] = 1; 317 + xdf1->changed[i] = true; 318 + /* i.e. discard */ 403 319 } 404 320 xdf1->nreff = nreff; 405 321 406 322 for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; 407 323 i <= xdf2->dend; i++, recs++) { 408 - if (dis2[i] == 1 || 409 - (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) { 410 - xdf2->rindex[nreff] = i; 411 - xdf2->ha[nreff] = (*recs)->ha; 412 - nreff++; 324 + if (action2[i] == KEEP || 325 + (action2[i] == INVESTIGATE && !xdl_clean_mmatch(action2, i, xdf2->dstart, xdf2->dend))) { 326 + xdf2->rindex[nreff++] = i; 327 + /* changed[i] remains false, i.e. keep */ 413 328 } else 414 - xdf2->rchg[i] = 1; 329 + xdf2->changed[i] = true; 330 + /* i.e. discard */ 415 331 } 416 332 xdf2->nreff = nreff; 417 333 418 - xdl_free(dis); 334 + cleanup: 335 + xdl_free(action1); 336 + xdl_free(action2); 419 337 420 - return 0; 338 + return ret; 421 339 } 422 340 423 341 ··· 426 344 */ 427 345 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) { 428 346 long i, lim; 429 - xrecord_t **recs1, **recs2; 347 + xrecord_t *recs1, *recs2; 430 348 431 349 recs1 = xdf1->recs; 432 350 recs2 = xdf2->recs; 433 351 for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim; 434 352 i++, recs1++, recs2++) 435 - if ((*recs1)->ha != (*recs2)->ha) 353 + if (recs1->ha != recs2->ha) 436 354 break; 437 355 438 356 xdf1->dstart = xdf2->dstart = i; ··· 440 358 recs1 = xdf1->recs + xdf1->nrec - 1; 441 359 recs2 = xdf2->recs + xdf2->nrec - 1; 442 360 for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--) 443 - if ((*recs1)->ha != (*recs2)->ha) 361 + if (recs1->ha != recs2->ha) 444 362 break; 445 363 446 364 xdf1->dend = xdf1->nrec - i - 1; ··· 460 378 461 379 return 0; 462 380 } 381 + 382 + int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, 383 + xdfenv_t *xe) { 384 + long enl1, enl2, sample; 385 + xdlclassifier_t cf; 386 + 387 + memset(&cf, 0, sizeof(cf)); 388 + 389 + /* 390 + * For histogram diff, we can afford a smaller sample size and 391 + * thus a poorer estimate of the number of lines, as the hash 392 + * table (rhash) won't be filled up/grown. The number of lines 393 + * (nrecs) will be updated correctly anyway by 394 + * xdl_prepare_ctx(). 395 + */ 396 + sample = (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF 397 + ? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1); 398 + 399 + enl1 = xdl_guess_lines(mf1, sample) + 1; 400 + enl2 = xdl_guess_lines(mf2, sample) + 1; 401 + 402 + if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) 403 + return -1; 404 + 405 + if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) { 406 + 407 + xdl_free_classifier(&cf); 408 + return -1; 409 + } 410 + if (xdl_prepare_ctx(2, mf2, enl2, xpp, &cf, &xe->xdf2) < 0) { 411 + 412 + xdl_free_ctx(&xe->xdf1); 413 + xdl_free_classifier(&cf); 414 + return -1; 415 + } 416 + 417 + if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) && 418 + (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) && 419 + xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) { 420 + 421 + xdl_free_ctx(&xe->xdf2); 422 + xdl_free_ctx(&xe->xdf1); 423 + xdl_free_classifier(&cf); 424 + return -1; 425 + } 426 + 427 + xdl_free_classifier(&cf); 428 + 429 + return 0; 430 + }
+2 -7
xdiff/xtypes.h
··· 39 39 } chastore_t; 40 40 41 41 typedef struct s_xrecord { 42 - struct s_xrecord *next; 43 42 char const *ptr; 44 43 long size; 45 44 unsigned long ha; 46 45 } xrecord_t; 47 46 48 47 typedef struct s_xdfile { 49 - chastore_t rcha; 48 + xrecord_t *recs; 50 49 long nrec; 51 - unsigned int hbits; 52 - xrecord_t **rhash; 53 50 long dstart, dend; 54 - xrecord_t **recs; 55 - char *rchg; 51 + bool *changed; 56 52 long *rindex; 57 53 long nreff; 58 - unsigned long *ha; 59 54 } xdfile_t; 60 55 61 56 typedef struct s_xdfenv {
+8 -8
xdiff/xutils.c
··· 464 464 mmfile_t subfile1, subfile2; 465 465 xdfenv_t env; 466 466 467 - subfile1.ptr = (char *)diff_env->xdf1.recs[line1 - 1]->ptr; 468 - subfile1.size = diff_env->xdf1.recs[line1 + count1 - 2]->ptr + 469 - diff_env->xdf1.recs[line1 + count1 - 2]->size - subfile1.ptr; 470 - subfile2.ptr = (char *)diff_env->xdf2.recs[line2 - 1]->ptr; 471 - subfile2.size = diff_env->xdf2.recs[line2 + count2 - 2]->ptr + 472 - diff_env->xdf2.recs[line2 + count2 - 2]->size - subfile2.ptr; 467 + subfile1.ptr = (char *)diff_env->xdf1.recs[line1 - 1].ptr; 468 + subfile1.size = diff_env->xdf1.recs[line1 + count1 - 2].ptr + 469 + diff_env->xdf1.recs[line1 + count1 - 2].size - subfile1.ptr; 470 + subfile2.ptr = (char *)diff_env->xdf2.recs[line2 - 1].ptr; 471 + subfile2.size = diff_env->xdf2.recs[line2 + count2 - 2].ptr + 472 + diff_env->xdf2.recs[line2 + count2 - 2].size - subfile2.ptr; 473 473 if (xdl_do_diff(&subfile1, &subfile2, xpp, &env) < 0) 474 474 return -1; 475 475 476 - memcpy(diff_env->xdf1.rchg + line1 - 1, env.xdf1.rchg, count1); 477 - memcpy(diff_env->xdf2.rchg + line2 - 1, env.xdf2.rchg, count2); 476 + memcpy(diff_env->xdf1.changed + line1 - 1, env.xdf1.changed, count1); 477 + memcpy(diff_env->xdf2.changed + line2 - 1, env.xdf2.changed, count2); 478 478 479 479 xdl_free_env(&env); 480 480