Skip to content

Commit

Permalink
Generalize TopoSort iterator impl (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q authored Jun 6, 2023
1 parent 53a3285 commit 5747394
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
23 changes: 13 additions & 10 deletions src/algorithms/toposort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use std::{collections::VecDeque, iter::FusedIterator};
/// # Example
///
/// ```
/// # use portgraph::{algorithms::toposort, Direction, PortGraph};
/// # use portgraph::{algorithms::{toposort, TopoSort}, Direction, PortGraph};
/// let mut graph = PortGraph::new();
/// let node_a = graph.add_node(2, 2);
/// let node_b = graph.add_node(2, 2);
/// graph.link_nodes(node_a, 0, node_b, 0).unwrap();
///
/// // Run a topological sort on the graph starting at node A.
/// let topo = toposort(&graph, [node_a], Direction::Outgoing);
/// let topo: TopoSort = toposort(&graph, [node_a], Direction::Outgoing);
/// assert_eq!(topo.collect::<Vec<_>>(), [node_a, node_b]);
/// ```
pub fn toposort<Map>(
Expand Down Expand Up @@ -55,7 +55,7 @@ where
///
/// ```
/// # use portgraph::{Direction, PortGraph};
/// # use portgraph::algorithms::{toposort, toposort_filtered};
/// # use portgraph::algorithms::{toposort, toposort_filtered, TopoSort};
/// let mut graph = PortGraph::new();
/// let node_a = graph.add_node(2, 2);
/// let node_b = graph.add_node(2, 2);
Expand All @@ -65,7 +65,7 @@ where
/// graph.link_nodes(node_a, 1, node_c, 0).unwrap();
///
/// // Run a topological sort on the graph starting at node A.
/// let topo = toposort_filtered(
/// let topo: TopoSort = toposort_filtered(
/// &graph,
/// [node_a, node_d],
/// Direction::Outgoing,
Expand Down Expand Up @@ -215,14 +215,17 @@ where
}
}

impl<'graph> Iterator for TopoSort<'graph> {
impl<'graph, Map> Iterator for TopoSort<'graph, Map>
where
Map: SecondaryMap<PortIndex, bool>,
{
type Item = NodeIndex;

fn next(&mut self) -> Option<Self::Item> {
let node = self.candidate_nodes.pop_front()?;

for port in self.graph.ports(node, self.direction) {
self.visited_ports.set(port.index(), true);
self.visited_ports.set(port, true);

if self.ignore_port(node, port) {
continue;
Expand All @@ -234,7 +237,7 @@ impl<'graph> Iterator for TopoSort<'graph> {
if self.becomes_ready(target, link) {
self.candidate_nodes.push_back(target);
}
self.visited_ports.set(link.index(), true);
self.visited_ports.set(link, true);
}
}

Expand All @@ -251,7 +254,7 @@ impl<'graph> Iterator for TopoSort<'graph> {
}
}

impl<'graph> FusedIterator for TopoSort<'graph> {}
impl<'graph, Map> FusedIterator for TopoSort<'graph, Map> where Map: SecondaryMap<PortIndex, bool> {}

#[cfg(test)]
mod test {
Expand All @@ -276,13 +279,13 @@ mod test {
graph.link_nodes(node_c, 0, node_d, 0).unwrap();

// Run a topological sort on the graph starting at node A.
let topo = toposort(&graph, [node_a, node_d], Direction::Outgoing);
let topo: TopoSort = toposort(&graph, [node_a, node_d], Direction::Outgoing);
assert_eq!(
topo.collect::<Vec<_>>(),
[node_a, node_d, node_b, node_e, node_c]
);

let topo_filtered = toposort_filtered(
let topo_filtered: TopoSort = toposort_filtered(
&graph,
[node_a, node_d],
Direction::Outgoing,
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//!
//! ```
//! use portgraph::{PortGraph, Direction};
//! use portgraph::algorithms::toposort;
//! use portgraph::algorithms::{toposort, TopoSort};
//!
//! // Create a graph with two nodes, each with two input and two output ports
//! let mut graph = PortGraph::new();
Expand All @@ -43,7 +43,7 @@
//! graph.link_ports(port_a, port_b).unwrap();
//!
//! // Run a topological sort on the graph starting at node A.
//! let topo = toposort(&graph, [node_a], Direction::Outgoing);
//! let topo: TopoSort = toposort(&graph, [node_a], Direction::Outgoing);
//! assert_eq!(topo.collect::<Vec<_>>(), [node_a, node_b]);
//! ```
//!
Expand Down

0 comments on commit 5747394

Please sign in to comment.