2002-09-08 01:08:12 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2002-2006 The ScummVM project
|
2002-09-08 01:08: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.
|
2002-09-08 01:08:12 +00:00
|
|
|
*
|
2006-02-11 09:53:53 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2002-09-08 01:08:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COMMON_LIST_H
|
|
|
|
#define COMMON_LIST_H
|
|
|
|
|
2003-08-01 12:21:04 +00:00
|
|
|
#include "common/scummsys.h"
|
2002-11-14 13:33:16 +00:00
|
|
|
#include <assert.h>
|
2002-09-08 01:08:12 +00:00
|
|
|
|
2003-10-02 17:43:02 +00:00
|
|
|
namespace Common {
|
2002-09-08 01:08:12 +00:00
|
|
|
|
2004-04-12 01:19:26 +00:00
|
|
|
/**
|
|
|
|
* Simple double linked list, modelled after the list template of the standard
|
2005-09-03 16:10:08 +00:00
|
|
|
* C++ library.
|
2004-04-12 01:19:26 +00:00
|
|
|
*/
|
2002-09-08 01:08:12 +00:00
|
|
|
template <class T>
|
|
|
|
class List {
|
|
|
|
protected:
|
2005-05-10 00:45:39 +00:00
|
|
|
#if defined (_WIN32_WCE) || defined (_MSC_VER)
|
2005-05-09 22:39:16 +00:00
|
|
|
//FIXME evc4 and msvc7 doesn't like it as protected member
|
|
|
|
public:
|
|
|
|
#endif
|
2004-04-12 01:19:26 +00:00
|
|
|
struct NodeBase {
|
|
|
|
NodeBase *_prev;
|
|
|
|
NodeBase *_next;
|
|
|
|
};
|
2005-09-03 16:10:08 +00:00
|
|
|
|
2004-04-12 01:19:26 +00:00
|
|
|
template <class T2>
|
|
|
|
struct Node : public NodeBase {
|
|
|
|
T2 _data;
|
2005-09-03 16:10:08 +00:00
|
|
|
|
2004-04-12 01:19:26 +00:00
|
|
|
Node<T2>(const T2 &x) : _data(x) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T2>
|
2004-12-12 21:38:11 +00:00
|
|
|
class Iterator {
|
|
|
|
friend class List<T>;
|
2004-04-12 01:19:26 +00:00
|
|
|
NodeBase *_node;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2006-02-12 00:33:04 +00:00
|
|
|
#if !defined (PALMOS_MODE) && !defined (__WINSCW__)
|
2004-12-12 21:38:11 +00:00
|
|
|
explicit Iterator<T2>(NodeBase *node) : _node(node) {}
|
2005-09-03 16:10:08 +00:00
|
|
|
#else
|
|
|
|
Iterator<T2>(NodeBase *node) : _node(node) {}
|
|
|
|
#endif
|
|
|
|
|
2004-12-12 21:38:11 +00:00
|
|
|
public:
|
2004-05-08 19:34:06 +00:00
|
|
|
Iterator<T2>() : _node(0) {}
|
2004-04-12 01:19:26 +00:00
|
|
|
|
|
|
|
// Prefix inc
|
|
|
|
Iterator<T2> &operator++() {
|
2004-05-08 19:34:06 +00:00
|
|
|
if (_node)
|
|
|
|
_node = _node->_next;
|
2004-04-12 01:19:26 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
// Postfix inc
|
|
|
|
Iterator<T2> operator++(int) {
|
2006-02-11 18:02:13 +00:00
|
|
|
Iterator tmp(_node);
|
2006-03-31 22:15:42 +00:00
|
|
|
++(*this);
|
2004-04-12 01:19:26 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
// Prefix dec
|
|
|
|
Iterator<T2> &operator--() {
|
2004-05-08 19:34:06 +00:00
|
|
|
if (_node)
|
|
|
|
_node = _node->_prev;
|
2004-04-12 01:19:26 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
// Postfix dec
|
|
|
|
Iterator<T2> operator--(int) {
|
2006-02-11 18:02:13 +00:00
|
|
|
Iterator tmp(_node);
|
2006-03-31 22:15:42 +00:00
|
|
|
--(*this);
|
2004-04-12 01:19:26 +00:00
|
|
|
return tmp;
|
2004-04-09 15:10:23 +00:00
|
|
|
}
|
2004-04-16 22:04:32 +00:00
|
|
|
T2& operator*() const {
|
2004-05-08 19:34:06 +00:00
|
|
|
assert(_node);
|
2006-01-29 03:44:13 +00:00
|
|
|
#if (__GNUC__ == 2) && (__GNUC_MINOR__ >= 95)
|
|
|
|
return static_cast<List<T>::Node<T2> *>(_node)->_data;
|
|
|
|
#else
|
2004-04-12 01:19:26 +00:00
|
|
|
return static_cast<Node<T2>*>(_node)->_data;
|
2006-01-29 03:44:13 +00:00
|
|
|
#endif
|
2004-04-09 15:10:23 +00:00
|
|
|
}
|
2004-04-16 22:04:32 +00:00
|
|
|
T2* operator->() const {
|
2004-04-12 01:19:26 +00:00
|
|
|
return &(operator*());
|
|
|
|
}
|
2005-09-03 16:10:08 +00:00
|
|
|
|
2004-04-12 01:19:26 +00:00
|
|
|
bool operator==(const Iterator<T2>& x) const {
|
|
|
|
return _node == x._node;
|
|
|
|
}
|
2005-09-03 16:10:08 +00:00
|
|
|
|
2004-04-12 01:19:26 +00:00
|
|
|
bool operator!=(const Iterator<T2>& x) const {
|
|
|
|
return _node != x._node;
|
2004-04-09 15:10:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2004-04-12 01:19:26 +00:00
|
|
|
NodeBase *_anchor;
|
2002-09-08 01:08:12 +00:00
|
|
|
|
2003-10-05 14:02:28 +00:00
|
|
|
public:
|
2004-04-12 01:19:26 +00:00
|
|
|
typedef Iterator<T> iterator;
|
2004-04-16 22:04:32 +00:00
|
|
|
typedef Iterator<const T> const_iterator;
|
2003-10-05 14:02:28 +00:00
|
|
|
|
2002-09-08 01:08:12 +00:00
|
|
|
public:
|
2004-04-12 01:19:26 +00:00
|
|
|
List<T>() {
|
|
|
|
_anchor = new NodeBase;
|
|
|
|
_anchor->_prev = _anchor;
|
|
|
|
_anchor->_next = _anchor;
|
|
|
|
}
|
|
|
|
List<T>(const List<T>& list) {
|
|
|
|
_anchor = new NodeBase;
|
|
|
|
_anchor->_prev = _anchor;
|
|
|
|
_anchor->_next = _anchor;
|
|
|
|
|
|
|
|
insert(begin(), list.begin(), list.end());
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
2003-03-06 16:27:06 +00:00
|
|
|
|
2005-06-21 22:08:21 +00:00
|
|
|
#ifndef __SYMBIAN32__
|
|
|
|
~List<T>()
|
|
|
|
#else
|
|
|
|
~List()
|
|
|
|
#endif
|
|
|
|
{
|
2004-04-12 01:19:26 +00:00
|
|
|
clear();
|
|
|
|
delete _anchor;
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
2003-03-06 16:27:06 +00:00
|
|
|
|
2004-04-12 01:19:26 +00:00
|
|
|
void push_front(const T& element) {
|
|
|
|
insert(begin(), element);
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
2003-03-06 16:27:06 +00:00
|
|
|
|
2004-04-12 01:19:26 +00:00
|
|
|
void push_back(const T& element) {
|
|
|
|
insert(end(), element);
|
2002-10-15 15:32:32 +00:00
|
|
|
}
|
|
|
|
|
2004-04-09 15:10:23 +00:00
|
|
|
void insert(iterator pos, const T& element) {
|
2004-04-12 01:19:26 +00:00
|
|
|
NodeBase *newNode = new Node<T>(element);
|
2005-09-03 16:10:08 +00:00
|
|
|
|
2004-04-12 01:19:26 +00:00
|
|
|
newNode->_next = pos._node;
|
|
|
|
newNode->_prev = pos._node->_prev;
|
|
|
|
newNode->_prev->_next = newNode;
|
|
|
|
newNode->_next->_prev = newNode;
|
2004-03-15 02:45:40 +00:00
|
|
|
}
|
2003-03-06 16:27:06 +00:00
|
|
|
|
2004-04-12 01:19:26 +00:00
|
|
|
template <typename iterator2>
|
|
|
|
void insert(iterator pos, iterator2 first, iterator2 last) {
|
|
|
|
for (; first != last; ++first)
|
|
|
|
insert(pos, *first);
|
2004-04-09 15:10:23 +00:00
|
|
|
}
|
2003-03-06 16:27:06 +00:00
|
|
|
|
2004-04-12 01:19:26 +00:00
|
|
|
iterator erase(iterator pos) {
|
|
|
|
assert(pos != end());
|
|
|
|
|
|
|
|
NodeBase *next = pos._node->_next;
|
|
|
|
NodeBase *prev = pos._node->_prev;
|
|
|
|
Node<T> *node = static_cast<Node<T> *>(pos._node);
|
|
|
|
prev->_next = next;
|
|
|
|
next->_prev = prev;
|
|
|
|
delete node;
|
2004-12-12 21:38:11 +00:00
|
|
|
return iterator(next);
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2004-04-12 01:19:26 +00:00
|
|
|
iterator erase(iterator first, iterator last) {
|
2006-02-11 18:02:13 +00:00
|
|
|
while (first != last)
|
2004-04-12 01:19:26 +00:00
|
|
|
erase(first++);
|
2006-02-11 18:02:13 +00:00
|
|
|
|
2004-04-12 01:19:26 +00:00
|
|
|
return last;
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2004-12-14 20:57:23 +00:00
|
|
|
void remove(const T &val) {
|
|
|
|
iterator i = begin();
|
|
|
|
while (i != end())
|
|
|
|
if (val == i.operator*())
|
|
|
|
i = erase(i);
|
|
|
|
else
|
|
|
|
++i;
|
|
|
|
}
|
2004-04-12 01:19:26 +00:00
|
|
|
|
2002-09-08 01:08:12 +00:00
|
|
|
|
2004-04-09 15:10:23 +00:00
|
|
|
List<T>& operator =(const List<T>& list) {
|
2004-04-12 01:19:26 +00:00
|
|
|
if (this != &list) {
|
2004-12-12 21:54:33 +00:00
|
|
|
iterator i;
|
|
|
|
const_iterator j;
|
|
|
|
|
|
|
|
for (i = begin(), j = list.begin(); (i != end()) && (j != list.end()) ; ++i, ++j) {
|
|
|
|
static_cast<Node<T> *>(i._node)->_data = static_cast<Node<T> *>(j._node)->_data;
|
|
|
|
}
|
|
|
|
|
2005-05-08 21:49:52 +00:00
|
|
|
if (i == end())
|
2004-12-12 21:54:33 +00:00
|
|
|
insert(i, j, list.end());
|
|
|
|
else
|
|
|
|
erase(i, end());
|
2004-04-12 01:19:26 +00:00
|
|
|
}
|
2005-09-03 16:10:08 +00:00
|
|
|
|
2004-04-12 01:19:26 +00:00
|
|
|
return *this;
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2004-02-05 00:19:57 +00:00
|
|
|
uint size() const {
|
2004-04-12 01:19:26 +00:00
|
|
|
int n = 0;
|
2004-04-09 15:10:23 +00:00
|
|
|
for (const_iterator i = begin(); i != end(); ++i)
|
2004-04-12 01:19:26 +00:00
|
|
|
++n;
|
|
|
|
return n;
|
2003-03-06 16:27:06 +00:00
|
|
|
}
|
2002-09-08 01:08:12 +00:00
|
|
|
|
2003-03-06 16:27:06 +00:00
|
|
|
void clear() {
|
2004-04-09 15:10:23 +00:00
|
|
|
erase(begin(), end());
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
2005-09-03 16:10:08 +00:00
|
|
|
|
2006-03-28 09:42:54 +00:00
|
|
|
bool empty() const {
|
2004-04-12 01:19:26 +00:00
|
|
|
return (_anchor == _anchor->_next);
|
2003-03-06 16:27:06 +00:00
|
|
|
}
|
2002-09-08 01:08:12 +00:00
|
|
|
|
2003-10-05 14:02:28 +00:00
|
|
|
|
2004-02-05 00:19:57 +00:00
|
|
|
iterator begin() {
|
2004-12-12 21:38:11 +00:00
|
|
|
return iterator(_anchor->_next);
|
2003-10-05 14:02:28 +00:00
|
|
|
}
|
|
|
|
|
2004-02-05 00:19:57 +00:00
|
|
|
iterator end() {
|
2004-12-12 21:38:11 +00:00
|
|
|
return iterator(_anchor);
|
2003-10-05 14:02:28 +00:00
|
|
|
}
|
|
|
|
|
2004-02-05 00:19:57 +00:00
|
|
|
const_iterator begin() const {
|
2004-12-12 21:38:11 +00:00
|
|
|
return const_iterator(_anchor->_next);
|
2003-10-05 14:02:28 +00:00
|
|
|
}
|
|
|
|
|
2004-02-05 00:19:57 +00:00
|
|
|
const_iterator end() const {
|
2004-12-12 21:38:11 +00:00
|
|
|
return const_iterator(_anchor);
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-10-02 17:43:02 +00:00
|
|
|
} // End of namespace Common
|
2002-09-08 01:08:12 +00:00
|
|
|
|
|
|
|
#endif
|