@@ -24,19 +24,37 @@ pub trait HugrInternals {
2424    where 
2525        Self :  ' p ; 
2626
27+     /// The portgraph graph structure returned by [`HugrInternals::region_portgraph`]. 
28+ type  RegionPortgraph < ' p > :  LinkView < LinkEndpoint :  Eq >  + Clone  + ' p 
29+     where 
30+         Self :  ' p ; 
31+ 
2732    /// The type of nodes in the Hugr. 
2833type  Node :  Copy  + Ord  + std:: fmt:: Debug  + std:: fmt:: Display  + std:: hash:: Hash ; 
2934
35+     /// A mapping between HUGR nodes and portgraph nodes in the graph returned by 
36+ /// [`HugrInternals::region_portgraph`]. 
37+ type  RegionPortgraphNodes :  PortgraphNodeMap < Self :: Node > ; 
38+ 
3039    /// Returns a reference to the underlying portgraph. 
3140fn  portgraph ( & self )  -> Self :: Portgraph < ' _ > ; 
3241
33-     /// Returns a flat portgraph view of a region in the HUGR. 
34- /// 
35- /// This is a subgraph of [`HugrInternals::portgraph`], with a flat hierarchy. 
42+     /// Returns a flat portgraph view of a region in the HUGR, and a mapping between 
43+ /// HUGR nodes and portgraph nodes in the graph. 
44+ // 
45+     // NOTE: Ideally here we would just return `Self::RegionPortgraph<'_>`, but 
46+     // when doing so we are unable to restrict the type to implement petgraph's 
47+     // traits over references (e.g. `&MyGraph : IntoNodeIdentifiers`, which is 
48+     // needed if we want to use petgraph's algorithms on the region graph). 
49+     // This won't be solvable until we don't do the big petgraph refactor -.- 
50+     // In the meantime, just wrap the portgraph in a `FlatRegion` as needed. 
3651    fn  region_portgraph ( 
3752        & self , 
3853        parent :  Self :: Node , 
39-     )  -> portgraph:: view:: FlatRegion < ' _ ,  impl  LinkView < LinkEndpoint :  Eq >  + Clone  + ' _ > ; 
54+     )  -> ( 
55+         portgraph:: view:: FlatRegion < ' _ ,  Self :: RegionPortgraph < ' _ > > , 
56+         Self :: RegionPortgraphNodes , 
57+     ) ; 
4058
4159    /// Returns the portgraph [Hierarchy](portgraph::Hierarchy) of the graph 
4260/// returned by [`HugrInternals::portgraph`]. 
@@ -65,14 +83,56 @@ pub trait HugrInternals {
6583    fn  base_hugr ( & self )  -> & Hugr ; 
6684} 
6785
86+ /// A map between hugr nodes and portgraph nodes in the graph returned by 
87+ /// [`HugrInternals::region_portgraph`]. 
88+ pub  trait  PortgraphNodeMap < N > :  Clone  + Sized  + std:: fmt:: Debug  { 
89+     /// Returns the portgraph index of a HUGR node in the associated region 
90+ /// graph. 
91+ /// 
92+ /// If the node is not in the region, the result is undefined. 
93+ fn  to_portgraph ( & self ,  node :  N )  -> portgraph:: NodeIndex ; 
94+ 
95+     /// Returns the HUGR node for a portgraph node in the associated region 
96+ /// graph. 
97+ /// 
98+ /// If the node is not in the region, the result is undefined. 
99+ #[ allow( clippy:: wrong_self_convention) ]  
100+     fn  from_portgraph ( & self ,  node :  portgraph:: NodeIndex )  -> N ; 
101+ } 
102+ 
103+ /// An identity map between HUGR nodes and portgraph nodes. 
104+ #[ derive(  
105+     Copy ,  Clone ,  Debug ,  Default ,  Eq ,  PartialEq ,  Hash ,  PartialOrd ,  Ord ,  derive_more:: Display ,  
106+ ) ] 
107+ pub  struct  DefaultPGNodeMap ; 
108+ 
109+ impl  PortgraphNodeMap < Node >  for  DefaultPGNodeMap  { 
110+     #[ inline]  
111+     fn  to_portgraph ( & self ,  node :  Node )  -> portgraph:: NodeIndex  { 
112+         node. into_portgraph ( ) 
113+     } 
114+ 
115+     #[ inline]  
116+     fn  from_portgraph ( & self ,  node :  portgraph:: NodeIndex )  -> Node  { 
117+         node. into ( ) 
118+     } 
119+ } 
120+ 
68121impl  HugrInternals  for  Hugr  { 
69122    type  Portgraph < ' p > 
70123        = & ' p  MultiPortGraph 
71124    where 
72125        Self :  ' p ; 
73126
127+     type  RegionPortgraph < ' p > 
128+         = & ' p  MultiPortGraph 
129+     where 
130+         Self :  ' p ; 
131+ 
74132    type  Node  = Node ; 
75133
134+     type  RegionPortgraphNodes  = DefaultPGNodeMap ; 
135+ 
76136    #[ inline]  
77137    fn  portgraph ( & self )  -> Self :: Portgraph < ' _ >  { 
78138        & self . graph 
@@ -82,10 +142,14 @@ impl HugrInternals for Hugr {
82142    fn  region_portgraph ( 
83143        & self , 
84144        parent :  Self :: Node , 
85-     )  -> portgraph:: view:: FlatRegion < ' _ ,  impl  LinkView < LinkEndpoint :  Eq >  + Clone  + ' _ >  { 
145+     )  -> ( 
146+         portgraph:: view:: FlatRegion < ' _ ,  Self :: RegionPortgraph < ' _ > > , 
147+         Self :: RegionPortgraphNodes , 
148+     )  { 
86149        let  pg = self . portgraph ( ) ; 
87150        let  root = self . to_portgraph_node ( parent) ; 
88-         portgraph:: view:: FlatRegion :: new_without_root ( pg,  & self . hierarchy ,  root) 
151+         let  region = portgraph:: view:: FlatRegion :: new_without_root ( pg,  & self . hierarchy ,  root) ; 
152+         ( region,  DefaultPGNodeMap ) 
89153    } 
90154
91155    #[ inline]  
0 commit comments