Skip to content

Commit

Permalink
refactor: simplify extends code
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Nov 6, 2023
1 parent abb6299 commit fd3c522
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
2 changes: 1 addition & 1 deletion matching/src/matchings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<'a> Matchings<'a> {
self.matching_entries.insert(key, value);
}

pub fn extend(&mut self, matchings: HashMap<UnorderedPair<&'a CSTNode<'a>>, MatchingEntry>) {
pub fn extend(&mut self, matchings: Matchings<'a>) {
self.matching_entries.extend(matchings);
}
}
Expand Down
20 changes: 6 additions & 14 deletions matching/src/ordered_tree_matching.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{calculate_matchings, matching_entry::MatchingEntry, Matchings};
use model::CSTNode;
use std::collections::HashMap;
use utils::unordered_pair::UnorderedPair;

#[derive(PartialEq, Eq, Debug, Clone)]
Expand Down Expand Up @@ -69,33 +68,26 @@ pub fn ordered_tree_matching<'a>(left: &'a CSTNode, right: &'a CSTNode) -> Match

let mut i = m;
let mut j = n;
let mut children = Vec::<Matchings<'a>>::new();

let mut matchings = Matchings::from_single(
UnorderedPair::new(left, right),
MatchingEntry::new(matrix_m[m][n] + root_matching, left == right),
);

while i >= 1 && j >= 1 {
match matrix_t.get(i).unwrap().get(j).unwrap().0 {
Direction::TOP => i = i - 1,
Direction::LEFT => j = j - 1,
Direction::DIAG => {
if matrix_m[i][j] > matrix_m[i - 1][j - 1] {
children.push(matrix_t[i][j].1.clone());
matchings.extend(matrix_t[i][j].1.clone());
}
i = i - 1;
j = j - 1;
}
}
}

let mut matchings = Matchings::from_single(
UnorderedPair::new(left, right),
MatchingEntry::new(matrix_m[m][n] + root_matching, left == right),
);
matchings.extend(children.into_iter().fold(
HashMap::new(),
|mut acc, child_matchings| {
acc.extend(child_matchings);
acc
},
));
matchings
}
(
Expand Down

0 comments on commit fd3c522

Please sign in to comment.