/* scheduler.C
 */

#include <values.h>
#include "scheduler.h"
#include "statistics.h"
#include "events.h"
#include "main.h"


// Remove the event of minimum time and execute its handler

void Scheduler::handleNextEvent()

{
  int minIndex;
  Event *e;

  // Find the event of minimum time

  minIndex = 0;
  for (int i=1; i<events.size(); i++)
    if (events[i]->time < events[minIndex]->time) {
      minIndex = i;
    }

  // Treat the event

  e = events[minIndex];
  events.remove( minIndex );	// Remove it from the queue
  currentTime = e->time;	// Update current time

  if (currentTime >= stoppingTime) {
    statistics.finalReport();	// stop the simulation
    exit(0);
  }

  e->handleEvent();		// Handle the event
  delete e;			// Delete the event
}
