1- use std:: collections:: VecDeque ;
1+ use crate :: utils:: graphs:: graph:: Graph ;
2+ use std:: collections:: { HashMap , VecDeque } ;
3+ use std:: hash:: Hash ;
24
3- pub struct AllPaths < ' a , T > {
4- adjacency : & ' a dyn Fn ( T ) -> Vec < T > ,
5+ pub trait IsEnd < T > {
6+ fn is_end ( & self , value : & T ) -> bool ;
57}
68
9+ impl < T : PartialEq > IsEnd < T > for T {
10+ fn is_end ( & self , value : & T ) -> bool {
11+ self == value
12+ }
13+ }
14+
15+ impl < T : PartialEq > IsEnd < T > for Vec < T > {
16+ fn is_end ( & self , value : & T ) -> bool {
17+ self . contains ( value)
18+ }
19+ }
20+
21+ pub struct AllPaths < ' a , T : ' a > {
22+ adjacency : Box < dyn Fn ( T ) -> Vec < T > + ' a > ,
23+ }
24+
25+ impl < ' a , T > From < & ' a Graph < T > > for AllPaths < ' a , T >
26+ where
27+ T : Eq + Hash + Copy + PartialEq + Clone ,
28+ {
29+ fn from ( graph : & ' a Graph < T > ) -> Self {
30+ Self :: new ( move |p : T | graph. neighbours ( & p) )
31+ }
32+ }
33+
34+ impl < ' a , T > From < & ' a HashMap < T , Vec < T > > > for AllPaths < ' a , T >
35+ where
36+ T : Eq + Hash + Copy + PartialEq + Clone ,
37+ {
38+ fn from ( value : & ' a HashMap < T , Vec < T > > ) -> Self {
39+ Self :: new ( move |p : T | value. get ( & p) . unwrap ( ) . to_vec ( ) )
40+ }
41+ }
742impl < ' a , T > AllPaths < ' a , T >
843where
944 T : PartialEq + Clone ,
1045{
11- pub fn new ( adjacency : & ' a dyn Fn ( T ) -> Vec < T > ) -> Self {
12- Self { adjacency }
46+ pub fn new ( adjacency : impl Fn ( T ) -> Vec < T > + ' a ) -> Self {
47+ Self {
48+ adjacency : Box :: new ( adjacency) ,
49+ }
1350 }
1451
15- pub fn generate ( & self , start : T , end : T ) -> Vec < VecDeque < T > > {
52+ pub fn paths < E > ( & self , start : T , end : E ) -> Vec < VecDeque < T > >
53+ where
54+ E : IsEnd < T > ,
55+ {
1656 let mut paths: Vec < VecDeque < T > > = Vec :: new ( ) ;
1757
18- self . visit ( start, end, Vec :: new ( ) , VecDeque :: new ( ) , & mut paths) ;
58+ self . visit ( start, & end, Vec :: new ( ) , VecDeque :: new ( ) , & mut paths) ;
1959
2060 paths
2161 }
2262
23- fn visit (
63+ fn visit < E > (
2464 & self ,
2565 from : T ,
26- end : T ,
66+ end : & E ,
2767 mut visited : Vec < T > ,
2868 mut path : VecDeque < T > ,
2969 paths : & mut Vec < VecDeque < T > > ,
30- ) {
70+ ) where
71+ E : IsEnd < T > ,
72+ {
3173 visited. push ( from. clone ( ) ) ;
3274 path. push_back ( from. clone ( ) ) ;
3375
34- if from == end {
76+ if end. is_end ( & from ) {
3577 paths. push ( path. clone ( ) ) ;
3678
3779 return ;
3880 }
3981
4082 for p in ( self . adjacency ) ( from) {
4183 if !visited. contains ( & p) {
42- self . visit ( p, end. clone ( ) , visited. clone ( ) , path. clone ( ) , paths) ;
84+ self . visit ( p, end, visited. clone ( ) , path. clone ( ) , paths) ;
4385 }
4486 }
4587 }
4890#[ cfg( test) ]
4991mod tests {
5092 use crate :: utils:: graphs:: all_paths:: AllPaths ;
93+ use crate :: utils:: graphs:: graph:: Graph ;
5194 use crate :: utils:: point:: Point ;
5295 use std:: collections:: { HashMap , VecDeque } ;
5396
@@ -66,13 +109,63 @@ mod tests {
66109 ] ) ;
67110 let adjacency = |p : Point | graph. get ( & p) . unwrap ( ) . to_vec ( ) ;
68111
69- let all_paths: AllPaths < Point > = AllPaths :: new ( & adjacency) ;
112+ let all_paths: AllPaths < Point > = AllPaths :: new ( adjacency) ;
70113
71- let paths = all_paths. generate ( p2, p3) ;
114+ let paths = all_paths. paths ( p2, p3) ;
72115
73116 assert_eq ! ( 3 , paths. len( ) ) ;
74117 assert ! ( paths. contains( & VecDeque :: from( vec![ p2, p1, p3] ) ) ) ;
75118 assert ! ( paths. contains( & VecDeque :: from( vec![ p2, p0, p3] ) ) ) ;
76119 assert ! ( paths. contains( & VecDeque :: from( vec![ p2, p0, p1, p3] ) ) ) ;
77120 }
121+
122+ #[ test]
123+ fn paths_vec_end ( ) {
124+ let p0 = Point :: new ( 0 , 0 ) ;
125+ let p1 = Point :: new ( 1 , 1 ) ;
126+ let p2 = Point :: new ( 2 , 2 ) ;
127+ let p3 = Point :: new ( 3 , 3 ) ;
128+ let p4 = Point :: new ( 4 , 4 ) ;
129+
130+ let graph: HashMap < Point , Vec < Point > > = HashMap :: from ( [
131+ ( p0, vec ! [ p1, p2] ) ,
132+ ( p1, vec ! [ p3] ) ,
133+ ( p2, vec ! [ p4] ) ,
134+ ( p3, vec ! [ ] ) ,
135+ ( p4, vec ! [ ] ) ,
136+ ] ) ;
137+ let adjacency = |p : Point | graph. get ( & p) . unwrap ( ) . to_vec ( ) ;
138+
139+ let all_paths: AllPaths < Point > = AllPaths :: new ( adjacency) ;
140+
141+ let paths = all_paths. paths ( p0, vec ! [ p3, p4] ) ;
142+
143+ assert_eq ! ( 2 , paths. len( ) ) ;
144+ assert ! ( paths. contains( & VecDeque :: from( vec![ p0, p1, p3] ) ) ) ;
145+ assert ! ( paths. contains( & VecDeque :: from( vec![ p0, p2, p4] ) ) ) ;
146+ }
147+
148+ #[ test]
149+ fn paths_from_graph ( ) {
150+ let p0 = Point :: new ( 0 , 0 ) ;
151+ let p1 = Point :: new ( 1 , 1 ) ;
152+ let p2 = Point :: new ( 2 , 2 ) ;
153+ let p3 = Point :: new ( 3 , 3 ) ;
154+
155+ let mut graph = Graph :: undirected ( ) ;
156+ graph. add_edge ( p0, p1) ;
157+ graph. add_edge ( p0, p2) ;
158+ graph. add_edge ( p0, p3) ;
159+ graph. add_edge ( p1, p3) ;
160+ graph. add_edge ( p2, p1) ;
161+
162+ let all_paths = AllPaths :: from ( & graph) ;
163+ let paths = all_paths. paths ( p2, p3) ;
164+
165+ assert_eq ! ( 4 , paths. len( ) ) ;
166+ assert ! ( paths. contains( & VecDeque :: from( vec![ p2, p1, p3] ) ) ) ;
167+ assert ! ( paths. contains( & VecDeque :: from( vec![ p2, p0, p3] ) ) ) ;
168+ assert ! ( paths. contains( & VecDeque :: from( vec![ p2, p0, p1, p3] ) ) ) ;
169+ assert ! ( paths. contains( & VecDeque :: from( vec![ p2, p1, p0, p3] ) ) ) ;
170+ }
78171}
0 commit comments