Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'udpor-phase6' into 'master'
[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::remove(const EventSet& events)
13 {
14   for (const auto e : events) {
15     remove(e);
16   }
17 }
18
19 void Unfolding::remove(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->global_events_.erase(e);
25   this->event_handles.remove(e);
26 }
27
28 void 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   // Map the handle to its owner
40   this->event_handles.insert(handle);
41   this->global_events_[handle] = std::move(e);
42 }
43
44 bool Unfolding::contains_event_equivalent_to(const UnfoldingEvent* e) const
45 {
46   // Notice the use of `==` equality here. `e` may not be contained in the
47   // unfolding; but some event which is "equivalent" to it could be.
48   for (const auto event : *this) {
49     if (*event == *e) {
50       return true;
51     }
52   }
53   return false;
54 }
55
56 EventSet Unfolding::get_immediate_conflicts_of(const UnfoldingEvent* e) const
57 {
58   EventSet immediate_conflicts;
59   for (const auto event : *this) {
60     if (event->immediately_conflicts_with(e)) {
61       immediate_conflicts.insert(e);
62     }
63   }
64   return immediate_conflicts;
65 }
66
67 } // namespace simgrid::mc::udpor