Skip to content

Commit f8a7548

Browse files
committed
update README for patience diff
1 parent a8c9add commit f8a7548

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

diff/README.mbt.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# Myers Diff
1+
# Diff
22

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`.
45

56
`Diff` works with any element type that implements `Hash + Eq`. Constructing a
67
`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" {
3334
}
3435
```
3536

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+
3674
## Group Into Hunks And Render
3775

3876
`group` splits the edit script into `Hunk[T]` values, keeping `radius` lines

0 commit comments

Comments
 (0)