From df47e4586cd9b41ee1e0eab5f9aaccbe0db23ec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Henrique?= Date: Sat, 28 Oct 2023 03:13:20 +0000 Subject: [PATCH 1/2] refactor(matching,merge): Use ref to store CSTNode in the hashmap key --- matching/src/lib.rs | 5 ++- matching/src/matchings.rs | 21 ++++++------- matching/src/ordered_tree_matching.rs | 45 ++++++++++++--------------- merge/src/odered_merge.rs | 6 ++-- 4 files changed, 35 insertions(+), 42 deletions(-) diff --git a/matching/src/lib.rs b/matching/src/lib.rs index 475f157..9d7b851 100644 --- a/matching/src/lib.rs +++ b/matching/src/lib.rs @@ -7,6 +7,9 @@ pub use matching_entry::MatchingEntry; pub use matchings::Matchings; pub use ordered_tree_matching::ordered_tree_matching; -pub fn calculate_matchings<'a>(left: &'a model::CSTNode, right: &'a model::CSTNode) -> Matchings<'a> { +pub fn calculate_matchings<'a>( + left: &'a model::CSTNode, + right: &'a model::CSTNode, +) -> Matchings<'a> { return ordered_tree_matching::ordered_tree_matching(left, right); } diff --git a/matching/src/matchings.rs b/matching/src/matchings.rs index 7ad9c3b..a063fc0 100644 --- a/matching/src/matchings.rs +++ b/matching/src/matchings.rs @@ -8,7 +8,7 @@ use crate::matching_entry::MatchingEntry; #[derive(Debug, Clone)] pub struct Matchings<'a> { - matching_entries: HashMap>, MatchingEntry>, + matching_entries: HashMap>, MatchingEntry>, } impl<'a> Matchings<'a> { @@ -18,16 +18,16 @@ impl<'a> Matchings<'a> { } } - pub fn new(matching_entries: HashMap>, MatchingEntry>) -> Self { + pub fn new(matching_entries: HashMap>, MatchingEntry>) -> Self { Matchings { matching_entries } } pub fn find_matching_for(&self, a_node: &'a CSTNode) -> Option { self.matching_entries .iter() - .find(|(UnorderedPair(left, right), ..)| left == a_node || right == a_node) + .find(|(UnorderedPair(left, right), ..)| left == &a_node || right == &a_node) .map(|(UnorderedPair(left, right), matching)| { - let matching_node = if left == a_node { right } else { left }; + let matching_node = if left == &a_node { right } else { left }; Matching { matching_node, score: matching.score, @@ -38,8 +38,8 @@ impl<'a> Matchings<'a> { pub fn get_matching_entry( &'a self, - left: CSTNode<'a>, - right: CSTNode<'a>, + left: &'a CSTNode<'a>, + right: &'a CSTNode<'a>, ) -> Option<&MatchingEntry> { self.matching_entries.get(&UnorderedPair(left, right)) } @@ -52,7 +52,7 @@ mod tests { #[test] fn returns_none_if_a_matching_for_the_node_is_not_found() { let a_node = CSTNode::Terminal { - kind: "kind".into(), + kind: "kind", value: "value".into(), }; @@ -62,15 +62,12 @@ mod tests { #[test] fn returns_some_match_if_a_matching_for_the_node_is_found() { let a_node = CSTNode::Terminal { - kind: "kind".into(), + kind: "kind", value: "value".into(), }; let mut matchings = HashMap::new(); - matchings.insert( - UnorderedPair(a_node.clone(), a_node.clone()), - MatchingEntry::new(1, true), - ); + matchings.insert(UnorderedPair(&a_node, &a_node), MatchingEntry::new(1, true)); assert_eq!( Some(Matching { diff --git a/matching/src/ordered_tree_matching.rs b/matching/src/ordered_tree_matching.rs index a9915ce..dedc667 100644 --- a/matching/src/ordered_tree_matching.rs +++ b/matching/src/ordered_tree_matching.rs @@ -13,7 +13,7 @@ enum Direction { #[derive(Clone)] struct Entry<'a>( pub Direction, - pub HashMap>, MatchingEntry>, + pub HashMap>, MatchingEntry>, ); impl<'a> Default for Entry<'a> { @@ -29,7 +29,7 @@ pub fn ordered_tree_matching<'a>(left: &'a CSTNode, right: &'a CSTNode) -> Match fn ordered_tree_matching_helper<'a>( left: &'a CSTNode, right: &'a CSTNode, -) -> HashMap>, MatchingEntry> { +) -> HashMap>, MatchingEntry> { match (left, right) { ( CSTNode::NonTerminal { @@ -55,12 +55,7 @@ fn ordered_tree_matching_helper<'a>( let right_child = children_right.get(j - 1).unwrap(); let w = ordered_tree_matching_helper(left_child, right_child); - let matching = w - .get(&UnorderedPair::new( - left_child.to_owned(), - right_child.to_owned(), - )) - .unwrap(); + let matching = w.get(&UnorderedPair::new(left_child, right_child)).unwrap(); if matrix_m[i][j - 1] > matrix_m[i - 1][j] { if matrix_m[i][j - 1] > matrix_m[i - 1][j - 1] + matching.score { @@ -84,7 +79,7 @@ fn ordered_tree_matching_helper<'a>( let mut i = m; let mut j = n; - let mut children = Vec::<&HashMap, MatchingEntry>>::new(); + let mut children = Vec::<&HashMap, MatchingEntry>>::new(); while i >= 1 && j >= 1 { match matrix_t.get(i).unwrap().get(j).unwrap().0 { @@ -102,10 +97,7 @@ fn ordered_tree_matching_helper<'a>( let matching = MatchingEntry::new(matrix_m[m][n] + root_matching, left == right); let mut result = HashMap::new(); - result.insert( - UnorderedPair::new(left.to_owned(), right.to_owned()), - matching, - ); + result.insert(UnorderedPair::new(left, right), matching); children.into_iter().for_each(|child_matchings| { child_matchings.iter().for_each(|(key, matching)| { result.insert(key.to_owned(), matching.to_owned()); @@ -126,7 +118,7 @@ fn ordered_tree_matching_helper<'a>( let mut result = HashMap::new(); let is_perfetch_match = kind_left == kind_right && value_left == value_right; result.insert( - UnorderedPair::new(left.to_owned(), right.to_owned()), + UnorderedPair::new(left, right), MatchingEntry::new(is_perfetch_match.into(), is_perfetch_match), ); result @@ -134,7 +126,7 @@ fn ordered_tree_matching_helper<'a>( (_, _) => { let mut result = HashMap::new(); result.insert( - UnorderedPair::new(left.to_owned(), right.to_owned()), + UnorderedPair::new(left, right), MatchingEntry::new(0, false), ); result @@ -162,7 +154,7 @@ mod tests { assert_eq!( Some(&MatchingEntry::new(1, true)), - matchings.get_matching_entry(left.clone(), right.clone()) + matchings.get_matching_entry(&left, &right) ) } @@ -181,7 +173,7 @@ mod tests { assert_eq!( Some(&MatchingEntry::new(0, false)), - matchings.get_matching_entry(left.clone(), right.clone()) + matchings.get_matching_entry(&left, &right) ) } @@ -200,7 +192,7 @@ mod tests { assert_eq!( Some(&MatchingEntry::new(0, false)), - matchings.get_matching_entry(left.clone(), right.clone()) + matchings.get_matching_entry(&left, &right) ) } @@ -219,7 +211,7 @@ mod tests { assert_eq!( Some(&MatchingEntry::new(0, false)), - matchings.get_matching_entry(left.clone(), right.clone()) + matchings.get_matching_entry(&left, &right) ) } @@ -242,7 +234,7 @@ mod tests { assert_eq!( Some(&MatchingEntry::new(1, true)), - matchings.get_matching_entry(child.clone(), child) + matchings.get_matching_entry(&child, &child) ) } @@ -268,7 +260,10 @@ mod tests { let matchings = ordered_tree_matching(&left, &right); - assert_eq!(None, matchings.get_matching_entry(left_child, right_child)) + assert_eq!( + None, + matchings.get_matching_entry(&left_child, &right_child) + ) } #[test] @@ -295,7 +290,7 @@ mod tests { assert_eq!( Some(&MatchingEntry::new(2, false)), - matchings.get_matching_entry(left.clone(), right.clone()) + matchings.get_matching_entry(&left, &right) ) } @@ -319,7 +314,7 @@ mod tests { assert_eq!( Some(&MatchingEntry::new(2, true)), - matchings.get_matching_entry(left.clone(), right.clone()) + matchings.get_matching_entry(&left, &right) ) } @@ -348,12 +343,12 @@ mod tests { assert_eq!( Some(&MatchingEntry::new(2, true)), - matchings.get_matching_entry(intermediate.clone(), intermediate) + matchings.get_matching_entry(&intermediate, &intermediate) ); assert_eq!( Some(&MatchingEntry::new(3, true)), - matchings.get_matching_entry(left.clone(), right.clone()) + matchings.get_matching_entry(&left, &right) ) } } diff --git a/merge/src/odered_merge.rs b/merge/src/odered_merge.rs index 4733c06..1355ab1 100644 --- a/merge/src/odered_merge.rs +++ b/merge/src/odered_merge.rs @@ -63,10 +63,8 @@ pub fn ordered_merge<'a>( let matching_base_left = base_left_matchings.find_matching_for(cur_left.unwrap()); let matching_base_right = base_right_matchings.find_matching_for(cur_right.unwrap()); - let bidirectional_matching_left_right = left_right_matchings.get_matching_entry( - cur_left.unwrap().to_owned(), - cur_right.unwrap().to_owned(), - ); + let bidirectional_matching_left_right = + left_right_matchings.get_matching_entry(cur_left.unwrap(), cur_right.unwrap()); let left_matching_in_right = left_right_matchings.find_matching_for(cur_left.unwrap()); let right_matching_in_left = From 802ce858127424fc4405bdbf363b3afbe9926133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Henrique?= Date: Sat, 28 Oct 2023 03:33:22 +0000 Subject: [PATCH 2/2] refactor(merge,model): Use ref instead of box --- merge/src/odered_merge.rs | 64 +++++++++++++++++++-------------------- model/src/cst_node.rs | 4 +-- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/merge/src/odered_merge.rs b/merge/src/odered_merge.rs index 1355ab1..c74d61c 100644 --- a/merge/src/odered_merge.rs +++ b/merge/src/odered_merge.rs @@ -106,8 +106,8 @@ pub fn ordered_merge<'a>( (None, Some(_), Some(_), None, Some(matching_base_right)) => { if !matching_base_right.is_perfect_match { result_children.push(CSTNode::Conflict { - left: Box::new(None), - right: Box::new(Some(cur_right.unwrap().to_owned())), + left: None, + right: Some(cur_right.unwrap()), }); } @@ -120,7 +120,7 @@ pub fn ordered_merge<'a>( } (None, Some(_), None, None, Some(matching_base_right)) => { if !matching_base_right.is_perfect_match { - result_children.push(CSTNode::Conflict { left: None.into(), right: Some(cur_right.unwrap().to_owned()).into()}) + result_children.push(CSTNode::Conflict { left: None.into(), right: Some(cur_right.unwrap()).into()}) } cur_right = children_right_it.next(); } @@ -131,8 +131,8 @@ pub fn ordered_merge<'a>( (None, None, Some(matching_base_left), Some(_), Some(_)) => { if !matching_base_left.is_perfect_match { result_children.push(CSTNode::Conflict { - left: Box::new(Some(cur_left.unwrap().to_owned())), - right: Box::new(None), + left: Some(cur_left.unwrap()), + right: None, }); } @@ -140,7 +140,7 @@ pub fn ordered_merge<'a>( } (None, None, Some(matching_base_left), Some(_), None) => { if !matching_base_left.is_perfect_match { - result_children.push(CSTNode::Conflict { left: Some(cur_left.unwrap().to_owned()).into(), right: None.into() }) + result_children.push(CSTNode::Conflict { left: Some(cur_left.unwrap()).into(), right: None.into() }) } cur_left = children_left_it.next(); } @@ -149,20 +149,20 @@ pub fn ordered_merge<'a>( (true, true) => {} (true, false) => { result_children.push(CSTNode::Conflict { - left: Some(cur_left.unwrap().to_owned()).into(), + left: Some(cur_left.unwrap()).into(), right: None.into() }) } (false, true) => { result_children.push(CSTNode::Conflict { left: None.into(), - right: Some(cur_right.unwrap().to_owned()).into() + right: Some(cur_right.unwrap()).into() }) } (false, false) => { result_children.push(CSTNode::Conflict { - left: Some(cur_left.unwrap().to_owned()).into(), - right: Some(cur_right.unwrap().to_owned()).into() + left: Some(cur_left.unwrap()).into(), + right: Some(cur_right.unwrap()).into() }) } }; @@ -175,8 +175,8 @@ pub fn ordered_merge<'a>( if !matching_base_left.is_perfect_match { result_children.push(CSTNode::Conflict { - left: Box::new(Some(cur_left.unwrap().to_owned())), - right: Box::new(None), + left: Some(cur_left.unwrap()), + right: None, }) } @@ -197,8 +197,8 @@ pub fn ordered_merge<'a>( if !matching_base_right.is_perfect_match { result_children.push(CSTNode::Conflict { - left: Box::new(None), - right: Box::new(Some(cur_right.unwrap().to_owned())), + left: None, + right: Some(cur_right.unwrap()), }) } @@ -207,8 +207,8 @@ pub fn ordered_merge<'a>( } (None, None, None, None, None) => { result_children.push(CSTNode::Conflict { - left: Box::new(Some(cur_left.unwrap().to_owned())), - right: Box::new(Some(cur_right.unwrap().to_owned())), + left: Some(cur_left.unwrap()), + right: Some(cur_right.unwrap()), }); cur_left = children_left_it.next(); @@ -659,14 +659,14 @@ mod tests { }], }, CSTNode::Conflict { - left: Box::new(None), - right: Box::new(Some(CSTNode::NonTerminal { + left: None, + right: Some(&CSTNode::NonTerminal { kind: "subtree".into(), children: vec![CSTNode::Terminal { kind: "kind_c".into(), value: "value_c".into(), }], - })), + }), }, ], }, @@ -685,14 +685,14 @@ mod tests { }], }, CSTNode::Conflict { - left: Box::new(Some(CSTNode::NonTerminal { + left: Some(&CSTNode::NonTerminal { kind: "subtree".into(), children: vec![CSTNode::Terminal { kind: "kind_c".into(), value: "value_c".into(), }], - })), - right: Box::new(None), + }), + right: None, }, ], }, @@ -730,14 +730,14 @@ mod tests { &CSTNode::NonTerminal { kind: "kind".into(), children: vec![CSTNode::Conflict { - left: Box::new(Some(CSTNode::Terminal { + left: Some(&CSTNode::Terminal { kind: "kind_a".into(), value: "value_a".into(), - })), - right: Box::new(Some(CSTNode::Terminal { + }), + right: Some(&CSTNode::Terminal { kind: "kind_b".into(), value: "value_b".into(), - })), + }), }], }, ) @@ -847,7 +847,7 @@ mod tests { kind: "kind".into(), children: vec![ CSTNode::Conflict { - left: Some(CSTNode::NonTerminal { + left: Some(&CSTNode::NonTerminal { kind: "subtree".into(), children: vec![CSTNode::Terminal { kind: "kind_c".into(), @@ -874,7 +874,7 @@ mod tests { children: vec![ CSTNode::Conflict { left: None.into(), - right: Some(CSTNode::NonTerminal { + right: Some(&CSTNode::NonTerminal { kind: "subtree".into(), children: vec![CSTNode::Terminal { kind: "kind_c".into(), @@ -1061,7 +1061,7 @@ mod tests { children: vec![ CSTNode::Conflict { left: None.into(), - right: Some(CSTNode::NonTerminal { + right: Some(&CSTNode::NonTerminal { kind: "subtree", children: vec![CSTNode::Terminal { kind: "kind_b", @@ -1085,7 +1085,7 @@ mod tests { kind: "kind".into(), children: vec![ CSTNode::Conflict { - left: Some(CSTNode::NonTerminal { + left: Some(&CSTNode::NonTerminal { kind: "subtree", children: vec![CSTNode::Terminal { kind: "kind_b", @@ -1252,7 +1252,7 @@ mod tests { &CSTNode::NonTerminal { kind: "kind", children: vec![CSTNode::Conflict { - left: Some(CSTNode::NonTerminal { + left: Some(&CSTNode::NonTerminal { kind: "subtree_b", children: vec![CSTNode::Terminal { kind: "kind_c", @@ -1272,7 +1272,7 @@ mod tests { kind: "kind", children: vec![CSTNode::Conflict { left: None.into(), - right: Some(CSTNode::NonTerminal { + right: Some(&CSTNode::NonTerminal { kind: "subtree_b", children: vec![CSTNode::Terminal { kind: "kind_c", diff --git a/model/src/cst_node.rs b/model/src/cst_node.rs index 3cd1bbe..0b7d55a 100644 --- a/model/src/cst_node.rs +++ b/model/src/cst_node.rs @@ -9,8 +9,8 @@ pub enum CSTNode<'a> { children: Vec>, }, Conflict { - left: Box>>, - right: Box>>, + left: Option<&'a CSTNode<'a>>, + right: Option<&'a CSTNode<'a>>, }, }