Skip to content

Commit c992482

Browse files
committed
feat: use macros to create constructors and impls
1 parent 5cc5cc0 commit c992482

File tree

9 files changed

+292
-209
lines changed

9 files changed

+292
-209
lines changed

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub mod quickfind;
7070
pub mod quickunion;
7171
pub mod rng;
7272

73+
use core::borrow::{Borrow, BorrowMut};
7374
use core::marker::PhantomData;
7475
use core::ops::AddAssign;
7576

@@ -184,7 +185,7 @@ where
184185
self.heuristic.as_mut(),
185186
a,
186187
b,
187-
self.rng.as_mut(),
188+
&mut self.rng,
188189
)
189190
}
190191
/// Gets the representative slice
@@ -218,7 +219,7 @@ pub trait AlgorithmContainer {
218219
type RepresentativeContainer<'a, V: VertexType + 'a, const N: usize>: AsRef<[V]> + AsMut<[V]>;
219220

220221
/// Any kind of RNG
221-
type RngKind<'a>: RngCore + AsMut<Self::RngKind<'a>>;
222+
type RngKind<'a>: RngCore;
222223
}
223224

224225
/// Union operation

src/quickunion/algorithm_container.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ macro_rules! impl_random {
4747
() => {
4848
impl<R, const P: bool> AlgorithmContainer for QuickUnion<ByRandom<R>, P>
4949
where
50-
R: RngCore + AsMut<R>,
50+
R: RngCore,
5151
{
5252
type HeuristicKind<'a> = ByRandom<R>;
5353
type HeuristicContainer<'a, const N: usize> = [usize; 0];
@@ -57,7 +57,7 @@ macro_rules! impl_random {
5757

5858
impl<R, const P: bool> AlgorithmContainer for QuickUnion<ByRandom<R, Borrowed<Fat>>, P>
5959
where
60-
R: RngCore + AsMut<R>,
60+
R: RngCore,
6161
{
6262
type HeuristicKind<'a> = ByRandom<R, Borrowed<Fat>>;
6363
type HeuristicContainer<'a, const N: usize> = &'a mut [usize];
@@ -67,7 +67,7 @@ macro_rules! impl_random {
6767

6868
impl<R, const P: bool> AlgorithmContainer for QuickUnion<ByRandom<R, Borrowed<Thin>>, P>
6969
where
70-
R: RngCore + AsMut<R>,
70+
R: RngCore,
7171
{
7272
type HeuristicKind<'a> = ByRandom<R, Borrowed<Thin>>;
7373
type HeuristicContainer<'a, const N: usize> = &'a mut [usize];

src/quickunion/borrowed_impls.rs

Lines changed: 109 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,128 @@
11
//! Borrowed and Thin borrowed implementations for QuickUnion
22
33
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,
66
};
77
use rand_core::RngCore;
88

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+
}
3729

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+
};
4546
}
4647

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+
}
5764
}
58-
}
59-
}
6065

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+
}
8279
}
83-
}
80+
};
8481
}
8582

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+
}
97102
}
98-
}
99-
}
100103

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+
}
112122
}
113-
}
123+
};
114124
}
115125

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

Comments
 (0)