From 51a9ac1ae37174dac5fe7d6e9c3a31e92523c35e Mon Sep 17 00:00:00 2001 From: Freyskeyd Date: Thu, 5 Nov 2015 23:16:51 +0100 Subject: [PATCH] feat(distance/levenshtein): vector method Signed-off-by: Freyskeyd --- src/distance.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/distance.rs b/src/distance.rs index 9bc6765..5f753da 100644 --- a/src/distance.rs +++ b/src/distance.rs @@ -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 { + let mut r: Vec = Vec::with_capacity(v.len()); + for b in v.iter() { + r.push(levenshtein(a, b)); + } + + r +} + #[cfg(test)] mod tests { use super::*;