Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Clean up maximum_subsets_iterator
[simgrid.git] / src / mc / explo / udpor / maximal_subsets_iterator.cpp
index a811401..6a57878 100644 (file)
@@ -8,9 +8,25 @@ namespace simgrid::mc::udpor {
 
 void maximal_subsets_iterator::increment()
 {
-  // Until we discover otherwise, we default to being done
-  auto next_event_ref = topological_ordering.end();
+  if (current_maximal_set = std::nullopt) {
+    return;
+  }
+
+  const auto next_event_ref = continue_traversal_of_maximal_events_tree();
+  if (next_event_ref == topological_ordering.end()) {
+    current_maximal_set = std::nullopt;
+    return;
+  }
 
+  // We found some other event `e'` which is not in causally related with anything
+  // that currently exists in `current_maximal_set`. Add it in
+  add_element_to_current_maximal_set(*next_event_ref);
+  backtrack_points.push(next_event_ref);
+}
+
+maximal_subsets_iterator::topological_order_position
+maximal_subsets_iterator::continue_traversal_of_maximal_events_tree()
+{
   while (not backtrack_points.empty()) {
     // This is an iterator which points to the latest event `e` that
     // was added to what is currently the maximal set
@@ -25,41 +41,42 @@ void maximal_subsets_iterator::increment()
     // will not change whether or not to now allow someone before `e`
     // in the ordering (otherwise, they would have to be in `e`'s history
     // and therefore would come after `e`)
-    next_event_ref = bookkeeper.find_next_event(latest_event_ref, topological_ordering.end());
-
-    // If we can't find another event to add after `e`,
-    // then we retry after first removing the latest event.
-    // This effectively tests "check now with all combinations that
-    // exclude the latest event".
-    //
-    // Note: it is important to remove the element FIRST before performing
-    // the second search, as removal may enable dependencies of `e` to be selected
-    if (next_event_ref == topological_ordering.end()) {
+    auto next_event_ref = bookkeeper.find_next_candidate_event(latest_event_ref, topological_ordering.end());
+
+    // If we found some event, we can stop
+    if (next_event_ref != topological_ordering.end() and should_consider_event(*next_event_ref)) {
+      return next_event_ref;
+    } else {
+      // Otherwise, if we can't find another event to add after `e` that
+      // we need to consider, we retry after first removing the latest event.
+      // This effectively tests "check now with all combinations that3
+      // exclude the latest event".
+      //
+      // Note: it is important to remove the element FIRST before performing
+      // the second search, as removal may enable dependencies of `e` to be selected
       remove_element_from_current_maximal_set(*latest_event_ref);
       backtrack_points.pop();
 
       // We begin the search AFTER the event we popped: we only want
       // to consider those events that could be added AFTER `e` and
       // not `e` itself again
-      next_event_ref = bookkeeper.find_next_event(latest_event_ref + 1, topological_ordering.end());
+      next_event_ref = bookkeeper.find_next_candidate_event(latest_event_ref + 1, topological_ordering.end());
 
-      // If we finally found some event, we can stop
-      if (next_event_ref != topological_ordering.end()) {
-        break;
+      // If we finally found some event AFTER removal, we can stop
+      if (next_event_ref != topological_ordering.end() and should_consider_event(*next_event_ref)) {
+        return next_event_ref;
       }
     }
   }
+  return topological_ordering.end();
+}
 
-  // If after all of the backtracking we still have no luck, we've finished
-  if (next_event_ref == topological_ordering.end()) {
-    return;
+bool maximal_subsets_iterator::should_consider_event(const UnfoldingEvent* e) const
+{
+  if (filter_function.has_value()) {
+    return filter_function.value()(e);
   }
-
-  // Otherwise we found some other event `e'` which is not in conflict with anything
-  // that currently exists in `current_maximal_set`. Add it in and perform
-  // some more bookkeeping
-  add_element_to_current_maximal_set(*next_event_ref);
-  backtrack_points.push(next_event_ref);
+  return true; // If nobody specified a filter, we default to considering the event
 }
 
 bool maximal_subsets_iterator::bookkeeper::is_candidate_event(const UnfoldingEvent* e) const
@@ -72,19 +89,25 @@ bool maximal_subsets_iterator::bookkeeper::is_candidate_event(const UnfoldingEve
 
 void maximal_subsets_iterator::add_element_to_current_maximal_set(const UnfoldingEvent* e)
 {
-  current_maximal_set.insert(e);
+  xbt_assert(current_maximal_set.has_value(), "Attempting to add an event to the maximal set "
+                                              "when iteration has completed. This indicates that "
+                                              "the termination condition for the iterator is broken");
+  current_maximal_set.value().insert(e);
   bookkeeper.mark_included_in_maximal_set(e);
 }
 
 void maximal_subsets_iterator::remove_element_from_current_maximal_set(const UnfoldingEvent* e)
 {
-  current_maximal_set.remove(e);
+  xbt_assert(current_maximal_set.has_value(), "Attempting to remove an event to the maximal set "
+                                              "when iteration has completed. This indicates that "
+                                              "the termination condition for the iterator is broken");
+  current_maximal_set.value().remove(e);
   bookkeeper.mark_removed_from_maximal_set(e);
 }
 
 maximal_subsets_iterator::topological_order_position
-maximal_subsets_iterator::bookkeeper::find_next_event(topological_order_position first,
-                                                      topological_order_position last) const
+maximal_subsets_iterator::bookkeeper::find_next_candidate_event(topological_order_position first,
+                                                                topological_order_position last) const
 {
   return std::find_if(first, last, [&](const UnfoldingEvent* e) { return is_candidate_event(e); });
 }