Struct NodeMut

Source
pub struct NodeMut<'a, N>
where N: 'a,
{ /* private fields */ }
Expand description

Represents a borrowed node in the Eytzinger tree. This node may be used mutate this node’s value and child nodes.

Implementations§

Source§

impl<'a, N> NodeMut<'a, N>

Source

pub fn tree(&self) -> &EytzingerTree<N>

Gets the Eytzinger tree this node is for.

Source

pub fn value(&self) -> &N

Gets the value stored at this node.

§Examples
use lz_eytzinger_tree::{EytzingerTree, Node};

let mut tree = {
    let mut tree = EytzingerTree::<u32>::new(8);
    tree.set_root_value(5);
    tree
};

let root = tree.root_mut().unwrap();
assert_eq!(root.value(), &5);
Examples found in repository?
examples/binary_tree.rs (line 37)
33    pub fn insert(&mut self, value: T) {
34        if let Some(root) = self.tree.root_mut() {
35            let mut current = root;
36            loop {
37                if &value == current.value() {
38                    return;
39                }
40
41                if &value < current.value() {
42                    match current.to_child(LEFT) {
43                        Ok(left) => {
44                            current = left;
45                            continue;
46                        }
47                        Err(mut current) => {
48                            current.set_child_value(LEFT, value);
49                            return;
50                        }
51                    }
52                }
53
54                match current.to_child(RIGHT) {
55                    Ok(right) => {
56                        current = right;
57                        continue;
58                    }
59                    Err(mut current) => {
60                        current.set_child_value(RIGHT, value);
61                        return;
62                    }
63                }
64            }
65        }
66        self.tree.set_root_value(value);
67    }
Source

pub fn value_mut(&mut self) -> &mut N

Gets the mutable value stored at this node.

§Examples
use lz_eytzinger_tree::{EytzingerTree, Node};

let mut tree = {
    let mut tree = EytzingerTree::<u32>::new(8);
    tree.set_root_value(5);
    tree
};

let mut root = tree.root_mut().unwrap();
*root.value_mut() = 8;
assert_eq!(root.value(), &8);
Source

pub fn into_value_mut(self) -> &'a mut N

Gets the mutable value stored at this node.

This differs from value_mut in that it takes ownership of the current node and the value is lifetime bound to the tree and not to the current node.

Source

pub fn parent(&self) -> Option<Node<'_, N>>

Gets the parent of this node or None is there was none.

Source

pub fn to_parent(self) -> Result<Self, Self>

Gets the mutable paret of this node or None if there wasn’t one.

This differs from parent_mut in that it takes ownership of the current node and is lifetime bound to the tree and not to the current node.

Source

pub fn child(&self, index: usize) -> Option<Node<'_, N>>

Gets the child of this node at the specified index or None if there wasn’t one.

Source

pub fn child_mut(&mut self, index: usize) -> Option<NodeMut<'_, N>>

Gets the mutable child of this node at the specified index or None if there wasn’t one.

Source

pub fn to_child(self, index: usize) -> Result<Self, Self>

Gets the mutable child of this node at the specified index or None if there wasn’t one.

This differs from child_mut in that it takes ownership of the current node and is lifetime bound to the tree and not to the current node.

Examples found in repository?
examples/binary_tree.rs (line 42)
33    pub fn insert(&mut self, value: T) {
34        if let Some(root) = self.tree.root_mut() {
35            let mut current = root;
36            loop {
37                if &value == current.value() {
38                    return;
39                }
40
41                if &value < current.value() {
42                    match current.to_child(LEFT) {
43                        Ok(left) => {
44                            current = left;
45                            continue;
46                        }
47                        Err(mut current) => {
48                            current.set_child_value(LEFT, value);
49                            return;
50                        }
51                    }
52                }
53
54                match current.to_child(RIGHT) {
55                    Ok(right) => {
56                        current = right;
57                        continue;
58                    }
59                    Err(mut current) => {
60                        current.set_child_value(RIGHT, value);
61                        return;
62                    }
63                }
64            }
65        }
66        self.tree.set_root_value(value);
67    }
Source

pub fn set_child_value(&mut self, index: usize, new_value: N) -> NodeMut<'_, N>

Sets the value of the child at the specified index.

§Returns

The new mutable child.

