ScummVM API documentation
list.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef COMMON_LIST_H
23 #define COMMON_LIST_H
24 
25 #include "common/list_intern.h"
26 
27 namespace Common {
28 
43 template<typename t_T>
44 class List {
45 protected:
49  NodeBase _anchor;
51 public:
55  typedef t_T value_type;
56  typedef uint size_type;
58 public:
62  constexpr List() : _anchor(&_anchor, &_anchor) {}
63  List(const List<t_T> &list) {
64  _anchor._prev = &_anchor;
65  _anchor._next = &_anchor;
66 
67  insert(begin(), list.begin(), list.end());
68  }
69 
70  ~List() {
71  clear();
72  }
73 
77  void insert(iterator pos, const t_T &element) {
78  insert(pos._node, element);
79  }
80 
84  template<typename iterator2>
85  void insert(iterator pos, iterator2 first, iterator2 last) {
86  for (; first != last; ++first)
87  insert(pos, *first);
88  }
89 
94  iterator erase(iterator pos) {
95  assert(pos != end());
96  return iterator(erase(pos._node)._next);
97  }
98 
103  iterator reverse_erase(iterator pos) {
104  assert(pos != end());
105  return iterator(erase(pos._node)._prev);
106  }
107 
113  iterator erase(iterator first, iterator last) {
114  NodeBase *f = first._node;
115  NodeBase *l = last._node;
116  while (f != l)
117  f = erase(f)._next;
118  return last;
119  }
120 
124  void remove(const t_T &val) {
125  NodeBase *i = _anchor._next;
126  while (i != &_anchor)
127  if (val == static_cast<Node *>(i)->_data)
128  i = erase(i)._next;
129  else
130  i = i->_next;
131  }
132 
134  void push_front(const t_T &element) {
135  insert(_anchor._next, element);
136  }
137 
139  void push_back(const t_T &element) {
140  insert(&_anchor, element);
141  }
142 
144  void pop_front() {
145  assert(!empty());
146  erase(_anchor._next);
147  }
148 
150  void pop_back() {
151  assert(!empty());
152  erase(_anchor._prev);
153  }
154 
156  t_T &front() {
157  return static_cast<Node *>(_anchor._next)->_data;
158  }
159 
161  const t_T &front() const {
162  return static_cast<Node *>(_anchor._next)->_data;
163  }
164 
166  t_T &back() {
167  return static_cast<Node *>(_anchor._prev)->_data;
168  }
169 
171  const t_T &back() const {
172  return static_cast<Node *>(_anchor._prev)->_data;
173  }
174 
176  List<t_T> &operator=(const List<t_T> &list) {
177  if (this != &list) {
178  iterator i;
179  const iterator e = end();
180  const_iterator i2;
181  const_iterator e2 = list.end();
182 
183  for (i = begin(), i2 = list.begin(); (i != e) && (i2 != e2); ++i, ++i2) {
184  static_cast<Node *>(i._node)->_data = static_cast<const Node *>(i2._node)->_data;
185  }
186 
187  if (i == e)
188  insert(i, i2, e2);
189  else
190  erase(i, e);
191  }
192 
193  return *this;
194  }
195 
197  size_type size() const {
198  size_type n = 0;
199  for (const NodeBase *cur = _anchor._next; cur != &_anchor; cur = cur->_next)
200  ++n;
201  return n;
202  }
203 
205  void clear() {
206  NodeBase *pos = _anchor._next;
207  while (pos != &_anchor) {
208  Node *node = static_cast<Node *>(pos);
209  pos = pos->_next;
210  delete node;
211  }
212 
213  _anchor._prev = &_anchor;
214  _anchor._next = &_anchor;
215  }
216 
218  bool empty() const {
219  return (&_anchor == _anchor._next);
220  }
221 
226  iterator begin() {
227  return iterator(_anchor._next);
228  }
229 
234  iterator reverse_begin() {
235  return iterator(_anchor._prev);
236  }
237 
239  iterator end() {
240  return iterator(&_anchor);
241  }
242 
247  const_iterator begin() const {
248  return const_iterator(_anchor._next);
249  }
250 
255  const_iterator reverse_begin() const {
256  return const_iterator(_anchor._prev);
257  }
258 
260  const_iterator end() const {
261  return const_iterator(const_cast<NodeBase *>(&_anchor));
262  }
263 
264 protected:
268  NodeBase erase(NodeBase *pos) {
269  NodeBase n = *pos;
270  Node *node = static_cast<Node *>(pos);
271  n._prev->_next = n._next;
272  n._next->_prev = n._prev;
273  delete node;
274  return n;
275  }
276 
280  void insert(NodeBase *pos, const t_T &element) {
281  ListInternal::NodeBase *newNode = new Node(element);
282  assert(newNode);
283 
284  newNode->_next = pos;
285  newNode->_prev = pos->_prev;
286  newNode->_prev->_next = newNode;
287  newNode->_next->_prev = newNode;
288  }
289 };
290 
293 } // End of namespace Common
294 
295 #endif
void pop_front()
Definition: list.h:144
constexpr List()
Definition: list.h:62
Definition: list_intern.h:33
uint size_type
Definition: list.h:56
NodeBase erase(NodeBase *pos)
Definition: list.h:268
const_iterator begin() const
Definition: list.h:247
const_iterator reverse_begin() const
Definition: list.h:255
NodeBase _anchor
Definition: list.h:49
t_T value_type
Definition: list.h:55
Definition: list.h:44
const_iterator end() const
Definition: list.h:260
size_type size() const
Definition: list.h:197
void insert(iterator pos, const t_T &element)
Definition: list.h:77
void push_front(const t_T &element)
Definition: list.h:134
List(const List< t_T > &list)
Definition: list.h:63
t_T & back()
Definition: list.h:166
ListInternal::NodeBase NodeBase
Definition: list.h:46
void insert(NodeBase *pos, const t_T &element)
Definition: list.h:280
void insert(iterator pos, iterator2 first, iterator2 last)
Definition: list.h:85
iterator end()
Definition: list.h:239
iterator erase(iterator first, iterator last)
Definition: list.h:113
iterator begin()
Definition: list.h:226
Definition: algorithm.h:29
ListInternal::Iterator< t_T > iterator
Definition: list.h:52
void clear()
Definition: list.h:205
ListInternal::ConstIterator< t_T > const_iterator
Definition: list.h:53
const t_T & front() const
Definition: list.h:161
t_T & front()
Definition: list.h:156
List< t_T > & operator=(const List< t_T > &list)
Definition: list.h:176
Definition: list_intern.h:48
iterator reverse_begin()
Definition: list.h:234
Definition: list_intern.h:51
iterator erase(iterator pos)
Definition: list.h:94
iterator reverse_erase(iterator pos)
Definition: list.h:103
void push_back(const t_T &element)
Definition: list.h:139
void pop_back()
Definition: list.h:150
bool empty() const
Definition: list.h:218
Definition: list_intern.h:42
ListInternal::Node< t_T > Node
Definition: list.h:47
const t_T & back() const
Definition: list.h:171