2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
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-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
|
|
|
/**
|
2007-02-28 14:07:47 +00:00
|
|
|
* Simple double linked list, modeled after the list template of the standard
|
2008-01-27 19:47:41 +00:00
|
|
|
* C++ library.
|
2004-04-12 01:19:26 +00:00
|
|
|
*/
|
2007-07-30 10:18:25 +00:00
|
|
|
template <class t_T>
|
2002-09-08 01:08:12 +00:00
|
|
|
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;
|
|
|
|
};
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-30 10:18:25 +00:00
|
|
|
template <class t_T2>
|
2004-04-12 01:19:26 +00:00
|
|
|
struct Node : public NodeBase {
|
2007-07-30 10:18:25 +00:00
|
|
|
t_T2 _data;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-07-30 10:18:25 +00:00
|
|
|
Node(const t_T2 &x) : _data(x) {}
|
2004-04-12 01:19:26 +00:00
|
|
|
};
|
|
|
|
|
2007-07-30 10:18:25 +00:00
|
|
|
template <class t_T2>
|
2004-12-12 21:38:11 +00:00
|
|
|
class Iterator {
|
2008-04-02 02:15:47 +00:00
|
|
|
template<class T> friend class Iterator;
|
2007-07-30 10:18:25 +00:00
|
|
|
friend class List<t_T>;
|
2004-04-12 01:19:26 +00:00
|
|
|
NodeBase *_node;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2006-10-28 14:40:43 +00:00
|
|
|
#if !defined (__WINSCW__)
|
2006-10-13 20:18:27 +00:00
|
|
|
explicit Iterator(NodeBase *node) : _node(node) {}
|
2005-09-03 16:10:08 +00:00
|
|
|
#else
|
2006-10-13 20:18:27 +00:00
|
|
|
Iterator(NodeBase *node) : _node(node) {}
|
2005-09-03 16:10:08 +00:00
|
|
|
#endif
|
|
|
|
|
2004-12-12 21:38:11 +00:00
|
|
|
public:
|
2006-10-13 20:18:27 +00:00
|
|
|
Iterator() : _node(0) {}
|
2008-04-02 02:15:00 +00:00
|
|
|
template<class T>
|
|
|
|
Iterator(const Iterator<T> &c) : _node(c._node) {}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
Iterator<t_T2> &operator =(const Iterator<T> &c) {
|
|
|
|
_node = c._node;
|
|
|
|
return *this;
|
|
|
|
}
|
2004-04-12 01:19:26 +00:00
|
|
|
|
|
|
|
// Prefix inc
|
2007-07-30 10:18:25 +00:00
|
|
|
Iterator<t_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
|
2007-07-30 10:18:25 +00:00
|
|
|
Iterator<t_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
|
2007-07-30 10:18:25 +00:00
|
|
|
Iterator<t_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
|
2007-07-30 10:18:25 +00:00
|
|
|
Iterator<t_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
|
|
|
}
|
2008-04-02 02:15:00 +00:00
|
|
|
t_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)
|
2007-07-30 10:18:25 +00:00
|
|
|
return static_cast<List<t_T>::Node<t_T2> *>(_node)->_data;
|
2006-01-29 03:44:13 +00:00
|
|
|
#else
|
2007-07-30 10:18:25 +00:00
|
|
|
return static_cast<Node<t_T2>*>(_node)->_data;
|
2006-01-29 03:44:13 +00:00
|
|
|
#endif
|
2004-04-09 15:10:23 +00:00
|
|
|
}
|
2008-04-02 02:15:00 +00:00
|
|
|
t_T2 *operator->() const {
|
2004-04-12 01:19:26 +00:00
|
|
|
return &(operator*());
|
|
|
|
}
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2008-04-02 02:15:00 +00:00
|
|
|
template<class T>
|
|
|
|
bool operator==(const Iterator<T> &x) const {
|
2004-04-12 01:19:26 +00:00
|
|
|
return _node == x._node;
|
|
|
|
}
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2008-04-02 02:15:00 +00:00
|
|
|
template<class T>
|
|
|
|
bool operator!=(const Iterator<T> &x) const {
|
2004-04-12 01:19:26 +00:00
|
|
|
return _node != x._node;
|
2004-04-09 15:10:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-03-30 06:37:46 +00:00
|
|
|
NodeBase _anchor;
|
2002-09-08 01:08:12 +00:00
|
|
|
|
2003-10-05 14:02:28 +00:00
|
|
|
public:
|
2007-07-30 10:18:25 +00:00
|
|
|
typedef Iterator<t_T> iterator;
|
|
|
|
typedef Iterator<const t_T> const_iterator;
|
2007-05-23 12:02:31 +00:00
|
|
|
|
2007-07-30 10:18:25 +00:00
|
|
|
typedef t_T value_type;
|
2003-10-05 14:02:28 +00:00
|
|
|
|
2002-09-08 01:08:12 +00:00
|
|
|
public:
|
2006-10-13 20:18:27 +00:00
|
|
|
List() {
|
2008-03-30 06:37:46 +00:00
|
|
|
_anchor._prev = &_anchor;
|
|
|
|
_anchor._next = &_anchor;
|
2004-04-12 01:19:26 +00:00
|
|
|
}
|
2007-07-30 10:18:25 +00:00
|
|
|
List(const List<t_T>& list) {
|
2008-03-30 06:37:46 +00:00
|
|
|
_anchor._prev = &_anchor;
|
|
|
|
_anchor._next = &_anchor;
|
2004-04-12 01:19:26 +00:00
|
|
|
|
|
|
|
insert(begin(), list.begin(), list.end());
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
2003-03-06 16:27:06 +00:00
|
|
|
|
2006-10-13 20:18:27 +00:00
|
|
|
~List() {
|
2004-04-12 01:19:26 +00:00
|
|
|
clear();
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
2003-03-06 16:27:06 +00:00
|
|
|
|
2007-07-30 10:18:25 +00:00
|
|
|
void push_front(const t_T& element) {
|
2004-04-12 01:19:26 +00:00
|
|
|
insert(begin(), element);
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
2003-03-06 16:27:06 +00:00
|
|
|
|
2007-07-30 10:18:25 +00:00
|
|
|
void push_back(const t_T& element) {
|
2004-04-12 01:19:26 +00:00
|
|
|
insert(end(), element);
|
2002-10-15 15:32:32 +00:00
|
|
|
}
|
|
|
|
|
2007-07-30 10:18:25 +00:00
|
|
|
void insert(iterator pos, const t_T& element) {
|
|
|
|
NodeBase *newNode = new Node<t_T>(element);
|
2008-01-27 19:47:41 +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;
|
2007-07-30 10:18:25 +00:00
|
|
|
Node<t_T> *node = static_cast<Node<t_T> *>(pos._node);
|
2004-04-12 01:19:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-05-26 17:18:23 +00:00
|
|
|
iterator reverse_erase(iterator pos) {
|
|
|
|
assert(pos != end());
|
|
|
|
|
|
|
|
NodeBase *next = pos._node->_next;
|
|
|
|
NodeBase *prev = pos._node->_prev;
|
2007-07-30 10:18:25 +00:00
|
|
|
Node<t_T> *node = static_cast<Node<t_T> *>(pos._node);
|
2006-05-26 17:18:23 +00:00
|
|
|
prev->_next = next;
|
|
|
|
next->_prev = prev;
|
|
|
|
delete node;
|
|
|
|
return iterator(prev);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-07-30 10:18:25 +00:00
|
|
|
void remove(const t_T &val) {
|
2004-12-14 20:57:23 +00:00
|
|
|
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
|
|
|
|
2007-07-30 10:18:25 +00:00
|
|
|
List<t_T>& operator =(const List<t_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) {
|
2007-07-30 10:18:25 +00:00
|
|
|
static_cast<Node<t_T> *>(i._node)->_data = static_cast<Node<t_T> *>(j._node)->_data;
|
2004-12-12 21:54:33 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2008-01-27 19:47:41 +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
|
|
|
}
|
2008-01-27 19:47:41 +00:00
|
|
|
|
|
|
|
bool empty() const {
|
2008-03-30 06:37:46 +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() {
|
2008-03-30 06:37:46 +00:00
|
|
|
return iterator(_anchor._next);
|
2003-10-05 14:02:28 +00:00
|
|
|
}
|
|
|
|
|
2006-05-26 17:18:23 +00:00
|
|
|
iterator reverse_begin() {
|
2008-03-30 06:37:46 +00:00
|
|
|
return iterator(_anchor._prev);
|
2006-05-26 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
2004-02-05 00:19:57 +00:00
|
|
|
iterator end() {
|
2008-03-30 06:37:46 +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 {
|
2008-03-30 06:37:46 +00:00
|
|
|
return const_iterator(_anchor._next);
|
2003-10-05 14:02:28 +00:00
|
|
|
}
|
|
|
|
|
2006-05-26 17:18:23 +00:00
|
|
|
const_iterator reverse_begin() const {
|
2008-03-30 06:37:46 +00:00
|
|
|
return const_iterator(_anchor._prev);
|
2006-05-26 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
2004-02-05 00:19:57 +00:00
|
|
|
const_iterator end() const {
|
2008-03-30 06:37:46 +00:00
|
|
|
return const_iterator(const_cast<NodeBase*>(&_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
|