From 302257e54ae4bd57d1fde68f67ed9eb934323b76 Mon Sep 17 00:00:00 2001 From: Dima Kudosh Date: Sat, 13 Feb 2016 13:38:17 +0300 Subject: [PATCH] Add Sequence Matcher, add ability to find largest common sequence --- Cargo.toml | 2 +- src/lib.rs | 4 +--- src/sequencematcher.rs | 52 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 src/sequencematcher.rs diff --git a/Cargo.toml b/Cargo.toml index 40bfcb3..e3650a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,4 @@ [package] name = "difflib" version = "0.1.0" -authors = ["Dima Kudosh "] +authors = ["Dima Kudosh "] diff --git a/src/lib.rs b/src/lib.rs index a93251b..b76a9b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1 @@ -#[test] -fn it_works() { -} +pub mod sequencematcher; \ No newline at end of file diff --git a/src/sequencematcher.rs b/src/sequencematcher.rs new file mode 100644 index 0000000..8cafd08 --- /dev/null +++ b/src/sequencematcher.rs @@ -0,0 +1,52 @@ +#[derive(Debug)] +pub struct Match{ + first_start: usize, + second_start: usize, + size: usize +} + +pub struct SequenceMatcher<'a>{ + first_sequence: &'a str, + second_sequence: &'a str +} + +impl<'a> SequenceMatcher<'a>{ + pub fn new(first_sequence: &'a str, second_sequence: &'a str) -> SequenceMatcher<'a> + { + SequenceMatcher{ + first_sequence: first_sequence, + second_sequence: second_sequence + } + } + + pub fn find_longest_match(&self, first_start: usize, first_end: usize, second_start: usize, second_end: usize) -> Match + { + let first_sequence: Vec = self.first_sequence.chars().collect(); + let second_sequence: Vec = self.second_sequence.chars().collect(); + let mut start = 0; + let mut end = 0; + let mut arr: Vec> = vec![vec![0; second_sequence.len() + 1]; first_sequence.len() + 1]; + for i in 0..first_sequence.len(){ + for j in 0..second_sequence.len(){ + if first_sequence[i] == second_sequence[j]{ + arr[i + 1][j + 1] = arr[i][j] + 1; + if arr[i + 1][j + 1] > arr[start][end]{ + start = i + 1; + end = j + 1; + } + } + } + } + Match{ + first_start: start - arr[start][end], + second_start: end - arr[start][end], + size: arr[start][end] + } + } + + pub fn get_matching_blocks(&self) //-> Vec + { + let first_length = self.first_sequence.len(); + let second_length = self.second_sequence.len(); + } +} \ No newline at end of file