beautypg.com

An ice cream store simulation – HP Integrity NonStop H-Series User Manual

Page 125

background image

the second runs the simulation. A data field is also provided to hold the current simulation "time."

Storing Pointers versus Storing Values

class simulation {
public:
simulation () : eventQueue(), time(0) { }

void scheduleEvent (event * newEvent)
{ eventQueue.push (newEvent); }

void run();

unsigned int time;

protected:
priority_queue, eventComparison> eventQueue;
};

Notice the declaration of the priority queue used to hold the pending events. In this case we are using a

vector

as the underlying container. We could just as easily have used a

deque

.

The heart of the simulation is the member function run(), which defines the event loop. This procedure
makes use of three of the five priority queue operations, namely top(), pop(), and empty(). It is
implemented as follows:

void simulation::run()
{
while (! eventQueue.empty()) {
event * nextEvent = eventQueue.top();
eventQueue.pop();
time = nextEvent->time;
nextEvent->processEvent();
delete nextEvent; // free memory used by event
}
}

An Ice Cream Store Simulation

Obtaining the sample program

To illustrate the use of our simulation framework, this example program gives a simple simulation of an ice
cream store. Such a simulation might be used, for example, to determine the optimal number of chairs that
should be provided, based on assumptions such as the frequency that customers will arrive, the length of
time they will stay, and so on.

Our store simulation will be based around a subclass of class simulation, defined as follows:

class storeSimulation : public simulation {
public:
storeSimulation()
: freeChairs(35), profit(0.0), simulation() { }

This manual is related to the following products: