From 3991193c9847334fe889bc8da07bbf7e465522c4 Mon Sep 17 00:00:00 2001 From: "Narazaki, Shuji" Date: Sat, 3 Feb 2024 09:09:44 +0900 Subject: [PATCH] ditto --- src/bdd.rs | 9 +++++---- src/node.rs | 22 ++++++---------------- src/types.rs | 28 +++++++++++++++++++++------- src/zdd.rs | 1 + 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/bdd.rs b/src/bdd.rs index 9d0bc7a..b1c4634 100644 --- a/src/bdd.rs +++ b/src/bdd.rs @@ -1,7 +1,8 @@ +//! Binary Decision Diagram use { crate::{ - node::{DecisionDiagramNode, Node, Vertex}, - types::{BooleanOperator, DecisionDiagram, ReducedDecisionDiagram}, + node::{Node, Vertex}, + types::{BooleanOperator, DecisionDiagram, DecisionDiagramNode, ReducedDecisionDiagram}, }, itertools::Itertools, std::{ @@ -260,8 +261,8 @@ impl ReducedDecisionDiagram for BDD { mod test { use crate::{ bdd::BDD, - node::{DecisionDiagramNode, Node}, - types::DecisionDiagram, + node::Node, + types::{DecisionDiagram, DecisionDiagramNode}, }; #[test] diff --git a/src/node.rs b/src/node.rs index 9c372d8..f699e80 100644 --- a/src/node.rs +++ b/src/node.rs @@ -1,5 +1,6 @@ +//! Element type for Decision Diagrams use { - crate::types::DecisionDiagram, + crate::types::{DecisionDiagram, DecisionDiagramNode}, std::{ collections::{HashMap, HashSet}, hash::Hash, @@ -10,17 +11,6 @@ use { pub type Node = Rc; -pub trait DecisionDiagramNode { - fn new_constant(b: bool) -> Self; - fn new_var(var_index: usize, low: Node, high: Node) -> Self; - fn is_constant(&self) -> Option; - // return 0 or 1 for terminal nodes, and `vi + 2` for nonterminal node which var_index is `vi`. - fn unified_key(&self) -> usize; - fn var_index(&self) -> Option; - fn low(&self) -> Option<&Self>; - fn high(&self) -> Option<&Self>; -} - #[derive(Clone, Debug)] pub enum Vertex { Bool(bool), @@ -157,11 +147,11 @@ impl DecisionDiagram for Node { impl DecisionDiagramNode for Node { /// returns a new terminal node. - fn new_constant(b: bool) -> Self { + fn new_constant(b: bool) -> Node { Rc::new(Vertex::Bool(b)) } /// returns a new non-terminal node. - fn new_var(var_index: usize, low: Node, high: Node) -> Self { + fn new_var(var_index: usize, low: Node, high: Node) -> Node { Rc::new(Vertex::Var { var_index, low, @@ -203,13 +193,13 @@ impl DecisionDiagramNode for Node { Vertex::Var { var_index, .. } => Some(var_index), } } - fn low(&self) -> Option<&Self> { + fn low(&self) -> Option<&Node> { match **self { Vertex::Bool(_) => None, Vertex::Var { ref low, .. } => Some(low), } } - fn high(&self) -> Option<&Self> { + fn high(&self) -> Option<&Node> { match **self { Vertex::Bool(_) => None, Vertex::Var { ref high, .. } => Some(high), diff --git a/src/types.rs b/src/types.rs index fc9cfaf..c6e6d67 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,14 +1,8 @@ +//! Types and traits use std::{collections::HashSet, io}; pub(crate) type BooleanOperator = (Box bool>, bool); -pub trait ReducedDecisionDiagram { - /// convert the current graph to one which is a reduced diagram - fn reduce(&mut self); - /// return a new graph generated by apply `op` to this and the other graph - fn apply(&self, op: Box bool>, unit: bool, _other: &Self) -> Self; -} - pub trait DecisionDiagram { type Element; // return the hashset of all (non)terminal nodes in graph. @@ -21,3 +15,23 @@ pub trait DecisionDiagram { // write the graph in graphvis format fn write_as_gv(&self, sink: impl io::Write) -> io::Result<()>; } + +pub trait DecisionDiagramNode { + // return a new terminal node + fn new_constant(b: bool) -> Self; + // return a new non-terminal node + fn new_var(var_index: usize, low: Self, high: Self) -> Self; + fn is_constant(&self) -> Option; + // return 0 or 1 for terminal nodes, and `vi + 2` for nonterminal node which var_index is `vi`. + fn unified_key(&self) -> usize; + fn var_index(&self) -> Option; + fn low(&self) -> Option<&Self>; + fn high(&self) -> Option<&Self>; +} + +pub trait ReducedDecisionDiagram { + /// convert the current graph to one which is a reduced diagram + fn reduce(&mut self); + /// return a new graph generated by apply `op` to this and the other graph + fn apply(&self, op: Box bool>, unit: bool, _other: &Self) -> Self; +} diff --git a/src/zdd.rs b/src/zdd.rs index 586285a..cb3cb7c 100644 --- a/src/zdd.rs +++ b/src/zdd.rs @@ -1,3 +1,4 @@ +//! Zero-suppressed Decision Diagram use { crate::{ node::Node,