Skip to content

Commit

Permalink
feat: Add filter_edge_kind to PortIterator (#1593)
Browse files Browse the repository at this point in the history
I need to filter for `EdgeKind::Value(_)` (and possibly
`EdgeKind::Const(_)`) so thought of making a more general filter
available.
  • Loading branch information
lmondada authored Oct 18, 2024
1 parent 0f6c25a commit 6097391
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions hugr-core/src/hugr/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,11 +680,21 @@ where
/// Filter an iterator of node-ports to only dataflow dependency specifying
/// ports (Value and StateOrder)
fn dataflow_ports_only(self, hugr: &impl HugrView) -> impl Iterator<Item = (Node, P)> {
self.filter_edge_kind(
|kind| matches!(kind, Some(EdgeKind::Value(..) | EdgeKind::StateOrder)),
hugr,
)
}

/// Filter an iterator of node-ports based on the port kind.
fn filter_edge_kind(
self,
predicate: impl Fn(Option<EdgeKind>) -> bool,
hugr: &impl HugrView,
) -> impl Iterator<Item = (Node, P)> {
self.filter(move |(n, p)| {
matches!(
hugr.get_optype(*n).port_kind(*p),
Some(EdgeKind::Value(_) | EdgeKind::StateOrder)
)
let kind = hugr.get_optype(*n).port_kind(*p);
predicate(kind)
})
}
}
Expand Down

0 comments on commit 6097391

Please sign in to comment.