Skip to content

Commit 76e267d

Browse files
committed
graphs tools : refactoring
1 parent 6a86d70 commit 76e267d

File tree

8 files changed

+128
-9
lines changed

8 files changed

+128
-9
lines changed

module/core/format_tools/src/format/print.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ mod private
230230
pub struct RowDescriptor
231231
{
232232

233-
233+
234234
/// Index of the row.
235235
pub irow : usize,
236236
/// Height of the row.
@@ -387,7 +387,7 @@ mod private
387387

388388
let rows = table.rows().map( | r |
389389
{
390-
let mut unsorted : Vec< ( usize, Cow< 'data, str > ) > = r.cells().map( | ( key, c ) |
390+
let mut unsorted : Vec< ( usize, Cow< 'data, str > ) > = r.cells().map( | ( key, c ) |
391391
{
392392
if !key_to_ikey.contains_key( key.borrow() )
393393
{
@@ -557,7 +557,7 @@ mod private
557557

558558
mchars[ 0 ] = col_descriptors.iter().fold( 0, | acc, col | acc + col.width );
559559
mchars[ 1 ] = row_descriptors.iter().fold( 0, | acc, row | acc + if row.vis { row.height } else { 0 } );
560-
560+
561561
let mut x = InputExtract::< '_ >
562562
{
563563
mcells,

module/move/graphs_tools/src/abs.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ mod private
6767
/// Iterate over out nodes of
6868
fn node_out_nodes( &'a self, node_id : Self::NodeId ) -> BoxedIter< 'a, Self::NodeId >;
6969

70-
71-
7270
}
7371

7472
}

module/move/graphs_tools/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ mod_interface!
4343
// #[ cfg( not( feature = "no_std" ) ) ]
4444
// layer algo;
4545

46+
/// Print tree.
47+
layer tree_print;
48+
4649
// own use ::meta_tools::prelude::*;
4750
}
4851

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
/// Define a private namespace for all its items.
3+
mod private
4+
{
5+
6+
use crate::*;
7+
pub use iter_tools::{ _IterTrait, IterTrait, BoxedIter };
8+
9+
use std::
10+
{
11+
hash::Hash,
12+
fmt,
13+
};
14+
15+
// /// Represent directed graph. Can be zero-sized structure if nodes own all the information.
16+
// pub trait GraphDirected< 'a >
17+
// {
18+
// /// Uniquely identify a node.
19+
// type NodeId : NodeId;
20+
// /// Node itself.
21+
// type Node : Node + 'a;
22+
//
23+
// /// Get a reference on a node by its id.
24+
// fn node_ref( &'a self, node_id : Self::NodeId ) -> &'a Self::Node;
25+
// /// Get id by its node reference.
26+
// fn node_id( &self, node_id : &'a Self::Node ) -> Self::NodeId;
27+
//
28+
// /// Iterate over out nodes of
29+
// fn node_out_nodes( &'a self, node_id : Self::NodeId ) -> BoxedIter< 'a, Self::NodeId >;
30+
//
31+
// }
32+
33+
/// Print directed graph as a tree.
34+
pub trait GraphDirectedPrintAsTree< 'g >
35+
where
36+
Self : abs::GraphDirected< 'g >,
37+
{
38+
39+
/// Print directed graph as a tree.
40+
fn _print_as_tree< 'w >( &self, write : &'w mut ( dyn core::fmt::Write + 'w ), node : Self::NodeId ) -> fmt::Result
41+
{
42+
// let node = self.node_ref( node );
43+
write.write_fmt( format_args!( "{:?}", node ) )
44+
}
45+
46+
/// Print directed graph as a tree.
47+
fn print_as_tree< 'w >( &self, node : Self::NodeId ) -> String
48+
{
49+
// let node = self.node_ref( node );
50+
let mut result = String::new();
51+
self._print_as_tree( &mut result, node ).unwrap();
52+
result
53+
}
54+
55+
}
56+
57+
impl< 'g, T > GraphDirectedPrintAsTree< 'g > for T
58+
where
59+
Self : abs::GraphDirected< 'g >,
60+
{
61+
}
62+
63+
// impl fmt::Debug for Context< '_ >
64+
// {
65+
// fn fmt( &self, c : &mut fmt::Formatter< '_ > ) -> fmt::Result
66+
// {
67+
// c
68+
// .debug_struct( "Context" )
69+
// .field( "buf", &"dyn fmt::Write" )
70+
// .field( "printer", &self.printer )
71+
// .finish()
72+
// }
73+
// }
74+
75+
}
76+
77+
crate::mod_interface!
78+
{
79+
own use
80+
{
81+
GraphDirectedPrintAsTree,
82+
};
83+
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::*;
33
use derive_tools::From;
44
use the_module::abs;
55
use iter_tools::{ _IterTrait, IterTrait, BoxedIter };
6+
use std::fmt;
67

78
#[ derive( Debug ) ]
89
pub struct Node
@@ -13,6 +14,7 @@ pub struct Node
1314

1415
impl the_module::abs::Node for Node {}
1516

17+
#[ allow( dead_code ) ]
1618
impl Node
1719
{
1820
pub fn new< IntoId : Into< NodeId > >( id : IntoId ) -> Node
@@ -49,6 +51,7 @@ pub struct Graph
4951
nodes : HashMap< NodeId, Node >,
5052
}
5153

54+
#[ allow( dead_code ) ]
5255
impl Graph
5356
{
5457

@@ -99,14 +102,23 @@ impl< 'a > abs::GraphDirected< 'a > for Graph
99102
}
100103
}
101104

102-
#[ derive( Debug, Copy, Clone, Hash, PartialEq, Eq, From ) ]
105+
#[ derive( Copy, Clone, Hash, PartialEq, Eq, From ) ]
103106
pub struct NodeId( usize );
104107

108+
impl fmt::Debug for NodeId
109+
{
110+
fn fmt( &self, c : &mut fmt::Formatter< '_ > ) -> fmt::Result
111+
{
112+
c
113+
.write_fmt( format_args!( "node::{:?}", self.0 ) )
114+
}
115+
}
116+
105117
impl the_module::abs::NodeId for NodeId {}
106118

107-
// xxx
119+
// Constructors
108120

109-
use std::cell::RefCell;
121+
#[ allow( dead_code ) ]
110122
impl Graph
111123
{
112124

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ pub mod graph;
77
mod basic_test;
88
mod nodes_test;
99
mod search_test;
10+
mod tree_print_test;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn test_dfs()
105105
// use the_module::search;
106106
// use the_module::abs;
107107
use the_module::search::ForGraphDirected;
108-
let mut graph = Graph::duplet();
108+
let graph = Graph::duplet();
109109

110110
// Prepare a vector to collect visited nodes
111111
let mut visited_nodes = Vec::new();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use super::*;
2+
3+
use graph::map_of_nodes::
4+
{
5+
Node, NodeId, Graph,
6+
};
7+
8+
// =
9+
10+
#[ test ]
11+
fn _print_as_tree()
12+
{
13+
use the_module::tree_print::GraphDirectedPrintAsTree;
14+
let graph = Graph::duplet();
15+
16+
let mut got = String::new();
17+
let r = graph._print_as_tree( &mut got, 1.into() );
18+
let exp = "node::1";
19+
assert_eq!( got, exp );
20+
assert!( r.is_ok() );
21+
22+
}

0 commit comments

Comments
 (0)