Skip to content

Commit 541baf8

Browse files
committed
graphs_tools : reimplementing
1 parent cb9a95e commit 541baf8

File tree

11 files changed

+492
-18
lines changed

11 files changed

+492
-18
lines changed

module/move/graphs_tools/Cargo.toml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,30 @@ workspace = true
2323
features = [ "full" ]
2424
all-features = false
2525

26-
2726
[features]
2827
default = [
29-
"enabled"
28+
"enabled",
29+
"debug",
3030
]
3131
full = [
32-
"enabled",
32+
"default",
33+
]
34+
enabled = [
35+
"meta_tools/enabled",
36+
"iter_tools/enabled",
37+
# "data_type/enabled",
38+
# "strs_tools/enabled",
39+
"collection_tools/enabled",
3340
]
34-
no_std = []
35-
use_alloc = [ "no_std" ]
36-
enabled = [ "meta_tools/enabled", "iter_tools/enabled", "data_type/enabled", "strs_tools/enabled" ]
41+
debug = []
3742

3843
[dependencies]
39-
indexmap = "~1.8"
44+
# indexmap = "~1.8"
4045
meta_tools = { workspace = true, features = [ "default" ] }
4146
iter_tools = { workspace = true, features = [ "default" ] }
42-
data_type = { workspace = true, features = [ "default" ] }
43-
strs_tools = { workspace = true, features = [ "default" ] }
47+
# data_type = { workspace = true, features = [ "default" ] }
48+
collection_tools = { workspace = true, features = [ "default" ] }
49+
# strs_tools = { workspace = true, features = [ "default" ] }
4450
derive_tools = { workspace = true, features = [ "default" ] }
4551
# type_constructor ={ workspace = true, features = [ "default" ] }
4652

