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). */
5 3b0f3d61 2020-01-22 neels * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
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.
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.
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>
26 e10a628a 2020-09-16 stsp #include <diff/arraylist.h>
27 3b0f3d61 2020-01-22 neels #include <diff/diff_main.h>
29 3b0f3d61 2020-01-22 neels #include "debug.h"
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.
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.
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;
54 3b0f3d61 2020-01-22 neels #define diff_box_empty(DIFF_SNAKE) ((DIFF_SNAKE)->left_end == 0)
57 3b0f3d61 2020-01-22 neels /* If the two contents of a file are A B C D E and X B C Y,
58 3b0f3d61 2020-01-22 neels * the Myers diff graph looks like:
62 3b0f3d61 2020-01-22 neels * k-1 0 1 2 3 4 5
63 3b0f3d61 2020-01-22 neels * \ A B C D E
64 3b0f3d61 2020-01-22 neels * 0 o-o-o-o-o-o
65 3b0f3d61 2020-01-22 neels * X | | | | | |
66 3b0f3d61 2020-01-22 neels * 1 o-o-o-o-o-o
67 3b0f3d61 2020-01-22 neels * B | |\| | | |
68 3b0f3d61 2020-01-22 neels * 2 o-o-o-o-o-o
69 3b0f3d61 2020-01-22 neels * C | | |\| | |
70 3b0f3d61 2020-01-22 neels * 3 o-o-o-o-o-o
71 3b0f3d61 2020-01-22 neels * Y | | | | | |\
72 3b0f3d61 2020-01-22 neels * 4 o-o-o-o-o-o c1
76 3b0f3d61 2020-01-22 neels * Moving right means delete an atom from the left-hand-side,
77 3b0f3d61 2020-01-22 neels * Moving down means add an atom from the right-hand-side.
78 0d27172a 2020-05-06 neels * Diagonals indicate identical atoms on both sides, the challenge is to use as
79 0d27172a 2020-05-06 neels * many diagonals as possible.
81 0d27172a 2020-05-06 neels * The original Myers algorithm walks all the way from the top left to the
82 0d27172a 2020-05-06 neels * bottom right, remembers all steps, and then backtraces to find the shortest
83 0d27172a 2020-05-06 neels * path. However, that requires keeping the entire graph in memory, which needs
84 3b0f3d61 2020-01-22 neels * quadratic space.
86 0d27172a 2020-05-06 neels * Myers adds a variant that uses linear space -- note, not linear time, only
87 0d27172a 2020-05-06 neels * linear space: walk forward and backward, find a meeting point in the middle,
88 0d27172a 2020-05-06 neels * and recurse on the two separate sections. This is called "divide and
89 0d27172a 2020-05-06 neels * conquer".
91 0d27172a 2020-05-06 neels * d: the step number, starting with 0, a.k.a. the distance from the starting
93 0d27172a 2020-05-06 neels * k: relative index in the state array for the forward scan, indicating on
94 0d27172a 2020-05-06 neels * which diagonal through the diff graph we currently are.
95 0d27172a 2020-05-06 neels * c: relative index in the state array for the backward scan, indicating the
96 0d27172a 2020-05-06 neels * diagonal number from the bottom up.
98 3b0f3d61 2020-01-22 neels * The "divide and conquer" traversal through the Myers graph looks like this:
100 3b0f3d61 2020-01-22 neels * | d= 0 1 2 3 2 1 0
101 3b0f3d61 2020-01-22 neels * ----+--------------------------------------------
105 3b0f3d61 2020-01-22 neels * 3 | 3,0 5,2 2
107 3b0f3d61 2020-01-22 neels * 2 | 2,0 5,3 1
109 3b0f3d61 2020-01-22 neels * 1 | 1,0 4,3 >= 4,3 5,4<-- 0
110 3b0f3d61 2020-01-22 neels * | / / \ /
111 3b0f3d61 2020-01-22 neels * 0 | -->0,0 3,3 4,4 -1
113 3b0f3d61 2020-01-22 neels * -1 | 0,1 1,2 3,4 -2
115 3b0f3d61 2020-01-22 neels * -2 | 0,2 -3
118 3b0f3d61 2020-01-22 neels * | forward-> <-backward
120 3b0f3d61 2020-01-22 neels * x,y pairs here are the coordinates in the Myers graph:
121 3b0f3d61 2020-01-22 neels * x = atom index in left-side source, y = atom index in the right-side source.
123 0d27172a 2020-05-06 neels * Only one forward column and one backward column are kept in mem, each need at
124 0d27172a 2020-05-06 neels * most left.len + 1 + right.len items. Note that each d step occupies either
125 0d27172a 2020-05-06 neels * the even or the odd items of a column: if e.g. the previous column is in the
126 0d27172a 2020-05-06 neels * odd items, the next column is formed in the even items, without overwriting
127 0d27172a 2020-05-06 neels * the previous column's results.
129 0d27172a 2020-05-06 neels * Also note that from the diagonal index k and the x coordinate, the y
130 0d27172a 2020-05-06 neels * coordinate can be derived:
131 3b0f3d61 2020-01-22 neels * y = x - k
132 0d27172a 2020-05-06 neels * Hence the state array only needs to keep the x coordinate, i.e. the position
133 0d27172a 2020-05-06 neels * in the left-hand file, and the y coordinate, i.e. position in the right-hand
134 0d27172a 2020-05-06 neels * file, is derived from the index in the state array.
136 0d27172a 2020-05-06 neels * The two traces meet at 4,3, the first step (here found in the forward
137 0d27172a 2020-05-06 neels * traversal) where a forward position is on or past a backward traced position
138 0d27172a 2020-05-06 neels * on the same diagonal.
140 3b0f3d61 2020-01-22 neels * This divides the problem space into:
142 3b0f3d61 2020-01-22 neels * 0 1 2 3 4 5
143 3b0f3d61 2020-01-22 neels * A B C D E
144 3b0f3d61 2020-01-22 neels * 0 o-o-o-o-o
145 3b0f3d61 2020-01-22 neels * X | | | | |
146 3b0f3d61 2020-01-22 neels * 1 o-o-o-o-o
147 3b0f3d61 2020-01-22 neels * B | |\| | |
148 3b0f3d61 2020-01-22 neels * 2 o-o-o-o-o
149 3b0f3d61 2020-01-22 neels * C | | |\| |
150 3b0f3d61 2020-01-22 neels * 3 o-o-o-o-*-o *: forward and backward meet here
154 3b0f3d61 2020-01-22 neels * Doing the same on each section lead to:
156 3b0f3d61 2020-01-22 neels * 0 1 2 3 4 5
157 3b0f3d61 2020-01-22 neels * A B C D E
160 3b0f3d61 2020-01-22 neels * 1 o-b b: backward d=1 first reaches here (sliding up the snake)
161 3b0f3d61 2020-01-22 neels * B \ f: then forward d=2 reaches here (sliding down the snake)
162 3b0f3d61 2020-01-22 neels * 2 o As result, the box from b to f is found to be identical;
163 0d27172a 2020-05-06 neels * C \ leaving a top box from 0,0 to 1,1 and a bottom trivial
164 0d27172a 2020-05-06 neels * 3 f-o tail 3,3 to 4,3.
168 3b0f3d61 2020-01-22 neels * 4 o *: forward and backward meet here
170 3b0f3d61 2020-01-22 neels * and solving the last top left box gives:
172 3b0f3d61 2020-01-22 neels * 0 1 2 3 4 5
173 3b0f3d61 2020-01-22 neels * A B C D E -A
174 3b0f3d61 2020-01-22 neels * 0 o-o +X
186 3b0f3d61 2020-01-22 neels #define xk_to_y(X, K) ((X) - (K))
187 3b0f3d61 2020-01-22 neels #define xc_to_y(X, C, DELTA) ((X) - (C) + (DELTA))
188 3b0f3d61 2020-01-22 neels #define k_to_c(K, DELTA) ((K) + (DELTA))
189 3b0f3d61 2020-01-22 neels #define c_to_k(C, DELTA) ((C) - (DELTA))
191 3b0f3d61 2020-01-22 neels /* Do one forwards step in the "divide and conquer" graph traversal.
192 3b0f3d61 2020-01-22 neels * left: the left side to diff.
193 3b0f3d61 2020-01-22 neels * right: the right side to diff against.
194 0d27172a 2020-05-06 neels * kd_forward: the traversal state for forwards traversal, modified by this
195 0d27172a 2020-05-06 neels * function.
196 3b0f3d61 2020-01-22 neels * This is carried over between invocations with increasing d.
197 0d27172a 2020-05-06 neels * kd_forward points at the center of the state array, allowing
198 0d27172a 2020-05-06 neels * negative indexes.
199 0d27172a 2020-05-06 neels * kd_backward: the traversal state for backwards traversal, to find a meeting
201 0d27172a 2020-05-06 neels * Since forwards is done first, kd_backward will be valid for d -
202 0d27172a 2020-05-06 neels * 1, not d.
203 0d27172a 2020-05-06 neels * kd_backward points at the center of the state array, allowing
204 0d27172a 2020-05-06 neels * negative indexes.
205 0d27172a 2020-05-06 neels * d: Step or distance counter, indicating for what value of d the kd_forward
206 0d27172a 2020-05-06 neels * should be populated.
207 0d27172a 2020-05-06 neels * For d == 0, kd_forward[0] is initialized, i.e. the first invocation should
208 0d27172a 2020-05-06 neels * be for d == 0.
209 3b0f3d61 2020-01-22 neels * meeting_snake: resulting meeting point, if any.
210 0d27172a 2020-05-06 neels * Return true when a meeting point has been identified.
212 ac2eeeff 2020-09-20 neels static int
213 ac2eeeff 2020-09-20 neels diff_divide_myers_forward(bool *found_midpoint,
214 ac2eeeff 2020-09-20 neels struct diff_data *left, struct diff_data *right,
215 61a7b578 2020-05-06 neels int *kd_forward, int *kd_backward, int d,
216 61a7b578 2020-05-06 neels struct diff_box *meeting_snake)
218 3b0f3d61 2020-01-22 neels int delta = (int)right->atoms.len - (int)left->atoms.len;
221 ac2eeeff 2020-09-20 neels *found_midpoint = false;
223 3b0f3d61 2020-01-22 neels debug("-- %s d=%d\n", __func__, d);
225 3b0f3d61 2020-01-22 neels for (k = d; k >= -d; k -= 2) {
226 3b0f3d61 2020-01-22 neels if (k < -(int)right->atoms.len || k > (int)left->atoms.len) {
227 0d27172a 2020-05-06 neels /* This diagonal is completely outside of the Myers
228 0d27172a 2020-05-06 neels * graph, don't calculate it. */
229 3b0f3d61 2020-01-22 neels if (k < -(int)right->atoms.len)
230 0d27172a 2020-05-06 neels debug(" %d k < -(int)right->atoms.len %d\n", k,
231 0d27172a 2020-05-06 neels -(int)right->atoms.len);
233 0d27172a 2020-05-06 neels debug(" %d k > left->atoms.len %d\n", k,
234 0d27172a 2020-05-06 neels left->atoms.len);
235 3b0f3d61 2020-01-22 neels if (k < 0) {
236 0d27172a 2020-05-06 neels /* We are traversing negatively, and already
237 0d27172a 2020-05-06 neels * below the entire graph, nothing will come of
238 0d27172a 2020-05-06 neels * this. */
239 3b0f3d61 2020-01-22 neels debug(" break");
242 3b0f3d61 2020-01-22 neels debug(" continue");
245 3b0f3d61 2020-01-22 neels debug("- k = %d\n", k);
246 3b0f3d61 2020-01-22 neels if (d == 0) {
247 0d27172a 2020-05-06 neels /* This is the initializing step. There is no prev_k
248 0d27172a 2020-05-06 neels * yet, get the initial x from the top left of the Myers
249 0d27172a 2020-05-06 neels * graph. */
252 0d27172a 2020-05-06 neels /* Favoring "-" lines first means favoring moving rightwards in
253 0d27172a 2020-05-06 neels * the Myers graph.
254 0d27172a 2020-05-06 neels * For this, all k should derive from k - 1, only the bottom
255 0d27172a 2020-05-06 neels * most k derive from k + 1:
257 3b0f3d61 2020-01-22 neels * | d= 0 1 2
258 3b0f3d61 2020-01-22 neels * ----+----------------
260 3b0f3d61 2020-01-22 neels * 2 | 2,0 <-- from prev_k = 2 - 1 = 1
264 3b0f3d61 2020-01-22 neels * 0 | -->0,0 3,3
266 0d27172a 2020-05-06 neels * -1 | 0,1 <-- bottom most for d=1 from
267 0d27172a 2020-05-06 neels * | \\ prev_k = -1 + 1 = 0
268 0d27172a 2020-05-06 neels * -2 | 0,2 <-- bottom most for d=2 from
269 0d27172a 2020-05-06 neels * prev_k = -2 + 1 = -1
271 0d27172a 2020-05-06 neels * Except when a k + 1 from a previous run already means a
272 0d27172a 2020-05-06 neels * further advancement in the graph.
273 3b0f3d61 2020-01-22 neels * If k == d, there is no k + 1 and k - 1 is the only option.
274 0d27172a 2020-05-06 neels * If k < d, use k + 1 in case that yields a larger x. Also use
275 0d27172a 2020-05-06 neels * k + 1 if k - 1 is outside the graph.
277 0d27172a 2020-05-06 neels else if (k > -d
278 0d27172a 2020-05-06 neels && (k == d
279 0d27172a 2020-05-06 neels || (k - 1 >= -(int)right->atoms.len
280 0d27172a 2020-05-06 neels && kd_forward[k - 1] >= kd_forward[k + 1]))) {
281 3b0f3d61 2020-01-22 neels /* Advance from k - 1.
282 0d27172a 2020-05-06 neels * From position prev_k, step to the right in the Myers
283 0d27172a 2020-05-06 neels * graph: x += 1.
285 3b0f3d61 2020-01-22 neels int prev_k = k - 1;
286 f71e8098 2020-05-05 neels int prev_x = kd_forward[prev_k];
287 3b0f3d61 2020-01-22 neels x = prev_x + 1;
289 3b0f3d61 2020-01-22 neels /* The bottom most one.
290 0d27172a 2020-05-06 neels * From position prev_k, step to the bottom in the Myers
291 0d27172a 2020-05-06 neels * graph: y += 1.
292 0d27172a 2020-05-06 neels * Incrementing y is achieved by decrementing k while
293 0d27172a 2020-05-06 neels * keeping the same x.
294 3b0f3d61 2020-01-22 neels * (since we're deriving y from y = x - k).
296 3b0f3d61 2020-01-22 neels int prev_k = k + 1;
297 f71e8098 2020-05-05 neels int prev_x = kd_forward[prev_k];
298 3b0f3d61 2020-01-22 neels x = prev_x;
301 f71e8098 2020-05-05 neels int x_before_slide = x;
302 3b0f3d61 2020-01-22 neels /* Slide down any snake that we might find here. */
303 b3fb4686 2020-09-20 neels while (x < left->atoms.len && xk_to_y(x, k) < right->atoms.len) {
304 b3fb4686 2020-09-20 neels bool same;
305 b3fb4686 2020-09-20 neels int r = diff_atom_same(&same,
306 b3fb4686 2020-09-20 neels &left->atoms.head[x],
307 b3fb4686 2020-09-20 neels &right->atoms.head[
308 b3fb4686 2020-09-20 neels xk_to_y(x, k)]);
311 b3fb4686 2020-09-20 neels if (!same)
315 3b0f3d61 2020-01-22 neels kd_forward[k] = x;
316 c5419a05 2020-05-05 neels if (x_before_slide != x) {
317 c5419a05 2020-05-05 neels debug(" down %d similar lines\n", x - x_before_slide);
320 3b0f3d61 2020-01-22 neels if (DEBUG) {
322 3b0f3d61 2020-01-22 neels for (fi = d; fi >= k; fi--) {
323 0d27172a 2020-05-06 neels debug("kd_forward[%d] = (%d, %d)\n", fi,
324 0d27172a 2020-05-06 neels kd_forward[fi], kd_forward[fi] - fi);
328 3b0f3d61 2020-01-22 neels if (x < 0 || x > left->atoms.len
329 3b0f3d61 2020-01-22 neels || xk_to_y(x, k) < 0 || xk_to_y(x, k) > right->atoms.len)
332 0d27172a 2020-05-06 neels /* Figured out a new forwards traversal, see if this has gone
333 0d27172a 2020-05-06 neels * onto or even past a preceding backwards traversal.
335 0d27172a 2020-05-06 neels * If the delta in length is odd, then d and backwards_d hit the
336 0d27172a 2020-05-06 neels * same state indexes:
337 3b0f3d61 2020-01-22 neels * | d= 0 1 2 1 0
338 3b0f3d61 2020-01-22 neels * ----+---------------- ----------------
344 3b0f3d61 2020-01-22 neels * 2 | 2,0====5,3 1
346 3b0f3d61 2020-01-22 neels * 1 | 1,0 5,4<-- 0
348 3b0f3d61 2020-01-22 neels * 0 | -->0,0 3,3====4,4 -1
350 3b0f3d61 2020-01-22 neels * -1 | 0,1 -2
352 3b0f3d61 2020-01-22 neels * -2 | 0,2 -3
355 0d27172a 2020-05-06 neels * If the delta is even, they end up off-by-one, i.e. on
356 0d27172a 2020-05-06 neels * different diagonals:
358 3b0f3d61 2020-01-22 neels * | d= 0 1 2 1 0
359 3b0f3d61 2020-01-22 neels * ----+---------------- ----------------
363 3b0f3d61 2020-01-22 neels * 2 | 2,0 off 2
365 3b0f3d61 2020-01-22 neels * 1 | 1,0 4,3 1
366 3b0f3d61 2020-01-22 neels * | / // \
367 3b0f3d61 2020-01-22 neels * 0 | -->0,0 3,3 4,4<-- 0
369 3b0f3d61 2020-01-22 neels * -1 | 0,1 3,4 -1
371 3b0f3d61 2020-01-22 neels * -2 | 0,2 -2
374 0d27172a 2020-05-06 neels * So in the forward path, we can only match up diagonals when
375 0d27172a 2020-05-06 neels * the delta is odd.
377 fd42ca98 2020-05-05 neels if ((delta & 1) == 0)
379 0d27172a 2020-05-06 neels /* Forwards is done first, so the backwards one was still at
380 0d27172a 2020-05-06 neels * d - 1. Can't do this for d == 0. */
381 3b0f3d61 2020-01-22 neels int backwards_d = d - 1;
382 fd42ca98 2020-05-05 neels if (backwards_d < 0)
385 fd42ca98 2020-05-05 neels debug("backwards_d = %d\n", backwards_d);
387 0d27172a 2020-05-06 neels /* If both sides have the same length, forward and backward
388 0d27172a 2020-05-06 neels * start on the same diagonal, meaning the backwards state index
390 0d27172a 2020-05-06 neels * As soon as the lengths are not the same, the backwards
391 0d27172a 2020-05-06 neels * traversal starts on a different diagonal, and c = k shifted
392 0d27172a 2020-05-06 neels * by the difference in length.
394 fd42ca98 2020-05-05 neels int c = k_to_c(k, delta);
396 0d27172a 2020-05-06 neels /* When the file sizes are very different, the traversal trees
397 0d27172a 2020-05-06 neels * start on far distant diagonals.
398 0d27172a 2020-05-06 neels * They don't necessarily meet straight on. See whether this
399 0d27172a 2020-05-06 neels * forward value is on a diagonal that is also valid in
400 0d27172a 2020-05-06 neels * kd_backward[], and match them if so. */
401 fd42ca98 2020-05-05 neels if (c >= -backwards_d && c <= backwards_d) {
402 0d27172a 2020-05-06 neels /* Current k is on a diagonal that exists in
403 0d27172a 2020-05-06 neels * kd_backward[]. If the two x positions have met or
404 0d27172a 2020-05-06 neels * passed (forward walked onto or past backward), then
405 0d27172a 2020-05-06 neels * we've found a midpoint / a mid-box.
407 0d27172a 2020-05-06 neels * When forwards and backwards traversals meet, the
408 0d27172a 2020-05-06 neels * endpoints of the mid-snake are not the two points in
409 0d27172a 2020-05-06 neels * kd_forward and kd_backward, but rather the section
410 0d27172a 2020-05-06 neels * that was slid (if any) of the current
411 0d27172a 2020-05-06 neels * forward/backward traversal only.
413 f71e8098 2020-05-05 neels * For example:
435 0d27172a 2020-05-06 neels * The forward traversal reached M from the top and slid
436 0d27172a 2020-05-06 neels * downwards to A. The backward traversal already
437 0d27172a 2020-05-06 neels * reached X, which is not a straight line from M
438 0d27172a 2020-05-06 neels * anymore, so picking a mid-snake from M to X would
439 0d27172a 2020-05-06 neels * yield a mistake.
441 0d27172a 2020-05-06 neels * The correct mid-snake is between M and A. M is where
442 0d27172a 2020-05-06 neels * the forward traversal hit the diagonal that the
443 0d27172a 2020-05-06 neels * backward traversal has already passed, and A is what
444 0d27172a 2020-05-06 neels * it reaches when sliding down identical lines.
446 fd42ca98 2020-05-05 neels int backward_x = kd_backward[c];
447 50198b5f 2020-05-05 neels debug("Compare: k=%d c=%d is (%d,%d) >= (%d,%d)?\n",
448 0d27172a 2020-05-06 neels k, c, x, xk_to_y(x, k), backward_x,
449 0d27172a 2020-05-06 neels xc_to_y(backward_x, c, delta));
450 f71e8098 2020-05-05 neels if (x >= backward_x) {
451 fd42ca98 2020-05-05 neels *meeting_snake = (struct diff_box){
452 f71e8098 2020-05-05 neels .left_start = x_before_slide,
453 fd42ca98 2020-05-05 neels .left_end = x,
454 0d27172a 2020-05-06 neels .right_start = xc_to_y(x_before_slide,
455 0d27172a 2020-05-06 neels c, delta),
456 fd42ca98 2020-05-05 neels .right_end = xk_to_y(x, k),
458 fd42ca98 2020-05-05 neels debug("HIT x=(%u,%u) - y=(%u,%u)\n",
459 fd42ca98 2020-05-05 neels meeting_snake->left_start,
460 fd42ca98 2020-05-05 neels meeting_snake->right_start,
461 fd42ca98 2020-05-05 neels meeting_snake->left_end,
462 fd42ca98 2020-05-05 neels meeting_snake->right_end);
463 0d27172a 2020-05-06 neels debug_dump_myers_graph(left, right, NULL,
464 0d27172a 2020-05-06 neels kd_forward, d,
465 0d27172a 2020-05-06 neels kd_backward, d-1);
466 ac2eeeff 2020-09-20 neels *found_midpoint = true;
472 0d27172a 2020-05-06 neels debug_dump_myers_graph(left, right, NULL, kd_forward, d,
473 0d27172a 2020-05-06 neels kd_backward, d-1);
477 3b0f3d61 2020-01-22 neels /* Do one backwards step in the "divide and conquer" graph traversal.
478 3b0f3d61 2020-01-22 neels * left: the left side to diff.
479 3b0f3d61 2020-01-22 neels * right: the right side to diff against.
480 0d27172a 2020-05-06 neels * kd_forward: the traversal state for forwards traversal, to find a meeting
482 0d27172a 2020-05-06 neels * Since forwards is done first, after this, both kd_forward and
483 0d27172a 2020-05-06 neels * kd_backward will be valid for d.
484 0d27172a 2020-05-06 neels * kd_forward points at the center of the state array, allowing
485 0d27172a 2020-05-06 neels * negative indexes.
486 0d27172a 2020-05-06 neels * kd_backward: the traversal state for backwards traversal, to find a meeting
488 3b0f3d61 2020-01-22 neels * This is carried over between invocations with increasing d.
489 0d27172a 2020-05-06 neels * kd_backward points at the center of the state array, allowing
490 0d27172a 2020-05-06 neels * negative indexes.
491 0d27172a 2020-05-06 neels * d: Step or distance counter, indicating for what value of d the kd_backward
492 0d27172a 2020-05-06 neels * should be populated.
493 0d27172a 2020-05-06 neels * Before the first invocation, kd_backward[0] shall point at the bottom
494 0d27172a 2020-05-06 neels * right of the Myers graph (left.len, right.len).
495 3b0f3d61 2020-01-22 neels * The first invocation will be for d == 1.
496 3b0f3d61 2020-01-22 neels * meeting_snake: resulting meeting point, if any.
497 0d27172a 2020-05-06 neels * Return true when a meeting point has been identified.
499 ac2eeeff 2020-09-20 neels static int
500 ac2eeeff 2020-09-20 neels diff_divide_myers_backward(bool *found_midpoint,
501 ac2eeeff 2020-09-20 neels struct diff_data *left, struct diff_data *right,
502 61a7b578 2020-05-06 neels int *kd_forward, int *kd_backward, int d,
503 61a7b578 2020-05-06 neels struct diff_box *meeting_snake)
505 3b0f3d61 2020-01-22 neels int delta = (int)right->atoms.len - (int)left->atoms.len;
509 ac2eeeff 2020-09-20 neels *found_midpoint = false;
511 3b0f3d61 2020-01-22 neels debug("-- %s d=%d\n", __func__, d);
513 3b0f3d61 2020-01-22 neels for (c = d; c >= -d; c -= 2) {
514 3b0f3d61 2020-01-22 neels if (c < -(int)left->atoms.len || c > (int)right->atoms.len) {
515 0d27172a 2020-05-06 neels /* This diagonal is completely outside of the Myers
516 0d27172a 2020-05-06 neels * graph, don't calculate it. */
517 3b0f3d61 2020-01-22 neels if (c < -(int)left->atoms.len)
518 0d27172a 2020-05-06 neels debug(" %d c < -(int)left->atoms.len %d\n", c,
519 0d27172a 2020-05-06 neels -(int)left->atoms.len);
521 0d27172a 2020-05-06 neels debug(" %d c > right->atoms.len %d\n", c,
522 0d27172a 2020-05-06 neels right->atoms.len);
523 3b0f3d61 2020-01-22 neels if (c < 0) {
524 0d27172a 2020-05-06 neels /* We are traversing negatively, and already
525 0d27172a 2020-05-06 neels * below the entire graph, nothing will come of
526 0d27172a 2020-05-06 neels * this. */
527 3b0f3d61 2020-01-22 neels debug(" break");
530 3b0f3d61 2020-01-22 neels debug(" continue");
533 3b0f3d61 2020-01-22 neels debug("- c = %d\n", c);
534 3b0f3d61 2020-01-22 neels if (d == 0) {
535 0d27172a 2020-05-06 neels /* This is the initializing step. There is no prev_c
536 0d27172a 2020-05-06 neels * yet, get the initial x from the bottom right of the
537 0d27172a 2020-05-06 neels * Myers graph. */
538 3b0f3d61 2020-01-22 neels x = left->atoms.len;
540 0d27172a 2020-05-06 neels /* Favoring "-" lines first means favoring moving rightwards in
541 0d27172a 2020-05-06 neels * the Myers graph.
542 0d27172a 2020-05-06 neels * For this, all c should derive from c - 1, only the bottom
543 0d27172a 2020-05-06 neels * most c derive from c + 1:
546 3b0f3d61 2020-01-22 neels * ---------------------------------------------------
550 3b0f3d61 2020-01-22 neels * from prev_c = c - 1 --> 5,2 2
554 3b0f3d61 2020-01-22 neels * 4,3 5,4<-- 0
556 3b0f3d61 2020-01-22 neels * bottom most for d=1 from c + 1 --> 4,4 -1
558 3b0f3d61 2020-01-22 neels * bottom most for d=2 --> 3,4 -2
560 0d27172a 2020-05-06 neels * Except when a c + 1 from a previous run already means a
561 0d27172a 2020-05-06 neels * further advancement in the graph.
562 3b0f3d61 2020-01-22 neels * If c == d, there is no c + 1 and c - 1 is the only option.
563 0d27172a 2020-05-06 neels * If c < d, use c + 1 in case that yields a larger x.
564 0d27172a 2020-05-06 neels * Also use c + 1 if c - 1 is outside the graph.
566 3b0f3d61 2020-01-22 neels else if (c > -d && (c == d
567 3b0f3d61 2020-01-22 neels || (c - 1 >= -(int)right->atoms.len
568 3b0f3d61 2020-01-22 neels && kd_backward[c - 1] <= kd_backward[c + 1]))) {
569 3b0f3d61 2020-01-22 neels /* A top one.
570 0d27172a 2020-05-06 neels * From position prev_c, step upwards in the Myers
571 0d27172a 2020-05-06 neels * graph: y -= 1.
572 0d27172a 2020-05-06 neels * Decrementing y is achieved by incrementing c while
573 0d27172a 2020-05-06 neels * keeping the same x. (since we're deriving y from
574 0d27172a 2020-05-06 neels * y = x - c + delta).
576 3b0f3d61 2020-01-22 neels int prev_c = c - 1;
577 f71e8098 2020-05-05 neels int prev_x = kd_backward[prev_c];
578 3b0f3d61 2020-01-22 neels x = prev_x;
580 3b0f3d61 2020-01-22 neels /* The bottom most one.
581 0d27172a 2020-05-06 neels * From position prev_c, step to the left in the Myers
582 0d27172a 2020-05-06 neels * graph: x -= 1.
584 3b0f3d61 2020-01-22 neels int prev_c = c + 1;
585 f71e8098 2020-05-05 neels int prev_x = kd_backward[prev_c];
586 3b0f3d61 2020-01-22 neels x = prev_x - 1;
589 0d27172a 2020-05-06 neels /* Slide up any snake that we might find here (sections of
590 0d27172a 2020-05-06 neels * identical lines on both sides). */
591 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,
593 0d27172a 2020-05-06 neels xc_to_y(x, c, delta)-1);
594 3b0f3d61 2020-01-22 neels if (x > 0) {
595 0d27172a 2020-05-06 neels debug(" l=");
596 0d27172a 2020-05-06 neels debug_dump_atom(left, right, &left->atoms.head[x-1]);
598 3b0f3d61 2020-01-22 neels if (xc_to_y(x, c, delta) > 0) {
599 0d27172a 2020-05-06 neels debug(" r=");
600 0d27172a 2020-05-06 neels debug_dump_atom(right, left,
601 0d27172a 2020-05-06 neels &right->atoms.head[xc_to_y(x, c, delta)-1]);
603 f71e8098 2020-05-05 neels int x_before_slide = x;
604 b3fb4686 2020-09-20 neels while (x > 0 && xc_to_y(x, c, delta) > 0) {
605 b3fb4686 2020-09-20 neels bool same;
606 b3fb4686 2020-09-20 neels int r = diff_atom_same(&same,
607 b3fb4686 2020-09-20 neels &left->atoms.head[x-1],
608 b3fb4686 2020-09-20 neels &right->atoms.head[
609 b3fb4686 2020-09-20 neels xc_to_y(x, c, delta)-1]);
612 b3fb4686 2020-09-20 neels if (!same)
616 3b0f3d61 2020-01-22 neels kd_backward[c] = x;
617 c5419a05 2020-05-05 neels if (x_before_slide != x) {
618 c5419a05 2020-05-05 neels debug(" up %d similar lines\n", x_before_slide - x);
621 3b0f3d61 2020-01-22 neels if (DEBUG) {
623 3b0f3d61 2020-01-22 neels for (fi = d; fi >= c; fi--) {
624 0d27172a 2020-05-06 neels debug("kd_backward[%d] = (%d, %d)\n",
626 0d27172a 2020-05-06 neels kd_backward[fi],
627 3b0f3d61 2020-01-22 neels kd_backward[fi] - fi + delta);
631 3b0f3d61 2020-01-22 neels if (x < 0 || x > left->atoms.len
632 0d27172a 2020-05-06 neels || xc_to_y(x, c, delta) < 0
633 0d27172a 2020-05-06 neels || xc_to_y(x, c, delta) > right->atoms.len)
636 0d27172a 2020-05-06 neels /* Figured out a new backwards traversal, see if this has gone
637 0d27172a 2020-05-06 neels * onto or even past a preceding forwards traversal.
639 0d27172a 2020-05-06 neels * If the delta in length is even, then d and backwards_d hit
640 0d27172a 2020-05-06 neels * the same state indexes -- note how this is different from in
641 0d27172a 2020-05-06 neels * the forwards traversal, because now both d are the same:
643 3b0f3d61 2020-01-22 neels * | d= 0 1 2 2 1 0
644 3b0f3d61 2020-01-22 neels * ----+---------------- --------------------
650 3b0f3d61 2020-01-22 neels * 2 | 2,0====5,2 2
652 3b0f3d61 2020-01-22 neels * 1 | 1,0 5,3 1
654 3b0f3d61 2020-01-22 neels * 0 | -->0,0 3,3====4,3 5,4<-- 0
656 3b0f3d61 2020-01-22 neels * -1 | 0,1 4,4 -1
658 3b0f3d61 2020-01-22 neels * -2 | 0,2 -2
661 0d27172a 2020-05-06 neels * If the delta is odd, they end up off-by-one, i.e. on
662 0d27172a 2020-05-06 neels * different diagonals.
663 0d27172a 2020-05-06 neels * So in the backward path, we can only match up diagonals when
664 0d27172a 2020-05-06 neels * the delta is even.
666 0d27172a 2020-05-06 neels if ((delta & 1) != 0)
668 0d27172a 2020-05-06 neels /* Forwards was done first, now both d are the same. */
669 0d27172a 2020-05-06 neels int forwards_d = d;
671 0d27172a 2020-05-06 neels /* As soon as the lengths are not the same, the
672 0d27172a 2020-05-06 neels * backwards traversal starts on a different diagonal,
673 0d27172a 2020-05-06 neels * and c = k shifted by the difference in length.
675 0d27172a 2020-05-06 neels int k = c_to_k(c, delta);
677 0d27172a 2020-05-06 neels /* When the file sizes are very different, the traversal trees
678 0d27172a 2020-05-06 neels * start on far distant diagonals.
679 0d27172a 2020-05-06 neels * They don't necessarily meet straight on. See whether this
680 0d27172a 2020-05-06 neels * backward value is also on a valid diagonal in kd_forward[],
681 0d27172a 2020-05-06 neels * and match them if so. */
682 0d27172a 2020-05-06 neels if (k >= -forwards_d && k <= forwards_d) {
683 0d27172a 2020-05-06 neels /* Current c is on a diagonal that exists in
684 0d27172a 2020-05-06 neels * kd_forward[]. If the two x positions have met or
685 0d27172a 2020-05-06 neels * passed (backward walked onto or past forward), then
686 0d27172a 2020-05-06 neels * we've found a midpoint / a mid-box.
688 0d27172a 2020-05-06 neels * When forwards and backwards traversals meet, the
689 0d27172a 2020-05-06 neels * endpoints of the mid-snake are not the two points in
690 0d27172a 2020-05-06 neels * kd_forward and kd_backward, but rather the section
691 0d27172a 2020-05-06 neels * that was slid (if any) of the current
692 0d27172a 2020-05-06 neels * forward/backward traversal only.
694 0d27172a 2020-05-06 neels * For example:
714 0d27172a 2020-05-06 neels * The backward traversal reached M from the bottom and
715 0d27172a 2020-05-06 neels * slid upwards. The forward traversal already reached
716 0d27172a 2020-05-06 neels * X, which is not a straight line from M anymore, so
717 0d27172a 2020-05-06 neels * picking a mid-snake from M to X would yield a
718 0d27172a 2020-05-06 neels * mistake.
720 0d27172a 2020-05-06 neels * The correct mid-snake is between M and A. M is where
721 0d27172a 2020-05-06 neels * the backward traversal hit the diagonal that the
722 0d27172a 2020-05-06 neels * forwards traversal has already passed, and A is what
723 0d27172a 2020-05-06 neels * it reaches when sliding up identical lines.
726 0d27172a 2020-05-06 neels int forward_x = kd_forward[k];
727 0d27172a 2020-05-06 neels debug("Compare: k=%d c=%d is (%d,%d) >= (%d,%d)?\n",
728 0d27172a 2020-05-06 neels k, c, forward_x, xk_to_y(forward_x, k),
729 0d27172a 2020-05-06 neels x, xc_to_y(x, c, delta));
730 0d27172a 2020-05-06 neels if (forward_x >= x) {
731 0d27172a 2020-05-06 neels *meeting_snake = (struct diff_box){
732 0d27172a 2020-05-06 neels .left_start = x,
733 0d27172a 2020-05-06 neels .left_end = x_before_slide,
734 0d27172a 2020-05-06 neels .right_start = xc_to_y(x, c, delta),
735 0d27172a 2020-05-06 neels .right_end = xk_to_y(x_before_slide, k),
737 0d27172a 2020-05-06 neels debug("HIT x=%u,%u - y=%u,%u\n",
738 0d27172a 2020-05-06 neels meeting_snake->left_start,
739 0d27172a 2020-05-06 neels meeting_snake->right_start,
740 0d27172a 2020-05-06 neels meeting_snake->left_end,
741 0d27172a 2020-05-06 neels meeting_snake->right_end);
742 0d27172a 2020-05-06 neels debug_dump_myers_graph(left, right, NULL,
743 0d27172a 2020-05-06 neels kd_forward, d,
744 0d27172a 2020-05-06 neels kd_backward, d);
745 ac2eeeff 2020-09-20 neels *found_midpoint = true;
750 0d27172a 2020-05-06 neels debug_dump_myers_graph(left, right, NULL, kd_forward, d, kd_backward,
755 0d27172a 2020-05-06 neels /* Myers "Divide et Impera": tracing forwards from the start and backwards from
756 0d27172a 2020-05-06 neels * the end to find a midpoint that divides the problem into smaller chunks.
757 0d27172a 2020-05-06 neels * Requires only linear amounts of memory. */
759 0d27172a 2020-05-06 neels diff_algo_myers_divide(const struct diff_algo_config *algo_config,
760 0d27172a 2020-05-06 neels struct diff_state *state)
762 3e6cba3a 2020-08-13 stsp int rc = ENOMEM;
763 3b0f3d61 2020-01-22 neels struct diff_data *left = &state->left;
764 3b0f3d61 2020-01-22 neels struct diff_data *right = &state->right;
766 3b0f3d61 2020-01-22 neels debug("\n** %s\n", __func__);
767 3b0f3d61 2020-01-22 neels debug("left:\n");
768 3b0f3d61 2020-01-22 neels debug_dump(left);
769 3b0f3d61 2020-01-22 neels debug("right:\n");
770 3b0f3d61 2020-01-22 neels debug_dump(right);
771 50198b5f 2020-05-05 neels debug_dump_myers_graph(left, right, NULL, NULL, 0, NULL, 0);
773 0d27172a 2020-05-06 neels /* Allocate two columns of a Myers graph, one for the forward and one
774 0d27172a 2020-05-06 neels * for the backward traversal. */
775 3b0f3d61 2020-01-22 neels unsigned int max = left->atoms.len + right->atoms.len;
776 3b0f3d61 2020-01-22 neels size_t kd_len = max + 1;
777 3b0f3d61 2020-01-22 neels size_t kd_buf_size = kd_len << 1;
778 3b0f3d61 2020-01-22 neels int *kd_buf = reallocarray(NULL, kd_buf_size, sizeof(int));
779 3b0f3d61 2020-01-22 neels if (!kd_buf)
780 3e6cba3a 2020-08-13 stsp return ENOMEM;
782 3b0f3d61 2020-01-22 neels for (i = 0; i < kd_buf_size; i++)
783 3b0f3d61 2020-01-22 neels kd_buf[i] = -1;
784 3b0f3d61 2020-01-22 neels int *kd_forward = kd_buf;
785 3b0f3d61 2020-01-22 neels int *kd_backward = kd_buf + kd_len;
787 0d27172a 2020-05-06 neels /* The 'k' axis in Myers spans positive and negative indexes, so point
788 0d27172a 2020-05-06 neels * the kd to the middle.
789 3b0f3d61 2020-01-22 neels * It is then possible to index from -max/2 .. max/2. */
790 3b0f3d61 2020-01-22 neels kd_forward += max/2;
791 3b0f3d61 2020-01-22 neels kd_backward += max/2;
794 3b0f3d61 2020-01-22 neels struct diff_box mid_snake = {};
795 a45330b1 2020-05-05 neels bool found_midpoint = false;
796 3b0f3d61 2020-01-22 neels for (d = 0; d <= (max/2); d++) {
798 3b0f3d61 2020-01-22 neels debug("-- d=%d\n", d);
799 ac2eeeff 2020-09-20 neels r = diff_divide_myers_forward(&found_midpoint, left, right,
800 ac2eeeff 2020-09-20 neels kd_forward, kd_backward, d,
801 ac2eeeff 2020-09-20 neels &mid_snake);
804 a45330b1 2020-05-05 neels if (found_midpoint)
806 ac2eeeff 2020-09-20 neels r = diff_divide_myers_backward(&found_midpoint, left, right,
807 ac2eeeff 2020-09-20 neels kd_forward, kd_backward, d,
808 ac2eeeff 2020-09-20 neels &mid_snake);
811 a45330b1 2020-05-05 neels if (found_midpoint)
815 a45330b1 2020-05-05 neels if (!found_midpoint) {
816 0d27172a 2020-05-06 neels /* Divide and conquer failed to find a meeting point. Use the
817 0d27172a 2020-05-06 neels * fallback_algo defined in the algo_config (leave this to the
818 0d27172a 2020-05-06 neels * caller). This is just paranoia/sanity, we normally should
819 0d27172a 2020-05-06 neels * always find a midpoint.
821 3b0f3d61 2020-01-22 neels debug(" no midpoint \n");
822 3b0f3d61 2020-01-22 neels rc = DIFF_RC_USE_DIFF_ALGO_FALLBACK;
823 3b0f3d61 2020-01-22 neels goto return_rc;
825 3b0f3d61 2020-01-22 neels debug(" mid snake L: %u to %u of %u R: %u to %u of %u\n",
826 3b0f3d61 2020-01-22 neels mid_snake.left_start, mid_snake.left_end, left->atoms.len,
827 0d27172a 2020-05-06 neels mid_snake.right_start, mid_snake.right_end,
828 0d27172a 2020-05-06 neels right->atoms.len);
830 3b0f3d61 2020-01-22 neels /* Section before the mid-snake. */
831 3b0f3d61 2020-01-22 neels debug("Section before the mid-snake\n");
833 3b0f3d61 2020-01-22 neels struct diff_atom *left_atom = &left->atoms.head[0];
834 3b0f3d61 2020-01-22 neels unsigned int left_section_len = mid_snake.left_start;
835 3b0f3d61 2020-01-22 neels struct diff_atom *right_atom = &right->atoms.head[0];
836 3b0f3d61 2020-01-22 neels unsigned int right_section_len = mid_snake.right_start;
838 3b0f3d61 2020-01-22 neels if (left_section_len && right_section_len) {
839 0d27172a 2020-05-06 neels /* Record an unsolved chunk, the caller will apply
840 0d27172a 2020-05-06 neels * inner_algo() on this chunk. */
841 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, false,
842 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
843 0d27172a 2020-05-06 neels right_atom,
844 0d27172a 2020-05-06 neels right_section_len))
845 3b0f3d61 2020-01-22 neels goto return_rc;
846 3b0f3d61 2020-01-22 neels } else if (left_section_len && !right_section_len) {
847 0d27172a 2020-05-06 neels /* Only left atoms and none on the right, they form a
848 0d27172a 2020-05-06 neels * "minus" chunk, then. */
849 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
850 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
851 3b0f3d61 2020-01-22 neels right_atom, 0))
852 3b0f3d61 2020-01-22 neels goto return_rc;
853 3b0f3d61 2020-01-22 neels } else if (!left_section_len && right_section_len) {
854 0d27172a 2020-05-06 neels /* No left atoms, only atoms on the right, they form a
855 0d27172a 2020-05-06 neels * "plus" chunk, then. */
856 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
857 3b0f3d61 2020-01-22 neels left_atom, 0,
858 0d27172a 2020-05-06 neels right_atom,
859 0d27172a 2020-05-06 neels right_section_len))
860 3b0f3d61 2020-01-22 neels goto return_rc;
862 0d27172a 2020-05-06 neels /* else: left_section_len == 0 and right_section_len == 0, i.e.
863 0d27172a 2020-05-06 neels * nothing before the mid-snake. */
865 a45330b1 2020-05-05 neels if (!diff_box_empty(&mid_snake)) {
866 0d27172a 2020-05-06 neels /* The midpoint is a "snake", i.e. on a section of
867 0d27172a 2020-05-06 neels * identical data on both sides: that section
868 0d27172a 2020-05-06 neels * immediately becomes a solved diff chunk. */
869 a45330b1 2020-05-05 neels debug("the mid-snake\n");
870 a45330b1 2020-05-05 neels if (!diff_state_add_chunk(state, true,
871 0d27172a 2020-05-06 neels &left->atoms.head[mid_snake.left_start],
872 0d27172a 2020-05-06 neels mid_snake.left_end - mid_snake.left_start,
873 0d27172a 2020-05-06 neels &right->atoms.head[mid_snake.right_start],
874 0d27172a 2020-05-06 neels mid_snake.right_end - mid_snake.right_start))
875 a45330b1 2020-05-05 neels goto return_rc;
878 3b0f3d61 2020-01-22 neels /* Section after the mid-snake. */
879 3b0f3d61 2020-01-22 neels debug("Section after the mid-snake\n");
880 0d27172a 2020-05-06 neels debug(" left_end %u right_end %u\n",
881 0d27172a 2020-05-06 neels mid_snake.left_end, mid_snake.right_end);
882 0d27172a 2020-05-06 neels debug(" left_count %u right_count %u\n",
883 0d27172a 2020-05-06 neels left->atoms.len, right->atoms.len);
884 3b0f3d61 2020-01-22 neels left_atom = &left->atoms.head[mid_snake.left_end];
885 3b0f3d61 2020-01-22 neels left_section_len = left->atoms.len - mid_snake.left_end;
886 3b0f3d61 2020-01-22 neels right_atom = &right->atoms.head[mid_snake.right_end];
887 3b0f3d61 2020-01-22 neels right_section_len = right->atoms.len - mid_snake.right_end;
889 3b0f3d61 2020-01-22 neels if (left_section_len && right_section_len) {
890 0d27172a 2020-05-06 neels /* Record an unsolved chunk, the caller will apply
891 0d27172a 2020-05-06 neels * inner_algo() on this chunk. */
892 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, false,
893 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
894 0d27172a 2020-05-06 neels right_atom,
895 0d27172a 2020-05-06 neels right_section_len))
896 3b0f3d61 2020-01-22 neels goto return_rc;
897 3b0f3d61 2020-01-22 neels } else if (left_section_len && !right_section_len) {
898 0d27172a 2020-05-06 neels /* Only left atoms and none on the right, they form a
899 0d27172a 2020-05-06 neels * "minus" chunk, then. */
900 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
901 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
902 3b0f3d61 2020-01-22 neels right_atom, 0))
903 3b0f3d61 2020-01-22 neels goto return_rc;
904 3b0f3d61 2020-01-22 neels } else if (!left_section_len && right_section_len) {
905 0d27172a 2020-05-06 neels /* No left atoms, only atoms on the right, they form a
906 0d27172a 2020-05-06 neels * "plus" chunk, then. */
907 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
908 3b0f3d61 2020-01-22 neels left_atom, 0,
909 0d27172a 2020-05-06 neels right_atom,
910 0d27172a 2020-05-06 neels right_section_len))
911 3b0f3d61 2020-01-22 neels goto return_rc;
913 0d27172a 2020-05-06 neels /* else: left_section_len == 0 and right_section_len == 0, i.e.
914 0d27172a 2020-05-06 neels * nothing after the mid-snake. */
917 3b0f3d61 2020-01-22 neels rc = DIFF_RC_OK;
919 3b0f3d61 2020-01-22 neels return_rc:
920 3b0f3d61 2020-01-22 neels free(kd_buf);
921 3b0f3d61 2020-01-22 neels debug("** END %s\n", __func__);
922 3b0f3d61 2020-01-22 neels return rc;
925 0d27172a 2020-05-06 neels /* Myers Diff tracing from the start all the way through to the end, requiring
926 0d27172a 2020-05-06 neels * quadratic amounts of memory. This can fail if the required space surpasses
927 0d27172a 2020-05-06 neels * algo_config->permitted_state_size. */
929 0d27172a 2020-05-06 neels diff_algo_myers(const struct diff_algo_config *algo_config,
930 0d27172a 2020-05-06 neels struct diff_state *state)
932 0d27172a 2020-05-06 neels /* do a diff_divide_myers_forward() without a _backward(), so that it
933 0d27172a 2020-05-06 neels * walks forward across the entire files to reach the end. Keep each
934 0d27172a 2020-05-06 neels * run's state, and do a final backtrace. */
935 3e6cba3a 2020-08-13 stsp int rc = ENOMEM;
936 3b0f3d61 2020-01-22 neels struct diff_data *left = &state->left;
937 3b0f3d61 2020-01-22 neels struct diff_data *right = &state->right;
939 3b0f3d61 2020-01-22 neels debug("\n** %s\n", __func__);
940 3b0f3d61 2020-01-22 neels debug("left:\n");
941 3b0f3d61 2020-01-22 neels debug_dump(left);
942 3b0f3d61 2020-01-22 neels debug("right:\n");
943 3b0f3d61 2020-01-22 neels debug_dump(right);
944 50198b5f 2020-05-05 neels debug_dump_myers_graph(left, right, NULL, NULL, 0, NULL, 0);
946 0d27172a 2020-05-06 neels /* Allocate two columns of a Myers graph, one for the forward and one
947 0d27172a 2020-05-06 neels * for the backward traversal. */
948 3b0f3d61 2020-01-22 neels unsigned int max = left->atoms.len + right->atoms.len;
949 3b0f3d61 2020-01-22 neels size_t kd_len = max + 1 + max;
950 3b0f3d61 2020-01-22 neels size_t kd_buf_size = kd_len * kd_len;
951 50198b5f 2020-05-05 neels size_t kd_state_size = kd_buf_size * sizeof(int);
952 3b0f3d61 2020-01-22 neels debug("state size: %zu\n", kd_buf_size);
953 3b0f3d61 2020-01-22 neels if (kd_buf_size < kd_len /* overflow? */
954 50198b5f 2020-05-05 neels || kd_state_size > algo_config->permitted_state_size) {
955 3b0f3d61 2020-01-22 neels debug("state size %zu > permitted_state_size %zu, use fallback_algo\n",
956 50198b5f 2020-05-05 neels kd_state_size, algo_config->permitted_state_size);
957 3b0f3d61 2020-01-22 neels return DIFF_RC_USE_DIFF_ALGO_FALLBACK;
960 3b0f3d61 2020-01-22 neels int *kd_buf = reallocarray(NULL, kd_buf_size, sizeof(int));
961 3b0f3d61 2020-01-22 neels if (!kd_buf)
962 3e6cba3a 2020-08-13 stsp return ENOMEM;
964 3b0f3d61 2020-01-22 neels for (i = 0; i < kd_buf_size; i++)
965 3b0f3d61 2020-01-22 neels kd_buf[i] = -1;
967 0d27172a 2020-05-06 neels /* The 'k' axis in Myers spans positive and negative indexes, so point
968 0d27172a 2020-05-06 neels * the kd to the middle.
969 3b0f3d61 2020-01-22 neels * It is then possible to index from -max .. max. */
970 3b0f3d61 2020-01-22 neels int *kd_origin = kd_buf + max;
971 3b0f3d61 2020-01-22 neels int *kd_column = kd_origin;
974 3b0f3d61 2020-01-22 neels int backtrack_d = -1;
975 3b0f3d61 2020-01-22 neels int backtrack_k = 0;
978 3b0f3d61 2020-01-22 neels for (d = 0; d <= max; d++, kd_column += kd_len) {
979 3b0f3d61 2020-01-22 neels debug("-- d=%d\n", d);
981 3b0f3d61 2020-01-22 neels debug("-- %s d=%d\n", __func__, d);
983 3b0f3d61 2020-01-22 neels for (k = d; k >= -d; k -= 2) {
984 0d27172a 2020-05-06 neels if (k < -(int)right->atoms.len
985 0d27172a 2020-05-06 neels || k > (int)left->atoms.len) {
986 0d27172a 2020-05-06 neels /* This diagonal is completely outside of the
987 0d27172a 2020-05-06 neels * Myers graph, don't calculate it. */
988 3b0f3d61 2020-01-22 neels if (k < -(int)right->atoms.len)
989 0d27172a 2020-05-06 neels debug(" %d k <"
990 0d27172a 2020-05-06 neels " -(int)right->atoms.len %d\n",
991 0d27172a 2020-05-06 neels k, -(int)right->atoms.len);
993 0d27172a 2020-05-06 neels debug(" %d k > left->atoms.len %d\n", k,
994 0d27172a 2020-05-06 neels left->atoms.len);
995 3b0f3d61 2020-01-22 neels if (k < 0) {
996 0d27172a 2020-05-06 neels /* We are traversing negatively, and
997 0d27172a 2020-05-06 neels * already below the entire graph,
998 0d27172a 2020-05-06 neels * nothing will come of this. */
999 3b0f3d61 2020-01-22 neels debug(" break");
1002 3b0f3d61 2020-01-22 neels debug(" continue");
1003 3b0f3d61 2020-01-22 neels continue;
1006 3b0f3d61 2020-01-22 neels debug("- k = %d\n", k);
1007 3b0f3d61 2020-01-22 neels if (d == 0) {
1008 0d27172a 2020-05-06 neels /* This is the initializing step. There is no
1009 0d27172a 2020-05-06 neels * prev_k yet, get the initial x from the top
1010 0d27172a 2020-05-06 neels * left of the Myers graph. */
1013 3b0f3d61 2020-01-22 neels int *kd_prev_column = kd_column - kd_len;
1015 0d27172a 2020-05-06 neels /* Favoring "-" lines first means favoring
1016 0d27172a 2020-05-06 neels * moving rightwards in the Myers graph.
1017 0d27172a 2020-05-06 neels * For this, all k should derive from k - 1,
1018 0d27172a 2020-05-06 neels * only the bottom most k derive from k + 1:
1020 3b0f3d61 2020-01-22 neels * | d= 0 1 2
1021 3b0f3d61 2020-01-22 neels * ----+----------------
1023 0d27172a 2020-05-06 neels * 2 | 2,0 <-- from
1024 0d27172a 2020-05-06 neels * | / prev_k = 2 - 1 = 1
1025 3b0f3d61 2020-01-22 neels * 1 | 1,0
1027 3b0f3d61 2020-01-22 neels * 0 | -->0,0 3,3
1029 0d27172a 2020-05-06 neels * -1 | 0,1 <-- bottom most for d=1
1030 0d27172a 2020-05-06 neels * | \\ from prev_k = -1+1 = 0
1031 0d27172a 2020-05-06 neels * -2 | 0,2 <-- bottom most for
1032 0d27172a 2020-05-06 neels * d=2 from
1033 0d27172a 2020-05-06 neels * prev_k = -2+1 = -1
1035 0d27172a 2020-05-06 neels * Except when a k + 1 from a previous run
1036 0d27172a 2020-05-06 neels * already means a further advancement in the
1038 0d27172a 2020-05-06 neels * If k == d, there is no k + 1 and k - 1 is the
1039 0d27172a 2020-05-06 neels * only option.
1040 0d27172a 2020-05-06 neels * If k < d, use k + 1 in case that yields a
1041 0d27172a 2020-05-06 neels * larger x. Also use k + 1 if k - 1 is outside
1042 0d27172a 2020-05-06 neels * the graph.
1044 0d27172a 2020-05-06 neels if (k > -d
1045 0d27172a 2020-05-06 neels && (k == d
1046 0d27172a 2020-05-06 neels || (k - 1 >= -(int)right->atoms.len
1047 0d27172a 2020-05-06 neels && kd_prev_column[k - 1]
1048 0d27172a 2020-05-06 neels >= kd_prev_column[k + 1]))) {
1049 3b0f3d61 2020-01-22 neels /* Advance from k - 1.
1050 0d27172a 2020-05-06 neels * From position prev_k, step to the
1051 0d27172a 2020-05-06 neels * right in the Myers graph: x += 1.
1053 3b0f3d61 2020-01-22 neels int prev_k = k - 1;
1054 3b0f3d61 2020-01-22 neels int prev_x = kd_prev_column[prev_k];
1055 3b0f3d61 2020-01-22 neels x = prev_x + 1;
1057 3b0f3d61 2020-01-22 neels /* The bottom most one.
1058 0d27172a 2020-05-06 neels * From position prev_k, step to the
1059 0d27172a 2020-05-06 neels * bottom in the Myers graph: y += 1.
1060 0d27172a 2020-05-06 neels * Incrementing y is achieved by
1061 0d27172a 2020-05-06 neels * decrementing k while keeping the same
1062 0d27172a 2020-05-06 neels * x. (since we're deriving y from y =
1063 0d27172a 2020-05-06 neels * x - k).
1065 3b0f3d61 2020-01-22 neels int prev_k = k + 1;
1066 3b0f3d61 2020-01-22 neels int prev_x = kd_prev_column[prev_k];
1067 3b0f3d61 2020-01-22 neels x = prev_x;
1071 3b0f3d61 2020-01-22 neels /* Slide down any snake that we might find here. */
1072 0d27172a 2020-05-06 neels while (x < left->atoms.len
1073 b3fb4686 2020-09-20 neels && xk_to_y(x, k) < right->atoms.len) {
1074 b3fb4686 2020-09-20 neels bool same;
1075 b3fb4686 2020-09-20 neels int r = diff_atom_same(&same,
1076 b3fb4686 2020-09-20 neels &left->atoms.head[x],
1077 b3fb4686 2020-09-20 neels &right->atoms.head[
1078 b3fb4686 2020-09-20 neels xk_to_y(x, k)]);
1080 b3fb4686 2020-09-20 neels return r;
1081 b3fb4686 2020-09-20 neels if (!same)
1085 3b0f3d61 2020-01-22 neels kd_column[k] = x;
1087 3b0f3d61 2020-01-22 neels if (DEBUG) {
1089 3b0f3d61 2020-01-22 neels for (fi = d; fi >= k; fi-=2) {
1090 0d27172a 2020-05-06 neels debug("kd_column[%d] = (%d, %d)\n", fi,
1091 0d27172a 2020-05-06 neels kd_column[fi],
1092 0d27172a 2020-05-06 neels kd_column[fi] - fi);
1096 0d27172a 2020-05-06 neels if (x == left->atoms.len
1097 0d27172a 2020-05-06 neels && xk_to_y(x, k) == right->atoms.len) {
1098 3b0f3d61 2020-01-22 neels /* Found a path */
1099 3b0f3d61 2020-01-22 neels backtrack_d = d;
1100 3b0f3d61 2020-01-22 neels backtrack_k = k;
1101 3b0f3d61 2020-01-22 neels debug("Reached the end at d = %d, k = %d\n",
1102 3b0f3d61 2020-01-22 neels backtrack_d, backtrack_k);
1107 3b0f3d61 2020-01-22 neels if (backtrack_d >= 0)
1111 50198b5f 2020-05-05 neels debug_dump_myers_graph(left, right, kd_origin, NULL, 0, NULL, 0);
1113 3b0f3d61 2020-01-22 neels /* backtrack. A matrix spanning from start to end of the file is ready:
1115 3b0f3d61 2020-01-22 neels * | d= 0 1 2 3 4
1116 3b0f3d61 2020-01-22 neels * ----+---------------------------------
1120 3b0f3d61 2020-01-22 neels * 2 | 2,0
1122 3b0f3d61 2020-01-22 neels * 1 | 1,0 4,3
1123 3b0f3d61 2020-01-22 neels * | / / \
1124 3b0f3d61 2020-01-22 neels * 0 | -->0,0 3,3 4,4 --> backtrack_d = 4, backtrack_k = 0
1125 3b0f3d61 2020-01-22 neels * | \ / \
1126 3b0f3d61 2020-01-22 neels * -1 | 0,1 3,4
1128 3b0f3d61 2020-01-22 neels * -2 | 0,2
1131 3b0f3d61 2020-01-22 neels * From (4,4) backwards, find the previous position that is the largest, and remember it.
1134 3b0f3d61 2020-01-22 neels for (d = backtrack_d, k = backtrack_k; d >= 0; d--) {
1135 3b0f3d61 2020-01-22 neels x = kd_column[k];
1136 3b0f3d61 2020-01-22 neels y = xk_to_y(x, k);
1138 0d27172a 2020-05-06 neels /* When the best position is identified, remember it for that
1139 0d27172a 2020-05-06 neels * kd_column.
1140 0d27172a 2020-05-06 neels * That kd_column is no longer needed otherwise, so just
1141 0d27172a 2020-05-06 neels * re-purpose kd_column[0] = x and kd_column[1] = y,
1142 3b0f3d61 2020-01-22 neels * so that there is no need to allocate more memory.
1144 3b0f3d61 2020-01-22 neels kd_column[0] = x;
1145 3b0f3d61 2020-01-22 neels kd_column[1] = y;
1146 3b0f3d61 2020-01-22 neels debug("Backtrack d=%d: xy=(%d, %d)\n",
1147 3b0f3d61 2020-01-22 neels d, kd_column[0], kd_column[1]);
1149 3b0f3d61 2020-01-22 neels /* Don't access memory before kd_buf */
1150 3b0f3d61 2020-01-22 neels if (d == 0)
1152 3b0f3d61 2020-01-22 neels int *kd_prev_column = kd_column - kd_len;
1154 3b0f3d61 2020-01-22 neels /* When y == 0, backtracking downwards (k-1) is the only way.
1155 3b0f3d61 2020-01-22 neels * When x == 0, backtracking upwards (k+1) is the only way.
1157 3b0f3d61 2020-01-22 neels * | d= 0 1 2 3 4
1158 3b0f3d61 2020-01-22 neels * ----+---------------------------------
1161 3b0f3d61 2020-01-22 neels * | ..y == 0
1162 3b0f3d61 2020-01-22 neels * 2 | 2,0
1164 3b0f3d61 2020-01-22 neels * 1 | 1,0 4,3
1165 3b0f3d61 2020-01-22 neels * | / / \
1166 0d27172a 2020-05-06 neels * 0 | -->0,0 3,3 4,4 --> backtrack_d = 4,
1167 0d27172a 2020-05-06 neels * | \ / \ backtrack_k = 0
1168 3b0f3d61 2020-01-22 neels * -1 | 0,1 3,4
1170 3b0f3d61 2020-01-22 neels * -2 | 0,2__
1171 3b0f3d61 2020-01-22 neels * | x == 0
1173 3b0f3d61 2020-01-22 neels debug("prev[k-1] = %d,%d prev[k+1] = %d,%d\n",
1174 3b0f3d61 2020-01-22 neels kd_prev_column[k-1], xk_to_y(kd_prev_column[k-1],k-1),
1175 3b0f3d61 2020-01-22 neels kd_prev_column[k+1], xk_to_y(kd_prev_column[k+1],k+1));
1176 3b0f3d61 2020-01-22 neels if (y == 0
1177 0d27172a 2020-05-06 neels || (x > 0
1178 0d27172a 2020-05-06 neels && kd_prev_column[k - 1] >= kd_prev_column[k + 1])) {
1179 3b0f3d61 2020-01-22 neels k = k - 1;
1180 3b0f3d61 2020-01-22 neels debug("prev k=k-1=%d x=%d y=%d\n",
1181 0d27172a 2020-05-06 neels k, kd_prev_column[k],
1182 0d27172a 2020-05-06 neels xk_to_y(kd_prev_column[k], k));
1184 3b0f3d61 2020-01-22 neels k = k + 1;
1185 3b0f3d61 2020-01-22 neels debug("prev k=k+1=%d x=%d y=%d\n",
1186 0d27172a 2020-05-06 neels k, kd_prev_column[k],
1187 0d27172a 2020-05-06 neels xk_to_y(kd_prev_column[k], k));
1189 3b0f3d61 2020-01-22 neels kd_column = kd_prev_column;
1192 3b0f3d61 2020-01-22 neels /* Forwards again, this time recording the diff chunks.
1193 0d27172a 2020-05-06 neels * Definitely start from 0,0. kd_column[0] may actually point to the
1194 0d27172a 2020-05-06 neels * bottom of a snake starting at 0,0 */
1198 3b0f3d61 2020-01-22 neels kd_column = kd_origin;
1199 3b0f3d61 2020-01-22 neels for (d = 0; d <= backtrack_d; d++, kd_column += kd_len) {
1200 3b0f3d61 2020-01-22 neels int next_x = kd_column[0];
1201 3b0f3d61 2020-01-22 neels int next_y = kd_column[1];
1202 3b0f3d61 2020-01-22 neels debug("Forward track from xy(%d,%d) to xy(%d,%d)\n",
1203 3b0f3d61 2020-01-22 neels x, y, next_x, next_y);
1205 3b0f3d61 2020-01-22 neels struct diff_atom *left_atom = &left->atoms.head[x];
1206 3b0f3d61 2020-01-22 neels int left_section_len = next_x - x;
1207 3b0f3d61 2020-01-22 neels struct diff_atom *right_atom = &right->atoms.head[y];
1208 3b0f3d61 2020-01-22 neels int right_section_len = next_y - y;
1210 3e6cba3a 2020-08-13 stsp rc = ENOMEM;
1211 3b0f3d61 2020-01-22 neels if (left_section_len && right_section_len) {
1212 3b0f3d61 2020-01-22 neels /* This must be a snake slide.
1213 0d27172a 2020-05-06 neels * Snake slides have a straight line leading into them
1214 0d27172a 2020-05-06 neels * (except when starting at (0,0)). Find out whether the
1215 0d27172a 2020-05-06 neels * lead-in is horizontal or vertical:
1218 3b0f3d61 2020-01-22 neels * ---------->
1220 3b0f3d61 2020-01-22 neels * r| o-o o
1227 0d27172a 2020-05-06 neels * If left_section_len > right_section_len, the lead-in
1228 0d27172a 2020-05-06 neels * is horizontal, meaning first remove one atom from the
1229 0d27172a 2020-05-06 neels * left before sliding down the snake.
1230 0d27172a 2020-05-06 neels * If right_section_len > left_section_len, the lead-in
1231 0d27172a 2020-05-06 neels * is vetical, so add one atom from the right before
1232 0d27172a 2020-05-06 neels * sliding down the snake. */
1233 3b0f3d61 2020-01-22 neels if (left_section_len == right_section_len + 1) {
1234 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
1235 3b0f3d61 2020-01-22 neels left_atom, 1,
1236 3b0f3d61 2020-01-22 neels right_atom, 0))
1237 3b0f3d61 2020-01-22 neels goto return_rc;
1238 3b0f3d61 2020-01-22 neels left_atom++;
1239 3b0f3d61 2020-01-22 neels left_section_len--;
1240 3b0f3d61 2020-01-22 neels } else if (right_section_len == left_section_len + 1) {
1241 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
1242 3b0f3d61 2020-01-22 neels left_atom, 0,
1243 3b0f3d61 2020-01-22 neels right_atom, 1))
1244 3b0f3d61 2020-01-22 neels goto return_rc;
1245 3b0f3d61 2020-01-22 neels right_atom++;
1246 3b0f3d61 2020-01-22 neels right_section_len--;
1247 3b0f3d61 2020-01-22 neels } else if (left_section_len != right_section_len) {
1248 0d27172a 2020-05-06 neels /* The numbers are making no sense. Should never
1249 0d27172a 2020-05-06 neels * happen. */
1250 3b0f3d61 2020-01-22 neels rc = DIFF_RC_USE_DIFF_ALGO_FALLBACK;
1251 3b0f3d61 2020-01-22 neels goto return_rc;
1254 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
1255 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
1256 0d27172a 2020-05-06 neels right_atom,
1257 0d27172a 2020-05-06 neels right_section_len))
1258 3b0f3d61 2020-01-22 neels goto return_rc;
1259 3b0f3d61 2020-01-22 neels } else if (left_section_len && !right_section_len) {
1260 0d27172a 2020-05-06 neels /* Only left atoms and none on the right, they form a
1261 0d27172a 2020-05-06 neels * "minus" chunk, then. */
1262 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
1263 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
1264 3b0f3d61 2020-01-22 neels right_atom, 0))
1265 3b0f3d61 2020-01-22 neels goto return_rc;
1266 3b0f3d61 2020-01-22 neels } else if (!left_section_len && right_section_len) {
1267 0d27172a 2020-05-06 neels /* No left atoms, only atoms on the right, they form a
1268 0d27172a 2020-05-06 neels * "plus" chunk, then. */
1269 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
1270 3b0f3d61 2020-01-22 neels left_atom, 0,
1271 0d27172a 2020-05-06 neels right_atom,
1272 0d27172a 2020-05-06 neels right_section_len))
1273 3b0f3d61 2020-01-22 neels goto return_rc;
1276 3b0f3d61 2020-01-22 neels x = next_x;
1277 3b0f3d61 2020-01-22 neels y = next_y;
1280 3b0f3d61 2020-01-22 neels rc = DIFF_RC_OK;
1282 3b0f3d61 2020-01-22 neels return_rc:
1283 3b0f3d61 2020-01-22 neels free(kd_buf);
1284 3b0f3d61 2020-01-22 neels debug("** END %s rc=%d\n", __func__, rc);
1285 3b0f3d61 2020-01-22 neels return rc;