pulau_rs/quickunion/
borrowed_impls.rs

1//! Borrowed and Thin borrowed implementations for QuickUnion
2
3use crate::{
4    quickunion::heuristics::ByRandom, rng::PhantomRng, Borrowed, ByRank, BySize, Fat, QuickUnion,
5    Thin, UnionFind, Unweighted, VertexType,
6};
7use rand_core::RngCore;
8
9/// Implements `UnionFind::new` for Borrowed<Fat> and Borrowed<Thin> variants.
10macro_rules! impl_unionfind_borrowed {
11    ($($heur:ident),* $(,)?) => {
12        $(
13            impl<'a, T, const N: usize, const P: bool>
14                UnionFind<'a, QuickUnion<$heur<Borrowed<Fat>>, P>, T, N>
15            where
16                T: VertexType,
17            {
18                pub fn new(representative: &'a mut [T], heuristic: &'a mut [usize]) -> Self {
19                    debug_assert!(representative.len() >= N);
20                    debug_assert!(heuristic.len() >= N);
21                    Self {
22                        representative,
23                        heuristic,
24                        algorithm: Default::default(),
25                        rng: PhantomRng,
26                    }
27                }
28            }
29
30            impl<'a, T, const N: usize, const P: bool>
31                UnionFind<'a, QuickUnion<$heur<Borrowed<Thin>>, P>, T, N>
32            where
33                T: VertexType,
34            {
35                pub fn new(representative: &'a mut [T; N], heuristic: &'a mut [usize; N]) -> Self {
36                    Self {
37                        representative,
38                        heuristic,
39                        algorithm: Default::default(),
40                        rng: PhantomRng,
41                    }
42                }
43            }
44        )*
45    };
46}
47
48/// Implements `UnionFind::new` for Unweighted borrowed variants.
49macro_rules! impl_unionfind_unweighted_borrowed {
50    () => {
51        impl<'a, T, const N: usize, const P: bool>
52            UnionFind<'a, QuickUnion<Unweighted<Borrowed<Fat>>, P>, T, N>
53        where
54            T: VertexType,
55        {
56            pub fn new(representative: &'a mut [T]) -> Self {
57                Self {
58                    representative,
59                    heuristic: [0; 0],
60                    algorithm: Default::default(),
61                    rng: PhantomRng,
62                }
63            }
64        }
65
66        impl<'a, T, const N: usize, const P: bool>
67            UnionFind<'a, QuickUnion<Unweighted<Borrowed<Thin>>, P>, T, N>
68        where
69            T: VertexType,
70        {
71            pub fn new(representative: &'a mut [T; N]) -> Self {
72                Self {
73                    representative,
74                    heuristic: [0; 0],
75                    algorithm: Default::default(),
76                    rng: PhantomRng,
77                }
78            }
79        }
80    };
81}
82
83/// Implements `UnionFind::new` for ByRandom<R, Borrowed<Fat>>.
84macro_rules! impl_unionfind_random_borrowed {
85    () => {
86        impl<'a, R, T, const N: usize, const P: bool>
87            UnionFind<'a, QuickUnion<ByRandom<R, Borrowed<Fat>>, P>, T, N>
88        where
89            T: VertexType,
90            R: RngCore + AsMut<R> + AsRef<R>,
91        {
92            pub fn new(representative: &'a mut [T], heuristic: &'a mut [usize], rng: R) -> Self {
93                debug_assert!(representative.len() >= N);
94                debug_assert!(heuristic.len() >= N);
95                Self {
96                    representative,
97                    heuristic,
98                    algorithm: Default::default(),
99                    rng,
100                }
101            }
102        }
103
104        impl<'a, R, T, const N: usize, const P: bool>
105            UnionFind<'a, QuickUnion<ByRandom<R, Borrowed<Thin>>, P>, T, N>
106        where
107            T: VertexType,
108            R: RngCore + AsMut<R> + AsRef<R>,
109        {
110            pub fn new(
111                representative: &'a mut [T; N],
112                heuristic: &'a mut [usize; N],
113                rng: R,
114            ) -> Self {
115                Self {
116                    representative,
117                    heuristic,
118                    algorithm: Default::default(),
119                    rng,
120                }
121            }
122        }
123    };
124}
125
126impl_unionfind_borrowed!(ByRank, BySize);
127impl_unionfind_unweighted_borrowed!();
128impl_unionfind_random_borrowed!();