Skip to content

Commit

Permalink
feat: impl GraphRef for PetgraphWrapper
Browse files Browse the repository at this point in the history
Since PetgraphWrapper is a reference to a HugrView, we implement
GraphRef directly. This simplifies the implementation of several traits.
  • Loading branch information
doug-q committed Nov 7, 2023
1 parent 1a07cd9 commit 69a7950
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/hugr/views/petgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,28 @@ use petgraph::visit as pv;
/// Wrapper for a HugrView that implements petgraph's traits.
///
/// It can be used to apply petgraph's algorithms to a Hugr.
#[derive(Debug, Clone, Copy)]
#[derive(Debug)]
pub struct PetgraphWrapper<'a, T> {
pub(crate) hugr: &'a T,
}

impl<'a, T> Clone for PetgraphWrapper<'a, T> {
fn clone(&self) -> Self {
*self
}
}

impl<'a, T> Copy for PetgraphWrapper<'a, T> {}

impl<'a, T> From<&'a T> for PetgraphWrapper<'a, T>
where
T: HugrView,
{
fn from(hugr: &'a T) -> Self {
Self { hugr }
}
}

impl<'a, T> pv::GraphBase for PetgraphWrapper<'a, T>
where
T: HugrView,
Expand All @@ -32,6 +49,8 @@ where
type EdgeType = petgraph::Directed;
}

impl<'a, T> pv::GraphRef for PetgraphWrapper<'a, T> where T: HugrView {}

impl<'a, T> pv::NodeCount for PetgraphWrapper<'a, T>
where
T: HugrView,
Expand Down Expand Up @@ -75,12 +94,12 @@ where
type EdgeWeight = EdgeKind;
}

impl<'g, 'a, T> pv::IntoNodeReferences for &'g PetgraphWrapper<'a, T>
impl<'a, T> pv::IntoNodeReferences for PetgraphWrapper<'a, T>
where
T: HugrView,
{
type NodeRef = HugrNodeRef<'g>;
type NodeReferences = MapWithCtx<<T as HugrView>::Nodes<'g>, Self, HugrNodeRef<'g>>;
type NodeRef = HugrNodeRef<'a>;
type NodeReferences = MapWithCtx<<T as HugrView>::Nodes<'a>, Self, HugrNodeRef<'a>>;

fn node_references(self) -> Self::NodeReferences {
self.hugr
Expand All @@ -90,33 +109,33 @@ where
}
}

impl<'g, 'a, T> pv::IntoNodeIdentifiers for &'g PetgraphWrapper<'a, T>
impl<'a, T> pv::IntoNodeIdentifiers for PetgraphWrapper<'a, T>
where
T: HugrView,
{
type NodeIdentifiers = <T as HugrView>::Nodes<'g>;
type NodeIdentifiers = <T as HugrView>::Nodes<'a>;

fn node_identifiers(self) -> Self::NodeIdentifiers {
self.hugr.nodes()
}
}

impl<'g, 'a, T> pv::IntoNeighbors for &'g PetgraphWrapper<'a, T>
impl<'a, T> pv::IntoNeighbors for PetgraphWrapper<'a, T>
where
T: HugrView,
{
type Neighbors = <T as HugrView>::Neighbours<'g>;
type Neighbors = <T as HugrView>::Neighbours<'a>;

fn neighbors(self, n: Self::NodeId) -> Self::Neighbors {
self.hugr.output_neighbours(n)
}
}

impl<'g, 'a, T> pv::IntoNeighborsDirected for &'g PetgraphWrapper<'a, T>
impl<'a, T> pv::IntoNeighborsDirected for PetgraphWrapper<'a, T>
where
T: HugrView,
{
type NeighborsDirected = <T as HugrView>::Neighbours<'g>;
type NeighborsDirected = <T as HugrView>::Neighbours<'a>;

fn neighbors_directed(
self,
Expand Down

0 comments on commit 69a7950

Please sign in to comment.