pulau_rs/quickunion/heuristics/
by_size.rs

1//! BySize heuristic for QuickUnion
2
3use core::marker::PhantomData;
4
5use crate::{Owned, VertexType, quickunion::Heuristic, rng::PhantomRng};
6
7pub struct BySize<K = Owned>(PhantomData<K>);
8
9impl<K> Heuristic for BySize<K> {
10    type RngProvider = PhantomRng;
11
12    #[inline(always)]
13    fn handle_decision<T>(
14        mut a: T::IdentifierType,
15        mut b: T::IdentifierType,
16        size: &mut [usize],
17        representative: &mut [T],
18        _r: &mut Self::RngProvider,
19    ) where
20        T: VertexType,
21    {
22        if a != b {
23            if size[T::usize(a)] < size[T::usize(b)] {
24                core::mem::swap(&mut a, &mut b);
25            }
26            representative[T::usize(b)] = representative[T::usize(a)];
27            size[T::usize(a)] += size[T::usize(b)];
28        }
29    }
30}