11//! Quick Find implementations
22
3- use crate :: { AlgorithmContainer , Connected , Find , Union , UnionFind , VertexType } ;
3+ use core:: marker:: PhantomData ;
4+
5+ use crate :: {
6+ quickunion:: Heuristic , rng:: PhantomRng , AlgorithmContainer , Borrowed , Connected , Find , Owned ,
7+ Union , UnionFind , VertexType ,
8+ } ;
49
510/// [`QuickFind`] algorithm
611#[ derive( Debug , Default ) ]
7- pub struct QuickFind < const IS_SLICE : bool = false> ;
12+ pub struct QuickFind < K = Owned > {
13+ _p : PhantomData < K > ,
14+ }
15+
16+ pub struct NoHeuristic ;
17+
18+ impl Heuristic for NoHeuristic {
19+ type RngProvider = PhantomRng ;
20+
21+ fn handle_decision < T > (
22+ _a : T :: IdentifierType ,
23+ _b : T :: IdentifierType ,
24+ _heuristic : & mut [ usize ] ,
25+ _representative : & mut [ T ] ,
26+ _r : & mut Self :: RngProvider ,
27+ ) where
28+ T : VertexType ,
29+ {
30+ unreachable ! ( "Should not be called!" )
31+ }
32+ }
833
9- impl AlgorithmContainer for QuickFind < false > {
34+ impl AlgorithmContainer for QuickFind < Owned > {
35+ type HeuristicKind < ' a > = NoHeuristic ;
1036 type HeuristicContainer < ' a , const N : usize > = [ usize ; 0 ] ;
1137 type RepresentativeContainer < ' a , R : VertexType + ' a , const N : usize > = [ R ; N ] ;
38+ type RngKind < ' a > = PhantomRng ;
1239}
1340
14- impl AlgorithmContainer for QuickFind < true > {
41+ impl AlgorithmContainer for QuickFind < Borrowed > {
42+ type HeuristicKind < ' a > = NoHeuristic ;
1543 type HeuristicContainer < ' a , const N : usize > = [ usize ; 0 ] ;
1644 type RepresentativeContainer < ' a , R : VertexType + ' a , const N : usize > = & ' a mut [ R ] ;
45+ type RngKind < ' a > = PhantomRng ;
1746}
1847
1948macro_rules! generate_default_ctor_quickfind {
@@ -32,14 +61,15 @@ macro_rules! generate_default_ctor_quickfind {
3261 representative,
3362 heuristic: [ 0 ; 0 ] ,
3463 algorithm: Default :: default ( ) ,
64+ rng: PhantomRng
3565 }
3666 }
3767 }
3868 ) *
3969 } ;
4070}
4171
42- impl < ' a , T , const N : usize > UnionFind < ' a , QuickFind < true > , T , N >
72+ impl < ' a , T , const N : usize > UnionFind < ' a , QuickFind < Borrowed > , T , N >
4373where
4474 T : VertexType ,
4575{
@@ -48,11 +78,12 @@ where
4878 representative,
4979 heuristic : [ 0 ; 0 ] ,
5080 algorithm : Default :: default ( ) ,
81+ rng : PhantomRng ,
5182 }
5283 }
5384}
5485
55- impl < T , const IS_SLICE : bool > Connected < T > for QuickFind < IS_SLICE >
86+ impl < T , K > Connected < T > for QuickFind < K >
5687where
5788 T : VertexType ,
5889 Self : Find < T > ,
6293 }
6394}
6495
65- impl < T , const IS_SLICE : bool > Union < T > for QuickFind < IS_SLICE >
96+ impl < T , K > Union < T , NoHeuristic > for QuickFind < K >
6697where
6798 T : VertexType ,
6899 Self : Find < T > ,
72103 _heuristic : & mut [ usize ] ,
73104 a : T :: IdentifierType ,
74105 b : T :: IdentifierType ,
106+ _r : & mut PhantomRng ,
75107 ) {
76108 let root_a = Self :: find ( representative, a) ;
77109 let root_b = Self :: find ( representative, b) ;
83115 }
84116}
85117
86- impl < T , const IS_SLICE : bool > Find < T > for QuickFind < IS_SLICE >
118+ impl < T , K > Find < T > for QuickFind < K >
87119where
88120 T : VertexType ,
89121{
@@ -97,7 +129,7 @@ generate_default_ctor_quickfind!(u8, u16, u32, u64, usize);
97129
98130#[ cfg( test) ]
99131mod tests {
100- use crate :: { tests:: CityVertex , QuickFind , UnionFind } ;
132+ use crate :: { rng :: PhantomRng , tests:: CityVertex , Borrowed , QuickFind , UnionFind } ;
101133 use core:: { mem, panic} ;
102134
103135 #[ test]
@@ -113,7 +145,7 @@ mod tests {
113145 #[ test]
114146 fn test_qf_slice ( ) {
115147 let mut representative = ( 0 ..10 ) . collect :: < heapless:: Vec < _ , 10 > > ( ) ;
116- let mut uf = UnionFind :: < QuickFind < true > , u32 , 10 > :: new ( representative. as_mut ( ) ) ;
148+ let mut uf = UnionFind :: < QuickFind < Borrowed > , u32 , 10 > :: new ( representative. as_mut ( ) ) ;
117149 uf. union_sets ( 4 , 3 ) ;
118150 uf. union_sets ( 3 , 8 ) ;
119151 uf. union_sets ( 6 , 5 ) ;
@@ -137,6 +169,7 @@ mod tests {
137169 representative : cities,
138170 heuristic : [ 0 ; 0 ] ,
139171 algorithm : Default :: default ( ) ,
172+ rng : PhantomRng ,
140173 } )
141174 }
142175 }
@@ -169,7 +202,7 @@ mod tests {
169202 ) ;
170203 assert_eq ! (
171204 mem:: size_of:: <& ' _ [ u32 ] >( ) ,
172- mem:: size_of:: <UnionFind :: <' _, QuickFind <true >, u32 , 10 >>( )
205+ mem:: size_of:: <UnionFind :: <' _, QuickFind <Borrowed >, u32 , 10 >>( )
173206 ) ;
174207 assert_eq ! (
175208 mem:: size_of:: <[ CityVertex <' _>; 10 ] >( ) ,
0 commit comments