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 3991193 commit 4447740
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
19 changes: 9 additions & 10 deletions src/bdd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ use {
};

#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct BDD {
graph: Node,
pub struct BDD<N: DecisionDiagramNode> {
graph: N,
phantom: PhantomData<()>,
}

impl BDD {
pub fn new_from(graph: Node) -> BDD {
impl BDD<Node> {
pub fn new_from(graph: Node) -> Self {
let mut bdd = BDD {
graph: graph.clone(),
..Default::default()
Expand All @@ -29,9 +29,8 @@ impl BDD {
}
}

impl DecisionDiagram for BDD {
type Element = Node;
fn all_nodes(&self) -> HashSet<&Node> {
impl<N: DecisionDiagram<N> + DecisionDiagramNode> DecisionDiagram<N> for BDD<N> {
fn all_nodes(&self) -> HashSet<&N> {
self.graph.all_nodes()
}
fn len(&self) -> usize {
Expand All @@ -42,7 +41,7 @@ impl DecisionDiagram for BDD {
}
}

impl ReducedDecisionDiagram for BDD {
impl ReducedDecisionDiagram for BDD<Node> {
// convert tree to BDD
fn reduce(&mut self) {
let root = self.graph.clone();
Expand Down Expand Up @@ -119,7 +118,7 @@ impl ReducedDecisionDiagram for BDD {
.unwrap()
.clone();
}
fn apply(&self, op: Box<dyn Fn(bool, bool) -> bool>, unit: bool, other: &Self) -> BDD {
fn apply(&self, op: Box<dyn Fn(bool, bool) -> bool>, unit: bool, other: &Self) -> BDD<Node> {
let mut from_index: HashMap<usize, Node> = HashMap::new();
from_index.insert(0, Node::new_constant(false));
from_index.insert(1, Node::new_constant(true));
Expand Down Expand Up @@ -269,7 +268,7 @@ mod test {
fn test() {
let f = Node::new_constant(false);
let n: Node = Node::new_var(2, f.clone(), f.clone());
let bdd: BDD = BDD::new_from(n);
let bdd: BDD<Node> = BDD::new_from(n);
assert_eq!(bdd.len(), 1);
}
}
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ fn main() {
.write_as_gv(f4)
.expect("fail to serialize");

let x1x3: BDD = BDD::new_from(example::x1x3());
let x1x3: BDD<Node> = BDD::new_from(example::x1x3());
let x1x3f = File::create("x1x2-bdd.gv").expect("fail to create");
x1x3.write_as_gv(x1x3f).expect("fail to save");
let x2x3: BDD = BDD::new_from(example::x2x3());
let x2x3: BDD<Node> = BDD::new_from(example::x2x3());
let x2x3f = File::create("x2x3-bdd.gv").expect("fail to create");
x2x3.write_as_gv(x2x3f).expect("fail to save");
let applied: BDD = x1x3.apply(Box::new(|a, b| a | b), true, &x2x3);
let applied: BDD<Node> = x1x3.apply(Box::new(|a, b| a | b), true, &x2x3);
let applyf = File::create("apply-bdd.gv").expect("fail to create");
applied.write_as_gv(applyf).expect("fail to save");
}
19 changes: 9 additions & 10 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ impl Default for Vertex {
}
}

impl DecisionDiagram for Node {
type Element = Node;
impl DecisionDiagram<Node> for Node {
/// returns the number of nodes under self and self itself.
///```
/// use ddir::dd::DecisionDiagram;
/// use ddir::node::{DecisionDiagramNode, Node};
/// use ddir::node::Node;
/// use ddir::types::{DecisionDiagram, DecisionDiagramNode};
///
/// let f = Node::new_constant(false);
/// assert_eq!(f.len(), 1);
Expand All @@ -60,8 +59,8 @@ impl DecisionDiagram for Node {
}
/// returns all nodes under self and self itself.
///```
/// use ddir::dd::DecisionDiagram;
/// use ddir::node::{DecisionDiagramNode, Node};
/// use ddir::node::Node;
/// use ddir::types::{DecisionDiagram, DecisionDiagramNode};
///
/// let f = Node::new_constant(false);
/// let n = Node::new_var(2, f.clone(), f.clone());
Expand Down Expand Up @@ -160,8 +159,8 @@ impl DecisionDiagramNode for Node {
}
/// returns `None` if self is a non-terminal node.
///```
/// use ddir::dd::DecisionDiagram;
/// use ddir::node::{DecisionDiagramNode, Node};
/// use ddir::node::Node;
/// use ddir::types::{DecisionDiagram, DecisionDiagramNode};
///
/// let f = Node::new_constant(false);
/// assert!(f.is_constant().is_some());
Expand All @@ -180,8 +179,8 @@ impl DecisionDiagramNode for Node {
}
/// returns the number of nodes under self and self itself.
///```
/// use ddir::dd::DecisionDiagram;
/// use ddir::node::{DecisionDiagramNode, Node};
/// use ddir::node::Node;
/// use ddir::types::{DecisionDiagram, DecisionDiagramNode};
///
/// let f = Node::new_constant(false);
/// let n = Node::new_var(2, f.clone(), f.clone());
Expand Down
18 changes: 10 additions & 8 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//! Types and traits
use std::{collections::HashSet, io};
use std::{collections::HashSet, hash::Hash, io};

pub(crate) type BooleanOperator = (Box<dyn Fn(bool, bool) -> bool>, bool);

pub trait DecisionDiagram {
type Element;
pub trait DecisionDiagram<N: DecisionDiagramNode> {
// return the hashset of all (non)terminal nodes in graph.
fn all_nodes(&self) -> HashSet<&Self::Element>;
fn all_nodes(&self) -> HashSet<&N>;
// return the number of (non)terminal nodes in graph.
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
Expand All @@ -16,16 +15,19 @@ pub trait DecisionDiagram {
fn write_as_gv(&self, sink: impl io::Write) -> io::Result<()>;
}

pub trait DecisionDiagramNode {
// return a new terminal node
pub trait DecisionDiagramNode: Clone + Default + Eq + Hash {
/// return a new terminal node
fn new_constant(b: bool) -> Self;
// return a new non-terminal node
/// 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`.
/// return 0 or 1 for terminal nodes, and `vi + 2` for nonterminal node which var_index is `vi`.
fn unified_key(&self) -> usize;
/// return the decision var
fn var_index(&self) -> Option<usize>;
/// return the node element for the decision var == false
fn low(&self) -> Option<&Self>;
/// return the node element for the decision var == true
fn high(&self) -> Option<&Self>;
}

Expand Down
18 changes: 9 additions & 9 deletions src/zdd.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
//! Zero-suppressed Decision Diagram

use {
crate::{
node::Node,
types::{DecisionDiagram, ReducedDecisionDiagram},
types::{DecisionDiagram, DecisionDiagramNode, ReducedDecisionDiagram},
},
std::{collections::HashSet, io, marker::PhantomData},
};

#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct ZDD {
graph: Node,
pub struct ZDD<N> {
graph: N,
phantom: PhantomData<()>,
}

impl ZDD {
pub fn new_from(graph: Node) -> ZDD {
impl ZDD<Node> {
pub fn new_from(graph: Node) -> ZDD<Node> {
let mut zdd = ZDD {
graph: graph.clone(),
..Default::default()
Expand All @@ -24,9 +25,8 @@ impl ZDD {
}
}

impl DecisionDiagram for ZDD {
type Element = Node;
fn all_nodes(&self) -> HashSet<&Node> {
impl<N: DecisionDiagram<N> + DecisionDiagramNode> DecisionDiagram<N> for ZDD<N> {
fn all_nodes(&self) -> HashSet<&N> {
self.graph.all_nodes()
}
fn len(&self) -> usize {
Expand All @@ -37,7 +37,7 @@ impl DecisionDiagram for ZDD {
}
}

impl ReducedDecisionDiagram for ZDD {
impl ReducedDecisionDiagram for ZDD<Node> {
fn reduce(&mut self) {
todo!()
}
Expand Down

0 comments on commit 4447740

Please sign in to comment.