|
1 | | -# Myers Diff |
| 1 | +# Diff |
2 | 2 |
|
3 | | -Compute edit scripts between two sequences using the Myers diff algorithm. |
| 3 | +Compute edit scripts between two sequences using the Myers diff algorithm by |
| 4 | +default, or patience diff when you pass `patience=true`. |
4 | 5 |
|
5 | 6 | `Diff` works with any element type that implements `Hash + Eq`. Constructing a |
6 | 7 | `Diff[T]` bundles the source arrays with the edit script. Call `group` on the |
@@ -33,6 +34,43 @@ test "Diff computes deletes inserts and equals" { |
33 | 34 | } |
34 | 35 | ``` |
35 | 36 |
|
| 37 | +## Prefer Unique Anchors With Patience Diff |
| 38 | + |
| 39 | +Pass `patience=true` to `Diff(old~, new~, patience=true)` to enable patience |
| 40 | +diff. This first finds elements that appear exactly once in both inputs and |
| 41 | +uses them as anchors, then runs Myers diff on the unmatched ranges between |
| 42 | +those anchors. This can produce more stable result when repeated elements move |
| 43 | +around. |
| 44 | + |
| 45 | +```mbt check |
| 46 | +///| |
| 47 | +test "patience diff keeps unique anchors in place" { |
| 48 | + let old = ["unique", "dup", "dup"][:] |
| 49 | + let new = ["dup", "unique", "dup"][:] |
| 50 | +
|
| 51 | + let myers = @diff.Diff(old~, new~) |
| 52 | + let patience = @diff.Diff(old~, new~, patience=true) |
| 53 | +
|
| 54 | + assert_true( |
| 55 | + myers.edits[:] |
| 56 | + is [ |
| 57 | + Delete(old_index=0, new_index=0, old_len=1), |
| 58 | + Equal(old_index=1, new_index=0, len=1), |
| 59 | + Insert(old_index=2, new_index=1, new_len=1), |
| 60 | + Equal(old_index=2, new_index=2, len=1), |
| 61 | + ], |
| 62 | + ) |
| 63 | + assert_true( |
| 64 | + patience.edits[:] |
| 65 | + is [ |
| 66 | + Insert(old_index=0, new_index=0, new_len=1), |
| 67 | + Equal(old_index=0, new_index=1, len=2), |
| 68 | + Delete(old_index=2, new_index=3, old_len=1), |
| 69 | + ], |
| 70 | + ) |
| 71 | +} |
| 72 | +``` |
| 73 | + |
36 | 74 | ## Group Into Hunks And Render |
37 | 75 |
|
38 | 76 | `group` splits the edit script into `Hunk[T]` values, keeping `radius` lines |
|
0 commit comments