#diff #dam-lev #lev

dam_lev

Implements the Damerau–Levenshtein diff algorithm

5 releases (3 breaking)

new 0.4.1 May 5, 2025
0.4.0 Apr 6, 2025
0.3.0 Apr 2, 2025
0.2.0 Mar 31, 2025
0.1.0 Mar 31, 2025

#706 in Algorithms

Download history 171/week @ 2025-03-26 189/week @ 2025-04-02 12/week @ 2025-04-09 2/week @ 2025-04-16 115/week @ 2025-04-30

141 downloads per month

MIT license

10KB
125 lines

dam_lev implements the Damerau–Levenshtein diff algorithm. That is, it takes two sequences and determines the minimum number of transpositions, substitutions, insertions, and deletions needed to transform the first sequence into the second.

Usage

dam_lev::diff takes two slices of some type T which implements Clone + PartialEq and returns a vector of dam_lev::Mutation objects. This is an enum type with four variants corresponding to the four types of transformations. For example,

use dam_lev::Mutation::*;

let seq1 = ['a', 'b', 'c', 'd', 'e', 'f'];
let seq2 = ['b', 'c', 'e', 'd', 'x', 'y'];
let diffs = dam_lev::diff(&seq1, &seq2);
assert_eq!(diffs, vec![Deletion(0), Transposition(3), Substitution(5, 4), Insertion(6, 5)]);

We see that the sequence of transformations is

  • Delete the item at index 0 ('a').
  • Transpose the item at index 3 ('d') with its successor.
  • Substitute the item at index 5 ('f') with the item from the second sequence at index 4 ('x').
  • Insert at index 6 the item from the second sequence at index 5 ('y').

Note the index for the transposition. Even though, after the deletion, the 'd' is at index 2, it's at index 3 in the original version of the sequence. Likewise for the successive mutations.

Compare function

If you want to customize how the items are compared, you can use diff_with_compare:

let seq1 = [1, 2, 3];
let seq2 = [91, 92, 93];
let compare = |num1: &u32, num2: &u32| num1 % 3 == num2 % 3;
let diffs = dam_lev::diff_with_compare(&seq1, &seq2, compare);
assert_eq!(diffs.len(), 0);

In this case, the items must only implement Clone and compare must be of type FnMut(&T, &T) -> bool.

No runtime deps