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