Skip to content

Commit 4ae6ec0

Browse files
committed
graphs tools : refactor
1 parent 701b068 commit 4ae6ec0

File tree

3 files changed

+35
-57
lines changed

3 files changed

+35
-57
lines changed

module/move/graphs_tools/src/abs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ mod private
6060
type Node : Node + 'a;
6161

6262
/// Get a reference on a node by its id.
63-
fn node_ref( &self, node_id : Self::NodeId ) -> &'a Self::Node;
63+
fn node_ref( &'a self, node_id : Self::NodeId ) -> &'a Self::Node;
6464
/// Get id by its node reference.
6565
fn node_id( &self, node_id : &'a Self::Node ) -> Self::NodeId;
6666

6767
/// Iterate over out nodes of
68-
fn node_out_nodes( &self, node_id : Self::NodeId ) -> BoxedIter< 'a, Self::NodeId >;
68+
fn node_out_nodes( &'a self, node_id : Self::NodeId ) -> BoxedIter< 'a, Self::NodeId >;
6969
// fn node_out_nodes( &self, node_id : Self::NodeId ) -> impl _IterTrait< 'a, Self::NodeId >;
7070

7171
}

module/move/graphs_tools/tests/inc/graph/map_of_nodes.rs

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,28 @@ impl Node
3232
}
3333

3434
#[ derive( Default ) ]
35-
pub struct Graph< 'a >
35+
pub struct Graph
3636
{
37-
nodes : HashMap< NodeId, &'a Node >,
37+
nodes : HashMap< NodeId, Node >,
3838
}
3939

40-
impl< 'a > Graph< 'a >
40+
impl Graph
4141
{
4242

43-
pub fn add_node( &mut self, node : &'a Node )
43+
pub fn add_node( &mut self, node : Node )
4444
{
4545
self.nodes.insert( node.id, node );
4646
}
4747

4848
}
4949

50-
impl< 'a > abs::GraphDirected< 'a > for Graph< 'a >
50+
impl< 'a > abs::GraphDirected< 'a > for Graph
5151
{
5252

5353
type NodeId = NodeId;
5454
type Node = Node;
5555

56-
fn node_ref( &self, node_id : NodeId ) -> &'a Node
56+
fn node_ref( &'a self, node_id : NodeId ) -> &'a Node
5757
{
5858
self.nodes.get( &node_id ).expect( "If id exist then node shoudl also exist" )
5959
}
@@ -63,7 +63,7 @@ impl< 'a > abs::GraphDirected< 'a > for Graph< 'a >
6363
node.id
6464
}
6565

66-
fn node_out_nodes( &self, node_id : NodeId ) -> BoxedIter< 'a, Self::NodeId >
66+
fn node_out_nodes( &'a self, node_id : NodeId ) -> BoxedIter< 'a, Self::NodeId >
6767
{
6868
if let Some( node ) = self.nodes.get( &node_id )
6969
{
@@ -83,35 +83,30 @@ impl the_module::abs::NodeId for NodeId {}
8383

8484
// xxx
8585

86-
impl< 'a > Graph< 'a >
86+
use std::cell::RefCell;
87+
impl Graph
8788
{
8889

89-
// pub fn duplet() -> ( Vec< Node >, Self )
90-
// {
91-
//
92-
// // Create nodes
93-
// let mut node0 = Node::new( 0 );
94-
// let node1 = Node::new( 1 );
95-
// let node2 = Node::new( 2 );
96-
//
97-
// // Set up the graph structure
98-
// node0
99-
// .add_child( &node1 )
100-
// .add_child( &node2 )
101-
// ;
102-
//
103-
// let mut nodes_darray = vec![ node0, node1, node2 ];
104-
// let mut result = ( nodes_darray, Self::default() );
105-
//
106-
// let mut graph = Self::default();
107-
// graph.add_node( &result.0[ 0 ] );
108-
// graph.add_node( &result.0[ 1 ] );
109-
// graph.add_node( &result.0[ 2 ] );
110-
// core::mem::swap( &mut result.1, &mut graph );
111-
//
112-
// // return ( Default::default(), Default::default() );
113-
// return result;
114-
// // return ( nodes_darray, graph );
115-
// }
90+
pub fn duplet() -> Self
91+
{
92+
93+
// Create nodes
94+
let mut node0 = Node::new( 0 );
95+
let node1 = Node::new( 1 );
96+
let node2 = Node::new( 2 );
97+
98+
// Set up the graph structure
99+
node0
100+
.add_child( &node1 )
101+
.add_child( &node2 )
102+
;
103+
104+
let mut graph = Self::default();
105+
graph.add_node( node0 );
106+
graph.add_node( node1 );
107+
graph.add_node( node2 );
108+
109+
graph
110+
}
116111

117112
}

module/move/graphs_tools/tests/inc/search_test/dfs_test.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,7 @@ fn test_dfs()
105105
// use the_module::search;
106106
// use the_module::abs;
107107
use the_module::search::ForGraphDirected;
108-
109-
// Create nodes
110-
let mut node1 = Node::new( 1 );
111-
let node2 = Node::new( 2 );
112-
let node3 = Node::new( 3 );
113-
let node4 = Node::new( 4 );
114-
115-
// Set up the graph structure
116-
node1
117-
.add_child( &node2 )
118-
.add_child( &node3 )
119-
.add_child( &node4 );
120-
121-
let mut graph = Graph::default();
122-
graph.add_node( &node1 );
123-
graph.add_node( &node2 );
124-
graph.add_node( &node3 );
125-
graph.add_node( &node4 );
108+
let mut graph = Graph::duplet();
126109

127110
// Prepare a vector to collect visited nodes
128111
let mut visited_nodes = Vec::new();
@@ -161,14 +144,14 @@ fn test_dfs()
161144

162145
// Create search options
163146
the_module::search::options()
164-
.start_id( 1 )
147+
.start_id( 0 )
165148
.visit_set( visit )
166149
.method_set( the_module::search::Dfs )
167150
.form()
168151
.search( &graph )
169152
;
170153

171154
// Assert the order of visited nodes
172-
assert_eq!( visited_nodes, into_vec![ 1, 4, 3, 2 ] );
155+
assert_eq!( visited_nodes, into_vec![ 0, 2, 1 ] );
173156

174157
}

0 commit comments

Comments
 (0)