Skip to content

Commit 4034881

Browse files
authored
Merge pull request #185 from orxfun/feat/#87-NodeRef--root
Feat/#87 node ref root
2 parents 26b818c + 11efe03 commit 4034881

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

src/node_mut.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
///

src/node_ref.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,51 @@ where
8888
self.node().prev().get().is_none()
8989
}
9090

91+
/// Returns the `root` node of the tree that this node belongs to.
92+
///
93+
/// Note that if this node is the root of its tree, this method will return itself.
94+
///
95+
/// # Examples
96+
///
97+
/// ```
98+
/// use orx_tree::*;
99+
///
100+
/// // 1
101+
/// // ╱ ╲
102+
/// // ╱ ╲
103+
/// // 2 3
104+
/// // ╱ ╲ ╱ ╲
105+
/// // 4 5 6 7
106+
/// // | | ╱ ╲
107+
/// // 8 9 10 11
108+
///
109+
/// let mut tree = DynTree::new(1);
110+
///
111+
/// let mut root = tree.root_mut();
112+
/// let [id2, id3] = root.push_children([2, 3]);
113+
/// let [id4, _] = tree.node_mut(id2).push_children([4, 5]);
114+
/// tree.node_mut(id4).push_child(8);
115+
/// let [id6, id7] = tree.node_mut(id3).push_children([6, 7]);
116+
/// tree.node_mut(id6).push_child(9);
117+
/// let [id10, _] = tree.node_mut(id7).push_children([10, 11]);
118+
///
119+
/// // reach back to root from any node
120+
///
121+
/// let n1 = tree.root();
122+
/// assert_eq!(n1.root(), tree.root());
123+
///
124+
/// let n4 = tree.node(id4);
125+
/// assert_eq!(n4.root(), tree.root());
126+
///
127+
/// let n10 = tree.node(id10);
128+
/// assert_eq!(n10.root(), tree.root());
129+
/// ```
130+
fn root(&self) -> Node<'a, V, M, P> {
131+
let ends = self.col().ends().get();
132+
let root_ptr = ends.expect("Tree is not-empty, and hence, has a root");
133+
Node::new(self.col(), root_ptr)
134+
}
135+
91136
/// Returns true if this is a leaf node; equivalently, if [`num_children`] is zero.
92137
///
93138
/// [`num_children`]: NodeRef::num_children

0 commit comments

Comments
 (0)