Logo AND Algorithmique Numérique Distribuée

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