Examples found in repository?
examples/binary_tree.rs (line 48)
33    pub fn insert(&mut self, value: T) {
34        if let Some(root) = self.tree.root_mut() {
35            let mut current = root;
36            loop {
37                if &value == current.value() {
38                    return;
39                }
40
41                if &value < current.value() {
42                    match current.to_child(LEFT) {
43                        Ok(left) => {
44                            current = left;
45                            continue;
46                        }
47                        Err(mut current) => {
48                            current.set_child_value(LEFT, value);
49                            return;
50                        }
51                    }
52                }
53
54                match current.to_child(RIGHT) {
55                    Ok(right) => {
56                        current = right;
57                        continue;
58                    }
59                    Err(mut current) => {
60                        current.set_child_value(RIGHT, value);
61                        return;
62                    }
63                }
64            }
65        }
66        self.tree.set_root_value(value);
67    }
Source

pub fn remove_child_value( &mut self, index: usize, ) -> (Option<N>, VacantEntry<'_, N>)

Removes the child value at the specified child index. This will also remove all children of the specified child.

§Returns

The old child value if there was one.

Source

pub fn child_entry(&mut self, index: usize) -> Entry<'_, N>

Gets the child entry of this node at the specified index. This node is not consumed in the process so the child entry is lifetime bound to this node.

Source

pub fn to_child_entry(self, index: usize) -> Entry<'a, N>

Gets the child entry of this node at the specified index.

This differs from child_entry in that it takes ownership of the current node and the entry is lifetime bound to the tree and not to the current node.

Source

pub fn remove(self) -> (N, VacantEntry<'a, N>)

Removes this node from the tree.

§Examples
use lz_eytzinger_tree::{EytzingerTree, Node};

let mut tree = {
    let mut tree = EytzingerTree::<u32>::new(8);
    tree.set_root_value(5);
    tree
};
{
    let mut root = tree.root_mut().unwrap();
    root.remove();
}
assert_eq!(tree.root(), None);
Source

pub fn as_node(&self) -> Node<'_, N>

Gets a view of this mutable node as an immutable node. The resulting node is lifetime bound to this node so the immutable node may not outlive this mutable node.

Source

pub fn child_iter(&self) -> NodeChildIter<'_, N>

Gets an iterator over the immediate children of this node. This only includes children for which there is a node.

§Examples
use lz_eytzinger_tree::{EytzingerTree, Node};

let mut tree = {
    let mut tree = EytzingerTree::<u32>::new(8);
    {
        let mut root = tree.set_root_value(5);
        root.set_child_value(0, 1);
        root.set_child_value(2, 3);

    }
    tree
};

let root = tree.root_mut().unwrap();
let child_values: Vec<_> = root.child_iter().map(|n| n.value()).collect();
assert_eq!(child_values, vec![&1, &3]);
Source

pub fn depth_first_iter(&self, order: DepthFirstOrder) -> DepthFirstIter<'_, N>

Gets a depth-first iterator over this and all child nodes.

Source

pub fn breadth_first_iter(&self) -> BreadthFirstIter<'_, N>

Gets a breadth-first iterator over this and all child nodes.

Source

pub fn split_off(self) -> EytzingerTree<N>

Trait Implementations§

Source§

impl<'a, N> Debug for NodeMut<'a, N>
where N: 'a + Debug,

Source§

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

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

impl<'a, N> Deref for NodeMut<'a, N>

Source§

type Target = N

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<'a, N> DerefMut for NodeMut<'a, N>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'a, N> From<&'a NodeMut<'a, N>> for Node<'a, N>

Source§

fn from(value: &'a NodeMut<'a, N>) -> Node<'a, N>

Converts to this type from the input type.
Source§

impl<'a, N> From<NodeMut<'a, N>> for Node<'a, N>

Source§

fn from(value: NodeMut<'a, N>) -> Node<'a, N>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a, N> Freeze for NodeMut<'a, N>

§

impl<'a, N> RefUnwindSafe for NodeMut<'a, N>
where N: RefUnwindSafe,

§

impl<'a, N> Send for NodeMut<'a, N>
where N: Send,

§

impl<'a, N> Sync for NodeMut<'a, N>
where N: Sync,

§

impl<'a, N> Unpin for NodeMut<'a, N>

§

impl<'a, N> !UnwindSafe for NodeMut<'a, N>

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> 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, 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.