mba/diff.c

filter: Statementstmt 88.0% 132 / 150 Branchbran 80.1% 109 / 136 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.2% 241 / 286 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« XS.xs ↑ Summary blib/lib/LCS/XS.pm »
1 /* diff - compute a shortest edit script (SES) given two sequences
2 * Copyright (c) 2004 Michael B. Allen <mba2000 ioplex.com>
3 *
4 * The MIT License
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24  
25 /* This algorithm is basically Myers' solution to SES/LCS with
26 * the Hirschberg linear space refinement as described in the
27 * following publication:
28 *
29 * E. Myers, ``An O(ND) Difference Algorithm and Its Variations,''
30 * Algorithmica 1, 2 (1986), 251-266.
31 * http://www.cs.arizona.edu/people/gene/PAPERS/diff.ps
32 *
33 * This is the same algorithm used by GNU diff(1).
34 */
35  
36 #include <stdlib.h>
37 #include <limits.h>
38 #include <errno.h>
39  
40 #include "mba/msgno.h"
41 #include "mba/diff.h"
42  
43 #define FV(k) _v(ctx, (k), 0)
44 #define RV(k) _v(ctx, (k), 1)
45  
46 struct _ctx {
47 cmp_fn cmp;
48 void *context;
49 struct varray *buf;
50 struct varray *ses;
51 int si;
52 int dmax;
53 };
54  
55 struct middle_snake {
56 int x, y, u, v;
57 };
58  
59 static void
60 4226 _setv(struct _ctx *ctx, int k, int r, int val)
61 {
62 int j;
63 int *i;
64 /* Pack -N to N into 0 to N * 2
65 */
66 4226 ▶ j = k <= 0 ? -k * 4 + r : k * 4 + (r - 2);
Branch: 1 branch
BranchTrueFalseTotal
j = k <= 0 ? -k * 4 + r : k * 4 + (r - 2); 2141 2085 4226
67  
68 4226 i = (int *)varray_get(ctx->buf, j);
69 4226 *i = val;
70 4226 }
71 static int
72 10435 _v(struct _ctx *ctx, int k, int r)
73 {
74 int j;
75  
76 10435 ▶ j = k <= 0 ? -k * 4 + r : k * 4 + (r - 2);
Branch: 1 branch
BranchTrueFalseTotal
j = k <= 0 ? -k * 4 + r : k * 4 + (r - 2); 5260 5175 10435
77  
78 10435 return *((int *)varray_get(ctx->buf, j));
79 }
80  
81 static int
82 73 _find_middle_snake(const void *a, int aoff, int n,
83 const void *b, int boff, int m,
84 struct _ctx *ctx,
85 struct middle_snake *ms)
86 {
87 int delta, odd, mid, d;
88  
89 73 delta = n - m;
90 73 odd = delta & 1;
91 73 mid = (n + m) / 2;
92 73 mid += odd;
93  
94 73 _setv(ctx, 1, 0, 0);
95 73 _setv(ctx, delta - 1, 1, n);
96  
97 317 ▶ for (d = 0; d <= mid; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d <= mid; d++) { 317 0 317
98 int k, x, y;
99  
100 317 ▶ if ((2 * d - 1) >= ctx->dmax) {
Branch: 1 branch
BranchTrueFalseTotal
if ((2 * d - 1) >= ctx->dmax) { 0 317 317
101 0 return ctx->dmax;
102 }
103  
104 2403 ▶ for (k = d; k >= -d; k -= 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = d; k >= -d; k -= 2) { 2117 286 2403
105 2117 ▶ if (k == -d || (k != d && FV(k - 1) < FV(k + 1))) {
Branch: 3 branches
BranchTrueFalseTotal
if (k == -d || (k != d && FV(k - 1) < FV(k + 1))) { 1825 292 2117
if (k == -d || (k != d && FV(k - 1) < FV(k + 1))) { 1581 244 1825
if (k == -d || (k != d && FV(k - 1) < FV(k + 1))) { 1573 8 1581
106 1865 x = FV(k + 1);
107 } else {
108 252 x = FV(k - 1) + 1;
109 }
110 2117 y = x - k;
111  
112 2117 ms->x = x;
113 2117 ms->y = y;
114 2117 ▶ if (ctx->cmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cmp) { 2114 3 2117
115 3581 ▶ while (x < n && y < m
Branch: 1 branch
BranchTrueFalseTotal
while (x < n && y < m 625 842 1467
116 3003 ▶ && ctx->cmp(a, aoff + x, b, boff + y, ctx->context) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
&& ctx->cmp(a, aoff + x, b, boff + y, ctx->context) == 0) { 1467 911 2378
&& ctx->cmp(a, aoff + x, b, boff + y, ctx->context) == 0) { 264 361 625
117 264 x++; y++;
118 }
119 } else {
120 3 const unsigned char *a0 = (const unsigned char *)a + aoff;
121 3 const unsigned char *b0 = (const unsigned char *)b + boff;
122 4 ▶ while (x < n && y < m && a0[x] == b0[y]) {
Branch: 3 branches
BranchTrueFalseTotal
while (x < n && y < m && a0[x] == b0[y]) { 3 1 4
while (x < n && y < m && a0[x] == b0[y]) { 3 0 3
while (x < n && y < m && a0[x] == b0[y]) { 1 2 3
123 1 x++; y++;
124 }
125 }
126 2117 _setv(ctx, k, 0, x);
127  
128 2117 ▶ if (odd && k >= (delta - (d - 1)) && k <= (delta + (d - 1))) {
Branch: 3 branches
BranchTrueFalseTotal
if (odd && k >= (delta - (d - 1)) && k <= (delta + (d - 1))) { 1691 426 2117
if (odd && k >= (delta - (d - 1)) && k <= (delta + (d - 1))) { 973 718 1691
if (odd && k >= (delta - (d - 1)) && k <= (delta + (d - 1))) { 77 896 973
129 77 ▶ if (x >= RV(k)) {
Branch: 1 branch
BranchTrueFalseTotal
if (x >= RV(k)) { 31 46 77
130 31 ms->u = x;
131 31 ms->v = y;
132 31 return 2 * d - 1;
133 }
134 }
135 }
136 2207 ▶ for (k = d; k >= -d; k -= 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = d; k >= -d; k -= 2) { 1963 244 2207
137 1963 int kr = (n - m) + k;
138  
139 1963 ▶ if (k == d || (k != -d && RV(kr - 1) < RV(kr + 1))) {
Branch: 3 branches
BranchTrueFalseTotal
if (k == d || (k != -d && RV(kr - 1) < RV(kr + 1))) { 1677 286 1963
if (k == d || (k != -d && RV(kr - 1) < RV(kr + 1))) { 1498 179 1677
if (k == d || (k != -d && RV(kr - 1) < RV(kr + 1))) { 1493 5 1498
140 1779 x = RV(kr - 1);
141 } else {
142 184 x = RV(kr + 1) - 1;
143 }
144 1963 y = x - kr;
145  
146 1963 ms->u = x;
147 1963 ms->v = y;
148 1963 ▶ if (ctx->cmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cmp) { 1961 2 1963
149 3360 ▶ while (x > 0 && y > 0
Branch: 1 branch
BranchTrueFalseTotal
while (x > 0 && y > 0 557 842 1399
150 2765 ▶ && ctx->cmp(a, aoff + (x - 1), b, boff + (y - 1), ctx->context) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
&& ctx->cmp(a, aoff + (x - 1), b, boff + (y - 1), ctx->context) == 0) { 1399 809 2208
&& ctx->cmp(a, aoff + (x - 1), b, boff + (y - 1), ctx->context) == 0) { 247 310 557
151 247 x--; y--;
152 }
153 } else {
154 2 const unsigned char *a0 = (const unsigned char *)a + aoff;
155 2 const unsigned char *b0 = (const unsigned char *)b + boff;
156 3 ▶ while (x > 0 && y > 0 && a0[x - 1] == b0[y - 1]) {
Branch: 3 branches
BranchTrueFalseTotal
while (x > 0 && y > 0 && a0[x - 1] == b0[y - 1]) { 3 0 3
while (x > 0 && y > 0 && a0[x - 1] == b0[y - 1]) { 2 1 3
while (x > 0 && y > 0 && a0[x - 1] == b0[y - 1]) { 1 1 2
157 1 x--; y--;
158 }
159 }
160 1963 _setv(ctx, kr, 1, x);
161  
162 1963 ▶ if (!odd && kr >= -d && kr <= d) {
Branch: 3 branches
BranchTrueFalseTotal
if (!odd && kr >= -d && kr <= d) { 378 1585 1963
if (!odd && kr >= -d && kr <= d) { 330 48 378
if (!odd && kr >= -d && kr <= d) { 120 210 330
163 120 ▶ if (x <= FV(kr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (x <= FV(kr)) { 42 78 120
164 42 ms->x = x;
165 42 ms->y = y;
166 42 return 2 * d;
167 }
168 }
169 }
170 }
171  
172 0 errno = EFAULT;
173  
174 0 return -1;
175 }
176  
177 static void
178 198 _edit(struct _ctx *ctx, int op, int off, int len)
179 {
180 struct diff_edit *e;
181  
182 198 ▶ if (len == 0 || ctx->ses == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (len == 0 || ctx->ses == NULL) { 140 58 198
if (len == 0 || ctx->ses == NULL) { 0 140 140
183 58 return;
184 } /* Add an edit to the SES (or
185 * coalesce if the op is the same)
186 */
187 140 e = varray_get(ctx->ses, ctx->si);
188 140 ▶ if (e->op != op) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->op != op) { 120 20 140
189 120 ▶ if (e->op) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->op) { 90 30 120
190 90 ctx->si++;
191 90 e = varray_get(ctx->ses, ctx->si);
192 }
193 120 e->op = op;
194 120 e->off = off;
195 120 e->len = len;
196 } else {
197 20 e->len += len;
198 }
199 }
200  
201 static int
202 160 _ses(const void *a, int aoff, int n,
203 const void *b, int boff, int m,
204 struct _ctx *ctx)
205 {
206 struct middle_snake ms;
207 int d;
208  
209 160 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 37 123 160
210 37 _edit(ctx, DIFF_INSERT, boff, m);
211 37 d = m;
212 123 ▶ } else if (m == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (m == 0) { 50 73 123
213 50 _edit(ctx, DIFF_DELETE, aoff, n);
214 50 d = n;
215 } else {
216 /* Find the middle "snake" around which we
217 * recursively solve the sub-problems.
218 */
219 73 d = _find_middle_snake(a, aoff, n, b, boff, m, ctx, &ms);
220 73 ▶ if (d == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (d == -1) { 0 73 73
221 0 return -1;
222 73 ▶ } else if (d >= ctx->dmax) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (d >= ctx->dmax) { 0 73 73
223 0 return ctx->dmax;
224 73 ▶ } else if (ctx->ses == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ctx->ses == NULL) { 0 73 73
225 0 return d;
226 73 ▶ } else if (d > 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (d > 1) { 65 8 73
227 65 ▶ if (_ses(a, aoff, ms.x, b, boff, ms.y, ctx) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (_ses(a, aoff, ms.x, b, boff, ms.y, ctx) == -1) { 0 65 65
228 0 return -1;
229 }
230  
231 65 _edit(ctx, DIFF_MATCH, aoff + ms.x, ms.u - ms.x);
232  
233 65 aoff += ms.u;
234 65 boff += ms.v;
235 65 n -= ms.u;
236 65 m -= ms.v;
237 65 ▶ if (_ses(a, aoff, n, b, boff, m, ctx) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (_ses(a, aoff, n, b, boff, m, ctx) == -1) { 0 65 65
238 0 return -1;
239 }
240 } else {
241 8 int x = ms.x;
242 8 int u = ms.u;
243  
244 /* There are only 4 base cases when the
245 * edit distance is 1.
246 *
247 * n > m m > n
248 *
249 * - |
250 * \ \ x != u
251 * \ \
252 *
253 * \ \
254 * \ \ x == u
255 * - |
256 */
257  
258 8 ▶ if (m > n) {
Branch: 1 branch
BranchTrueFalseTotal
if (m > n) { 4 4 8
259 4 ▶ if (x == u) {
Branch: 1 branch
BranchTrueFalseTotal
if (x == u) { 1 3 4
260 1 _edit(ctx, DIFF_MATCH, aoff, n);
261 1 _edit(ctx, DIFF_INSERT, boff + (m - 1), 1);
262 } else {
263 3 _edit(ctx, DIFF_INSERT, boff, 1);
264 3 _edit(ctx, DIFF_MATCH, aoff, n);
265 }
266 } else {
267 4 ▶ if (x == u) {
Branch: 1 branch
BranchTrueFalseTotal
if (x == u) { 1 3 4
268 1 _edit(ctx, DIFF_MATCH, aoff, m);
269 1 _edit(ctx, DIFF_DELETE, aoff + (n - 1), 1);
270 } else {
271 3 _edit(ctx, DIFF_DELETE, aoff, 1);
272 3 _edit(ctx, DIFF_MATCH, aoff + 1, m);
273 }
274 }
275 }
276 }
277  
278 160 return d;
279 }
280 int
281 30 diff(const void *a, int aoff, int n,
282 const void *b, int boff, int m,
283 cmp_fn cmp, void *context, int dmax,
284 struct varray *ses, int *sn,
285 struct varray *buf)
286 {
287 struct _ctx ctx;
288 int d, x, y;
289 30 struct diff_edit *e = NULL;
290 struct varray tmp;
291  
292 30 ctx.cmp = cmp;
293 30 ctx.context = context;
294 30 ▶ if (buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf) { 0 30 30
295 0 ctx.buf = buf;
296 } else {
297 30 varray_init(&tmp, sizeof(int), NULL);
298 30 ctx.buf = &tmp;
299 }
300 30 ctx.ses = ses;
301 30 ctx.si = 0;
302 30 ▶ ctx.dmax = dmax ? dmax : INT_MAX;
Branch: 1 branch
BranchTrueFalseTotal
ctx.dmax = dmax ? dmax : INT_MAX; 0 30 30
303  
304 30 ▶ if (ses && sn) {
Branch: 2 branches
BranchTrueFalseTotal
if (ses && sn) { 30 0 30
if (ses && sn) { 30 0 30
305 30 ▶ if ((e = varray_get(ses, 0)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((e = varray_get(ses, 0)) == NULL) { 0 30 30
306 0 AMSG("");
307 0 ▶ if (buf == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf == NULL) { 0 0 0
308 0 varray_deinit(&tmp);
309 }
310 0 return -1;
311 }
312 30 e->op = 0;
313 }
314  
315 /* The _ses function assumes the SES will begin or end with a delete
316 * or insert. The following will insure this is true by eating any
317 * beginning matches. This is also a quick to process sequences
318 * that match entirely.
319 */
320 30 x = y = 0;
321 30 ▶ if (cmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (cmp) { 29 1 30
322 112 ▶ while (x < n && y < m && cmp(a, aoff + x, b, boff + y, context) == 0) {
Branch: 3 branches
BranchTrueFalseTotal
while (x < n && y < m && cmp(a, aoff + x, b, boff + y, context) == 0) { 111 1 112
while (x < n && y < m && cmp(a, aoff + x, b, boff + y, context) == 0) { 110 1 111
while (x < n && y < m && cmp(a, aoff + x, b, boff + y, context) == 0) { 83 27 110
323 83 x++; y++;
324 }
325 } else {
326 1 const unsigned char *a0 = (const unsigned char *)a + aoff;
327 1 const unsigned char *b0 = (const unsigned char *)b + boff;
328 1 ▶ while (x < n && y < m && a0[x] == b0[y]) {
Branch: 3 branches
BranchTrueFalseTotal
while (x < n && y < m && a0[x] == b0[y]) { 1 0 1
while (x < n && y < m && a0[x] == b0[y]) { 1 0 1
while (x < n && y < m && a0[x] == b0[y]) { 0 1 1
329 0 x++; y++;
330 }
331 }
332 30 _edit(&ctx, DIFF_MATCH, aoff, x);
333  
334 30 ▶ if ((d = _ses(a, aoff + x, n - x, b, boff + y, m - y, &ctx)) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((d = _ses(a, aoff + x, n - x, b, boff + y, m - y, &ctx)) == -1) { 0 30 30
335 0 AMSG("");
336 0 ▶ if (buf == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf == NULL) { 0 0 0
337 0 varray_deinit(&tmp);
338 }
339 0 return -1;
340 }
341 30 ▶ if (ses && sn) {
Branch: 2 branches
BranchTrueFalseTotal
if (ses && sn) { 30 0 30
if (ses && sn) { 30 0 30
342 30 ▶ *sn = e->op ? ctx.si + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
*sn = e->op ? ctx.si + 1 : 0; 30 0 30
343 }
344  
345 30 ▶ if (buf == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf == NULL) { 30 0 30
346 30 varray_deinit(&tmp);
347 }
348  
349 30 return d;
350 }
351  
« XS.xs ↑ Summary blib/lib/LCS/XS.pm »