Skip to main content

CursoredTree

Struct CursoredTree 

Source
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>

Source

pub fn new(root_value: T) -> Self

Create a new tree with a single root node. The cursor starts at root.

Source

pub fn cursor_path(&self) -> &[usize]

Index path from the root to the current node.

Source

pub fn cursor(&self) -> &[usize]

Alias for cursor_path.

Source

pub fn current(&self) -> &Node<T>

Reference to the node at the cursor.

Source

pub fn current_mut(&mut self) -> &mut Node<T>

Mutable reference to the node at the cursor.

Source

pub fn current_id(&self) -> u64

Id of the node at the cursor.

Source

pub fn has_parent(&self) -> bool

true when the cursor is not at the root.

Source

pub fn has_children(&self) -> bool

true when the current node has at least one child.

Source

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.

Source

pub fn go_parent(&mut self) -> bool

Move the cursor to the parent. Returns false at the root.

Source

pub fn go_child(&mut self, index: usize) -> bool

Move the cursor to the child at index. Returns false if out of bounds.

Source

pub fn go_child_last(&mut self) -> bool

Move the cursor to the last (newest) child. Returns false when there are no children.

Source

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.

Source

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.

Source

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.

Source

pub fn go_root(&mut self) -> bool

Move the cursor back to the root. Returns false if already there.

Source

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.

Source

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.

Source

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.

Source

pub fn tree(&self) -> &Tree<T>

Immutable reference to the inner Tree.

Source

pub fn tree_mut(&mut self) -> &mut Tree<T>

Mutable reference to the inner Tree.

Use with care: structural changes may invalidate the cursor. Call methods on CursoredTree instead when possible.

Methods from Deref<Target = Tree<T>>§

Source

pub fn root(&self) -> &Node<T>

Reference to the root node.

Source

pub fn next_id(&self) -> u64

The id that will be assigned to the next inserted node.

Source

pub fn find(&self, id: u64) -> Option<&Node<T>>

Look up a node by id. O(1) amortised.

Source

pub fn find_path_to(&self, id: u64) -> Option<Vec<usize>>

Return the index path from the root to the given id. O(1) amortised.

Source

pub fn dfs(&self) -> DfsIter<'_, T>

Depth-first iterator yielding (depth, &Node<T>) pairs.

Source

pub fn flatten(&self) -> Vec<(usize, &Node<T>)>

Collect dfs into a Vec.

Trait Implementations§

Source§

impl<T: Clone> Clone for CursoredTree<T>

Source§

fn clone(&self) -> CursoredTree<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for CursoredTree<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for CursoredTree<T>

Source§

type Target = Tree<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<T> Freeze for CursoredTree<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for CursoredTree<T>
where T: RefUnwindSafe,

§

impl<T> Send for CursoredTree<T>
where T: Send,

§

impl<T> Sync for CursoredTree<T>
where T: Sync,

§

impl<T> Unpin for CursoredTree<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for CursoredTree<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for CursoredTree<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.