Skip to content

Commit

Permalink
Implement petgraph only on a wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Sep 7, 2023
1 parent b883758 commit 46b53d3
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 175 deletions.
7 changes: 5 additions & 2 deletions src/hugr/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::ops::{OpTag, OpTrait, OpType, ValidateOp};
use crate::types::{EdgeKind, Type};
use crate::{Direction, Hugr, Node, Port};

use super::views::petgraph::PetgraphWrapper;
use super::views::{HierarchyView, HugrView, SiblingGraph};
use super::NodeType;

Expand Down Expand Up @@ -99,7 +100,8 @@ impl<'a, 'b> ValidationContext<'a, 'b> {
/// The results of this computation should be cached in `self.dominators`.
/// We don't do it here to avoid mutable borrows.
fn compute_dominator(&self, parent: Node) -> Dominators<Node> {
let region: SiblingGraph = SiblingGraph::new(self.hugr, parent);
let region: PetgraphWrapper<SiblingGraph> =
PetgraphWrapper::new(SiblingGraph::new(self.hugr, parent));
let entry_node = self.hugr.children(parent).next().unwrap();
dominators::simple_fast(&region, entry_node)
}
Expand Down Expand Up @@ -364,7 +366,8 @@ impl<'a, 'b> ValidationContext<'a, 'b> {
return Ok(());
};

let region: SiblingGraph = SiblingGraph::new(self.hugr, parent);
let region: PetgraphWrapper<SiblingGraph> =
PetgraphWrapper::new(SiblingGraph::new(self.hugr, parent));
let postorder = Topo::new(&region);
let nodes_visited = postorder.iter(&region).filter(|n| *n != parent).count();
let node_count = self.hugr.children(parent).count();
Expand Down
16 changes: 2 additions & 14 deletions src/hugr/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod sibling_subgraph;
mod tests;

pub use descendants::DescendantsGraph;
pub use petgraph::PetgraphWrapper;
pub use sibling::SiblingGraph;
pub use sibling_subgraph::SiblingSubgraph;

Expand All @@ -22,7 +23,6 @@ use crate::ops::handle::NodeHandle;
use crate::ops::{FuncDecl, FuncDefn, OpName, OpTag, OpType, DFG};
use crate::types::{EdgeKind, FunctionType};
use crate::{Direction, Node, Port};
use ::petgraph::visit as pv;

/// A trait for inspecting HUGRs.
/// For end users we intend this to be superseded by region-specific APIs.
Expand Down Expand Up @@ -232,19 +232,7 @@ pub trait HugrView: sealed::HugrInternals {
}

/// A common trait for views of a HUGR hierarchical subgraph.
pub trait HierarchyView<'a>:
HugrView
+ pv::GraphBase<NodeId = Node>
+ pv::GraphProp
+ pv::NodeCount
+ pv::NodeIndexable
+ pv::EdgeCount
+ pv::Visitable
+ pv::GetAdjacencyMatrix
+ pv::Visitable
where
for<'g> &'g Self: pv::IntoNeighborsDirected + pv::IntoNodeIdentifiers,
{
pub trait HierarchyView<'a>: HugrView {
/// The base from which the subgraph is derived.
type Base;

Expand Down
4 changes: 2 additions & 2 deletions src/hugr/views/descendants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type RegionGraph<'g> = portgraph::view::Region<'g, &'g MultiPortGraph>;
/// its immediate children. Prefer using [`SiblingGraph`] when possible,
/// as it is more efficient.
///
/// Implements the [`HierarchyView`] trait, as well as [`HugrView`] and petgraph's
/// _visit_ traits, so can be used interchangeably with [`SiblingGraph`].
/// Implements the [`HierarchyView`] trait, as well as [`HugrView`], it can be
/// used interchangeably with [`SiblingGraph`].
///
/// [`SiblingGraph`]: super::SiblingGraph
pub struct DescendantsGraph<'g, Root = Node, Base = Hugr>
Expand Down
Loading

0 comments on commit 46b53d3

Please sign in to comment.