Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Getting rid of dup code #15

Merged
merged 2 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions src/hamming.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{StringDiffAlgorithm, StringDiffOp};

pub struct HammingDistance {}
impl HammingDistance{}
impl StringDiffAlgorithm for HammingDistance {
fn diff<'a>(&self, s1: &'a str, s2: &'a str) -> Vec<StringDiffOp> {
if s1.len() != s2.len() {
Expand All @@ -22,17 +23,7 @@ impl StringDiffAlgorithm for HammingDistance {
}

fn distance<'a>(&self, s1: &'a str, s2: &'a str) -> usize {
if s1.len() != s2.len() {
panic!("Strings must be same length");
} else {
let mut edit_distance: usize = 0;
for i in 0..s1.len() {
if s1.chars().nth(i).unwrap() != s2.chars().nth(i).unwrap() {
edit_distance += 1;
}
}
edit_distance
}
self.diff(s1, s2).len()
notalfredo marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
43 changes: 10 additions & 33 deletions src/levenshtein.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ impl LevenshteinDistance {

diff_ops
}
}
impl StringDiffAlgorithm for LevenshteinDistance {
fn diff<'a>(&self, s1: &'a str, s2: &'a str) -> Vec<StringDiffOp> {
pub(crate) fn get_operation_matrix(s1: &str, s2: &str) -> Vec<Vec<char>> {
let first_string_len: usize = s1.len();
let second_string_len: usize = s2.len();

Expand Down Expand Up @@ -154,39 +152,18 @@ impl StringDiffAlgorithm for LevenshteinDistance {
); //substitution
}
}
dir_vector
}

LevenshteinDistance::get_operations(&dir_vector, s2, s1)
}
impl StringDiffAlgorithm for LevenshteinDistance {
fn diff<'a>(&self, s1: &'a str, s2: &'a str) -> Vec<StringDiffOp> {
let dir_matrix = LevenshteinDistance::get_operation_matrix(s1, s2);
notalfredo marked this conversation as resolved.
Show resolved Hide resolved
LevenshteinDistance::get_operations(&dir_matrix, s2, s1)
}
fn distance<'a>(&self, s1: &'a str, s2: &'a str) -> usize {
let first_string_len: usize = s1.len();
let second_string_len: usize = s2.len();

let mut dist_vector = vec![vec![0usize; first_string_len + 1]; second_string_len + 1];

for i in 0..first_string_len + 1 {
dist_vector[0][i] = i;
}

for i in 0..second_string_len + 1 {
dist_vector[i][0] = i;
}

let mut sub_cost: usize = 0;
for i in 1..second_string_len + 1 {
for j in 1..first_string_len + 1 {
if s1.chars().nth(j - 1).unwrap() == s2.chars().nth(i - 1).unwrap() {
sub_cost = 0;
} else {
sub_cost = 1;
}
dist_vector[i][j] = LevenshteinDistance::min_dist(
dist_vector[i - 1][j] + 1,
dist_vector[i][j - 1] + 1,
dist_vector[i - 1][j - 1] + sub_cost,
);
}
}
dist_vector[second_string_len][first_string_len]
let dir_matrix = LevenshteinDistance::get_operation_matrix(s1, s2);
notalfredo marked this conversation as resolved.
Show resolved Hide resolved
LevenshteinDistance::get_operations(&dir_matrix, s2, s1).len()
}
}

Expand Down