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: Implement petgraph traits on Hugr #498

Merged
merged 10 commits into from
Sep 7, 2023
13 changes: 8 additions & 5 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,9 +100,10 @@ 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 = SiblingGraph::new(self.hugr, parent);
let graph: PetgraphWrapper<SiblingGraph> = PetgraphWrapper::new(&region);
let entry_node = self.hugr.children(parent).next().unwrap();
dominators::simple_fast(&region, entry_node)
dominators::simple_fast(&graph, entry_node)
}

/// Check the constraints on a single node.
Expand Down Expand Up @@ -364,9 +366,10 @@ impl<'a, 'b> ValidationContext<'a, 'b> {
return Ok(());
};

let region: SiblingGraph = SiblingGraph::new(self.hugr, parent);
let postorder = Topo::new(&region);
let nodes_visited = postorder.iter(&region).filter(|n| *n != parent).count();
let region = SiblingGraph::new(self.hugr, parent);
let graph: PetgraphWrapper<SiblingGraph> = PetgraphWrapper::new(&region);
let postorder = Topo::new(&graph);
let nodes_visited = postorder.iter(&graph).filter(|n| *n != parent).count();
let node_count = self.hugr.children(parent).count();
if nodes_visited != node_count {
return Err(ValidationError::NotADag {
Expand Down
18 changes: 3 additions & 15 deletions src/hugr/views.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! Read-only access into HUGR graphs and subgraphs.

pub mod descendants;
mod petgraph;
pub mod petgraph;
pub mod sibling;
pub mod sibling_subgraph;

#[cfg(test)]
mod tests;

pub use self::petgraph::PetgraphWrapper;
pub use descendants::DescendantsGraph;
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