1 /* Copyright (c) 2008-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "src/mc/explo/udpor/Unfolding.hpp"
10 namespace simgrid::mc::udpor {
12 void Unfolding::mark_finished(const EventSet& events)
14 for (const auto e : events) {
19 void Unfolding::mark_finished(const UnfoldingEvent* e)
22 throw std::invalid_argument("Expected a non-null pointer to an event, but received NULL");
28 const UnfoldingEvent* Unfolding::insert(std::unique_ptr<UnfoldingEvent> e)
30 const UnfoldingEvent* handle = e.get();
31 if (auto loc = this->global_events_.find(handle); loc != this->global_events_.end()) {
32 // This is bad: someone wrapped the raw event address twice
33 // in two different unique ptrs and attempted to
34 // insert it into the unfolding...
35 throw std::invalid_argument("Attempted to insert an unfolding event owned twice."
36 "This will result in a double free error and must be fixed.");
39 // Attempt to search first for an event in `U`. If it exists, we use that event
40 // instead of `e` since it is semantically equivalent to `e` (i.e. `e` is
41 // effectively already contained in the unfolding)
42 if (auto loc = std::find_if(U.begin(), U.end(), [=](const auto e_i) { return *e_i == *handle; }); loc != U.end()) {
43 // Return the handle to that event and ignore adding in a duplicate event
47 // Then look for `e` in `G`. It's possible `e` was already constructed
48 // in the past, in which case we can simply re-use it.
50 // Note, though, that in this case we must move the event in `G` into
51 // `U`: we've inserted `e` into the unfolding, so we expect it to be in `U`
52 if (auto loc = std::find_if(G.begin(), G.end(), [=](const auto e_i) { return *e_i == *handle; }); loc != G.end()) {
53 const auto e_equiv = *loc;
59 // Otherwise `e` is truly a "new" event
60 this->U.insert(handle);
61 this->event_handles.insert(handle);
62 this->global_events_[handle] = std::move(e);
66 EventSet Unfolding::get_immediate_conflicts_of(const UnfoldingEvent* e) const
68 EventSet immediate_conflicts;
69 for (const auto event : U) {
70 if (event->immediately_conflicts_with(e)) {
71 immediate_conflicts.insert(event);
74 return immediate_conflicts;
77 } // namespace simgrid::mc::udpor