annotate lib/diffseq.h @ 13243:97c81b93ec7c

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