Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalise toposort to LinkView #96

Merged
merged 7 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add closure lifetime to Toposort
  • Loading branch information
lmondada committed Jul 28, 2023
commit ff4f5048a69278f4a671fb90ae61c53906ad8e54
2 changes: 1 addition & 1 deletion benches/benchmarks/toposort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use portgraph::{
use super::generators::*;

fn run_toposort(graph: &PortGraph, roots: impl IntoIterator<Item = NodeIndex>) {
let topo: TopoSort = toposort(graph, roots, Direction::Outgoing);
let topo: TopoSort<_> = toposort(graph, roots, Direction::Outgoing);
for n in topo {
black_box(n);
}
Expand Down
43 changes: 23 additions & 20 deletions src/algorithms/toposort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ use std::{collections::VecDeque, fmt::Debug, iter::FusedIterator};
/// let topo: TopoSort<_> = toposort(&graph, [node_a], Direction::Outgoing);
/// assert_eq!(topo.collect::<Vec<_>>(), [node_a, node_b]);
/// ```
pub fn toposort<Map, G: LinkView>(
pub fn toposort<'f, Map, G: LinkView>(
graph: G,
source: impl IntoIterator<Item = NodeIndex>,
direction: Direction,
) -> TopoSort<G, Map>
) -> TopoSort<'f, G, Map>
where
Map: SecondaryMap<PortIndex, bool>,
{
Expand Down Expand Up @@ -75,16 +75,16 @@ where
/// );
/// assert_eq!(topo.collect::<Vec<_>>(), [node_a, node_b]);
/// ```
pub fn toposort_filtered<Map, G>(
pub fn toposort_filtered<'f, Map, G>(
graph: G,
source: impl IntoIterator<Item = NodeIndex>,
source: impl IntoIterator<Item = NodeIndex> + 'f,
direction: Direction,
node_filter: impl FnMut(NodeIndex) -> bool,
port_filter: impl FnMut(NodeIndex, PortIndex) -> bool,
) -> TopoSort<G, Map>
node_filter: impl FnMut(NodeIndex) -> bool + 'f,
port_filter: impl FnMut(NodeIndex, PortIndex) -> bool + 'f,
) -> TopoSort<'f, G, Map>
where
Map: SecondaryMap<PortIndex, bool>,
G: LinkView,
G: LinkView + 'f,
{
TopoSort::new(
graph,
Expand All @@ -100,7 +100,7 @@ where
/// See [`toposort`] for more information.
///
/// Implements [Kahn's algorithm](https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm).
pub struct TopoSort<G, Map = BitVec> {
pub struct TopoSort<'f, G, Map = BitVec> {
graph: G,
visited_ports: Map,
/// A VecDeque is used for the node list to produce a canonical ordering,
Expand All @@ -112,13 +112,13 @@ pub struct TopoSort<G, Map = BitVec> {
nodes_seen: usize,
/// A filter closure for the nodes to visit. If the closure returns false,
/// the node is skipped.
node_filter: Option<Box<dyn FnMut(NodeIndex) -> bool>>,
node_filter: Option<Box<dyn FnMut(NodeIndex) -> bool + 'f>>,
/// A filter closure for the ports to visit. If the closure returns false,
/// the port is skipped.
port_filter: Option<Box<dyn FnMut(NodeIndex, PortIndex) -> bool>>,
port_filter: Option<Box<dyn FnMut(NodeIndex, PortIndex) -> bool + 'f>>,
}

impl<Map, G> TopoSort<G, Map>
impl<'f, Map, G> TopoSort<'f, G, Map>
where
Map: SecondaryMap<PortIndex, bool>,
G: LinkView,
Expand All @@ -131,8 +131,8 @@ where
graph: G,
source: impl IntoIterator<Item = NodeIndex>,
direction: Direction,
mut node_filter: Option<Box<dyn FnMut(NodeIndex) -> bool>>,
port_filter: Option<Box<dyn FnMut(NodeIndex, PortIndex) -> bool>>,
mut node_filter: Option<Box<dyn FnMut(NodeIndex) -> bool + 'f>>,
port_filter: Option<Box<dyn FnMut(NodeIndex, PortIndex) -> bool + 'f>>,
) -> Self {
let mut visited_ports: Map = SecondaryMap::new();

Expand Down Expand Up @@ -179,11 +179,13 @@ where
/// Checks if a node becomes ready once it is visited from `from_port`, i.e.
/// it has been reached from all its linked ports.
fn becomes_ready(&mut self, node: NodeIndex, from_port: impl Into<PortIndex>) -> bool {
let from_port = from_port.into();
if self.ignore_node(node) {
return false;
}
self.graph.ports(node, self.direction.reverse()).all(|p| {
if p == from_port.into() {
let ports: Vec<_> = self.graph.ports(node, self.direction.reverse()).collect();
ports.into_iter().all(|p| {
if p == from_port {
// This port must have not been visited yet. Otherwise, the node
// would have been already been reported as ready and added to
// the candidate list.
Expand Down Expand Up @@ -219,7 +221,7 @@ where
}
}

impl<Map, G> Iterator for TopoSort<G, Map>
impl<'f, Map, G> Iterator for TopoSort<'f, G, Map>
where
Map: SecondaryMap<PortIndex, bool>,
G: LinkView,
Expand All @@ -228,8 +230,9 @@ where

fn next(&mut self) -> Option<Self::Item> {
let node = self.candidate_nodes.pop_front()?;
let ports = self.graph.ports(node, self.direction).collect::<Vec<_>>();

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

if self.ignore_port(node, port) {
Expand Down Expand Up @@ -259,14 +262,14 @@ where
}
}

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

impl<Map, G> Debug for TopoSort<G, Map>
impl<'f, Map, G> Debug for TopoSort<'f, G, Map>
where
Map: Debug,
G: Debug,
Expand Down