Skip to content

Commit

Permalink
Implement node depth calculation (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
twizmwazin authored Nov 5, 2024
1 parent a59ebf2 commit babcafb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 15 additions & 5 deletions crates/clarirs_core/src/ast/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ impl<'c, O: Op<'c> + Serialize> AstNode<'c, O> {
.child_iter()
.flat_map(|child| child.variables().clone().into_iter())
.collect::<HashSet<String>>();
let depth = op.depth();

Self {
op,
ctx,
hash,
symbolic,
variables,
depth: 0, // TODO: Implement depth calculation
depth,
}
}

Expand All @@ -94,17 +95,17 @@ impl<'c, O: Op<'c> + Serialize> AstNode<'c, O> {
pub fn variables(&self) -> &HashSet<String> {
&self.variables
}

pub fn depth(&self) -> u32 {
self.depth
}
}

impl<'c, O: Op<'c>> Op<'c> for AstNode<'c, O> {
fn child_iter(&self) -> IntoIter<VarAst<'c>> {
self.op.child_iter()
}

fn depth(&self) -> u32 {
self.depth
}

fn is_true(&self) -> bool {
self.op.is_true()
}
Expand Down Expand Up @@ -142,6 +143,15 @@ impl<'c> Op<'c> for VarAst<'c> {
}
}

fn depth(&self) -> u32 {
match self {
VarAst::Boolean(ast) => ast.depth(),
VarAst::BitVec(ast) => ast.depth(),
VarAst::Float(ast) => ast.depth(),
VarAst::String(ast) => ast.depth(),
}
}

fn is_true(&self) -> bool {
match self {
VarAst::Boolean(ast) => ast.is_true(),
Expand Down
4 changes: 4 additions & 0 deletions crates/clarirs_core/src/ast/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ pub trait Op<'c>: Debug + Hash + Serialize {
self.child_iter().collect()
}

fn depth(&self) -> u32 {
1 + self.children().iter().map(|c| c.depth()).max().unwrap_or(0)
}

fn is_true(&self) -> bool {
false
}
Expand Down

0 comments on commit babcafb

Please sign in to comment.