@@ -3363,6 +3363,106 @@ where
33633363 M : MemoryPolicy ,
33643364 P : PinnedStorage ,
33653365{
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+ #[ allow( clippy:: missing_panics_doc) ]
3412+ let root_ptr = ends. expect ( "Tree is not-empty, and hence, has a root" ) ;
3413+ NodeMut :: new ( self . col , root_ptr)
3414+ }
3415+
3416+ /// Consumes this mutable node and returns the mutable `root` node of the tree that this node belongs to.
3417+ ///
3418+ /// Note that if this node is the root of its tree, this method will return itself.
3419+ ///
3420+ /// # Examples
3421+ ///
3422+ /// ```
3423+ /// use orx_tree::*;
3424+ ///
3425+ /// // 1
3426+ /// // ╱ ╲
3427+ /// // ╱ ╲
3428+ /// // 2 3
3429+ /// // ╱ ╲ ╱ ╲
3430+ /// // 4 5 6 7
3431+ /// // | | ╱ ╲
3432+ /// // 8 9 10 11
3433+ ///
3434+ /// let mut tree = DynTree::new(1);
3435+ ///
3436+ /// let mut root = tree.root_mut();
3437+ /// let [id2, id3] = root.push_children([2, 3]);
3438+ /// let [id4, _] = tree.node_mut(id2).push_children([4, 5]);
3439+ /// tree.node_mut(id4).push_child(8);
3440+ /// let [id6, id7] = tree.node_mut(id3).push_children([6, 7]);
3441+ /// tree.node_mut(id6).push_child(9);
3442+ /// let [id10, _] = tree.node_mut(id7).push_children([10, 11]);
3443+ ///
3444+ /// // reach back to root from any node
3445+ ///
3446+ /// let n1 = tree.root_mut();
3447+ /// let mut root = n1.into_root_mut();
3448+ /// *root.data_mut() += 100;
3449+ /// assert_eq!(tree.root().data(), &101);
3450+ ///
3451+ /// let n4 = tree.node_mut(id4);
3452+ /// *n4.into_root_mut().data_mut() += 100;
3453+ /// assert_eq!(tree.root().data(), &201);
3454+ ///
3455+ /// let n10 = tree.node_mut(id10);
3456+ /// *n10.into_root_mut().data_mut() += 100;
3457+ /// assert_eq!(tree.root().data(), &301);
3458+ /// ```
3459+ pub fn into_root_mut ( self ) -> NodeMut < ' a , V , M , P > {
3460+ let ends = self . col . ends_mut ( ) . get ( ) ;
3461+ #[ allow( clippy:: missing_panics_doc) ]
3462+ let root_ptr = ends. expect ( "Tree is not-empty, and hence, has a root" ) ;
3463+ NodeMut :: new ( self . col , root_ptr)
3464+ }
3465+
33663466 /// Returns the mutable node of this node's parent,
33673467 /// returns None if this is the root node.
33683468 ///
0 commit comments