Version: SMASH-3.4
actions.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2018,2020
3  * SMASH Team
4  *
5  * GNU General Public License (GPLv3 or later)
6  */
7 #ifndef SRC_INCLUDE_SMASH_ACTIONS_H_
8 #define SRC_INCLUDE_SMASH_ACTIONS_H_
9 
10 #include <algorithm>
11 #include <stdexcept>
12 #include <utility>
13 #include <vector>
14 
15 #include "action.h"
16 #include "forwarddeclarations.h"
17 
18 namespace smash {
19 
20 /**
21  * \ingroup data
22  *
23  * The Actions class abstracts the storage and manipulation of actions.
24  *
25  * \note
26  * The Actions object cannot be copied, because it does not make sense
27  * semantically. Move semantics make sense and can be implemented when needed.
28  */
29 class Actions {
30  public:
31  /// Default constructor, creating an empty Actions object.
32  Actions() {}
33  /**
34  * Creates a new Actions object from an ActionList.
35  *
36  * The actions are stored in a heap and not sorted. The entries of
37  * the ActionList are rendered invalid by this constructor.
38  *
39  * \param[in] action_list The ActionList from which to construct the Actions
40  * object
41  */
42  explicit Actions(ActionList&& action_list) : data_(std::move(action_list)) {
43  std::make_heap(data_.begin(), data_.end(), cmp);
44  }
45 
46  /// Cannot be copied
47  Actions(const Actions&) = delete;
48  /// Cannot be copied
49  Actions& operator=(const Actions&) = delete;
50 
51  /// \return whether the list of actions is empty.
52  bool is_empty() const { return data_.empty(); }
53 
54  /**
55  * Return the first action in the list and removes it from the list.
56  *
57  * \throw RuntimeError if the list is empty.
58  */
59  ActionPtr pop() {
60  if (data_.empty()) {
61  throw std::runtime_error("Empty actions list!");
62  }
63  std::pop_heap(data_.begin(), data_.end(), cmp);
64  ActionPtr act = std::move(data_.back());
65  data_.pop_back();
66  return act;
67  }
68 
69  /// Return time of execution of earliest action
70  double earliest_time() const { return data_.front()->time_of_execution(); }
71 
72  /**
73  * Insert a list of actions into this object.
74  *
75  * They're inserted at the right places to keep the complete list a heap.
76  *
77  * \param[in] new_acts The actions that will be inserted.
78  */
79  void insert(ActionList&& new_acts) {
80  for (auto& a : new_acts) {
81  insert(std::move(a));
82  }
83  }
84 
85  /**
86  * Insert an action into this container.
87  *
88  * The function makes sure that the action is inserted at the right place.
89  *
90  * \param[in] action The action to insert.
91  */
92  void insert(ActionPtr&& action) {
93  data_.push_back(std::move(action));
94  std::push_heap(data_.begin(), data_.end(), cmp);
95  }
96 
97  /// \return Number of actions.
98  ActionList::size_type size() const { return data_.size(); }
99 
100  /// Delete all actions.
101  void clear() { data_.clear(); }
102 
103  /// \return an iterator to the earliest action.
104  std::vector<ActionPtr>::const_reverse_iterator begin() const {
105  return data_.crbegin();
106  }
107 
108  /// \return an iterator to the place following the last action.
109  std::vector<ActionPtr>::const_reverse_iterator end() const {
110  return data_.crend();
111  }
112 
113  private:
114  /**
115  * Compare two action pointer such that the maximum is the most recent
116  * action.
117  *
118  * \param[in] a First action
119  * \param[in] b Second action
120  * \return Whether the first action will be executed later than the second.
121  */
122  static bool cmp(const ActionPtr& a, const ActionPtr& b) {
123  return a->time_of_execution() > b->time_of_execution();
124  }
125 
126  /**
127  * Dynamic data.
128  *
129  * Vector is likely the best container type here. Because std::sort requires
130  * random access iterators. Any linked data structure (e.g. list) thus
131  * requires a less efficient sort algorithm.
132  */
133  std::vector<ActionPtr> data_;
134 };
135 
136 } // namespace smash
137 
138 #endif // SRC_INCLUDE_SMASH_ACTIONS_H_
The Actions class abstracts the storage and manipulation of actions.
Definition: actions.h:29
Actions()
Default constructor, creating an empty Actions object.
Definition: actions.h:32
void insert(ActionPtr &&action)
Insert an action into this container.
Definition: actions.h:92
ActionPtr pop()
Return the first action in the list and removes it from the list.
Definition: actions.h:59
Actions(ActionList &&action_list)
Creates a new Actions object from an ActionList.
Definition: actions.h:42
double earliest_time() const
Return time of execution of earliest action.
Definition: actions.h:70
static bool cmp(const ActionPtr &a, const ActionPtr &b)
Compare two action pointer such that the maximum is the most recent action.
Definition: actions.h:122
Actions & operator=(const Actions &)=delete
Cannot be copied.
ActionList::size_type size() const
Definition: actions.h:98
void insert(ActionList &&new_acts)
Insert a list of actions into this object.
Definition: actions.h:79
std::vector< ActionPtr > data_
Dynamic data.
Definition: actions.h:133
bool is_empty() const
Definition: actions.h:52
std::vector< ActionPtr >::const_reverse_iterator end() const
Definition: actions.h:109
Actions(const Actions &)=delete
Cannot be copied.
std::vector< ActionPtr >::const_reverse_iterator begin() const
Definition: actions.h:104
void clear()
Delete all actions.
Definition: actions.h:101
Definition: action.h:24