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