Skip to content

Commit

Permalink
refactor: Simplify matching code
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Nov 6, 2023
1 parent fe21a3e commit abb6299
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
19 changes: 17 additions & 2 deletions matching/src/matchings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ impl<'a> Matchings<'a> {
}
}

pub fn from_single(key: UnorderedPair<&'a CSTNode>, value: MatchingEntry) -> Self {
let mut matching_entries = HashMap::new();
matching_entries.insert(key, value);
Matchings { matching_entries }
}

pub fn new(matching_entries: HashMap<UnorderedPair<&'a CSTNode<'a>>, MatchingEntry>) -> Self {
Matchings { matching_entries }
}
Expand Down Expand Up @@ -51,18 +57,27 @@ impl<'a> Matchings<'a> {
) -> bool {
self.find_matching_for(left).is_some() && self.find_matching_for(right).is_some()
}

pub fn push(&mut self, key: UnorderedPair<&'a CSTNode>, value: MatchingEntry) {
self.matching_entries.insert(key, value);
}

pub fn extend(&mut self, matchings: HashMap<UnorderedPair<&'a CSTNode<'a>>, MatchingEntry>) {
self.matching_entries.extend(matchings);
}
}

impl Default for Matchings<'_> {
fn default() -> Self {
Self { matching_entries: Default::default() }
Self::empty()
}
}

impl<'a> IntoIterator for Matchings<'a> {
type Item = (UnorderedPair<&'a CSTNode<'a>>, MatchingEntry);

type IntoIter = std::collections::hash_map::IntoIter<UnorderedPair<&'a CSTNode<'a>>, MatchingEntry>;
type IntoIter =
std::collections::hash_map::IntoIter<UnorderedPair<&'a CSTNode<'a>>, MatchingEntry>;

fn into_iter(self) -> Self::IntoIter {
self.matching_entries.into_iter()
Expand Down
46 changes: 20 additions & 26 deletions matching/src/ordered_tree_matching.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{matching_entry::MatchingEntry, Matchings, calculate_matchings};
use crate::{calculate_matchings, matching_entry::MatchingEntry, Matchings};
use model::CSTNode;
use std::collections::HashMap;
use utils::unordered_pair::UnorderedPair;
Expand All @@ -11,10 +11,7 @@ enum Direction {
}

#[derive(Clone)]
struct Entry<'a>(
pub Direction,
pub Matchings<'a>,
);
struct Entry<'a>(pub Direction, pub Matchings<'a>);

impl<'a> Default for Entry<'a> {
fn default() -> Self {
Expand Down Expand Up @@ -88,15 +85,18 @@ pub fn ordered_tree_matching<'a>(left: &'a CSTNode, right: &'a CSTNode) -> Match
}
}

let matching = MatchingEntry::new(matrix_m[m][n] + root_matching, left == right);
let mut result = HashMap::new();
result.insert(UnorderedPair::new(left, right), matching);
children.into_iter().for_each(|child_matchings| {
child_matchings.into_iter().for_each(|(key, matching)| {
result.insert(key.to_owned(), matching.to_owned());
})
});
Matchings::new(result)
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
}
(
CSTNode::Terminal {
Expand All @@ -108,22 +108,16 @@ pub fn ordered_tree_matching<'a>(left: &'a CSTNode, right: &'a CSTNode) -> Match
value: value_right,
},
) => {
let mut result = HashMap::new();
let is_perfetch_match = kind_left == kind_right && value_left == value_right;
result.insert(
Matchings::from_single(
UnorderedPair::new(left, right),
MatchingEntry::new(is_perfetch_match.into(), is_perfetch_match),
);
Matchings::new(result)
}
(_, _) => {
let mut result = HashMap::new();
result.insert(
UnorderedPair::new(left, right),
MatchingEntry::new(0, false),
);
Matchings::new(result)
)
}
(_, _) => Matchings::from_single(
UnorderedPair::new(left, right),
MatchingEntry::new(0, false),
),
}
}

Expand Down

0 comments on commit abb6299

Please sign in to comment.