-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.ltl
More file actions
61 lines (52 loc) · 3.25 KB
/
diff.ltl
File metadata and controls
61 lines (52 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// ─── Section Name ────────────────────────────────────────
// ═══════════════════════════════════════════════════════════
// LATERALUS — Title
// Description line
// ═══════════════════════════════════════════════════════════
// ═══════════════════════════════════════════════════════════
-- stdlib/diff.ltl
// ─── Diff Module ────────────────────────────────────────
//
// This module provides the Myers diff algorithm for computing edit distance and line-level diffs between two strings.
module Diff {
// ────────────────────────────────────────────────────────────────
// ─── Struct Definition: EditDistance ──────────────────────────────
//
// Represents an edit distance between two strings.
//
// ┌───────────┬────────────────────────────────────────────────────┐
// │ Field │ Type │
// ├── distance│ int │
// └───────────┴────────────────────────────────────────────────────┘
struct EditDistance {
distance: int
}
// ────────────────────────────────────────────────────────────────
// ─── Function: edit_distance ───────────────────────────────────────
//
// Computes the edit distance between two strings using the Myers algorithm.
//
// Args:
// a (str): First string.
// b (str): Second string.
//
// Returns:
// EditDistance: An edit distance object containing the minimum edit distance.
fn edit_distance(a: str, b: str) -> EditDistance {
let mut dp = map<list<int>>{(0, 0) => 0};
let mut prev = map<list<int>>((0, 0) => None);
for i in range(len(a)) {
for j in range(len(b)) {
if a[i] == b[j] {
let new_dp = dp[(i + 1, j + 1)];
dp[(i + 1, j + 1)] = new_dp;
prev[(i + 1, j + 1)] = Some((new_dp, (i, j)));
} else {
let cost = abs(i - j);
let new_dp = dp[(i + 1, j + 1)];
dp[(i + 1, j + 1)] = min(new_dp, new_dp + cost);
prev[(i + 1, j + 1)] = Some((min(new_dp, new_dp + cost), (i, j)));
}
}
}
// Reconstruct the edit sequence