diff --git a/src/sequencematcher.rs b/src/sequencematcher.rs index 43d4b6c..9f51d80 100644 --- a/src/sequencematcher.rs +++ b/src/sequencematcher.rs @@ -1,5 +1,5 @@ use std::cmp::Ordering; -use utils::slice_str; +use utils::{slice_str, calculate_ratio}; #[derive(Debug, Clone, Copy)] pub struct Match{ @@ -162,4 +162,9 @@ impl<'a> SequenceMatcher<'a>{ self.opcodes = Some(opcodes); return self.opcodes.as_ref().unwrap().clone() } + + pub fn ratio(&mut self) -> f32{ + let matches = self.get_matching_blocks().iter().fold(0, |res, &m| res + m.size); + calculate_ratio(matches, self.first_sequence.len() + self.second_sequence.len()) + } } \ No newline at end of file diff --git a/src/utils.rs b/src/utils.rs index 4066c42..8060adf 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -9,4 +9,12 @@ pub fn slice_str(string: &str, start: usize, end: usize) -> Option<&str> unsafe{ Some(string.slice_unchecked(start, end)) } +} + + +pub fn calculate_ratio(matches: usize, length: usize) -> f32{ + if length != 0{ + return 2.0 * matches as f32 / length as f32 + } + return 1.0 } \ No newline at end of file