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
This commit is contained in:
chriseth
2025-09-05 21:13:43 +02:00
committed by GitHub
parent f95c0087c4
commit 7675b99e34

View File

@@ -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<T, V>(
initial_variables: impl IntoIterator<Item = V>,
blocking_variables: impl IntoIterator<Item = V>,
@@ -37,11 +38,6 @@ where
{
let mut reachable_variables = initial_variables.into_iter().collect::<HashSet<_>>();
let blocking_variables = blocking_variables.into_iter().collect::<HashSet<_>>();
// 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 {