SyDEVS  v0.6.7
Multiscale Simulation and Systems Modeling Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
time_queue.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef SYDEVS_TIME_QUEUE_H_
3 #define SYDEVS_TIME_QUEUE_H_
4 
6 #include <map>
7 #include <set>
8 #include <deque>
9 
10 namespace sydevs {
11 
12 
35 {
36 public:
37  using event_id_set = std::set<int64>;
38 
39  time_queue();
40  explicit time_queue(duration dt0);
41  explicit time_queue(const time_point& t0);
42 
43  time_queue(const time_queue&) = default;
44  time_queue& operator=(const time_queue&) = default;
45  time_queue(time_queue&&) = default;
46  time_queue& operator=(time_queue&&) = default;
47  ~time_queue() = default;
48 
49  bool empty() const;
50  int64 size() const;
51  int64 time_count() const;
52 
53  const time_point& current_time() const;
54 
55  const time_point& advance_time();
56  const time_point& advance_time(duration dt);
57  const time_point& advance_time(const time_point t);
58 
59  bool can_advance_by(duration dt);
60 
61  duration imminent_duration() const;
62  duration duration_until(int64 event_id) const;
63  duration duration_at(int64 time_index) const;
64 
65  const event_id_set& imminent_event_ids() const;
66  const event_id_set& event_ids_at(int64 time_index) const;
67 
68  void pop_imminent_event(int64 event_id);
69  void pop_imminent_events();
70 
71  void plan_event(int64 event_id, duration dt);
72  bool cancel_event(int64 event_id);
73 
74 private:
75  using event_account = std::pair<duration, scale>;
76  using queue_iterator = std::deque<duration>::iterator;
77 
78  duration duration_from_phase(duration pdt) const;
79  duration refined_duration(duration dt, scale precision) const;
80  duration refined_duration_from_phase(duration pdt, scale precision) const;
81  duration rescaled_duration_from_phase(duration pdt, scale precision) const;
82  duration phase_from_duration(duration dt);
83 
84  queue_iterator lower_bound(duration pdt);
85 
86  scale default_precision_;
87  time_point ct_;
88  std::deque<duration> queue_;
89  std::map<duration, event_id_set> event_id_sets_;
90  std::map<int64, event_account> event_accounts_;
91 };
92 
93 
94 inline bool time_queue::empty() const
95 {
96  return event_accounts_.empty();
97 }
98 
99 
100 inline int64 time_queue::size() const
101 {
102  return int64(event_accounts_.size());
103 }
104 
105 
107 {
108  return int64(event_id_sets_.size());
109 }
110 
111 
113 {
114  return ct_;
115 }
116 
117 
118 } // namespace
119 
120 #endif
duration duration_at(int64 time_index) const
Returns the duration at time_index.
Definition: time_queue.cpp:104
std::set< int64 > event_id_set
The container used to store ids of events occurring at the same time.
Definition: time_queue.h:37
bool cancel_event(int64 event_id)
Cancels the event with id event_id, returning true if successful.
Definition: time_queue.cpp:178
time_queue()
Constructs an empty time queue with a current time of zero.
Definition: time_queue.cpp:7
bool empty() const
Returns true if the time queue is empty.
Definition: time_queue.h:94
const time_point & current_time() const
Returns the current time.
Definition: time_queue.h:112
const event_id_set & imminent_event_ids() const
Returns the imminent events.
Definition: time_queue.cpp:112
int64 size() const
Returns the number of events in the time queue.
Definition: time_queue.h:100
duration duration_until(int64 event_id) const
Returns the duration until the event with id event_id, or infinity if no such event exists...
Definition: time_queue.cpp:92
time_queue & operator=(const time_queue &)=default
Copy assignment.
A data type which represents the general concept of scale as a dimensionless power of 1000...
Definition: scale.h:70
A data structure which represents a point in time as an arbitrary-precision multiple of its shortest ...
Definition: time_point.h:84
A data structure which supports the scheduling of future events.
Definition: time_queue.h:34
int64 time_count() const
Returns the number of distinct event times in the time queue.
Definition: time_queue.h:106
const time_point & advance_time()
Advances the current time to that of the imminent events.
Definition: time_queue.cpp:40
duration imminent_duration() const
Returns the duration until the imminent event(s).
Definition: time_queue.cpp:82
bool can_advance_by(duration dt)
Returns true if time can be advanced by the specified duration.
Definition: time_queue.cpp:65
void pop_imminent_event(int64 event_id)
Pops the specified imminent event with id event_id.
Definition: time_queue.cpp:127
~time_queue()=default
Destructor.
int64_t int64
Definition: number_types.h:14
void plan_event(int64 event_id, duration dt)
Schedules an event with id event_id after a planned duration of dt.
Definition: time_queue.cpp:157
const event_id_set & event_ids_at(int64 time_index) const
Returns the events at time_index.
Definition: time_queue.cpp:119
void pop_imminent_events()
Pops the imminent events.
Definition: time_queue.cpp:145