Blame


1 3b0f3d61 2020-01-22 neels /* Myers diff algorithm implementation, invented by Eugene W. Myers [1].
2 3b0f3d61 2020-01-22 neels * Implementations of both the Myers Divide Et Impera (using linear space)
3 3b0f3d61 2020-01-22 neels * and the canonical Myers algorithm (using quadratic space). */
4 3b0f3d61 2020-01-22 neels /*
5 3b0f3d61 2020-01-22 neels * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
6 3b0f3d61 2020-01-22 neels *
7 3b0f3d61 2020-01-22 neels * Permission to use, copy, modify, and distribute this software for any
8 3b0f3d61 2020-01-22 neels * purpose with or without fee is hereby granted, provided that the above
9 3b0f3d61 2020-01-22 neels * copyright notice and this permission notice appear in all copies.
10 3b0f3d61 2020-01-22 neels *
11 3b0f3d61 2020-01-22 neels * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 3b0f3d61 2020-01-22 neels * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 3b0f3d61 2020-01-22 neels * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 3b0f3d61 2020-01-22 neels * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 3b0f3d61 2020-01-22 neels * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 3b0f3d61 2020-01-22 neels * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 3b0f3d61 2020-01-22 neels * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 3b0f3d61 2020-01-22 neels */
19 3b0f3d61 2020-01-22 neels
20 e10a628a 2020-09-16 stsp #include <inttypes.h>
21 e10a628a 2020-09-16 stsp #include <stdbool.h>
22 e10a628a 2020-09-16 stsp #include <stdlib.h>
23 e10a628a 2020-09-16 stsp #include <string.h>
24 e10a628a 2020-09-16 stsp #include <errno.h>
25 e10a628a 2020-09-16 stsp
26 e10a628a 2020-09-16 stsp #include <diff/arraylist.h>
27 3b0f3d61 2020-01-22 neels #include <diff/diff_main.h>
28 3b0f3d61 2020-01-22 neels
29 3b0f3d61 2020-01-22 neels #include "debug.h"
30 3b0f3d61 2020-01-22 neels
31 3b0f3d61 2020-01-22 neels /* Myers' diff algorithm [1] is nicely explained in [2].
32 3b0f3d61 2020-01-22 neels * [1] http://www.xmailserver.org/diff2.pdf
33 3b0f3d61 2020-01-22 neels * [2] https://blog.jcoglan.com/2017/02/12/the-myers-diff-algorithm-part-1/ ff.
34 3b0f3d61 2020-01-22 neels *
35 3b0f3d61 2020-01-22 neels * Myers approaches finding the smallest diff as a graph problem.
36 3b0f3d61 2020-01-22 neels * The crux is that the original algorithm requires quadratic amount of memory:
37 0d27172a 2020-05-06 neels * both sides' lengths added, and that squared. So if we're diffing lines of
38 0d27172a 2020-05-06 neels * text, two files with 1000 lines each would blow up to a matrix of about
39 0d27172a 2020-05-06 neels * 2000 * 2000 ints of state, about 16 Mb of RAM to figure out 2 kb of text.
40 0d27172a 2020-05-06 neels * The solution is using Myers' "divide and conquer" extension algorithm, which
41 0d27172a 2020-05-06 neels * does the original traversal from both ends of the files to reach a middle
42 0d27172a 2020-05-06 neels * where these "snakes" touch, hence does not need to backtrace the traversal,
43 0d27172a 2020-05-06 neels * and so gets away with only keeping a single column of that huge state matrix
44 0d27172a 2020-05-06 neels * in memory.
45 3b0f3d61 2020-01-22 neels */
46 3b0f3d61 2020-01-22 neels
47 3b0f3d61 2020-01-22 neels struct diff_box {
48 3b0f3d61 2020-01-22 neels unsigned int left_start;
49 3b0f3d61 2020-01-22 neels unsigned int left_end;
50 3b0f3d61 2020-01-22 neels unsigned int right_start;
51 3b0f3d61 2020-01-22 neels unsigned int right_end;
52 3b0f3d61 2020-01-22 neels };
53 3b0f3d61 2020-01-22 neels
54 3b0f3d61 2020-01-22 neels /* If the two contents of a file are A B C D E and X B C Y,
55 3b0f3d61 2020-01-22 neels * the Myers diff graph looks like:
56 3b0f3d61 2020-01-22 neels *
57 3b0f3d61 2020-01-22 neels * k0 k1
58 3b0f3d61 2020-01-22 neels * \ \
59 3b0f3d61 2020-01-22 neels * k-1 0 1 2 3 4 5
60 3b0f3d61 2020-01-22 neels * \ A B C D E
61 3b0f3d61 2020-01-22 neels * 0 o-o-o-o-o-o
62 3b0f3d61 2020-01-22 neels * X | | | | | |
63 3b0f3d61 2020-01-22 neels * 1 o-o-o-o-o-o
64 3b0f3d61 2020-01-22 neels * B | |\| | | |
65 3b0f3d61 2020-01-22 neels * 2 o-o-o-o-o-o
66 3b0f3d61 2020-01-22 neels * C | | |\| | |
67 3b0f3d61 2020-01-22 neels * 3 o-o-o-o-o-o
68 3b0f3d61 2020-01-22 neels * Y | | | | | |\
69 3b0f3d61 2020-01-22 neels * 4 o-o-o-o-o-o c1
70 3b0f3d61 2020-01-22 neels * \ \
71 3b0f3d61 2020-01-22 neels * c-1 c0
72 3b0f3d61 2020-01-22 neels *
73 3b0f3d61 2020-01-22 neels * Moving right means delete an atom from the left-hand-side,
74 3b0f3d61 2020-01-22 neels * Moving down means add an atom from the right-hand-side.
75 0d27172a 2020-05-06 neels * Diagonals indicate identical atoms on both sides, the challenge is to use as
76 0d27172a 2020-05-06 neels * many diagonals as possible.
77 3b0f3d61 2020-01-22 neels *
78 0d27172a 2020-05-06 neels * The original Myers algorithm walks all the way from the top left to the
79 0d27172a 2020-05-06 neels * bottom right, remembers all steps, and then backtraces to find the shortest
80 0d27172a 2020-05-06 neels * path. However, that requires keeping the entire graph in memory, which needs
81 3b0f3d61 2020-01-22 neels * quadratic space.
82 3b0f3d61 2020-01-22 neels *
83 0d27172a 2020-05-06 neels * Myers adds a variant that uses linear space -- note, not linear time, only
84 0d27172a 2020-05-06 neels * linear space: walk forward and backward, find a meeting point in the middle,
85 0d27172a 2020-05-06 neels * and recurse on the two separate sections. This is called "divide and
86 0d27172a 2020-05-06 neels * conquer".
87 3b0f3d61 2020-01-22 neels *
88 0d27172a 2020-05-06 neels * d: the step number, starting with 0, a.k.a. the distance from the starting
89 0d27172a 2020-05-06 neels * point.
90 0d27172a 2020-05-06 neels * k: relative index in the state array for the forward scan, indicating on
91 0d27172a 2020-05-06 neels * which diagonal through the diff graph we currently are.
92 0d27172a 2020-05-06 neels * c: relative index in the state array for the backward scan, indicating the
93 0d27172a 2020-05-06 neels * diagonal number from the bottom up.
94 3b0f3d61 2020-01-22 neels *
95 3b0f3d61 2020-01-22 neels * The "divide and conquer" traversal through the Myers graph looks like this:
96 3b0f3d61 2020-01-22 neels *
97 3b0f3d61 2020-01-22 neels * | d= 0 1 2 3 2 1 0
98 3b0f3d61 2020-01-22 neels * ----+--------------------------------------------
99 3b0f3d61 2020-01-22 neels * k= | c=
100 3b0f3d61 2020-01-22 neels * 4 | 3
101 3b0f3d61 2020-01-22 neels * |
102 3b0f3d61 2020-01-22 neels * 3 | 3,0 5,2 2
103 3b0f3d61 2020-01-22 neels * | / \
104 3b0f3d61 2020-01-22 neels * 2 | 2,0 5,3 1
105 3b0f3d61 2020-01-22 neels * | / \
106 3b0f3d61 2020-01-22 neels * 1 | 1,0 4,3 >= 4,3 5,4<-- 0
107 3b0f3d61 2020-01-22 neels * | / / \ /
108 3b0f3d61 2020-01-22 neels * 0 | -->0,0 3,3 4,4 -1
109 3b0f3d61 2020-01-22 neels * | \ / /
110 3b0f3d61 2020-01-22 neels * -1 | 0,1 1,2 3,4 -2
111 3b0f3d61 2020-01-22 neels * | \ /
112 3b0f3d61 2020-01-22 neels * -2 | 0,2 -3
113 3b0f3d61 2020-01-22 neels * | \
114 3b0f3d61 2020-01-22 neels * | 0,3
115 3b0f3d61 2020-01-22 neels * | forward-> <-backward
116 3b0f3d61 2020-01-22 neels *
117 3b0f3d61 2020-01-22 neels * x,y pairs here are the coordinates in the Myers graph:
118 3b0f3d61 2020-01-22 neels * x = atom index in left-side source, y = atom index in the right-side source.
119 3b0f3d61 2020-01-22 neels *
120 0d27172a 2020-05-06 neels * Only one forward column and one backward column are kept in mem, each need at
121 0d27172a 2020-05-06 neels * most left.len + 1 + right.len items. Note that each d step occupies either
122 0d27172a 2020-05-06 neels * the even or the odd items of a column: if e.g. the previous column is in the
123 0d27172a 2020-05-06 neels * odd items, the next column is formed in the even items, without overwriting
124 0d27172a 2020-05-06 neels * the previous column's results.
125 3b0f3d61 2020-01-22 neels *
126 0d27172a 2020-05-06 neels * Also note that from the diagonal index k and the x coordinate, the y
127 0d27172a 2020-05-06 neels * coordinate can be derived:
128 3b0f3d61 2020-01-22 neels * y = x - k
129 0d27172a 2020-05-06 neels * Hence the state array only needs to keep the x coordinate, i.e. the position
130 0d27172a 2020-05-06 neels * in the left-hand file, and the y coordinate, i.e. position in the right-hand
131 0d27172a 2020-05-06 neels * file, is derived from the index in the state array.
132 3b0f3d61 2020-01-22 neels *
133 0d27172a 2020-05-06 neels * The two traces meet at 4,3, the first step (here found in the forward
134 0d27172a 2020-05-06 neels * traversal) where a forward position is on or past a backward traced position
135 0d27172a 2020-05-06 neels * on the same diagonal.
136 3b0f3d61 2020-01-22 neels *
137 3b0f3d61 2020-01-22 neels * This divides the problem space into:
138 3b0f3d61 2020-01-22 neels *
139 3b0f3d61 2020-01-22 neels * 0 1 2 3 4 5
140 3b0f3d61 2020-01-22 neels * A B C D E
141 3b0f3d61 2020-01-22 neels * 0 o-o-o-o-o
142 3b0f3d61 2020-01-22 neels * X | | | | |
143 3b0f3d61 2020-01-22 neels * 1 o-o-o-o-o
144 3b0f3d61 2020-01-22 neels * B | |\| | |
145 3b0f3d61 2020-01-22 neels * 2 o-o-o-o-o
146 3b0f3d61 2020-01-22 neels * C | | |\| |
147 3b0f3d61 2020-01-22 neels * 3 o-o-o-o-*-o *: forward and backward meet here
148 3b0f3d61 2020-01-22 neels * Y | |
149 3b0f3d61 2020-01-22 neels * 4 o-o
150 3b0f3d61 2020-01-22 neels *
151 3b0f3d61 2020-01-22 neels * Doing the same on each section lead to:
152 3b0f3d61 2020-01-22 neels *
153 3b0f3d61 2020-01-22 neels * 0 1 2 3 4 5
154 3b0f3d61 2020-01-22 neels * A B C D E
155 3b0f3d61 2020-01-22 neels * 0 o-o
156 3b0f3d61 2020-01-22 neels * X | |
157 3b0f3d61 2020-01-22 neels * 1 o-b b: backward d=1 first reaches here (sliding up the snake)
158 3b0f3d61 2020-01-22 neels * B \ f: then forward d=2 reaches here (sliding down the snake)
159 3b0f3d61 2020-01-22 neels * 2 o As result, the box from b to f is found to be identical;
160 0d27172a 2020-05-06 neels * C \ leaving a top box from 0,0 to 1,1 and a bottom trivial
161 0d27172a 2020-05-06 neels * 3 f-o tail 3,3 to 4,3.
162 3b0f3d61 2020-01-22 neels *
163 3b0f3d61 2020-01-22 neels * 3 o-*
164 3b0f3d61 2020-01-22 neels * Y |
165 3b0f3d61 2020-01-22 neels * 4 o *: forward and backward meet here
166 3b0f3d61 2020-01-22 neels *
167 3b0f3d61 2020-01-22 neels * and solving the last top left box gives:
168 3b0f3d61 2020-01-22 neels *
169 3b0f3d61 2020-01-22 neels * 0 1 2 3 4 5
170 3b0f3d61 2020-01-22 neels * A B C D E -A
171 3b0f3d61 2020-01-22 neels * 0 o-o +X
172 3b0f3d61 2020-01-22 neels * X | B
173 3b0f3d61 2020-01-22 neels * 1 o C
174 3b0f3d61 2020-01-22 neels * B \ -D
175 3b0f3d61 2020-01-22 neels * 2 o -E
176 3b0f3d61 2020-01-22 neels * C \ +Y
177 3b0f3d61 2020-01-22 neels * 3 o-o-o
178 3b0f3d61 2020-01-22 neels * Y |
179 3b0f3d61 2020-01-22 neels * 4 o
180 3b0f3d61 2020-01-22 neels *
181 3b0f3d61 2020-01-22 neels */
182 3b0f3d61 2020-01-22 neels
183 3b0f3d61 2020-01-22 neels #define xk_to_y(X, K) ((X) - (K))
184 3b0f3d61 2020-01-22 neels #define xc_to_y(X, C, DELTA) ((X) - (C) + (DELTA))
185 3b0f3d61 2020-01-22 neels #define k_to_c(K, DELTA) ((K) + (DELTA))
186 3b0f3d61 2020-01-22 neels #define c_to_k(C, DELTA) ((C) - (DELTA))
187 3b0f3d61 2020-01-22 neels
188 3b0f3d61 2020-01-22 neels /* Do one forwards step in the "divide and conquer" graph traversal.
189 3b0f3d61 2020-01-22 neels * left: the left side to diff.
190 3b0f3d61 2020-01-22 neels * right: the right side to diff against.
191 0d27172a 2020-05-06 neels * kd_forward: the traversal state for forwards traversal, modified by this
192 0d27172a 2020-05-06 neels * function.
193 3b0f3d61 2020-01-22 neels * This is carried over between invocations with increasing d.
194 0d27172a 2020-05-06 neels * kd_forward points at the center of the state array, allowing
195 0d27172a 2020-05-06 neels * negative indexes.
196 0d27172a 2020-05-06 neels * kd_backward: the traversal state for backwards traversal, to find a meeting
197 0d27172a 2020-05-06 neels * point.
198 0d27172a 2020-05-06 neels * Since forwards is done first, kd_backward will be valid for d -
199 0d27172a 2020-05-06 neels * 1, not d.
200 0d27172a 2020-05-06 neels * kd_backward points at the center of the state array, allowing
201 0d27172a 2020-05-06 neels * negative indexes.
202 0d27172a 2020-05-06 neels * d: Step or distance counter, indicating for what value of d the kd_forward
203 0d27172a 2020-05-06 neels * should be populated.
204 0d27172a 2020-05-06 neels * For d == 0, kd_forward[0] is initialized, i.e. the first invocation should
205 0d27172a 2020-05-06 neels * be for d == 0.
206 3b0f3d61 2020-01-22 neels * meeting_snake: resulting meeting point, if any.
207 0d27172a 2020-05-06 neels * Return true when a meeting point has been identified.
208 3b0f3d61 2020-01-22 neels */
209 ac2eeeff 2020-09-20 neels static int
210 ac2eeeff 2020-09-20 neels diff_divide_myers_forward(bool *found_midpoint,
211 ac2eeeff 2020-09-20 neels struct diff_data *left, struct diff_data *right,
212 61a7b578 2020-05-06 neels int *kd_forward, int *kd_backward, int d,
213 61a7b578 2020-05-06 neels struct diff_box *meeting_snake)
214 3b0f3d61 2020-01-22 neels {
215 3b0f3d61 2020-01-22 neels int delta = (int)right->atoms.len - (int)left->atoms.len;
216 3b0f3d61 2020-01-22 neels int k;
217 3b0f3d61 2020-01-22 neels int x;
218 ac2eeeff 2020-09-20 neels *found_midpoint = false;
219 3b0f3d61 2020-01-22 neels
220 3b0f3d61 2020-01-22 neels debug("-- %s d=%d\n", __func__, d);
221 3b0f3d61 2020-01-22 neels
222 3b0f3d61 2020-01-22 neels for (k = d; k >= -d; k -= 2) {
223 3b0f3d61 2020-01-22 neels if (k < -(int)right->atoms.len || k > (int)left->atoms.len) {
224 0d27172a 2020-05-06 neels /* This diagonal is completely outside of the Myers
225 0d27172a 2020-05-06 neels * graph, don't calculate it. */
226 3b0f3d61 2020-01-22 neels if (k < -(int)right->atoms.len)
227 0d27172a 2020-05-06 neels debug(" %d k < -(int)right->atoms.len %d\n", k,
228 0d27172a 2020-05-06 neels -(int)right->atoms.len);
229 3b0f3d61 2020-01-22 neels else
230 0d27172a 2020-05-06 neels debug(" %d k > left->atoms.len %d\n", k,
231 0d27172a 2020-05-06 neels left->atoms.len);
232 3b0f3d61 2020-01-22 neels if (k < 0) {
233 0d27172a 2020-05-06 neels /* We are traversing negatively, and already
234 0d27172a 2020-05-06 neels * below the entire graph, nothing will come of
235 0d27172a 2020-05-06 neels * this. */
236 3b0f3d61 2020-01-22 neels debug(" break");
237 3b0f3d61 2020-01-22 neels break;
238 3b0f3d61 2020-01-22 neels }
239 3b0f3d61 2020-01-22 neels debug(" continue");
240 3b0f3d61 2020-01-22 neels continue;
241 3b0f3d61 2020-01-22 neels }
242 3b0f3d61 2020-01-22 neels debug("- k = %d\n", k);
243 3b0f3d61 2020-01-22 neels if (d == 0) {
244 0d27172a 2020-05-06 neels /* This is the initializing step. There is no prev_k
245 0d27172a 2020-05-06 neels * yet, get the initial x from the top left of the Myers
246 0d27172a 2020-05-06 neels * graph. */
247 3b0f3d61 2020-01-22 neels x = 0;
248 3b0f3d61 2020-01-22 neels }
249 0d27172a 2020-05-06 neels /* Favoring "-" lines first means favoring moving rightwards in
250 0d27172a 2020-05-06 neels * the Myers graph.
251 0d27172a 2020-05-06 neels * For this, all k should derive from k - 1, only the bottom
252 0d27172a 2020-05-06 neels * most k derive from k + 1:
253 3b0f3d61 2020-01-22 neels *
254 3b0f3d61 2020-01-22 neels * | d= 0 1 2
255 3b0f3d61 2020-01-22 neels * ----+----------------
256 3b0f3d61 2020-01-22 neels * k= |
257 3b0f3d61 2020-01-22 neels * 2 | 2,0 <-- from prev_k = 2 - 1 = 1
258 3b0f3d61 2020-01-22 neels * | /
259 3b0f3d61 2020-01-22 neels * 1 | 1,0
260 3b0f3d61 2020-01-22 neels * | /
261 3b0f3d61 2020-01-22 neels * 0 | -->0,0 3,3
262 3b0f3d61 2020-01-22 neels * | \\ /
263 0d27172a 2020-05-06 neels * -1 | 0,1 <-- bottom most for d=1 from
264 0d27172a 2020-05-06 neels * | \\ prev_k = -1 + 1 = 0
265 0d27172a 2020-05-06 neels * -2 | 0,2 <-- bottom most for d=2 from
266 0d27172a 2020-05-06 neels * prev_k = -2 + 1 = -1
267 3b0f3d61 2020-01-22 neels *
268 0d27172a 2020-05-06 neels * Except when a k + 1 from a previous run already means a
269 0d27172a 2020-05-06 neels * further advancement in the graph.
270 3b0f3d61 2020-01-22 neels * If k == d, there is no k + 1 and k - 1 is the only option.
271 0d27172a 2020-05-06 neels * If k < d, use k + 1 in case that yields a larger x. Also use
272 0d27172a 2020-05-06 neels * k + 1 if k - 1 is outside the graph.
273 3b0f3d61 2020-01-22 neels */
274 0d27172a 2020-05-06 neels else if (k > -d
275 0d27172a 2020-05-06 neels && (k == d
276 0d27172a 2020-05-06 neels || (k - 1 >= -(int)right->atoms.len
277 0d27172a 2020-05-06 neels && kd_forward[k - 1] >= kd_forward[k + 1]))) {
278 3b0f3d61 2020-01-22 neels /* Advance from k - 1.
279 0d27172a 2020-05-06 neels * From position prev_k, step to the right in the Myers
280 0d27172a 2020-05-06 neels * graph: x += 1.
281 3b0f3d61 2020-01-22 neels */
282 3b0f3d61 2020-01-22 neels int prev_k = k - 1;
283 f71e8098 2020-05-05 neels int prev_x = kd_forward[prev_k];
284 3b0f3d61 2020-01-22 neels x = prev_x + 1;
285 3b0f3d61 2020-01-22 neels } else {
286 3b0f3d61 2020-01-22 neels /* The bottom most one.
287 0d27172a 2020-05-06 neels * From position prev_k, step to the bottom in the Myers
288 0d27172a 2020-05-06 neels * graph: y += 1.
289 0d27172a 2020-05-06 neels * Incrementing y is achieved by decrementing k while
290 0d27172a 2020-05-06 neels * keeping the same x.
291 3b0f3d61 2020-01-22 neels * (since we're deriving y from y = x - k).
292 3b0f3d61 2020-01-22 neels */
293 3b0f3d61 2020-01-22 neels int prev_k = k + 1;
294 f71e8098 2020-05-05 neels int prev_x = kd_forward[prev_k];
295 3b0f3d61 2020-01-22 neels x = prev_x;
296 3b0f3d61 2020-01-22 neels }
297 3b0f3d61 2020-01-22 neels
298 f71e8098 2020-05-05 neels int x_before_slide = x;
299 3b0f3d61 2020-01-22 neels /* Slide down any snake that we might find here. */
300 b3fb4686 2020-09-20 neels while (x < left->atoms.len && xk_to_y(x, k) < right->atoms.len) {
301 b3fb4686 2020-09-20 neels bool same;
302 b3fb4686 2020-09-20 neels int r = diff_atom_same(&same,
303 b3fb4686 2020-09-20 neels &left->atoms.head[x],
304 b3fb4686 2020-09-20 neels &right->atoms.head[
305 b3fb4686 2020-09-20 neels xk_to_y(x, k)]);
306 ac2eeeff 2020-09-20 neels if (r)
307 ac2eeeff 2020-09-20 neels return r;
308 b3fb4686 2020-09-20 neels if (!same)
309 b3fb4686 2020-09-20 neels break;
310 b3fb4686 2020-09-20 neels x++;
311 b3fb4686 2020-09-20 neels }
312 3b0f3d61 2020-01-22 neels kd_forward[k] = x;
313 c5419a05 2020-05-05 neels if (x_before_slide != x) {
314 c5419a05 2020-05-05 neels debug(" down %d similar lines\n", x - x_before_slide);
315 c5419a05 2020-05-05 neels }
316 3b0f3d61 2020-01-22 neels
317 3b0f3d61 2020-01-22 neels if (DEBUG) {
318 3b0f3d61 2020-01-22 neels int fi;
319 3b0f3d61 2020-01-22 neels for (fi = d; fi >= k; fi--) {
320 0d27172a 2020-05-06 neels debug("kd_forward[%d] = (%d, %d)\n", fi,
321 0d27172a 2020-05-06 neels kd_forward[fi], kd_forward[fi] - fi);
322 3b0f3d61 2020-01-22 neels }
323 3b0f3d61 2020-01-22 neels }
324 3b0f3d61 2020-01-22 neels
325 3b0f3d61 2020-01-22 neels if (x < 0 || x > left->atoms.len
326 3b0f3d61 2020-01-22 neels || xk_to_y(x, k) < 0 || xk_to_y(x, k) > right->atoms.len)
327 3b0f3d61 2020-01-22 neels continue;
328 3b0f3d61 2020-01-22 neels
329 0d27172a 2020-05-06 neels /* Figured out a new forwards traversal, see if this has gone
330 0d27172a 2020-05-06 neels * onto or even past a preceding backwards traversal.
331 3b0f3d61 2020-01-22 neels *
332 0d27172a 2020-05-06 neels * If the delta in length is odd, then d and backwards_d hit the
333 0d27172a 2020-05-06 neels * same state indexes:
334 3b0f3d61 2020-01-22 neels * | d= 0 1 2 1 0
335 3b0f3d61 2020-01-22 neels * ----+---------------- ----------------
336 3b0f3d61 2020-01-22 neels * k= | c=
337 3b0f3d61 2020-01-22 neels * 4 | 3
338 3b0f3d61 2020-01-22 neels * |
339 3b0f3d61 2020-01-22 neels * 3 | 2
340 3b0f3d61 2020-01-22 neels * | same
341 3b0f3d61 2020-01-22 neels * 2 | 2,0====5,3 1
342 3b0f3d61 2020-01-22 neels * | / \
343 3b0f3d61 2020-01-22 neels * 1 | 1,0 5,4<-- 0
344 3b0f3d61 2020-01-22 neels * | / /
345 3b0f3d61 2020-01-22 neels * 0 | -->0,0 3,3====4,4 -1
346 3b0f3d61 2020-01-22 neels * | \ /
347 3b0f3d61 2020-01-22 neels * -1 | 0,1 -2
348 3b0f3d61 2020-01-22 neels * | \
349 3b0f3d61 2020-01-22 neels * -2 | 0,2 -3
350 3b0f3d61 2020-01-22 neels * |
351 3b0f3d61 2020-01-22 neels *
352 0d27172a 2020-05-06 neels * If the delta is even, they end up off-by-one, i.e. on
353 0d27172a 2020-05-06 neels * different diagonals:
354 3b0f3d61 2020-01-22 neels *
355 3b0f3d61 2020-01-22 neels * | d= 0 1 2 1 0
356 3b0f3d61 2020-01-22 neels * ----+---------------- ----------------
357 3b0f3d61 2020-01-22 neels * | c=
358 3b0f3d61 2020-01-22 neels * 3 | 3
359 3b0f3d61 2020-01-22 neels * |
360 3b0f3d61 2020-01-22 neels * 2 | 2,0 off 2
361 3b0f3d61 2020-01-22 neels * | / \\
362 3b0f3d61 2020-01-22 neels * 1 | 1,0 4,3 1
363 3b0f3d61 2020-01-22 neels * | / // \
364 3b0f3d61 2020-01-22 neels * 0 | -->0,0 3,3 4,4<-- 0
365 3b0f3d61 2020-01-22 neels * | \ / /
366 3b0f3d61 2020-01-22 neels * -1 | 0,1 3,4 -1
367 3b0f3d61 2020-01-22 neels * | \ //
368 3b0f3d61 2020-01-22 neels * -2 | 0,2 -2
369 3b0f3d61 2020-01-22 neels * |
370 3b0f3d61 2020-01-22 neels *
371 0d27172a 2020-05-06 neels * So in the forward path, we can only match up diagonals when
372 0d27172a 2020-05-06 neels * the delta is odd.
373 3b0f3d61 2020-01-22 neels */
374 fd42ca98 2020-05-05 neels if ((delta & 1) == 0)
375 fd42ca98 2020-05-05 neels continue;
376 0d27172a 2020-05-06 neels /* Forwards is done first, so the backwards one was still at
377 0d27172a 2020-05-06 neels * d - 1. Can't do this for d == 0. */
378 3b0f3d61 2020-01-22 neels int backwards_d = d - 1;
379 fd42ca98 2020-05-05 neels if (backwards_d < 0)
380 fd42ca98 2020-05-05 neels continue;
381 3b0f3d61 2020-01-22 neels
382 fd42ca98 2020-05-05 neels debug("backwards_d = %d\n", backwards_d);
383 3b0f3d61 2020-01-22 neels
384 0d27172a 2020-05-06 neels /* If both sides have the same length, forward and backward
385 0d27172a 2020-05-06 neels * start on the same diagonal, meaning the backwards state index
386 0d27172a 2020-05-06 neels * c == k.
387 0d27172a 2020-05-06 neels * As soon as the lengths are not the same, the backwards
388 0d27172a 2020-05-06 neels * traversal starts on a different diagonal, and c = k shifted
389 0d27172a 2020-05-06 neels * by the difference in length.
390 fd42ca98 2020-05-05 neels */
391 fd42ca98 2020-05-05 neels int c = k_to_c(k, delta);
392 fd42ca98 2020-05-05 neels
393 0d27172a 2020-05-06 neels /* When the file sizes are very different, the traversal trees
394 0d27172a 2020-05-06 neels * start on far distant diagonals.
395 0d27172a 2020-05-06 neels * They don't necessarily meet straight on. See whether this
396 0d27172a 2020-05-06 neels * forward value is on a diagonal that is also valid in
397 0d27172a 2020-05-06 neels * kd_backward[], and match them if so. */
398 fd42ca98 2020-05-05 neels if (c >= -backwards_d && c <= backwards_d) {
399 0d27172a 2020-05-06 neels /* Current k is on a diagonal that exists in
400 0d27172a 2020-05-06 neels * kd_backward[]. If the two x positions have met or
401 0d27172a 2020-05-06 neels * passed (forward walked onto or past backward), then
402 0d27172a 2020-05-06 neels * we've found a midpoint / a mid-box.
403 fd42ca98 2020-05-05 neels *
404 0d27172a 2020-05-06 neels * When forwards and backwards traversals meet, the
405 0d27172a 2020-05-06 neels * endpoints of the mid-snake are not the two points in
406 0d27172a 2020-05-06 neels * kd_forward and kd_backward, but rather the section
407 0d27172a 2020-05-06 neels * that was slid (if any) of the current
408 0d27172a 2020-05-06 neels * forward/backward traversal only.
409 fd42ca98 2020-05-05 neels *
410 f71e8098 2020-05-05 neels * For example:
411 f71e8098 2020-05-05 neels *
412 f71e8098 2020-05-05 neels * o
413 f71e8098 2020-05-05 neels * \
414 f71e8098 2020-05-05 neels * o
415 f71e8098 2020-05-05 neels * \
416 f71e8098 2020-05-05 neels * o
417 f71e8098 2020-05-05 neels * \
418 f71e8098 2020-05-05 neels * o
419 f71e8098 2020-05-05 neels * \
420 f71e8098 2020-05-05 neels * X o o
421 f71e8098 2020-05-05 neels * | | |
422 f71e8098 2020-05-05 neels * o-o-o o
423 f71e8098 2020-05-05 neels * \|
424 f71e8098 2020-05-05 neels * M
425 f71e8098 2020-05-05 neels * \
426 f71e8098 2020-05-05 neels * o
427 f71e8098 2020-05-05 neels * \
428 f71e8098 2020-05-05 neels * A o
429 f71e8098 2020-05-05 neels * | |
430 f71e8098 2020-05-05 neels * o-o-o
431 f71e8098 2020-05-05 neels *
432 0d27172a 2020-05-06 neels * The forward traversal reached M from the top and slid
433 0d27172a 2020-05-06 neels * downwards to A. The backward traversal already
434 0d27172a 2020-05-06 neels * reached X, which is not a straight line from M
435 0d27172a 2020-05-06 neels * anymore, so picking a mid-snake from M to X would
436 0d27172a 2020-05-06 neels * yield a mistake.
437 f71e8098 2020-05-05 neels *
438 0d27172a 2020-05-06 neels * The correct mid-snake is between M and A. M is where
439 0d27172a 2020-05-06 neels * the forward traversal hit the diagonal that the
440 0d27172a 2020-05-06 neels * backward traversal has already passed, and A is what
441 0d27172a 2020-05-06 neels * it reaches when sliding down identical lines.
442 fd42ca98 2020-05-05 neels */
443 fd42ca98 2020-05-05 neels int backward_x = kd_backward[c];
444 50198b5f 2020-05-05 neels debug("Compare: k=%d c=%d is (%d,%d) >= (%d,%d)?\n",
445 0d27172a 2020-05-06 neels k, c, x, xk_to_y(x, k), backward_x,
446 0d27172a 2020-05-06 neels xc_to_y(backward_x, c, delta));
447 f71e8098 2020-05-05 neels if (x >= backward_x) {
448 fd42ca98 2020-05-05 neels *meeting_snake = (struct diff_box){
449 f71e8098 2020-05-05 neels .left_start = x_before_slide,
450 fd42ca98 2020-05-05 neels .left_end = x,
451 0d27172a 2020-05-06 neels .right_start = xc_to_y(x_before_slide,
452 0d27172a 2020-05-06 neels c, delta),
453 fd42ca98 2020-05-05 neels .right_end = xk_to_y(x, k),
454 fd42ca98 2020-05-05 neels };
455 fd42ca98 2020-05-05 neels debug("HIT x=(%u,%u) - y=(%u,%u)\n",
456 fd42ca98 2020-05-05 neels meeting_snake->left_start,
457 fd42ca98 2020-05-05 neels meeting_snake->right_start,
458 fd42ca98 2020-05-05 neels meeting_snake->left_end,
459 fd42ca98 2020-05-05 neels meeting_snake->right_end);
460 0d27172a 2020-05-06 neels debug_dump_myers_graph(left, right, NULL,
461 0d27172a 2020-05-06 neels kd_forward, d,
462 0d27172a 2020-05-06 neels kd_backward, d-1);
463 ac2eeeff 2020-09-20 neels *found_midpoint = true;
464 ac2eeeff 2020-09-20 neels return 0;
465 3b0f3d61 2020-01-22 neels }
466 3b0f3d61 2020-01-22 neels }
467 3b0f3d61 2020-01-22 neels }
468 50198b5f 2020-05-05 neels
469 0d27172a 2020-05-06 neels debug_dump_myers_graph(left, right, NULL, kd_forward, d,
470 0d27172a 2020-05-06 neels kd_backward, d-1);
471 ac2eeeff 2020-09-20 neels return 0;
472 3b0f3d61 2020-01-22 neels }
473 3b0f3d61 2020-01-22 neels
474 3b0f3d61 2020-01-22 neels /* Do one backwards step in the "divide and conquer" graph traversal.
475 3b0f3d61 2020-01-22 neels * left: the left side to diff.
476 3b0f3d61 2020-01-22 neels * right: the right side to diff against.
477 0d27172a 2020-05-06 neels * kd_forward: the traversal state for forwards traversal, to find a meeting
478 0d27172a 2020-05-06 neels * point.
479 0d27172a 2020-05-06 neels * Since forwards is done first, after this, both kd_forward and
480 0d27172a 2020-05-06 neels * kd_backward will be valid for d.
481 0d27172a 2020-05-06 neels * kd_forward points at the center of the state array, allowing
482 0d27172a 2020-05-06 neels * negative indexes.
483 0d27172a 2020-05-06 neels * kd_backward: the traversal state for backwards traversal, to find a meeting
484 0d27172a 2020-05-06 neels * point.
485 3b0f3d61 2020-01-22 neels * This is carried over between invocations with increasing d.
486 0d27172a 2020-05-06 neels * kd_backward points at the center of the state array, allowing
487 0d27172a 2020-05-06 neels * negative indexes.
488 0d27172a 2020-05-06 neels * d: Step or distance counter, indicating for what value of d the kd_backward
489 0d27172a 2020-05-06 neels * should be populated.
490 0d27172a 2020-05-06 neels * Before the first invocation, kd_backward[0] shall point at the bottom
491 0d27172a 2020-05-06 neels * right of the Myers graph (left.len, right.len).
492 3b0f3d61 2020-01-22 neels * The first invocation will be for d == 1.
493 3b0f3d61 2020-01-22 neels * meeting_snake: resulting meeting point, if any.
494 0d27172a 2020-05-06 neels * Return true when a meeting point has been identified.
495 3b0f3d61 2020-01-22 neels */
496 ac2eeeff 2020-09-20 neels static int
497 ac2eeeff 2020-09-20 neels diff_divide_myers_backward(bool *found_midpoint,
498 ac2eeeff 2020-09-20 neels struct diff_data *left, struct diff_data *right,
499 61a7b578 2020-05-06 neels int *kd_forward, int *kd_backward, int d,
500 61a7b578 2020-05-06 neels struct diff_box *meeting_snake)
501 3b0f3d61 2020-01-22 neels {
502 3b0f3d61 2020-01-22 neels int delta = (int)right->atoms.len - (int)left->atoms.len;
503 3b0f3d61 2020-01-22 neels int c;
504 3b0f3d61 2020-01-22 neels int x;
505 ac2eeeff 2020-09-20 neels
506 ac2eeeff 2020-09-20 neels *found_midpoint = false;
507 3b0f3d61 2020-01-22 neels
508 3b0f3d61 2020-01-22 neels debug("-- %s d=%d\n", __func__, d);
509 3b0f3d61 2020-01-22 neels
510 3b0f3d61 2020-01-22 neels for (c = d; c >= -d; c -= 2) {
511 3b0f3d61 2020-01-22 neels if (c < -(int)left->atoms.len || c > (int)right->atoms.len) {
512 0d27172a 2020-05-06 neels /* This diagonal is completely outside of the Myers
513 0d27172a 2020-05-06 neels * graph, don't calculate it. */
514 3b0f3d61 2020-01-22 neels if (c < -(int)left->atoms.len)
515 0d27172a 2020-05-06 neels debug(" %d c < -(int)left->atoms.len %d\n", c,
516 0d27172a 2020-05-06 neels -(int)left->atoms.len);
517 3b0f3d61 2020-01-22 neels else
518 0d27172a 2020-05-06 neels debug(" %d c > right->atoms.len %d\n", c,
519 0d27172a 2020-05-06 neels right->atoms.len);
520 3b0f3d61 2020-01-22 neels if (c < 0) {
521 0d27172a 2020-05-06 neels /* We are traversing negatively, and already
522 0d27172a 2020-05-06 neels * below the entire graph, nothing will come of
523 0d27172a 2020-05-06 neels * this. */
524 3b0f3d61 2020-01-22 neels debug(" break");
525 3b0f3d61 2020-01-22 neels break;
526 3b0f3d61 2020-01-22 neels }
527 3b0f3d61 2020-01-22 neels debug(" continue");
528 3b0f3d61 2020-01-22 neels continue;
529 3b0f3d61 2020-01-22 neels }
530 3b0f3d61 2020-01-22 neels debug("- c = %d\n", c);
531 3b0f3d61 2020-01-22 neels if (d == 0) {
532 0d27172a 2020-05-06 neels /* This is the initializing step. There is no prev_c
533 0d27172a 2020-05-06 neels * yet, get the initial x from the bottom right of the
534 0d27172a 2020-05-06 neels * Myers graph. */
535 3b0f3d61 2020-01-22 neels x = left->atoms.len;
536 3b0f3d61 2020-01-22 neels }
537 0d27172a 2020-05-06 neels /* Favoring "-" lines first means favoring moving rightwards in
538 0d27172a 2020-05-06 neels * the Myers graph.
539 0d27172a 2020-05-06 neels * For this, all c should derive from c - 1, only the bottom
540 0d27172a 2020-05-06 neels * most c derive from c + 1:
541 3b0f3d61 2020-01-22 neels *
542 3b0f3d61 2020-01-22 neels * 2 1 0
543 3b0f3d61 2020-01-22 neels * ---------------------------------------------------
544 3b0f3d61 2020-01-22 neels * c=
545 3b0f3d61 2020-01-22 neels * 3
546 3b0f3d61 2020-01-22 neels *
547 3b0f3d61 2020-01-22 neels * from prev_c = c - 1 --> 5,2 2
548 3b0f3d61 2020-01-22 neels * \
549 3b0f3d61 2020-01-22 neels * 5,3 1
550 3b0f3d61 2020-01-22 neels * \
551 3b0f3d61 2020-01-22 neels * 4,3 5,4<-- 0
552 3b0f3d61 2020-01-22 neels * \ /
553 3b0f3d61 2020-01-22 neels * bottom most for d=1 from c + 1 --> 4,4 -1
554 3b0f3d61 2020-01-22 neels * /
555 3b0f3d61 2020-01-22 neels * bottom most for d=2 --> 3,4 -2
556 3b0f3d61 2020-01-22 neels *
557 0d27172a 2020-05-06 neels * Except when a c + 1 from a previous run already means a
558 0d27172a 2020-05-06 neels * further advancement in the graph.
559 3b0f3d61 2020-01-22 neels * If c == d, there is no c + 1 and c - 1 is the only option.
560 0d27172a 2020-05-06 neels * If c < d, use c + 1 in case that yields a larger x.
561 0d27172a 2020-05-06 neels * Also use c + 1 if c - 1 is outside the graph.
562 3b0f3d61 2020-01-22 neels */
563 3b0f3d61 2020-01-22 neels else if (c > -d && (c == d
564 3b0f3d61 2020-01-22 neels || (c - 1 >= -(int)right->atoms.len
565 3b0f3d61 2020-01-22 neels && kd_backward[c - 1] <= kd_backward[c + 1]))) {
566 3b0f3d61 2020-01-22 neels /* A top one.
567 0d27172a 2020-05-06 neels * From position prev_c, step upwards in the Myers
568 0d27172a 2020-05-06 neels * graph: y -= 1.
569 0d27172a 2020-05-06 neels * Decrementing y is achieved by incrementing c while
570 0d27172a 2020-05-06 neels * keeping the same x. (since we're deriving y from
571 0d27172a 2020-05-06 neels * y = x - c + delta).
572 3b0f3d61 2020-01-22 neels */
573 3b0f3d61 2020-01-22 neels int prev_c = c - 1;
574 f71e8098 2020-05-05 neels int prev_x = kd_backward[prev_c];
575 3b0f3d61 2020-01-22 neels x = prev_x;
576 3b0f3d61 2020-01-22 neels } else {
577 3b0f3d61 2020-01-22 neels /* The bottom most one.
578 0d27172a 2020-05-06 neels * From position prev_c, step to the left in the Myers
579 0d27172a 2020-05-06 neels * graph: x -= 1.
580 3b0f3d61 2020-01-22 neels */
581 3b0f3d61 2020-01-22 neels int prev_c = c + 1;
582 f71e8098 2020-05-05 neels int prev_x = kd_backward[prev_c];
583 3b0f3d61 2020-01-22 neels x = prev_x - 1;
584 3b0f3d61 2020-01-22 neels }
585 3b0f3d61 2020-01-22 neels
586 0d27172a 2020-05-06 neels /* Slide up any snake that we might find here (sections of
587 0d27172a 2020-05-06 neels * identical lines on both sides). */
588 0d27172a 2020-05-06 neels debug("c=%d x-1=%d Yb-1=%d-1=%d\n", c, x-1, xc_to_y(x, c,
589 0d27172a 2020-05-06 neels delta),
590 0d27172a 2020-05-06 neels xc_to_y(x, c, delta)-1);
591 3b0f3d61 2020-01-22 neels if (x > 0) {
592 0d27172a 2020-05-06 neels debug(" l=");
593 0d27172a 2020-05-06 neels debug_dump_atom(left, right, &left->atoms.head[x-1]);
594 3b0f3d61 2020-01-22 neels }
595 3b0f3d61 2020-01-22 neels if (xc_to_y(x, c, delta) > 0) {
596 0d27172a 2020-05-06 neels debug(" r=");
597 0d27172a 2020-05-06 neels debug_dump_atom(right, left,
598 0d27172a 2020-05-06 neels &right->atoms.head[xc_to_y(x, c, delta)-1]);
599 3b0f3d61 2020-01-22 neels }
600 f71e8098 2020-05-05 neels int x_before_slide = x;
601 b3fb4686 2020-09-20 neels while (x > 0 && xc_to_y(x, c, delta) > 0) {
602 b3fb4686 2020-09-20 neels bool same;
603 b3fb4686 2020-09-20 neels int r = diff_atom_same(&same,
604 b3fb4686 2020-09-20 neels &left->atoms.head[x-1],
605 b3fb4686 2020-09-20 neels &right->atoms.head[
606 b3fb4686 2020-09-20 neels xc_to_y(x, c, delta)-1]);
607 ac2eeeff 2020-09-20 neels if (r)
608 ac2eeeff 2020-09-20 neels return r;
609 b3fb4686 2020-09-20 neels if (!same)
610 b3fb4686 2020-09-20 neels break;
611 50198b5f 2020-05-05 neels x--;
612 b3fb4686 2020-09-20 neels }
613 3b0f3d61 2020-01-22 neels kd_backward[c] = x;
614 c5419a05 2020-05-05 neels if (x_before_slide != x) {
615 c5419a05 2020-05-05 neels debug(" up %d similar lines\n", x_before_slide - x);
616 c5419a05 2020-05-05 neels }
617 3b0f3d61 2020-01-22 neels
618 3b0f3d61 2020-01-22 neels if (DEBUG) {
619 3b0f3d61 2020-01-22 neels int fi;
620 3b0f3d61 2020-01-22 neels for (fi = d; fi >= c; fi--) {
621 0d27172a 2020-05-06 neels debug("kd_backward[%d] = (%d, %d)\n",
622 0d27172a 2020-05-06 neels fi,
623 0d27172a 2020-05-06 neels kd_backward[fi],
624 3b0f3d61 2020-01-22 neels kd_backward[fi] - fi + delta);
625 3b0f3d61 2020-01-22 neels }
626 3b0f3d61 2020-01-22 neels }
627 3b0f3d61 2020-01-22 neels
628 3b0f3d61 2020-01-22 neels if (x < 0 || x > left->atoms.len
629 0d27172a 2020-05-06 neels || xc_to_y(x, c, delta) < 0
630 0d27172a 2020-05-06 neels || xc_to_y(x, c, delta) > right->atoms.len)
631 3b0f3d61 2020-01-22 neels continue;
632 3b0f3d61 2020-01-22 neels
633 0d27172a 2020-05-06 neels /* Figured out a new backwards traversal, see if this has gone
634 0d27172a 2020-05-06 neels * onto or even past a preceding forwards traversal.
635 3b0f3d61 2020-01-22 neels *
636 0d27172a 2020-05-06 neels * If the delta in length is even, then d and backwards_d hit
637 0d27172a 2020-05-06 neels * the same state indexes -- note how this is different from in
638 0d27172a 2020-05-06 neels * the forwards traversal, because now both d are the same:
639 3b0f3d61 2020-01-22 neels *
640 3b0f3d61 2020-01-22 neels * | d= 0 1 2 2 1 0
641 3b0f3d61 2020-01-22 neels * ----+---------------- --------------------
642 3b0f3d61 2020-01-22 neels * k= | c=
643 3b0f3d61 2020-01-22 neels * 4 |
644 3b0f3d61 2020-01-22 neels * |
645 3b0f3d61 2020-01-22 neels * 3 | 3
646 3b0f3d61 2020-01-22 neels * | same
647 3b0f3d61 2020-01-22 neels * 2 | 2,0====5,2 2
648 3b0f3d61 2020-01-22 neels * | / \
649 3b0f3d61 2020-01-22 neels * 1 | 1,0 5,3 1
650 3b0f3d61 2020-01-22 neels * | / / \
651 3b0f3d61 2020-01-22 neels * 0 | -->0,0 3,3====4,3 5,4<-- 0
652 3b0f3d61 2020-01-22 neels * | \ / /
653 3b0f3d61 2020-01-22 neels * -1 | 0,1 4,4 -1
654 3b0f3d61 2020-01-22 neels * | \
655 3b0f3d61 2020-01-22 neels * -2 | 0,2 -2
656 3b0f3d61 2020-01-22 neels * |
657 3b0f3d61 2020-01-22 neels * -3
658 0d27172a 2020-05-06 neels * If the delta is odd, they end up off-by-one, i.e. on
659 0d27172a 2020-05-06 neels * different diagonals.
660 0d27172a 2020-05-06 neels * So in the backward path, we can only match up diagonals when
661 0d27172a 2020-05-06 neels * the delta is even.
662 3b0f3d61 2020-01-22 neels */
663 0d27172a 2020-05-06 neels if ((delta & 1) != 0)
664 0d27172a 2020-05-06 neels continue;
665 0d27172a 2020-05-06 neels /* Forwards was done first, now both d are the same. */
666 0d27172a 2020-05-06 neels int forwards_d = d;
667 3b0f3d61 2020-01-22 neels
668 0d27172a 2020-05-06 neels /* As soon as the lengths are not the same, the
669 0d27172a 2020-05-06 neels * backwards traversal starts on a different diagonal,
670 0d27172a 2020-05-06 neels * and c = k shifted by the difference in length.
671 0d27172a 2020-05-06 neels */
672 0d27172a 2020-05-06 neels int k = c_to_k(c, delta);
673 3b0f3d61 2020-01-22 neels
674 0d27172a 2020-05-06 neels /* When the file sizes are very different, the traversal trees
675 0d27172a 2020-05-06 neels * start on far distant diagonals.
676 0d27172a 2020-05-06 neels * They don't necessarily meet straight on. See whether this
677 0d27172a 2020-05-06 neels * backward value is also on a valid diagonal in kd_forward[],
678 0d27172a 2020-05-06 neels * and match them if so. */
679 0d27172a 2020-05-06 neels if (k >= -forwards_d && k <= forwards_d) {
680 0d27172a 2020-05-06 neels /* Current c is on a diagonal that exists in
681 0d27172a 2020-05-06 neels * kd_forward[]. If the two x positions have met or
682 0d27172a 2020-05-06 neels * passed (backward walked onto or past forward), then
683 0d27172a 2020-05-06 neels * we've found a midpoint / a mid-box.
684 0d27172a 2020-05-06 neels *
685 0d27172a 2020-05-06 neels * When forwards and backwards traversals meet, the
686 0d27172a 2020-05-06 neels * endpoints of the mid-snake are not the two points in
687 0d27172a 2020-05-06 neels * kd_forward and kd_backward, but rather the section
688 0d27172a 2020-05-06 neels * that was slid (if any) of the current
689 0d27172a 2020-05-06 neels * forward/backward traversal only.
690 0d27172a 2020-05-06 neels *
691 0d27172a 2020-05-06 neels * For example:
692 0d27172a 2020-05-06 neels *
693 0d27172a 2020-05-06 neels * o-o-o
694 0d27172a 2020-05-06 neels * | |
695 0d27172a 2020-05-06 neels * o A
696 0d27172a 2020-05-06 neels * | \
697 0d27172a 2020-05-06 neels * o o
698 0d27172a 2020-05-06 neels * \
699 0d27172a 2020-05-06 neels * M
700 0d27172a 2020-05-06 neels * |\
701 0d27172a 2020-05-06 neels * o o-o-o
702 0d27172a 2020-05-06 neels * | | |
703 0d27172a 2020-05-06 neels * o o X
704 0d27172a 2020-05-06 neels * \
705 0d27172a 2020-05-06 neels * o
706 0d27172a 2020-05-06 neels * \
707 0d27172a 2020-05-06 neels * o
708 0d27172a 2020-05-06 neels * \
709 0d27172a 2020-05-06 neels * o
710 0d27172a 2020-05-06 neels *
711 0d27172a 2020-05-06 neels * The backward traversal reached M from the bottom and
712 0d27172a 2020-05-06 neels * slid upwards. The forward traversal already reached
713 0d27172a 2020-05-06 neels * X, which is not a straight line from M anymore, so
714 0d27172a 2020-05-06 neels * picking a mid-snake from M to X would yield a
715 0d27172a 2020-05-06 neels * mistake.
716 0d27172a 2020-05-06 neels *
717 0d27172a 2020-05-06 neels * The correct mid-snake is between M and A. M is where
718 0d27172a 2020-05-06 neels * the backward traversal hit the diagonal that the
719 0d27172a 2020-05-06 neels * forwards traversal has already passed, and A is what
720 0d27172a 2020-05-06 neels * it reaches when sliding up identical lines.
721 0d27172a 2020-05-06 neels */
722 f71e8098 2020-05-05 neels
723 0d27172a 2020-05-06 neels int forward_x = kd_forward[k];
724 0d27172a 2020-05-06 neels debug("Compare: k=%d c=%d is (%d,%d) >= (%d,%d)?\n",
725 0d27172a 2020-05-06 neels k, c, forward_x, xk_to_y(forward_x, k),
726 0d27172a 2020-05-06 neels x, xc_to_y(x, c, delta));
727 0d27172a 2020-05-06 neels if (forward_x >= x) {
728 0d27172a 2020-05-06 neels *meeting_snake = (struct diff_box){
729 0d27172a 2020-05-06 neels .left_start = x,
730 0d27172a 2020-05-06 neels .left_end = x_before_slide,
731 0d27172a 2020-05-06 neels .right_start = xc_to_y(x, c, delta),
732 0d27172a 2020-05-06 neels .right_end = xk_to_y(x_before_slide, k),
733 0d27172a 2020-05-06 neels };
734 0d27172a 2020-05-06 neels debug("HIT x=%u,%u - y=%u,%u\n",
735 0d27172a 2020-05-06 neels meeting_snake->left_start,
736 0d27172a 2020-05-06 neels meeting_snake->right_start,
737 0d27172a 2020-05-06 neels meeting_snake->left_end,
738 0d27172a 2020-05-06 neels meeting_snake->right_end);
739 0d27172a 2020-05-06 neels debug_dump_myers_graph(left, right, NULL,
740 0d27172a 2020-05-06 neels kd_forward, d,
741 0d27172a 2020-05-06 neels kd_backward, d);
742 ac2eeeff 2020-09-20 neels *found_midpoint = true;
743 ac2eeeff 2020-09-20 neels return 0;
744 3b0f3d61 2020-01-22 neels }
745 3b0f3d61 2020-01-22 neels }
746 3b0f3d61 2020-01-22 neels }
747 0d27172a 2020-05-06 neels debug_dump_myers_graph(left, right, NULL, kd_forward, d, kd_backward,
748 0d27172a 2020-05-06 neels d);
749 ac2eeeff 2020-09-20 neels return 0;
750 cd25827e 2020-09-20 neels }
751 cd25827e 2020-09-20 neels
752 cd25827e 2020-09-20 neels /* Integer square root approximation */
753 cd25827e 2020-09-20 neels static int
754 cd25827e 2020-09-20 neels shift_sqrt(int val)
755 cd25827e 2020-09-20 neels {
756 cd25827e 2020-09-20 neels int i;
757 cd25827e 2020-09-20 neels for (i = 1; val > 0; val >>= 2)
758 cd25827e 2020-09-20 neels i <<= 1;
759 cd25827e 2020-09-20 neels return i;
760 3b0f3d61 2020-01-22 neels }
761 3b0f3d61 2020-01-22 neels
762 0d27172a 2020-05-06 neels /* Myers "Divide et Impera": tracing forwards from the start and backwards from
763 0d27172a 2020-05-06 neels * the end to find a midpoint that divides the problem into smaller chunks.
764 0d27172a 2020-05-06 neels * Requires only linear amounts of memory. */
765 3e6cba3a 2020-08-13 stsp int
766 0d27172a 2020-05-06 neels diff_algo_myers_divide(const struct diff_algo_config *algo_config,
767 0d27172a 2020-05-06 neels struct diff_state *state)
768 3b0f3d61 2020-01-22 neels {
769 3e6cba3a 2020-08-13 stsp int rc = ENOMEM;
770 3b0f3d61 2020-01-22 neels struct diff_data *left = &state->left;
771 3b0f3d61 2020-01-22 neels struct diff_data *right = &state->right;
772 3b0f3d61 2020-01-22 neels
773 3b0f3d61 2020-01-22 neels debug("\n** %s\n", __func__);
774 3b0f3d61 2020-01-22 neels debug("left:\n");
775 3b0f3d61 2020-01-22 neels debug_dump(left);
776 3b0f3d61 2020-01-22 neels debug("right:\n");
777 3b0f3d61 2020-01-22 neels debug_dump(right);
778 50198b5f 2020-05-05 neels debug_dump_myers_graph(left, right, NULL, NULL, 0, NULL, 0);
779 3b0f3d61 2020-01-22 neels
780 0d27172a 2020-05-06 neels /* Allocate two columns of a Myers graph, one for the forward and one
781 0d27172a 2020-05-06 neels * for the backward traversal. */
782 3b0f3d61 2020-01-22 neels unsigned int max = left->atoms.len + right->atoms.len;
783 3b0f3d61 2020-01-22 neels size_t kd_len = max + 1;
784 3b0f3d61 2020-01-22 neels size_t kd_buf_size = kd_len << 1;
785 3b0f3d61 2020-01-22 neels int *kd_buf = reallocarray(NULL, kd_buf_size, sizeof(int));
786 3b0f3d61 2020-01-22 neels if (!kd_buf)
787 3e6cba3a 2020-08-13 stsp return ENOMEM;
788 3b0f3d61 2020-01-22 neels int i;
789 3b0f3d61 2020-01-22 neels for (i = 0; i < kd_buf_size; i++)
790 3b0f3d61 2020-01-22 neels kd_buf[i] = -1;
791 3b0f3d61 2020-01-22 neels int *kd_forward = kd_buf;
792 3b0f3d61 2020-01-22 neels int *kd_backward = kd_buf + kd_len;
793 cd25827e 2020-09-20 neels int max_effort = shift_sqrt(max/2);
794 3b0f3d61 2020-01-22 neels
795 0d27172a 2020-05-06 neels /* The 'k' axis in Myers spans positive and negative indexes, so point
796 0d27172a 2020-05-06 neels * the kd to the middle.
797 3b0f3d61 2020-01-22 neels * It is then possible to index from -max/2 .. max/2. */
798 3b0f3d61 2020-01-22 neels kd_forward += max/2;
799 3b0f3d61 2020-01-22 neels kd_backward += max/2;
800 3b0f3d61 2020-01-22 neels
801 3b0f3d61 2020-01-22 neels int d;
802 3b0f3d61 2020-01-22 neels struct diff_box mid_snake = {};
803 a45330b1 2020-05-05 neels bool found_midpoint = false;
804 3b0f3d61 2020-01-22 neels for (d = 0; d <= (max/2); d++) {
805 ac2eeeff 2020-09-20 neels int r;
806 3b0f3d61 2020-01-22 neels debug("-- d=%d\n", d);
807 ac2eeeff 2020-09-20 neels r = diff_divide_myers_forward(&found_midpoint, left, right,
808 ac2eeeff 2020-09-20 neels kd_forward, kd_backward, d,
809 ac2eeeff 2020-09-20 neels &mid_snake);
810 ac2eeeff 2020-09-20 neels if (r)
811 ac2eeeff 2020-09-20 neels return r;
812 a45330b1 2020-05-05 neels if (found_midpoint)
813 3b0f3d61 2020-01-22 neels break;
814 ac2eeeff 2020-09-20 neels r = diff_divide_myers_backward(&found_midpoint, left, right,
815 ac2eeeff 2020-09-20 neels kd_forward, kd_backward, d,
816 ac2eeeff 2020-09-20 neels &mid_snake);
817 ac2eeeff 2020-09-20 neels if (r)
818 ac2eeeff 2020-09-20 neels return r;
819 a45330b1 2020-05-05 neels if (found_midpoint)
820 cd25827e 2020-09-20 neels break;
821 cd25827e 2020-09-20 neels
822 cd25827e 2020-09-20 neels /* Limit the effort spent looking for a mid snake. If files have
823 cd25827e 2020-09-20 neels * very few lines in common, the effort spent to find nice mid
824 cd25827e 2020-09-20 neels * snakes is just not worth it, the diff result will still be
825 cd25827e 2020-09-20 neels * essentially minus everything on the left, plus everything on
826 cd25827e 2020-09-20 neels * the right, with a few useless matches here and there. */
827 cd25827e 2020-09-20 neels if (d > max_effort) {
828 cd25827e 2020-09-20 neels /* pick the furthest reaching point from
829 cd25827e 2020-09-20 neels * kd_forward and kd_backward, and use that as a
830 cd25827e 2020-09-20 neels * midpoint, to not step into another diff algo
831 cd25827e 2020-09-20 neels * recursion with unchanged box. */
832 cd25827e 2020-09-20 neels int delta = (int)right->atoms.len - (int)left->atoms.len;
833 cd25827e 2020-09-20 neels int x;
834 cd25827e 2020-09-20 neels int y;
835 cd25827e 2020-09-20 neels int i;
836 cd25827e 2020-09-20 neels int best_forward_i = 0;
837 cd25827e 2020-09-20 neels int best_forward_distance = 0;
838 cd25827e 2020-09-20 neels int best_backward_i = 0;
839 cd25827e 2020-09-20 neels int best_backward_distance = 0;
840 cd25827e 2020-09-20 neels int distance;
841 cd25827e 2020-09-20 neels int best_forward_x;
842 cd25827e 2020-09-20 neels int best_forward_y;
843 cd25827e 2020-09-20 neels int best_backward_x;
844 cd25827e 2020-09-20 neels int best_backward_y;
845 cd25827e 2020-09-20 neels
846 cd25827e 2020-09-20 neels debug("~~~ d = %d > max_effort = %d\n", d, max_effort);
847 cd25827e 2020-09-20 neels
848 cd25827e 2020-09-20 neels for (i = d; i >= -d; i -= 2) {
849 cd25827e 2020-09-20 neels if (i >= -(int)right->atoms.len && i <= (int)left->atoms.len) {
850 cd25827e 2020-09-20 neels x = kd_forward[i];
851 cd25827e 2020-09-20 neels y = xk_to_y(x, i);
852 cd25827e 2020-09-20 neels distance = x + y;
853 cd25827e 2020-09-20 neels if (distance > best_forward_distance) {
854 cd25827e 2020-09-20 neels best_forward_distance = distance;
855 cd25827e 2020-09-20 neels best_forward_i = i;
856 cd25827e 2020-09-20 neels }
857 cd25827e 2020-09-20 neels }
858 cd25827e 2020-09-20 neels
859 cd25827e 2020-09-20 neels if (i >= -(int)left->atoms.len && i <= (int)right->atoms.len) {
860 cd25827e 2020-09-20 neels x = kd_backward[i];
861 cd25827e 2020-09-20 neels y = xc_to_y(x, i, delta);
862 cd25827e 2020-09-20 neels distance = (right->atoms.len - x)
863 cd25827e 2020-09-20 neels + (left->atoms.len - y);
864 cd25827e 2020-09-20 neels if (distance >= best_backward_distance) {
865 cd25827e 2020-09-20 neels best_backward_distance = distance;
866 cd25827e 2020-09-20 neels best_backward_i = i;
867 cd25827e 2020-09-20 neels }
868 cd25827e 2020-09-20 neels }
869 cd25827e 2020-09-20 neels }
870 cd25827e 2020-09-20 neels
871 cd25827e 2020-09-20 neels /* The myers-divide didn't meet in the middle. We just
872 cd25827e 2020-09-20 neels * figured out the places where the forward path
873 cd25827e 2020-09-20 neels * advanced the most, and the backward path advanced the
874 cd25827e 2020-09-20 neels * most. Just divide at whichever one of those two is better.
875 cd25827e 2020-09-20 neels *
876 cd25827e 2020-09-20 neels * o-o
877 cd25827e 2020-09-20 neels * |
878 cd25827e 2020-09-20 neels * o
879 cd25827e 2020-09-20 neels * \
880 cd25827e 2020-09-20 neels * o
881 cd25827e 2020-09-20 neels * \
882 cd25827e 2020-09-20 neels * F <-- cut here
883 cd25827e 2020-09-20 neels *
884 cd25827e 2020-09-20 neels *
885 cd25827e 2020-09-20 neels *
886 cd25827e 2020-09-20 neels * or here --> B
887 cd25827e 2020-09-20 neels * \
888 cd25827e 2020-09-20 neels * o
889 cd25827e 2020-09-20 neels * \
890 cd25827e 2020-09-20 neels * o
891 cd25827e 2020-09-20 neels * |
892 cd25827e 2020-09-20 neels * o-o
893 cd25827e 2020-09-20 neels */
894 cd25827e 2020-09-20 neels best_forward_x = kd_forward[best_forward_i];
895 cd25827e 2020-09-20 neels best_forward_y = xk_to_y(best_forward_x, best_forward_i);
896 cd25827e 2020-09-20 neels best_backward_x = kd_backward[best_backward_i];
897 cd25827e 2020-09-20 neels best_backward_y = xc_to_y(x, best_backward_i, delta);
898 cd25827e 2020-09-20 neels
899 cd25827e 2020-09-20 neels if (best_forward_distance >= best_backward_distance) {
900 cd25827e 2020-09-20 neels x = best_forward_x;
901 cd25827e 2020-09-20 neels y = best_forward_y;
902 cd25827e 2020-09-20 neels } else {
903 cd25827e 2020-09-20 neels x = best_backward_x;
904 cd25827e 2020-09-20 neels y = best_backward_y;
905 cd25827e 2020-09-20 neels }
906 cd25827e 2020-09-20 neels
907 cd25827e 2020-09-20 neels debug("max_effort cut at x=%d y=%d\n", x, y);
908 cd25827e 2020-09-20 neels if (x < 0 || y < 0
909 cd25827e 2020-09-20 neels || x > left->atoms.len || y > right->atoms.len)
910 cd25827e 2020-09-20 neels break;
911 cd25827e 2020-09-20 neels
912 cd25827e 2020-09-20 neels found_midpoint = true;
913 cd25827e 2020-09-20 neels mid_snake = (struct diff_box){
914 cd25827e 2020-09-20 neels .left_start = x,
915 cd25827e 2020-09-20 neels .left_end = x,
916 cd25827e 2020-09-20 neels .right_start = y,
917 cd25827e 2020-09-20 neels .right_end = y,
918 cd25827e 2020-09-20 neels };
919 3b0f3d61 2020-01-22 neels break;
920 cd25827e 2020-09-20 neels }
921 3b0f3d61 2020-01-22 neels }
922 3b0f3d61 2020-01-22 neels
923 a45330b1 2020-05-05 neels if (!found_midpoint) {
924 0d27172a 2020-05-06 neels /* Divide and conquer failed to find a meeting point. Use the
925 0d27172a 2020-05-06 neels * fallback_algo defined in the algo_config (leave this to the
926 0d27172a 2020-05-06 neels * caller). This is just paranoia/sanity, we normally should
927 0d27172a 2020-05-06 neels * always find a midpoint.
928 3b0f3d61 2020-01-22 neels */
929 3b0f3d61 2020-01-22 neels debug(" no midpoint \n");
930 3b0f3d61 2020-01-22 neels rc = DIFF_RC_USE_DIFF_ALGO_FALLBACK;
931 3b0f3d61 2020-01-22 neels goto return_rc;
932 3b0f3d61 2020-01-22 neels } else {
933 3b0f3d61 2020-01-22 neels debug(" mid snake L: %u to %u of %u R: %u to %u of %u\n",
934 3b0f3d61 2020-01-22 neels mid_snake.left_start, mid_snake.left_end, left->atoms.len,
935 0d27172a 2020-05-06 neels mid_snake.right_start, mid_snake.right_end,
936 0d27172a 2020-05-06 neels right->atoms.len);
937 3b0f3d61 2020-01-22 neels
938 3b0f3d61 2020-01-22 neels /* Section before the mid-snake. */
939 3b0f3d61 2020-01-22 neels debug("Section before the mid-snake\n");
940 3b0f3d61 2020-01-22 neels
941 3b0f3d61 2020-01-22 neels struct diff_atom *left_atom = &left->atoms.head[0];
942 3b0f3d61 2020-01-22 neels unsigned int left_section_len = mid_snake.left_start;
943 3b0f3d61 2020-01-22 neels struct diff_atom *right_atom = &right->atoms.head[0];
944 3b0f3d61 2020-01-22 neels unsigned int right_section_len = mid_snake.right_start;
945 3b0f3d61 2020-01-22 neels
946 3b0f3d61 2020-01-22 neels if (left_section_len && right_section_len) {
947 0d27172a 2020-05-06 neels /* Record an unsolved chunk, the caller will apply
948 0d27172a 2020-05-06 neels * inner_algo() on this chunk. */
949 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, false,
950 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
951 0d27172a 2020-05-06 neels right_atom,
952 0d27172a 2020-05-06 neels right_section_len))
953 3b0f3d61 2020-01-22 neels goto return_rc;
954 3b0f3d61 2020-01-22 neels } else if (left_section_len && !right_section_len) {
955 0d27172a 2020-05-06 neels /* Only left atoms and none on the right, they form a
956 0d27172a 2020-05-06 neels * "minus" chunk, then. */
957 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
958 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
959 3b0f3d61 2020-01-22 neels right_atom, 0))
960 3b0f3d61 2020-01-22 neels goto return_rc;
961 3b0f3d61 2020-01-22 neels } else if (!left_section_len && right_section_len) {
962 0d27172a 2020-05-06 neels /* No left atoms, only atoms on the right, they form a
963 0d27172a 2020-05-06 neels * "plus" chunk, then. */
964 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
965 3b0f3d61 2020-01-22 neels left_atom, 0,
966 0d27172a 2020-05-06 neels right_atom,
967 0d27172a 2020-05-06 neels right_section_len))
968 3b0f3d61 2020-01-22 neels goto return_rc;
969 3b0f3d61 2020-01-22 neels }
970 0d27172a 2020-05-06 neels /* else: left_section_len == 0 and right_section_len == 0, i.e.
971 0d27172a 2020-05-06 neels * nothing before the mid-snake. */
972 3b0f3d61 2020-01-22 neels
973 cd25827e 2020-09-20 neels if (mid_snake.left_end > mid_snake.left_start) {
974 0d27172a 2020-05-06 neels /* The midpoint is a "snake", i.e. on a section of
975 0d27172a 2020-05-06 neels * identical data on both sides: that section
976 0d27172a 2020-05-06 neels * immediately becomes a solved diff chunk. */
977 a45330b1 2020-05-05 neels debug("the mid-snake\n");
978 a45330b1 2020-05-05 neels if (!diff_state_add_chunk(state, true,
979 0d27172a 2020-05-06 neels &left->atoms.head[mid_snake.left_start],
980 0d27172a 2020-05-06 neels mid_snake.left_end - mid_snake.left_start,
981 0d27172a 2020-05-06 neels &right->atoms.head[mid_snake.right_start],
982 0d27172a 2020-05-06 neels mid_snake.right_end - mid_snake.right_start))
983 a45330b1 2020-05-05 neels goto return_rc;
984 a45330b1 2020-05-05 neels }
985 3b0f3d61 2020-01-22 neels
986 3b0f3d61 2020-01-22 neels /* Section after the mid-snake. */
987 3b0f3d61 2020-01-22 neels debug("Section after the mid-snake\n");
988 0d27172a 2020-05-06 neels debug(" left_end %u right_end %u\n",
989 0d27172a 2020-05-06 neels mid_snake.left_end, mid_snake.right_end);
990 0d27172a 2020-05-06 neels debug(" left_count %u right_count %u\n",
991 0d27172a 2020-05-06 neels left->atoms.len, right->atoms.len);
992 3b0f3d61 2020-01-22 neels left_atom = &left->atoms.head[mid_snake.left_end];
993 3b0f3d61 2020-01-22 neels left_section_len = left->atoms.len - mid_snake.left_end;
994 3b0f3d61 2020-01-22 neels right_atom = &right->atoms.head[mid_snake.right_end];
995 3b0f3d61 2020-01-22 neels right_section_len = right->atoms.len - mid_snake.right_end;
996 3b0f3d61 2020-01-22 neels
997 3b0f3d61 2020-01-22 neels if (left_section_len && right_section_len) {
998 0d27172a 2020-05-06 neels /* Record an unsolved chunk, the caller will apply
999 0d27172a 2020-05-06 neels * inner_algo() on this chunk. */
1000 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, false,
1001 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
1002 0d27172a 2020-05-06 neels right_atom,
1003 0d27172a 2020-05-06 neels right_section_len))
1004 3b0f3d61 2020-01-22 neels goto return_rc;
1005 3b0f3d61 2020-01-22 neels } else if (left_section_len && !right_section_len) {
1006 0d27172a 2020-05-06 neels /* Only left atoms and none on the right, they form a
1007 0d27172a 2020-05-06 neels * "minus" chunk, then. */
1008 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
1009 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
1010 3b0f3d61 2020-01-22 neels right_atom, 0))
1011 3b0f3d61 2020-01-22 neels goto return_rc;
1012 3b0f3d61 2020-01-22 neels } else if (!left_section_len && right_section_len) {
1013 0d27172a 2020-05-06 neels /* No left atoms, only atoms on the right, they form a
1014 0d27172a 2020-05-06 neels * "plus" chunk, then. */
1015 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
1016 3b0f3d61 2020-01-22 neels left_atom, 0,
1017 0d27172a 2020-05-06 neels right_atom,
1018 0d27172a 2020-05-06 neels right_section_len))
1019 3b0f3d61 2020-01-22 neels goto return_rc;
1020 3b0f3d61 2020-01-22 neels }
1021 0d27172a 2020-05-06 neels /* else: left_section_len == 0 and right_section_len == 0, i.e.
1022 0d27172a 2020-05-06 neels * nothing after the mid-snake. */
1023 3b0f3d61 2020-01-22 neels }
1024 3b0f3d61 2020-01-22 neels
1025 3b0f3d61 2020-01-22 neels rc = DIFF_RC_OK;
1026 3b0f3d61 2020-01-22 neels
1027 3b0f3d61 2020-01-22 neels return_rc:
1028 3b0f3d61 2020-01-22 neels free(kd_buf);
1029 3b0f3d61 2020-01-22 neels debug("** END %s\n", __func__);
1030 3b0f3d61 2020-01-22 neels return rc;
1031 3b0f3d61 2020-01-22 neels }
1032 3b0f3d61 2020-01-22 neels
1033 0d27172a 2020-05-06 neels /* Myers Diff tracing from the start all the way through to the end, requiring
1034 0d27172a 2020-05-06 neels * quadratic amounts of memory. This can fail if the required space surpasses
1035 0d27172a 2020-05-06 neels * algo_config->permitted_state_size. */
1036 3e6cba3a 2020-08-13 stsp int
1037 0d27172a 2020-05-06 neels diff_algo_myers(const struct diff_algo_config *algo_config,
1038 0d27172a 2020-05-06 neels struct diff_state *state)
1039 3b0f3d61 2020-01-22 neels {
1040 0d27172a 2020-05-06 neels /* do a diff_divide_myers_forward() without a _backward(), so that it
1041 0d27172a 2020-05-06 neels * walks forward across the entire files to reach the end. Keep each
1042 0d27172a 2020-05-06 neels * run's state, and do a final backtrace. */
1043 3e6cba3a 2020-08-13 stsp int rc = ENOMEM;
1044 3b0f3d61 2020-01-22 neels struct diff_data *left = &state->left;
1045 3b0f3d61 2020-01-22 neels struct diff_data *right = &state->right;
1046 3b0f3d61 2020-01-22 neels
1047 3b0f3d61 2020-01-22 neels debug("\n** %s\n", __func__);
1048 3b0f3d61 2020-01-22 neels debug("left:\n");
1049 3b0f3d61 2020-01-22 neels debug_dump(left);
1050 3b0f3d61 2020-01-22 neels debug("right:\n");
1051 3b0f3d61 2020-01-22 neels debug_dump(right);
1052 50198b5f 2020-05-05 neels debug_dump_myers_graph(left, right, NULL, NULL, 0, NULL, 0);
1053 3b0f3d61 2020-01-22 neels
1054 0d27172a 2020-05-06 neels /* Allocate two columns of a Myers graph, one for the forward and one
1055 0d27172a 2020-05-06 neels * for the backward traversal. */
1056 3b0f3d61 2020-01-22 neels unsigned int max = left->atoms.len + right->atoms.len;
1057 3b0f3d61 2020-01-22 neels size_t kd_len = max + 1 + max;
1058 3b0f3d61 2020-01-22 neels size_t kd_buf_size = kd_len * kd_len;
1059 50198b5f 2020-05-05 neels size_t kd_state_size = kd_buf_size * sizeof(int);
1060 3b0f3d61 2020-01-22 neels debug("state size: %zu\n", kd_buf_size);
1061 3b0f3d61 2020-01-22 neels if (kd_buf_size < kd_len /* overflow? */
1062 50198b5f 2020-05-05 neels || kd_state_size > algo_config->permitted_state_size) {
1063 3b0f3d61 2020-01-22 neels debug("state size %zu > permitted_state_size %zu, use fallback_algo\n",
1064 50198b5f 2020-05-05 neels kd_state_size, algo_config->permitted_state_size);
1065 3b0f3d61 2020-01-22 neels return DIFF_RC_USE_DIFF_ALGO_FALLBACK;
1066 3b0f3d61 2020-01-22 neels }
1067 3b0f3d61 2020-01-22 neels
1068 3b0f3d61 2020-01-22 neels int *kd_buf = reallocarray(NULL, kd_buf_size, sizeof(int));
1069 3b0f3d61 2020-01-22 neels if (!kd_buf)
1070 3e6cba3a 2020-08-13 stsp return ENOMEM;
1071 3b0f3d61 2020-01-22 neels int i;
1072 3b0f3d61 2020-01-22 neels for (i = 0; i < kd_buf_size; i++)
1073 3b0f3d61 2020-01-22 neels kd_buf[i] = -1;
1074 3b0f3d61 2020-01-22 neels
1075 0d27172a 2020-05-06 neels /* The 'k' axis in Myers spans positive and negative indexes, so point
1076 0d27172a 2020-05-06 neels * the kd to the middle.
1077 3b0f3d61 2020-01-22 neels * It is then possible to index from -max .. max. */
1078 3b0f3d61 2020-01-22 neels int *kd_origin = kd_buf + max;
1079 3b0f3d61 2020-01-22 neels int *kd_column = kd_origin;
1080 3b0f3d61 2020-01-22 neels
1081 3b0f3d61 2020-01-22 neels int d;
1082 3b0f3d61 2020-01-22 neels int backtrack_d = -1;
1083 3b0f3d61 2020-01-22 neels int backtrack_k = 0;
1084 3b0f3d61 2020-01-22 neels int k;
1085 3b0f3d61 2020-01-22 neels int x, y;
1086 3b0f3d61 2020-01-22 neels for (d = 0; d <= max; d++, kd_column += kd_len) {
1087 3b0f3d61 2020-01-22 neels debug("-- d=%d\n", d);
1088 3b0f3d61 2020-01-22 neels
1089 3b0f3d61 2020-01-22 neels debug("-- %s d=%d\n", __func__, d);
1090 3b0f3d61 2020-01-22 neels
1091 3b0f3d61 2020-01-22 neels for (k = d; k >= -d; k -= 2) {
1092 0d27172a 2020-05-06 neels if (k < -(int)right->atoms.len
1093 0d27172a 2020-05-06 neels || k > (int)left->atoms.len) {
1094 0d27172a 2020-05-06 neels /* This diagonal is completely outside of the
1095 0d27172a 2020-05-06 neels * Myers graph, don't calculate it. */
1096 3b0f3d61 2020-01-22 neels if (k < -(int)right->atoms.len)
1097 0d27172a 2020-05-06 neels debug(" %d k <"
1098 0d27172a 2020-05-06 neels " -(int)right->atoms.len %d\n",
1099 0d27172a 2020-05-06 neels k, -(int)right->atoms.len);
1100 3b0f3d61 2020-01-22 neels else
1101 0d27172a 2020-05-06 neels debug(" %d k > left->atoms.len %d\n", k,
1102 0d27172a 2020-05-06 neels left->atoms.len);
1103 3b0f3d61 2020-01-22 neels if (k < 0) {
1104 0d27172a 2020-05-06 neels /* We are traversing negatively, and
1105 0d27172a 2020-05-06 neels * already below the entire graph,
1106 0d27172a 2020-05-06 neels * nothing will come of this. */
1107 3b0f3d61 2020-01-22 neels debug(" break");
1108 3b0f3d61 2020-01-22 neels break;
1109 3b0f3d61 2020-01-22 neels }
1110 3b0f3d61 2020-01-22 neels debug(" continue");
1111 3b0f3d61 2020-01-22 neels continue;
1112 3b0f3d61 2020-01-22 neels }
1113 3b0f3d61 2020-01-22 neels
1114 3b0f3d61 2020-01-22 neels debug("- k = %d\n", k);
1115 3b0f3d61 2020-01-22 neels if (d == 0) {
1116 0d27172a 2020-05-06 neels /* This is the initializing step. There is no
1117 0d27172a 2020-05-06 neels * prev_k yet, get the initial x from the top
1118 0d27172a 2020-05-06 neels * left of the Myers graph. */
1119 3b0f3d61 2020-01-22 neels x = 0;
1120 3b0f3d61 2020-01-22 neels } else {
1121 3b0f3d61 2020-01-22 neels int *kd_prev_column = kd_column - kd_len;
1122 3b0f3d61 2020-01-22 neels
1123 0d27172a 2020-05-06 neels /* Favoring "-" lines first means favoring
1124 0d27172a 2020-05-06 neels * moving rightwards in the Myers graph.
1125 0d27172a 2020-05-06 neels * For this, all k should derive from k - 1,
1126 0d27172a 2020-05-06 neels * only the bottom most k derive from k + 1:
1127 3b0f3d61 2020-01-22 neels *
1128 3b0f3d61 2020-01-22 neels * | d= 0 1 2
1129 3b0f3d61 2020-01-22 neels * ----+----------------
1130 3b0f3d61 2020-01-22 neels * k= |
1131 0d27172a 2020-05-06 neels * 2 | 2,0 <-- from
1132 0d27172a 2020-05-06 neels * | / prev_k = 2 - 1 = 1
1133 3b0f3d61 2020-01-22 neels * 1 | 1,0
1134 3b0f3d61 2020-01-22 neels * | /
1135 3b0f3d61 2020-01-22 neels * 0 | -->0,0 3,3
1136 3b0f3d61 2020-01-22 neels * | \\ /
1137 0d27172a 2020-05-06 neels * -1 | 0,1 <-- bottom most for d=1
1138 0d27172a 2020-05-06 neels * | \\ from prev_k = -1+1 = 0
1139 0d27172a 2020-05-06 neels * -2 | 0,2 <-- bottom most for
1140 0d27172a 2020-05-06 neels * d=2 from
1141 0d27172a 2020-05-06 neels * prev_k = -2+1 = -1
1142 3b0f3d61 2020-01-22 neels *
1143 0d27172a 2020-05-06 neels * Except when a k + 1 from a previous run
1144 0d27172a 2020-05-06 neels * already means a further advancement in the
1145 0d27172a 2020-05-06 neels * graph.
1146 0d27172a 2020-05-06 neels * If k == d, there is no k + 1 and k - 1 is the
1147 0d27172a 2020-05-06 neels * only option.
1148 0d27172a 2020-05-06 neels * If k < d, use k + 1 in case that yields a
1149 0d27172a 2020-05-06 neels * larger x. Also use k + 1 if k - 1 is outside
1150 0d27172a 2020-05-06 neels * the graph.
1151 3b0f3d61 2020-01-22 neels */
1152 0d27172a 2020-05-06 neels if (k > -d
1153 0d27172a 2020-05-06 neels && (k == d
1154 0d27172a 2020-05-06 neels || (k - 1 >= -(int)right->atoms.len
1155 0d27172a 2020-05-06 neels && kd_prev_column[k - 1]
1156 0d27172a 2020-05-06 neels >= kd_prev_column[k + 1]))) {
1157 3b0f3d61 2020-01-22 neels /* Advance from k - 1.
1158 0d27172a 2020-05-06 neels * From position prev_k, step to the
1159 0d27172a 2020-05-06 neels * right in the Myers graph: x += 1.
1160 3b0f3d61 2020-01-22 neels */
1161 3b0f3d61 2020-01-22 neels int prev_k = k - 1;
1162 3b0f3d61 2020-01-22 neels int prev_x = kd_prev_column[prev_k];
1163 3b0f3d61 2020-01-22 neels x = prev_x + 1;
1164 3b0f3d61 2020-01-22 neels } else {
1165 3b0f3d61 2020-01-22 neels /* The bottom most one.
1166 0d27172a 2020-05-06 neels * From position prev_k, step to the
1167 0d27172a 2020-05-06 neels * bottom in the Myers graph: y += 1.
1168 0d27172a 2020-05-06 neels * Incrementing y is achieved by
1169 0d27172a 2020-05-06 neels * decrementing k while keeping the same
1170 0d27172a 2020-05-06 neels * x. (since we're deriving y from y =
1171 0d27172a 2020-05-06 neels * x - k).
1172 3b0f3d61 2020-01-22 neels */
1173 3b0f3d61 2020-01-22 neels int prev_k = k + 1;
1174 3b0f3d61 2020-01-22 neels int prev_x = kd_prev_column[prev_k];
1175 3b0f3d61 2020-01-22 neels x = prev_x;
1176 3b0f3d61 2020-01-22 neels }
1177 3b0f3d61 2020-01-22 neels }
1178 3b0f3d61 2020-01-22 neels
1179 3b0f3d61 2020-01-22 neels /* Slide down any snake that we might find here. */
1180 0d27172a 2020-05-06 neels while (x < left->atoms.len
1181 b3fb4686 2020-09-20 neels && xk_to_y(x, k) < right->atoms.len) {
1182 b3fb4686 2020-09-20 neels bool same;
1183 b3fb4686 2020-09-20 neels int r = diff_atom_same(&same,
1184 b3fb4686 2020-09-20 neels &left->atoms.head[x],
1185 b3fb4686 2020-09-20 neels &right->atoms.head[
1186 b3fb4686 2020-09-20 neels xk_to_y(x, k)]);
1187 b3fb4686 2020-09-20 neels if (r)
1188 b3fb4686 2020-09-20 neels return r;
1189 b3fb4686 2020-09-20 neels if (!same)
1190 b3fb4686 2020-09-20 neels break;
1191 b3fb4686 2020-09-20 neels x++;
1192 b3fb4686 2020-09-20 neels }
1193 3b0f3d61 2020-01-22 neels kd_column[k] = x;
1194 3b0f3d61 2020-01-22 neels
1195 3b0f3d61 2020-01-22 neels if (DEBUG) {
1196 3b0f3d61 2020-01-22 neels int fi;
1197 3b0f3d61 2020-01-22 neels for (fi = d; fi >= k; fi-=2) {
1198 0d27172a 2020-05-06 neels debug("kd_column[%d] = (%d, %d)\n", fi,
1199 0d27172a 2020-05-06 neels kd_column[fi],
1200 0d27172a 2020-05-06 neels kd_column[fi] - fi);
1201 3b0f3d61 2020-01-22 neels }
1202 3b0f3d61 2020-01-22 neels }
1203 3b0f3d61 2020-01-22 neels
1204 0d27172a 2020-05-06 neels if (x == left->atoms.len
1205 0d27172a 2020-05-06 neels && xk_to_y(x, k) == right->atoms.len) {
1206 3b0f3d61 2020-01-22 neels /* Found a path */
1207 3b0f3d61 2020-01-22 neels backtrack_d = d;
1208 3b0f3d61 2020-01-22 neels backtrack_k = k;
1209 3b0f3d61 2020-01-22 neels debug("Reached the end at d = %d, k = %d\n",
1210 3b0f3d61 2020-01-22 neels backtrack_d, backtrack_k);
1211 3b0f3d61 2020-01-22 neels break;
1212 3b0f3d61 2020-01-22 neels }
1213 3b0f3d61 2020-01-22 neels }
1214 3b0f3d61 2020-01-22 neels
1215 3b0f3d61 2020-01-22 neels if (backtrack_d >= 0)
1216 3b0f3d61 2020-01-22 neels break;
1217 3b0f3d61 2020-01-22 neels }
1218 3b0f3d61 2020-01-22 neels
1219 50198b5f 2020-05-05 neels debug_dump_myers_graph(left, right, kd_origin, NULL, 0, NULL, 0);
1220 3b0f3d61 2020-01-22 neels
1221 3b0f3d61 2020-01-22 neels /* backtrack. A matrix spanning from start to end of the file is ready:
1222 3b0f3d61 2020-01-22 neels *
1223 3b0f3d61 2020-01-22 neels * | d= 0 1 2 3 4
1224 3b0f3d61 2020-01-22 neels * ----+---------------------------------
1225 3b0f3d61 2020-01-22 neels * k= |
1226 3b0f3d61 2020-01-22 neels * 3 |
1227 3b0f3d61 2020-01-22 neels * |
1228 3b0f3d61 2020-01-22 neels * 2 | 2,0
1229 3b0f3d61 2020-01-22 neels * | /
1230 3b0f3d61 2020-01-22 neels * 1 | 1,0 4,3
1231 3b0f3d61 2020-01-22 neels * | / / \
1232 3b0f3d61 2020-01-22 neels * 0 | -->0,0 3,3 4,4 --> backtrack_d = 4, backtrack_k = 0
1233 3b0f3d61 2020-01-22 neels * | \ / \
1234 3b0f3d61 2020-01-22 neels * -1 | 0,1 3,4
1235 3b0f3d61 2020-01-22 neels * | \
1236 3b0f3d61 2020-01-22 neels * -2 | 0,2
1237 3b0f3d61 2020-01-22 neels * |
1238 3b0f3d61 2020-01-22 neels *
1239 3b0f3d61 2020-01-22 neels * From (4,4) backwards, find the previous position that is the largest, and remember it.
1240 3b0f3d61 2020-01-22 neels *
1241 3b0f3d61 2020-01-22 neels */
1242 3b0f3d61 2020-01-22 neels for (d = backtrack_d, k = backtrack_k; d >= 0; d--) {
1243 3b0f3d61 2020-01-22 neels x = kd_column[k];
1244 3b0f3d61 2020-01-22 neels y = xk_to_y(x, k);
1245 3b0f3d61 2020-01-22 neels
1246 0d27172a 2020-05-06 neels /* When the best position is identified, remember it for that
1247 0d27172a 2020-05-06 neels * kd_column.
1248 0d27172a 2020-05-06 neels * That kd_column is no longer needed otherwise, so just
1249 0d27172a 2020-05-06 neels * re-purpose kd_column[0] = x and kd_column[1] = y,
1250 3b0f3d61 2020-01-22 neels * so that there is no need to allocate more memory.
1251 3b0f3d61 2020-01-22 neels */
1252 3b0f3d61 2020-01-22 neels kd_column[0] = x;
1253 3b0f3d61 2020-01-22 neels kd_column[1] = y;
1254 3b0f3d61 2020-01-22 neels debug("Backtrack d=%d: xy=(%d, %d)\n",
1255 3b0f3d61 2020-01-22 neels d, kd_column[0], kd_column[1]);
1256 3b0f3d61 2020-01-22 neels
1257 3b0f3d61 2020-01-22 neels /* Don't access memory before kd_buf */
1258 3b0f3d61 2020-01-22 neels if (d == 0)
1259 3b0f3d61 2020-01-22 neels break;
1260 3b0f3d61 2020-01-22 neels int *kd_prev_column = kd_column - kd_len;
1261 3b0f3d61 2020-01-22 neels
1262 3b0f3d61 2020-01-22 neels /* When y == 0, backtracking downwards (k-1) is the only way.
1263 3b0f3d61 2020-01-22 neels * When x == 0, backtracking upwards (k+1) is the only way.
1264 3b0f3d61 2020-01-22 neels *
1265 3b0f3d61 2020-01-22 neels * | d= 0 1 2 3 4
1266 3b0f3d61 2020-01-22 neels * ----+---------------------------------
1267 3b0f3d61 2020-01-22 neels * k= |
1268 3b0f3d61 2020-01-22 neels * 3 |
1269 3b0f3d61 2020-01-22 neels * | ..y == 0
1270 3b0f3d61 2020-01-22 neels * 2 | 2,0
1271 3b0f3d61 2020-01-22 neels * | /
1272 3b0f3d61 2020-01-22 neels * 1 | 1,0 4,3
1273 3b0f3d61 2020-01-22 neels * | / / \
1274 0d27172a 2020-05-06 neels * 0 | -->0,0 3,3 4,4 --> backtrack_d = 4,
1275 0d27172a 2020-05-06 neels * | \ / \ backtrack_k = 0
1276 3b0f3d61 2020-01-22 neels * -1 | 0,1 3,4
1277 3b0f3d61 2020-01-22 neels * | \
1278 3b0f3d61 2020-01-22 neels * -2 | 0,2__
1279 3b0f3d61 2020-01-22 neels * | x == 0
1280 3b0f3d61 2020-01-22 neels */
1281 3b0f3d61 2020-01-22 neels debug("prev[k-1] = %d,%d prev[k+1] = %d,%d\n",
1282 3b0f3d61 2020-01-22 neels kd_prev_column[k-1], xk_to_y(kd_prev_column[k-1],k-1),
1283 3b0f3d61 2020-01-22 neels kd_prev_column[k+1], xk_to_y(kd_prev_column[k+1],k+1));
1284 3b0f3d61 2020-01-22 neels if (y == 0
1285 0d27172a 2020-05-06 neels || (x > 0
1286 0d27172a 2020-05-06 neels && kd_prev_column[k - 1] >= kd_prev_column[k + 1])) {
1287 3b0f3d61 2020-01-22 neels k = k - 1;
1288 3b0f3d61 2020-01-22 neels debug("prev k=k-1=%d x=%d y=%d\n",
1289 0d27172a 2020-05-06 neels k, kd_prev_column[k],
1290 0d27172a 2020-05-06 neels xk_to_y(kd_prev_column[k], k));
1291 3b0f3d61 2020-01-22 neels } else {
1292 3b0f3d61 2020-01-22 neels k = k + 1;
1293 3b0f3d61 2020-01-22 neels debug("prev k=k+1=%d x=%d y=%d\n",
1294 0d27172a 2020-05-06 neels k, kd_prev_column[k],
1295 0d27172a 2020-05-06 neels xk_to_y(kd_prev_column[k], k));
1296 3b0f3d61 2020-01-22 neels }
1297 3b0f3d61 2020-01-22 neels kd_column = kd_prev_column;
1298 3b0f3d61 2020-01-22 neels }
1299 3b0f3d61 2020-01-22 neels
1300 3b0f3d61 2020-01-22 neels /* Forwards again, this time recording the diff chunks.
1301 0d27172a 2020-05-06 neels * Definitely start from 0,0. kd_column[0] may actually point to the
1302 0d27172a 2020-05-06 neels * bottom of a snake starting at 0,0 */
1303 3b0f3d61 2020-01-22 neels x = 0;
1304 3b0f3d61 2020-01-22 neels y = 0;
1305 3b0f3d61 2020-01-22 neels
1306 3b0f3d61 2020-01-22 neels kd_column = kd_origin;
1307 3b0f3d61 2020-01-22 neels for (d = 0; d <= backtrack_d; d++, kd_column += kd_len) {
1308 3b0f3d61 2020-01-22 neels int next_x = kd_column[0];
1309 3b0f3d61 2020-01-22 neels int next_y = kd_column[1];
1310 3b0f3d61 2020-01-22 neels debug("Forward track from xy(%d,%d) to xy(%d,%d)\n",
1311 3b0f3d61 2020-01-22 neels x, y, next_x, next_y);
1312 3b0f3d61 2020-01-22 neels
1313 3b0f3d61 2020-01-22 neels struct diff_atom *left_atom = &left->atoms.head[x];
1314 3b0f3d61 2020-01-22 neels int left_section_len = next_x - x;
1315 3b0f3d61 2020-01-22 neels struct diff_atom *right_atom = &right->atoms.head[y];
1316 3b0f3d61 2020-01-22 neels int right_section_len = next_y - y;
1317 3b0f3d61 2020-01-22 neels
1318 3e6cba3a 2020-08-13 stsp rc = ENOMEM;
1319 3b0f3d61 2020-01-22 neels if (left_section_len && right_section_len) {
1320 3b0f3d61 2020-01-22 neels /* This must be a snake slide.
1321 0d27172a 2020-05-06 neels * Snake slides have a straight line leading into them
1322 0d27172a 2020-05-06 neels * (except when starting at (0,0)). Find out whether the
1323 0d27172a 2020-05-06 neels * lead-in is horizontal or vertical:
1324 3b0f3d61 2020-01-22 neels *
1325 3b0f3d61 2020-01-22 neels * left
1326 3b0f3d61 2020-01-22 neels * ---------->
1327 3b0f3d61 2020-01-22 neels * |
1328 3b0f3d61 2020-01-22 neels * r| o-o o
1329 3b0f3d61 2020-01-22 neels * i| \ |
1330 3b0f3d61 2020-01-22 neels * g| o o
1331 3b0f3d61 2020-01-22 neels * h| \ \
1332 3b0f3d61 2020-01-22 neels * t| o o
1333 3b0f3d61 2020-01-22 neels * v
1334 3b0f3d61 2020-01-22 neels *
1335 0d27172a 2020-05-06 neels * If left_section_len > right_section_len, the lead-in
1336 0d27172a 2020-05-06 neels * is horizontal, meaning first remove one atom from the
1337 0d27172a 2020-05-06 neels * left before sliding down the snake.
1338 0d27172a 2020-05-06 neels * If right_section_len > left_section_len, the lead-in
1339 0d27172a 2020-05-06 neels * is vetical, so add one atom from the right before
1340 0d27172a 2020-05-06 neels * sliding down the snake. */
1341 3b0f3d61 2020-01-22 neels if (left_section_len == right_section_len + 1) {
1342 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
1343 3b0f3d61 2020-01-22 neels left_atom, 1,
1344 3b0f3d61 2020-01-22 neels right_atom, 0))
1345 3b0f3d61 2020-01-22 neels goto return_rc;
1346 3b0f3d61 2020-01-22 neels left_atom++;
1347 3b0f3d61 2020-01-22 neels left_section_len--;
1348 3b0f3d61 2020-01-22 neels } else if (right_section_len == left_section_len + 1) {
1349 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
1350 3b0f3d61 2020-01-22 neels left_atom, 0,
1351 3b0f3d61 2020-01-22 neels right_atom, 1))
1352 3b0f3d61 2020-01-22 neels goto return_rc;
1353 3b0f3d61 2020-01-22 neels right_atom++;
1354 3b0f3d61 2020-01-22 neels right_section_len--;
1355 3b0f3d61 2020-01-22 neels } else if (left_section_len != right_section_len) {
1356 0d27172a 2020-05-06 neels /* The numbers are making no sense. Should never
1357 0d27172a 2020-05-06 neels * happen. */
1358 3b0f3d61 2020-01-22 neels rc = DIFF_RC_USE_DIFF_ALGO_FALLBACK;
1359 3b0f3d61 2020-01-22 neels goto return_rc;
1360 3b0f3d61 2020-01-22 neels }
1361 3b0f3d61 2020-01-22 neels
1362 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
1363 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
1364 0d27172a 2020-05-06 neels right_atom,
1365 0d27172a 2020-05-06 neels right_section_len))
1366 3b0f3d61 2020-01-22 neels goto return_rc;
1367 3b0f3d61 2020-01-22 neels } else if (left_section_len && !right_section_len) {
1368 0d27172a 2020-05-06 neels /* Only left atoms and none on the right, they form a
1369 0d27172a 2020-05-06 neels * "minus" chunk, then. */
1370 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
1371 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
1372 3b0f3d61 2020-01-22 neels right_atom, 0))
1373 3b0f3d61 2020-01-22 neels goto return_rc;
1374 3b0f3d61 2020-01-22 neels } else if (!left_section_len && right_section_len) {
1375 0d27172a 2020-05-06 neels /* No left atoms, only atoms on the right, they form a
1376 0d27172a 2020-05-06 neels * "plus" chunk, then. */
1377 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
1378 3b0f3d61 2020-01-22 neels left_atom, 0,
1379 0d27172a 2020-05-06 neels right_atom,
1380 0d27172a 2020-05-06 neels right_section_len))
1381 3b0f3d61 2020-01-22 neels goto return_rc;
1382 3b0f3d61 2020-01-22 neels }
1383 3b0f3d61 2020-01-22 neels
1384 3b0f3d61 2020-01-22 neels x = next_x;
1385 3b0f3d61 2020-01-22 neels y = next_y;
1386 3b0f3d61 2020-01-22 neels }
1387 3b0f3d61 2020-01-22 neels
1388 3b0f3d61 2020-01-22 neels rc = DIFF_RC_OK;
1389 3b0f3d61 2020-01-22 neels
1390 3b0f3d61 2020-01-22 neels return_rc:
1391 3b0f3d61 2020-01-22 neels free(kd_buf);
1392 3b0f3d61 2020-01-22 neels debug("** END %s rc=%d\n", __func__, rc);
1393 3b0f3d61 2020-01-22 neels return rc;
1394 3b0f3d61 2020-01-22 neels }