Skip to content

Commit

Permalink
ditto
Browse files Browse the repository at this point in the history
  • Loading branch information
shnarazk committed Feb 3, 2024
1 parent b56eee9 commit 3991193
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
9 changes: 5 additions & 4 deletions src/bdd.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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]
Expand Down
22 changes: 6 additions & 16 deletions src/node.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Element type for Decision Diagrams
use {
crate::types::DecisionDiagram,
crate::types::{DecisionDiagram, DecisionDiagramNode},
std::{
collections::{HashMap, HashSet},
hash::Hash,
Expand All @@ -10,17 +11,6 @@ use {

pub type Node = Rc<Vertex>;

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<bool>;
// 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<usize>;
fn low(&self) -> Option<&Self>;
fn high(&self) -> Option<&Self>;
}

#[derive(Clone, Debug)]
pub enum Vertex {
Bool(bool),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
28 changes: 21 additions & 7 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
//! Types and traits
use std::{collections::HashSet, io};

pub(crate) type BooleanOperator = (Box<dyn Fn(bool, bool) -> 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<dyn Fn(bool, bool) -> bool>, unit: bool, _other: &Self) -> Self;
}

pub trait DecisionDiagram {
type Element;
// return the hashset of all (non)terminal nodes in graph.
Expand All @@ -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<bool>;
// 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<usize>;
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<dyn Fn(bool, bool) -> bool>, unit: bool, _other: &Self) -> Self;
}
1 change: 1 addition & 0 deletions src/zdd.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Zero-suppressed Decision Diagram
use {
crate::{
node::Node,
Expand Down

0 comments on commit 3991193

Please sign in to comment.