Blame


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