From b7f308628638207b7bc56e99cb273cf83ad092f8 Mon Sep 17 00:00:00 2001 From: fpaparel Date: Wed, 22 Feb 2006 23:19:51 +0000 Subject: [PATCH] - implemented swap in schedulable_list.cc git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@390 3ecf2c5c-341e-0410-92b4-d18e462d057c --- src/backend/schedulable_list.cc | 76 ++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 20 deletions(-) diff --git a/src/backend/schedulable_list.cc b/src/backend/schedulable_list.cc index e97e2b1..5ca912b 100644 --- a/src/backend/schedulable_list.cc +++ b/src/backend/schedulable_list.cc @@ -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. 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. 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. 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()) return NULL; - + list::iterator i = _list.begin(); for (uint f=0; f < where; f++) i++; @@ -82,7 +82,7 @@ SchedulableList::get_item_at(const uint& where) const { if (_list.size() == 0 || where >= _list.size()) return NULL; - + list::const_iterator i = _list.begin(); for (uint f=0; f < where; f++) i++; @@ -103,17 +103,17 @@ SchedulableList::size() const void SchedulableList::add_at_bottom(const SchedulableStatus& ss) { - _list.push_back(ss); + _list.push_back(ss); } void 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. Ex. remove(0); removes the top of the list @@ -134,7 +134,7 @@ SchedulableList::remove(const uint& position) } /** - + */ bool 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 if (where == which) return true; - + list::iterator i_where = _list.begin(); list::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; } /** @@ -169,34 +169,70 @@ SchedulableList::insert_at(const uint& which, const uint& where) void SchedulableList::clear() { - _list.clear(); + _list.clear(); } -/** +/** \brief Returns TRUE if the two objects have the same SchedulableStatus objects in the same order. */ -bool +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 +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::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::iterator i1 = _list.begin(); + list::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; +} +