Skip to content

Commit

Permalink
delegate! to internal iterator impls
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Nov 14, 2024
1 parent 901cfad commit bd79b3b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 133 deletions.
84 changes: 24 additions & 60 deletions src/multiportgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,59 +259,28 @@ impl PortMut for MultiPortGraph {
impl LinkView for MultiPortGraph {
type LinkEndpoint = SubportIndex;

#[inline]
fn get_connections(
&self,
from: NodeIndex,
to: NodeIndex,
) -> impl Iterator<Item = (Self::LinkEndpoint, Self::LinkEndpoint)> + Clone {
self._get_connections(from, to)
}

#[inline]
fn port_links(
&self,
port: PortIndex,
) -> impl Iterator<Item = (Self::LinkEndpoint, Self::LinkEndpoint)> + Clone {
self._port_links(port)
}

#[inline]
fn links(
&self,
node: NodeIndex,
direction: Direction,
) -> impl Iterator<Item = (Self::LinkEndpoint, Self::LinkEndpoint)> + Clone {
self._links(node, direction)
}

#[inline]
fn all_links(
&self,
node: NodeIndex,
) -> impl Iterator<Item = (Self::LinkEndpoint, Self::LinkEndpoint)> + Clone {
self._all_links(node)
}

#[inline]
fn neighbours(
&self,
node: NodeIndex,
direction: Direction,
) -> impl Iterator<Item = NodeIndex> + Clone {
self._neighbours(node, direction)
}

#[inline]
fn all_neighbours(&self, node: NodeIndex) -> impl Iterator<Item = NodeIndex> + Clone {
self._all_neighbours(node)
}

#[inline]
fn link_count(&self) -> usize {
// Do not count the links between copy nodes and their main nodes.
self.graph.link_count() - self.copy_node_count
}

delegate! {
to self {
#[call(_get_connections)]
fn get_connections(&self, from: NodeIndex, to: NodeIndex) -> impl Iterator<Item = (Self::LinkEndpoint, Self::LinkEndpoint)> + Clone;
#[call(_port_links)]
fn port_links(&self, port: PortIndex) -> impl Iterator<Item = (Self::LinkEndpoint, Self::LinkEndpoint)> + Clone;
#[call(_links)]
fn links(&self, node: NodeIndex, direction: Direction) -> impl Iterator<Item = (Self::LinkEndpoint, Self::LinkEndpoint)> + Clone;
#[call(_all_links)]
fn all_links(&self, node: NodeIndex) -> impl Iterator<Item = (Self::LinkEndpoint, Self::LinkEndpoint)> + Clone;
#[call(_neighbours)]
fn neighbours(&self, node: NodeIndex, direction: Direction) -> impl Iterator<Item = NodeIndex> + Clone;
#[call(_all_neighbours)]
fn all_neighbours(&self, node: NodeIndex) -> impl Iterator<Item = NodeIndex> + Clone;
}
}
}

impl LinkMut for MultiPortGraph {
Expand Down Expand Up @@ -346,18 +315,13 @@ impl MultiView for MultiPortGraph {
self.get_subport_from_index(link)
}

#[inline]
fn subports(
&self,
node: NodeIndex,
direction: Direction,
) -> impl Iterator<Item = SubportIndex> + Clone {
self._subports(node, direction)
}

#[inline]
fn all_subports(&self, node: NodeIndex) -> impl Iterator<Item = SubportIndex> + Clone {
self._all_subports(node)
delegate! {
to self {
#[call(_subports)]
fn subports(&self, node: NodeIndex, direction: Direction) -> impl Iterator<Item = SubportIndex> + Clone;
#[call(_all_subports)]
fn all_subports(&self, node: NodeIndex) -> impl Iterator<Item = SubportIndex> + Clone;
}
}
}

Expand Down
105 changes: 32 additions & 73 deletions src/portgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
mod iter;

use delegate::delegate;
use std::mem::{replace, take};
use std::num::{NonZeroU16, NonZeroU32};
use std::ops::Range;
Expand Down Expand Up @@ -352,18 +353,6 @@ impl PortView for PortGraph {
node_meta.ports(direction).nth(offset).map(PortIndex::new)
}

fn ports(
&self,
node: NodeIndex,
direction: Direction,
) -> impl Iterator<Item = PortIndex> + Clone {
self._ports(node, direction)
}

fn all_ports(&self, node: NodeIndex) -> impl Iterator<Item = PortIndex> + Clone {
self._all_ports(node)
}

#[inline]
fn input(&self, node: NodeIndex, offset: usize) -> Option<PortIndex> {
self.port_index(node, PortOffset::new_incoming(offset))
Expand All @@ -385,19 +374,6 @@ impl PortView for PortGraph {
}
}

fn port_offsets(
&self,
node: NodeIndex,
direction: Direction,
) -> impl Iterator<Item = PortOffset> + Clone {
self._port_offsets(node, direction)
}

#[inline]
fn all_port_offsets(&self, node: NodeIndex) -> impl Iterator<Item = PortOffset> + Clone {
self._all_port_offsets(node)
}

#[inline]
fn contains_node(&self, node: NodeIndex) -> bool {
self.node_meta_valid(node).is_some()
Expand All @@ -423,16 +399,6 @@ impl PortView for PortGraph {
self.port_count
}

#[inline]
fn nodes_iter(&self) -> impl Iterator<Item = NodeIndex> + Clone {
self._nodes_iter()
}

#[inline]
fn ports_iter(&self) -> impl Iterator<Item = PortIndex> + Clone {
self._ports_iter()
}

#[inline]
fn node_capacity(&self) -> usize {
self.node_meta.capacity()
Expand All @@ -448,6 +414,22 @@ impl PortView for PortGraph {
self.node_meta_valid(node)
.map_or(0, |node_meta| node_meta.capacity())
}
delegate! {
to self {
#[call(_ports)]
fn ports(&self, node: NodeIndex, direction: Direction) -> impl Iterator<Item = PortIndex> + Clone;
#[call(_all_ports)]
fn all_ports(&self, node: NodeIndex) -> impl Iterator<Item = PortIndex> + Clone;
#[call(_port_offsets)]
fn port_offsets(&self, node: NodeIndex, direction: Direction) -> impl Iterator<Item = PortOffset> + Clone;
#[call(_all_port_offsets)]
fn all_port_offsets(&self, node: NodeIndex) -> impl Iterator<Item = PortOffset> + Clone;
#[call(_nodes_iter)]
fn nodes_iter(&self) -> impl Iterator<Item = NodeIndex> + Clone;
#[call(_ports_iter)]
fn ports_iter(&self) -> impl Iterator<Item = PortIndex> + Clone;
}
}
}

impl PortMut for PortGraph {
Expand Down Expand Up @@ -728,15 +710,6 @@ impl PortMut for PortGraph {
impl LinkView for PortGraph {
type LinkEndpoint = PortIndex;

#[inline]
fn get_connections(
&self,
from: NodeIndex,
to: NodeIndex,
) -> impl Iterator<Item = (PortIndex, PortIndex)> + Clone {
self._get_connections(from, to)
}

fn port_links(&self, port: PortIndex) -> impl Iterator<Item = (PortIndex, PortIndex)> + Clone {
self.port_meta_valid(port).unwrap();
match self.port_link[port.index()] {
Expand All @@ -749,39 +722,25 @@ impl LinkView for PortGraph {
}
}

fn links(
&self,
node: NodeIndex,
direction: Direction,
) -> impl Iterator<Item = (Self::LinkEndpoint, Self::LinkEndpoint)> + Clone {
self._links(node, direction)
}

fn all_links(
&self,
node: NodeIndex,
) -> impl Iterator<Item = (Self::LinkEndpoint, Self::LinkEndpoint)> + Clone {
self._all_links(node)
}

#[inline]
fn neighbours(
&self,
node: NodeIndex,
direction: Direction,
) -> impl Iterator<Item = NodeIndex> + Clone {
self._neighbours(node, direction)
}

#[inline]
fn all_neighbours(&self, node: NodeIndex) -> impl Iterator<Item = NodeIndex> + Clone {
self._all_neighbours(node)
}

#[inline]
fn link_count(&self) -> usize {
self.link_count
}

delegate! {
to self {
#[call(_get_connections)]
fn get_connections(&self, from: NodeIndex, to: NodeIndex) -> impl Iterator<Item = (Self::LinkEndpoint, Self::LinkEndpoint)> + Clone;
#[call(_links)]
fn links(&self, node: NodeIndex, direction: Direction) -> impl Iterator<Item = (Self::LinkEndpoint, Self::LinkEndpoint)> + Clone;
#[call(_all_links)]
fn all_links(&self, node: NodeIndex) -> impl Iterator<Item = (Self::LinkEndpoint, Self::LinkEndpoint)> + Clone;
#[call(_neighbours)]
fn neighbours(&self, node: NodeIndex, direction: Direction) -> impl Iterator<Item = NodeIndex> + Clone;
#[call(_all_neighbours)]
fn all_neighbours(&self, node: NodeIndex) -> impl Iterator<Item = NodeIndex> + Clone;
}
}
}

impl LinkMut for PortGraph {
Expand Down

0 comments on commit bd79b3b

Please sign in to comment.