public
Created

[gsoc-diff] xdl_trim_head()

  • Download Gist
gistfile1.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
diff --git a/xdiff/xprepare.c b/xdiff/xprepare.c
index 82bff88..061ace9 100644
--- a/xdiff/xprepare.c
+++ b/xdiff/xprepare.c
@@ -55,7 +55,7 @@ static void xdl_free_classifier(xdlclassifier_t *cf);
static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned int hbits,
xrecord_t *rec);
static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
- xdlclassifier_t *cf, xdfile_t *xdf);
+ xdlclassifier_t *cf, xdfile_t *xdf, xrecord_t **arec);
static void xdl_free_ctx(xdfile_t *xdf);
static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
static int xdl_cleanup_records(xdfile_t *xdf1, xdfile_t *xdf2);
@@ -104,9 +104,10 @@ static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned
line = rec->ptr;
hi = (long) XDL_HASHLONG(rec->ha, cf->hbits);
for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next)
- if (rcrec->ha == rec->ha &&
+ if (rec->ha == 0 ||
+ (rcrec->ha == rec->ha &&
xdl_recmatch(rcrec->line, rcrec->size,
- rec->ptr, rec->size, cf->flags))
+ rec->ptr, rec->size, cf->flags)))
break;
if (!rcrec) {
@@ -132,8 +133,55 @@ static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned
}
+static int xdl_trim_head(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
+ xdfile_t *xdf1, xdfile_t *xdf2) {
+
+ const int blk = 128;
+ long trimmed = 0, recovered = 0;
+ long smaller = XDL_MIN(mf1->size, mf2->size);
+ char const *p1, *p2;
+ char const *cur, *top;
+ char const *prev1, *prev2;
+
+ /* part 1: blocks */
+ p1 = mf1->ptr;
+ p2 = mf2->ptr;
+
+ top = p1 + smaller;
+
+ while (blk + trimmed <= smaller && !memcmp(p1, p2, blk)) {
+ trimmed += blk;
+ p1 += blk;
+ p2 += blk;
+ }
+
+ while (recovered < trimmed) {
+ if (*(p1 - recovered++) == '\n')
+ break;
+ }
+ p1 -= recovered;
+ p2 -= recovered;
+
+ prev1 = xdf1->rstart = cur = p1;
+ prev2 = xdf2->rstart = p2;
+
+ /* part 2: line based */
+ while (cur < top
+ && (cur = memchr(cur, '\n', top - cur))
+ && !memcmp(prev1, prev2, ++cur - prev1)) {
+ prev2 += cur - prev1;
+ prev1 = cur;
+ }
+
+ xdf1->rstart = prev1;
+ xdf2->rstart = prev2;
+
+ return 0;
+}
+
+
static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
- xdlclassifier_t *cf, xdfile_t *xdf) {
+ xdlclassifier_t *cf, xdfile_t *xdf, xrecord_t **arec) {
unsigned int hbits;
long nrec, hsize, bsize;
unsigned long hav;
@@ -167,10 +215,19 @@ static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
}
nrec = 0;
+ xdf->dstart = 0;
if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) {
for (top = blk + bsize; cur < top; ) {
prev = cur;
- hav = xdl_hash_record(&cur, top, xpp->flags);
+ if (cur < xdf->rstart) {
+ if (arec)
+ cur += (arec++)[0]->size;
+ else
+ cur = memchr(cur, '\n', top - cur) + 1;
+ hav = 0;
+ xdf->dstart++;
+ } else
+ hav = xdl_hash_record(&cur, top, xpp->flags);
if (nrec >= narec) {
narec *= 2;
if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *))))
@@ -207,7 +264,6 @@ static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
xdf->rindex = rindex;
xdf->nreff = 0;
xdf->ha = ha;
- xdf->dstart = 0;
xdf->dend = nrec - 1;
return 0;
@@ -257,12 +313,18 @@ int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
return -1;
}
- if (xdl_prepare_ctx(mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
+ if (xdl_trim_head(mf1, mf2, xpp, &xe->xdf1, &xe->xdf2) < 0) {
+
+ xdl_free_classifier(&cf);
+ return -1;
+ }
+
+ if (xdl_prepare_ctx(mf1, enl1, xpp, &cf, &xe->xdf1, NULL) < 0) {
xdl_free_classifier(&cf);
return -1;
}
- if (xdl_prepare_ctx(mf2, enl2, xpp, &cf, &xe->xdf2) < 0) {
+ if (xdl_prepare_ctx(mf2, enl2, xpp, &cf, &xe->xdf2, xe->xdf1.recs) < 0) {
xdl_free_ctx(&xe->xdf1);
xdl_free_classifier(&cf);
@@ -428,15 +490,19 @@ static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
long i, lim;
xrecord_t **recs1, **recs2;
- recs1 = xdf1->recs;
- recs2 = xdf2->recs;
- for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
- i++, recs1++, recs2++)
+ lim = XDL_MIN(xdf1->nrec, xdf2->nrec);
+ i = XDL_MIN(lim, xdf1->dstart);
+ /*
+ i = xdf1->dstart;
+ recs1 = xdf1->recs + i;
+ recs2 = xdf2->recs + i;
+ for (lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim;
+ i++, recs1++, recs2++)
if ((*recs1)->ha != (*recs2)->ha)
break;
xdf1->dstart = xdf2->dstart = i;
-
+ */
recs1 = xdf1->recs + xdf1->nrec - 1;
recs2 = xdf2->recs + xdf2->nrec - 1;
for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.