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. */
4 3b0f3d61 2020-01-22 neels * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
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.
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.
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>
26 1dfba055 2020-10-07 stsp #include <arraylist.h>
27 1dfba055 2020-10-07 stsp #include <diff_main.h>
29 85ab4559 2020-09-22 stsp #include "diff_internal.h"
30 2a1b94d0 2020-09-26 stsp #include "diff_debug.h"
32 ca6fcbdc 2020-10-20 neels /* Algorithm to find unique lines:
33 ca6fcbdc 2020-10-20 neels * 0: stupidly iterate atoms
35 ca6fcbdc 2020-10-20 neels * 2: mergesort
37 1c7f8717 2020-10-22 neels #define UNIQUE_STRATEGY 1
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;
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;
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.
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)])
59 ca6fcbdc 2020-10-20 neels #if UNIQUE_STRATEGY == 0
61 ca6fcbdc 2020-10-20 neels /* Stupid iteration and comparison of all atoms */
63 ca6fcbdc 2020-10-20 neels diff_atoms_mark_unique(struct diff_data *d, unsigned int *unique_count)
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;
72 ca6fcbdc 2020-10-20 neels diff_data_foreach_atom(i, d) {
73 ca6fcbdc 2020-10-20 neels struct diff_atom *j;
75 ca6fcbdc 2020-10-20 neels if (!PATIENCE(i).unique_here)
78 ca6fcbdc 2020-10-20 neels diff_data_foreach_atom_from(i + 1, j, d) {
80 ca6fcbdc 2020-10-20 neels int r = diff_atom_same(&same, i, j);
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;
90 ca6fcbdc 2020-10-20 neels PATIENCE(j).unique_here = false;
91 ca6fcbdc 2020-10-20 neels PATIENCE(j).unique_in_both = false;
95 ca6fcbdc 2020-10-20 neels if (unique_count)
96 ca6fcbdc 2020-10-20 neels *unique_count = count;
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)
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;
112 ca6fcbdc 2020-10-20 neels r = diff_atoms_mark_unique(left, &unique_in_both);
115 ca6fcbdc 2020-10-20 neels r = diff_atoms_mark_unique(right, NULL);
119 ca6fcbdc 2020-10-20 neels debug("unique_in_both %u\n", unique_in_both);
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)
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);
132 ca6fcbdc 2020-10-20 neels if (!same)
134 ca6fcbdc 2020-10-20 neels if (!PATIENCE(j).unique_here) {
135 ca6fcbdc 2020-10-20 neels found_in_b = 2; /* or more */
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;
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);
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)
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;
164 ca6fcbdc 2020-10-20 neels if (!PATIENCE(j).unique_in_both)
166 ca6fcbdc 2020-10-20 neels r = diff_atom_same(&same, i, j);
169 ca6fcbdc 2020-10-20 neels if (!same)
171 ca6fcbdc 2020-10-20 neels found_in_a = true;
175 ca6fcbdc 2020-10-20 neels if (!found_in_a)
176 ca6fcbdc 2020-10-20 neels PATIENCE(i).unique_in_both = false;
179 ca6fcbdc 2020-10-20 neels if (unique_in_both_count)
180 ca6fcbdc 2020-10-20 neels *unique_in_both_count = unique_in_both;
184 ca6fcbdc 2020-10-20 neels #else /* UNIQUE_STRATEGY != 0 */
186 ca6fcbdc 2020-10-20 neels /* Use an optimized sorting algorithm (qsort, mergesort) to find unique lines */
188 ca6fcbdc 2020-10-20 neels int diff_atoms_compar(const void *_a, const void *_b)
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;
193 65688edf 2020-10-15 neels int rc = 0;
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;
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 */
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)
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);
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;
230 60b6e08b 2020-10-12 neels return cmp;
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)
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);
240 12c5bde7 2020-10-16 stsp mergesort(atoms, atoms_count, sizeof(struct diff_atom*),
241 ca6fcbdc 2020-10-20 neels diff_atoms_compar);
243 60b6e08b 2020-10-12 neels return atoms[0]->root->err;
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)
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;
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;
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;
270 60b6e08b 2020-10-12 neels diff_data_foreach_atom(b, right) {
271 60b6e08b 2020-10-12 neels all_atoms[len++] = b;
274 12c5bde7 2020-10-16 stsp rc = diff_atoms_sort(all_atoms, len);
276 60b6e08b 2020-10-12 neels goto free_and_exit;
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);
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)
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);
307 60b6e08b 2020-10-12 neels goto free_and_exit;
311 72e4a018 2020-10-22 neels next_differing_i = last_identical_i + 1;
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 ++;
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);
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++;
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;
343 60b6e08b 2020-10-12 neels *unique_in_both_count_p = unique_in_both_count;
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;
349 ca6fcbdc 2020-10-20 neels #endif /* UNIQUE_STRATEGY != 0 */
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)
356 0d27172a 2020-05-06 neels debug("trivially combine identical lines"
357 0d27172a 2020-05-06 neels " around unique_in_both lines\n");
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];
367 6c8c5d3f 2020-10-12 neels if (!PATIENCE(l).unique_in_both)
370 bb7fb738 2020-01-27 neels debug("check identical lines around ");
371 bb7fb738 2020-01-27 neels debug_dump_atom(left, right, l);
373 6c8c5d3f 2020-10-12 neels unsigned int r_idx = diff_atom_idx(right, PATIENCE(l).pos_in_other);
375 d362ea2e 2020-07-25 stsp struct diff_range identical_l;
376 d362ea2e 2020-07-25 stsp struct diff_range identical_r;
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]);
394 b3fb4686 2020-09-20 neels if (!same)
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]);
412 b3fb4686 2020-09-20 neels if (!same)
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)
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)--;
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;
429 bb7fb738 2020-01-27 neels l_min = identical_l.end;
430 bb7fb738 2020-01-27 neels r_min = identical_r.end;
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);
439 bb7fb738 2020-01-27 neels debug("next_l_idx = %u\n", next_l_idx);
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)
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;
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;
461 0d27172a 2020-05-06 neels return lo;
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/ */
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)
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;
485 3b0f3d61 2020-01-22 neels debug("\n** %s\n", __func__);
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;
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);
496 6c8c5d3f 2020-10-12 neels goto free_and_exit;
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);
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;
511 44cf4950 2020-09-20 neels rc = diff_atoms_swallow_identical_neighbors(left, right,
512 44cf4950 2020-09-20 neels &unique_in_both_count);
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);
518 44cf4950 2020-09-20 neels rc = ENOMEM;
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;
526 0d27172a 2020-05-06 neels /* This subscope marks the lifetime of the atom_pointers
527 0d27172a 2020-05-06 neels * allocation */
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*));
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;
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;
546 3b0f3d61 2020-01-22 neels /* Take all common, unique items from 'left' ... */
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)
553 3b0f3d61 2020-01-22 neels *uniques_end = atom;
554 3b0f3d61 2020-01-22 neels uniques_end++;
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++;
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;
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;
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);
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;
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]);
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. */
615 3b0f3d61 2020-01-22 neels debug("\ntraverse LCS, possibly recursing:\n");
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;
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);
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;
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
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. */
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);
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;
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;
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;
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. */
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) {
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));
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;
716 bb7fb738 2020-01-27 neels left_pos = left_idx + 1;
717 bb7fb738 2020-01-27 neels right_pos = right_idx + 1;
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);
723 3b0f3d61 2020-01-22 neels debug("** END %s\n", __func__);
725 3b0f3d61 2020-01-22 neels rc = DIFF_RC_OK;
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);
733 6c8c5d3f 2020-10-12 neels free(lcs);
734 3b0f3d61 2020-01-22 neels return rc;