@@ -13,7 +13,7 @@ use crate::visits::{
13
13
use nonmax:: NonMaxUsize ;
14
14
use std:: { collections:: VecDeque , ops:: ControlFlow , ops:: ControlFlow :: Continue } ;
15
15
use sux:: bits:: BitVec ;
16
- use webgraph:: traits:: RandomAccessGraph ;
16
+ use webgraph:: traits:: { RandomAccessGraph , RandomAccessLabeling } ;
17
17
18
18
/// A sequential breadth-first visit.
19
19
///
@@ -103,21 +103,21 @@ use webgraph::traits::RandomAccessGraph;
103
103
///
104
104
/// Note that the iterator modifies the state of the visit, so it can re-use
105
105
/// the allocations.
106
- pub struct Seq < G : RandomAccessGraph > {
107
- graph : G ,
106
+ pub struct Seq < ' a , G : RandomAccessGraph > {
107
+ graph : & ' a G ,
108
108
visited : BitVec ,
109
109
/// The visit queue; to avoid storing distances, we use `None` as a
110
110
/// separator between levels. [`NonMaxUsize`] is used to avoid
111
111
/// storage for the option variant tag.
112
112
queue : VecDeque < Option < NonMaxUsize > > ,
113
113
}
114
114
115
- impl < G : RandomAccessGraph > Seq < G > {
115
+ impl < ' a , G : RandomAccessGraph > Seq < ' a , G > {
116
116
/// Creates a new sequential visit.
117
117
///
118
118
/// # Arguments
119
119
/// * `graph`: an immutable reference to the graph to visit.
120
- pub fn new ( graph : G ) -> Self {
120
+ pub fn new ( graph : & ' a G ) -> Self {
121
121
let num_nodes = graph. num_nodes ( ) ;
122
122
Self {
123
123
graph,
@@ -127,7 +127,7 @@ impl<G: RandomAccessGraph> Seq<G> {
127
127
}
128
128
}
129
129
130
- impl < G : RandomAccessGraph > Sequential < EventPred > for Seq < G > {
130
+ impl < ' a , G : RandomAccessGraph > Sequential < EventPred > for Seq < ' a , G > {
131
131
fn visit_filtered_with <
132
132
R : IntoIterator < Item = usize > ,
133
133
T ,
@@ -256,39 +256,42 @@ impl<G: RandomAccessGraph> Sequential<EventPred> for Seq<G> {
256
256
}
257
257
}
258
258
259
- impl < ' a , G : RandomAccessGraph > IntoIterator for & ' a mut Seq < G > {
259
+ impl < ' a , ' b , G : RandomAccessGraph > IntoIterator for & ' a mut Seq < ' b , G > {
260
260
type Item = usize ;
261
- type IntoIter = BfsOrder < ' a , G > ;
261
+ type IntoIter = BfsOrder < ' a , ' b , G > ;
262
262
263
263
fn into_iter ( self ) -> Self :: IntoIter {
264
264
BfsOrder :: new ( self )
265
265
}
266
266
}
267
267
268
268
/// Iterator on **all nodes** of the graph in a BFS order
269
- pub struct BfsOrder < ' a , G : RandomAccessGraph > {
270
- visit : & ' a mut Seq < G > ,
269
+ pub struct BfsOrder < ' a , ' b , G : RandomAccessGraph > {
270
+ visit : & ' a mut Seq < ' b , G > ,
271
271
/// If the queue is empty, resume the BFS from that node.
272
272
///
273
273
/// This allows initializing the BFS from all orphan nodes without reading
274
274
/// the reverse graph.
275
275
start : usize ,
276
+ succ : <<G as RandomAccessLabeling >:: Labels < ' a > as IntoIterator >:: IntoIter ,
276
277
/// Number of visited nodes, used to compute the length of the iterator.
277
278
visited_nodes : usize ,
278
279
}
279
280
280
- impl < G : RandomAccessGraph > BfsOrder < ' _ , G > {
281
- pub fn new ( visit : & mut Seq < G > ) -> BfsOrder < G > {
281
+ impl < ' a , ' b , G : RandomAccessGraph > BfsOrder < ' a , ' b , G > {
282
+ pub fn new ( visit : & ' a mut Seq < ' b , G > ) -> BfsOrder < ' a , ' b , G > {
282
283
visit. reset ( ) ; // ensure we start from a clean state
284
+ let succ = visit. graph . successors ( 0 ) . into_iter ( ) ;
283
285
BfsOrder {
284
286
visit,
285
287
start : 0 ,
288
+ succ,
286
289
visited_nodes : 0 ,
287
290
}
288
291
}
289
292
}
290
293
291
- impl < G : RandomAccessGraph > Iterator for BfsOrder < ' _ , G > {
294
+ impl < ' a , ' b , G : RandomAccessGraph > Iterator for BfsOrder < ' a , ' b , G > {
292
295
type Item = usize ;
293
296
294
297
fn next ( & mut self ) -> Option < usize > {
@@ -317,7 +320,7 @@ impl<G: RandomAccessGraph> Iterator for BfsOrder<'_, G> {
317
320
}
318
321
}
319
322
320
- impl < G : RandomAccessGraph > ExactSizeIterator for BfsOrder < ' _ , G > {
323
+ impl < ' a , ' b , G : RandomAccessGraph > ExactSizeIterator for BfsOrder < ' a , ' b , G > {
321
324
fn len ( & self ) -> usize {
322
325
self . visit . graph . num_nodes ( ) - self . visited_nodes
323
326
}
0 commit comments