Blame


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