module/move/graphs_tools/src/abs.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
mod private
44
{
55

6+
pub use iter_tools::{ _IterTrait, IterTrait, BoxedIter };
7+
68
use std::
79
{
810
hash::Hash,
@@ -39,14 +41,46 @@ mod private
3941
{
4042
}
4143

42-
/// Uniquely identify the node.
44+
/// Uniquely identify a node.
4345
pub trait NodeId : IdentityInterface
4446
{
4547
}
4648

49+
/// Node itsef.
50+
pub trait Node
51+
{
52+
}
53+
54+
/// Represent directed graph. Can be zero-sized structure if nodes own all the information.
55+
pub trait GraphDirected< 'a >
56+
{
57+
/// Uniquely identify a node.
58+
type NodeId : NodeId;
59+
/// Node itself.
60+
type Node : Node + 'a;
61+
62+
/// Get a reference on a node by its id.
63+
fn node_ref( &self, node_id : Self::NodeId ) -> &'a Self::Node;
64+
/// Get id by its node reference.
65+
fn node_id( &self, node_id : &'a Self::Node ) -> Self::NodeId;
66+
67+
/// Iterate over out nodes of
68+
fn node_out_nodes( &self, node_id : Self::NodeId ) -> BoxedIter< 'a, Self::NodeId >;
69+
// fn node_out_nodes( &self, node_id : Self::NodeId ) -> impl _IterTrait< 'a, Self::NodeId >;
70+
71+
}
72+
4773
}
4874

4975
crate::mod_interface!
5076
{
51-
own use { IdentityInterface, NodeId };
77+
own use
78+
{
79+
_IterTrait,
80+
IdentityInterface,
81+
NodeId,
82+
Node,
83+
GraphDirected,
84+
85+
};
5286
}

module/move/graphs_tools/src/canonical.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
/// Define a private namespace for all its items.
33
mod private
44
{
5+
56
}
67

78
crate::mod_interface!
89
{
10+
911
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
/// Define a private namespace for all its items.
3+
mod private
4+
{
5+
6+
}
7+
8+
crate::mod_interface!
9+
{
10+
11+
}

module/move/graphs_tools/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![ cfg_attr( feature = "no_std", no_std ) ]
1+
// #![ cfg_attr( feature = "no_std", no_std ) ]
22
#![ doc( html_logo_url = "https://raw.githubusercontent.com/Wandalen/wTools/alpha/asset/img/graph_logo_v1_trans.png" ) ]
33
#![ doc( html_favicon_url = "https://raw.githubusercontent.com/Wandalen/wTools/alpha/asset/img/graph_logo_v1_trans.ico" ) ]
44
#![ doc( html_root_url = "https://docs.rs/graphs_tools/latest/graphs_tools/" ) ]
@@ -13,8 +13,8 @@
1313
#![ allow( unused_imports ) ]
1414
use iter_tools::iter;
1515
// use data_type::dt;
16-
use meta_tools::meta;
17-
use strs_tools::string;
16+
// use meta_tools::meta;
17+
// use strs_tools::string;
1818
use meta_tools::mod_interface;
1919

2020
/// Define a private namespace for all its items.
@@ -26,13 +26,18 @@ mod_interface!
2626
{
2727

2828
/// Abstract layer.
29-
#[ cfg( not( feature = "no_std" ) ) ]
3029
layer abs;
3130

31+
/// Search algorithms.
32+
layer search;
33+
3234
/// Canonical representation.
33-
#[ cfg( not( feature = "no_std" ) ) ]
3435
layer canonical;
3536

37+
/// For diagnostics only.
38+
#[ cfg( feature = "debug" ) ]
39+
layer debug;
40+
3641
// /// Algorithms.
3742
// #[ cfg( not( feature = "no_std" ) ) ]
3843
// layer algo;
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
mod private
2+
{
3+
/// Options for configuring a graph search.
4+
#[ derive( Debug ) ]
5+
pub struct Options< 'a, Method, NodeId, Node, Visit >
6+
where
7+
NodeId : crate::abs::NodeId,
8+
Node : crate::abs::Node + 'a,
9+
Visit : FnMut( &'a Node ),
10+
Method : super::Method,
11+
{
12+
/// Starting node ID for the search.
13+
pub start_id : NodeId,
14+
/// Function to call on each visited node.
15+
pub visit : Visit,
16+
/// Additional options specific to the search method.
17+
pub _extra : Method::ExtraOptions,
18+
/// Phantom data to associate types and lifetimes.
19+
pub _phantom : std::marker::PhantomData< ( Method, Node, &'a () ) >,
20+
}
21+
22+
/// Trait for performing searches on directed graphs.
23+
pub trait ForGraphDirected< 'a > : crate::abs::GraphDirected< 'a >
24+
{
25+
/// Perform a search using specified options and method.
26+
fn search< Visit, Method >
27+
(
28+
&'a self,
29+
o : Options< 'a, Method, Self::NodeId, Self::Node, Visit >,
30+
)
31+
where
32+
Visit : FnMut( &'a Self::Node ),
33+
Method : super::Method,
34+
{
35+
Method::_search( self, o )
36+
}
37+
}
38+
39+
impl< 'a, T > ForGraphDirected< 'a > for T
40+
where
41+
T : crate::abs::GraphDirected< 'a >,
42+
{
43+
}
44+
45+
/// Trait for defining specific search strategies like DFS or BFS.
46+
pub trait Method
47+
{
48+
/// Additional options for the search method.
49+
type ExtraOptions;
50+
51+
/// Execute the search on a graph.
52+
fn _search< 'a, Graph, Visit >
53+
(
54+
graph : &'a Graph,
55+
o : Options< 'a, Self, Graph::NodeId, Graph::Node, Visit >,
56+
)
57+
where
58+
Visit : FnMut( &'a Graph::Node ),
59+
Graph : crate::abs::GraphDirected< 'a > + ?Sized,
60+
Self : Sized;
61+
}
62+
63+
/// Depth-first search strategy.
64+
#[ derive( Debug ) ]
65+
pub struct Dfs;
66+
67+
impl Method for Dfs
68+
{
69+
type ExtraOptions = ();
70+
71+
/// Perform depth-first search on a graph.
72+
fn _search< 'a, Graph, Visit >
73+
(
74+
graph : &'a Graph,
75+
mut o : Options< 'a, Self, Graph::NodeId, Graph::Node, Visit >,
76+
)
77+
where
78+
Visit : FnMut( &'a Graph::Node ),
79+
Graph : crate::abs::GraphDirected< 'a > + ?Sized,
80+
{
81+
let mut visited = collection_tools::HashSet::new();
82+
let mut stack = collection_tools::Vec::new();
83+
stack.push( o.start_id );
84+
85+
while let Some( node_id ) = stack.pop()
86+
{
87+
let node = graph.node_ref( node_id );
88+
if visited.insert( node_id )
89+
{
90+
( o.visit )( node );
91+
for child_id in graph.node_out_nodes( node_id )
92+
{
93+
stack.push( child_id );
94+
}
95+
}
96+
}
97+
}
98+
}
99+
100+
/// Breadth-first search strategy.
101+
#[ derive( Debug ) ]
102+
pub struct Bfs;
103+
104+
impl Method for Bfs
105+
{
106+
type ExtraOptions = ();
107+
108+
/// Perform breadth-first search on a graph.
109+
fn _search< 'a, Graph, Visit >
110+
(
111+
graph : &'a Graph,
112+
mut o : Options< 'a, Self, Graph::NodeId, Graph::Node, Visit >,
113+
)
114+
where
115+
Visit : FnMut( &'a Graph::Node ),
116+
Graph : crate::abs::GraphDirected< 'a > + ?Sized,
117+
{
118+
let mut visited = collection_tools::HashSet::new();
119+
let mut queue = collection_tools::VecDeque::new();
120+
queue.push_back( o.start_id );
121+
122+
while let Some( node_id ) = queue.pop_front()
123+
{
124+
let node = graph.node_ref( node_id );
125+
if visited.insert( node_id )
126+
{
127+
( o.visit )( node );
128+
for child_id in graph.node_out_nodes( node_id )
129+
{
130+
queue.push_back( child_id );
131+
}
132+
}
133+
}
134+
}
135+
}
136+
}
137+
138+
crate::mod_interface!
139+
{
140+
own use
141+
{
142+
Method,
143+
Options,
144+
ForGraphDirected,
145+
Dfs,
146+
Bfs,
147+
};
148+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![ allow( unused_imports ) ]
22

33
use super::*;
4-
// use std::collections::HashSet;
5-
// use wtools::prelude::*;
64

75
mod basic_test;
6+
mod nodes;
7+
mod search;

0 commit comments

Comments
 (0)