Skip to content

Commit

Permalink
feat: More general portgraph references (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Sep 6, 2023
1 parent abfaba6 commit 0a796a3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
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.9.0", features = ["serde", "petgraph"] }
pyo3 = { version = "0.19.0", optional = true, features = [
"multiple-pymethods",
] }
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

0 comments on commit 0a796a3

Please sign in to comment.