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+ }
0 commit comments