pub enum UfCell<U, M, P>{
Root((U::Info, M::Data)),
Child((usize, P::T)),
}Variants§
Implementations§
Source§impl<U, M, P> UfCell<U, M, P>
impl<U, M, P> UfCell<U, M, P>
Sourcefn root_mut(&mut self) -> Option<&mut (U::Info, M::Data)>
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 }Sourcefn child_mut(&mut self) -> Option<&mut (usize, P::T)>
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§
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>where
<U as UnionStrategy>::Info: RefUnwindSafe,
<M as UfMergeSpec>::Data: RefUnwindSafe,
<P as Magma>::T: RefUnwindSafe,
impl<U, M, P> Send for UfCell<U, M, P>
impl<U, M, P> Sync for UfCell<U, M, P>
impl<U, M, P> Unpin for UfCell<U, M, P>
impl<U, M, P> UnwindSafe for UfCell<U, M, P>where
<U as UnionStrategy>::Info: UnwindSafe,
<M as UfMergeSpec>::Data: UnwindSafe,
<P as Magma>::T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more