pulau_rs/quickunion/
algorithm_container.rs

1//! AlgorithmContainer trait implementations for QuickUnion
2
3use rand_core::RngCore;
4
5use crate::{
6    quickunion::heuristics::ByRandom, rng::PhantomRng, AlgorithmContainer, Borrowed, ByRank,
7    BySize, Fat, Owned, QuickUnion, Thin, Unweighted, VertexType,
8};
9
10/// Implements AlgorithmContainer for QuickUnion with `Owned` heuristics.
11macro_rules! impl_owned {
12    ($($heur:ident),* $(,)?) => {
13        $(
14            impl AlgorithmContainer for QuickUnion<$heur<Owned>> {
15                type HeuristicKind<'a> = $heur;
16                type HeuristicContainer<'a, const N: usize> = [usize; N];
17                type RepresentativeContainer<'a, V: VertexType + 'a, const N: usize> = [V; N];
18                type RngKind<'a> = PhantomRng;
19            }
20        )*
21    };
22}
23
24/// Implements AlgorithmContainer for QuickUnion with Borrowed<Fat|Thin> heuristics.
25macro_rules! impl_borrowed {
26    ($($heur:ident),* $(,)?) => {
27        $(
28            impl<const P: bool> AlgorithmContainer for QuickUnion<$heur<Borrowed<Fat>>, P> {
29                type HeuristicKind<'a> = $heur<Borrowed<Fat>>;
30                type HeuristicContainer<'a, const N: usize> = &'a mut [usize];
31                type RepresentativeContainer<'a, V: VertexType + 'a, const N: usize> = &'a mut [V];
32                type RngKind<'a> = PhantomRng;
33            }
34
35            impl<const P: bool> AlgorithmContainer for QuickUnion<$heur<Borrowed<Thin>>, P> {
36                type HeuristicKind<'a> = $heur<Borrowed<Thin>>;
37                type HeuristicContainer<'a, const N: usize> = &'a mut [usize; N];
38                type RepresentativeContainer<'a, V: VertexType + 'a, const N: usize> = &'a mut [V; N];
39                type RngKind<'a> = PhantomRng;
40            }
41        )*
42    };
43}
44
45/// Implements AlgorithmContainer for QuickUnion with ByRandom<R> and borrowed variants.
46macro_rules! impl_random {
47    () => {
48        impl<R, const P: bool> AlgorithmContainer for QuickUnion<ByRandom<R>, P>
49        where
50            R: RngCore,
51        {
52            type HeuristicKind<'a> = ByRandom<R>;
53            type HeuristicContainer<'a, const N: usize> = [usize; 0];
54            type RepresentativeContainer<'a, V: VertexType + 'a, const N: usize> = [V; N];
55            type RngKind<'a> = R;
56        }
57
58        impl<R, const P: bool> AlgorithmContainer for QuickUnion<ByRandom<R, Borrowed<Fat>>, P>
59        where
60            R: RngCore,
61        {
62            type HeuristicKind<'a> = ByRandom<R, Borrowed<Fat>>;
63            type HeuristicContainer<'a, const N: usize> = &'a mut [usize];
64            type RepresentativeContainer<'a, V: VertexType + 'a, const N: usize> = &'a mut [V];
65            type RngKind<'a> = R;
66        }
67
68        impl<R, const P: bool> AlgorithmContainer for QuickUnion<ByRandom<R, Borrowed<Thin>>, P>
69        where
70            R: RngCore,
71        {
72            type HeuristicKind<'a> = ByRandom<R, Borrowed<Thin>>;
73            type HeuristicContainer<'a, const N: usize> = &'a mut [usize];
74            type RepresentativeContainer<'a, V: VertexType + 'a, const N: usize> = &'a mut [V];
75            type RngKind<'a> = R;
76        }
77    };
78}
79
80impl<const P: bool> AlgorithmContainer for QuickUnion<Unweighted<Owned>, P> {
81    type HeuristicKind<'a> = Unweighted;
82    type HeuristicContainer<'a, const N: usize> = [usize; 0];
83    type RepresentativeContainer<'a, V: VertexType + 'a, const N: usize> = [V; N];
84    type RngKind<'a> = PhantomRng;
85}
86
87impl<const P: bool> AlgorithmContainer for QuickUnion<Unweighted<Borrowed<Fat>>, P> {
88    type HeuristicKind<'a> = Unweighted<Borrowed<Fat>>;
89    type HeuristicContainer<'a, const N: usize> = [usize; 0];
90    type RepresentativeContainer<'a, V: VertexType + 'a, const N: usize> = &'a mut [V];
91    type RngKind<'a> = PhantomRng;
92}
93
94impl<const P: bool> AlgorithmContainer for QuickUnion<Unweighted<Borrowed<Thin>>, P> {
95    type HeuristicKind<'a> = Unweighted<Borrowed<Thin>>;
96    type HeuristicContainer<'a, const N: usize> = [usize; 0];
97    type RepresentativeContainer<'a, V: VertexType + 'a, const N: usize> = &'a mut [V; N];
98    type RngKind<'a> = PhantomRng;
99}
100
101impl_owned!(ByRank, BySize);
102impl_borrowed!(ByRank, BySize);
103impl_random!();