svn path=/trunk/boinc/; revision=24791

This commit is contained in:
David Anderson 2011-12-14 01:38:14 +00:00
parent 751f4f56f5
commit a1bb8c7ff9
1 changed files with 33 additions and 0 deletions

View File

@ -1,8 +1,32 @@
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2011 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
// The world's smallest discrete event simulator.
// Uses the STL "heap" data structure for efficient event storage.
#include <vector>
#include <algorithm>
using std::vector;
// base class for events.
// t is the time when the event occurs.
// handle() is what you want to happen then
//
struct EVENT {
double t;
virtual void handle(){}
@ -15,11 +39,17 @@ bool compare(EVENT* e1, EVENT* e2) {
struct SIMULATOR {
vector<EVENT*> events;
double now;
// add an event
//
void insert(EVENT* e) {
//printf("adding %x\n", e);
events.push_back(e);
push_heap(events.begin(), events.end(), compare);
}
// remove an event
//
void remove(EVENT* e) {
vector<EVENT*>::iterator i;
//printf("removing %x\n", e);
@ -33,6 +63,9 @@ struct SIMULATOR {
}
//printf("%x not found\n", e);
}
// run the simulator for the given time period
//
void simulate(double dur) {
while (events.size()) {
EVENT* e = events.front();