Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
86da9d5ff0aa0241431b56456d06e851267e6a48
[simgrid.git] / src / mc / udpor_global.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 "udpor_global.hpp"
7 #include "xbt/log.h"
8
9 #include <algorithm>
10 #include <iterator>
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_udpor_global, mc, "udpor_global");
13
14 namespace simgrid::mc::udpor {
15
16 void EventSet::remove(UnfoldingEvent* e)
17 {
18   this->events_.erase(e);
19 }
20
21 void EventSet::subtract(const EventSet& other)
22 {
23   this->events_ = std::move(subtracting(other).events_);
24 }
25
26 // void EventSet::subtract(const Configuration& other);
27
28 EventSet EventSet::subtracting(const EventSet& other) const
29 {
30   std::set<UnfoldingEvent*> result;
31   std::set_difference(this->events_.begin(), this->events_.end(), other.events_.begin(), other.events_.end(),
32                       std::inserter(result, result.end()));
33   return EventSet(std::move(result));
34 }
35
36 EventSet EventSet::subtracting(UnfoldingEvent* e) const
37 {
38   auto result = this->events_;
39   result.erase(e);
40   return EventSet(std::move(result));
41 }
42 // EventSet EventSet::subtracting(const Configuration* e) const;
43
44 void EventSet::insert(UnfoldingEvent* e)
45 {
46   // TODO: Potentially react if the event is already inserted
47   this->events_.insert(e);
48 }
49
50 void EventSet::form_union(const EventSet& other)
51 {
52   this->events_ = std::move(make_union(other).events_);
53 }
54
55 // void EventSet::form_union(const Configuration&);
56 EventSet EventSet::make_union(UnfoldingEvent* e) const
57 {
58   auto result = this->events_;
59   result.insert(e);
60   return EventSet(std::move(result));
61 }
62
63 EventSet EventSet::make_union(const EventSet& other) const
64 {
65   std::set<UnfoldingEvent*> result;
66   std::set_union(this->events_.begin(), this->events_.end(), other.events_.begin(), other.events_.end(),
67                  std::inserter(result, result.end()));
68   return EventSet(std::move(result));
69 }
70
71 // EventSet EventSet::make_union(const Configuration& e) const;
72
73 size_t EventSet::size() const
74 {
75   return this->events_.size();
76 }
77
78 bool EventSet::empty() const
79 {
80   return this->events_.empty();
81 }
82
83 bool EventSet::contains(UnfoldingEvent* e) const
84 {
85   return this->events_.find(e) != this->events_.end();
86 }
87
88 bool EventSet::is_subset_of(const EventSet& other) const
89 {
90   // If there is some element not contained in `other`, then
91   // the set difference will contain that element and the
92   // result won't be empty
93   return subtracting(other).empty();
94 }
95
96 void Configuration::add_event(UnfoldingEvent* e)
97 {
98   this->events_.insert(e);
99   this->newest_event = e;
100
101   // TODO: Re-compute the maxmimal events
102 }
103
104 UnfoldingEvent::UnfoldingEvent(unsigned int nb_events, std::string const& trans_tag, EventSet const& immediate_causes)
105     : UnfoldingEvent(nb_events, trans_tag, immediate_causes, 0)
106 {
107   // TODO: Implement this correctly
108 }
109
110 UnfoldingEvent::UnfoldingEvent(unsigned int nb_events, std::string const& trans_tag, EventSet const& immediate_causes,
111                                StateHandle sid)
112 {
113   // TODO: Implement this
114 }
115
116 StateManager::Handle StateManager::record_state(std::unique_ptr<State> state)
117 {
118   if (state.get() == nullptr) {
119     throw std::invalid_argument("Expected a newly-allocated State but got NULL instead");
120   }
121
122   const auto integer_handle = this->current_handle_;
123   this->state_map_.insert({integer_handle, std::move(state)});
124
125   // TODO: Check for state handle overflow!
126   this->current_handle_++;
127   return integer_handle;
128 }
129
130 std::optional<std::reference_wrapper<State>> StateManager::get_state(StateManager::Handle handle)
131 {
132   auto state = this->state_map_.find(handle);
133   if (state == this->state_map_.end()) {
134     return std::nullopt;
135   }
136   auto& state_ref = *state->second.get();
137   return std::optional<std::reference_wrapper<State>>{state_ref};
138 }
139
140 } // namespace simgrid::mc::udpor