From dedc621a4dc0bf5e4792a69c3f6a09d6cbd69025 Mon Sep 17 00:00:00 2001 From: Mario Carneiro Date: Mon, 20 Feb 2023 05:47:31 -0500 Subject: [PATCH] make tree ops public --- src/formula.rs | 13 ++++++++----- src/tree.rs | 24 +++++++++++++++--------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/formula.rs b/src/formula.rs index 948cc73..fcbcc17 100644 --- a/src/formula.rs +++ b/src/formula.rs @@ -25,9 +25,6 @@ use crate::scopeck::Hyp; use crate::segment_set::SegmentSet; use crate::statement::SymbolType; use crate::statement::TokenIter; -use crate::tree::NodeId; -use crate::tree::SiblingIter; -use crate::tree::Tree; use crate::util::fast_extend; use crate::util::HashMap; use crate::verify::ProofBuilder; @@ -40,6 +37,10 @@ use std::iter::FromIterator; use std::ops::Range; use std::sync::Arc; +pub use crate::tree::NodeId; +pub use crate::tree::SiblingIter; +pub use crate::tree::Tree; + /// An atom representing a typecode (for "set.mm", that's one of 'wff', 'class', 'setvar' or '|-') pub type TypeCode = Atom; @@ -163,8 +164,10 @@ impl<'a> Debug for SubstitutionsRef<'a> { #[derive(Clone, Default, Debug)] pub struct Formula { typecode: TypeCode, - tree: Arc>, - root: NodeId, + /// The underlying tree structure. + pub tree: Arc>, + /// The root of the tree + pub root: NodeId, variables: Bitset, } diff --git a/src/tree.rs b/src/tree.rs index 4afae3b..4f5e389 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -3,7 +3,8 @@ use core::ops::Index; use core::ops::IndexMut; -pub(crate) type NodeId = usize; +/// An index into the tree. +pub type NodeId = usize; #[derive(Debug)] struct TreeNode { @@ -14,7 +15,7 @@ struct TreeNode { /// A tree implementation, hopefully efficient for representing formulas #[derive(Debug)] -pub(crate) struct Tree { +pub struct Tree { nodes: Vec>, } @@ -50,7 +51,7 @@ impl Tree { /// Checked accessor to a tree node #[inline] - fn node(&self, node_id: NodeId) -> &'_ TreeNode { + fn node(&self, node_id: NodeId) -> &TreeNode { assert!(node_id > 0, "Cannot index null node!"); assert!(node_id <= self.nodes.len(), "Cannot index outside of tree!"); &self.nodes[node_id - 1] @@ -65,7 +66,8 @@ impl Tree { } /// iterator through the children of the given node - pub(crate) fn children_iter(&self, node_id: NodeId) -> SiblingIter<'_, TreeItem> { + #[must_use] + pub fn children_iter(&self, node_id: NodeId) -> SiblingIter<'_, TreeItem> { SiblingIter { tree: self, current_id: self.first_child(node_id), @@ -74,7 +76,8 @@ impl Tree { /// returns the next sibling node id, or `None` if this is the last sibling. /// This executes in O(1) - pub(crate) fn next_sibling(&self, node_id: NodeId) -> Option { + #[must_use] + pub fn next_sibling(&self, node_id: NodeId) -> Option { match self.node(node_id).next_sibling { 0 => None, node_id => Some(node_id), @@ -82,7 +85,8 @@ impl Tree { } /// returns the first child node, if any - pub(crate) fn first_child(&self, node_id: NodeId) -> Option { + #[must_use] + pub fn first_child(&self, node_id: NodeId) -> Option { match self.node(node_id).first_child { 0 => None, node_id => Some(node_id), @@ -92,12 +96,14 @@ impl Tree { /// returns the child node with the given index among children nodes /// Indices starts with 0, so querying index 0 will return the first child node, if any. /// This executes in O(n) - pub(crate) fn nth_child(&self, node_id: NodeId, index: usize) -> Option { + #[must_use] + pub fn nth_child(&self, node_id: NodeId, index: usize) -> Option { self.children_iter(node_id).nth(index) } /// returns whether the given node has children or not - pub(crate) fn has_children(&self, node_id: NodeId) -> bool { + #[must_use] + pub fn has_children(&self, node_id: NodeId) -> bool { self.node(node_id).first_child != 0 } @@ -153,7 +159,7 @@ impl Clone for Tree { /// An iterator through sibling nodes #[derive(Debug)] -pub(crate) struct SiblingIter<'a, TreeItem> { +pub struct SiblingIter<'a, TreeItem> { tree: &'a Tree, current_id: Option, }