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