From 609739196455255060acd279bead410f1e8d7a34 Mon Sep 17 00:00:00 2001 From: Luca Mondada <72734770+lmondada@users.noreply.github.com> Date: Fri, 18 Oct 2024 11:54:35 +0100 Subject: [PATCH] feat: Add filter_edge_kind to PortIterator (#1593) I need to filter for `EdgeKind::Value(_)` (and possibly `EdgeKind::Const(_)`) so thought of making a more general filter available. --- hugr-core/src/hugr/views.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/hugr-core/src/hugr/views.rs b/hugr-core/src/hugr/views.rs index 5779070fa..d17eaf44f 100644 --- a/hugr-core/src/hugr/views.rs +++ b/hugr-core/src/hugr/views.rs @@ -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 { + 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) -> bool, + hugr: &impl HugrView, + ) -> impl Iterator { 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) }) } }