Skip to content

Commit 5637824

Browse files
committed
implement NodeMut::into_root_mut
1 parent 2ede802 commit 5637824

File tree

1 file changed

+98
-49
lines changed

1 file changed

+98
-49
lines changed

src/node_mut.rs

Lines changed: 98 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -90,55 +90,6 @@ where
9090
P: PinnedStorage,
9191
MO: NodeMutOrientation,
9292
{
93-
/// Returns the mutable `root` node of the tree that this node belongs to.
94-
///
95-
/// Note that if this node is the root of its tree, this method will return itself.
96-
///
97-
/// # Examples
98-
///
99-
/// ```
100-
/// use orx_tree::*;
101-
///
102-
/// // 1
103-
/// // ╱ ╲
104-
/// // ╱ ╲
105-
/// // 2 3
106-
/// // ╱ ╲ ╱ ╲
107-
/// // 4 5 6 7
108-
/// // | | ╱ ╲
109-
/// // 8 9 10 11
110-
///
111-
/// let mut tree = DynTree::new(1);
112-
///
113-
/// let mut root = tree.root_mut();
114-
/// let [id2, id3] = root.push_children([2, 3]);
115-
/// let [id4, _] = tree.node_mut(id2).push_children([4, 5]);
116-
/// tree.node_mut(id4).push_child(8);
117-
/// let [id6, id7] = tree.node_mut(id3).push_children([6, 7]);
118-
/// tree.node_mut(id6).push_child(9);
119-
/// let [id10, _] = tree.node_mut(id7).push_children([10, 11]);
120-
///
121-
/// // reach back to root from any node
122-
///
123-
/// let mut n1 = tree.root_mut();
124-
/// let mut root = n1.root_mut();
125-
/// *root.data_mut() += 100;
126-
/// assert_eq!(tree.root().data(), &101);
127-
///
128-
/// let mut n4 = tree.node_mut(id4);
129-
/// *n4.root_mut().data_mut() += 100;
130-
/// assert_eq!(tree.root().data(), &201);
131-
///
132-
/// let mut n10 = tree.node_mut(id10);
133-
/// *n10.root_mut().data_mut() += 100;
134-
/// assert_eq!(tree.root().data(), &301);
135-
/// ```
136-
pub fn root_mut(&'a mut self) -> NodeMut<'a, V, M, P, MO> {
137-
let ends = self.col.ends_mut().get();
138-
let root_ptr = ends.expect("Tree is not-empty, and hence, has a root");
139-
NodeMut::new(&mut self.col, root_ptr)
140-
}
141-
14293
/// Returns a mutable reference to data of this node.
14394
///
14495
/// # Examples
@@ -3412,6 +3363,104 @@ where
34123363
M: MemoryPolicy,
34133364
P: PinnedStorage,
34143365
{
3366+
/// Returns the mutable `root` node of the tree that this node belongs to.
3367+
///
3368+
/// Note that if this node is the root of its tree, this method will return itself.
3369+
///
3370+
/// # Examples
3371+
///
3372+
/// ```
3373+
/// use orx_tree::*;
3374+
///
3375+
/// // 1
3376+
/// // ╱ ╲
3377+
/// // ╱ ╲
3378+
/// // 2 3
3379+
/// // ╱ ╲ ╱ ╲
3380+
/// // 4 5 6 7
3381+
/// // | | ╱ ╲
3382+
/// // 8 9 10 11
3383+
///
3384+
/// let mut tree = DynTree::new(1);
3385+
///
3386+
/// let mut root = tree.root_mut();
3387+
/// let [id2, id3] = root.push_children([2, 3]);
3388+
/// let [id4, _] = tree.node_mut(id2).push_children([4, 5]);
3389+
/// tree.node_mut(id4).push_child(8);
3390+
/// let [id6, id7] = tree.node_mut(id3).push_children([6, 7]);
3391+
/// tree.node_mut(id6).push_child(9);
3392+
/// let [id10, _] = tree.node_mut(id7).push_children([10, 11]);
3393+
///
3394+
/// // reach back to root from any node
3395+
///
3396+
/// let mut n1 = tree.root_mut();
3397+
/// let mut root = n1.root_mut();
3398+
/// *root.data_mut() += 100;
3399+
/// assert_eq!(tree.root().data(), &101);
3400+
///
3401+
/// let mut n4 = tree.node_mut(id4);
3402+
/// *n4.root_mut().data_mut() += 100;
3403+
/// assert_eq!(tree.root().data(), &201);
3404+
///
3405+
/// let mut n10 = tree.node_mut(id10);
3406+
/// *n10.root_mut().data_mut() += 100;
3407+
/// assert_eq!(tree.root().data(), &301);
3408+
/// ```
3409+
pub fn root_mut(&'a mut self) -> NodeMut<'a, V, M, P> {
3410+
let ends = self.col.ends_mut().get();
3411+
let root_ptr = ends.expect("Tree is not-empty, and hence, has a root");
3412+
NodeMut::new(&mut self.col, root_ptr)
3413+
}
3414+
3415+
/// Consumes this mutable node and returns the mutable `root` node of the tree that this node belongs to.
3416+
///
3417+
/// Note that if this node is the root of its tree, this method will return itself.
3418+
///
3419+
/// # Examples
3420+
///
3421+
/// ```
3422+
/// use orx_tree::*;
3423+
///
3424+
/// // 1
3425+
/// // ╱ ╲
3426+
/// // ╱ ╲
3427+
/// // 2 3
3428+
/// // ╱ ╲ ╱ ╲
3429+
/// // 4 5 6 7
3430+
/// // | | ╱ ╲
3431+
/// // 8 9 10 11
3432+
///
3433+
/// let mut tree = DynTree::new(1);
3434+
///
3435+
/// let mut root = tree.root_mut();
3436+
/// let [id2, id3] = root.push_children([2, 3]);
3437+
/// let [id4, _] = tree.node_mut(id2).push_children([4, 5]);
3438+
/// tree.node_mut(id4).push_child(8);
3439+
/// let [id6, id7] = tree.node_mut(id3).push_children([6, 7]);
3440+
/// tree.node_mut(id6).push_child(9);
3441+
/// let [id10, _] = tree.node_mut(id7).push_children([10, 11]);
3442+
///
3443+
/// // reach back to root from any node
3444+
///
3445+
/// let n1 = tree.root_mut();
3446+
/// let mut root = n1.into_root_mut();
3447+
/// *root.data_mut() += 100;
3448+
/// assert_eq!(tree.root().data(), &101);
3449+
///
3450+
/// let n4 = tree.node_mut(id4);
3451+
/// *n4.into_root_mut().data_mut() += 100;
3452+
/// assert_eq!(tree.root().data(), &201);
3453+
///
3454+
/// let n10 = tree.node_mut(id10);
3455+
/// *n10.into_root_mut().data_mut() += 100;
3456+
/// assert_eq!(tree.root().data(), &301);
3457+
/// ```
3458+
pub fn into_root_mut(self) -> NodeMut<'a, V, M, P> {
3459+
let ends = self.col.ends_mut().get();
3460+
let root_ptr = ends.expect("Tree is not-empty, and hence, has a root");
3461+
NodeMut::new(self.col, root_ptr)
3462+
}
3463+
34153464
/// Returns the mutable node of this node's parent,
34163465
/// returns None if this is the root node.
34173466
///

0 commit comments

Comments
 (0)