Version: SMASH-3.1
smash::Actions Class Reference

#include <actions.h>

The Actions class abstracts the storage and manipulation of actions.

Note
The Actions object cannot be copied, because it does not make sense semantically. Move semantics make sense and can be implemented when needed.

Definition at line 29 of file actions.h.

Public Member Functions

 Actions ()
 Default constructor, creating an empty Actions object. More...
 
 Actions (ActionList &&action_list)
 Creates a new Actions object from an ActionList. More...
 
 Actions (const Actions &)=delete
 Cannot be copied. More...
 
Actionsoperator= (const Actions &)=delete
 Cannot be copied. More...
 
bool is_empty () const
 
ActionPtr pop ()
 Return the first action in the list and removes it from the list. More...
 
double earliest_time () const
 Return time of execution of earliest action. More...
 
void insert (ActionList &&new_acts)
 Insert a list of actions into this object. More...
 
void insert (ActionPtr &&action)
 Insert an action into this container. More...
 
ActionList::size_type size () const
 
void clear ()
 Delete all actions. More...
 
std::vector< ActionPtr >::const_reverse_iterator begin () const
 
std::vector< ActionPtr >::const_reverse_iterator end () const
 

Static Private Member Functions

static bool cmp (const ActionPtr &a, const ActionPtr &b)
 Compare two action pointer such that the maximum is the most recent action. More...
 

Private Attributes

std::vector< ActionPtr > data_
 Dynamic data. More...
 

Constructor & Destructor Documentation

◆ Actions() [1/3]

smash::Actions::Actions ( )
inline

Default constructor, creating an empty Actions object.

Definition at line 32 of file actions.h.

32 {}

◆ Actions() [2/3]

smash::Actions::Actions ( ActionList &&  action_list)
inlineexplicit

Creates a new Actions object from an ActionList.

The actions are stored in a heap and not sorted. The entries of the ActionList are rendered invalid by this constructor.

Parameters
[in]action_listThe ActionList from which to construct the Actions object

Definition at line 42 of file actions.h.

42  : data_(std::move(action_list)) {
43  std::make_heap(data_.begin(), data_.end(), cmp);
44  }
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
std::vector< ActionPtr > data_
Dynamic data.
Definition: actions.h:133

◆ Actions() [3/3]

smash::Actions::Actions ( const Actions )
delete

Cannot be copied.

Member Function Documentation

◆ operator=()

Actions& smash::Actions::operator= ( const Actions )
delete

Cannot be copied.

◆ is_empty()

bool smash::Actions::is_empty ( ) const
inline
Returns
whether the list of actions is empty.

Definition at line 52 of file actions.h.

52 { return data_.empty(); }

◆ pop()

ActionPtr smash::Actions::pop ( )
inline

Return the first action in the list and removes it from the list.

Exceptions
RuntimeErrorif the list is empty.

Definition at line 59 of file actions.h.

59  {
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  }

◆ earliest_time()

double smash::Actions::earliest_time ( ) const
inline

Return time of execution of earliest action.

Definition at line 70 of file actions.h.

70 { return data_.front()->time_of_execution(); }

◆ insert() [1/2]

void smash::Actions::insert ( ActionList &&  new_acts)
inline

Insert a list of actions into this object.

They're inserted at the right places to keep the complete list a heap.

Parameters
[in]new_actsThe actions that will be inserted.

Definition at line 79 of file actions.h.

79  {
80  for (auto& a : new_acts) {
81  insert(std::move(a));
82  }
83  }
void insert(ActionList &&new_acts)
Insert a list of actions into this object.
Definition: actions.h:79

◆ insert() [2/2]

void smash::Actions::insert ( ActionPtr &&  action)
inline

Insert an action into this container.

The function makes sure that the action is inserted at the right place.

Parameters
[in]actionThe action to insert.

Definition at line 92 of file actions.h.

92  {
93  data_.push_back(std::move(action));
94  std::push_heap(data_.begin(), data_.end(), cmp);
95  }

◆ size()

ActionList::size_type smash::Actions::size ( ) const
inline
Returns
Number of actions.

Definition at line 98 of file actions.h.

98 { return data_.size(); }

◆ clear()

void smash::Actions::clear ( )
inline

Delete all actions.

Definition at line 101 of file actions.h.

101 { data_.clear(); }

◆ begin()

std::vector<ActionPtr>::const_reverse_iterator smash::Actions::begin ( ) const
inline
Returns
an iterator to the earliest action.

Definition at line 104 of file actions.h.

104  {
105  return data_.crbegin();
106  }

◆ end()

std::vector<ActionPtr>::const_reverse_iterator smash::Actions::end ( ) const
inline
Returns
an iterator to the place following the last action.

Definition at line 109 of file actions.h.

109  {
110  return data_.crend();
111  }

◆ cmp()

static bool smash::Actions::cmp ( const ActionPtr &  a,
const ActionPtr &  b 
)
inlinestaticprivate

Compare two action pointer such that the maximum is the most recent action.

Parameters
[in]aFirst action
[in]bSecond action
Returns
Whether the first action will be executed later than the second.

Definition at line 122 of file actions.h.

122  {
123  return a->time_of_execution() > b->time_of_execution();
124  }

Member Data Documentation

◆ data_

std::vector<ActionPtr> smash::Actions::data_
private

Dynamic data.

Vector is likely the best container type here. Because std::sort requires random access iterators. Any linked data structure (e.g. list) thus requires a less efficient sort algorithm.

Definition at line 133 of file actions.h.


The documentation for this class was generated from the following file: