Version: SMASH-3.1
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 
29 class Actions {
30  public:
32  Actions() {}
42  explicit Actions(ActionList&& action_list) : data_(std::move(action_list)) {
43  std::make_heap(data_.begin(), data_.end(), cmp);
44  }
45 
47  Actions(const Actions&) = delete;
49  Actions& operator=(const Actions&) = delete;
50 
52  bool is_empty() const { return data_.empty(); }
53 
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 
70  double earliest_time() const { return data_.front()->time_of_execution(); }
71 
79  void insert(ActionList&& new_acts) {
80  for (auto& a : new_acts) {
81  insert(std::move(a));
82  }
83  }
84 
92  void insert(ActionPtr&& action) {
93  data_.push_back(std::move(action));
94  std::push_heap(data_.begin(), data_.end(), cmp);
95  }
96 
98  ActionList::size_type size() const { return data_.size(); }
99 
101  void clear() { data_.clear(); }
102 
104  std::vector<ActionPtr>::const_reverse_iterator begin() const {
105  return data_.crbegin();
106  }
107 
109  std::vector<ActionPtr>::const_reverse_iterator end() const {
110  return data_.crend();
111  }
112 
113  private:
122  static bool cmp(const ActionPtr& a, const ActionPtr& b) {
123  return a->time_of_execution() > b->time_of_execution();
124  }
125 
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