forked from gfrd/egfrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventScheduler.hpp
143 lines (112 loc) · 2.94 KB
/
EventScheduler.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifndef __EVENTSCHEDULER_HPP
#define __EVENTSCHEDULER_HPP
//
// written by Koichi Takahashi <[email protected]>,
// E-Cell Project.
//
#include <boost/range/iterator_range.hpp>
#include <boost/shared_ptr.hpp>
#include <stdexcept>
#include "DynamicPriorityQueue.hpp"
/**
Event scheduler.
This class works as a sequential
event scheduler with a heap-tree based priority queue.
*/
template<typename Ttime_>
class EventScheduler
{
public:
typedef Ttime_ time_type;
struct Event
{
typedef Ttime_ time_type;
Event(time_type const& time): time_(time) {}
virtual ~Event() {}
time_type const& time() const
{
return time_;
}
protected:
const time_type time_;
};
protected:
struct event_comparator
{
bool operator()(boost::shared_ptr<Event> const& lhs,
boost::shared_ptr<Event> const& rhs) const
{
return lhs->time() <= rhs->time();
}
};
typedef DynamicPriorityQueue<boost::shared_ptr<Event>, event_comparator> EventPriorityQueue;
public:
typedef typename EventPriorityQueue::size_type size_type;
typedef typename EventPriorityQueue::identifier_type identifier_type;
typedef typename EventPriorityQueue::value_type value_type;
typedef boost::iterator_range<typename EventPriorityQueue::const_iterator> events_range;
public:
EventScheduler(): time_( 0.0 ) {}
~EventScheduler() {}
time_type time() const
{
return time_;
}
size_type size() const
{
return eventPriorityQueue_.size();
}
value_type const& top() const
{
return eventPriorityQueue_.top();
}
value_type pop()
{
if (eventPriorityQueue_.empty())
{
throw std::out_of_range("queue is empty");
}
const value_type top(eventPriorityQueue_.top());
eventPriorityQueue_.pop();
time_ = top.second->time();
return top;
}
value_type const& second() const
{
return eventPriorityQueue_.second();
}
boost::shared_ptr<Event> get(identifier_type const& id) const
{
return eventPriorityQueue_.get(id);
}
void clear()
{
time_ = 0.0;
eventPriorityQueue_.clear();
}
identifier_type add(boost::shared_ptr<Event> const& event)
{
return eventPriorityQueue_.push(event);
}
void remove(identifier_type const& id)
{
eventPriorityQueue_.pop(id);
}
void update(value_type const& pair)
{
eventPriorityQueue_.replace(pair);
}
bool check() const
{
return eventPriorityQueue_.check();
}
events_range events() const
{
return boost::make_iterator_range(eventPriorityQueue_.begin(),
eventPriorityQueue_.end());
}
private:
EventPriorityQueue eventPriorityQueue_;
time_type time_;
};
#endif /* __EVENTSCHEDULER_HPP */