1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use std::ops::ControlFlow;

use crate::graph::*;

/// A recursive depth first traversal implementation,
/// if user provided closure returns [ControlFlow::Break] that particular node's edges will not be visited,
/// but will not stop the whole traversal.
pub fn depth_first<G, F>(graph: &G, index: G::Idx, callback: &mut F)
where
    G: Graph,
    F: FnMut(G::Idx) -> ControlFlow<()>,
{
    let node = graph.index(index);

    if let ControlFlow::Continue(()) = callback(index) {
        for edge in node.edges() {
            depth_first(graph, edge.target(), callback);
        }
    }
}