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>
impl<'a, N> NodeMut<'a, N>
Sourcepub fn tree(&self) -> &EytzingerTree<N>
pub fn tree(&self) -> &EytzingerTree<N>
Gets the Eytzinger tree this node is for.
Sourcepub fn value(&self) -> &N
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?
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 }
Sourcepub fn value_mut(&mut self) -> &mut N
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);
Sourcepub fn into_value_mut(self) -> &'a mut N
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.
Sourcepub fn parent(&self) -> Option<Node<'_, N>>
pub fn parent(&self) -> Option<Node<'_, N>>
Gets the parent of this node or None
is there was none.
Sourcepub fn to_parent(self) -> Result<Self, Self>
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.
Sourcepub fn child(&self, index: usize) -> Option<Node<'_, N>>
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.
Sourcepub fn child_mut(&mut self, index: usize) -> Option<NodeMut<'_, N>>
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.
Sourcepub fn to_child(self, index: usize) -> Result<Self, Self>
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?
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 }
Sourcepub fn set_child_value(&mut self, index: usize, new_value: N) -> NodeMut<'_, N>
pub fn set_child_value(&mut self, index: usize, new_value: N) -> NodeMut<'_, N>
Examples found in repository?
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 }
Sourcepub fn remove_child_value(
&mut self,
index: usize,
) -> (Option<N>, VacantEntry<'_, N>)
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.
Sourcepub fn child_entry(&mut self, index: usize) -> Entry<'_, N>
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.
Sourcepub fn to_child_entry(self, index: usize) -> Entry<'a, N>
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.
Sourcepub fn remove(self) -> (N, VacantEntry<'a, N>)
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);
Sourcepub fn as_node(&self) -> Node<'_, N>
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.
Sourcepub fn child_iter(&self) -> NodeChildIter<'_, N> ⓘ
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]);
Sourcepub fn depth_first_iter(&self, order: DepthFirstOrder) -> DepthFirstIter<'_, N> ⓘ
pub fn depth_first_iter(&self, order: DepthFirstOrder) -> DepthFirstIter<'_, N> ⓘ
Gets a depth-first iterator over this and all child nodes.
Sourcepub fn breadth_first_iter(&self) -> BreadthFirstIter<'_, N> ⓘ
pub fn breadth_first_iter(&self) -> BreadthFirstIter<'_, N> ⓘ
Gets a breadth-first iterator over this and all child nodes.