From a66115491969d7703a0a77e81b1a18938165c103 Mon Sep 17 00:00:00 2001 From: elvis Date: Wed, 16 Jul 2025 18:34:33 +0200 Subject: [PATCH] Better documentation, formatting code to 80 char --- src/rsprocess/bisimilarity.rs | 56 +++++++++++++++++++++-------------- src/rsprocess/presets.rs | 41 +++++++++++++++++-------- 2 files changed, 63 insertions(+), 34 deletions(-) diff --git a/src/rsprocess/bisimilarity.rs b/src/rsprocess/bisimilarity.rs index 372aa14..d399475 100644 --- a/src/rsprocess/bisimilarity.rs +++ b/src/rsprocess/bisimilarity.rs @@ -1,26 +1,16 @@ use std::collections::{BTreeSet, HashMap}; -use petgraph::visit::{ EdgeIndexable, - EdgeRef, +use petgraph::visit::{ EdgeRef, + GraphBase, IntoEdgeReferences, IntoEdges, - IntoNodeReferences, - NodeIndexable }; - -struct GraphPartition<'a, G> -where - G: EdgeIndexable + NodeIndexable + IntoEdgeReferences + IntoNodeReferences - + IntoEdges, - G::NodeId: std::cmp::Eq + std::hash::Hash, -{ - pub node_to_block: HashMap<(usize, G::NodeId), u32>, - pub block_to_node: HashMap>, - pub graphs: [&'a G; 2], - last_block: u32, - blocks: BTreeSet -} + IntoNodeReferences }; +// ----------------------------------------------------------------------------- +// Helper Functions +// ----------------------------------------------------------------------------- +#[inline(always)] fn equal_vectors(a: &Vec, b: &Vec) -> bool where T: PartialEq @@ -39,11 +29,27 @@ where true } + +// ----------------------------------------------------------------------------- +// Bisimilarity +// ----------------------------------------------------------------------------- + +struct GraphPartition<'a, G> +where + G: GraphBase, + G::NodeId: std::cmp::Eq + std::hash::Hash, +{ + pub node_to_block: HashMap<(usize, G::NodeId), u32>, + pub block_to_node: HashMap>, + pub graphs: [&'a G; 2], + last_block: u32, + blocks: BTreeSet +} + impl<'a, G> GraphPartition<'a, G> where - G: EdgeIndexable + NodeIndexable + IntoEdgeReferences + IntoNodeReferences - + IntoEdges, - G::NodeId: std::cmp::Eq + std::hash::Hash, + G: GraphBase, + G::NodeId: std::cmp::Eq + std::hash::Hash { pub fn new(graph_a: &'a G, graph_b: &'a G) -> Self { GraphPartition { node_to_block: HashMap::new(), @@ -63,6 +69,7 @@ where self.blocks.insert(self.last_block); } + #[inline(always)] pub fn iterate_blocks(&self) -> Vec { self.blocks.iter().cloned().collect::>() } @@ -78,7 +85,13 @@ where } true } +} +impl<'a, G> GraphPartition<'a, G> +where + G: IntoEdges, + G::NodeId: std::cmp::Eq + std::hash::Hash, +{ fn reachable_blocks( &self, label: &G::EdgeRef, @@ -145,8 +158,7 @@ pub fn bisimilarity_kanellakis_smolka<'a, G>( graph_b: &'a G ) -> bool where - G: EdgeIndexable + NodeIndexable + IntoEdgeReferences + IntoNodeReferences - + IntoEdges, + G: IntoNodeReferences + IntoEdges, G::NodeId: std::cmp::Eq + std::hash::Hash, G::EdgeRef: PartialEq { diff --git a/src/rsprocess/presets.rs b/src/rsprocess/presets.rs index 73eb9ed..3b77583 100644 --- a/src/rsprocess/presets.rs +++ b/src/rsprocess/presets.rs @@ -516,8 +516,6 @@ pub fn bisimilar( system_b: String ) -> Result { - digraph(system_a)?; - let system_b = read_file(system_a.get_translator(), system_b.to_string(), parser_instructions)?; @@ -529,15 +527,17 @@ pub fn bisimilar( EvaluatedSystem::Graph { graph, translator } => { if translator != *system_a.get_translator() { return Err("Bisimilarity not implemented for systems with \ - different encodings. Serialize the systems with \ - the same translator.".into()); + different encodings. Serialize the systems \ + with the same translator.".into()); } EvaluatedSystem::Graph { graph, translator } } }; + digraph(system_a)?; digraph(&mut system_b)?; + // since we ran digraph on both they have to be graphs match (system_a, &system_b) { (EvaluatedSystem::Graph { graph: a, translator: _ }, EvaluatedSystem::Graph { graph: b, translator: _ }) => { @@ -545,7 +545,10 @@ pub fn bisimilar( // and not &mut graph::RSgraph let a: &graph::RSgraph = a; let b: &graph::RSgraph = b; - Ok(format!("{}", super::bisimilarity::bisimilarity_kanellakis_smolka(&a, &b))) + Ok(format!( + "{}", + super::bisimilarity::bisimilarity_kanellakis_smolka(&a, &b) + )) }, _ => { unreachable!() } } @@ -565,7 +568,8 @@ fn generate_node_pringting_fn<'a>( // We are iterating over the node_display and constructing a function // (accumulator) that prints out our formatted nodes. So at each step we // call the previous function and add the next string or function. - let mut accumulator: Box String> = + let mut accumulator: + Box String> = Box::new(|_, _| String::new()); for nd in node_display { accumulator = match nd { @@ -575,7 +579,8 @@ fn generate_node_pringting_fn<'a>( let val = translator.clone(); Box::new(move |i, n| { (accumulator)(i, n) - + &graph::GraphMapNodesTy::from(d.clone(), val.clone()).get()(i, n) + + &graph::GraphMapNodesTy::from(d.clone(), + val.clone()).get()(i, n) }) }, NodeDisplay::Separator(s) => { @@ -628,7 +633,8 @@ fn generate_edge_pringting_fn<'a>( // We are iterating over the edge_display and constructing a function // (accumulator) that prints out our formatted nodes. So at each step we // call the previous function and add the next string or function. - let mut accumulator: Box String> = + let mut accumulator: + Box String> = Box::new(|_, _| String::new()); for nd in edge_display { accumulator = match nd { @@ -638,7 +644,8 @@ fn generate_edge_pringting_fn<'a>( let val = translator.clone(); Box::new(move |i, n| { (accumulator)(i, n) - + &graph::GraphMapEdgesTy::from(d.clone(), val.clone()).get()(i, n) + + &graph::GraphMapEdgesTy::from(d.clone(), + val.clone()).get()(i, n) }) } EdgeDisplay::Separator(s) => { @@ -656,7 +663,11 @@ fn generate_node_color_fn<'a>( node_color: &'a graph::NodeColor, original_graph: Rc, translator: Rc, -) -> Box, <&Graph as IntoNodeReferences>::NodeRef) -> String + 'a> { +) -> Box, + <&Graph as IntoNodeReferences>::NodeRef +) -> String + 'a> +{ Box::new( move |i, n| { let cloned_node_color = node_color.clone(); @@ -680,7 +691,11 @@ use petgraph::visit::IntoEdgeReferences; fn generate_edge_color_fn<'a>( edge_color: &'a graph::EdgeColor, original_graph: Rc>, -) -> Box, <&Graph as IntoEdgeReferences>::EdgeRef) -> String + 'a> { +) -> Box, + <&Graph as IntoEdgeReferences>::EdgeRef +) -> String + 'a> +{ Box::new( move |i, n| { let cloned_edge_color = edge_color.clone(); @@ -727,7 +742,9 @@ pub fn dot( let graph = Rc::new(graph.to_owned()); let node_formatter = - generate_node_color_fn(node_color, Rc::clone(&graph), rc_translator); + generate_node_color_fn(node_color, + Rc::clone(&graph), + rc_translator); let edge_formatter = generate_edge_color_fn(edge_color, graph);