pub struct CursoredTree<T> { /* private fields */ }Expand description
Tree with a movable cursor that tracks the current position.
Wraps a Tree<T> and maintains a cursor as a Vec<usize> path from
the root. Navigation methods return bool to indicate whether the move
succeeded; the cursor is never left in an invalid state.
CursoredTree<T> implements Deref<Target = Tree<T>>, so all read-only
Tree methods are available directly. For mutation through the inner
tree, use tree_mut.
Implementations§
Source§impl<T> CursoredTree<T>
impl<T> CursoredTree<T>
Sourcepub fn new(root_value: T) -> Self
pub fn new(root_value: T) -> Self
Create a new tree with a single root node. The cursor starts at root.
Sourcepub fn cursor_path(&self) -> &[usize]
pub fn cursor_path(&self) -> &[usize]
Index path from the root to the current node.
Sourcepub fn cursor(&self) -> &[usize]
pub fn cursor(&self) -> &[usize]
Alias for cursor_path.
Sourcepub fn current_mut(&mut self) -> &mut Node<T>
pub fn current_mut(&mut self) -> &mut Node<T>
Mutable reference to the node at the cursor.
Sourcepub fn current_id(&self) -> u64
pub fn current_id(&self) -> u64
Id of the node at the cursor.
Sourcepub fn has_parent(&self) -> bool
pub fn has_parent(&self) -> bool
true when the cursor is not at the root.
Sourcepub fn has_children(&self) -> bool
pub fn has_children(&self) -> bool
true when the current node has at least one child.
Sourcepub fn push(&mut self, value: T) -> u64
pub fn push(&mut self, value: T) -> u64
Add a child to the current node and move the cursor to it.
Always succeeds because the parent is the current node.
Sourcepub fn go_child(&mut self, index: usize) -> bool
pub fn go_child(&mut self, index: usize) -> bool
Move the cursor to the child at index. Returns false if out of
bounds.
Sourcepub fn go_child_last(&mut self) -> bool
pub fn go_child_last(&mut self) -> bool
Move the cursor to the last (newest) child. Returns false when
there are no children.
Sourcepub fn go_sibling_next(&mut self) -> bool
pub fn go_sibling_next(&mut self) -> bool
Move the cursor to the next sibling. Returns false when there is
no next sibling or the cursor is at the root.
Sourcepub fn go_sibling_prev(&mut self) -> bool
pub fn go_sibling_prev(&mut self) -> bool
Move the cursor to the previous sibling. Returns false when there
is no previous sibling or the cursor is at the root.
Sourcepub fn go_to(&mut self, id: u64) -> bool
pub fn go_to(&mut self, id: u64) -> bool
Jump the cursor to the node with the given id. Returns false when
the id does not exist.
Sourcepub fn go_root(&mut self) -> bool
pub fn go_root(&mut self) -> bool
Move the cursor back to the root. Returns false if already there.
Sourcepub fn push_child(
&mut self,
parent_id: u64,
value: T,
) -> Result<u64, ParentNotFound>
pub fn push_child( &mut self, parent_id: u64, value: T, ) -> Result<u64, ParentNotFound>
Append a child to the node identified by parent_id.
The cursor is not moved. See push for the
push-and-move variant.
Sourcepub fn remove_subtree(&mut self, id: u64) -> Option<Node<T>>
pub fn remove_subtree(&mut self, id: u64) -> Option<Node<T>>
Remove a subtree. If the cursor was inside the removed subtree, it is repaired to the nearest surviving ancestor.
Sourcepub fn prune(&mut self, policy: PrunePolicy)
pub fn prune(&mut self, policy: PrunePolicy)
Prune the tree. If the cursor was on a pruned node, it is repaired to the nearest surviving ancestor.
Methods from Deref<Target = Tree<T>>§
Trait Implementations§
Source§impl<T: Clone> Clone for CursoredTree<T>
impl<T: Clone> Clone for CursoredTree<T>
Source§fn clone(&self) -> CursoredTree<T>
fn clone(&self) -> CursoredTree<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more