Skip to content

Commit 1d2a891

Browse files
committed
graphs tools : refactor
1 parent 6056d18 commit 1d2a891

File tree

12 files changed

+380
-247
lines changed

12 files changed

+380
-247
lines changed

module/move/graphs_tools/src/search.rs

Lines changed: 6 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ mod private
3838
Visit : FnMut( &'a Graph::Node ),
3939
Method : super::Method,
4040
{
41+
/// Search traversing each node in an order specified by method.
4142
pub fn search( self, graph : &'a Graph )
4243
{
4344
graph.search( self )
@@ -104,91 +105,20 @@ mod private
104105
Self : Sized;
105106
}
106107

107-
/// Depth-first search strategy.
108-
#[ derive( Debug, Default ) ]
109-
pub struct Dfs;
110-
111-
impl Method for Dfs
112-
{
113-
type ExtraOptions = ();
114-
115-
/// Perform depth-first search on a graph.
116-
fn _search< 'a, Graph, Visit >
117-
(
118-
graph : &'a Graph,
119-
mut o : Options< 'a, Self, Graph, Visit >,
120-
)
121-
where
122-
Visit : FnMut( &'a Graph::Node ),
123-
Graph : ForGraphDirected< 'a > + ?Sized,
124-
{
125-
let mut visited = collection_tools::HashSet::new();
126-
let mut stack = collection_tools::Vec::new();
127-
stack.push( o.start_id );
128-
129-
while let Some( node_id ) = stack.pop()
130-
{
131-
let node = graph.node_ref( node_id );
132-
if visited.insert( node_id )
133-
{
134-
( o.visit )( node );
135-
for child_id in graph.node_out_nodes( node_id )
136-
{
137-
stack.push( child_id );
138-
}
139-
}
140-
}
141-
}
142-
}
143-
144-
/// Breadth-first search strategy.
145-
#[ derive( Debug, Default ) ]
146-
pub struct Bfs;
147-
148-
impl Method for Bfs
149-
{
150-
type ExtraOptions = ();
151-
152-
/// Perform breadth-first search on a graph.
153-
fn _search< 'a, Graph, Visit >
154-
(
155-
graph : &'a Graph,
156-
mut o : Options< 'a, Self, Graph, Visit >,
157-
)
158-
where
159-
Visit : FnMut( &'a Graph::Node ),
160-
Graph : ForGraphDirected< 'a > + ?Sized,
161-
{
162-
let mut visited = collection_tools::HashSet::new();
163-
let mut queue = collection_tools::VecDeque::new();
164-
queue.push_back( o.start_id );
165-
166-
while let Some( node_id ) = queue.pop_front()
167-
{
168-
let node = graph.node_ref( node_id );
169-
if visited.insert( node_id )
170-
{
171-
( o.visit )( node );
172-
for child_id in graph.node_out_nodes( node_id )
173-
{
174-
queue.push_back( child_id );
175-
}
176-
}
177-
}
178-
}
179-
}
180-
181108
}
182109

183110
crate::mod_interface!
184111
{
112+
layer
113+
{
114+
dfs,
115+
bfs,
116+
};
185117
own use
186118
{
187119
options,
188120
Method,
189121
Options,
190122
ForGraphDirected,
191-
Dfs,
192-
Bfs,
193123
};
194124
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! Breadth first search method.
2+
3+
mod private
4+
{
5+
use crate::*;
6+
use search::{ Method, ForGraphDirected, Options };
7+
8+
/// Breadth-first search strategy.
9+
#[ derive( Debug, Default ) ]
10+
pub struct Bfs;
11+
12+
impl Method for Bfs
13+
{
14+
type ExtraOptions = ();
15+
16+
/// Perform breadth-first search on a graph.
17+
fn _search< 'a, Graph, Visit >
18+
(
19+
graph : &'a Graph,
20+
mut o : Options< 'a, Self, Graph, Visit >,
21+
)
22+
where
23+
Visit : FnMut( &'a Graph::Node ),
24+
Graph : ForGraphDirected< 'a > + ?Sized,
25+
{
26+
let mut visited = collection_tools::HashSet::new();
27+
let mut queue = collection_tools::VecDeque::new();
28+
queue.push_back( o.start_id );
29+
30+
while let Some( node_id ) = queue.pop_front()
31+
{
32+
let node = graph.node_ref( node_id );
33+
if visited.insert( node_id )
34+
{
35+
( o.visit )( node );
36+
for child_id in graph.node_out_nodes( node_id )
37+
{
38+
queue.push_back( child_id );
39+
}
40+
}
41+
}
42+
}
43+
}
44+
45+
}
46+
47+
crate::mod_interface!
48+
{
49+
orphan use
50+
{
51+
Bfs,
52+
};
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! Depth first search method.
2+
3+
mod private
4+
{
5+
use crate::*;
6+
use search::{ Method, ForGraphDirected, Options };
7+
8+
/// Depth-first search method.
9+
#[ derive( Debug, Default ) ]
10+
pub struct Dfs;
11+
12+
impl Method for Dfs
13+
{
14+
type ExtraOptions = ();
15+
16+
/// Perform depth-first search on a graph.
17+
fn _search< 'a, Graph, Visit >
18+
(
19+
graph : &'a Graph,
20+
mut o : Options< 'a, Self, Graph, Visit >,
21+
)
22+
where
23+
Visit : FnMut( &'a Graph::Node ),
24+
Graph : ForGraphDirected< 'a > + ?Sized,
25+
{
26+
let mut visited = collection_tools::HashSet::new();
27+
let mut stack = collection_tools::Vec::new();
28+
stack.push( o.start_id );
29+
30+
while let Some( node_id ) = stack.pop()
31+
{
32+
let node = graph.node_ref( node_id );
33+
if visited.insert( node_id )
34+
{
35+
( o.visit )( node );
36+
for child_id in graph.node_out_nodes( node_id )
37+
{
38+
stack.push( child_id );
39+
}
40+
}
41+
}
42+
}
43+
44+
}
45+
46+
}
47+
48+
crate::mod_interface!
49+
{
50+
orphan use
51+
{
52+
Dfs,
53+
};
54+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use super::*;
2+
3+
pub mod map_of_nodes;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use super::*;
2+
3+
use derive_tools::From;
4+
use the_module::abs;
5+
use iter_tools::{ _IterTrait, IterTrait, BoxedIter };
6+
7+
#[ derive( Debug ) ]
8+
pub struct Node< 'a >
9+
{
10+
pub id : NodeId,
11+
pub children : Vec< &'a Node< 'a > >,
12+
}
13+
14+
impl< 'a > the_module::abs::Node for Node< 'a > {}
15+
16+
impl< 'a > Node< 'a >
17+
{
18+
pub fn new< IntoId : Into< NodeId > >( id : IntoId ) -> Node< 'a >
19+
{
20+
Node
21+
{
22+
id : id.into(),
23+
children : Vec::new(),
24+
}
25+
}
26+
27+
pub fn add_child( &mut self, child : &'a Node< 'a > ) -> &mut Self
28+
{
29+
self.children.push( child );
30+
self
31+
}
32+
}
33+
34+
#[ derive( Default ) ]
35+
pub struct Graph< 'a >
36+
{
37+
nodes : HashMap< NodeId, &'a Node< 'a > >,
38+
}
39+
40+
impl< 'a > Graph< 'a >
41+
{
42+
43+
pub fn add_node( &mut self, node : &'a Node< 'a > )
44+
{
45+
self.nodes.insert( node.id, node );
46+
}
47+
48+
}
49+
50+
impl< 'a > abs::GraphDirected< 'a > for Graph< 'a >
51+
{
52+
53+
type NodeId = NodeId;
54+
type Node = Node< 'a >;
55+
56+
fn node_ref( &self, node_id : NodeId ) -> &'a Node< 'a >
57+
{
58+
self.nodes.get( &node_id ).expect( "If id exist then node shoudl also exist" )
59+
}
60+
61+
fn node_id( &self, node : &'a Node< 'a > ) -> NodeId
62+
{
63+
node.id
64+
}
65+
66+
fn node_out_nodes( &self, node_id : NodeId ) -> BoxedIter< 'a, Self::NodeId >
67+
{
68+
if let Some( node ) = self.nodes.get( &node_id )
69+
{
70+
Box::new( node.children.iter().map( | child | child.id ) )
71+
}
72+
else
73+
{
74+
Box::new( std::iter::empty() )
75+
}
76+
}
77+
}
78+
79+
#[ derive( Debug, Copy, Clone, Hash, PartialEq, Eq, From ) ]
80+
pub struct NodeId( usize );
81+
82+
impl the_module::abs::NodeId for NodeId {}

module/move/graphs_tools/tests/inc/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
use super::*;
44

5+
pub mod graph;
6+
57
mod basic_test;
6-
mod nodes;
7-
mod search;
8+
mod nodes_test;
9+
mod search_test;

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

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)