Set up a tree container that allows for some certain traversals #79

Merged
savanni merged 6 commits from tree-traversals into main 2023-10-25 03:13:39 +00:00
1 changed files with 7 additions and 6 deletions
Showing only changes of commit c2e78d7c54 - Show all commits

View File

@ -27,7 +27,7 @@ impl<T> Tree<T> {
} }
/// Use a breadth-first-search pattern to find a node, returning the node if found. /// Use a breadth-first-search pattern to find a node, returning the node if found.
pub fn find_bfs<'a, F>(&'a self, op: F) -> Option<Node<T>> pub fn find_bfs<F>(&self, op: F) -> Option<Node<T>>
where where
F: FnOnce(&T) -> bool + Copy, F: FnOnce(&T) -> bool + Copy,
{ {
@ -84,14 +84,15 @@ impl<T> Node<T> {
}))) })))
} }
// I am copying the value because I don't have a mechanism for keeping the borrow ref active. /// Immutably retrieve the data in this node.
// My next step is to figure out how to keep the borrow ref active so that I don't have to pub fn value<'a>(&self) -> Ref<T> {
// clone the item. Then I could drop the Clone constraint. // Ref::map is not actually a member function. I don't know why this was done, other than
pub fn value<'a>(&'a self) -> Ref<T> { // maybe to avoid conflicting with other `map` declarations. Why that is necessary when
// Option::map exists as a member, I don't know.
Ref::map(self.0.borrow(), |v| &v.value) Ref::map(self.0.borrow(), |v| &v.value)
} }
pub fn children<'a>(&'a self) -> Ref<Vec<Node<T>>> { pub fn children<'a>(&self) -> Ref<Vec<Node<T>>> {
Ref::map(self.0.borrow(), |v| &v.children) Ref::map(self.0.borrow(), |v| &v.children)
} }