Add Sequence Matcher, add ability to find largest common sequence

This commit is contained in:
Dima Kudosh
2016-02-13 13:38:17 +03:00
parent d01307bbfc
commit 302257e54a
3 changed files with 54 additions and 4 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
[package]
name = "difflib"
version = "0.1.0"
authors = ["Dima Kudosh <LagMegantic@yandex.ru>"]
authors = ["Dima Kudosh <dimakudosh@gmail.com>"]
+1 -3
View File
@@ -1,3 +1 @@
#[test]
fn it_works() {
}
pub mod sequencematcher;
+52
View File
@@ -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<char> = self.first_sequence.chars().collect();
let second_sequence: Vec<char> = self.second_sequence.chars().collect();
let mut start = 0;
let mut end = 0;
let mut arr: Vec<Vec<usize>> = 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<Match>
{
let first_length = self.first_sequence.len();
let second_length = self.second_sequence.len();
}
}