Debian Codegraph @

lib/diffseq.h

raw symbols SCIP
1 /*
2 * fstrcmp - fuzzy string compare library
3 * Copyright (C) 2009, 2011 Peter Miller.
4 *
5 * Derived from gettext 0.17
6 * Analyze differences between two vectors.
7 * Copyright (C) 1988-1989, 1992-1995, 2001-2004, 2006, 2007 Free
8 * Software Foundation, Inc.
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24
25 /*
26 * The basic idea is to consider two vectors as similar if, when
27 * transforming the first vector into the second vector through a
28 * sequence of edits (inserts and deletes of one element each), this
29 * sequence is short - or equivalently, if the ordered list of elements
30 * that are untouched by these edits is long. For a good introduction
31 * to the subject, read about the "Levenshtein distance" in Wikipedia.
32 *
33 * The basic algorithm is described in:
34 * "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
35 * Algorithmica Vol. 1 No. 2, 1986, pp. 251-266;
36 * see especially section 4.2, which describes the variation used below.
37 *
38 * The basic algorithm was independently discovered as described in:
39 * "Algorithms for Approximate String Matching", E. Ukkonen, Information
40 * and Control Vol. 64, 1985, pp. 100-118.
41 *
42 * Unless the 'find_minimal' flag is set, this code uses the
43 * TOO_EXPENSIVE heuristic, by Paul Eggert, to limit the cost to
44 * O(N**1.5 log N) at the price of producing suboptimal output for large
45 * inputs with many differences.
46 */
47
48 /*
49 * Before including this file, you need to define:
50 *
51 * ELEMENT
52 * The element type of the vectors being compared.
53 * EQUAL
54 * A two-argument macro that tests two elements for equality.
55 * OFFSET
56 * A signed integer type sufficient to hold the difference between
57 * two indices. Usually something like ssize_t.
58 * EXTRA_CONTEXT_FIELDS
59 * Declarations of fields for 'struct context'.
60 * NOTE_DELETE(ctxt, xoff)
61 * Record the removal of the object xvec[xoff].
62 * NOTE_INSERT(ctxt, yoff)
63 * Record the insertion of the object yvec[yoff].
64 * USE_HEURISTIC
65 * (Optional) Define if you want to support the heuristic for large
66 * vectors.
67 */
68
69 /* Maximum value of type OFFSET. */
70 #define OFFSET_MAX \
71 ((((OFFSET)1 << (sizeof (OFFSET) * CHAR_BIT - 2)) - 1) * 2 + 1)
72
73 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
74 #ifndef IF_LINT
75 # ifdef lint
76 # define IF_LINT(Code) Code
77 # else
78 # define IF_LINT(Code) /* empty */
79 # endif
80 #endif
81
82 /*
83 * Context of comparison operation.
84 */
85 struct context
86 {
87 /*
88 * Vectors being compared.
89 */
90 const ELEMENT *xvec;
91 const ELEMENT *yvec;
92
93 /*
94 * Extra fields.
95 */
96 EXTRA_CONTEXT_FIELDS
97
98 /*
99 * Vector, indexed by diagonal, containing 1 + the X coordinate of
100 * the point furthest along the given diagonal in the forward search
101 * of the edit matrix.
102 */
103 OFFSET *fdiag;
104
105 /*
106 * Vector, indexed by diagonal, containing the X coordinate of the
107 * point furthest along the given diagonal in the backward search of
108 * the edit matrix.
109 */
110 OFFSET *bdiag;
111
112 #ifdef USE_HEURISTIC
113 /*
114 * This corresponds to the diff -H flag. With this heuristic, for
115 * vectors with a constant small density of changes, the algorithm is
116 * linear in the vectors size.
117 */
118 unsigned heuristic :1;
119 #endif
120
121 /*
122 * Edit scripts longer than this are too expensive to compute.
123 */
124 OFFSET too_expensive;
125
126 /*
127 * Snakes bigger than this are considered 'big'.
128 */
129 #define SNAKE_LIMIT 20
130 };
131
132 struct partition
133 {
134 /*
135 * Midpoints of this partition.
136 */
137 OFFSET xmid;
138 OFFSET ymid;
139
140 /*
141 * True if low half will be analyzed minimally.
142 */
143 unsigned lo_minimal :1;
144
145 /*
146 * Likewise for high half.
147 */
148 unsigned hi_minimal :1;
149 };
150
151
152 /*
153 * Find the midpoint of the shortest edit script for a specified portion
154 * of the two vectors.
155 *
156 * Scan from the beginnings of the vectors, and simultaneously from
157 * the ends, doing a breadth-first search through the space of
158 * edit-sequence. When the two searches meet, we have found the
159 * midpoint of the shortest edit sequence.
160 *
161 * If FIND_MINIMAL is true, find the minimal edit script regardless of
162 * expense. Otherwise, if the search is too expensive, use heuristics
163 * to stop the search and report a suboptimal answer.
164 *
165 * Set PART->(xmid,ymid) to the midpoint (XMID,YMID). The diagonal
166 * number XMID - YMID equals the number of inserted elements minus
167 * the number of deleted elements (counting only elements before the
168 * midpoint).
169 *
170 * Set PART->lo_minimal to true iff the minimal edit script for the left
171 * half of the partition is known; similarly for PART->hi_minimal.
172 *
173 * This function assumes that the first elements of the specified
174 * portions of the two vectors do not match, and likewise that the last
175 * elements do not match. The caller must trim matching elements from
176 * the beginning and end of the portions it is going to specify.
177 *
178 * If we return the "wrong" partitions, the worst this can do is cause
179 * suboptimal diff output. It cannot cause incorrect diff output.
180 */
181
182 static void
183 diag(OFFSET xoff, OFFSET xlim, OFFSET yoff, OFFSET ylim, int find_minimal,
184 struct partition *part, struct context *ctxt)
185 {
186 OFFSET *const fd = ctxt->fdiag; /* Give the compiler a chance. */
187 OFFSET *const bd = ctxt->bdiag; /* Additional help for the compiler. */
188 const ELEMENT *const xv = ctxt->xvec; /* Still more help for the compiler */
189 const ELEMENT *const yv = ctxt->yvec; /* And more and more . . . */
190 const OFFSET dmin = xoff - ylim; /* Minimum valid diagonal. */
191 const OFFSET dmax = xlim - yoff; /* Maximum valid diagonal. */
192 const OFFSET fmid = xoff - yoff; /* Center diagonal of top-down search. */
193 const OFFSET bmid = xlim - ylim; /* Center diagonal of bottom-up search. */
194 OFFSET fmin = fmid;
195 OFFSET fmax = fmid; /* Limits of top-down search. */
196 OFFSET bmin = bmid;
197 OFFSET bmax = bmid; /* Limits of bottom-up search. */
198 OFFSET c; /* Cost. */
199
200 /*
201 * True if southeast corner is on an odd diagonal with respect to
202 * the northwest.
203 */
204 int odd = (fmid - bmid) & 1;
205
206 fd[fmid] = xoff;
207 bd[bmid] = xlim;
208
209 for (c = 1; ; ++c)
210 {
211 OFFSET d; /* Active diagonal. */
212 #ifdef USE_HEURISTIC
213 int big_snake = 0;
214 #endif
215
216 /* Extend the top-down search by an edit step in each diagonal. */
217 if (fmin > dmin)
218 fd[--fmin - 1] = -1;
219 else
220 ++fmin;
221 if (fmax < dmax)
222 fd[++fmax + 1] = -1;
223 else
224 --fmax;
225 for (d = fmax; d >= fmin; d -= 2)
226 {
227 OFFSET x;
228 OFFSET y;
229 OFFSET tlo = fd[d - 1];
230 OFFSET thi = fd[d + 1];
231 OFFSET x0 = tlo < thi ? thi : tlo + 1;
232
233 for
234 (
235 x = x0, y = x0 - d;
236 x < xlim && y < ylim && EQUAL(xv[x], yv[y]);
237 x++, y++
238 )
239 ;
240 #ifdef USE_HEURISTIC
241 if (x - x0 > SNAKE_LIMIT)
242 big_snake = 1;
243 #endif
244 fd[d] = x;
245 if (odd && bmin <= d && d <= bmax && bd[d] <= x)
246 {
247 part->xmid = x;
248 part->ymid = y;
249 part->lo_minimal = 1;
250 part->hi_minimal = 1;
251 return;
252 }
253 }
254
255 /* Similarly extend the bottom-up search. */
256 if (bmin > dmin)
257 bd[--bmin - 1] = OFFSET_MAX;
258 else
259 ++bmin;
260 if (bmax < dmax)
261 bd[++bmax + 1] = OFFSET_MAX;
262 else
263 --bmax;
264 for (d = bmax; d >= bmin; d -= 2)
265 {
266 OFFSET x;
267 OFFSET y;
268 OFFSET tlo = bd[d - 1];
269 OFFSET thi = bd[d + 1];
270 OFFSET x0 = tlo < thi ? tlo : thi - 1;
271
272 for
273 (
274 x = x0, y = x0 - d;
275 xoff < x && yoff < y && EQUAL(xv[x - 1], yv[y - 1]);
276 x--, y--
277 )
278 ;
279 #ifdef USE_HEURISTIC
280 if (x0 - x > SNAKE_LIMIT)
281 big_snake = 1;
282 #endif
283 bd[d] = x;
284 if (!odd && fmin <= d && d <= fmax && x <= fd[d])
285 {
286 part->xmid = x;
287 part->ymid = y;
288 part->lo_minimal = 1;
289 part->hi_minimal = 1;
290 return;
291 }
292 }
293
294 if (find_minimal)
295 continue;
296
297 #ifdef USE_HEURISTIC
298 /*
299 * Heuristic: check occasionally for a diagonal that has made
300 * lots of progress compared with the edit distance. If we have
301 * any such, find the one that has made the most progress and
302 * return it as if it had succeeded.
303 *
304 * With this heuristic, for vectors with a constant small
305 * density of changes, the algorithm is linear in the vector
306 * size.
307 */
308 if (200 < c && big_snake && ctxt->heuristic)
309 {
310 OFFSET best = 0;
311
312 for (d = fmax; d >= fmin; d -= 2)
313 {
314 OFFSET dd = d - fmid;
315 OFFSET x = fd[d];
316 OFFSET y = x - d;
317 OFFSET v = (x - xoff) * 2 - dd;
318
319 if (v > 12 * (c + (dd < 0 ? -dd : dd)))
320 {
321 if
322 (
323 v > best
324 &&
325 xoff + SNAKE_LIMIT <= x && x < xlim
326 &&
327 yoff + SNAKE_LIMIT <= y && y < ylim
328 )
329 {
330 int k;
331
332 /*
333 * We have a good enough best diagonal; now
334 * insist that it end with a significant snake.
335 */
336 for (k = 1; EQUAL(xv[x - k], yv[y - k]); k++)
337 {
338 if (k == SNAKE_LIMIT)
339 {
340 best = v;
341 part->xmid = x;
342 part->ymid = y;
343 break;
344 }
345 }
346 }
347 }
348 }
349 if (best > 0)
350 {
351 part->lo_minimal = 1;
352 part->hi_minimal = 0;
353 return;
354 }
355
356 best = 0;
357 for (d = bmax; d >= bmin; d -= 2)
358 {
359 OFFSET dd = d - bmid;
360 OFFSET x = bd[d];
361 OFFSET y = x - d;
362 OFFSET v = (xlim - x) * 2 + dd;
363
364 if (v > 12 * (c + (dd < 0 ? -dd : dd)))
365 {
366 if
367 (
368 v > best
369 &&
370 xoff < x && x <= xlim - SNAKE_LIMIT
371 &&
372 yoff < y && y <= ylim - SNAKE_LIMIT
373 )
374 {
375 int k;
376
377 /*
378 * We have a good enough best diagonal; now
379 * insist that it end with a significant snake.
380 */
381 for (k = 0; EQUAL(xv[x + k], yv[y + k]); k++)
382 {
383 if (k == SNAKE_LIMIT - 1)
384 {
385 best = v;
386 part->xmid = x;
387 part->ymid = y;
388 break;
389 }
390 }
391 }
392 }
393 }
394 if (best > 0)
395 {
396 part->lo_minimal = 0;
397 part->hi_minimal = 1;
398 return;
399 }
400 }
401 #endif /* USE_HEURISTIC */
402
403 /*
404 * Heuristic: if we've gone well beyond the call of duty, give
405 * up and report halfway between our best results so far.
406 */
407 if (c >= ctxt->too_expensive)
408 {
409 OFFSET fxybest = 0;
410 OFFSET fxbest = 0;
411 OFFSET bxybest = 0;
412 OFFSET bxbest = 0;
413
414 /*
415 * Find forward diagonal that maximizes X + Y.
416 */
417 fxybest = -1;
418 for (d = fmax; d >= fmin; d -= 2)
419 {
420 OFFSET x = MIN(fd[d], xlim);
421 OFFSET y = x - d;
422 if (ylim < y)
423 {
424 x = ylim + d;
425 y = ylim;
426 }
427 if (fxybest < x + y)
428 {
429 fxybest = x + y;
430 fxbest = x;
431 }
432 }
433
434 /*
435 * Find backward diagonal that minimizes X + Y.
436 */
437 bxybest = OFFSET_MAX;
438 for (d = bmax; d >= bmin; d -= 2)
439 {
440 OFFSET x = MAX(xoff, bd[d]);
441 OFFSET y = x - d;
442 if (y < yoff)
443 {
444 x = yoff + d;
445 y = yoff;
446 }
447 if (x + y < bxybest)
448 {
449 bxybest = x + y;
450 bxbest = x;
451 }
452 }
453
454 /*
455 * Use the better of the two diagonals.
456 */
457 if ((xlim + ylim) - bxybest < fxybest - (xoff + yoff))
458 {
459 part->xmid = fxbest;
460 part->ymid = fxybest - fxbest;
461 part->lo_minimal = 1;
462 part->hi_minimal = 0;
463 }
464 else
465 {
466 part->xmid = bxbest;
467 part->ymid = bxybest - bxbest;
468 part->lo_minimal = 0;
469 part->hi_minimal = 1;
470 }
471 return;
472 }
473 }
474 }
475
476
477 /*
478 * Compare in detail contiguous subsequences of the two vectors which are known,
479 * as a whole, to match each other.
480 *
481 * The subsequence of vector 0 is [XOFF, XLIM) and likewise for vector 1.
482 *
483 * Note that XLIM, YLIM are exclusive bounds. All indices into the vectors are
484 * origin-0.
485 *
486 * If FIND_MINIMAL, find a minimal difference no matter how expensive it is.
487 *
488 * The results are recorded by invoking NOTE_DELETE and NOTE_INSERT.
489 */
490
491 static void
492 compareseq(OFFSET xoff, OFFSET xlim, OFFSET yoff, OFFSET ylim,
493 int find_minimal, struct context *ctxt)
494 {
495 ELEMENT const *xv = ctxt->xvec; /* Help the compiler. */
496 ELEMENT const *yv = ctxt->yvec;
497
498 /*
499 * Slide down the bottom initial diagonal
500 */
501 while (xoff < xlim && yoff < ylim && EQUAL(xv[xoff], yv[yoff]))
502 {
503 xoff++;
504 yoff++;
505 }
506
507 /*
508 * Slide up the top initial diagonal.
509 */
510 while (xoff < xlim && yoff < ylim && EQUAL(xv[xlim - 1], yv[ylim - 1]))
511 {
512 xlim--;
513 ylim--;
514 }
515
516 /*
517 * Handle simple cases.
518 */
519 if (xoff == xlim)
520 {
521 while (yoff < ylim)
522 {
523 NOTE_INSERT(ctxt, yoff);
524 yoff++;
525 }
526 }
527 else if (yoff == ylim)
528 {
529 while (xoff < xlim)
530 {
531 NOTE_DELETE(ctxt, xoff);
532 xoff++;
533 }
534 }
535 else
536 {
537 struct partition part;
538
539 /*
540 * Find a point of correspondence in the middle of the vectors.
541 */
542 diag(xoff, xlim, yoff, ylim, find_minimal, &part, ctxt);
543
544 /*
545 * Use the partitions to split this problem into subproblems.
546 */
547 compareseq(xoff, part.xmid, yoff, part.ymid, part.lo_minimal, ctxt);
548 compareseq(part.xmid, xlim, part.ymid, ylim, part.hi_minimal, ctxt);
549 }
550 }
551
552 #undef ELEMENT
553 #undef EQUAL
554 #undef OFFSET
555 #undef EXTRA_CONTEXT_FIELDS
556 #undef NOTE_DELETE
557 #undef NOTE_INSERT
558 #undef USE_HEURISTIC
559 #undef OFFSET_MAX