- implemented swap in schedulable_list.cc

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@390 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
fpaparel 2006-02-22 23:19:51 +00:00
parent 73a3e72118
commit b7f3086286
1 changed files with 56 additions and 20 deletions

View File

@ -30,7 +30,7 @@ SchedulableList::SchedulableList()
} }
/** /**
Returns a pointer to the first element. If the queue is empty the NULL pointer will Returns a pointer to the first element. If the queue is empty the NULL pointer will
be returned. be returned.
DON'T call delete on the returned pointer! Its destruction is managed by the queue. DON'T call delete on the returned pointer! Its destruction is managed by the queue.
@ -45,7 +45,7 @@ SchedulableList::top()
} }
/** /**
Returns a pointer to the last element. If the queue is empty the NULL pointer will Returns a pointer to the last element. If the queue is empty the NULL pointer will
be returned. be returned.
DON'T call delete on the returned pointer! Its destruction is managed by the queue. DON'T call delete on the returned pointer! Its destruction is managed by the queue.
@ -60,7 +60,7 @@ SchedulableList::bottom()
} }
/** /**
Returns a pointer to the element at position "where". If the queue is empty or "where" is 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. out of rangethe NULL pointer will be returned.
DON'T call delete on the returned pointer! Its destruction is managed by the queue. DON'T call delete on the returned pointer! Its destruction is managed by the queue.
@ -70,7 +70,7 @@ SchedulableList::get_item_at(const uint& where)
{ {
if (_list.size() == 0 || where >= _list.size()) if (_list.size() == 0 || where >= _list.size())
return NULL; return NULL;
list<SchedulableStatus>::iterator i = _list.begin(); list<SchedulableStatus>::iterator i = _list.begin();
for (uint f=0; f < where; f++) for (uint f=0; f < where; f++)
i++; i++;
@ -82,7 +82,7 @@ SchedulableList::get_item_at(const uint& where) const
{ {
if (_list.size() == 0 || where >= _list.size()) if (_list.size() == 0 || where >= _list.size())
return NULL; return NULL;
list<SchedulableStatus>::const_iterator i = _list.begin(); list<SchedulableStatus>::const_iterator i = _list.begin();
for (uint f=0; f < where; f++) for (uint f=0; f < where; f++)
i++; i++;
@ -103,17 +103,17 @@ SchedulableList::size() const
void void
SchedulableList::add_at_bottom(const SchedulableStatus& ss) SchedulableList::add_at_bottom(const SchedulableStatus& ss)
{ {
_list.push_back(ss); _list.push_back(ss);
} }
void void
SchedulableList::add_at_top(const SchedulableStatus& ss) SchedulableList::add_at_top(const SchedulableStatus& ss)
{ {
_list.push_front(ss); _list.push_front(ss);
} }
/** /**
Removes an element from the list. Returns a smart pointer a copy of it or to NULL if Removes an element from the list. Returns a smart pointer a copy of it or to NULL if
"position" is out of range. "position" is out of range.
Ex. remove(0); removes the top of the list Ex. remove(0); removes the top of the list
@ -134,7 +134,7 @@ SchedulableList::remove(const uint& position)
} }
/** /**
*/ */
bool bool
SchedulableList::insert_at(const uint& which, const uint& where) SchedulableList::insert_at(const uint& which, const uint& where)
@ -145,21 +145,21 @@ SchedulableList::insert_at(const uint& which, const uint& where)
//nothing to do //nothing to do
if (where == which) if (where == which)
return true; return true;
list<SchedulableStatus>::iterator i_where = _list.begin(); list<SchedulableStatus>::iterator i_where = _list.begin();
list<SchedulableStatus>::iterator i_which = _list.begin(); list<SchedulableStatus>::iterator i_which = _list.begin();
for (uint f=0; f < where; f++) for (uint f=0; f < where; f++)
i_where++; i_where++;
for (uint f=0; f < which; f++) for (uint f=0; f < which; f++)
i_which++; i_which++;
//save and pop WHICH //save and pop WHICH
SchedulableStatus temp = *i_which; SchedulableStatus temp = *i_which;
_list.erase(i_which); _list.erase(i_which);
//insert WHICH before WHERE //insert WHICH before WHERE
_list.insert(i_where, temp); _list.insert(i_where, temp);
return true; return true;
} }
/** /**
@ -169,34 +169,70 @@ SchedulableList::insert_at(const uint& which, const uint& where)
void void
SchedulableList::clear() SchedulableList::clear()
{ {
_list.clear(); _list.clear();
} }
/** /**
\brief Returns TRUE if the two objects have the same SchedulableStatus objects in the same order. \brief Returns TRUE if the two objects have the same SchedulableStatus objects in the same order.
*/ */
bool bool
SchedulableList::operator==(const SchedulableList& dx) const SchedulableList::operator==(const SchedulableList& dx) const
{ {
return _list == dx._list; return _list == dx._list;
} }
/** /**
\brief Returns TRUE if the two objects have the same SchedulableStatus objects with NO ORDER IMPORTANCE. \brief Returns TRUE if the two objects have the same SchedulableStatus objects with NO ORDER IMPORTANCE.
*/ */
bool bool
SchedulableList::has_same_objects(const SchedulableList& dx) const SchedulableList::has_same_objects(const SchedulableList& dx) const
{ {
if (_list.size() != dx._list.size()) if (_list.size() != dx._list.size())
return false; return false;
//check if dx has ALL and ONLY the elements holded by _list with no order importance //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++) 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!! if (find(dx._list.begin(), dx._list.end(), *f) == dx._list.end()) //element NOT found!!
return false; return false;
return true; 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;
}