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

feat!: impl HugrView for any &(mut) to a HugrView #1678

Merged
merged 18 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
6 changes: 5 additions & 1 deletion hugr-core/src/builder/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,11 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> BlockBuilder<B> {
Dataflow::set_outputs(self, [branch_wire].into_iter().chain(outputs))
}
fn create(base: B, block_n: Node) -> Result<Self, BuildError> {
let block_op = base.get_optype(block_n).as_dataflow_block().unwrap();
let block_op = base
.as_ref()
.get_optype(block_n)
.as_dataflow_block()
.unwrap();
let signature = block_op.inner_signature();
let db = DFGBuilder::create_with_io(base, block_n, signature)?;
Ok(BlockBuilder::from_dfg_builder(db))
Expand Down
30 changes: 26 additions & 4 deletions hugr-core/src/hugr/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::ops::Range;

use delegate::delegate;
use portgraph::{LinkView, MultiPortGraph, PortMut, PortView};

use crate::ops::handle::NodeHandle;
Expand Down Expand Up @@ -31,25 +32,46 @@ pub trait HugrInternals {
fn root_node(&self) -> Node;
}

impl<T: AsRef<Hugr>> HugrInternals for T {
impl HugrInternals for Hugr {
type Portgraph<'p> = &'p MultiPortGraph where Self: 'p;

#[inline]
fn portgraph(&self) -> Self::Portgraph<'_> {
&self.as_ref().graph
&self.graph
}

#[inline]
fn base_hugr(&self) -> &Hugr {
self.as_ref()
self
}

#[inline]
fn root_node(&self) -> Node {
self.as_ref().root.into()
self.root.into()
}
}

impl<T: HugrInternals> HugrInternals for &T {
type Portgraph<'p> = T::Portgraph<'p> where Self: 'p;
delegate! {
to (**self) {
fn portgraph(&self) -> Self::Portgraph<'_>;
fn base_hugr(&self) -> &Hugr;
fn root_node(&self) -> Node;
}
}
}

impl<T: HugrInternals> HugrInternals for &mut T {
type Portgraph<'p> = T::Portgraph<'p> where Self: 'p;
delegate! {
to (**self) {
fn portgraph(&self) -> Self::Portgraph<'_>;
fn base_hugr(&self) -> &Hugr;
fn root_node(&self) -> Node;
}
}
}
/// Trait for accessing the mutable internals of a Hugr(Mut).
///
/// Specifically, this trait lets you apply arbitrary modifications that may
Expand Down
108 changes: 76 additions & 32 deletions hugr-core/src/hugr/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod tests;

pub use self::petgraph::PetgraphWrapper;
use self::render::RenderConfig;
use delegate::delegate;
pub use descendants::DescendantsGraph;
pub use root_checked::RootChecked;
pub use sibling::SiblingGraph;
Expand Down Expand Up @@ -513,41 +514,35 @@ impl ExtractHugr for &mut Hugr {
}
}

