Logo AND Algorithmique Numérique Distribuée

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