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: More general portgraph references #494

Merged
merged 10 commits into from
Sep 6, 2023
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ path = "src/lib.rs"

[dependencies]
thiserror = "1.0.28"
portgraph = { version = "0.8.0", features = ["serde", "petgraph"] }
portgraph = { version = "0.8.0", features = ["serde", "petgraph"], git = "https://github.com/CQCL/portgraph", branch="feat/non-ref-wrappers" }
pyo3 = { version = "0.19.0", optional = true, features = [
"multiple-pymethods",
] }
Expand Down
3 changes: 2 additions & 1 deletion src/builder/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use super::{
BuildError, Container,
};

use crate::hugr::hugrmut::sealed::HugrMutInternals;
aborgna-q marked this conversation as resolved.
Show resolved Hide resolved
use crate::{
extension::ExtensionRegistry,
hugr::{hugrmut::sealed::HugrMutInternals, views::HugrView, ValidationError},
hugr::{views::HugrView, ValidationError},
ops,
types::{Type, TypeBound},
};
Expand Down
1 change: 0 additions & 1 deletion src/hugr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! The Hugr data structure, and its basic component handles.

pub mod hugrmut;

pub mod rewrite;
pub mod serialize;
pub mod validate;
Expand Down
10 changes: 6 additions & 4 deletions src/hugr/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,12 @@ pub(crate) mod sealed {
/// view.
pub trait HugrInternals {
/// The underlying portgraph view type.
type Portgraph: LinkView;
type Portgraph<'p>: LinkView + Clone + 'p
where
Self: 'p;

/// Returns a reference to the underlying portgraph.
fn portgraph(&self) -> &Self::Portgraph;
fn portgraph(&self) -> Self::Portgraph<'_>;

/// Returns the Hugr at the base of a chain of views.
fn base_hugr(&self) -> &Hugr;
Expand All @@ -398,10 +400,10 @@ pub(crate) mod sealed {
where
T: AsRef<super::Hugr>,
{
type Portgraph = MultiPortGraph;
type Portgraph<'p> = &'p MultiPortGraph where Self: 'p;

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

Expand Down
12 changes: 6 additions & 6 deletions src/hugr/views/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{hugr::NodeType, hugr::OpType, Direction, Hugr, Node, Port};

use super::{sealed::HugrInternals, HugrView, NodeMetadata};

type FlatRegionGraph<'g> = portgraph::view::FlatRegion<'g, MultiPortGraph>;
type FlatRegionGraph<'g> = portgraph::view::FlatRegion<'g, &'g MultiPortGraph>;

/// View of a HUGR sibling graph.
///
Expand Down Expand Up @@ -221,7 +221,7 @@ where
}
}

type RegionGraph<'g> = portgraph::view::Region<'g, MultiPortGraph>;
type RegionGraph<'g> = portgraph::view::Region<'g, &'g MultiPortGraph>;

/// View of a HUGR descendants graph.
///
Expand Down Expand Up @@ -483,10 +483,10 @@ where
Root: NodeHandle,
Base: HugrInternals,
{
type Portgraph = FlatRegionGraph<'g>;
type Portgraph<'p> = &'p FlatRegionGraph<'g> where Self: 'p;

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

Expand All @@ -506,10 +506,10 @@ where
Root: NodeHandle,
Base: HugrInternals,
{
type Portgraph = RegionGraph<'g>;
type Portgraph<'p> = &'p RegionGraph<'g> where Self: 'p;

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

Expand Down
12 changes: 6 additions & 6 deletions src/hugr/views/sibling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<'g, Base: HugrView> SiblingSubgraph<'g, Base> {
/// subgraph is empty.
pub fn try_from_dataflow_graph<Root>(dfg_graph: &'g Base) -> Result<Self, InvalidSubgraph>
where
Base: HugrView<RootHandle = Root>,
Base: Clone + HugrView<RootHandle = Root>,
Root: ContainerHandle<ChildrenHandle = DataflowOpID>,
{
let parent = dfg_graph.root();
Expand Down Expand Up @@ -147,7 +147,7 @@ impl<'g, Base: HugrView> SiblingSubgraph<'g, Base> {
outgoing: OutgoingPorts,
) -> Result<Self, InvalidSubgraph>
where
Base: HugrView,
Base: Clone + HugrView,
{
let mut checker = ConvexChecker::new(base);
Self::try_from_boundary_ports_with_checker(base, incoming, outgoing, &mut checker)
Expand All @@ -168,14 +168,14 @@ impl<'g, Base: HugrView> SiblingSubgraph<'g, Base> {
checker: &mut ConvexChecker<'g, Base>,
) -> Result<Self, InvalidSubgraph>
where
Base: HugrView,
Base: Clone + HugrView,
{
let pg = base.portgraph();
let to_pg = |(n, p): (Node, Port)| pg.port_index(n.index, p.offset).expect("invalid port");

// Ordering of the edges here is preserved and becomes ordering of the signature.
let subpg = Subgraph::new_subgraph(
pg,
pg.clone(),
inputs
.iter()
.flatten()
Expand Down Expand Up @@ -377,8 +377,8 @@ impl<'g, Base: HugrView> SiblingSubgraph<'g, Base> {
///
/// This can be used when constructing multiple sibling subgraphs to speed up
/// convexity checking.
pub struct ConvexChecker<'g, Base: HugrView>(
portgraph::algorithms::ConvexChecker<&'g Base::Portgraph>,
pub struct ConvexChecker<'g, Base: 'g + HugrView>(
portgraph::algorithms::ConvexChecker<Base::Portgraph<'g>>,
);

impl<'g, Base: HugrView> ConvexChecker<'g, Base> {
Expand Down