impl<T: AsRef<Hugr>> HugrView for T {
impl HugrView for Hugr {
#[inline]
fn contains_node(&self, node: Node) -> bool {
self.as_ref().graph.contains_node(node.pg_index())
self.graph.contains_node(node.pg_index())
}

#[inline]
fn node_count(&self) -> usize {
self.as_ref().graph.node_count()
self.graph.node_count()
}

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

#[inline]
fn nodes(&self) -> impl Iterator<Item = Node> + Clone {
self.as_ref().graph.nodes_iter().map_into()
self.graph.nodes_iter().map_into()
}

#[inline]
fn node_ports(&self, node: Node, dir: Direction) -> impl Iterator<Item = Port> + Clone {
self.as_ref()
.graph
.port_offsets(node.pg_index(), dir)
.map_into()
self.graph.port_offsets(node.pg_index(), dir).map_into()
}

#[inline]
fn all_node_ports(&self, node: Node) -> impl Iterator<Item = Port> + Clone {
self.as_ref()
.graph
.all_port_offsets(node.pg_index())
.map_into()
self.graph.all_port_offsets(node.pg_index()).map_into()
}

#[inline]
Expand All @@ -557,57 +552,106 @@ impl<T: AsRef<Hugr>> HugrView for T {
port: impl Into<Port>,
) -> impl Iterator<Item = (Node, Port)> + Clone {
let port = port.into();
let hugr = self.as_ref();
let port = hugr

let port = self
.graph
.port_index(node.pg_index(), port.pg_offset())
.unwrap();
hugr.graph.port_links(port).map(|(_, link)| {
self.graph.port_links(port).map(|(_, link)| {
let port = link.port();
let node = hugr.graph.port_node(port).unwrap();
let offset = hugr.graph.port_offset(port).unwrap();
let node = self.graph.port_node(port).unwrap();
let offset = self.graph.port_offset(port).unwrap();
(node.into(), offset.into())
})
}

#[inline]
fn node_connections(&self, node: Node, other: Node) -> impl Iterator<Item = [Port; 2]> + Clone {
let hugr = self.as_ref();

hugr.graph
self.graph
.get_connections(node.pg_index(), other.pg_index())
.map(|(p1, p2)| {
[p1, p2].map(|link| hugr.graph.port_offset(link.port()).unwrap().into())
[p1, p2].map(|link| self.graph.port_offset(link.port()).unwrap().into())
})
}

#[inline]
fn num_ports(&self, node: Node, dir: Direction) -> usize {
self.as_ref().graph.num_ports(node.pg_index(), dir)
self.graph.num_ports(node.pg_index(), dir)
}

#[inline]
fn children(&self, node: Node) -> impl DoubleEndedIterator<Item = Node> + Clone {
self.as_ref().hierarchy.children(node.pg_index()).map_into()
self.hierarchy.children(node.pg_index()).map_into()
}

#[inline]
fn neighbours(&self, node: Node, dir: Direction) -> impl Iterator<Item = Node> + Clone {
self.as_ref()
.graph
.neighbours(node.pg_index(), dir)
.map_into()
self.graph.neighbours(node.pg_index(), dir).map_into()
}

#[inline]
fn all_neighbours(&self, node: Node) -> impl Iterator<Item = Node> + Clone {
self.as_ref()
.graph
.all_neighbours(node.pg_index())
.map_into()
self.graph.all_neighbours(node.pg_index()).map_into()
}
}

macro_rules! hugr_view_methods {
() => {
delegate! {
to (**self) {

#[inline]
fn contains_node(&self, node: Node) -> bool;

#[inline]
fn node_count(&self) -> usize;

#[inline]
fn edge_count(&self) -> usize;

#[inline]
fn nodes(&self) -> impl Iterator<Item = Node> + Clone;

#[inline]
fn node_ports(&self, node: Node, dir: Direction) -> impl Iterator<Item = Port> + Clone;

#[inline]
fn all_node_ports(&self, node: Node) -> impl Iterator<Item = Port> + Clone;

#[inline]
fn linked_ports(
&self,
node: Node,
port: impl Into<Port>,
) -> impl Iterator<Item = (Node, Port)> + Clone;

#[inline]
fn node_connections(&self, node: Node, other: Node) -> impl Iterator<Item = [Port; 2]> + Clone;

#[inline]
fn num_ports(&self, node: Node, dir: Direction) -> usize;

#[inline]
fn children(&self, node: Node) -> impl DoubleEndedIterator<Item = Node> + Clone;

#[inline]
fn neighbours(&self, node: Node, dir: Direction) -> impl Iterator<Item = Node> + Clone;

#[inline]
fn all_neighbours(&self, node: Node) -> impl Iterator<Item = Node> + Clone;
}
}
}
}

impl<T: HugrView> HugrView for &T {
hugr_view_methods! {}
}

impl<T: HugrView> HugrView for &mut T {
hugr_view_methods! {}
}
aborgna-q marked this conversation as resolved.
Show resolved Hide resolved

/// Trait implementing methods on port iterators.
pub trait PortIterator<P>: Iterator<Item = (Node, P)>
where
Expand Down
65 changes: 62 additions & 3 deletions hugr-core/src/hugr/views/root_checked.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::marker::PhantomData;

use crate::hugr::internal::HugrMutInternals;
use delegate::delegate;
use portgraph::MultiPortGraph;

use crate::hugr::internal::{HugrInternals, HugrMutInternals};
use crate::hugr::{HugrError, HugrMut};
use crate::ops::handle::NodeHandle;
use crate::{Hugr, Node};
use crate::{Direction, Hugr, Node, Port};

use super::{check_tag, RootTagged};
use super::{check_tag, HugrView, RootTagged};

/// A view of the whole Hugr.
/// (Just provides static checking of the type of the root node)
Expand Down Expand Up @@ -45,6 +48,62 @@ impl<Root> RootChecked<&mut Hugr, Root> {
}
}

impl<H: AsRef<Hugr>, Root> HugrInternals for RootChecked<H, Root> {
type Portgraph<'p> = &'p MultiPortGraph where Self: 'p;
delegate! {
to self.as_ref() {
fn portgraph(&self) -> Self::Portgraph<'_>;
fn base_hugr(&self) -> &Hugr;
fn root_node(&self) -> Node;
}
}
}
impl<H: AsRef<Hugr>, Root> HugrView for RootChecked<H, Root> {
delegate! {
to self.as_ref() {
#[inline]
fn contains_node(&self, node: Node) -> bool;

#[inline]
fn node_count(&self) -> usize;

#[inline]
fn edge_count(&self) -> usize;

#[inline]
fn nodes(&self) -> impl Iterator<Item = Node> + Clone;

#[inline]
fn node_ports(&self, node: Node, dir: Direction) -> impl Iterator<Item = Port> + Clone;

#[inline]
fn all_node_ports(&self, node: Node) -> impl Iterator<Item = Port> + Clone;

#[inline]
fn linked_ports(
&self,
node: Node,
port: impl Into<Port>,
) -> impl Iterator<Item = (Node, Port)> + Clone;

#[inline]
fn node_connections(&self, node: Node, other: Node) -> impl Iterator<Item = [Port; 2]> + Clone;

#[inline]
fn num_ports(&self, node: Node, dir: Direction) -> usize;

#[inline]
fn children(&self, node: Node) -> impl DoubleEndedIterator<Item = Node> + Clone;

#[inline]
fn neighbours(&self, node: Node, dir: Direction) -> impl Iterator<Item = Node> + Clone;

#[inline]
fn all_neighbours(&self, node: Node) -> impl Iterator<Item = Node> + Clone;
}
}
aborgna-q marked this conversation as resolved.
Show resolved Hide resolved
}

impl<H: AsRef<Hugr>, Root: NodeHandle> RootTagged for RootChecked<H, Root> {
type RootHandle = Root;
}
Expand Down
Loading