From c0197718b02112834b3795f64ca244f7c052b385 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 21 Jan 2025 11:51:59 +0000 Subject: [PATCH 1/2] refactor PortGraph::port_links --- src/portgraph.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/portgraph.rs b/src/portgraph.rs index 88168e3..81c8a28 100644 --- a/src/portgraph.rs +++ b/src/portgraph.rs @@ -712,14 +712,9 @@ impl LinkView for PortGraph { fn port_links(&self, port: PortIndex) -> impl Iterator + Clone { self.port_meta_valid(port).unwrap(); - match self.port_link[port.index()] { - Some(link) => std::iter::once((port, link)), - None => { - let mut iter = std::iter::once((port, port)); - iter.next(); - iter - } - } + self.port_link[port.index()] + .map(|link| (port, link)) + .into_iter() } #[inline] From 30f998e09f76dac8a8b0191a15253afbf16e26f4 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Tue, 21 Jan 2025 12:00:20 +0000 Subject: [PATCH 2/2] RPITIT pt1 - Inline MultiPortgraph helper methods, keep _sub_port meths --- src/multiportgraph.rs | 56 ++++++++++++++++++++++--------- src/multiportgraph/iter.rs | 69 ++++++-------------------------------- 2 files changed, 52 insertions(+), 73 deletions(-) diff --git a/src/multiportgraph.rs b/src/multiportgraph.rs index 0a3b403..f0a4055 100644 --- a/src/multiportgraph.rs +++ b/src/multiportgraph.rs @@ -265,21 +265,47 @@ impl LinkView for MultiPortGraph { self.graph.link_count() - self.copy_node_count } - delegate! { - to self { - #[call(_get_connections)] - fn get_connections(&self, from: NodeIndex, to: NodeIndex) -> impl Iterator + Clone; - #[call(_port_links)] - fn port_links(&self, port: PortIndex) -> impl Iterator + Clone; - #[call(_links)] - fn links(&self, node: NodeIndex, direction: Direction) -> impl Iterator + Clone; - #[call(_all_links)] - fn all_links(&self, node: NodeIndex) -> impl Iterator + Clone; - #[call(_neighbours)] - fn neighbours(&self, node: NodeIndex, direction: Direction) -> impl Iterator + Clone; - #[call(_all_neighbours)] - fn all_neighbours(&self, node: NodeIndex) -> impl Iterator + Clone; - } + fn get_connections( + &self, + from: NodeIndex, + to: NodeIndex, + ) -> impl Iterator + Clone { + NodeConnections::new(self, to, self.output_links(from)) + } + + fn port_links( + &self, + port: PortIndex, + ) -> impl Iterator + Clone { + PortLinks::new(self, port) + } + + fn links( + &self, + node: NodeIndex, + direction: Direction, + ) -> impl Iterator + Clone { + NodeLinks::new(self, self.graph._ports(node, direction), 0..0) + } + + fn all_links( + &self, + node: NodeIndex, + ) -> impl Iterator + Clone { + let output_ports = self.graph.node_outgoing_ports(node); + NodeLinks::new(self, self.graph._all_ports(node), output_ports) + } + + fn neighbours( + &self, + node: NodeIndex, + direction: Direction, + ) -> impl Iterator + Clone { + Neighbours::new(self, self._subports(node, direction), node, false) + } + + fn all_neighbours(&self, node: NodeIndex) -> impl Iterator + Clone { + Neighbours::new(self, self._all_subports(node), node, true) } } diff --git a/src/multiportgraph/iter.rs b/src/multiportgraph/iter.rs index 5a444cf..5e530d1 100644 --- a/src/multiportgraph/iter.rs +++ b/src/multiportgraph/iter.rs @@ -12,54 +12,6 @@ use crate::{Direction, LinkView, NodeIndex, PortIndex, PortOffset, PortView}; /// /// Used internally by other iterator implementations to avoid the generic RPITIT return types. impl MultiPortGraph { - #[inline] - /// Returns an iterator over every pair of matching ports connecting `from` - /// with `to`. - pub(crate) fn _get_connections(&self, from: NodeIndex, to: NodeIndex) -> NodeConnections { - NodeConnections::new(self, to, self._output_links(from)) - } - - /// Returns the port that the given `port` is linked to. - #[inline] - pub(crate) fn _port_links(&self, port: PortIndex) -> PortLinks { - PortLinks::new(self, port) - } - - /// Iterates over the connected links of the `node` in the given - /// `direction`. - #[inline] - pub(crate) fn _links(&self, node: NodeIndex, direction: Direction) -> NodeLinks { - NodeLinks::new(self, self.graph._ports(node, direction), 0..0) - } - - /// Iterates over the connected input and output links of the `node` in sequence. - #[inline] - pub(crate) fn _all_links(&self, node: NodeIndex) -> NodeLinks { - let output_ports = self.graph.node_outgoing_ports(node); - NodeLinks::new(self, self.graph._all_ports(node), output_ports) - } - - /// Iterates over the connected output links of the `node`. Shorthand for - /// [`LinkView::links`]. - #[must_use] - #[inline] - pub(crate) fn _output_links(&self, node: NodeIndex) -> NodeLinks { - self._links(node, Direction::Outgoing) - } - - /// Iterates over neighbour nodes in the given `direction`. - /// May contain duplicates if the graph has multiple links between nodes. - #[inline] - pub(crate) fn _neighbours(&self, node: NodeIndex, direction: Direction) -> Neighbours { - Neighbours::new(self, self._subports(node, direction), node, false) - } - - /// Iterates over the input and output neighbours of the `node` in sequence. - #[inline] - pub(crate) fn _all_neighbours(&self, node: NodeIndex) -> Neighbours { - Neighbours::new(self, self._all_subports(node), node, true) - } - /// Iterates over all the subports of the `node` in the given `direction`. #[inline] pub(crate) fn _subports(&self, node: NodeIndex, direction: Direction) -> NodeSubports { @@ -321,18 +273,14 @@ impl FusedIterator for NodeLinks<'_> {} /// Iterator over the links between two nodes, created by /// [`MultiPortGraph::get_connections`]. #[derive(Debug, Clone)] -pub struct NodeConnections<'a> { +pub struct NodeConnections<'a, NL: 'a> { multigraph: &'a MultiPortGraph, target: NodeIndex, - links: NodeLinks<'a>, + links: NL, } -impl<'a> NodeConnections<'a> { - pub(super) fn new( - multigraph: &'a MultiPortGraph, - target: NodeIndex, - links: NodeLinks<'a>, - ) -> Self { +impl<'a, NL: Iterator + 'a> NodeConnections<'a, NL> { + pub(super) fn new(multigraph: &'a MultiPortGraph, target: NodeIndex, links: NL) -> Self { Self { multigraph, target, @@ -341,7 +289,9 @@ impl<'a> NodeConnections<'a> { } } -impl Iterator for NodeConnections<'_> { +impl<'a, NL: Iterator + 'a> Iterator + for NodeConnections<'a, NL> +{ /// A link from one of the node's subports to another subport. type Item = (SubportIndex, SubportIndex); @@ -356,7 +306,10 @@ impl Iterator for NodeConnections<'_> { } } -impl FusedIterator for NodeConnections<'_> {} +impl<'a, NL: Iterator + 'a> FusedIterator + for NodeConnections<'a, NL> +{ +} /// Iterator over the links of a port #[derive(Debug, Clone)]