Compute the diff between two sequences using the LCS-based algorithm. This finds the operations needed to transform `old` into `new`. # Arguments `old` - The original sequence `new` - The modified sequence # Returns A [`DiffResult`] containing the operations to transform old → new.
(old: &[Line<'a>], new: &[Line<'a>])
| 35 | /// |
| 36 | /// A [`DiffResult`] containing the operations to transform old → new. |
| 37 | pub fn diff<'a>(old: &[Line<'a>], new: &[Line<'a>]) -> DiffResult { |
| 38 | let n = old.len(); |
| 39 | let m = new.len(); |
| 40 | |
| 41 | // Special cases for empty sequences |
| 42 | if n == 0 && m == 0 { |
| 43 | return DiffResult::new(); |
| 44 | } |
| 45 | if n == 0 { |
| 46 | let mut result = DiffResult::new(); |
| 47 | result.push(DiffOp::insert(0, 0, m)); |
| 48 | return result; |
| 49 | } |
| 50 | if m == 0 { |
| 51 | let mut result = DiffResult::new(); |
| 52 | result.push(DiffOp::delete(0, 0, n)); |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | // Compute LCS using dynamic programming |
| 57 | let lcs = compute_lcs(old, new); |
| 58 | |
| 59 | // Convert LCS to diff operations |
| 60 | lcs_to_diff(&lcs, n, m) |
| 61 | } |
| 62 | |
| 63 | /// Represents an element in the LCS. |
| 64 | #[derive(Debug, Clone, Copy)] |