1
- use crate :: core:: { property:: VertexIn , Graph } ;
1
+ use crate :: core:: { property:: VertexIn , Ensure , Graph , GraphDeref } ;
2
2
use std:: borrow:: Borrow ;
3
3
4
4
/// Performs [depth-first traversal](https://mathworld.wolfram.com/Depth-FirstTraversal.html)
@@ -90,59 +90,71 @@ use std::borrow::Borrow;
90
90
///
91
91
/// [`next`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
92
92
/// [`get_vertex`]: ../core/property/trait.HasVertex.html#method.get_vertex
93
- pub struct Dfs < ' a , G , F >
93
+ pub struct Dfs < G , F >
94
94
where
95
- G : ' a + Graph ,
95
+ G : Ensure + GraphDeref ,
96
96
{
97
97
/// A reference to the graph being traversed.
98
98
///
99
99
/// This is use by `Dfs` when doing the traversal. Mutating
100
100
/// this reference between calls to
101
101
/// [`next`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next)
102
102
/// is undefined behaviour.
103
- pub graph : & ' a G ,
103
+ pub graph : G ,
104
104
105
105
/// A custom payload, available to the function called upon a vertex exit.
106
106
/// See [`new`](#method.new).
107
107
pub payload : F ,
108
- visited : Vec < G :: Vertex > ,
108
+ visited : Vec < < G :: Graph as Graph > :: Vertex > ,
109
109
110
110
/// The vertex on the stack, and whether on_exit should be called upon
111
111
/// popping.
112
- stack : Vec < ( G :: Vertex , bool ) > ,
112
+ stack : Vec < ( < G :: Graph as Graph > :: Vertex , bool ) > ,
113
113
114
114
/// Function to call when visiting a vertex
115
- on_visit : fn ( & mut Self , G :: Vertex ) ,
115
+ on_visit : fn ( & G :: Graph , < G :: Graph as Graph > :: Vertex , & mut F ) ,
116
116
117
117
/// Function to call when exiting a vertex.
118
118
///
119
119
/// Provides a reference to the graph, the vertex that is exiting,
120
120
/// and a mutable reference to the payload given to the Dfs.
121
- on_exit : fn ( & G , G :: Vertex , & mut F ) ,
121
+ on_exit : fn ( & G :: Graph , < G :: Graph as Graph > :: Vertex , & mut F ) ,
122
122
123
123
/// Function to call when exploring an edge.
124
124
///
125
125
/// When a vertex is being visited, this function is called for
126
126
/// every outgoing edge, regardless of whether the sinked vertex
127
127
/// (second vertex argument) has already been visited.
128
- on_explore : fn ( & mut Self , G :: Vertex , G :: Vertex , & G :: EdgeWeight ) ,
128
+ on_explore : fn (
129
+ & G :: Graph ,
130
+ <G :: Graph as Graph >:: Vertex ,
131
+ <G :: Graph as Graph >:: Vertex ,
132
+ & <G :: Graph as Graph >:: EdgeWeight ,
133
+ & mut F ,
134
+ ) ,
129
135
}
130
136
131
- impl < ' a , G , F > Dfs < ' a , G , F >
137
+ impl < G , F > Dfs < G , F >
132
138
where
133
- G : ' a + Graph ,
139
+ G : Ensure + GraphDeref ,
134
140
{
135
141
pub fn new (
136
- g : & ' a G ,
137
- on_visit : fn ( & mut Self , G :: Vertex ) ,
138
- on_exit : fn ( & G , G :: Vertex , & mut F ) ,
139
- on_explore : fn ( & mut Self , G :: Vertex , G :: Vertex , & G :: EdgeWeight ) ,
142
+ g : G ,
143
+ on_visit : fn ( & G :: Graph , <G :: Graph as Graph >:: Vertex , & mut F ) ,
144
+ on_exit : fn ( & G :: Graph , <G :: Graph as Graph >:: Vertex , & mut F ) ,
145
+ on_explore : fn (
146
+ & G :: Graph ,
147
+ <G :: Graph as Graph >:: Vertex ,
148
+ <G :: Graph as Graph >:: Vertex ,
149
+ & <G :: Graph as Graph >:: EdgeWeight ,
150
+ & mut F ,
151
+ ) ,
140
152
payload : F ,
141
153
) -> Self
142
154
where
143
- G : VertexIn < 1 > ,
155
+ G :: Graph : VertexIn < 1 > ,
144
156
{
145
- let v = g. vertex_at :: < 0 > ( ) ;
157
+ let v = g. graph ( ) . vertex_at :: < 0 > ( ) ;
146
158
let mut result = Self {
147
159
graph : g,
148
160
visited : Vec :: new ( ) ,
@@ -157,16 +169,22 @@ where
157
169
result
158
170
}
159
171
160
- fn visit ( & mut self , to_return : G :: Vertex )
172
+ fn visit ( & mut self , to_return : < G :: Graph as Graph > :: Vertex )
161
173
{
162
- ( self . on_visit ) ( self , to_return) ;
174
+ ( self . on_visit ) ( self . graph . graph ( ) , to_return, & mut self . payload ) ;
163
175
// Mark visited
164
176
self . visited . push ( to_return) ;
165
177
166
178
// Explore children
167
- for ( child, weight) in self . graph . edges_sourced_in ( to_return. clone ( ) )
179
+ for ( child, weight) in self . graph . graph ( ) . edges_sourced_in ( to_return. clone ( ) )
168
180
{
169
- ( self . on_explore ) ( self , to_return, child, weight. borrow ( ) ) ;
181
+ ( self . on_explore ) (
182
+ self . graph . graph ( ) ,
183
+ to_return,
184
+ child,
185
+ weight. borrow ( ) ,
186
+ & mut self . payload ,
187
+ ) ;
170
188
if !self . visited ( child. clone ( ) )
171
189
{
172
190
// Push to stack without exit mark
@@ -175,7 +193,7 @@ where
175
193
}
176
194
}
177
195
178
- pub fn visited ( & self , v : G :: Vertex ) -> bool
196
+ pub fn visited ( & self , v : < G :: Graph as Graph > :: Vertex ) -> bool
179
197
{
180
198
self . visited . contains ( & v)
181
199
}
@@ -185,7 +203,7 @@ where
185
203
///
186
204
/// If there was nothing to pop and call `on_exit` on, return false,
187
205
/// otherwise returns true.
188
- pub fn advance_next_exit ( & mut self ) -> Option < G :: Vertex >
206
+ pub fn advance_next_exit ( & mut self ) -> Option < < G :: Graph as Graph > :: Vertex >
189
207
{
190
208
while let Some ( last) = self . stack . last ( )
191
209
{
@@ -196,7 +214,7 @@ where
196
214
// If its exit marked, call the closure on it.
197
215
if last. 1
198
216
{
199
- ( self . on_exit ) ( self . graph , last. 0 , & mut self . payload ) ;
217
+ ( self . on_exit ) ( self . graph . graph ( ) , last. 0 , & mut self . payload ) ;
200
218
return Some ( last. 0 ) ;
201
219
}
202
220
}
@@ -208,7 +226,7 @@ where
208
226
None
209
227
}
210
228
211
- pub fn continue_from ( & mut self , v : G :: Vertex ) -> bool
229
+ pub fn continue_from ( & mut self , v : < G :: Graph as Graph > :: Vertex ) -> bool
212
230
{
213
231
if !self . visited ( v. clone ( ) )
214
232
{
@@ -221,16 +239,25 @@ where
221
239
}
222
240
}
223
241
224
- pub fn do_nothing_on_visit ( _: & mut Self , _: G :: Vertex ) { }
242
+ pub fn do_nothing_on_visit ( _: & G :: Graph , _: < G :: Graph as Graph > :: Vertex , _ : & mut F ) { }
225
243
226
- pub fn do_nothing_on_exit ( _: & G , _: G :: Vertex , _: & mut F ) { }
244
+ pub fn do_nothing_on_exit ( _: & G :: Graph , _: < G :: Graph as Graph > :: Vertex , _: & mut F ) { }
227
245
228
- pub fn do_nothing_on_explore ( _: & mut Self , _: G :: Vertex , _: G :: Vertex , _: & G :: EdgeWeight ) { }
246
+ pub fn do_nothing_on_explore (
247
+ _: & G :: Graph ,
248
+ _: <G :: Graph as Graph >:: Vertex ,
249
+ _: <G :: Graph as Graph >:: Vertex ,
250
+ _: & <G :: Graph as Graph >:: EdgeWeight ,
251
+ _: & mut F ,
252
+ )
253
+ {
254
+ }
229
255
}
230
256
231
- impl < ' a , G > Dfs < ' a , G , ( ) >
257
+ impl < G > Dfs < G , ( ) >
232
258
where
233
- G : ' a + VertexIn < 1 > ,
259
+ G : Ensure + GraphDeref ,
260
+ G :: Graph : VertexIn < 1 > ,
234
261
{
235
262
/// Constructs a new `Dfs` to traverse the specified graph.
236
263
///
@@ -247,7 +274,7 @@ where
247
274
///
248
275
/// [`next`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
249
276
/// [`get_vertex`]: ../core/property/trait.HasVertex.html#method.get_vertex
250
- pub fn new_simple ( g : & ' a G ) -> Self
277
+ pub fn new_simple ( g : G ) -> Self
251
278
{
252
279
Self :: new (
253
280
g,
@@ -259,11 +286,11 @@ where
259
286
}
260
287
}
261
288
262
- impl < ' a , G , F > Iterator for Dfs < ' a , G , F >
289
+ impl < ' a , G , F > Iterator for Dfs < G , F >
263
290
where
264
- G : ' a + Graph ,
291
+ G : ' a + Ensure + GraphDeref ,
265
292
{
266
- type Item = G :: Vertex ;
293
+ type Item = < G :: Graph as Graph > :: Vertex ;
267
294
268
295
fn next ( & mut self ) -> Option < Self :: Item >
269
296
{
0 commit comments