From 7675b99e346f4f928c2644427aa4532d97fa6566 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 5 Sep 2025 21:13:43 +0200 Subject: [PATCH] Adjust reachability (#3250) Include blocking variables in the returned set of reachable variables. This is needed in https://github.com/powdr-labs/powdr/pull/3046 --- constraint-solver/src/reachability.rs | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/constraint-solver/src/reachability.rs b/constraint-solver/src/reachability.rs index d5efa10b0..c68dc5542 100644 --- a/constraint-solver/src/reachability.rs +++ b/constraint-solver/src/reachability.rs @@ -23,9 +23,10 @@ where /// Returns the set of all variables reachable from an initial set via shared constraints /// (algebraic constraints and bus interactions). -/// The set of blocking variables is a barrier that stops the reachability search. -/// The returned set does not contain the blocking variables but it does contain -/// the initial variables. +/// The set of blocking variables is a barrier that stops the reachability search, in the +/// sense that we consider constraints that can also contain blocking variables, but we +/// only continue the search from the non-blocking variables in constraints. +/// The returned set contains reachable blocking variables and the initial variables. pub fn reachable_variables_except_blocked( initial_variables: impl IntoIterator, blocking_variables: impl IntoIterator, @@ -37,11 +38,6 @@ where { let mut reachable_variables = initial_variables.into_iter().collect::>(); let blocking_variables = blocking_variables.into_iter().collect::>(); - // We just remove variables, order does not matter. - #[allow(clippy::iter_over_hash_type)] - for v in &blocking_variables { - reachable_variables.remove(v); - } loop { let size_before = reachable_variables.len(); @@ -51,16 +47,11 @@ where { if constraint .referenced_variables() - .any(|var| reachable_variables.contains(var)) + .any(|var| reachable_variables.contains(var) && !blocking_variables.contains(var)) { - // This constraint is connected to a reachable variable. - // Add all variables of this constraint except the blocking ones. - reachable_variables.extend( - constraint - .referenced_variables() - .filter(|&var| !blocking_variables.contains(var)) - .cloned(), - ); + // This constraint is connected to a reachable variable, + // add all variables of this constraint. + reachable_variables.extend(constraint.referenced_variables().cloned()); } } if reachable_variables.len() == size_before {