2011-11-03 03:29:07 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
struct EVENT {
|
|
|
|
double t;
|
|
|
|
virtual void handle(){}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool compare(EVENT* e1, EVENT* e2) {
|
|
|
|
return (e1->t > e2->t);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SIMULATOR {
|
|
|
|
vector<EVENT*> events;
|
2011-11-19 00:29:55 +00:00
|
|
|
double now;
|
2011-11-03 03:29:07 +00:00
|
|
|
void insert(EVENT* e) {
|
|
|
|
events.push_back(e);
|
|
|
|
push_heap(events.begin(), events.end(), compare);
|
|
|
|
}
|
2011-11-24 21:16:32 +00:00
|
|
|
void remove(EVENT* e) {
|
|
|
|
vector<EVENT*>::iterator i;
|
|
|
|
for (i=events.begin(); i!=events.end(); i++) {
|
|
|
|
if (*i == e) {
|
|
|
|
events.erase(i);
|
|
|
|
make_heap(events.begin(), events.end(), compare);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-03 03:29:07 +00:00
|
|
|
void simulate(double dur) {
|
|
|
|
while (events.size()) {
|
|
|
|
EVENT* e = events.front();
|
|
|
|
pop_heap(events.begin(), events.end(), compare);
|
|
|
|
events.pop_back();
|
2011-11-19 00:29:55 +00:00
|
|
|
now = e->t;
|
|
|
|
if (now > dur) break;
|
2011-11-03 03:29:07 +00:00
|
|
|
e->handle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|