sgpemv2/src/backend/schedulable_list.cc
johnny 47c184ac3f - "Commenting The Source(tm)" 02-2006 - The best summer camp of the world - Second part
- Reorganized the .cc files to match the .hh methods order.


git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@421 3ecf2c5c-341e-0410-92b4-d18e462d057c
2006-02-24 01:24:21 +00:00

215 lines
4.6 KiB
C++

// src/backend/schedulable_list.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 "schedulable_list.hh"
using namespace sgpem;
using namespace std;
using namespace memory;
SchedulableList::SchedulableList()
{
}
SchedulableStatus*
SchedulableList::top()
{
if (_list.size() == 0)
return NULL;
return &_list.front();
}
SchedulableStatus*
SchedulableList::bottom()
{
if (_list.size() == 0)
return NULL;
return &_list.back();
}
/**
Returns a pointer to the element at position "where". If the queue is empty or "where" is
out of rangethe NULL pointer will be returned.
DON'T call delete on the returned pointer! Its destruction is managed by the queue.
*/
SchedulableStatus*
SchedulableList::get_item_at(const uint& where)
{
if (_list.size() == 0 || where >= _list.size())
return NULL;
list<SchedulableStatus>::iterator i = _list.begin();
for (uint f=0; f < where; f++)
i++;
return &(*i);
}
const SchedulableStatus*
SchedulableList::get_item_at(const uint& where) const
{
if (_list.size() == 0 || where >= _list.size())
return NULL;
list<SchedulableStatus>::const_iterator i = _list.begin();
for (uint f=0; f < where; f++)
i++;
return &(*i);
}
/**
Returns the number of elements inserted into the queue.
*/
uint
SchedulableList::size() const
{
return _list.size();
}
void
SchedulableList::add_at_top(const SchedulableStatus& ss)
{
_list.push_front(ss);
}
void
SchedulableList::add_at_bottom(const SchedulableStatus& ss)
{
_list.push_back(ss);
}
smart_ptr<SchedulableStatus>
SchedulableList::remove(const uint& position)
{
if (_list.size() == 0 || position >= _list.size())
return smart_ptr<SchedulableStatus>(NULL);
//creates a copy of the first element
smart_ptr<SchedulableStatus> sm = new SchedulableStatus(*top());
//pops the first element
_list.pop_front();
//returns the copy
return sm;
}
/**
*/
bool
SchedulableList::insert_at(const uint& which, const uint& where)
{
//out of range
if (which >= _list.size() || where >= _list.size())
return false;
//nothing to do
if (where == which)
return true;
list<SchedulableStatus>::iterator i_where = _list.begin();
list<SchedulableStatus>::iterator i_which = _list.begin();
for (uint f=0; f < where; f++)
i_where++;
for (uint f=0; f < which; f++)
i_which++;
//save and pop WHICH
SchedulableStatus temp = *i_which;
_list.erase(i_which);
//insert WHICH before WHERE
_list.insert(i_where, temp);
return true;
}
/**
Removes all elements
*/
void
SchedulableList::clear()
{
_list.clear();
}
/**
\brief Returns TRUE if the two objects have the same SchedulableStatus objects in the same order.
*/
bool
SchedulableList::operator==(const SchedulableList& dx) const
{
return _list == dx._list;
}
/**
\brief Returns TRUE if the two objects have the same SchedulableStatus objects with NO ORDER IMPORTANCE.
*/
bool
SchedulableList::has_same_objects(const SchedulableList& dx) const
{
if (_list.size() != dx._list.size())
return false;
//check if dx has ALL and ONLY the elements holded by _list with no order importance
for(list<SchedulableStatus>::const_iterator f=_list.begin(); f != _list.end(); f++)
if (find(dx._list.begin(), dx._list.end(), *f) == dx._list.end()) //element NOT found!!
return false;
return true;
}
void
SchedulableList::swap(unsigned int positionA, unsigned int positionB) throw()
{
if (positionA == positionB || positionA >= _list.size() || positionB >= _list.size())
return;
unsigned int min, max;
if (positionA < positionB)
{
min = positionA;
max = positionB;
}
else
{
min = positionB;
max = positionA;
}
list<SchedulableStatus>::iterator i1 = _list.begin();
list<SchedulableStatus>::iterator i2 = _list.begin();
//reach the first element;
for (uint f=0; f < min; f++)
i1++;
SchedulableStatus temp = *i1;
//reach the second element;
i2 = i1;
for (uint f=min; f < max; f++)
i2++;
*i1 = *i2;
*i2 = temp;
}