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  iterator insert(iterator pos, const t_T &element) {
78  insert(pos._node, element);
79  return --pos;
80  }
81 
85  template<typename iterator2>
86  void insert(iterator pos, iterator2 first, iterator2 last) {
87  for (; first != last; ++first)
88  insert(pos, *first);
89  }
90 
95  iterator erase(iterator pos) {
96  assert(pos != end());
97  return iterator(erase(pos._node)._next);
98  }
99 
104  iterator reverse_erase(iterator pos) {
105  assert(pos != end());
106  return iterator(erase(pos._node)._prev);
107  }
108 
114  iterator erase(iterator first, iterator last) {
115  NodeBase *f = first._node;
116  NodeBase *l = last._node;
117  while (f != l)
118  f = erase(f)._next;
119  return last;
120  }
121 
125  void remove(const t_T &val) {
126  NodeBase *i = _anchor._next;
127  while (i != &_anchor)
128  if (val == static_cast<Node *>(i)->_data)
129  i = erase(i)._next;
130  else
131  i = i->_next;
132  }
133 
135  void push_front(const t_T &element) {
136  insert(_anchor._next, element);
137  }
138 
140  void push_back(const t_T &element) {
141  insert(&_anchor, element);
142  }
143 
145  void pop_front() {
146  assert(!empty());
147  erase(_anchor._next);
148  }
149 
151  void pop_back() {
152  assert(!empty());
153  erase(_anchor._prev);
154  }
155 
157  t_T &front() {
158  return static_cast<Node *>(_anchor._next)->_data;
159  }
160 
162  const t_T &front() const {
163  return static_cast<Node *>(_anchor._next)->_data;
164  }
165 
167  t_T &back() {
168  return static_cast<Node *>(_anchor._prev)->_data;
169  }
170 
172  const t_T &back() const {
173  return static_cast<Node *>(_anchor._prev)->_data;
174  }
175 
177  List<t_T> &operator=(const List<t_T> &list) {
178  if (this != &list) {
179  iterator i;
180  const iterator e = end();
181  const_iterator i2;
182  const_iterator e2 = list.end();
183 
184  for (i = begin(), i2 = list.begin(); (i != e) && (i2 != e2); ++i, ++i2) {
185  static_cast<Node *>(i._node)->_data = static_cast<const Node *>(i2._node)->_data;
186  }
187 
188  if (i == e)
189  insert(i, i2, e2);
190  else
191  erase(i, e);
192  }
193 
194  return *this;
195  }
196 
198  size_type size() const {
199  size_type n = 0;
200  for (const NodeBase *cur = _anchor._next; cur != &_anchor; cur = cur->_next)
201  ++n;
202  return n;
203  }
204 
206  void clear() {
207  NodeBase *pos = _anchor._next;
208  while (pos != &_anchor) {
209  Node *node = static_cast<Node *>(pos);
210  pos = pos->_next;
211  delete node;
212  }
213 
214  _anchor._prev = &_anchor;
215  _anchor._next = &_anchor;
216  }
217 
219  bool empty() const {
220  return (&_anchor == _anchor._next);
221  }
222 
227  iterator begin() {
228  return iterator(_anchor._next);
229  }
230 
235  iterator reverse_begin() {
236  return iterator(_anchor._prev);
237  }
238 
240  iterator end() {
241  return iterator(&_anchor);
242  }
243 
248  const_iterator begin() const {
249  return const_iterator(_anchor._next);
250  }
251 
256  const_iterator reverse_begin() const {
257  return const_iterator(_anchor._prev);
258  }
259 
261  const_iterator end() const {
262  return const_iterator(const_cast<NodeBase *>(&_anchor));
263  }
264 
265 protected:
269  NodeBase erase(NodeBase *pos) {
270  NodeBase n = *pos;
271  Node *node = static_cast<Node *>(pos);
272  n._prev->_next = n._next;
273  n._next->_prev = n._prev;
274  delete node;
275  return n;
276  }
277 
281  void insert(NodeBase *pos, const t_T &element) {
282  ListInternal::NodeBase *newNode = new Node(element);
283  assert(newNode);
284 
285  newNode->_next = pos;
286  newNode->_prev = pos->_prev;
287  newNode->_prev->_next = newNode;
288  newNode->_next->_prev = newNode;
289  }
290 };
291 
294 } // End of namespace Common
295 
296 #endif
void pop_front()
Definition: list.h:145
constexpr List()
Definition: list.h:62
iterator insert(iterator pos, const t_T &element)
Definition: list.h:77
Definition: list_intern.h:33
uint size_type
Definition: list.h:56
NodeBase erase(NodeBase *pos)
Definition: list.h:269
const_iterator begin() const
Definition: list.h:248
const_iterator reverse_begin() const
Definition: list.h:256
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:261
size_type size() const
Definition: list.h:198
void push_front(const t_T &element)
Definition: list.h:135
List(const List< t_T > &list)
Definition: list.h:63
t_T & back()
Definition: list.h:167
ListInternal::NodeBase NodeBase
Definition: list.h:46
void insert(NodeBase *pos, const t_T &element)
Definition: list.h:281
void insert(iterator pos, iterator2 first, iterator2 last)
Definition: list.h:86
iterator end()
Definition: list.h:240
iterator erase(iterator first, iterator last)
Definition: list.h:114
iterator begin()
Definition: list.h:227
Definition: algorithm.h:29
ListInternal::Iterator< t_T > iterator
Definition: list.h:52
void clear()
Definition: list.h:206
ListInternal::ConstIterator< t_T > const_iterator
Definition: list.h:53
const t_T & front() const
Definition: list.h:162
t_T & front()
Definition: list.h:157
List< t_T > & operator=(const List< t_T > &list)
Definition: list.h:177
Definition: list_intern.h:48
iterator reverse_begin()
Definition: list.h:235
Definition: list_intern.h:51
iterator erase(iterator pos)
Definition: list.h:95
iterator reverse_erase(iterator pos)
Definition: list.h:104
void push_back(const t_T &element)
Definition: list.h:140
void pop_back()
Definition: list.h:151
bool empty() const
Definition: list.h:219
Definition: list_intern.h:42
ListInternal::Node< t_T > Node
Definition: list.h:47
const t_T & back() const
Definition: list.h:172