Node

Struct Node 

Source
pub struct Node<U, T> { /* private fields */ }
Expand description

Describes a node inside the Tree U: is the type for the node indentifier (must implement PartialEq) T: is the type for the node value

Implementations§

Source§

impl<U, T> Node<U, T>
where U: PartialEq,

Source

pub fn new(id: U, value: T) -> Node<U, T>

Instantiates a new Node. In order to use query methods the ID should be unique for each node in the tree

Source

pub fn with_children(self, children: Vec<Node<U, T>>) -> Node<U, T>

Sets Node children

Source

pub fn with_child(self, child: Node<U, T>) -> Node<U, T>

Create a new child in this Node

Source

pub fn id(&self) -> &U

Get reference to id

Source

pub fn value(&self) -> &T

Get reference to Node value

Source

pub fn set_value(&mut self, value: T)

Set the value of the Node

Source

pub fn children(&self) -> &[Node<U, T>]

Returns a reference to the Node’s children

Source

pub fn iter(&self) -> Iter<'_, Node<U, T>>

Returns an iterator over Node’s children

Source

pub fn iter_mut(&mut self) -> IterMut<'_, Node<U, T>>

Returns a mutable iterator over Node’s children

Source

pub fn add_child(&mut self, child: Node<U, T>)

Add a child to the Node

If the child already exists, it will be replaced

Source

pub fn remove_child(&mut self, id: &U)

Remove child from Node

Source

pub fn clear(&mut self)

Clear Node’s children

Source

pub fn truncate(&mut self, depth: usize)

Truncate tree at depth. If depth is 0, Node’s children will be cleared

Source

pub fn sort<F>(&mut self, compare: F)
where F: FnMut(&Node<U, T>, &Node<U, T>) -> Ordering,

Sort node children by predicate

Source

pub fn is_leaf(&self) -> bool

Returns whether this Node is a leaf (which means it has no children)

Source

pub fn query(&self, id: &U) -> Option<&Node<U, T>>

Search for id inside Node and return a reference to it, if exists

Source

pub fn query_mut(&mut self, id: &U) -> Option<&mut Node<U, T>>

Search for id inside Node and return a mutable reference to it, if exists

Source

pub fn find<P>(&self, predicate: &P) -> Vec<&Node<U, T>>
where P: Fn(&Node<U, T>) -> bool,

Find a node, in this branch, by predicate.

Source

pub fn count(&self) -> usize

Count items in tree (including self)

Source

pub fn depth(&self) -> usize

Calculate the maximum depth of the tree

Source

pub fn parent(&self, id: &U) -> Option<&Node<U, T>>

Get parent Node of id

Source

pub fn parent_mut(&mut self, id: &U) -> Option<&mut Node<U, T>>

Get mutable parent Node of id

Source

pub fn siblings(&self, id: &U) -> Option<Vec<&U>>

Get siblings for provided Node

Source

pub fn node_by_route(&self, route: &[usize]) -> Option<&Node<U, T>>

Given a vector of indexes, returns the node associated to the route

Source

pub fn node_by_route_mut(&mut self, route: &[usize]) -> Option<&mut Node<U, T>>

Given a vector of indexes, returns the mutable Node associated to the route

Source

pub fn route_by_node(&self, id: &U) -> Option<Vec<usize>>

Calculate the route of a Node by its id

Trait Implementations§

Source§

impl<U, T> Clone for Node<U, T>
where U: Clone, T: Clone,

Source§

fn clone(&self) -> Node<U, 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<U, T> Debug for Node<U, T>
where U: Debug, T: Debug,

Source§

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

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

impl<U, T> PartialEq for Node<U, T>
where U: PartialEq, T: PartialEq,

Source§

fn eq(&self, other: &Node<U, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<U, T> Eq for Node<U, T>
where U: Eq, T: Eq,

Source§

impl<U, T> StructuralPartialEq for Node<U, T>

Auto Trait Implementations§

§

impl<U, T> Freeze for Node<U, T>
where U: Freeze, T: Freeze,

§

impl<U, T> RefUnwindSafe for Node<U, T>

§

impl<U, T> Send for Node<U, T>
where U: Send, T: Send,

§

impl<U, T> Sync for Node<U, T>
where U: Sync, T: Sync,

§

impl<U, T> Unpin for Node<U, T>
where U: Unpin, T: Unpin,

§

impl<U, T> UnwindSafe for Node<U, T>
where U: UnwindSafe, 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<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.