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