bdiff.c
322 lines
| 6.8 KiB
| text/x-c
|
CLexer
/ mercurial / bdiff.c
|
r400 | /* | ||
bdiff.c - efficient binary diff extension for Mercurial | ||||
|
r2859 | Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | ||
|
r400 | |||
This software may be used and distributed according to the terms of | ||||
the GNU General Public License, incorporated herein by reference. | ||||
Based roughly on Python difflib | ||||
*/ | ||||
|
r34627 | #include <limits.h> | ||
|
r400 | #include <stdlib.h> | ||
#include <string.h> | ||||
|
r867 | |||
|
r34627 | #include "bdiff.h" | ||
#include "bitmanipulation.h" | ||||
|
r29539 | #include "compat.h" | ||
|
r400 | |||
|
r30318 | /* Hash implementation from diffutils */ | ||
#define ROL(v, n) ((v) << (n) | (v) >> (sizeof(v) * CHAR_BIT - (n))) | ||||
|
r34629 | #define HASH(h, c) ((c) + ROL(h, 7)) | ||
|
r30318 | |||
|
r474 | struct pos { | ||
int pos, len; | ||||
}; | ||||
|
r29541 | int bdiff_splitlines(const char *a, ssize_t len, struct bdiff_line **lr) | ||
|
r400 | { | ||
|
r13732 | unsigned hash; | ||
|
r13731 | int i; | ||
|
r400 | const char *p, *b = a; | ||
|
r34630 | const char *const plast = a + len - 1; | ||
|
r29540 | struct bdiff_line *l; | ||
|
r400 | |||
/* count the lines */ | ||||
i = 1; /* extra line for sentinel */ | ||||
|
r30308 | for (p = a; p < plast; p++) | ||
if (*p == '\n') | ||||
|
r400 | i++; | ||
|
r30308 | if (p == plast) | ||
i++; | ||||
|
r400 | |||
|
r29540 | *lr = l = (struct bdiff_line *)malloc(sizeof(struct bdiff_line) * i); | ||
|
r400 | if (!l) | ||
return -1; | ||||
/* build the line array and calculate hashes */ | ||||
|
r13732 | hash = 0; | ||
|
r30461 | for (p = a; p < plast; p++) { | ||
|
r30318 | hash = HASH(hash, *p); | ||
|
r5342 | |||
|
r30461 | if (*p == '\n') { | ||
|
r13732 | l->hash = hash; | ||
hash = 0; | ||||
|
r400 | l->len = p - b + 1; | ||
l->l = b; | ||||
|
r5341 | l->n = INT_MAX; | ||
|
r400 | l++; | ||
b = p + 1; | ||||
} | ||||
} | ||||
|
r30461 | if (p == plast) { | ||
hash = HASH(hash, *p); | ||||
l->hash = hash; | ||||
l->len = p - b + 1; | ||||
l->l = b; | ||||
l->n = INT_MAX; | ||||
l++; | ||||
} | ||||
|
r400 | /* set up a sentinel */ | ||
|
r13732 | l->hash = 0; | ||
|
r13731 | l->len = 0; | ||
|
r400 | l->l = a + len; | ||
return i - 1; | ||||
} | ||||
|
r29540 | static inline int cmp(struct bdiff_line *a, struct bdiff_line *b) | ||
|
r400 | { | ||
|
r34631 | return a->hash != b->hash || a->len != b->len || | ||
memcmp(a->l, b->l, a->len); | ||||
|
r400 | } | ||
|
r29540 | static int equatelines(struct bdiff_line *a, int an, struct bdiff_line *b, | ||
int bn) | ||||
|
r400 | { | ||
|
r5452 | int i, j, buckets = 1, t, scale; | ||
struct pos *h = NULL; | ||||
|
r400 | |||
/* build a hash table of the next highest power of 2 */ | ||||
while (buckets < bn + 1) | ||||
buckets *= 2; | ||||
|
r5339 | /* try to allocate a large hash table to avoid collisions */ | ||
|
r5452 | for (scale = 4; scale; scale /= 2) { | ||
|
r5339 | h = (struct pos *)malloc(scale * buckets * sizeof(struct pos)); | ||
|
r5452 | if (h) | ||
break; | ||||
} | ||||
|
r5339 | |||
|
r474 | if (!h) | ||
|
r400 | return 0; | ||
|
r5339 | buckets = buckets * scale - 1; | ||
|
r400 | /* clear the hash table */ | ||
|
r474 | for (i = 0; i <= buckets; i++) { | ||
|
r29013 | h[i].pos = -1; | ||
|
r474 | h[i].len = 0; | ||
} | ||||
|
r400 | |||
/* add lines to the hash table chains */ | ||||
|
r29013 | for (i = 0; i < bn; i++) { | ||
|
r400 | /* find the equivalence class */ | ||
|
r29013 | for (j = b[i].hash & buckets; h[j].pos != -1; | ||
|
r474 | j = (j + 1) & buckets) | ||
if (!cmp(b + i, b + h[j].pos)) | ||||
|
r400 | break; | ||
/* add to the head of the equivalence class */ | ||||
|
r474 | b[i].n = h[j].pos; | ||
|
r433 | b[i].e = j; | ||
|
r474 | h[j].pos = i; | ||
h[j].len++; /* keep track of popularity */ | ||||
|
r400 | } | ||
/* compute popularity threshold */ | ||||
|
r9534 | t = (bn >= 31000) ? bn / 1000 : 1000000 / (bn + 1); | ||
|
r400 | |||
/* match items in a to their equivalence class in b */ | ||||
for (i = 0; i < an; i++) { | ||||
/* find the equivalence class */ | ||||
|
r29013 | for (j = a[i].hash & buckets; h[j].pos != -1; | ||
|
r474 | j = (j + 1) & buckets) | ||
if (!cmp(a + i, b + h[j].pos)) | ||||
|
r400 | break; | ||
|
r433 | a[i].e = j; /* use equivalence class for quick compare */ | ||
|
r1542 | if (h[j].len <= t) | ||
|
r474 | a[i].n = h[j].pos; /* point to head of match list */ | ||
|
r400 | else | ||
|
r29013 | a[i].n = -1; /* too popular */ | ||
|
r400 | } | ||
/* discard hash tables */ | ||||
free(h); | ||||
return 1; | ||||
} | ||||
|
r29540 | static int longest_match(struct bdiff_line *a, struct bdiff_line *b, | ||
struct pos *pos, | ||||
|
r400 | int a1, int a2, int b1, int b2, int *omi, int *omj) | ||
{ | ||||
|
r30431 | int mi = a1, mj = b1, mk = 0, i, j, k, half, bhalf; | ||
|
r29015 | |||
/* window our search on large regions to better bound | ||||
worst-case performance. by choosing a window at the end, we | ||||
reduce skipping overhead on the b chains. */ | ||||
if (a2 - a1 > 30000) | ||||
a1 = a2 - 30000; | ||||
|
r30429 | half = (a1 + a2 - 1) / 2; | ||
|
r30431 | bhalf = (b1 + b2 - 1) / 2; | ||
|
r400 | |||
for (i = a1; i < a2; i++) { | ||||
|
r29013 | /* skip all lines in b after the current block */ | ||
for (j = a[i].n; j >= b2; j = b[j].n) | ||||
|
r400 | ; | ||
/* loop through all lines match a[i] in b */ | ||||
|
r29013 | for (; j >= b1; j = b[j].n) { | ||
|
r400 | /* does this extend an earlier match? */ | ||
|
r29322 | for (k = 1; j - k >= b1 && i - k >= a1; k++) { | ||
/* reached an earlier match? */ | ||||
if (pos[j - k].pos == i - k) { | ||||
k += pos[j - k].len; | ||||
break; | ||||
} | ||||
/* previous line mismatch? */ | ||||
if (a[i - k].e != b[j - k].e) | ||||
break; | ||||
} | ||||
|
r474 | pos[j].pos = i; | ||
pos[j].len = k; | ||||
|
r400 | |||
|
r29014 | /* best match so far? we prefer matches closer | ||
to the middle to balance recursion */ | ||||
|
r30430 | if (k > mk) { | ||
/* a longer match */ | ||||
|
r400 | mi = i; | ||
mj = j; | ||||
mk = k; | ||||
|
r30430 | } else if (k == mk) { | ||
|
r30433 | if (i > mi && i <= half && j > b1) { | ||
|
r30430 | /* same match but closer to half */ | ||
mi = i; | ||||
mj = j; | ||||
|
r30432 | } else if (i == mi && (mj > bhalf || i == a1)) { | ||
|
r30431 | /* same i but best earlier j */ | ||
|
r30430 | mj = j; | ||
} | ||||
|
r400 | } | ||
} | ||||
} | ||||
if (mk) { | ||||
mi = mi - mk + 1; | ||||
mj = mj - mk + 1; | ||||
} | ||||
|
r29323 | /* expand match to include subsequent popular lines */ | ||
|
r34631 | while (mi + mk < a2 && mj + mk < b2 && a[mi + mk].e == b[mj + mk].e) | ||
|
r400 | mk++; | ||
|
r29323 | *omi = mi; | ||
*omj = mj; | ||||
|
r5341 | |||
|
r29323 | return mk; | ||
|
r400 | } | ||
|
r29540 | static struct bdiff_hunk *recurse(struct bdiff_line *a, struct bdiff_line *b, | ||
struct pos *pos, | ||||
int a1, int a2, int b1, int b2, struct bdiff_hunk *l) | ||||
|
r400 | { | ||
int i, j, k; | ||||
|
r10500 | while (1) { | ||
/* find the longest match in this chunk */ | ||||
k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j); | ||||
if (!k) | ||||
|
r13089 | return l; | ||
|
r400 | |||
|
r10500 | /* and recurse on the remaining chunks on either side */ | ||
|
r13089 | l = recurse(a, b, pos, a1, i, b1, j, l); | ||
if (!l) | ||||
return NULL; | ||||
|
r34631 | l->next = | ||
(struct bdiff_hunk *)malloc(sizeof(struct bdiff_hunk)); | ||||
|
r13089 | if (!l->next) | ||
return NULL; | ||||
l = l->next; | ||||
l->a1 = i; | ||||
l->a2 = i + k; | ||||
l->b1 = j; | ||||
l->b2 = j + k; | ||||
l->next = NULL; | ||||
/* tail-recursion didn't happen, so do equivalent iteration */ | ||||
|
r10500 | a1 = i + k; | ||
b1 = j + k; | ||||
} | ||||
|
r400 | } | ||
|
r29541 | int bdiff_diff(struct bdiff_line *a, int an, struct bdiff_line *b, | ||
|
r29540 | int bn, struct bdiff_hunk *base) | ||
|
r400 | { | ||
|
r29540 | struct bdiff_hunk *curr; | ||
|
r474 | struct pos *pos; | ||
|
r13089 | int t, count = 0; | ||
|
r433 | |||
/* allocate and fill arrays */ | ||||
t = equatelines(a, an, b, bn); | ||||
|
r5571 | pos = (struct pos *)calloc(bn ? bn : 1, sizeof(struct pos)); | ||
|
r13089 | |||
if (pos && t) { | ||||
/* generate the matching block list */ | ||||
curr = recurse(a, b, pos, 0, an, 0, bn, base); | ||||
if (!curr) | ||||
return -1; | ||||
|
r433 | |||
|
r13089 | /* sentinel end hunk */ | ||
|
r34631 | curr->next = | ||
(struct bdiff_hunk *)malloc(sizeof(struct bdiff_hunk)); | ||||
|
r13089 | if (!curr->next) | ||
|
r13090 | return -1; | ||
|
r13089 | curr = curr->next; | ||
curr->a1 = curr->a2 = an; | ||||
curr->b1 = curr->b2 = bn; | ||||
curr->next = NULL; | ||||
|
r433 | } | ||
|
r474 | free(pos); | ||
|
r7104 | |||
|
r7625 | /* normalize the hunk list, try to push each hunk towards the end */ | ||
|
r13089 | for (curr = base->next; curr; curr = curr->next) { | ||
|
r29540 | struct bdiff_hunk *next = curr->next; | ||
|
r7104 | |||
|
r13089 | if (!next) | ||
|
r7104 | break; | ||
|
r29010 | if (curr->a2 == next->a1 || curr->b2 == next->b1) | ||
|
r34631 | while (curr->a2 < an && curr->b2 < bn && | ||
next->a1 < next->a2 && next->b1 < next->b2 && | ||||
!cmp(a + curr->a2, b + curr->b2)) { | ||||
|
r29011 | curr->a2++; | ||
next->a1++; | ||||
curr->b2++; | ||||
next->b1++; | ||||
} | ||||
|
r7104 | } | ||
|
r13089 | for (curr = base->next; curr; curr = curr->next) | ||
count++; | ||||
return count; | ||||
} | ||||
|
r29541 | void bdiff_freehunks(struct bdiff_hunk *l) | ||
|
r13089 | { | ||
|
r29540 | struct bdiff_hunk *n; | ||
|
r13089 | for (; l; l = n) { | ||
n = l->next; | ||||
free(l); | ||||
} | ||||
|
r433 | } | ||
|
r400 | |||