##// END OF EJS Templates
bdiff: re-wrap lines per clang-format...
Augie Fackler -
r34631:38463bd9 default
parent child Browse files
Show More
@@ -1,321 +1,322 b''
1 /*
1 /*
2 bdiff.c - efficient binary diff extension for Mercurial
2 bdiff.c - efficient binary diff extension for Mercurial
3
3
4 Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
4 Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
5
5
6 This software may be used and distributed according to the terms of
6 This software may be used and distributed according to the terms of
7 the GNU General Public License, incorporated herein by reference.
7 the GNU General Public License, incorporated herein by reference.
8
8
9 Based roughly on Python difflib
9 Based roughly on Python difflib
10 */
10 */
11
11
12 #include <limits.h>
12 #include <limits.h>
13 #include <stdlib.h>
13 #include <stdlib.h>
14 #include <string.h>
14 #include <string.h>
15
15
16 #include "bdiff.h"
16 #include "bdiff.h"
17 #include "bitmanipulation.h"
17 #include "bitmanipulation.h"
18 #include "compat.h"
18 #include "compat.h"
19
19
20 /* Hash implementation from diffutils */
20 /* Hash implementation from diffutils */
21 #define ROL(v, n) ((v) << (n) | (v) >> (sizeof(v) * CHAR_BIT - (n)))
21 #define ROL(v, n) ((v) << (n) | (v) >> (sizeof(v) * CHAR_BIT - (n)))
22 #define HASH(h, c) ((c) + ROL(h, 7))
22 #define HASH(h, c) ((c) + ROL(h, 7))
23
23
24 struct pos {
24 struct pos {
25 int pos, len;
25 int pos, len;
26 };
26 };
27
27
28 int bdiff_splitlines(const char *a, ssize_t len, struct bdiff_line **lr)
28 int bdiff_splitlines(const char *a, ssize_t len, struct bdiff_line **lr)
29 {
29 {
30 unsigned hash;
30 unsigned hash;
31 int i;
31 int i;
32 const char *p, *b = a;
32 const char *p, *b = a;
33 const char *const plast = a + len - 1;
33 const char *const plast = a + len - 1;
34 struct bdiff_line *l;
34 struct bdiff_line *l;
35
35
36 /* count the lines */
36 /* count the lines */
37 i = 1; /* extra line for sentinel */
37 i = 1; /* extra line for sentinel */
38 for (p = a; p < plast; p++)
38 for (p = a; p < plast; p++)
39 if (*p == '\n')
39 if (*p == '\n')
40 i++;
40 i++;
41 if (p == plast)
41 if (p == plast)
42 i++;
42 i++;
43
43
44 *lr = l = (struct bdiff_line *)malloc(sizeof(struct bdiff_line) * i);
44 *lr = l = (struct bdiff_line *)malloc(sizeof(struct bdiff_line) * i);
45 if (!l)
45 if (!l)
46 return -1;
46 return -1;
47
47
48 /* build the line array and calculate hashes */
48 /* build the line array and calculate hashes */
49 hash = 0;
49 hash = 0;
50 for (p = a; p < plast; p++) {
50 for (p = a; p < plast; p++) {
51 hash = HASH(hash, *p);
51 hash = HASH(hash, *p);
52
52
53 if (*p == '\n') {
53 if (*p == '\n') {
54 l->hash = hash;
54 l->hash = hash;
55 hash = 0;
55 hash = 0;
56 l->len = p - b + 1;
56 l->len = p - b + 1;
57 l->l = b;
57 l->l = b;
58 l->n = INT_MAX;
58 l->n = INT_MAX;
59 l++;
59 l++;
60 b = p + 1;
60 b = p + 1;
61 }
61 }
62 }
62 }
63
63
64 if (p == plast) {
64 if (p == plast) {
65 hash = HASH(hash, *p);
65 hash = HASH(hash, *p);
66 l->hash = hash;
66 l->hash = hash;
67 l->len = p - b + 1;
67 l->len = p - b + 1;
68 l->l = b;
68 l->l = b;
69 l->n = INT_MAX;
69 l->n = INT_MAX;
70 l++;
70 l++;
71 }
71 }
72
72
73 /* set up a sentinel */
73 /* set up a sentinel */
74 l->hash = 0;
74 l->hash = 0;
75 l->len = 0;
75 l->len = 0;
76 l->l = a + len;
76 l->l = a + len;
77 return i - 1;
77 return i - 1;
78 }
78 }
79
79
80 static inline int cmp(struct bdiff_line *a, struct bdiff_line *b)
80 static inline int cmp(struct bdiff_line *a, struct bdiff_line *b)
81 {
81 {
82 return a->hash != b->hash || a->len != b->len || memcmp(a->l, b->l, a->len);
82 return a->hash != b->hash || a->len != b->len ||
83 memcmp(a->l, b->l, a->len);
83 }
84 }
84
85
85 static int equatelines(struct bdiff_line *a, int an, struct bdiff_line *b,
86 static int equatelines(struct bdiff_line *a, int an, struct bdiff_line *b,
86 int bn)
87 int bn)
87 {
88 {
88 int i, j, buckets = 1, t, scale;
89 int i, j, buckets = 1, t, scale;
89 struct pos *h = NULL;
90 struct pos *h = NULL;
90
91
91 /* build a hash table of the next highest power of 2 */
92 /* build a hash table of the next highest power of 2 */
92 while (buckets < bn + 1)
93 while (buckets < bn + 1)
93 buckets *= 2;
94 buckets *= 2;
94
95
95 /* try to allocate a large hash table to avoid collisions */
96 /* try to allocate a large hash table to avoid collisions */
96 for (scale = 4; scale; scale /= 2) {
97 for (scale = 4; scale; scale /= 2) {
97 h = (struct pos *)malloc(scale * buckets * sizeof(struct pos));
98 h = (struct pos *)malloc(scale * buckets * sizeof(struct pos));
98 if (h)
99 if (h)
99 break;
100 break;
100 }
101 }
101
102
102 if (!h)
103 if (!h)
103 return 0;
104 return 0;
104
105
105 buckets = buckets * scale - 1;
106 buckets = buckets * scale - 1;
106
107
107 /* clear the hash table */
108 /* clear the hash table */
108 for (i = 0; i <= buckets; i++) {
109 for (i = 0; i <= buckets; i++) {
109 h[i].pos = -1;
110 h[i].pos = -1;
110 h[i].len = 0;
111 h[i].len = 0;
111 }
112 }
112
113
113 /* add lines to the hash table chains */
114 /* add lines to the hash table chains */
114 for (i = 0; i < bn; i++) {
115 for (i = 0; i < bn; i++) {
115 /* find the equivalence class */
116 /* find the equivalence class */
116 for (j = b[i].hash & buckets; h[j].pos != -1;
117 for (j = b[i].hash & buckets; h[j].pos != -1;
117 j = (j + 1) & buckets)
118 j = (j + 1) & buckets)
118 if (!cmp(b + i, b + h[j].pos))
119 if (!cmp(b + i, b + h[j].pos))
119 break;
120 break;
120
121
121 /* add to the head of the equivalence class */
122 /* add to the head of the equivalence class */
122 b[i].n = h[j].pos;
123 b[i].n = h[j].pos;
123 b[i].e = j;
124 b[i].e = j;
124 h[j].pos = i;
125 h[j].pos = i;
125 h[j].len++; /* keep track of popularity */
126 h[j].len++; /* keep track of popularity */
126 }
127 }
127
128
128 /* compute popularity threshold */
129 /* compute popularity threshold */
129 t = (bn >= 31000) ? bn / 1000 : 1000000 / (bn + 1);
130 t = (bn >= 31000) ? bn / 1000 : 1000000 / (bn + 1);
130
131
131 /* match items in a to their equivalence class in b */
132 /* match items in a to their equivalence class in b */
132 for (i = 0; i < an; i++) {
133 for (i = 0; i < an; i++) {
133 /* find the equivalence class */
134 /* find the equivalence class */
134 for (j = a[i].hash & buckets; h[j].pos != -1;
135 for (j = a[i].hash & buckets; h[j].pos != -1;
135 j = (j + 1) & buckets)
136 j = (j + 1) & buckets)
136 if (!cmp(a + i, b + h[j].pos))
137 if (!cmp(a + i, b + h[j].pos))
137 break;
138 break;
138
139
139 a[i].e = j; /* use equivalence class for quick compare */
140 a[i].e = j; /* use equivalence class for quick compare */
140 if (h[j].len <= t)
141 if (h[j].len <= t)
141 a[i].n = h[j].pos; /* point to head of match list */
142 a[i].n = h[j].pos; /* point to head of match list */
142 else
143 else
143 a[i].n = -1; /* too popular */
144 a[i].n = -1; /* too popular */
144 }
145 }
145
146
146 /* discard hash tables */
147 /* discard hash tables */
147 free(h);
148 free(h);
148 return 1;
149 return 1;
149 }
150 }
150
151
151 static int longest_match(struct bdiff_line *a, struct bdiff_line *b,
152 static int longest_match(struct bdiff_line *a, struct bdiff_line *b,
152 struct pos *pos,
153 struct pos *pos,
153 int a1, int a2, int b1, int b2, int *omi, int *omj)
154 int a1, int a2, int b1, int b2, int *omi, int *omj)
154 {
155 {
155 int mi = a1, mj = b1, mk = 0, i, j, k, half, bhalf;
156 int mi = a1, mj = b1, mk = 0, i, j, k, half, bhalf;
156
157
157 /* window our search on large regions to better bound
158 /* window our search on large regions to better bound
158 worst-case performance. by choosing a window at the end, we
159 worst-case performance. by choosing a window at the end, we
159 reduce skipping overhead on the b chains. */
160 reduce skipping overhead on the b chains. */
160 if (a2 - a1 > 30000)
161 if (a2 - a1 > 30000)
161 a1 = a2 - 30000;
162 a1 = a2 - 30000;
162
163
163 half = (a1 + a2 - 1) / 2;
164 half = (a1 + a2 - 1) / 2;
164 bhalf = (b1 + b2 - 1) / 2;
165 bhalf = (b1 + b2 - 1) / 2;
165
166
166 for (i = a1; i < a2; i++) {
167 for (i = a1; i < a2; i++) {
167 /* skip all lines in b after the current block */
168 /* skip all lines in b after the current block */
168 for (j = a[i].n; j >= b2; j = b[j].n)
169 for (j = a[i].n; j >= b2; j = b[j].n)
169 ;
170 ;
170
171
171 /* loop through all lines match a[i] in b */
172 /* loop through all lines match a[i] in b */
172 for (; j >= b1; j = b[j].n) {
173 for (; j >= b1; j = b[j].n) {
173 /* does this extend an earlier match? */
174 /* does this extend an earlier match? */
174 for (k = 1; j - k >= b1 && i - k >= a1; k++) {
175 for (k = 1; j - k >= b1 && i - k >= a1; k++) {
175 /* reached an earlier match? */
176 /* reached an earlier match? */
176 if (pos[j - k].pos == i - k) {
177 if (pos[j - k].pos == i - k) {
177 k += pos[j - k].len;
178 k += pos[j - k].len;
178 break;
179 break;
179 }
180 }
180 /* previous line mismatch? */
181 /* previous line mismatch? */
181 if (a[i - k].e != b[j - k].e)
182 if (a[i - k].e != b[j - k].e)
182 break;
183 break;
183 }
184 }
184
185
185 pos[j].pos = i;
186 pos[j].pos = i;
186 pos[j].len = k;
187 pos[j].len = k;
187
188
188 /* best match so far? we prefer matches closer
189 /* best match so far? we prefer matches closer
189 to the middle to balance recursion */
190 to the middle to balance recursion */
190 if (k > mk) {
191 if (k > mk) {
191 /* a longer match */
192 /* a longer match */
192 mi = i;
193 mi = i;
193 mj = j;
194 mj = j;
194 mk = k;
195 mk = k;
195 } else if (k == mk) {
196 } else if (k == mk) {
196 if (i > mi && i <= half && j > b1) {
197 if (i > mi && i <= half && j > b1) {
197 /* same match but closer to half */
198 /* same match but closer to half */
198 mi = i;
199 mi = i;
199 mj = j;
200 mj = j;
200 } else if (i == mi && (mj > bhalf || i == a1)) {
201 } else if (i == mi && (mj > bhalf || i == a1)) {
201 /* same i but best earlier j */
202 /* same i but best earlier j */
202 mj = j;
203 mj = j;
203 }
204 }
204 }
205 }
205 }
206 }
206 }
207 }
207
208
208 if (mk) {
209 if (mk) {
209 mi = mi - mk + 1;
210 mi = mi - mk + 1;
210 mj = mj - mk + 1;
211 mj = mj - mk + 1;
211 }
212 }
212
213
213 /* expand match to include subsequent popular lines */
214 /* expand match to include subsequent popular lines */
214 while (mi + mk < a2 && mj + mk < b2 &&
215 while (mi + mk < a2 && mj + mk < b2 && a[mi + mk].e == b[mj + mk].e)
215 a[mi + mk].e == b[mj + mk].e)
216 mk++;
216 mk++;
217
217
218 *omi = mi;
218 *omi = mi;
219 *omj = mj;
219 *omj = mj;
220
220
221 return mk;
221 return mk;
222 }
222 }
223
223
224 static struct bdiff_hunk *recurse(struct bdiff_line *a, struct bdiff_line *b,
224 static struct bdiff_hunk *recurse(struct bdiff_line *a, struct bdiff_line *b,
225 struct pos *pos,
225 struct pos *pos,
226 int a1, int a2, int b1, int b2, struct bdiff_hunk *l)
226 int a1, int a2, int b1, int b2, struct bdiff_hunk *l)
227 {
227 {
228 int i, j, k;
228 int i, j, k;
229
229
230 while (1) {
230 while (1) {
231 /* find the longest match in this chunk */
231 /* find the longest match in this chunk */
232 k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j);
232 k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j);
233 if (!k)
233 if (!k)
234 return l;
234 return l;
235
235
236 /* and recurse on the remaining chunks on either side */
236 /* and recurse on the remaining chunks on either side */
237 l = recurse(a, b, pos, a1, i, b1, j, l);
237 l = recurse(a, b, pos, a1, i, b1, j, l);
238 if (!l)
238 if (!l)
239 return NULL;
239 return NULL;
240
240
241 l->next = (struct bdiff_hunk *)malloc(sizeof(struct bdiff_hunk));
241 l->next =
242 (struct bdiff_hunk *)malloc(sizeof(struct bdiff_hunk));
242 if (!l->next)
243 if (!l->next)
243 return NULL;
244 return NULL;
244
245
245 l = l->next;
246 l = l->next;
246 l->a1 = i;
247 l->a1 = i;
247 l->a2 = i + k;
248 l->a2 = i + k;
248 l->b1 = j;
249 l->b1 = j;
249 l->b2 = j + k;
250 l->b2 = j + k;
250 l->next = NULL;
251 l->next = NULL;
251
252
252 /* tail-recursion didn't happen, so do equivalent iteration */
253 /* tail-recursion didn't happen, so do equivalent iteration */
253 a1 = i + k;
254 a1 = i + k;
254 b1 = j + k;
255 b1 = j + k;
255 }
256 }
256 }
257 }
257
258
258 int bdiff_diff(struct bdiff_line *a, int an, struct bdiff_line *b,
259 int bdiff_diff(struct bdiff_line *a, int an, struct bdiff_line *b,
259 int bn, struct bdiff_hunk *base)
260 int bn, struct bdiff_hunk *base)
260 {
261 {
261 struct bdiff_hunk *curr;
262 struct bdiff_hunk *curr;
262 struct pos *pos;
263 struct pos *pos;
263 int t, count = 0;
264 int t, count = 0;
264
265
265 /* allocate and fill arrays */
266 /* allocate and fill arrays */
266 t = equatelines(a, an, b, bn);
267 t = equatelines(a, an, b, bn);
267 pos = (struct pos *)calloc(bn ? bn : 1, sizeof(struct pos));
268 pos = (struct pos *)calloc(bn ? bn : 1, sizeof(struct pos));
268
269
269 if (pos && t) {
270 if (pos && t) {
270 /* generate the matching block list */
271 /* generate the matching block list */
271
272
272 curr = recurse(a, b, pos, 0, an, 0, bn, base);
273 curr = recurse(a, b, pos, 0, an, 0, bn, base);
273 if (!curr)
274 if (!curr)
274 return -1;
275 return -1;
275
276
276 /* sentinel end hunk */
277 /* sentinel end hunk */
277 curr->next = (struct bdiff_hunk *)malloc(sizeof(struct bdiff_hunk));
278 curr->next =
279 (struct bdiff_hunk *)malloc(sizeof(struct bdiff_hunk));
278 if (!curr->next)
280 if (!curr->next)
279 return -1;
281 return -1;
280 curr = curr->next;
282 curr = curr->next;
281 curr->a1 = curr->a2 = an;
283 curr->a1 = curr->a2 = an;
282 curr->b1 = curr->b2 = bn;
284 curr->b1 = curr->b2 = bn;
283 curr->next = NULL;
285 curr->next = NULL;
284 }
286 }
285
287
286 free(pos);
288 free(pos);
287
289
288 /* normalize the hunk list, try to push each hunk towards the end */
290 /* normalize the hunk list, try to push each hunk towards the end */
289 for (curr = base->next; curr; curr = curr->next) {
291 for (curr = base->next; curr; curr = curr->next) {
290 struct bdiff_hunk *next = curr->next;
292 struct bdiff_hunk *next = curr->next;
291
293
292 if (!next)
294 if (!next)
293 break;
295 break;
294
296
295 if (curr->a2 == next->a1 || curr->b2 == next->b1)
297 if (curr->a2 == next->a1 || curr->b2 == next->b1)
296 while (curr->a2 < an && curr->b2 < bn
298 while (curr->a2 < an && curr->b2 < bn &&
297 && next->a1 < next->a2
299 next->a1 < next->a2 && next->b1 < next->b2 &&
298 && next->b1 < next->b2
300 !cmp(a + curr->a2, b + curr->b2)) {
299 && !cmp(a + curr->a2, b + curr->b2)) {
300 curr->a2++;
301 curr->a2++;
301 next->a1++;
302 next->a1++;
302 curr->b2++;
303 curr->b2++;
303 next->b1++;
304 next->b1++;
304 }
305 }
305 }
306 }
306
307
307 for (curr = base->next; curr; curr = curr->next)
308 for (curr = base->next; curr; curr = curr->next)
308 count++;
309 count++;
309 return count;
310 return count;
310 }
311 }
311
312
312 void bdiff_freehunks(struct bdiff_hunk *l)
313 void bdiff_freehunks(struct bdiff_hunk *l)
313 {
314 {
314 struct bdiff_hunk *n;
315 struct bdiff_hunk *n;
315 for (; l; l = n) {
316 for (; l; l = n) {
316 n = l->next;
317 n = l->next;
317 free(l);
318 free(l);
318 }
319 }
319 }
320 }
320
321
321
322
General Comments 0
You need to be logged in to leave comments. Login now