Skip to content

Commit c1de7da

Browse files
committed
Interim, fixing deadloop in BfsOrder
1 parent 02b17c8 commit c1de7da

File tree

1 file changed

+23
-19
lines changed
  • algo/src/visits/breadth_first

1 file changed

+23
-19
lines changed

algo/src/visits/breadth_first/seq.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -330,27 +330,31 @@ impl<'a, 'b, G: RandomAccessGraph> Iterator for BfsOrder<'a, 'b, G> {
330330
}
331331

332332
// the successors are exhausted, so we need to move to the next node
333-
self.current_node = match self.visit.queue.pop_front() {
334-
// if we have a node, we can continue visiting its successors
335-
Some(Some(node)) => node.into(),
336-
// new level separator, so we increment the distance
337-
Some(None) => {
338-
self.distance += 1;
339-
self.visit.queue.push_back(None);
340-
// TODO: this assumes the iter are fuse
341-
continue;
342-
}
343-
// if the queue is empty, we need to find the next unvisited node
344-
None => {
345-
while self.visit.visited[self.start] {
346-
self.start += 1;
347-
if self.start >= self.visit.graph.num_nodes() {
348-
return None;
333+
self.current_node = loop {
334+
match self.visit.queue.pop_front() {
335+
// if we have a node, we can continue visiting its successors
336+
Some(Some(node)) => break node.into(),
337+
// new level separator, so we increment the distance
338+
Some(None) => {
339+
self.distance += 1;
340+
// if the queue is not empty, we need to add a new level separator
341+
if !self.visit.queue.is_empty() {
342+
self.visit.queue.push_back(None);
343+
}
344+
continue;
345+
}
346+
// if the queue is empty, we need to find the next unvisited node
347+
None => {
348+
while self.visit.visited[self.start] {
349+
self.start += 1;
350+
if self.start >= self.visit.graph.num_nodes() {
351+
return None;
352+
}
349353
}
354+
self.visit.visited.set(self.start, true);
355+
self.distance = 0; // new visits, new distance
356+
break self.start
350357
}
351-
self.visit.visited.set(self.start, true);
352-
self.distance = 0; // new visits, new distance
353-
self.start
354358
}
355359
};
356360
// reset the successors iterator for the new current node

0 commit comments

Comments
 (0)