Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
421217e66f063b6ec1458e3ffc2ec6ddb4066f51
[simgrid.git] / src / mc / explo / udpor / Unfolding.cpp
1 /* Copyright (c) 2008-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/mc/explo/udpor/Unfolding.hpp"
7
8 #include <stdexcept>
9
10 namespace simgrid::mc::udpor {
11
12 void Unfolding::mark_finished(const EventSet& events)
13 {
14   for (const auto e : events) {
15     mark_finished(e);
16   }
17 }
18
19 void Unfolding::mark_finished(const UnfoldingEvent* e)
20 {
21   if (e == nullptr) {
22     throw std::invalid_argument("Expected a non-null pointer to an event, but received NULL");
23   }
24   this->U.remove(e);
25   this->G.insert(e);
26 }
27
28 const UnfoldingEvent* Unfolding::insert(std::unique_ptr<UnfoldingEvent> e)
29 {
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.");
37   }
38
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
44     return *loc;
45   }
46
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.
49   //
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;
54     G.remove(e_equiv);
55     U.insert(e_equiv);
56     return e_equiv;
57   }
58
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);
63   return handle;
64 }
65
66 EventSet Unfolding::get_immediate_conflicts_of(const UnfoldingEvent* e) const
67 {
68   EventSet immediate_conflicts;
69   for (const auto event : U) {
70     if (event->immediately_conflicts_with(e)) {
71       immediate_conflicts.insert(event);
72     }
73   }
74   return immediate_conflicts;
75 }
76
77 } // namespace simgrid::mc::udpor