From 0969b308d72971935b7049c65beac6f8cdbb5c69 Mon Sep 17 00:00:00 2001 From: Joao Duarte Date: Mon, 4 Dec 2023 23:54:50 -0300 Subject: [PATCH] refactor: Use unordered pair implementation from crates.io --- Cargo.lock | 14 ++--- Cargo.toml | 3 +- bin/Cargo.toml | 1 - matching/Cargo.toml | 2 +- matching/src/matchings.rs | 4 +- matching/src/ordered_tree_matching.rs | 11 ++-- matching/src/unordered_tree_matching.rs | 4 +- merge/Cargo.toml | 1 - utils/Cargo.toml | 8 --- utils/src/lib.rs | 1 - utils/src/unordered_pair.rs | 73 ------------------------- 11 files changed, 17 insertions(+), 105 deletions(-) delete mode 100644 utils/Cargo.toml delete mode 100644 utils/src/lib.rs delete mode 100644 utils/src/unordered_pair.rs diff --git a/Cargo.lock b/Cargo.lock index 3030f3c..c5ab313 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -84,7 +84,6 @@ dependencies = [ "merge", "model", "parsing", - "utils", ] [[package]] @@ -203,7 +202,7 @@ name = "matching" version = "0.1.0" dependencies = [ "model", - "utils", + "unordered-pair", ] [[package]] @@ -219,7 +218,6 @@ dependencies = [ "diffy", "matching", "model", - "utils", ] [[package]] @@ -389,16 +387,18 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unordered-pair" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d82c34093ea7dd15b4e42ecbc22adf732b466ac6e03abc83a56741fad611b0ec" + [[package]] name = "utf8parse" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" -[[package]] -name = "utils" -version = "0.1.0" - [[package]] name = "wait-timeout" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 5c918bf..3cf16d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,5 @@ members = [ "matching", "merge", "parsing", - "model", - "utils" + "model" ] \ No newline at end of file diff --git a/bin/Cargo.toml b/bin/Cargo.toml index 53df9a6..7680738 100644 --- a/bin/Cargo.toml +++ b/bin/Cargo.toml @@ -10,7 +10,6 @@ merge = { path = "../merge" } model = { path = "../model" } parsing = { path = "../parsing" } matching = { path = "../matching" } -utils = { path = "../utils" } assert_cmd = "2.0.12" clap = { version = "4.4.8", features = ["derive"] } diff --git a/matching/Cargo.toml b/matching/Cargo.toml index b8fd8b1..8c46f5b 100644 --- a/matching/Cargo.toml +++ b/matching/Cargo.toml @@ -7,4 +7,4 @@ edition = "2021" [dependencies] model = { path = "../model" } -utils = { path = "../utils" } +unordered-pair = "0.2.4" diff --git a/matching/src/matchings.rs b/matching/src/matchings.rs index 7dcb535..0f52797 100644 --- a/matching/src/matchings.rs +++ b/matching/src/matchings.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use model::CSTNode; -use utils::unordered_pair::UnorderedPair; +use unordered_pair::UnorderedPair; use crate::matching::Matching; use crate::matching_entry::MatchingEntry; @@ -47,7 +47,7 @@ impl<'a> Matchings<'a> { left: &'a CSTNode<'a>, right: &'a CSTNode<'a>, ) -> Option<&MatchingEntry> { - self.matching_entries.get(&UnorderedPair::new(left, right)) + self.matching_entries.get(&UnorderedPair(left, right)) } pub fn has_bidirectional_matching( diff --git a/matching/src/ordered_tree_matching.rs b/matching/src/ordered_tree_matching.rs index 7987949..c077c2b 100644 --- a/matching/src/ordered_tree_matching.rs +++ b/matching/src/ordered_tree_matching.rs @@ -1,6 +1,6 @@ use crate::{calculate_matchings, matching_entry::MatchingEntry, Matchings}; use model::CSTNode; -use utils::unordered_pair::UnorderedPair; +use unordered_pair::UnorderedPair; #[derive(PartialEq, Eq, Debug, Clone)] enum Direction { @@ -72,7 +72,7 @@ pub fn ordered_tree_matching<'a>(left: &'a CSTNode, right: &'a CSTNode) -> Match let mut j = n; let mut matchings = Matchings::from_single( - UnorderedPair::new(left, right), + UnorderedPair(left, right), MatchingEntry::new(matrix_m[m][n] + root_matching, left == right), ); @@ -106,14 +106,11 @@ pub fn ordered_tree_matching<'a>(left: &'a CSTNode, right: &'a CSTNode) -> Match ) => { let is_perfetch_match = kind_left == kind_right && value_left == value_right; Matchings::from_single( - UnorderedPair::new(left, right), + UnorderedPair(left, right), MatchingEntry::new(is_perfetch_match.into(), is_perfetch_match), ) } - (_, _) => Matchings::from_single( - UnorderedPair::new(left, right), - MatchingEntry::new(0, false), - ), + (_, _) => Matchings::from_single(UnorderedPair(left, right), MatchingEntry::new(0, false)), } } diff --git a/matching/src/unordered_tree_matching.rs b/matching/src/unordered_tree_matching.rs index 88e5f10..995991d 100644 --- a/matching/src/unordered_tree_matching.rs +++ b/matching/src/unordered_tree_matching.rs @@ -1,5 +1,5 @@ use model::CSTNode; -use utils::unordered_pair::UnorderedPair; +use unordered_pair::UnorderedPair; use crate::{calculate_matchings, MatchingEntry, Matchings}; @@ -19,7 +19,7 @@ pub fn unordered_tree_matching<'a>(left: &'a CSTNode, right: &'a CSTNode) -> cra ) => { let is_perfetch_match = kind_left == kind_right && value_left == value_right; Matchings::from_single( - UnorderedPair::new(left, right), + UnorderedPair(left, right), MatchingEntry::new(is_perfetch_match.into(), is_perfetch_match), ) } diff --git a/merge/Cargo.toml b/merge/Cargo.toml index dd2d04c..89fba63 100644 --- a/merge/Cargo.toml +++ b/merge/Cargo.toml @@ -8,5 +8,4 @@ edition = "2021" [dependencies] model = { path = "../model" } matching = { path = "../matching" } -utils = { path = "../utils" } diffy = "0.3.0" \ No newline at end of file diff --git a/utils/Cargo.toml b/utils/Cargo.toml deleted file mode 100644 index a0cba9a..0000000 --- a/utils/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "utils" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/utils/src/lib.rs b/utils/src/lib.rs deleted file mode 100644 index b3cfe69..0000000 --- a/utils/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod unordered_pair; diff --git a/utils/src/unordered_pair.rs b/utils/src/unordered_pair.rs deleted file mode 100644 index c558b6c..0000000 --- a/utils/src/unordered_pair.rs +++ /dev/null @@ -1,73 +0,0 @@ -use std::cmp::Ordering; -use std::hash::{Hash, Hasher}; - -#[derive(Clone, Debug)] -pub struct UnorderedPair(pub T, pub T); - -impl UnorderedPair { - pub fn new(a: T, b: T) -> Self { - if a < b { - Self(a, b) - } else { - Self(b, a) - } - } -} - -impl From<(T, T)> for UnorderedPair { - fn from(t: (T, T)) -> Self { - Self::new(t.0, t.1) - } -} - -impl> PartialEq> for UnorderedPair { - fn eq(&self, rhs: &Self) -> bool { - (self.0 == rhs.0 && self.1 == rhs.1) || (self.0 == rhs.1 && self.1 == rhs.0) - } -} - -impl Eq for UnorderedPair {} - -impl PartialOrd for UnorderedPair { - fn partial_cmp(&self, rhs: &Self) -> Option { - match self.0.partial_cmp(&rhs.0) { - Some(Ordering::Equal) => self.1.partial_cmp(&rhs.1), - v => v, - } - } -} - -impl Ord for UnorderedPair { - fn cmp(&self, rhs: &Self) -> std::cmp::Ordering { - match self.0.cmp(&rhs.0) { - Ordering::Equal => self.1.cmp(&rhs.1), - v => v, - } - } -} - -impl Hash for UnorderedPair { - fn hash(&self, hasher: &mut H) { - self.0.hash(hasher); - self.1.hash(hasher); - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn if_both_have_same_order_they_are_equal() { - let left = "This is a value"; - let right = "This is another value"; - assert_eq!(UnorderedPair(left, right), UnorderedPair(left, right)) - } - - #[test] - fn does_not_take_order_into_account_for_equality() { - let left = "This is a value"; - let right = "This is another value"; - assert_eq!(UnorderedPair(left, right), UnorderedPair(right, left)) - } -}