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