pulau_rs/quickunion/
constructors.rs

1//! Constructors and macros for QuickUnion
2
3use crate::{
4    quickunion::heuristics::ByRandom, rng::PhantomRng, ByRank, BySize, Owned, QuickUnion,
5    UnionFind, Unweighted,
6};
7use rand_core::RngCore;
8
9/// Helper macro to generate the representative array
10macro_rules! generate_representative {
11    ($n:expr, $num_type:ident) => {{
12        let mut representative = [0; $n];
13        for i in 0..($n as $num_type) {
14            representative[i as usize] = i;
15        }
16        representative
17    }};
18}
19
20/// Macro to generate Default impls for Owned heuristics (ByRank, BySize)
21macro_rules! generate_default_owned {
22    ($($num_type:ident),* $(,)?) => {
23        $(
24            impl<const N: usize> Default for UnionFind<'_, QuickUnion<ByRank>, $num_type, N> {
25                fn default() -> Self {
26                    Self {
27                        representative: generate_representative!(N, $num_type),
28                        heuristic: [0; N],
29                        algorithm: Default::default(),
30                        rng: PhantomRng,
31                    }
32                }
33            }
34
35            impl<const N: usize> Default for UnionFind<'_, QuickUnion<BySize>, $num_type, N> {
36                fn default() -> Self {
37                    Self {
38                        representative: generate_representative!(N, $num_type),
39                        heuristic: [1; N],
40                        algorithm: Default::default(),
41                        rng: PhantomRng,
42                    }
43                }
44            }
45        )*
46    };
47}
48
49/// Macro to generate Default impls for Unweighted heuristics
50macro_rules! generate_default_unweighted {
51    ($($num_type:ident),* $(,)?) => {
52        $(
53            impl<const N: usize, const P: bool> Default for UnionFind<'_, QuickUnion<Unweighted, P>, $num_type, N> {
54                fn default() -> Self {
55                    Self {
56                        representative: generate_representative!(N, $num_type),
57                        heuristic: [0; 0],
58                        algorithm: Default::default(),
59                        rng: PhantomRng,
60                    }
61                }
62            }
63        )*
64    };
65}
66
67/// Macro to generate Default impls for ByRandom<R> heuristics
68macro_rules! generate_default_random {
69    ($($num_type:ident),* $(,)?) => {
70        $(
71            impl<R, const N: usize, const P: bool> UnionFind<'_, QuickUnion<ByRandom<R, Owned>, P>, $num_type, N>
72            where
73                R: RngCore,
74            {
75                pub fn new(rng: R) -> Self {
76                    Self {
77                        representative: generate_representative!(N, $num_type),
78                        heuristic: [0; 0],
79                        algorithm: Default::default(),
80                        rng
81                    }
82                }
83            }
84        )*
85    };
86}
87
88// ✅ Generate all Default impls for primitive integer types
89generate_default_owned!(u8, u16, u32, u64, usize);
90generate_default_unweighted!(u8, u16, u32, u64, usize);
91generate_default_random!(u8, u16, u32, u64, usize);