sgpemv2/src/backend/history.cc
fpaparel e8b28cc2ec - Added some source code
git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@304 3ecf2c5c-341e-0410-92b4-d18e462d057c
2006-02-09 18:51:26 +00:00

128 lines
3.5 KiB
C++

// src/backend/history.cc - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "history.hh"
using namespace std;
using namespace sgpem;
using namespace memory;
//History::instance; //static object
History History::_instance(10); //dummy parameter
History::History(int) //private constructor. The parameter is discarded
:_total_time_elapsed(0)
{
}
History&
History::getInstance()
{
return _instance;
}
/**
Returns a pointer to a copy of the SchedulableStatus object relative to this instant.
It can be NULL if time is out of range or if the SimulationStatus object relative to
this instant has no running entities.
*/
smart_ptr<const SchedulableStatus>
History::getScheduledAt (int time)
{
if (time >= _total_time_elapsed || time < 0) //out of range
return smart_ptr<const SchedulableStatus>(NULL);
return getSimulationStatusAt(time)->getRunning();
}
/**
Returns a pointer to a copy of the SimulationStatus object relative to this instant or NULL
if time is out of range.
*/
smart_ptr<const SimulationStatus>
History::getSimulationStatusAt (int time)
{
if (time >= _total_time_elapsed || time < 0) //out of range
return smart_ptr<const SimulationStatus>(NULL);
int trascorso = 0;
for(vector<Slice>::iterator i=_slices.begin(); i < _slices.end(); i++)
if (time < trascorso + i->getDuration()) //FOUND!!
return smart_ptr<const SimulationStatus>(new SimulationStatus(i->getSimulationStatus()));
else //Go on...
trascorso += i->getDuration();
//never reached
return smart_ptr<const SimulationStatus>(NULL);
}
int
History::getCurrentTime()
{
return _total_time_elapsed;
}
/**
Appends to the history a SimulationStatus creating a Slice with duration of 1 instant.
Calls the method notify() in quality of ObservedSubject, updating all observers.
*/
void
History::enqueueSlice (SimulationStatus status)
{
if (_slices.size() == 0){
_slices.push_back(Slice(0, 1, status));
_total_time_elapsed = 1;
notify();
return;
}
//check the last slice
Slice& last = _slices[_slices.size()-1];
if (last.getSimulationStatus() == status) //increments the duration by ONE unit
{
last.setDuration(last.getDuration()+1);
}
else //insert a new slice CONTIGUOUS to the last one
{
_slices.push_back(Slice(last.getStartedAt() + last.getDuration(), 1, status));
}
_total_time_elapsed++; //one instant is passed...
notify();
}
/**
Removes all the informations about the simulation following the specified instant.
*/
void
History::truncateAt (int instant)
{
//reach the instant
vector<Slice>::iterator i = _slices.begin();
for (int k=0; k < instant; k++)
i++;
//replaces the current vector with the "trimmed" one.
_slices = vector<Slice>(_slices.begin(),i);
_total_time_elapsed = instant;
}