Blame


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