2

For two ordered sets of keys Paul Heckel's diffing algorithm generates the necessary actions to transform, or patch, one set to the other. I am asking how you'd do the patching without keeping a copy of the "old set" around, nor incurring a large performance penalty. The problem statement is agnostic to the choice of diffing algorithm; the Longest-Increasing-Subsequence algorithm, or similar, could be used just as easily.

To illustrate, the first set could be patched not in-place using the following Rust code:

let old = vec![0, 1, 6, 4, 9, 3, 2, 8];
let new = vec![3, 5, 2, 7, 1, 0, 9, 4, 8];

let mut out = old.clone(); /* <-- Creates temporary copy. */
for action in diff(&old, &new) {
    match action {
        Insert(i) => {
            out.insert(i, new[i]);
        }
        Remove(i) => {
            out.remove(i);
        }
        Move(i, j) => {
            out[j] = old[i]; /* <-- Uses old set. */
        }
        Update(i) => { /* ... */ }
    }
}

out == new // = true

However, the above code clones old to refer to when performing out[j] = old[i];. This is unwanted since I am operating on a data structure that is unwieldy to copy.

Axel F
  • 21
  • 2

0 Answers0