Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Functions having rvalue reference arguments should "std::move" those arguments (Sonar).
[simgrid.git] / src / mc / explo / udpor / EventSet.hpp
1 /* Copyright (c) 2007-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 #ifndef SIMGRID_MC_UDPOR_EVENT_SET_HPP
7 #define SIMGRID_MC_UDPOR_EVENT_SET_HPP
8
9 #include "src/mc/explo/udpor/udpor_forward.hpp"
10
11 #include <algorithm>
12 #include <cstddef>
13 #include <initializer_list>
14 #include <unordered_set>
15 #include <vector>
16 #include <xbt/asserts.h>
17
18 namespace simgrid::mc::udpor {
19
20 class EventSet {
21 private:
22   std::unordered_set<const UnfoldingEvent*> events_;
23
24 public:
25   EventSet()                           = default;
26   EventSet(const EventSet&)            = default;
27   EventSet& operator=(const EventSet&) = default;
28   EventSet& operator=(EventSet&&)      = default;
29   EventSet(EventSet&&)                 = default;
30   explicit EventSet(const Configuration& config);
31   explicit EventSet(const std::vector<const UnfoldingEvent*>& raw_events)
32       : events_(raw_events.begin(), raw_events.end())
33   {
34   }
35   explicit EventSet(std::unordered_set<const UnfoldingEvent*>&& raw_events) : events_(std::move(raw_events)) {}
36   explicit EventSet(std::initializer_list<const UnfoldingEvent*> event_list) : events_(std::move(event_list)) {}
37
38   auto begin() const { return this->events_.begin(); }
39   auto end() const { return this->events_.end(); }
40   auto cbegin() const { return this->events_.cbegin(); }
41   auto cend() const { return this->events_.cend(); }
42
43   void remove(const UnfoldingEvent*);
44   void subtract(const EventSet&);
45   void subtract(const Configuration&);
46   EventSet subtracting(const UnfoldingEvent*) const;
47   EventSet subtracting(const EventSet&) const;
48   EventSet subtracting(const Configuration&) const;
49
50   void insert(const UnfoldingEvent*);
51   void form_union(const EventSet&);
52   void form_union(const Configuration&);
53   EventSet make_union(const UnfoldingEvent*) const;
54   EventSet make_union(const EventSet&) const;
55   EventSet make_union(const Configuration&) const;
56   EventSet make_intersection(const EventSet&) const;
57   EventSet get_local_config() const;
58
59   size_t size() const;
60   bool empty() const;
61
62   bool contains(const UnfoldingEvent*) const;
63   bool contains(const History&) const;
64   bool contains_equivalent_to(const UnfoldingEvent*) const;
65   bool intersects(const EventSet&) const;
66   bool intersects(const History&) const;
67   bool is_subset_of(const EventSet&) const;
68
69   bool operator==(const EventSet& other) const { return this->events_ == other.events_; }
70   bool operator!=(const EventSet& other) const { return this->events_ != other.events_; }
71   std::string to_string() const;
72
73   /**
74    * @brief Whether or not this set of events could
75    * represent a configuration
76    */
77   bool is_valid_configuration() const;
78
79   /**
80    * @brief Whether or not this set of events is
81    * a *maximal event set*, i.e. whether each element
82    * of the set causes none of the others
83    *
84    * A set of events `E` is said to be _maximal_ if
85    * it is causally-free. Formally,
86    *
87    * 1. For each event `e` in `E`, there is no event
88    * `e'` in `E` such that `e < e'`
89    */
90   bool is_maximal() const;
91
92   /**
93    * @brief Whether or not this set of events is
94    * free of conflicts
95    *
96    * A set of events `E` is said to be _conflict free_
97    * if
98    *
99    * 1. For each event `e` in `E`, there is no event
100    * `e'` in `E` such that `e # e'` where `#` is the
101    * conflict relation over the unfolding from
102    * which the events `E` are derived
103    *
104    * @note: This method makes use only of the causality
105    * tree of the events in the set; i.e. it determines conflicts
106    * based solely on the unfolding and the definition of
107    * conflict in an unfolding. Some clever techniques
108    * exist for computing conflicts with specialized transition
109    * types (only mutexes if I remember correctly) that was
110    * referenced in The Anh Pham's thesis. This would require
111    * keeping track of information *outside* of any given
112    * set and probably doesn't work for all types of transitions
113    * anyway.
114    */
115   bool is_conflict_free() const;
116
117   /**
118    * @brief Produces the largest subset of this
119    * set of events which is maximal
120    */
121   EventSet get_largest_maximal_subset() const;
122
123   /**
124    * @brief Orders the events of the set such that
125    * "more recent" events (i.e. those that are farther down in
126    * the event structure's dependency chain) come after those
127    * that appeared "farther in the past"
128    *
129    * @returns a vector `V` with the following property:
130    *
131    * 1. Let i(e) := C -> I map events to their indices in `V`.
132    * For every pair of events e, e' in C, if e < e' then i(e) < i(e')
133    *
134    * Intuitively, events that are closer to the "bottom" of the event
135    * structure appear farther along in the list than those that appear
136    * closer to the "top"
137    */
138   std::vector<const UnfoldingEvent*> get_topological_ordering() const;
139
140   /**
141    * @brief Orders the events of set such that
142    * "more recent" events (i.e. those that are farther down in
143    * the event structure's dependency chain) come before those
144    * that appear "farther in the past"
145    *
146    * @note The events of the event structure are arranged such that
147    * e < e' implies a directed edge from e to e'. However, it is
148    * also useful to be able to traverse the *reverse* graph (for
149    * example when computing the compatibility graph of a configuration),
150    * hence the distinction between "reversed" and the method
151    * "EventSet::get_topological_ordering()"
152    *
153    * @returns a vector `V` with the following property:
154    *
155    * 1. Let i(e) := C -> I map events to their indices in `V`.
156    * For every pair of events e, e' in C, if e < e' then i(e) > i(e')
157    *
158    * Intuitively, events that are closer to the "top" of the event
159    * structure appear farther along in the list than those that appear
160    * closer to the "bottom"
161    */
162   std::vector<const UnfoldingEvent*> get_topological_ordering_of_reverse_graph() const;
163
164   /**
165    * @brief Moves the event set into a list
166    */
167   std::vector<const UnfoldingEvent*> move_into_vector() const&&;
168
169   using iterator       = decltype(events_)::iterator;
170   using const_iterator = decltype(events_)::const_iterator;
171   using value_type     = decltype(events_)::value_type;
172 };
173
174 } // namespace simgrid::mc::udpor
175 #endif