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 6c8c5d3f 2020-10-12 neels /* Per-atom state for the Patience Diff algorithm */
33 6c8c5d3f 2020-10-12 neels struct atom_patience {
34 6c8c5d3f 2020-10-12 neels bool unique_in_both;
35 6c8c5d3f 2020-10-12 neels struct diff_atom *pos_in_other;
36 6c8c5d3f 2020-10-12 neels struct diff_atom *prev_stack;
37 6c8c5d3f 2020-10-12 neels struct diff_range identical_lines;
38 6c8c5d3f 2020-10-12 neels };
39 6c8c5d3f 2020-10-12 neels
40 6c8c5d3f 2020-10-12 neels /* A diff_atom has a backpointer to the root diff_data. That points to the
41 6c8c5d3f 2020-10-12 neels * current diff_data, a possibly smaller section of the root. That current
42 6c8c5d3f 2020-10-12 neels * diff_data->algo_data is a pointer to an array of struct atom_patience. The
43 6c8c5d3f 2020-10-12 neels * atom's index in current diff_data gives the index in the atom_patience array.
44 6c8c5d3f 2020-10-12 neels */
45 6c8c5d3f 2020-10-12 neels #define PATIENCE(ATOM) \
46 6c8c5d3f 2020-10-12 neels (((struct atom_patience*)((ATOM)->root->current->algo_data))\
47 6c8c5d3f 2020-10-12 neels [diff_atom_idx((ATOM)->root->current, ATOM)])
48 6c8c5d3f 2020-10-12 neels
49 60b6e08b 2020-10-12 neels int diff_atoms_qsort_compar(const void *_a, const void *_b)
50 3b0f3d61 2020-01-22 neels {
51 60b6e08b 2020-10-12 neels const struct diff_atom *a = *(struct diff_atom**)_a;
52 60b6e08b 2020-10-12 neels const struct diff_atom *b = *(struct diff_atom**)_b;
53 60b6e08b 2020-10-12 neels int cmp;
54 65688edf 2020-10-15 neels int rc = 0;
55 60b6e08b 2020-10-12 neels
56 227cd87e 2020-10-15 stsp /* If there's been an error (e.g. I/O error) in a previous compar, we
57 227cd87e 2020-10-15 stsp * have no way to abort the qsort but just report the rc and stop
58 227cd87e 2020-10-15 stsp * comparing. Make sure to catch errors on either side. If atoms are
59 227cd87e 2020-10-15 stsp * from more than one diff_data, make sure the error, if any, spreads
60 227cd87e 2020-10-15 stsp * to all of them, so we can cut short all future comparisons. */
61 65688edf 2020-10-15 neels if (a->root->err)
62 65688edf 2020-10-15 neels rc = a->root->err;
63 65688edf 2020-10-15 neels if (b->root->err)
64 65688edf 2020-10-15 neels rc = b->root->err;
65 65688edf 2020-10-15 neels if (rc) {
66 60b6e08b 2020-10-12 neels a->root->err = rc;
67 60b6e08b 2020-10-12 neels b->root->err = rc;
68 65688edf 2020-10-15 neels /* just return 'equal' to not swap more positions */
69 60b6e08b 2020-10-12 neels return 0;
70 3b0f3d61 2020-01-22 neels }
71 3b0f3d61 2020-01-22 neels
72 60b6e08b 2020-10-12 neels /* Sort by the simplistic hash */
73 60b6e08b 2020-10-12 neels if (a->hash < b->hash)
74 60b6e08b 2020-10-12 neels return -1;
75 60b6e08b 2020-10-12 neels if (a->hash > b->hash)
76 60b6e08b 2020-10-12 neels return 1;
77 3b0f3d61 2020-01-22 neels
78 60b6e08b 2020-10-12 neels /* If hashes are the same, the lines may still differ. Do a full cmp. */
79 60b6e08b 2020-10-12 neels rc = diff_atom_cmp(&cmp, a, b);
80 60b6e08b 2020-10-12 neels
81 60b6e08b 2020-10-12 neels if (rc) {
82 60b6e08b 2020-10-12 neels /* Mark the I/O error so that the caller can find out about it.
83 60b6e08b 2020-10-12 neels * For the case atoms are from more than one diff_data, mark in
84 60b6e08b 2020-10-12 neels * both. */
85 60b6e08b 2020-10-12 neels a->root->err = rc;
86 60b6e08b 2020-10-12 neels if (a->root != b->root)
87 60b6e08b 2020-10-12 neels b->root->err = rc;
88 60b6e08b 2020-10-12 neels return 0;
89 3b0f3d61 2020-01-22 neels }
90 60b6e08b 2020-10-12 neels
91 60b6e08b 2020-10-12 neels return cmp;
92 3b0f3d61 2020-01-22 neels }
93 3b0f3d61 2020-01-22 neels
94 60b6e08b 2020-10-12 neels /* Sort an array of struct diff_atom* in-place. */
95 60b6e08b 2020-10-12 neels static int diff_atoms_qsort(struct diff_atom *atoms[],
96 60b6e08b 2020-10-12 neels size_t atoms_count)
97 3b0f3d61 2020-01-22 neels {
98 60b6e08b 2020-10-12 neels qsort(atoms, atoms_count, sizeof(struct diff_atom*),
99 60b6e08b 2020-10-12 neels diff_atoms_qsort_compar);
100 60b6e08b 2020-10-12 neels return atoms[0]->root->err;
101 60b6e08b 2020-10-12 neels }
102 3b0f3d61 2020-01-22 neels
103 60b6e08b 2020-10-12 neels static int
104 60b6e08b 2020-10-12 neels diff_atoms_mark_unique_in_both_by_qsort(struct diff_data *left,
105 60b6e08b 2020-10-12 neels struct diff_data *right,
106 60b6e08b 2020-10-12 neels unsigned int *unique_in_both_count_p)
107 60b6e08b 2020-10-12 neels {
108 60b6e08b 2020-10-12 neels struct diff_atom *a;
109 60b6e08b 2020-10-12 neels struct diff_atom *b;
110 60b6e08b 2020-10-12 neels struct diff_atom **all_atoms =
111 60b6e08b 2020-10-12 neels malloc((left->atoms.len + right->atoms.len)
112 60b6e08b 2020-10-12 neels * sizeof(struct diff_atom*));
113 60b6e08b 2020-10-12 neels unsigned int len = 0;
114 60b6e08b 2020-10-12 neels unsigned int i;
115 60b6e08b 2020-10-12 neels unsigned int unique_in_both_count = 0;
116 60b6e08b 2020-10-12 neels int rc;
117 6c8c5d3f 2020-10-12 neels left->err = 0;
118 6c8c5d3f 2020-10-12 neels right->err = 0;
119 60b6e08b 2020-10-12 neels left->root->err = 0;
120 60b6e08b 2020-10-12 neels right->root->err = 0;
121 60b6e08b 2020-10-12 neels diff_data_foreach_atom(a, left) {
122 60b6e08b 2020-10-12 neels all_atoms[len++] = a;
123 60b6e08b 2020-10-12 neels }
124 60b6e08b 2020-10-12 neels diff_data_foreach_atom(b, right) {
125 60b6e08b 2020-10-12 neels all_atoms[len++] = b;
126 60b6e08b 2020-10-12 neels }
127 60b6e08b 2020-10-12 neels
128 60b6e08b 2020-10-12 neels rc = diff_atoms_qsort(all_atoms, len);
129 60b6e08b 2020-10-12 neels if (rc)
130 60b6e08b 2020-10-12 neels goto free_and_exit;
131 60b6e08b 2020-10-12 neels
132 60b6e08b 2020-10-12 neels /* Now we have a sorted array of atom pointers. All similar lines are
133 60b6e08b 2020-10-12 neels * adjacent. Walk through the array and mark those that are unique on
134 60b6e08b 2020-10-12 neels * each side, but exist once in both sources. */
135 60b6e08b 2020-10-12 neels for (i = 0; i < len; i++) {
136 60b6e08b 2020-10-12 neels bool same;
137 60b6e08b 2020-10-12 neels unsigned int j;
138 60b6e08b 2020-10-12 neels unsigned int count_first_side = 1;
139 60b6e08b 2020-10-12 neels unsigned int count_other_side = 0;
140 60b6e08b 2020-10-12 neels a = all_atoms[i];
141 60b6e08b 2020-10-12 neels
142 60b6e08b 2020-10-12 neels for (j = i+1; j < len; j++) {
143 60b6e08b 2020-10-12 neels b = all_atoms[j];
144 60b6e08b 2020-10-12 neels rc = diff_atom_same(&same, a, b);
145 60b6e08b 2020-10-12 neels if (rc)
146 60b6e08b 2020-10-12 neels goto free_and_exit;
147 60b6e08b 2020-10-12 neels if (!same)
148 3b0f3d61 2020-01-22 neels break;
149 60b6e08b 2020-10-12 neels /* A following atom is the same. See on which side the
150 60b6e08b 2020-10-12 neels * repetition counts. */
151 60b6e08b 2020-10-12 neels if (a->root == b->root)
152 60b6e08b 2020-10-12 neels count_first_side ++;
153 60b6e08b 2020-10-12 neels else
154 60b6e08b 2020-10-12 neels count_other_side ++;
155 3b0f3d61 2020-01-22 neels }
156 3b0f3d61 2020-01-22 neels
157 60b6e08b 2020-10-12 neels /* Counted a section of similar atoms, put the results back to
158 60b6e08b 2020-10-12 neels * the atoms. */
159 60b6e08b 2020-10-12 neels if ((count_first_side == 1)
160 60b6e08b 2020-10-12 neels && (count_other_side == 1)) {
161 60b6e08b 2020-10-12 neels b = all_atoms[i+1];
162 6c8c5d3f 2020-10-12 neels PATIENCE(a).unique_in_both = true;
163 6c8c5d3f 2020-10-12 neels PATIENCE(a).pos_in_other = b;
164 6c8c5d3f 2020-10-12 neels PATIENCE(b).unique_in_both = true;
165 6c8c5d3f 2020-10-12 neels PATIENCE(b).pos_in_other = a;
166 60b6e08b 2020-10-12 neels unique_in_both_count++;
167 3b0f3d61 2020-01-22 neels }
168 3b0f3d61 2020-01-22 neels }
169 60b6e08b 2020-10-12 neels *unique_in_both_count_p = unique_in_both_count;
170 60b6e08b 2020-10-12 neels rc = 0;
171 60b6e08b 2020-10-12 neels free_and_exit:
172 60b6e08b 2020-10-12 neels free(all_atoms);
173 60b6e08b 2020-10-12 neels return rc;
174 3b0f3d61 2020-01-22 neels }
175 3b0f3d61 2020-01-22 neels
176 44cf4950 2020-09-20 neels static int
177 0d27172a 2020-05-06 neels diff_atoms_swallow_identical_neighbors(struct diff_data *left,
178 0d27172a 2020-05-06 neels struct diff_data *right,
179 61a7b578 2020-05-06 neels unsigned int *unique_in_both_count)
180 bb7fb738 2020-01-27 neels {
181 0d27172a 2020-05-06 neels debug("trivially combine identical lines"
182 0d27172a 2020-05-06 neels " around unique_in_both lines\n");
183 3b0f3d61 2020-01-22 neels
184 bb7fb738 2020-01-27 neels unsigned int l_idx;
185 bb7fb738 2020-01-27 neels unsigned int next_l_idx;
186 bb7fb738 2020-01-27 neels unsigned int l_min = 0;
187 bb7fb738 2020-01-27 neels unsigned int r_min = 0;
188 bb7fb738 2020-01-27 neels for (l_idx = 0; l_idx < left->atoms.len; l_idx = next_l_idx) {
189 bb7fb738 2020-01-27 neels next_l_idx = l_idx + 1;
190 bb7fb738 2020-01-27 neels struct diff_atom *l = &left->atoms.head[l_idx];
191 bb7fb738 2020-01-27 neels
192 6c8c5d3f 2020-10-12 neels if (!PATIENCE(l).unique_in_both)
193 bb7fb738 2020-01-27 neels continue;
194 bb7fb738 2020-01-27 neels
195 bb7fb738 2020-01-27 neels debug("check identical lines around ");
196 bb7fb738 2020-01-27 neels debug_dump_atom(left, right, l);
197 bb7fb738 2020-01-27 neels
198 6c8c5d3f 2020-10-12 neels unsigned int r_idx = diff_atom_idx(right, PATIENCE(l).pos_in_other);
199 bb7fb738 2020-01-27 neels
200 d362ea2e 2020-07-25 stsp struct diff_range identical_l;
201 d362ea2e 2020-07-25 stsp struct diff_range identical_r;
202 bb7fb738 2020-01-27 neels
203 bb7fb738 2020-01-27 neels /* Swallow upwards.
204 0d27172a 2020-05-06 neels * Each common-unique line swallows identical lines upwards and
205 0d27172a 2020-05-06 neels * downwards.
206 0d27172a 2020-05-06 neels * All common-unique lines that were part of the identical lines
207 0d27172a 2020-05-06 neels * following below were already swallowed in the previous
208 0d27172a 2020-05-06 neels * iteration, so we will never hit another common-unique line
209 0d27172a 2020-05-06 neels * above. */
210 bb7fb738 2020-01-27 neels for (identical_l.start = l_idx, identical_r.start = r_idx;
211 b3fb4686 2020-09-20 neels identical_l.start > l_min && identical_r.start > r_min;
212 b3fb4686 2020-09-20 neels identical_l.start--, identical_r.start--) {
213 b3fb4686 2020-09-20 neels bool same;
214 b3fb4686 2020-09-20 neels int r = diff_atom_same(&same,
215 0d27172a 2020-05-06 neels &left->atoms.head[identical_l.start - 1],
216 0d27172a 2020-05-06 neels &right->atoms.head[identical_r.start - 1]);
217 b3fb4686 2020-09-20 neels if (r)
218 44cf4950 2020-09-20 neels return r;
219 b3fb4686 2020-09-20 neels if (!same)
220 b3fb4686 2020-09-20 neels break;
221 b3fb4686 2020-09-20 neels }
222 bb7fb738 2020-01-27 neels
223 bb7fb738 2020-01-27 neels /* Swallow downwards */
224 bb7fb738 2020-01-27 neels for (identical_l.end = l_idx + 1, identical_r.end = r_idx + 1;
225 bb7fb738 2020-01-27 neels identical_l.end < left->atoms.len
226 b3fb4686 2020-09-20 neels && identical_r.end < right->atoms.len;
227 bb7fb738 2020-01-27 neels identical_l.end++, identical_r.end++,
228 bb7fb738 2020-01-27 neels next_l_idx++) {
229 0d27172a 2020-05-06 neels struct diff_atom *l_end;
230 0d27172a 2020-05-06 neels struct diff_atom *r_end;
231 b3fb4686 2020-09-20 neels bool same;
232 b3fb4686 2020-09-20 neels int r = diff_atom_same(&same,
233 b3fb4686 2020-09-20 neels &left->atoms.head[identical_l.end],
234 b3fb4686 2020-09-20 neels &right->atoms.head[identical_r.end]);
235 b3fb4686 2020-09-20 neels if (r)
236 44cf4950 2020-09-20 neels return r;
237 b3fb4686 2020-09-20 neels if (!same)
238 b3fb4686 2020-09-20 neels break;
239 0d27172a 2020-05-06 neels l_end = &left->atoms.head[identical_l.end];
240 ab699b5c 2020-05-12 stsp r_end = &right->atoms.head[identical_r.end];
241 6c8c5d3f 2020-10-12 neels if (!PATIENCE(l_end).unique_in_both)
242 0d27172a 2020-05-06 neels continue;
243 0d27172a 2020-05-06 neels /* Part of a chunk of identical lines, remove from
244 0d27172a 2020-05-06 neels * listing of unique_in_both lines */
245 6c8c5d3f 2020-10-12 neels PATIENCE(l_end).unique_in_both = false;
246 6c8c5d3f 2020-10-12 neels PATIENCE(r_end).unique_in_both = false;
247 0d27172a 2020-05-06 neels (*unique_in_both_count)--;
248 bb7fb738 2020-01-27 neels }
249 bb7fb738 2020-01-27 neels
250 6c8c5d3f 2020-10-12 neels PATIENCE(l).identical_lines = identical_l;
251 6c8c5d3f 2020-10-12 neels PATIENCE(PATIENCE(l).pos_in_other).identical_lines =
252 0d27172a 2020-05-06 neels identical_r;
253 bb7fb738 2020-01-27 neels
254 bb7fb738 2020-01-27 neels l_min = identical_l.end;
255 bb7fb738 2020-01-27 neels r_min = identical_r.end;
256 bb7fb738 2020-01-27 neels
257 6c8c5d3f 2020-10-12 neels if (!diff_range_empty(&PATIENCE(l).identical_lines)) {
258 0d27172a 2020-05-06 neels debug("common-unique line at l=%u r=%u swallowed"
259 0d27172a 2020-05-06 neels " identical lines l=%u-%u r=%u-%u\n",
260 bb7fb738 2020-01-27 neels l_idx, r_idx,
261 bb7fb738 2020-01-27 neels identical_l.start, identical_l.end,
262 bb7fb738 2020-01-27 neels identical_r.start, identical_r.end);
263 bb7fb738 2020-01-27 neels }
264 bb7fb738 2020-01-27 neels debug("next_l_idx = %u\n", next_l_idx);
265 bb7fb738 2020-01-27 neels }
266 44cf4950 2020-09-20 neels return 0;
267 bb7fb738 2020-01-27 neels }
268 bb7fb738 2020-01-27 neels
269 0d27172a 2020-05-06 neels /* binary search to find the stack to put this atom "card" on. */
270 0d27172a 2020-05-06 neels static int
271 0d27172a 2020-05-06 neels find_target_stack(struct diff_atom *atom,
272 0d27172a 2020-05-06 neels struct diff_atom **patience_stacks,
273 0d27172a 2020-05-06 neels unsigned int patience_stacks_count)
274 0d27172a 2020-05-06 neels {
275 0d27172a 2020-05-06 neels unsigned int lo = 0;
276 0d27172a 2020-05-06 neels unsigned int hi = patience_stacks_count;
277 0d27172a 2020-05-06 neels while (lo < hi) {
278 0d27172a 2020-05-06 neels unsigned int mid = (lo + hi) >> 1;
279 0d27172a 2020-05-06 neels
280 6c8c5d3f 2020-10-12 neels if (PATIENCE(patience_stacks[mid]).pos_in_other
281 6c8c5d3f 2020-10-12 neels < PATIENCE(atom).pos_in_other)
282 0d27172a 2020-05-06 neels lo = mid + 1;
283 0d27172a 2020-05-06 neels else
284 0d27172a 2020-05-06 neels hi = mid;
285 0d27172a 2020-05-06 neels }
286 0d27172a 2020-05-06 neels return lo;
287 0d27172a 2020-05-06 neels }
288 0d27172a 2020-05-06 neels
289 0d27172a 2020-05-06 neels /* Among the lines that appear exactly once in each side, find the longest
290 0d27172a 2020-05-06 neels * streak that appear in both files in the same order (with other stuff allowed
291 0d27172a 2020-05-06 neels * to interleave). Use patience sort for that, as in the Patience Diff
292 0d27172a 2020-05-06 neels * algorithm.
293 0d27172a 2020-05-06 neels * See https://bramcohen.livejournal.com/73318.html and, for a much more
294 0d27172a 2020-05-06 neels * detailed explanation,
295 3b0f3d61 2020-01-22 neels * https://blog.jcoglan.com/2017/09/19/the-patience-diff-algorithm/ */
296 3e6cba3a 2020-08-13 stsp int
297 0d27172a 2020-05-06 neels diff_algo_patience(const struct diff_algo_config *algo_config,
298 0d27172a 2020-05-06 neels struct diff_state *state)
299 3b0f3d61 2020-01-22 neels {
300 44cf4950 2020-09-20 neels int rc;
301 3b0f3d61 2020-01-22 neels struct diff_data *left = &state->left;
302 3b0f3d61 2020-01-22 neels struct diff_data *right = &state->right;
303 6c8c5d3f 2020-10-12 neels struct atom_patience *atom_patience_left =
304 6c8c5d3f 2020-10-12 neels calloc(left->atoms.len, sizeof(struct atom_patience));
305 6c8c5d3f 2020-10-12 neels struct atom_patience *atom_patience_right =
306 6c8c5d3f 2020-10-12 neels calloc(right->atoms.len, sizeof(struct atom_patience));
307 3b0f3d61 2020-01-22 neels unsigned int unique_in_both_count;
308 6c8c5d3f 2020-10-12 neels struct diff_atom **lcs = NULL;
309 3b0f3d61 2020-01-22 neels
310 3b0f3d61 2020-01-22 neels debug("\n** %s\n", __func__);
311 3b0f3d61 2020-01-22 neels
312 6c8c5d3f 2020-10-12 neels left->root->current = left;
313 6c8c5d3f 2020-10-12 neels right->root->current = right;
314 6c8c5d3f 2020-10-12 neels left->algo_data = atom_patience_left;
315 6c8c5d3f 2020-10-12 neels right->algo_data = atom_patience_right;
316 6c8c5d3f 2020-10-12 neels
317 0d27172a 2020-05-06 neels /* Find those lines that appear exactly once in 'left' and exactly once
318 0d27172a 2020-05-06 neels * in 'right'. */
319 60b6e08b 2020-10-12 neels rc = diff_atoms_mark_unique_in_both_by_qsort(left, right,
320 60b6e08b 2020-10-12 neels &unique_in_both_count);
321 44cf4950 2020-09-20 neels if (rc)
322 6c8c5d3f 2020-10-12 neels goto free_and_exit;
323 3b0f3d61 2020-01-22 neels
324 3b0f3d61 2020-01-22 neels debug("unique_in_both_count %u\n", unique_in_both_count);
325 3b0f3d61 2020-01-22 neels debug("left:\n");
326 3b0f3d61 2020-01-22 neels debug_dump(left);
327 3b0f3d61 2020-01-22 neels debug("right:\n");
328 3b0f3d61 2020-01-22 neels debug_dump(right);
329 3b0f3d61 2020-01-22 neels
330 3b0f3d61 2020-01-22 neels if (!unique_in_both_count) {
331 0d27172a 2020-05-06 neels /* Cannot apply Patience, tell the caller to use fallback_algo
332 0d27172a 2020-05-06 neels * instead. */
333 6c8c5d3f 2020-10-12 neels rc = DIFF_RC_USE_DIFF_ALGO_FALLBACK;
334 6c8c5d3f 2020-10-12 neels goto free_and_exit;
335 3b0f3d61 2020-01-22 neels }
336 3b0f3d61 2020-01-22 neels
337 44cf4950 2020-09-20 neels rc = diff_atoms_swallow_identical_neighbors(left, right,
338 44cf4950 2020-09-20 neels &unique_in_both_count);
339 44cf4950 2020-09-20 neels if (rc)
340 6c8c5d3f 2020-10-12 neels goto free_and_exit;
341 bb7fb738 2020-01-27 neels debug("After swallowing identical neighbors: unique_in_both = %u\n",
342 bb7fb738 2020-01-27 neels unique_in_both_count);
343 bb7fb738 2020-01-27 neels
344 44cf4950 2020-09-20 neels rc = ENOMEM;
345 44cf4950 2020-09-20 neels
346 0d27172a 2020-05-06 neels /* An array of Longest Common Sequence is the result of the below
347 0d27172a 2020-05-06 neels * subscope: */
348 3b0f3d61 2020-01-22 neels unsigned int lcs_count = 0;
349 3b0f3d61 2020-01-22 neels struct diff_atom *lcs_tail = NULL;
350 3b0f3d61 2020-01-22 neels
351 3b0f3d61 2020-01-22 neels {
352 0d27172a 2020-05-06 neels /* This subscope marks the lifetime of the atom_pointers
353 0d27172a 2020-05-06 neels * allocation */
354 3b0f3d61 2020-01-22 neels
355 3b0f3d61 2020-01-22 neels /* One chunk of storage for atom pointers */
356 0d27172a 2020-05-06 neels struct diff_atom **atom_pointers;
357 0d27172a 2020-05-06 neels atom_pointers = recallocarray(NULL, 0, unique_in_both_count * 2,
358 0d27172a 2020-05-06 neels sizeof(struct diff_atom*));
359 3b0f3d61 2020-01-22 neels
360 0d27172a 2020-05-06 neels /* Half for the list of atoms that still need to be put on
361 0d27172a 2020-05-06 neels * stacks */
362 3b0f3d61 2020-01-22 neels struct diff_atom **uniques = atom_pointers;
363 3b0f3d61 2020-01-22 neels
364 0d27172a 2020-05-06 neels /* Half for the patience sort state's "card stacks" -- we
365 0d27172a 2020-05-06 neels * remember only each stack's topmost "card" */
366 0d27172a 2020-05-06 neels struct diff_atom **patience_stacks;
367 0d27172a 2020-05-06 neels patience_stacks = atom_pointers + unique_in_both_count;
368 3b0f3d61 2020-01-22 neels unsigned int patience_stacks_count = 0;
369 3b0f3d61 2020-01-22 neels
370 3b0f3d61 2020-01-22 neels /* Take all common, unique items from 'left' ... */
371 3b0f3d61 2020-01-22 neels
372 3b0f3d61 2020-01-22 neels struct diff_atom *atom;
373 3b0f3d61 2020-01-22 neels struct diff_atom **uniques_end = uniques;
374 3b0f3d61 2020-01-22 neels diff_data_foreach_atom(atom, left) {
375 6c8c5d3f 2020-10-12 neels if (!PATIENCE(atom).unique_in_both)
376 3b0f3d61 2020-01-22 neels continue;
377 3b0f3d61 2020-01-22 neels *uniques_end = atom;
378 3b0f3d61 2020-01-22 neels uniques_end++;
379 3b0f3d61 2020-01-22 neels }
380 3b0f3d61 2020-01-22 neels
381 3b0f3d61 2020-01-22 neels /* ...and sort them to the order found in 'right'.
382 0d27172a 2020-05-06 neels * The idea is to find the leftmost stack that has a higher line
383 0d27172a 2020-05-06 neels * number and add it to the stack's top.
384 0d27172a 2020-05-06 neels * If there is no such stack, open a new one on the right. The
385 0d27172a 2020-05-06 neels * line number is derived from the atom*, which are array items
386 0d27172a 2020-05-06 neels * and hence reflect the relative position in the source file.
387 0d27172a 2020-05-06 neels * So we got the common-uniques from 'left' and sort them
388 6c8c5d3f 2020-10-12 neels * according to PATIENCE(atom).pos_in_other. */
389 3b0f3d61 2020-01-22 neels unsigned int i;
390 3b0f3d61 2020-01-22 neels for (i = 0; i < unique_in_both_count; i++) {
391 3b0f3d61 2020-01-22 neels atom = uniques[i];
392 3b0f3d61 2020-01-22 neels unsigned int target_stack;
393 0d27172a 2020-05-06 neels target_stack = find_target_stack(atom, patience_stacks,
394 0d27172a 2020-05-06 neels patience_stacks_count);
395 3b0f3d61 2020-01-22 neels assert(target_stack <= patience_stacks_count);
396 3b0f3d61 2020-01-22 neels patience_stacks[target_stack] = atom;
397 3b0f3d61 2020-01-22 neels if (target_stack == patience_stacks_count)
398 3b0f3d61 2020-01-22 neels patience_stacks_count++;
399 3b0f3d61 2020-01-22 neels
400 0d27172a 2020-05-06 neels /* Record a back reference to the next stack on the
401 0d27172a 2020-05-06 neels * left, which will form the final longest sequence
402 3b0f3d61 2020-01-22 neels * later. */
403 6c8c5d3f 2020-10-12 neels PATIENCE(atom).prev_stack = target_stack ?
404 0d27172a 2020-05-06 neels patience_stacks[target_stack - 1] : NULL;
405 3b0f3d61 2020-01-22 neels
406 3b0f3d61 2020-01-22 neels }
407 3b0f3d61 2020-01-22 neels
408 0d27172a 2020-05-06 neels /* backtrace through prev_stack references to form the final
409 0d27172a 2020-05-06 neels * longest common sequence */
410 3b0f3d61 2020-01-22 neels lcs_tail = patience_stacks[patience_stacks_count - 1];
411 3b0f3d61 2020-01-22 neels lcs_count = patience_stacks_count;
412 3b0f3d61 2020-01-22 neels
413 0d27172a 2020-05-06 neels /* uniques and patience_stacks are no longer needed.
414 6c8c5d3f 2020-10-12 neels * Backpointers are in PATIENCE(atom).prev_stack */
415 3b0f3d61 2020-01-22 neels free(atom_pointers);
416 3b0f3d61 2020-01-22 neels }
417 3b0f3d61 2020-01-22 neels
418 3b0f3d61 2020-01-22 neels lcs = recallocarray(NULL, 0, lcs_count, sizeof(struct diff_atom*));
419 3b0f3d61 2020-01-22 neels struct diff_atom **lcs_backtrace_pos = &lcs[lcs_count - 1];
420 3b0f3d61 2020-01-22 neels struct diff_atom *atom;
421 6c8c5d3f 2020-10-12 neels for (atom = lcs_tail; atom; atom = PATIENCE(atom).prev_stack, lcs_backtrace_pos--) {
422 3b0f3d61 2020-01-22 neels assert(lcs_backtrace_pos >= lcs);
423 3b0f3d61 2020-01-22 neels *lcs_backtrace_pos = atom;
424 3b0f3d61 2020-01-22 neels }
425 3b0f3d61 2020-01-22 neels
426 3b0f3d61 2020-01-22 neels unsigned int i;
427 3b0f3d61 2020-01-22 neels if (DEBUG) {
428 3b0f3d61 2020-01-22 neels debug("\npatience LCS:\n");
429 3b0f3d61 2020-01-22 neels for (i = 0; i < lcs_count; i++) {
430 3b0f3d61 2020-01-22 neels debug_dump_atom(left, right, lcs[i]);
431 3b0f3d61 2020-01-22 neels }
432 3b0f3d61 2020-01-22 neels }
433 3b0f3d61 2020-01-22 neels
434 3b0f3d61 2020-01-22 neels
435 0d27172a 2020-05-06 neels /* TODO: For each common-unique line found (now listed in lcs), swallow
436 0d27172a 2020-05-06 neels * lines upwards and downwards that are identical on each side. Requires
437 0d27172a 2020-05-06 neels * a way to represent atoms being glued to adjacent atoms. */
438 3b0f3d61 2020-01-22 neels
439 3b0f3d61 2020-01-22 neels debug("\ntraverse LCS, possibly recursing:\n");
440 3b0f3d61 2020-01-22 neels
441 0d27172a 2020-05-06 neels /* Now we have pinned positions in both files at which it makes sense to
442 0d27172a 2020-05-06 neels * divide the diff problem into smaller chunks. Go into the next round:
443 0d27172a 2020-05-06 neels * look at each section in turn, trying to again find common-unique
444 0d27172a 2020-05-06 neels * lines in those smaller sections. As soon as no more are found, the
445 0d27172a 2020-05-06 neels * remaining smaller sections are solved by Myers. */
446 3b0f3d61 2020-01-22 neels unsigned int left_pos = 0;
447 3b0f3d61 2020-01-22 neels unsigned int right_pos = 0;
448 3b0f3d61 2020-01-22 neels for (i = 0; i <= lcs_count; i++) {
449 3b0f3d61 2020-01-22 neels struct diff_atom *atom;
450 bb7fb738 2020-01-27 neels struct diff_atom *atom_r;
451 3b0f3d61 2020-01-22 neels unsigned int left_idx;
452 3b0f3d61 2020-01-22 neels unsigned int right_idx;
453 3b0f3d61 2020-01-22 neels
454 3b0f3d61 2020-01-22 neels if (i < lcs_count) {
455 3b0f3d61 2020-01-22 neels atom = lcs[i];
456 6c8c5d3f 2020-10-12 neels atom_r = PATIENCE(atom).pos_in_other;
457 2a1b94d0 2020-09-26 stsp debug("lcs[%u] = left[%u] = right[%u]\n", i,
458 bb7fb738 2020-01-27 neels diff_atom_idx(left, atom), diff_atom_idx(right, atom_r));
459 6c8c5d3f 2020-10-12 neels left_idx = PATIENCE(atom).identical_lines.start;
460 6c8c5d3f 2020-10-12 neels right_idx = PATIENCE(atom_r).identical_lines.start;
461 bb7fb738 2020-01-27 neels debug(" identical lines l %u-%u r %u-%u\n",
462 6c8c5d3f 2020-10-12 neels PATIENCE(atom).identical_lines.start, PATIENCE(atom).identical_lines.end,
463 6c8c5d3f 2020-10-12 neels PATIENCE(atom_r).identical_lines.start, PATIENCE(atom_r).identical_lines.end);
464 3b0f3d61 2020-01-22 neels } else {
465 3b0f3d61 2020-01-22 neels atom = NULL;
466 bb7fb738 2020-01-27 neels atom_r = NULL;
467 3b0f3d61 2020-01-22 neels left_idx = left->atoms.len;
468 3b0f3d61 2020-01-22 neels right_idx = right->atoms.len;
469 3b0f3d61 2020-01-22 neels }
470 3b0f3d61 2020-01-22 neels
471 0d27172a 2020-05-06 neels /* 'atom' now marks an atom that matches on both sides according
472 0d27172a 2020-05-06 neels * to patience-diff (a common-unique identical atom in both
473 0d27172a 2020-05-06 neels * files).
474 0d27172a 2020-05-06 neels * Handle the section before and the atom itself; the section
475 0d27172a 2020-05-06 neels * after will be handled by the next loop iteration -- note that
476 0d27172a 2020-05-06 neels * i loops to last element + 1 ("i <= lcs_count"), so that there
477 0d27172a 2020-05-06 neels * will be another final iteration to pick up the last remaining
478 0d27172a 2020-05-06 neels * items after the last LCS atom.
479 3b0f3d61 2020-01-22 neels * The sections before might also be empty on left and/or right.
480 0d27172a 2020-05-06 neels * left_pos and right_pos mark the indexes of the first atoms
481 0d27172a 2020-05-06 neels * that have not yet been handled in the previous loop
482 0d27172a 2020-05-06 neels * iteration. left_idx and right_idx mark the indexes of the
483 0d27172a 2020-05-06 neels * matching atom on left and right, respectively. */
484 3b0f3d61 2020-01-22 neels
485 0d27172a 2020-05-06 neels debug("iteration %u left_pos %u left_idx %u"
486 0d27172a 2020-05-06 neels " right_pos %u right_idx %u\n",
487 3b0f3d61 2020-01-22 neels i, left_pos, left_idx, right_pos, right_idx);
488 3b0f3d61 2020-01-22 neels
489 3b0f3d61 2020-01-22 neels /* Section before the matching atom */
490 3b0f3d61 2020-01-22 neels struct diff_atom *left_atom = &left->atoms.head[left_pos];
491 3b0f3d61 2020-01-22 neels unsigned int left_section_len = left_idx - left_pos;
492 3b0f3d61 2020-01-22 neels
493 3b0f3d61 2020-01-22 neels struct diff_atom *right_atom = &(right->atoms.head[right_pos]);
494 3b0f3d61 2020-01-22 neels unsigned int right_section_len = right_idx - right_pos;
495 3b0f3d61 2020-01-22 neels
496 3b0f3d61 2020-01-22 neels if (left_section_len && right_section_len) {
497 0d27172a 2020-05-06 neels /* Record an unsolved chunk, the caller will apply
498 0d27172a 2020-05-06 neels * inner_algo() on this chunk. */
499 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, false,
500 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
501 0d27172a 2020-05-06 neels right_atom,
502 0d27172a 2020-05-06 neels right_section_len))
503 6c8c5d3f 2020-10-12 neels goto free_and_exit;
504 3b0f3d61 2020-01-22 neels } else if (left_section_len && !right_section_len) {
505 0d27172a 2020-05-06 neels /* Only left atoms and none on the right, they form a
506 0d27172a 2020-05-06 neels * "minus" chunk, then. */
507 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
508 3b0f3d61 2020-01-22 neels left_atom, left_section_len,
509 3b0f3d61 2020-01-22 neels right_atom, 0))
510 6c8c5d3f 2020-10-12 neels goto free_and_exit;
511 3b0f3d61 2020-01-22 neels } else if (!left_section_len && right_section_len) {
512 0d27172a 2020-05-06 neels /* No left atoms, only atoms on the right, they form a
513 0d27172a 2020-05-06 neels * "plus" chunk, then. */
514 3b0f3d61 2020-01-22 neels if (!diff_state_add_chunk(state, true,
515 3b0f3d61 2020-01-22 neels left_atom, 0,
516 3b0f3d61 2020-01-22 neels right_atom, right_section_len))
517 6c8c5d3f 2020-10-12 neels goto free_and_exit;
518 3b0f3d61 2020-01-22 neels }
519 0d27172a 2020-05-06 neels /* else: left_section_len == 0 and right_section_len == 0, i.e.
520 0d27172a 2020-05-06 neels * nothing here. */
521 3b0f3d61 2020-01-22 neels
522 0d27172a 2020-05-06 neels /* The atom found to match on both sides forms a chunk of equals
523 0d27172a 2020-05-06 neels * on each side. In the very last iteration of this loop, there
524 0d27172a 2020-05-06 neels * is no matching atom, we were just cleaning out the remaining
525 0d27172a 2020-05-06 neels * lines. */
526 3b0f3d61 2020-01-22 neels if (atom) {
527 0d27172a 2020-05-06 neels void *ok;
528 0d27172a 2020-05-06 neels ok = diff_state_add_chunk(state, true,
529 0d27172a 2020-05-06 neels left->atoms.head
530 6c8c5d3f 2020-10-12 neels + PATIENCE(atom).identical_lines.start,
531 6c8c5d3f 2020-10-12 neels diff_range_len(&PATIENCE(atom).identical_lines),
532 0d27172a 2020-05-06 neels right->atoms.head
533 6c8c5d3f 2020-10-12 neels + PATIENCE(atom_r).identical_lines.start,
534 6c8c5d3f 2020-10-12 neels diff_range_len(&PATIENCE(atom_r).identical_lines));
535 0d27172a 2020-05-06 neels if (!ok)
536 6c8c5d3f 2020-10-12 neels goto free_and_exit;
537 6c8c5d3f 2020-10-12 neels left_pos = PATIENCE(atom).identical_lines.end;
538 6c8c5d3f 2020-10-12 neels right_pos = PATIENCE(atom_r).identical_lines.end;
539 bb7fb738 2020-01-27 neels } else {
540 bb7fb738 2020-01-27 neels left_pos = left_idx + 1;
541 bb7fb738 2020-01-27 neels right_pos = right_idx + 1;
542 3b0f3d61 2020-01-22 neels }
543 0d27172a 2020-05-06 neels debug("end of iteration %u left_pos %u left_idx %u"
544 0d27172a 2020-05-06 neels " right_pos %u right_idx %u\n",
545 bb7fb738 2020-01-27 neels i, left_pos, left_idx, right_pos, right_idx);
546 3b0f3d61 2020-01-22 neels }
547 3b0f3d61 2020-01-22 neels debug("** END %s\n", __func__);
548 3b0f3d61 2020-01-22 neels
549 3b0f3d61 2020-01-22 neels rc = DIFF_RC_OK;
550 3b0f3d61 2020-01-22 neels
551 6c8c5d3f 2020-10-12 neels free_and_exit:
552 6c8c5d3f 2020-10-12 neels left->root->current = NULL;
553 6c8c5d3f 2020-10-12 neels right->root->current = NULL;
554 6c8c5d3f 2020-10-12 neels free(atom_patience_left);
555 6c8c5d3f 2020-10-12 neels free(atom_patience_right);
556 6c8c5d3f 2020-10-12 neels if (lcs)
557 6c8c5d3f 2020-10-12 neels free(lcs);
558 3b0f3d61 2020-01-22 neels return rc;
559 3b0f3d61 2020-01-22 neels }