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 GraphRef for PetgraphWrapper #651

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Changes from all commits
Commits
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
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