Skip to content

Commit

Permalink
Remove Base type-parameter to SiblingGraph/DescendantsGraph, call bas…
Browse files Browse the repository at this point in the history
…e_hugr in c'tor
  • Loading branch information
acl-cqc committed Sep 12, 2023
1 parent 1a93ad8 commit d74ccf7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 49 deletions.
5 changes: 1 addition & 4 deletions src/hugr/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,8 @@ pub trait HugrView: sealed::HugrInternals {

/// A common trait for views of a HUGR hierarchical subgraph.
pub trait HierarchyView<'a>: HugrView {
/// The base from which the subgraph is derived.
type Base;

/// Create a hierarchical view of a HUGR given a root node.
fn new(hugr: &'a Self::Base, root: Node) -> Self;
fn new(hugr: &'a impl HugrView, root: Node) -> Self;
}

impl<T> HugrView for T
Expand Down
32 changes: 10 additions & 22 deletions src/hugr/views/descendants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,32 @@ type RegionGraph<'g> = portgraph::view::Region<'g, &'g MultiPortGraph>;
/// used interchangeably with [`SiblingGraph`].
///
/// [`SiblingGraph`]: super::SiblingGraph
pub struct DescendantsGraph<'g, Root = Node, Base = Hugr>
where
Base: HugrInternals,
{
pub struct DescendantsGraph<'g, Root = Node> {
/// The chosen root node.
root: Node,

/// The graph encoding the adjacency structure of the HUGR.
graph: RegionGraph<'g>,

/// The node hierarchy.
hugr: &'g Base,
hugr: &'g Hugr,

/// The operation handle of the root node.
_phantom: std::marker::PhantomData<Root>,
}

impl<'g, Root, Base: Clone> Clone for DescendantsGraph<'g, Root, Base>
impl<'g, Root> Clone for DescendantsGraph<'g, Root>
where
Root: NodeHandle,
Base: HugrInternals + HugrView,
{
fn clone(&self) -> Self {
DescendantsGraph::new(self.hugr, self.root)
}
}

impl<'g, Root, Base> HugrView for DescendantsGraph<'g, Root, Base>
impl<'g, Root> HugrView for DescendantsGraph<'g, Root>
where
Root: NodeHandle,
Base: HugrInternals + HugrView,
{
type RootHandle = Root;

Expand Down Expand Up @@ -173,36 +168,29 @@ where
}
}

impl<'a, Root, Base> HierarchyView<'a> for DescendantsGraph<'a, Root, Base>
impl<'a, Root> HierarchyView<'a> for DescendantsGraph<'a, Root>
where
Root: NodeHandle,
Base: HugrView,
{
type Base = Base;

fn new(hugr: &'a Base, root: Node) -> Self {
fn new(hugr: &'a impl HugrView, root: Node) -> Self {
let root_tag = hugr.get_optype(root).tag();
if !Root::TAG.is_superset(root_tag) {
// TODO: Return an error
panic!("Root node must have the correct operation type tag.")
}
let hugr = hugr.base_hugr();
Self {
root,
graph: RegionGraph::new_region(
&hugr.base_hugr().graph,
&hugr.base_hugr().hierarchy,
root.index,
),
graph: RegionGraph::new_region(&hugr.graph, &hugr.hierarchy, root.index),
hugr,
_phantom: std::marker::PhantomData,
}
}
}

impl<'g, Root, Base> super::sealed::HugrInternals for DescendantsGraph<'g, Root, Base>
impl<'g, Root> super::sealed::HugrInternals for DescendantsGraph<'g, Root>
where
Root: NodeHandle,
Base: HugrInternals,
{
type Portgraph<'p> = &'p RegionGraph<'g> where Self: 'p;

Expand All @@ -213,7 +201,7 @@ where

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

#[inline]
Expand Down
34 changes: 11 additions & 23 deletions src/hugr/views/sibling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,32 @@ type FlatRegionGraph<'g> = portgraph::view::FlatRegion<'g, &'g MultiPortGraph>;
/// used interchangeably with [`DescendantsGraph`].
///
/// [`DescendantsGraph`]: super::DescendantsGraph
pub struct SiblingGraph<'g, Root = Node, Base = Hugr>
where
Base: HugrInternals,
{
pub struct SiblingGraph<'g, Root = Node> {
/// The chosen root node.
root: Node,

/// The filtered portgraph encoding the adjacency structure of the HUGR.
graph: FlatRegionGraph<'g>,

/// The rest of the HUGR.
hugr: &'g Base,
/// View onto the underlying Hugr which this graph filters
hugr: &'g Hugr,

/// The operation type of the root node.
_phantom: std::marker::PhantomData<Root>,
}

impl<'g, Root, Base> Clone for SiblingGraph<'g, Root, Base>
impl<'g, Root> Clone for SiblingGraph<'g, Root>
where
Root: NodeHandle,
Base: HugrInternals + HugrView,
{
fn clone(&self) -> Self {
SiblingGraph::new(self.hugr, self.root)
}
}

impl<'g, Root, Base> HugrView for SiblingGraph<'g, Root, Base>
impl<'g, Root> HugrView for SiblingGraph<'g, Root>
where
Root: NodeHandle,
Base: HugrInternals + HugrView,
{
type RootHandle = Root;

Expand Down Expand Up @@ -186,36 +181,29 @@ where
}
}

impl<'a, Root, Base> HierarchyView<'a> for SiblingGraph<'a, Root, Base>
impl<'a, Root> HierarchyView<'a> for SiblingGraph<'a, Root>
where
Root: NodeHandle,
Base: HugrView,
{
type Base = Base;

fn new(hugr: &'a Base, root: Node) -> Self {
fn new(hugr: &'a impl HugrView, root: Node) -> Self {
let root_tag = hugr.get_optype(root).tag();
if !Root::TAG.is_superset(root_tag) {
// TODO: Return an error
panic!("Root node must have the correct operation type tag.")
}
let hugr = hugr.base_hugr();
Self {
root,
graph: FlatRegionGraph::new_flat_region(
&hugr.base_hugr().graph,
&hugr.base_hugr().hierarchy,
root.index,
),
graph: FlatRegionGraph::new_flat_region(&hugr.graph, &hugr.hierarchy, root.index),
hugr,
_phantom: std::marker::PhantomData,
}
}
}

impl<'g, Root, Base> HugrInternals for SiblingGraph<'g, Root, Base>
impl<'g, Root> HugrInternals for SiblingGraph<'g, Root>
where
Root: NodeHandle,
Base: HugrInternals,
{
type Portgraph<'p> = &'p FlatRegionGraph<'g> where Self: 'p;

Expand All @@ -226,7 +214,7 @@ where

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

#[inline]
Expand Down

0 comments on commit d74ccf7

Please sign in to comment.