levenshtein_diff/
util.rs

1pub type DistanceMatrix = Vec<Vec<usize>>;
2
3pub fn print_table(table: &DistanceMatrix) {
4    for row in table {
5        for item in row {
6            print!("{} ", item);
7        }
8        println!("");
9    }
10}
11
12// Returns an initialized distance table of dimensions m+1 * n+1
13// Where the first row is 0..n+1
14// The First column is 0..m+1
15// And the rest of the values are usize::MAX
16pub fn get_distance_table(m: usize, n: usize) -> DistanceMatrix {
17    let mut distances = Vec::with_capacity(m + 1);
18
19    // The first row
20    distances.push((0..n + 1).collect());
21
22    for i in 1..m + 1 {
23        // initialize the whole row to sentinel
24        distances.push(vec![usize::MAX; n + 1]);
25        // update the first item in the row
26        distances[i][0] = i;
27    }
28
29    distances
30}
31
32pub fn up_to_last<T>(slice: &[T]) -> &[T] {
33    slice.split_last().map_or(&[], |(_, rest)| rest)
34}