Function collect_runs

Source
pub fn collect_runs<G, F, E>(
    graph: G,
    include_node_fn: F,
) -> Option<impl Iterator<Item = Result<Vec<G::NodeId>, E>>>
Expand description

Collect runs that match a filter function

A run is a path of nodes where there is only a single successor and all nodes in the path match the given condition. Each node in the graph can appear in only a single run.

§Arguments:

  • graph: The DAG to collect runs from
  • include_node_fn: A filter function used for matching nodes. It takes in one argument, the node data payload/weight object, and returns a boolean whether the node matches the conditions or not. If it returns false, the node will be skipped, cutting the run it’s part of.

§Returns:

  • An Iterator object for extracting the runs one by one. Each run is of type Result<Vec<G::NodeId>>.
  • None if a cycle is found in the graph.

§Example

use petgraph::graph::DiGraph;
use rustworkx_core::dag_algo::collect_runs;

let mut graph: DiGraph<i32, ()> = DiGraph::new();
let n1 = graph.add_node(-1);
let n2 = graph.add_node(2);
let n3 = graph.add_node(3);
graph.add_edge(n1, n2, ());
graph.add_edge(n1, n3, ());

let positive_payload = |n| -> Result<bool, ()> {Ok(*graph.node_weight(n).expect("i32") > 0)};
let mut runs = collect_runs(&graph, positive_payload).expect("Some");

assert_eq!(runs.next(), Some(Ok(vec![n3])));
assert_eq!(runs.next(), Some(Ok(vec![n2])));
assert_eq!(runs.next(), None);