1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use crate::{AlgorithmContainer, Connected, Find, Union, UnionFind, VertexType};
#[derive(Debug, Default)]
pub struct QuickFind<const IS_SLICE: bool = false>;
impl AlgorithmContainer for QuickFind<false> {
type HeuristicContainer<'a, const N: usize> = [usize; 0];
type RepresentativeContainer<'a, R: VertexType + 'a, const N: usize> = [R; N];
}
impl AlgorithmContainer for QuickFind<true> {
type HeuristicContainer<'a, const N: usize> = [usize; 0];
type RepresentativeContainer<'a, R: VertexType + 'a, const N: usize> = &'a mut [R];
}
macro_rules! generate_default_ctor_quickfind {
($($num_type:ident), *) => {
$(
impl<const N: usize> Default for UnionFind<'_, QuickFind, $num_type, N>
{
fn default() -> Self {
let mut representative = [0; N];
for i in 0..(N as $num_type) {
representative[i as usize] = i;
}
Self {
representative,
heuristic: [0; 0],
algorithm: Default::default(),
}
}
}
)*
};
}
impl<'a, T, const N: usize> UnionFind<'a, QuickFind<true>, T, N>
where
T: VertexType,
{
pub fn new(representative: &'a mut [T]) -> Self {
Self {
representative,
heuristic: [0; 0],
algorithm: Default::default(),
}
}
}
impl<T, const IS_SLICE: bool> Connected<T> for QuickFind<IS_SLICE>
where
T: VertexType,
Self: Find<T>,
{
fn connected(representative: &mut [T], a: T::IdentifierType, b: T::IdentifierType) -> bool {
Self::find(representative, a) == Self::find(representative, b)
}
}
impl<T, const IS_SLICE: bool> Union<T> for QuickFind<IS_SLICE>
where
T: VertexType,
Self: Find<T>,
{
fn union_sets(
representative: &mut [T],
_heuristic: &mut [usize],
a: T::IdentifierType,
b: T::IdentifierType,
) {
let root_a = Self::find(representative, a);
let root_b = Self::find(representative, b);
for item in representative {
if *item == root_a {
*item = root_b;
}
}
}
}
impl<T, const IS_SLICE: bool> Find<T> for QuickFind<IS_SLICE>
where
T: VertexType,
{
fn find(representative: &mut [T], a: T::IdentifierType) -> T {
assert!(T::usize(a) < representative.len());
representative[T::usize(a)]
}
}
generate_default_ctor_quickfind!(u8, u16, u32, u64, usize);
#[cfg(test)]
mod tests {
use crate::{tests::CityVertex, QuickFind, UnionFind};
use core::{mem, panic};
#[test]
fn test_qf() {
let mut uf = UnionFind::<QuickFind, u32, 10>::default();
uf.union_sets(4, 3);
uf.union_sets(3, 8);
uf.union_sets(6, 5);
uf.union_sets(9, 4);
assert!(uf.connected(3, 9));
}
#[test]
fn test_qf_slice() {
let mut representative = (0..10).collect::<heapless::Vec<_, 10>>();
let mut uf = UnionFind::<QuickFind<true>, u32, 10>::new(representative.as_mut());
uf.union_sets(4, 3);
uf.union_sets(3, 8);
uf.union_sets(6, 5);
uf.union_sets(9, 4);
assert!(uf.connected(3, 9));
}
impl<'a, const N: usize> TryFrom<[CityVertex<'a>; N]>
for UnionFind<'_, QuickFind, CityVertex<'a>, N>
{
type Error = &'static str;
fn try_from(cities: [CityVertex<'a>; N]) -> Result<Self, Self::Error> {
for id in 0..N {
if cities[id].id as usize != id {
return Err("Invalid cities id!");
}
}
Ok(Self {
representative: cities,
heuristic: [0; 0],
algorithm: Default::default(),
})
}
}
#[test]
fn test_custom_type() {
let cities = [
CityVertex::new(0, "Zurich", 320),
CityVertex::new(1, "Munich", 210),
CityVertex::new(2, "Paris", 180),
CityVertex::new(3, "London", 190),
CityVertex::new(4, "Oslo", 250),
CityVertex::new(5, "Stockholm", 280),
CityVertex::new(6, "Helsinki", 280),
];
let mut uf = UnionFind::<QuickFind, CityVertex<'static>, 7>::try_from(cities).unwrap();
uf.union_sets(4, 3);
uf.union_sets(3, 2);
uf.union_sets(6, 5);
assert!(uf.connected(4, 2));
assert!(uf.connected(6, 5));
}
#[test]
fn test_sz() {
assert_eq!(
mem::size_of::<[u32; 10]>(),
mem::size_of::<UnionFind::<'_, QuickFind, u32, 10>>()
);
assert_eq!(
mem::size_of::<&'_ [u32]>(),
mem::size_of::<UnionFind::<'_, QuickFind<true>, u32, 10>>()
);
assert_eq!(
mem::size_of::<[CityVertex<'_>; 10]>(),
mem::size_of::<UnionFind::<'_, QuickFind, CityVertex<'_>, 10>>()
);
}
#[test]
fn test_getter() {
let mut uf = UnionFind::<QuickFind, u32, 10>::default();
uf.union_sets(4, 3);
uf.union_sets(3, 8);
uf.union_sets(6, 5);
uf.union_sets(9, 4);
for _ in uf.heuristic() {
panic!("Should not even loop!");
}
}
}