Blame


1 3b0f3d61 2020-01-22 neels /* Implementation of the Patience Diff algorithm invented by Bram Cohen:
2 3b0f3d61 2020-01-22 neels * Divide a diff problem into smaller chunks by an LCS of common-unique lines. */
3 3b0f3d61 2020-01-22 neels /*
4 3b0f3d61 2020-01-22 neels * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
5 3b0f3d61 2020-01-22 neels *
6 3b0f3d61 2020-01-22 neels * Permission to use, copy, modify, and distribute this software for any
7 3b0f3d61 2020-01-22 neels * purpose with or without fee is hereby granted, provided that the above
8 3b0f3d61 2020-01-22 neels * copyright notice and this permission notice appear in all copies.
9 3b0f3d61 2020-01-22 neels *
10 3b0f3d61 2020-01-22 neels * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 3b0f3d61 2020-01-22 neels * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 3b0f3d61 2020-01-22 neels * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 3b0f3d61 2020-01-22 neels * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 3b0f3d61 2020-01-22 neels * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 3b0f3d61 2020-01-22 neels * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 3b0f3d61 2020-01-22 neels * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3b0f3d61 2020-01-22 neels */
18 3b0f3d61 2020-01-22 neels
19 3b0f3d61 2020-01-22 neels #include <assert.h>
20 e10a628a 2020-09-16 stsp #include <inttypes.h>
21 e10a628a 2020-09-16 stsp #include <errno.h>
22 e10a628a 2020-09-16 stsp #include <stdbool.h>
23 e10a628a 2020-09-16 stsp #include <stdio.h>
24 e10a628a 2020-09-16 stsp #include <stdlib.h>
25 e10a628a 2020-09-16 stsp
26 1dfba055 2020-10-07 stsp #include <arraylist.h>
27 1dfba055 2020-10-07 stsp #include <diff_main.h>
28 3b0f3d61 2020-01-22 neels
29 85ab4559 2020-09-22 stsp #include "diff_internal.h"
30 2a1b94d0 2020-09-26 stsp #include "diff_debug.h"
31 3b0f3d61 2020-01-22 neels
32 3b0f3d61 2020-01-22 neels /* Set unique_here = true for all atoms that exist exactly once in this list. */
33 44cf4950 2020-09-20 neels static int
34 61a7b578 2020-05-06 neels diff_atoms_mark_unique(struct diff_data *d, unsigned int *unique_count)
35 3b0f3d61 2020-01-22 neels {
36 3b0f3d61 2020-01-22 neels struct diff_atom *i;
37 3b0f3d61 2020-01-22 neels unsigned int count = 0;
38 3b0f3d61 2020-01-22 neels diff_data_foreach_atom(i, d) {
39 3b0f3d61 2020-01-22 neels i->patience.unique_here = true;
40 3b0f3d61 2020-01-22 neels i->patience.unique_in_both = true;
41 3b0f3d61 2020-01-22 neels count++;
42 3b0f3d61 2020-01-22 neels }
43 3b0f3d61 2020-01-22 neels diff_data_foreach_atom(i, d) {
44 3b0f3d61 2020-01-22 neels struct diff_atom *j;
45 3b0f3d61 2020-01-22 neels
46 3b0f3d61 2020-01-22 neels if (!i->patience.unique_here)
47 3b0f3d61 2020-01-22 neels continue;
48 3b0f3d61 2020-01-22 neels
49 3b0f3d61 2020-01-22 neels diff_data_foreach_atom_from(i + 1, j, d) {
50 b3fb4686 2020-09-20 neels bool same;
51 b3fb4686 2020-09-20 neels int r = diff_atom_same(&same, i, j);
52 b3fb4686 2020-09-20 neels if (r)
53 44cf4950 2020-09-20 neels return r;
54 b3fb4686 2020-09-20 neels if (!same)
55 b3fb4686 2020-09-20 neels continue;
56 b3fb4686 2020-09-20 neels if (i->patience.unique_here) {
57 b3fb4686 2020-09-20 neels i->patience.unique_here = false;
58 b3fb4686 2020-09-20 neels i->patience.unique_in_both = false;
59 3b0f3d61 2020-01-22 neels count--;
60 3b0f3d61 2020-01-22 neels }
61 b3fb4686 2020-09-20 neels j->patience.unique_here = false;
62 b3fb4686 2020-09-20 neels j->patience.unique_in_both = false;
63 b3fb4686 2020-09-20 neels count--;
64 3b0f3d61 2020-01-22 neels }
65 3b0f3d61 2020-01-22 neels }
66 3b0f3d61 2020-01-22 neels if (unique_count)
67 3b0f3d61 2020-01-22 neels *unique_count = count;
68 44cf4950 2020-09-20 neels return 0;
69 3b0f3d61 2020-01-22 neels }
70 3b0f3d61 2020-01-22 neels
71 0d27172a 2020-05-06 neels /* Mark those lines as atom->patience.unique_in_both = true that appear exactly
72 0d27172a 2020-05-06 neels * once in each side. */
73 44cf4950 2020-09-20 neels static int
74 61a7b578 2020-05-06 neels diff_atoms_mark_unique_in_both(struct diff_data *left, struct diff_data *right,
75 61a7b578 2020-05-06 neels unsigned int *unique_in_both_count)
76 3b0f3d61 2020-01-22 neels {
77 0d27172a 2020-05-06 neels /* Derive the final unique_in_both count without needing an explicit
78 0d27172a 2020-05-06 neels * iteration. So this is just some optimiziation to save one iteration
79 0d27172a 2020-05-06 neels * in the end. */
80 3b0f3d61 2020-01-22 neels unsigned int unique_in_both;
81 44cf4950 2020-09-20 neels int r;
82 3b0f3d61 2020-01-22 neels
83 44cf4950 2020-09-20 neels r = diff_atoms_mark_unique(left, &unique_in_both);
84 44cf4950 2020-09-20 neels if (r)
85 44cf4950 2020-09-20 neels return r;
86 44cf4950 2020-09-20 neels r = diff_atoms_mark_unique(right, NULL);
87 44cf4950 2020-09-20 neels if (r)
88 44cf4950 2020-09-20 neels return r;
89 3b0f3d61 2020-01-22 neels
90 3b0f3d61 2020-01-22 neels debug("unique_in_both %u\n", unique_in_both);
91 3b0f3d61 2020-01-22 neels
92 3b0f3d61 2020-01-22 neels struct diff_atom *i;
93 3b0f3d61 2020-01-22 neels diff_data_foreach_atom(i, left) {
94 3b0f3d61 2020-01-22 neels if (!i->patience.unique_here)
95 3b0f3d61 2020-01-22 neels continue;
96 3b0f3d61 2020-01-22 neels struct diff_atom *j;
97 3b0f3d61 2020-01-22 neels int found_in_b = 0;
98 3b0f3d61 2020-01-22 neels diff_data_foreach_atom(j, right) {
99 b3fb4686 2020-09-20 neels bool same;
100 b3fb4686 2020-09-20 neels int r = diff_atom_same(&same, i, j);
101 b3fb4686 2020-09-20 neels if (r)
102 44cf4950 2020-09-20 neels return r;
103 b3fb4686 2020-09-20 neels if (!same)
104 3b0f3d61 2020-01-22 neels continue;
105 3b0f3d61 2020-01-22 neels if (!j->patience.unique_here) {
106 3b0f3d61 2020-01-22 neels found_in_b = 2; /* or more */
107 3b0f3d61 2020-01-22 neels break;
108 3b0f3d61 2020-01-22 neels } else {
109 3b0f3d61 2020-01-22 neels found_in_b = 1;
110 3b0f3d61 2020-01-22 neels j->patience.pos_in_other = i;
111 3b0f3d61 2020-01-22 neels i->patience.pos_in_other = j;
112 3b0f3d61 2020-01-22 neels }
113 3b0f3d61 2020-01-22 neels }
114 3b0f3d61 2020-01-22 neels
115 3b0f3d61 2020-01-22 neels if (found_in_b == 0 || found_in_b > 1) {
116 3b0f3d61 2020-01-22 neels i->patience.unique_in_both = false;
117 3b0f3d61 2020-01-22 neels unique_in_both--;
118 0d27172a 2020-05-06 neels debug("unique_in_both %u (%d) ", unique_in_both,
119 0d27172a 2020-05-06 neels found_in_b);
120 3b0f3d61 2020-01-22 neels debug_dump_atom(left, NULL, i);
121 3b0f3d61 2020-01-22 neels }
122 3b0f3d61 2020-01-22 neels }
123 3b0f3d61 2020-01-22 neels
124 0d27172a 2020-05-06 neels /* Still need to unmark right[*]->patience.unique_in_both for atoms that
125 0d27172a 2020-05-06 neels * don't exist in left */
126 3b0f3d61 2020-01-22 neels diff_data_foreach_atom(i, right) {
127 3b0f3d61 2020-01-22 neels if (!i->patience.unique_here
128 3b0f3d61 2020-01-22 neels || !i->patience.unique_in_both)
129 3b0f3d61 2020-01-22 neels continue;
130 3b0f3d61 2020-01-22 neels struct diff_atom *j;
131 3b0f3d61 2020-01-22 neels bool found_in_a = false;
132 3b0f3d61 2020-01-22 neels diff_data_foreach_atom(j, left) {
133 b3fb4686 2020-09-20 neels bool same;
134 b3fb4686 2020-09-20 neels int r;
135 3b0f3d61 2020-01-22 neels if (!j->patience.unique_in_both)
136 3b0f3d61 2020-01-22 neels continue;
137 b3fb4686 2020-09-20 neels r = diff_atom_same(&same, i, j);
138 b3fb4686 2020-09-20 neels if (r)
139 44cf4950 2020-09-20 neels return r;
140 b3fb4686 2020-09-20 neels if (!same)
141 3b0f3d61 2020-01-22 neels continue;
142 3b0f3d61 2020-01-22 neels found_in_a = true;
143 3b0f3d61 2020-01-22 neels break;
144 3b0f3d61 2020-01-22 neels }
145 3b0f3d61 2020-01-22 neels
146 3b0f3d61 2020-01-22 neels if (!found_in_a)
147 3b0f3d61 2020-01-22 neels i->patience.unique_in_both = false;
148 3b0f3d61 2020-01-22 neels }
149 3b0f3d61 2020-01-22 neels
150 3b0f3d61 2020-01-22 neels if (unique_in_both_count)
151 3b0f3d61 2020-01-22 neels *unique_in_both_count = unique_in_both;
152 44cf4950 2020-09-20 neels return 0;
153 3b0f3d61 2020-01-22 neels }
154 3b0f3d61 2020-01-22 neels
155 44cf4950 2020-09-20 neels static int
156 0d27172a 2020-05-06 neels diff_atoms_swallow_identical_neighbors(struct diff_data *left,
157 0d27172a 2020-05-06 neels struct diff_data *right,
158 61a7b578 2020-05-06 neels unsigned int *unique_in_both_count)
159 bb7fb738 2020-01-27 neels {
160 0d27172a 2020-05-06 neels debug("trivially combine identical lines"
161 0d27172a 2020-05-06 neels " around unique_in_both lines\n");
162 3b0f3d61 2020-01-22 neels
163 bb7fb738 2020-01-27 neels unsigned int l_idx;
164 bb7fb738 2020-01-27 neels unsigned int next_l_idx;
165 bb7fb738 2020-01-27 neels unsigned int l_min = 0;
166 bb7fb738 2020-01-27 neels unsigned int r_min = 0;
167 bb7fb738 2020-01-27 neels for (l_idx = 0; l_idx < left->atoms.len; l_idx = next_l_idx) {
168 bb7fb738 2020-01-27 neels next_l_idx = l_idx + 1;
169 bb7fb738 2020-01-27 neels struct diff_atom *l = &left->atoms.head[l_idx];
170 bb7fb738 2020-01-27 neels
171 bb7fb738 2020-01-27 neels if (!l->patience.unique_in_both)
172 bb7fb738 2020-01-27 neels continue;
173 bb7fb738 2020-01-27 neels
174 bb7fb738 2020-01-27 neels debug("check identical lines around ");
175 bb7fb738 2020-01-27 neels debug_dump_atom(left, right, l);
176 bb7fb738 2020-01-27 neels
177 0d27172a 2020-05-06 neels unsigned int r_idx = diff_atom_idx(right,
178 0d27172a 2020-05-06 neels l->patience.pos_in_other);
179 bb7fb738 2020-01-27 neels
180 d362ea2e 2020-07-25 stsp struct diff_range identical_l;
181 d362ea2e 2020-07-25 stsp struct diff_range identical_r;
182 bb7fb738 2020-01-27 neels
183 bb7fb738 2020-01-27 neels /* Swallow upwards.
184 0d27172a 2020-05-06 neels * Each common-unique line swallows identical lines upwards and
185 0d27172a 2020-05-06 neels * downwards.
186 0d27172a 2020-05-06 neels * All common-unique lines that were part of the identical lines
187 0d27172a 2020-05-06 neels * following below were already swallowed in the previous
188 0d27172a 2020-05-06 neels * iteration, so we will never hit another common-unique line
189 0d27172a 2020-05-06 neels * above. */
190 bb7fb738 2020-01-27 neels for (identical_l.start = l_idx, identical_r.start = r_idx;
191 b3fb4686 2020-09-20 neels identical_l.start > l_min && identical_r.start > r_min;
192 b3fb4686 2020-09-20 neels identical_l.start--, identical_r.start--) {
193 b3fb4686 2020-09-20 neels bool same;
194 b3fb4686 2020-09-20 neels int r = diff_atom_same(&same,
195 0d27172a 2020-05-06 neels &left->atoms.head[identical_l.start - 1],
196 0d27172a 2020-05-06 neels &right->atoms.head[identical_r.start - 1]);
197 b3fb4686 2020-09-20 neels if (r)
198 44cf4950 2020-09-20 neels return r;
199 b3fb4686 2020-09-20 neels if (!same)
200 b3fb4686 2020-09-20 neels break;
201 b3fb4686 2020-09-20 neels }
202 bb7fb738 2020-01-27 neels
203 bb7fb738 2020-01-27 neels /* Swallow downwards */
204 bb7fb738 2020-01-27 neels for (identical_l.end = l_idx + 1, identical_r.end = r_idx + 1;
205 bb7fb738 2020-01-27 neels identical_l.end < left->atoms.len
206 b3fb4686 2020-09-20 neels && identical_r.end < right->atoms.len;
207 bb7fb738 2020-01-27 neels identical_l.end++, identical_r.end++,
208 bb7fb738 2020-01-27 neels next_l_idx++) {
209 0d27172a 2020-05-06 neels struct diff_atom *l_end;
210 0d27172a 2020-05-06 neels struct diff_atom *r_end;
211 b3fb4686 2020-09-20 neels bool same;
212 b3fb4686 2020-09-20 neels int r = diff_atom_same(&same,
213 b3fb4686 2020-09-20 neels &left->atoms.head[identical_l.end],
214 b3fb4686 2020-09-20 neels &right->atoms.head[identical_r.end]);
215 b3fb4686 2020-09-20 neels if (r)
216 44cf4950 2020-09-20 neels return r;
217 b3fb4686 2020-09-20 neels if (!same)
218 b3fb4686 2020-09-20 neels break;
219 0d27172a 2020-05-06 neels l_end = &left->atoms.head[identical_l.end];
220 ab699b5c 2020-05-12 stsp r_end = &right->atoms.head[identical_r.end];
221 0d27172a 2020-05-06 neels if (!l_end->patience.unique_in_both)
222 0d27172a 2020-05-06 neels continue;
223 0d27172a 2020-05-06 neels /* Part of a chunk of identical lines, remove from
224 0d27172a 2020-05-06 neels * listing of unique_in_both lines */
225 0d27172a 2020-05-06 neels l_end->patience.unique_in_both = false;
226 0d27172a 2020-05-06 neels r_end->patience.unique_in_both = false;
227 0d27172a 2020-05-06 neels (*unique_in_both_count)--;
228 bb7fb738 2020-01-27 neels }
229 bb7fb738 2020-01-27 neels
230 bb7fb738 2020-01-27 neels l->patience.identical_lines = identical_l;
231 0d27172a 2020-05-06 neels l->patience.pos_in_other->patience.identical_lines =
232 0d27172a 2020-05-06 neels identical_r;
233 bb7fb738 2020-01-27 neels
234 bb7fb738 2020-01-27 neels l_min = identical_l.end;
235 bb7fb738 2020-01-27 neels r_min = identical_r.end;
236 bb7fb738 2020-01-27 neels
237 d362ea2e 2020-07-25 stsp if (!diff_range_empty(&l->patience.identical_lines)) {
238 0d27172a 2020-05-06 neels debug("common-unique line at l=%u r=%u swallowed"
239 0d27172a 2020-05-06 neels " identical lines l=%u-%u r=%u-%u\n",
240 bb7fb738 2020-01-27 neels l_idx, r_idx,
241 bb7fb738 2020-01-27 neels identical_l.start, identical_l.end,
242 bb7fb738 2020-01-27 neels identical_r.start, identical_r.end);
243 bb7fb738 2020-01-27 neels }
244 bb7fb738 2020-01-27 neels debug("next_l_idx = %u\n", next_l_idx);
245 bb7fb738 2020-01-27 neels }
246 44cf4950 2020-09-20 neels return 0;
247 bb7fb738 2020-01-27 neels }
248 bb7fb738 2020-01-27 neels
249 0d27172a 2020-05-06 neels /* binary search to find the stack to put this atom "card" on. */
250 0d27172a 2020-05-06 neels static int
251 0d27172a 2020-05-06 neels find_target_stack(struct diff_atom *atom,
252 0d27172a 2020-05-06 neels struct diff_atom **patience_stacks,
253 0d27172a 2020-05-06 neels unsigned int patience_stacks_count)
254 0d27172a 2020-05-06 neels {
255 0d27172a 2020-05-06 neels unsigned int lo = 0;
256 0d27172a 2020-05-06 neels unsigned int hi = patience_stacks_count;
257 0d27172a 2020-05-06 neels while (lo < hi) {
258 0d27172a 2020-05-06 neels unsigned int mid = (lo + hi) >> 1;
259 0d27172a 2020-05-06 neels
260 0d27172a 2020-05-06 neels if (patience_stacks[mid]->patience.pos_in_other
261 0d27172a 2020-05-06 neels < atom->patience.pos_in_other)
262 0d27172a 2020-05-06 neels lo = mid + 1;
263 0d27172a 2020-05-06 neels else
264 0d27172a 2020-05-06 neels hi = mid;
265 0d27172a 2020-05-06 neels }
266 0d27172a 2020-05-06 neels return lo;
267 0d27172a 2020-05-06 neels }
268 0d27172a 2020-05-06 neels
269 0d27172a 2020-05-06 neels /* Among the lines that appear exactly once in each side, find the longest
270 0d27172a 2020-05-06 neels * streak that appear in both files in the same order (with other stuff allowed
271 0d27172a 2020-05-06 neels * to interleave). Use patience sort for that, as in the Patience Diff
272 0d27172a 2020-05-06 neels * algorithm.
273 0d27172a 2020-05-06 neels * See https://bramcohen.livejournal.com/73318.html and, for a much more
274 0d27172a 2020-05-06 neels * detailed explanation,
275 3b0f3d61 2020-01-22 neels * https://blog.jcoglan.com/2017/09/19/the-patience-diff-algorithm/ */
276 3e6cba3a 2020-08-13 stsp int
277 0d27172a 2020-05-06 neels diff_algo_patience(const struct diff_algo_config *algo_config,
278 0d27172a 2020-05-06 neels struct diff_state *state)
279 3b0f3d61 2020-01-22 neels {
280 44cf4950 2020-09-20 neels int rc;
281 3b0f3d61 2020-01-22 neels
282 3b0f3d61 2020-01-22 neels struct diff_data *left = &state->left;
283 3b0f3d61 2020-01-22 neels struct diff_data *right = &state->right;
284 3b0f3d61 2020-01-22 neels
285 3b0f3d61 2020-01-22 neels unsigned int unique_in_both_count;
286 3b0f3d61 2020-01-22 neels
287 3b0f3d61 2020-01-22 neels debug("\n** %s\n", __func__);
288 3b0f3d61 2020-01-22 neels
289 0d27172a 2020-05-06 neels /* Find those lines that appear exactly once in 'left' and exactly once
290 0d27172a 2020-05-06 neels * in 'right'. */
291 44cf4950 2020-09-20 neels rc = diff_atoms_mark_unique_in_both(left, right, &unique_in_both_count);
292 44cf4950 2020-09-20 neels if (rc)
293 44cf4950 2020-09-20 neels return rc;
294 3b0f3d61 2020-01-22 neels
295 3b0f3d61 2020-01-22 neels debug("unique_in_both_count %u\n", unique_in_both_count);
296 3b0f3d61 2020-01-22 neels debug("left:\n");
297 3b0f3d61 2020-01-22 neels debug_dump(left);
298 3b0f3d61 2020-01-22 neels debug("right:\n");
299 3b0f3d61 2020-01-22 neels debug_dump(right);
300 3b0f3d61 2020-01-22 neels
301 3b0f3d61 2020-01-22 neels if (!unique_in_both_count) {
302 0d27172a 2020-05-06 neels /* Cannot apply Patience, tell the caller to use fallback_algo
303 0d27172a 2020-05-06 neels * instead. */
304 3b0f3d61 2020-01-22 neels return DIFF_RC_USE_DIFF_ALGO_FALLBACK;
305 3b0f3d61 2020-01-22 neels }
306 3b0f3d61 2020-01-22 neels
307 44cf4950 2020-09-20 neels rc = diff_atoms_swallow_identical_neighbors(left, right,
308 44cf4950 2020-09-20 neels &unique_in_both_count);
309 44cf4950 2020-09-20 neels if (rc)
310 44cf4950 2020-09-20 neels return rc;
311 bb7fb738 2020-01-27 neels debug("After swallowing identical neighbors: unique_in_both = %u\n",
312 bb7fb738 2020-01-27 neels unique_in_both_count);
313 bb7fb738 2020-01-27 neels
314 44cf4950 2020-09-20 neels rc = ENOMEM;
315 44cf4950 2020-09-20 neels
316 0d27172a 2020-05-06 neels /* An array of Longest Common Sequence is the result of the below
317 0d27172a 2020-05-06 neels * subscope: */
318 3b0f3d61 2020-01-22 neels unsigned int lcs_count = 0;
319 3b0f3d61 2020-01-22 neels struct diff_atom **lcs = NULL;
320 3b0f3d61 2020-01-22 neels struct diff_atom *lcs_tail = NULL;
321 3b0f3d61 2020-01-22 neels
322 3b0f3d61 2020-01-22 neels {
323 0d27172a 2020-05-06 neels /* This subscope marks the lifetime of the atom_pointers
324 0d27172a 2020-05-06 neels * allocation */
325 3b0f3d61 2020-01-22 neels
326 3b0f3d61 2020-01-22 neels /* One chunk of storage for atom pointers */
327 0d27172a 2020-05-06 neels struct diff_atom **atom_pointers;
328 0d27172a 2020-05-06 neels atom_pointers = recallocarray(NULL, 0, unique_in_both_count * 2,
329 0d27172a 2020-05-06 neels sizeof(struct diff_atom*));
330 3b0f3d61 2020-01-22 neels
331 0d27172a 2020-05-06 neels /* Half for the list of atoms that still need to be put on
332 0d27172a 2020-05-06 neels * stacks */
333 3b0f3d61 2020-01-22 neels struct diff_atom **uniques = atom_pointers;
334 3b0f3d61 2020-01-22 neels
335 0d27172a 2020-05-06 neels /* Half for the patience sort state's "card stacks" -- we
336 0d27172a 2020-05-06 neels * remember only each stack's topmost "card" */
337 0d27172a 2020-05-06 neels struct diff_atom **patience_stacks;
338 0d27172a 2020-05-06 neels patience_stacks = atom_pointers + unique_in_both_count;
339 3b0f3d61 2020-01-22 neels unsigned int patience_stacks_count = 0;
340 3b0f3d61 2020-01-22 neels
341 3b0f3d61 2020-01-22 neels /* Take all common, unique items from 'left' ... */
342 3b0f3d61 2020-01-22 neels
343 3b0f3d61 2020-01-22 neels struct diff_atom *atom;
344 3b0f3d61 2020-01-22 neels struct diff_atom **uniques_end = uniques;
345 3b0f3d61 2020-01-22 neels diff_data_foreach_atom(atom, left) {
346 3b0f3d61 2020-01-22 neels if (!atom->patience.unique_in_both)
347 3b0f3d61 2020-01-22 neels continue;
348 3b0f3d61 2020-01-22 neels *uniques_end = atom;
349 3b0f3d61 2020-01-22 neels uniques_end++;
350 3b0f3d61 2020-01-22 neels }
351 3b0f3d61 2020-01-22 neels
352 3b0f3d61 2020-01-22 neels /* ...and sort them to the order found in 'right'.
353 0d27172a 2020-05-06 neels * The idea is to find the leftmost stack that has a higher line
354 0d27172a 2020-05-06 neels * number and add it to the stack's top.
355 0d27172a 2020-05-06 neels * If there is no such stack, open a new one on the right. The
356 0d27172a 2020-05-06 neels * line number is derived from the atom*, which are array items
357 0d27172a 2020-05-06 neels * and hence reflect the relative position in the source file.
358 0d27172a 2020-05-06 neels * So we got the common-uniques from 'left' and sort them
359 0d27172a 2020-05-06 neels * according to atom->patience.pos_in_other. */
360 3b0f3d61 2020-01-22 neels unsigned int i;
361 3b0f3d61 2020-01-22 neels for (i = 0; i < unique_in_both_count; i++) {
362 3b0f3d61 2020-01-22 neels atom = uniques[i];
363 3b0f3d61 2020-01-22 neels unsigned int target_stack;
364 0d27172a 2020-05-06 neels target_stack = find_target_stack(atom, patience_stacks,
365 0d27172a 2020-05-06 neels patience_stacks_count);
366 3b0f3d61 2020-01-22 neels assert(target_stack <= patience_stacks_count);
367 3b0f3d61 2020-01-22 neels patience_stacks[target_stack] = atom;
368 3b0f3d61 2020-01-22 neels if (target_stack == patience_stacks_count)
369 3b0f3d61 2020-01-22 neels patience_stacks_count++;
370 3b0f3d61 2020-01-22 neels
371 0d27172a 2020-05-06 neels /* Record a back reference to the next stack on the
372 0d27172a 2020-05-06 neels * left, which will form the final longest sequence
373 3b0f3d61 2020-01-22 neels * later. */
374 0d27172a 2020-05-06 neels atom->patience.prev_stack = target_stack ?
375 0d27172a 2020-05-06 neels patience_stacks[target_stack - 1] : NULL;
376 3b0f3d61 2020-01-22 neels
377 3b0f3d61 2020-01-22 neels }
378 3b0f3d61 2020-01-22 neels
379 0d27172a 2020-05-06 neels /* backtrace through prev_stack references to form the final
380 0d27172a 2020-05-06 neels * longest common sequence */
381 3b0f3d61 2020-01-22 neels lcs_tail = patience_stacks[patience_stacks_count - 1];
382 3b0f3d61 2020-01-22 neels lcs_count = patience_stacks_count;
383 3b0f3d61 2020-01-22 neels
384 0d27172a 2020-05-06 neels /* uniques and patience_stacks are no longer needed.
385 0d27172a 2020-05-06 neels * Backpointers are in atom->patience.prev_stack */
386 3b0f3d61 2020-01-22 neels free(atom_pointers);
387 3b0f3d61 2020-01-22 neels }
388 3b0f3d61 2020-01-22 neels
389 3b0f3d61 2020-01-22 neels lcs = recallocarray(NULL, 0, lcs_count, sizeof(struct diff_atom*));
390 3b0f3d61 2020-01-22 neels struct diff_atom **lcs_backtrace_pos = &lcs[lcs_count - 1];
391 3b0f3d61 2020-01-22 neels struct diff_atom *atom;
392 3b0f3d61 2020-01-22 neels for (atom = lcs_tail; atom; atom = atom->patience.prev_stack, lcs_backtrace_pos--) {
393 3b0f3d61 2020-01-22 neels assert(lcs_backtrace_pos >= lcs);
394 3b0f3d61 2020-01-22 neels *lcs_backtrace_pos = atom;
395 3b0f3d61 2020-01-22 neels }
396 3b0f3d61 2020-01-22 neels
397 3b0f3d61 2020-01-22 neels unsigned int i;
398 3b0f3d61 2020-01-22 neels if (DEBUG) {
399 3b0f3d61 2020-01-22 neels debug("\npatience LCS:\n");
400 3b0f3d61 2020-01-22 neels for (i = 0; i < lcs_count; i++) {
401 3b0f3d61 2020-01-22 neels debug_dump_atom(left, right, lcs[i]);
402 3b0f3d61 2020-01-22 neels }
403 3b0f3d61 2020-01-22 neels }
404 3b0f3d61 2020-01-22 neels
405 3b0f3d61 2020-01-22 neels
406 0d27172a 2020-05-06 neels /* TODO: For each common-unique line found (now listed in lcs), swallow
407 0d27172a 2020-05-06 neels * lines upwards and downwards that are identical on each side. Requires
408 0d27172a 2020-05-06 neels * a way to represent atoms being glued to adjacent atoms. */
409 3b0f3d61 2020-01-22 neels
410 3b0f3d61 2020-01-22 neels debug("\ntraverse LCS, possibly recursing:\n");
411 3b0f3d61 2020-01-22 neels
412 0d27172a 2020-05-06 neels /* Now we have pinned positions in both files at which it makes sense to
413 0d27172a 2020-05-06 neels * divide the diff problem into smaller chunks. Go into the next round:
414 0d27172a 2020-05-06 neels * look at each section in turn, trying to again find common-unique
415 0d27172a 2020-05-06 neels * lines in those smaller sections. As soon as no more are found, the
416 0d27172a 2020-05-06 neels * remaining smaller sections are solved by Myers. */
417 3b0f3d61 2020-01-22 neels unsigned int left_pos = 0;
418 3b0f3d61 2020-01-22 neels unsigned int right_pos = 0;
419 3b0f3d61 2020-01-22 neels for (i = 0; i <= lcs_count; i++) {
420 3b0f3d61 2020-01-22 neels struct diff_atom *atom;
421 bb7fb738 2020-01-27 neels struct diff_atom *atom_r;
422 3b0f3d61 2020-01-22 neels unsigned int left_idx;
423 3b0f3d61 2020-01-22 neels unsigned int right_idx;
424 3b0f3d61 2020-01-22 neels
425 3b0f3d61 2020-01-22 neels if (i < lcs_count) {
426 3b0f3d61 2020-01-22 neels atom = lcs[i];
427 bb7fb738 2020-01-27 neels atom_r = atom->patience.pos_in_other;
428 2a1b94d0 2020-09-26 stsp debug("lcs[%u] = left[%u] = right[%u]\n", i,
429 bb7fb738 2020-01-27 neels diff_atom_idx(left, atom), diff_atom_idx(right, atom_r));
430 bb7fb738 2020-01-27 neels left_idx = atom->patience.identical_lines.start;
431 bb7fb738 2020-01-27 neels right_idx = atom_r->patience.identical_lines.start;
432 bb7fb738 2020-01-27 neels debug(" identical lines l %u-%u r %u-%u\n",
433 bb7fb738 2020-01-27 neels atom->patience.identical_lines.start, atom->patience.identical_lines.end,
434 bb7fb738 2020-01-27 neels atom_r->patience.identical_lines.start, atom_r->patience.identical_lines.end);
435 3b0f3d61 2020-01-22 neels } else {
436 3b0f3d61 2020-01-22 neels atom = NULL;
437 bb7fb738 2020-01-27 neels atom_r = NULL;
438 3b0f3d61 2020-01-22 neels left_idx = left->atoms.len;
439 3b0f3d61 2020-01-22 neels right_idx = right->atoms.len;
440 3b0f3d61 2020-01-22 neels }
441 3b0f3d61 2020-01-22 neels
442 0d27172a 2020-05-06 neels /* 'atom' now marks an atom that matches on both sides according
443 0d27172a 2020-05-06 neels * to patience-diff (a common-unique identical atom in both
444 0d27172a 2020-05-06 neels * files).
445 0d27172a 2020-05-06 neels * Handle the section before and the atom itself; the section
446 0d27172a 2020-05-06 neels * after will be handled by the next loop iteration -- note that
447 0d27172a 2020-05-06 neels * i loops to last element + 1 ("i <= lcs_count"), so that there
448 0d27172a 2020-05-06 neels * will be another final iteration to pick up the last remaining
449 0d27172a 2020-05-06 neels * items after the last LCS atom.
450 3b0f3d61 2020-01-22 neels * The sections before might also be empty on left and/or right.
451 0d27172a 2020-05-06 neels * left_pos and right_pos mark the indexes of the first atoms
452 0d27172a 2020-05-06 neels * that have not yet been handled in the previous loop
453 0d27172a 2020-05-06 neels * iteration. left_idx and right_idx mark the indexes of the
454 0d27172a 2020-05-06 neels * matching atom on left and right, respectively. */
455 3b0f3d61 2020-01-22 neels
456 0d27172a 2020-05-06 neels debug("iteration %u left_pos %u left_idx %u"
457 0d27172a 2020-05-06 neels " right_pos %u right_idx %u\n",
458 3b0f3d61 2020-01-22 neels i, left_pos, left_idx, right_pos, right_idx);
459 3b0f3d61 2020-01-22 neels
460 3b0f3d61 2020-01-22 neels /* Section before the matching atom */
461 3b0f3d61 2020-01-22 neels struct diff_atom *left_atom = &left->atoms.head[left_pos];
462 3b0f3d61 2020-01-22 neels unsigned int left_section_len = left_idx - left_pos;
463 3b0f3d61 2020-01-22 neels
464 3b0f3d61 2020-01-22 neels struct diff_atom *right_atom = &(right->atoms.head[right_pos]);
465 3b0f3d61 2020-01-22 neels unsigned int right_section_len = right_idx - right_pos;
466 3b0f3d61 2020-01-22 neels
467 3b0f3d61 2020-01-22 neels if (left_section_len && right_section_len) {
468 0d27172a 2020-05-06 neels /* Record an unsolved chunk, the caller will apply
469 0d27172a 2020-05-06 neels * inner_algo() on this chunk. */
470 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, false,
471 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
472 0d27172a 2020-05-06 neels right_atom,
473 0d27172a 2020-05-06 neels right_section_len))
474 3b0f3d61 2020-01-22 neels goto return_rc;
475 3b0f3d61 2020-01-22 neels } else if (left_section_len && !right_section_len) {
476 0d27172a 2020-05-06 neels /* Only left atoms and none on the right, they form a
477 0d27172a 2020-05-06 neels * "minus" chunk, then. */
478 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
479 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
480 3b0f3d61 2020-01-22 neels right_atom, 0))
481 3b0f3d61 2020-01-22 neels goto return_rc;
482 3b0f3d61 2020-01-22 neels } else if (!left_section_len && right_section_len) {
483 0d27172a 2020-05-06 neels /* No left atoms, only atoms on the right, they form a
484 0d27172a 2020-05-06 neels * "plus" chunk, then. */
485 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
486 3b0f3d61 2020-01-22 neels left_atom, 0,
487 3b0f3d61 2020-01-22 neels right_atom, right_section_len))
488 3b0f3d61 2020-01-22 neels goto return_rc;
489 3b0f3d61 2020-01-22 neels }
490 0d27172a 2020-05-06 neels /* else: left_section_len == 0 and right_section_len == 0, i.e.
491 0d27172a 2020-05-06 neels * nothing here. */
492 3b0f3d61 2020-01-22 neels
493 0d27172a 2020-05-06 neels /* The atom found to match on both sides forms a chunk of equals
494 0d27172a 2020-05-06 neels * on each side. In the very last iteration of this loop, there
495 0d27172a 2020-05-06 neels * is no matching atom, we were just cleaning out the remaining
496 0d27172a 2020-05-06 neels * lines. */
497 3b0f3d61 2020-01-22 neels if (atom) {
498 0d27172a 2020-05-06 neels void *ok;
499 0d27172a 2020-05-06 neels ok = diff_state_add_chunk(state, true,
500 0d27172a 2020-05-06 neels left->atoms.head
501 0d27172a 2020-05-06 neels + atom->patience.identical_lines.start,
502 d362ea2e 2020-07-25 stsp diff_range_len(&atom->patience.identical_lines),
503 0d27172a 2020-05-06 neels right->atoms.head
504 0d27172a 2020-05-06 neels + atom_r->patience.identical_lines.start,
505 d362ea2e 2020-07-25 stsp diff_range_len(&atom_r->patience.identical_lines));
506 0d27172a 2020-05-06 neels if (!ok)
507 3b0f3d61 2020-01-22 neels goto return_rc;
508 bb7fb738 2020-01-27 neels left_pos = atom->patience.identical_lines.end;
509 bb7fb738 2020-01-27 neels right_pos = atom_r->patience.identical_lines.end;
510 bb7fb738 2020-01-27 neels } else {
511 bb7fb738 2020-01-27 neels left_pos = left_idx + 1;
512 bb7fb738 2020-01-27 neels right_pos = right_idx + 1;
513 3b0f3d61 2020-01-22 neels }
514 0d27172a 2020-05-06 neels debug("end of iteration %u left_pos %u left_idx %u"
515 0d27172a 2020-05-06 neels " right_pos %u right_idx %u\n",
516 bb7fb738 2020-01-27 neels i, left_pos, left_idx, right_pos, right_idx);
517 3b0f3d61 2020-01-22 neels }
518 3b0f3d61 2020-01-22 neels debug("** END %s\n", __func__);
519 3b0f3d61 2020-01-22 neels
520 3b0f3d61 2020-01-22 neels rc = DIFF_RC_OK;
521 3b0f3d61 2020-01-22 neels
522 3b0f3d61 2020-01-22 neels return_rc:
523 3b0f3d61 2020-01-22 neels free(lcs);
524 3b0f3d61 2020-01-22 neels return rc;
525 3b0f3d61 2020-01-22 neels }