pub struct DsuRoot<T> { /* private fields */ }
Expand description
An owning smart pointer that always points to the root of a DSU tree.
One can logically consider that DsuRoot
smart pointers refer to a standalone DSU tree. When
merging two DSU trees by calling the merge_into
function, the two DSU trees are given
through two DsuRoot
smart pointers that logically refer to the two trees.
Implementations§
Source§impl<T> DsuRoot<T>
impl<T> DsuRoot<T>
Sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Create a new DSU tree node and attach a DsuRoot
smart pointer to the new node.
The new DSU tree node contains the given value.
Sourcepub fn same(lhs: &mut Self, rhs: &mut Self) -> bool
pub fn same(lhs: &mut Self, rhs: &mut Self) -> bool
Determine whether the two DsuRoot
smart pointers refer to the same tree.
use dsu_tree::DsuRoot;
let mut dsu_1 = DsuRoot::new(10);
let mut dsu_2 = DsuRoot::new(20);
assert!(!DsuRoot::same(&mut dsu_1, &mut dsu_2));
dsu_1.merge_into(&mut dsu_2);
assert!(DsuRoot::same(&mut dsu_1, &mut dsu_2));
Sourcepub fn value(&mut self) -> &T
pub fn value(&mut self) -> &T
Get the value contained in the DSU tree node pointed to by this DsuRoot
smart pointer.
This function requires &mut self
since the DsuRoot
smart pointer may move around over
the DSU tree so that it eventually points to the root node.
Sourcepub fn merge_into(&mut self, another: &mut Self)
pub fn merge_into(&mut self, another: &mut Self)
Merge two DSU trees into one.
The first DSU tree is given through self
. The second DSU tree is given through another
.
If initially, the two trees are the same tree, then this function does nothing. Otherwise,
the parent node of the root node of the tree specified through self
is set to the root
node of the tree specified through another
.