Skip to content

Commit

Permalink
feat(distance/levenshtein): vector method
Browse files Browse the repository at this point in the history
Signed-off-by: Freyskeyd <contact@freyskeyd.fr>
  • Loading branch information
Freyskeyd committed Nov 5, 2015
1 parent 63b207b commit 51a9ac1
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,25 @@ pub fn levenshtein(a: &str, b: &str) -> usize {
curr_distances[b.len()]
}

/// Calculates the levenshtein distance between a string and each string in a vector. Returns a
/// vector of corresponding values.
///
/// Examples:
/// ```
/// let v = vec!["test", "test1", "test12", "test123", "", "tset"];
/// let result = levenshtein_against_vec("test", &v);
/// let expect = vec![0, 1, 2, 3, 4, 2];
/// assert_eq!(expect, result);
/// ```
pub fn levenshtein_against_vec(a: &str, v: &Vec<&str>) -> Vec<usize> {
let mut r: Vec<usize> = Vec::with_capacity(v.len());
for b in v.iter() {
r.push(levenshtein(a, b));
}

r
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 51a9ac1

Please sign in to comment.