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
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COMMON_LIST_H
|
|
|
|
#define COMMON_LIST_H
|
|
|
|
|
2009-03-09 22:26:02 +00:00
|
|
|
#include "common/list_intern.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
|
|
|
*/
|
2009-03-09 22:26:02 +00:00
|
|
|
template<typename t_T>
|
2002-09-08 01:08:12 +00:00
|
|
|
class List {
|
|
|
|
protected:
|
2009-03-09 22:26:02 +00:00
|
|
|
typedef ListInternal::NodeBase NodeBase;
|
|
|
|
typedef ListInternal::Node<t_T> 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:
|
2009-03-09 22:26:02 +00:00
|
|
|
typedef ListInternal::Iterator<t_T> iterator;
|
|
|
|
typedef ListInternal::ConstIterator<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;
|
2012-02-22 19:10:04 +00:00
|
|
|
typedef uint size_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
|
|
|
}
|
2008-05-03 23:02:05 +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
|
|
|
|
2009-04-27 14:25:16 +00:00
|
|
|
/**
|
|
|
|
* Inserts element before pos.
|
|
|
|
*/
|
2008-05-03 23:02:05 +00:00
|
|
|
void insert(iterator pos, const t_T &element) {
|
2009-04-27 14:25:16 +00:00
|
|
|
insert(pos._node, element);
|
2004-03-15 02:45:40 +00:00
|
|
|
}
|
2003-03-06 16:27:06 +00:00
|
|
|
|
2009-04-27 14:25:16 +00:00
|
|
|
/**
|
|
|
|
* Inserts the elements from first to last before pos.
|
|
|
|
*/
|
2008-05-03 23:02:05 +00:00
|
|
|
template<typename iterator2>
|
2004-04-12 01:19:26 +00:00
|
|
|
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
|
|
|
|
2009-04-27 14:25:16 +00:00
|
|
|
/**
|
|
|
|
* Deletes the element at location pos and returns an iterator pointing
|
|
|
|
* to the element after the one which was deleted.
|
|
|
|
*/
|
2004-04-12 01:19:26 +00:00
|
|
|
iterator erase(iterator pos) {
|
|
|
|
assert(pos != end());
|
2009-04-27 14:25:16 +00:00
|
|
|
return iterator(erase(pos._node)._next);
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 14:25:16 +00:00
|
|
|
/**
|
|
|
|
* Deletes the element at location pos and returns an iterator pointing
|
|
|
|
* to the element before the one which was deleted.
|
|
|
|
*/
|
2006-05-26 17:18:23 +00:00
|
|
|
iterator reverse_erase(iterator pos) {
|
|
|
|
assert(pos != end());
|
2009-04-27 14:25:16 +00:00
|
|
|
return iterator(erase(pos._node)._prev);
|
2006-05-26 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 14:25:16 +00:00
|
|
|
/**
|
|
|
|
* Deletes the elements between first and last (including first but not
|
|
|
|
* last) and returns an iterator pointing to the element after the one
|
|
|
|
* which was deleted (i.e., last).
|
|
|
|
*/
|
2004-04-12 01:19:26 +00:00
|
|
|
iterator erase(iterator first, iterator last) {
|
2009-04-27 14:25:16 +00:00
|
|
|
NodeBase *f = first._node;
|
|
|
|
NodeBase *l = last._node;
|
|
|
|
while (f != l)
|
|
|
|
f = erase(f)._next;
|
2004-04-12 01:19:26 +00:00
|
|
|
return last;
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 14:25:16 +00:00
|
|
|
/**
|
|
|
|
* Removes all elements that are equal to val from the list.
|
|
|
|
*/
|
2007-07-30 10:18:25 +00:00
|
|
|
void remove(const t_T &val) {
|
2009-04-27 14:25:16 +00:00
|
|
|
NodeBase *i = _anchor._next;
|
|
|
|
while (i != &_anchor)
|
|
|
|
if (val == static_cast<Node *>(i)->_data)
|
|
|
|
i = erase(i)._next;
|
2004-12-14 20:57:23 +00:00
|
|
|
else
|
2009-04-27 14:25:16 +00:00
|
|
|
i = i->_next;
|
2004-12-14 20:57:23 +00:00
|
|
|
}
|
2004-04-12 01:19:26 +00:00
|
|
|
|
2009-04-27 14:25:16 +00:00
|
|
|
/** Inserts element at the start of the list. */
|
2009-04-27 14:23:20 +00:00
|
|
|
void push_front(const t_T &element) {
|
2009-04-27 14:25:16 +00:00
|
|
|
insert(_anchor._next, element);
|
2009-04-27 14:23:20 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 14:25:16 +00:00
|
|
|
/** Appends element to the end of the list. */
|
2009-04-27 14:23:20 +00:00
|
|
|
void push_back(const t_T &element) {
|
2009-04-27 14:25:16 +00:00
|
|
|
insert(&_anchor, element);
|
2009-04-27 14:23:20 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 14:25:16 +00:00
|
|
|
/** Removes the first element of the list. */
|
2008-07-09 02:27:05 +00:00
|
|
|
void pop_front() {
|
2009-04-27 14:25:16 +00:00
|
|
|
assert(!empty());
|
|
|
|
erase(_anchor._next);
|
2009-04-27 14:23:20 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 14:25:16 +00:00
|
|
|
/** Removes the last element of the list. */
|
2009-04-27 14:23:20 +00:00
|
|
|
void pop_back() {
|
2009-04-27 14:25:16 +00:00
|
|
|
assert(!empty());
|
|
|
|
erase(_anchor._prev);
|
2008-07-09 02:27:05 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 14:25:16 +00:00
|
|
|
/** Returns a reference to the first element of the list. */
|
2009-04-27 11:11:42 +00:00
|
|
|
t_T &front() {
|
|
|
|
return static_cast<Node *>(_anchor._next)->_data;
|
|
|
|
}
|
|
|
|
|
2009-04-27 14:25:16 +00:00
|
|
|
/** Returns a reference to the first element of the list. */
|
2009-04-27 11:11:42 +00:00
|
|
|
const t_T &front() const {
|
|
|
|
return static_cast<Node *>(_anchor._next)->_data;
|
|
|
|
}
|
|
|
|
|
2009-04-27 14:25:16 +00:00
|
|
|
/** Returns a reference to the last element of the list. */
|
2009-04-27 11:11:42 +00:00
|
|
|
t_T &back() {
|
|
|
|
return static_cast<Node *>(_anchor._prev)->_data;
|
|
|
|
}
|
|
|
|
|
2009-04-27 14:25:16 +00:00
|
|
|
/** Returns a reference to the last element of the list. */
|
2009-04-27 11:11:42 +00:00
|
|
|
const t_T &back() const {
|
|
|
|
return static_cast<Node *>(_anchor._prev)->_data;
|
|
|
|
}
|
|
|
|
|
2008-05-03 23:02:05 +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;
|
2009-04-11 00:29:34 +00:00
|
|
|
const iterator e = end();
|
|
|
|
const_iterator i2;
|
|
|
|
const_iterator e2 = list.end();
|
2004-12-12 21:54:33 +00:00
|
|
|
|
2009-04-11 00:29:34 +00:00
|
|
|
for (i = begin(), i2 = list.begin(); (i != e) && (i2 != e2) ; ++i, ++i2) {
|
|
|
|
static_cast<Node *>(i._node)->_data = static_cast<const Node *>(i2._node)->_data;
|
2004-12-12 21:54:33 +00:00
|
|
|
}
|
|
|
|
|
2009-04-11 00:29:34 +00:00
|
|
|
if (i == e)
|
|
|
|
insert(i, i2, e2);
|
2004-12-12 21:54:33 +00:00
|
|
|
else
|
2009-04-11 00:29:34 +00:00
|
|
|
erase(i, e);
|
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
|
|
|
}
|
|
|
|
|
2012-02-22 19:10:04 +00:00
|
|
|
size_type size() const {
|
|
|
|
size_type n = 0;
|
2009-04-11 00:29:34 +00:00
|
|
|
for (const NodeBase *cur = _anchor._next; cur != &_anchor; cur = cur->_next)
|
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() {
|
2009-04-28 10:23:26 +00:00
|
|
|
NodeBase *pos = _anchor._next;
|
|
|
|
while (pos != &_anchor) {
|
|
|
|
Node *node = static_cast<Node *>(pos);
|
|
|
|
pos = pos->_next;
|
|
|
|
delete node;
|
|
|
|
}
|
|
|
|
|
|
|
|
_anchor._prev = &_anchor;
|
|
|
|
_anchor._next = &_anchor;
|
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 {
|
2012-02-15 06:06:13 +00:00
|
|
|
return const_iterator(const_cast<NodeBase *>(&_anchor));
|
2002-09-08 01:08:12 +00:00
|
|
|
}
|
2009-04-27 14:25:16 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
NodeBase erase(NodeBase *pos) {
|
|
|
|
NodeBase n = *pos;
|
|
|
|
Node *node = static_cast<Node *>(pos);
|
|
|
|
n._prev->_next = n._next;
|
|
|
|
n._next->_prev = n._prev;
|
|
|
|
delete node;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts element before pos.
|
|
|
|
*/
|
|
|
|
void insert(NodeBase *pos, const t_T &element) {
|
|
|
|
ListInternal::NodeBase *newNode = new Node(element);
|
2009-05-03 22:45:31 +00:00
|
|
|
assert(newNode);
|
2009-04-27 14:25:16 +00:00
|
|
|
|
|
|
|
newNode->_next = pos;
|
|
|
|
newNode->_prev = pos->_prev;
|
|
|
|
newNode->_prev->_next = newNode;
|
|
|
|
newNode->_next->_prev = newNode;
|
|
|
|
}
|
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
|