Struct DsuRoot

Source
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>

Source

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.

Examples found in repository?
examples/kruskal.rs (line 66)
65    fn add_root(&mut self, value: usize) {
66        self.roots.push(RefCell::new(DsuRoot::new(value)));
67    }
Source

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));
Source

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.

Examples found in repository?
examples/kruskal.rs (line 70)
69    fn find(&self, id: usize) -> usize {
70        *self.roots[id].borrow_mut().value()
71    }
Source

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.

Examples found in repository?
examples/kruskal.rs (line 80)
73    fn merge(&mut self, first: usize, second: usize) {
74        if first == second {
75            return;
76        }
77
78        let mut first_root = self.roots[first].borrow_mut();
79        let mut second_root = self.roots[second].borrow_mut();
80        first_root.merge_into(&mut second_root);
81    }

Trait Implementations§

Source§

impl<T: Clone> Clone for DsuRoot<T>

Source§

fn clone(&self) -> DsuRoot<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for DsuRoot<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for DsuRoot<T>

§

impl<T> !RefUnwindSafe for DsuRoot<T>

§

impl<T> !Send for DsuRoot<T>

§

impl<T> !Sync for DsuRoot<T>

§

impl<T> Unpin for DsuRoot<T>

§

impl<T> !UnwindSafe for DsuRoot<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.