UfCell

Enum UfCell 

Source
pub enum UfCell<U, M, P>
where U: UnionStrategy, M: UfMergeSpec, P: Monoid,
{ Root((U::Info, M::Data)), Child((usize, P::T)), }

Variants§

§

Root((U::Info, M::Data))

§

Child((usize, P::T))

Implementations§

Source§

impl<U, M, P> UfCell<U, M, P>
where U: UnionStrategy, M: UfMergeSpec, P: Monoid,

Source

fn root_mut(&mut self) -> Option<&mut (U::Info, M::Data)>

Examples found in repository?
crates/competitive/src/data_structure/union_find.rs (line 417)
406    pub fn unite_noninv(&mut self, x: usize, y: usize, potential: P::T) -> bool {
407        let (rx, potx) = self.find(x);
408        let ry = self.find_root(y);
409        if rx == ry || y != ry {
410            return false;
411        }
412        H::unite(&mut self.history, rx, ry, &self.cells);
413        {
414            let ptr = self.cells.as_mut_ptr();
415            let (cx, cy) = unsafe { (&mut *ptr.add(rx), &mut *ptr.add(ry)) };
416            self.merger
417                .merge(&mut cx.root_mut().unwrap().1, &mut cy.root_mut().unwrap().1);
418        }
419        *self.root_info_mut(rx).unwrap() =
420            U::unite(&self.root_info(rx).unwrap(), &self.root_info(ry).unwrap());
421        self.cells[ry] = UfCell::Child((rx, P::operate(&potx, &potential)));
422        true
423    }
424}
425
426impl<U, F, M, P, H> UnionFindBase<U, F, M, P, H>
427where
428    U: UnionStrategy,
429    F: FindStrategy,
430    M: UfMergeSpec,
431    P: Group,
432    H: UndoStrategy<UfCell<U, M, P>>,
433{
434    pub fn difference(&mut self, x: usize, y: usize) -> Option<P::T> {
435        let (rx, potx) = self.find(x);
436        let (ry, poty) = self.find(y);
437        if rx == ry {
438            Some(P::operate(&P::inverse(&potx), &poty))
439        } else {
440            None
441        }
442    }
443
444    pub fn unite_with(&mut self, x: usize, y: usize, potential: P::T) -> bool {
445        let (mut rx, potx) = self.find(x);
446        let (mut ry, poty) = self.find(y);
447        if rx == ry {
448            return false;
449        }
450        let mut xinfo = self.root_info(rx).unwrap();
451        let mut yinfo = self.root_info(ry).unwrap();
452        let inverse = !U::check_directoin(&xinfo, &yinfo);
453        let potential = if inverse {
454            P::rinv_operate(&poty, &P::operate(&potx, &potential))
455        } else {
456            P::operate(&potx, &P::rinv_operate(&potential, &poty))
457        };
458        if inverse {
459            swap(&mut rx, &mut ry);
460            swap(&mut xinfo, &mut yinfo);
461        }
462        H::unite(&mut self.history, rx, ry, &self.cells);
463        {
464            let ptr = self.cells.as_mut_ptr();
465            let (cx, cy) = unsafe { (&mut *ptr.add(rx), &mut *ptr.add(ry)) };
466            self.merger
467                .merge(&mut cx.root_mut().unwrap().1, &mut cy.root_mut().unwrap().1);
468        }
469        *self.root_info_mut(rx).unwrap() = U::unite(&xinfo, &yinfo);
470        self.cells[ry] = UfCell::Child((rx, potential));
471        true
472    }
Source

fn child_mut(&mut self) -> Option<&mut (usize, P::T)>

Examples found in repository?
crates/competitive/src/data_structure/union_find.rs (line 376)
371    pub fn find(&mut self, x: usize) -> (usize, P::T) {
372        let (parent_parent, parent_potential) = match &self.cells[x] {
373            UfCell::Child((parent, _)) => self.find(*parent),
374            UfCell::Root(_) => return (x, P::unit()),
375        };
376        let (parent, potential) = self.cells[x].child_mut().unwrap();
377        let potential = if F::CHENGE_ROOT {
378            *parent = parent_parent;
379            *potential = P::operate(&parent_potential, potential);
380            potential.clone()
381        } else {
382            P::operate(&parent_potential, potential)
383        };
384        (parent_parent, potential)
385    }
386
387    pub fn find_root(&mut self, x: usize) -> usize {
388        let (parent, parent_parent) = match &self.cells[x] {
389            UfCell::Child((parent, _)) => (*parent, self.find_root(*parent)),
390            UfCell::Root(_) => return x,
391        };
392        if F::CHENGE_ROOT {
393            let (cx, cp) = {
394                let ptr = self.cells.as_mut_ptr();
395                unsafe { (&mut *ptr.add(x), &*ptr.add(parent)) }
396            };
397            let (parent, potential) = cx.child_mut().unwrap();
398            *parent = parent_parent;
399            if let UfCell::Child((_, ppot)) = &cp {
400                *potential = P::operate(ppot, potential);
401            }
402        }
403        parent_parent
404    }

Trait Implementations§

Source§

impl<U, M, P> Clone for UfCell<U, M, P>
where U: UnionStrategy<Info: Clone>, M: UfMergeSpec<Data: Clone>, P: Monoid,

Source§

fn clone(&self) -> Self

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<U, M, P> Debug for UfCell<U, M, P>
where U: UnionStrategy<Info: Debug>, M: UfMergeSpec<Data: Debug>, P: Monoid<T: Debug>,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<U, M, P> Freeze for UfCell<U, M, P>
where <U as UnionStrategy>::Info: Freeze, <M as UfMergeSpec>::Data: Freeze, <P as Magma>::T: Freeze,

§

impl<U, M, P> RefUnwindSafe for UfCell<U, M, P>

§

impl<U, M, P> Send for UfCell<U, M, P>
where <U as UnionStrategy>::Info: Send, <M as UfMergeSpec>::Data: Send, <P as Magma>::T: Send,

§

impl<U, M, P> Sync for UfCell<U, M, P>
where <U as UnionStrategy>::Info: Sync, <M as UfMergeSpec>::Data: Sync, <P as Magma>::T: Sync,

§

impl<U, M, P> Unpin for UfCell<U, M, P>
where <U as UnionStrategy>::Info: Unpin, <M as UfMergeSpec>::Data: Unpin, <P as Magma>::T: Unpin,

§

impl<U, M, P> UnwindSafe for UfCell<U, M, P>

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.