2004-12-15 00:24:12 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2004-2006 The ScummVM project
|
2004-12-15 00:24:12 +00:00
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
|
|
|
|
* This program 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 this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-12-15 00:24:12 +00:00
|
|
|
*
|
2006-02-11 12:44:16 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2004-12-15 00:24:12 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-07-23 15:16:50 +00:00
|
|
|
#ifndef SAGA_LIST_H
|
|
|
|
#define SAGA_LIST_H
|
2004-12-16 21:33:58 +00:00
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
#include "common/list.h"
|
|
|
|
|
|
|
|
namespace Saga {
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class SortedList : public Common::List<T> {
|
|
|
|
public:
|
2005-05-10 13:56:25 +00:00
|
|
|
typedef int (*CompareFunction) (const T& a, const T& b);
|
2004-12-16 21:33:58 +00:00
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
typedef typename Common::List<T>::iterator iterator;
|
|
|
|
typedef typename Common::List<T>::const_iterator const_iterator;
|
2005-07-09 23:07:46 +00:00
|
|
|
typedef typename Common::List<T> Common_List;
|
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
public:
|
2004-12-16 21:33:58 +00:00
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
iterator pushFront(const T& element) {
|
2004-12-15 20:22:52 +00:00
|
|
|
return insert(Common::List<T>::begin(), element);
|
2004-12-15 00:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator pushBack(const T& element) {
|
2005-07-09 23:07:46 +00:00
|
|
|
return insert(Common_List::end(), element);
|
2004-12-15 00:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator insert(iterator pos, const T& element) {
|
2005-07-09 23:07:46 +00:00
|
|
|
Common_List::insert(pos, element);
|
2004-12-15 00:24:12 +00:00
|
|
|
return --pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator pushFront() {
|
2005-07-09 23:07:46 +00:00
|
|
|
return insert(Common_List::begin());
|
2004-12-15 00:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator pushBack() {
|
2005-07-09 23:07:46 +00:00
|
|
|
return insert(Common_List::end());
|
2004-12-15 00:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator insert(iterator pos) {
|
|
|
|
T init;
|
|
|
|
return insert(pos, init);
|
|
|
|
}
|
|
|
|
|
2005-05-10 13:56:25 +00:00
|
|
|
iterator pushFront(const T& element, CompareFunction compareFunction) {
|
2004-12-15 20:22:52 +00:00
|
|
|
return insert(Common::List<T>::begin(), element, compareFunction);
|
2004-12-15 00:24:12 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 13:56:25 +00:00
|
|
|
iterator pushBack(const T& element, CompareFunction compareFunction) {
|
2005-07-09 23:07:46 +00:00
|
|
|
return insert(Common_List::end(), element, compareFunction);
|
2004-12-15 00:24:12 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 13:56:25 +00:00
|
|
|
iterator insert(iterator pos, const T& element, CompareFunction compareFunction) {
|
2004-12-15 00:24:12 +00:00
|
|
|
int res;
|
|
|
|
|
2005-07-09 23:07:46 +00:00
|
|
|
for (iterator i = Common_List::begin(); i != Common_List::end(); ++i) {
|
2004-12-15 00:24:12 +00:00
|
|
|
res = compareFunction(element, i.operator*());
|
|
|
|
if (res < 0) {
|
|
|
|
return insert(i, element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pushBack(element);
|
|
|
|
}
|
|
|
|
|
2005-05-10 13:56:25 +00:00
|
|
|
iterator reorderUp(iterator pos, CompareFunction compareFunction) {
|
2005-07-30 21:11:48 +00:00
|
|
|
iterator i(pos);
|
2004-12-15 00:24:12 +00:00
|
|
|
int res;
|
|
|
|
|
2005-07-30 21:11:48 +00:00
|
|
|
--i;
|
2004-12-15 20:22:52 +00:00
|
|
|
while (i != Common::List<T>::end()) {
|
2004-12-15 00:24:12 +00:00
|
|
|
res = compareFunction(i.operator*(), pos.operator*());
|
|
|
|
if (res <= 0) {
|
2004-12-16 21:33:58 +00:00
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
T temp(*pos);
|
|
|
|
erase(pos);
|
2005-07-30 21:11:48 +00:00
|
|
|
++i;
|
2004-12-16 20:49:17 +00:00
|
|
|
return insert(i, temp);
|
2004-12-15 00:24:12 +00:00
|
|
|
}
|
|
|
|
--i;
|
|
|
|
}
|
2004-12-16 20:49:17 +00:00
|
|
|
return pos;
|
2004-12-15 00:24:12 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 13:56:25 +00:00
|
|
|
iterator reorderDown(iterator pos, CompareFunction compareFunction) {
|
2005-07-30 21:11:48 +00:00
|
|
|
iterator i(pos);
|
2004-12-15 00:24:12 +00:00
|
|
|
int res;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
++i;
|
2004-12-15 20:22:52 +00:00
|
|
|
while (i != Common::List<T>::end()) {
|
2004-12-15 00:24:12 +00:00
|
|
|
res = compareFunction(i.operator*(), pos.operator*());
|
|
|
|
if (res >= 0) {
|
2004-12-16 21:33:58 +00:00
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
T temp(*pos);
|
|
|
|
erase(pos);
|
2004-12-16 20:49:17 +00:00
|
|
|
return insert(i, temp);
|
2004-12-15 00:24:12 +00:00
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
2004-12-16 20:49:17 +00:00
|
|
|
return pos;
|
2004-12-15 00:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator eraseAndPrev(iterator pos) {
|
2005-07-09 23:07:46 +00:00
|
|
|
assert(pos != Common_List::end());
|
2004-12-15 00:24:12 +00:00
|
|
|
iterator res(pos);
|
|
|
|
|
|
|
|
--res;
|
|
|
|
erase(pos);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove(const T* val) {
|
2005-07-09 23:07:46 +00:00
|
|
|
for (iterator i = Common_List::begin(); i != Common_List::end(); ++i)
|
2005-05-08 21:49:52 +00:00
|
|
|
if (val == i.operator->()) {
|
2004-12-15 00:24:12 +00:00
|
|
|
erase(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool locate(const T* val, iterator& foundedIterator) {
|
|
|
|
|
2004-12-15 20:22:52 +00:00
|
|
|
for (iterator i = Common::List<T>::begin(); i != Common::List<T>::end(); ++i)
|
2004-12-15 00:24:12 +00:00
|
|
|
if (val == i.operator->())
|
|
|
|
{
|
|
|
|
foundedIterator = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2004-12-16 21:33:58 +00:00
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
} // End of namespace Saga
|
2004-12-16 21:33:58 +00:00
|
|
|
|
|
|
|
#endif
|