Partly implement differ struce, add Opcode struct

This commit is contained in:
Dima Kudosh
2016-02-17 21:03:12 +03:00
parent ca75fcc862
commit 38cc286ba2
3 changed files with 201 additions and 7 deletions
+50 -6
View File
@@ -38,24 +38,68 @@ impl Match{
}
}
#[derive(Debug, Clone)]
pub struct Opcode{
pub tag: String,
pub first_start: usize,
pub first_end: usize,
pub second_start: usize,
pub second_end: usize
}
impl Opcode{
fn new(tag: String, first_start: usize, first_end: usize, second_start: usize, second_end: usize) -> Opcode {
Opcode{
tag: tag,
first_start: first_start,
first_end: first_end,
second_start: second_start,
second_end: second_end
}
}
}
pub struct SequenceMatcher<'a>{
first_sequence: &'a str,
second_sequence: &'a str,
matching_blocks: Option<Vec<Match>>,
opcodes: Option<Vec<(String, usize, usize, usize, usize)>>
opcodes: Option<Vec<Opcode>>,
autojunk: bool
}
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,
matching_blocks: None,
opcodes: None
opcodes: None,
autojunk: true,
}
}
pub fn set_seqs(&mut self, first_sequence: &'a str, second_sequence: &'a str) {
self.set_first_seq(first_sequence);
self.set_second_seq(second_sequence);
}
pub fn set_first_seq(&mut self, sequence: &'a str) {
self.first_sequence = sequence;
self.matching_blocks = None;
self.opcodes = None;
}
pub fn set_second_seq(&mut self, sequence: &'a str) {
self.second_sequence = sequence;
self.matching_blocks = None;
self.opcodes = None;
//self.fullbcount = None
//self.__chain_b()
}
pub fn find_longest_match(&self, first_start: usize, first_end: usize, second_start: usize, second_end: usize) -> Option<Match> {
let first_sequence: Vec<char> = slice_str(self.first_sequence, first_start, first_end).unwrap_or("").chars().collect();
let second_sequence: Vec<char> = slice_str(self.second_sequence, second_start, second_end).unwrap_or("").chars().collect();
@@ -133,7 +177,7 @@ impl<'a> SequenceMatcher<'a>{
self.matching_blocks.as_ref().unwrap().clone()
}
pub fn get_opcodes(&mut self) -> Vec<(String, usize, usize, usize, usize)>{
pub fn get_opcodes(&mut self) -> Vec<Opcode>{
if self.opcodes.as_ref().is_some(){
return self.opcodes.as_ref().unwrap().clone()
}
@@ -151,12 +195,12 @@ impl<'a> SequenceMatcher<'a>{
tag = String::from("insert");
}
if !tag.is_empty(){
opcodes.push( (tag, i, m.first_start, j, m.second_start) );
opcodes.push( Opcode::new(tag, i, m.first_start, j, m.second_start) );
}
i = m.first_start + m.size;
j = m.second_start + m.size;
if m.size != 0{
opcodes.push( (String::from("equal"), m.first_start, i, m.second_start, j) );
opcodes.push( Opcode::new(String::from("equal"), m.first_start, i, m.second_start, j) );
}
}
self.opcodes = Some(opcodes);
@@ -167,4 +211,4 @@ impl<'a> SequenceMatcher<'a>{
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())
}
}
}