Skip to content

Commit 373c150

Browse files
committed
Lifetime for accessing iterator
1 parent 01d7fbe commit 373c150

File tree

1 file changed

+17
-14
lines changed
  • algo/src/visits/breadth_first

1 file changed

+17
-14
lines changed

algo/src/visits/breadth_first/seq.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::visits::{
1313
use nonmax::NonMaxUsize;
1414
use std::{collections::VecDeque, ops::ControlFlow, ops::ControlFlow::Continue};
1515
use sux::bits::BitVec;
16-
use webgraph::traits::RandomAccessGraph;
16+
use webgraph::traits::{RandomAccessGraph, RandomAccessLabeling};
1717

1818
/// A sequential breadth-first visit.
1919
///
@@ -103,21 +103,21 @@ use webgraph::traits::RandomAccessGraph;
103103
///
104104
/// Note that the iterator modifies the state of the visit, so it can re-use
105105
/// the allocations.
106-
pub struct Seq<G: RandomAccessGraph> {
107-
graph: G,
106+
pub struct Seq<'a, G: RandomAccessGraph> {
107+
graph: &'a G,
108108
visited: BitVec,
109109
/// The visit queue; to avoid storing distances, we use `None` as a
110110
/// separator between levels. [`NonMaxUsize`] is used to avoid
111111
/// storage for the option variant tag.
112112
queue: VecDeque<Option<NonMaxUsize>>,
113113
}
114114

115-
impl<G: RandomAccessGraph> Seq<G> {
115+
impl<'a, G: RandomAccessGraph> Seq<'a, G> {
116116
/// Creates a new sequential visit.
117117
///
118118
/// # Arguments
119119
/// * `graph`: an immutable reference to the graph to visit.
120-
pub fn new(graph: G) -> Self {
120+
pub fn new(graph: &'a G) -> Self {
121121
let num_nodes = graph.num_nodes();
122122
Self {
123123
graph,
@@ -127,7 +127,7 @@ impl<G: RandomAccessGraph> Seq<G> {
127127
}
128128
}
129129

130-
impl<G: RandomAccessGraph> Sequential<EventPred> for Seq<G> {
130+
impl<'a, G: RandomAccessGraph> Sequential<EventPred> for Seq<'a, G> {
131131
fn visit_filtered_with<
132132
R: IntoIterator<Item = usize>,
133133
T,
@@ -256,39 +256,42 @@ impl<G: RandomAccessGraph> Sequential<EventPred> for Seq<G> {
256256
}
257257
}
258258

259-
impl<'a, G: RandomAccessGraph> IntoIterator for &'a mut Seq<G> {
259+
impl<'a, 'b, G: RandomAccessGraph> IntoIterator for &'a mut Seq<'b, G> {
260260
type Item = usize;
261-
type IntoIter = BfsOrder<'a, G>;
261+
type IntoIter = BfsOrder<'a, 'b, G>;
262262

263263
fn into_iter(self) -> Self::IntoIter {
264264
BfsOrder::new(self)
265265
}
266266
}
267267

268268
/// 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>,
271271
/// If the queue is empty, resume the BFS from that node.
272272
///
273273
/// This allows initializing the BFS from all orphan nodes without reading
274274
/// the reverse graph.
275275
start: usize,
276+
succ: <<G as RandomAccessLabeling>::Labels<'a> as IntoIterator>::IntoIter,
276277
/// Number of visited nodes, used to compute the length of the iterator.
277278
visited_nodes: usize,
278279
}
279280

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> {
282283
visit.reset(); // ensure we start from a clean state
284+
let succ = visit.graph.successors(0).into_iter();
283285
BfsOrder {
284286
visit,
285287
start: 0,
288+
succ,
286289
visited_nodes: 0,
287290
}
288291
}
289292
}
290293

291-
impl<G: RandomAccessGraph> Iterator for BfsOrder<'_, G> {
294+
impl<'a, 'b, G: RandomAccessGraph> Iterator for BfsOrder<'a, 'b, G> {
292295
type Item = usize;
293296

294297
fn next(&mut self) -> Option<usize> {
@@ -317,7 +320,7 @@ impl<G: RandomAccessGraph> Iterator for BfsOrder<'_, G> {
317320
}
318321
}
319322

320-
impl<G: RandomAccessGraph> ExactSizeIterator for BfsOrder<'_, G> {
323+
impl<'a, 'b, G: RandomAccessGraph> ExactSizeIterator for BfsOrder<'a, 'b, G> {
321324
fn len(&self) -> usize {
322325
self.visit.graph.num_nodes() - self.visited_nodes
323326
}

0 commit comments

Comments
 (0)