From 55c2785c0e18e99834c7088d72fcfbd98a531426 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 27 Jun 2026 18:20:38 -0400 Subject: [PATCH] Add filtering on branches mid-iteration. --- .../depth_first_search/depth_first_search.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/python_experiments/depth_first_search/depth_first_search.py b/python_experiments/depth_first_search/depth_first_search.py index a3d7587..5ca7bef 100755 --- a/python_experiments/depth_first_search/depth_first_search.py +++ b/python_experiments/depth_first_search/depth_first_search.py @@ -25,6 +25,7 @@ def main(): activity_tree, lambda act: act.id in (2, 5, 10), lambda act: act.id in (4,), + lambda act: act.id in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), ) while (depth_and_node := match_iter.get_next()) is not None: @@ -61,17 +62,20 @@ class MatchIter: stack: deque[StackEntry] is_match: Callable[[Activity], bool] is_transparent: Callable[[Activity], bool] + is_alive: Callable[[Activity], bool] def __init__( self, activity_tree: ActivityTree, is_match: Callable[[Activity], bool], is_transparent: Callable[[Activity], bool], + is_alive: Callable[[Activity], bool], ) -> None: self.activity_tree = activity_tree self.stack = deque([StackEntry(0)]) self.is_match = is_match self.is_transparent = is_transparent + self.is_alive = is_alive def get_next(self) -> tuple[int, bool, bool, Activity] | None: if (next_announce := self.get_next_announce()) is not None: @@ -104,7 +108,9 @@ class MatchIter: stack_entry.transparent = self.is_transparent(node) children = [ - c for c in self.activity_tree.activities.values() if c.parent == node.id + c + for c in self.activity_tree.activities.values() + if c.parent == node.id and self.is_alive(c) ] # for c in children: for c in reversed(children):