Skip to content

Commit 6056d18

Browse files
committed
graphs_tools: better options
1 parent 2135c4c commit 6056d18

File tree

2 files changed

+78
-17
lines changed

2 files changed

+78
-17
lines changed

module/move/graphs_tools/src/search.rs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ mod private
22
{
33
use crate::*;
44

5-
// xxx : write setters and default
5+
/// Former of Options for searching.
6+
pub fn options< 'a, Method, Graph, Visit >() -> OptionsFormer< 'a, Method, Graph, Visit >
7+
where
8+
Graph : crate::abs::GraphDirected< 'a > + ?Sized,
9+
Visit : FnMut( &'a Graph::Node ),
10+
Method : super::Method,
11+
{
12+
Options::former()
13+
}
14+
615
/// Options for configuring a graph search.
716
#[ derive( Debug, Default, Former ) ]
817
pub struct Options< 'a, Method, Graph, Visit >
@@ -23,6 +32,37 @@ mod private
2332
pub _phantom : std::marker::PhantomData< ( &'a (), ) >,
2433
}
2534

35+
impl< 'a, Method, Graph, Visit > Options< 'a, Method, Graph, Visit >
36+
where
37+
Graph : ForGraphDirected< 'a > + ?Sized,
38+
Visit : FnMut( &'a Graph::Node ),
39+
Method : super::Method,
40+
{
41+
pub fn search( self, graph : &'a Graph )
42+
{
43+
graph.search( self )
44+
}
45+
}
46+
47+
// xxx : adjust Former to eliminate need in this
48+
impl< 'a, Method, Graph, Visit > OptionsFormer< 'a, Method, Graph, Visit >
49+
where
50+
Graph : ForGraphDirected< 'a > + ?Sized,
51+
Visit : FnMut( &'a Graph::Node ),
52+
Method : super::Method,
53+
{
54+
pub fn visit_set( mut self, visit : Visit ) -> Self
55+
{
56+
self.storage.visit = Some( visit );
57+
self
58+
}
59+
pub fn method_set( mut self, method : Method ) -> Self
60+
{
61+
self.storage.method = Some( method );
62+
self
63+
}
64+
}
65+
2666
/// Trait for performing searches on directed graphs.
2767
pub trait ForGraphDirected< 'a > : crate::abs::GraphDirected< 'a >
2868
{
@@ -50,7 +90,7 @@ mod private
5090
pub trait Method : Default
5191
{
5292
/// Additional options for the search method.
53-
type ExtraOptions;
93+
type ExtraOptions : Default;
5494

5595
/// Execute the search on a graph.
5696
fn _search< 'a, Graph, Visit >
@@ -60,7 +100,7 @@ mod private
60100
)
61101
where
62102
Visit : FnMut( &'a Graph::Node ),
63-
Graph : crate::abs::GraphDirected< 'a > + ?Sized,
103+
Graph : ForGraphDirected< 'a > + ?Sized,
64104
Self : Sized;
65105
}
66106

@@ -80,7 +120,7 @@ mod private
80120
)
81121
where
82122
Visit : FnMut( &'a Graph::Node ),
83-
Graph : crate::abs::GraphDirected< 'a > + ?Sized,
123+
Graph : ForGraphDirected< 'a > + ?Sized,
84124
{
85125
let mut visited = collection_tools::HashSet::new();
86126
let mut stack = collection_tools::Vec::new();
@@ -117,7 +157,7 @@ mod private
117157
)
118158
where
119159
Visit : FnMut( &'a Graph::Node ),
120-
Graph : crate::abs::GraphDirected< 'a > + ?Sized,
160+
Graph : ForGraphDirected< 'a > + ?Sized,
121161
{
122162
let mut visited = collection_tools::HashSet::new();
123163
let mut queue = collection_tools::VecDeque::new();
@@ -144,6 +184,7 @@ crate::mod_interface!
144184
{
145185
own use
146186
{
187+
options,
147188
Method,
148189
Options,
149190
ForGraphDirected,

module/move/graphs_tools/tests/inc/search/dfs.rs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,39 @@ fn test_dfs()
125125
visited_nodes.push( node.id );
126126
};
127127

128-
// Create search options
129-
let search_options = search::Options
130-
{
131-
start_id : NodeId( 1 ),
132-
visit,
133-
method : search::Dfs,
134-
// ..Default::default()
135-
_extra : (),
136-
_phantom : Default::default(),
137-
};
128+
// // Create search options
129+
// let search_options = search::Options
130+
// {
131+
// start_id : NodeId( 1 ),
132+
// visit,
133+
// method : search::Dfs,
134+
// // ..Default::default()
135+
// _extra : (),
136+
// _phantom : Default::default(),
137+
// };
138+
139+
// // Create search options
140+
// let search_options = search::options()
141+
// .start_id( 1 )
142+
// .visit_set( visit )
143+
// .method_set( search::Dfs )
144+
// .form()
145+
// ;
146+
//
147+
// // Perform DFS
148+
// graph.search( search_options );
149+
//
150+
// // Assert the order of visited nodes
151+
// assert_eq!( visited_nodes, vec![ NodeId( 1 ), NodeId( 4 ), NodeId( 3 ), NodeId( 2 ) ] );
138152

139-
// Perform DFS
140-
graph.search( search_options );
153+
// Create search options
154+
let search_options = search::options()
155+
.start_id( 1 )
156+
.visit_set( visit )
157+
.method_set( search::Dfs )
158+
.form()
159+
.search( &graph )
160+
;
141161

142162
// Assert the order of visited nodes
143163
assert_eq!( visited_nodes, vec![ NodeId( 1 ), NodeId( 4 ), NodeId( 3 ), NodeId( 2 ) ] );

0 commit comments

Comments
 (0)