• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Length distance
2 use crate::{Algorithm, Result};
3 
4 /// Length distance is the absolute difference between the lengths of the two sequences.
5 ///
6 /// It's a very dumb algorithm that says that "qwer" and "zxcv" are the same.
7 /// Still, it works surprisingly well in some specific scenarios, especially on big
8 /// sequences.
9 #[derive(Default)]
10 pub struct Length {}
11 
12 impl Algorithm<usize> for Length {
for_iter<C, E>(&self, s1: C, s2: C) -> Result<usize> where C: Iterator<Item = E>, E: Eq,13     fn for_iter<C, E>(&self, s1: C, s2: C) -> Result<usize>
14     where
15         C: Iterator<Item = E>,
16         E: Eq,
17     {
18         let l1 = s1.count();
19         let l2 = s2.count();
20         Result {
21             abs: l1.abs_diff(l2),
22             is_distance: true,
23             max: l1.max(l2),
24             len1: l1,
25             len2: l2,
26         }
27     }
28 }
29 
30 #[cfg(test)]
31 mod tests {
32     use crate::str::length;
33     use assert2::assert;
34     use rstest::rstest;
35 
36     #[rstest]
37     #[case("", "", 0)]
38     #[case("", "a", 1)]
39     #[case("a", "", 1)]
40     #[case("a", "a", 0)]
41     #[case("a", "b", 0)]
42     #[case("abcde", "abcef", 0)]
43     #[case("abcde", "abcfde", 1)]
44     #[case("abcd", "bcd", 1)]
45     #[case("ab", "cdefg", 3)]
function_str(#[case] s1: &str, #[case] s2: &str, #[case] exp: usize)46     fn function_str(#[case] s1: &str, #[case] s2: &str, #[case] exp: usize) {
47         assert!(length(s1, s2) == exp);
48     }
49 }
50