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
27namespace Common {
28
43template<typename t_T>
44class List {
45protected:
51public:
55 typedef t_T value_type;
56 typedef uint size_type;
58public:
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 template<class... TArgs>
78 iterator emplace(iterator pos, TArgs &&...args) {
79 emplace(pos._node, Common::forward<TArgs>(args)...);
80 return --pos;
81 }
82
86 iterator insert(iterator pos, const t_T &element) {
87 insert(pos._node, element);
88 return --pos;
89 }
90
94 iterator insert(iterator pos, t_T &&element) {
95 insert(pos._node, Common::move(element));
96 return --pos;
97 }
98
102 template<typename iterator2>
103 void insert(iterator pos, iterator2 first, iterator2 last) {
104 for (; first != last; ++first)
105 insert(pos, *first);
106 }
107
113 assert(pos != end());
114 return iterator(erase(pos._node)._next);
115 }
116
122 assert(pos != end());
123 return iterator(erase(pos._node)._prev);
124 }
125
132 NodeBase *f = first._node;
133 NodeBase *l = last._node;
134 while (f != l)
135 f = erase(f)._next;
136 return last;
137 }
138
142 void remove(const t_T &val) {
143 NodeBase *i = _anchor._next;
144 while (i != &_anchor)
145 if (val == static_cast<Node *>(i)->_data)
146 i = erase(i)._next;
147 else
148 i = i->_next;
149 }
150
152 template<class... TArgs>
153 void emplace_front(TArgs &&...args) {
154 emplace(_anchor._next, Common::forward<TArgs>(args)...);
155 }
156
158 void push_front(const t_T &element) {
159 insert(_anchor._next, element);
160 }
161
163 void push_front(t_T &&element) {
164 insert(_anchor._next, Common::move(element));
165 }
166
168 template<class... TArgs>
169 void emplace_back(TArgs &&...args) {
170 emplace(&_anchor, Common::forward<TArgs>(args)...);
171 }
172
174 void push_back(const t_T &element) {
175 insert(&_anchor, element);
176 }
177
179 void push_back(t_T &&element) {
180 insert(&_anchor, Common::move(element));
181 }
182
184 void pop_front() {
185 assert(!empty());
186 erase(_anchor._next);
187 }
188
190 void pop_back() {
191 assert(!empty());
192 erase(_anchor._prev);
193 }
194
196 t_T &front() {
197 return static_cast<Node *>(_anchor._next)->_data;
198 }
199
201 const t_T &front() const {
202 return static_cast<Node *>(_anchor._next)->_data;
203 }
204
206 t_T &back() {
207 return static_cast<Node *>(_anchor._prev)->_data;
208 }
209
211 const t_T &back() const {
212 return static_cast<Node *>(_anchor._prev)->_data;
213 }
214
217 if (this != &list) {
218 iterator i;
219 const iterator e = end();
221 const_iterator e2 = list.end();
222
223 for (i = begin(), i2 = list.begin(); (i != e) && (i2 != e2); ++i, ++i2) {
224 static_cast<Node *>(i._node)->_data = static_cast<const Node *>(i2._node)->_data;
225 }
226
227 if (i == e)
228 insert(i, i2, e2);
229 else
230 erase(i, e);
231 }
232
233 return *this;
234 }
235
237 size_type size() const {
238 size_type n = 0;
239 for (const NodeBase *cur = _anchor._next; cur != &_anchor; cur = cur->_next)
240 ++n;
241 return n;
242 }
243
245 void clear() {
246 NodeBase *pos = _anchor._next;
247 while (pos != &_anchor) {
248 Node *node = static_cast<Node *>(pos);
249 pos = pos->_next;
250 delete node;
251 }
252
253 _anchor._prev = &_anchor;
254 _anchor._next = &_anchor;
255 }
256
258 bool empty() const {
259 return (&_anchor == _anchor._next);
260 }
261
267 return iterator(_anchor._next);
268 }
269
275 return iterator(_anchor._prev);
276 }
277
280 return iterator(&_anchor);
281 }
282
288 return const_iterator(_anchor._next);
289 }
290
296 return const_iterator(_anchor._prev);
297 }
298
301 return const_iterator(const_cast<NodeBase *>(&_anchor));
302 }
303
304protected:
309 NodeBase n = *pos;
310 Node *node = static_cast<Node *>(pos);
311 n._prev->_next = n._next;
312 n._next->_prev = n._prev;
313 delete node;
314 return n;
315 }
316
320 template<class... TArgs>
321 void emplace(NodeBase *pos, TArgs&&... args) {
322 ListInternal::NodeBase *newNode = new Node(Common::forward<TArgs>(args)...);
323 assert(newNode);
324
325 newNode->_next = pos;
326 newNode->_prev = pos->_prev;
327 newNode->_prev->_next = newNode;
328 newNode->_next->_prev = newNode;
329 }
330
334 void insert(NodeBase *pos, const t_T &element) {
335 ListInternal::NodeBase *newNode = new Node(element);
336 assert(newNode);
337
338 newNode->_next = pos;
339 newNode->_prev = pos->_prev;
340 newNode->_prev->_next = newNode;
341 newNode->_next->_prev = newNode;
342 }
343
347 void insert(NodeBase *pos, t_T &&element) {
348 ListInternal::NodeBase *newNode = new Node(Common::move(element));
349 assert(newNode);
350
351 newNode->_next = pos;
352 newNode->_prev = pos->_prev;
353 newNode->_prev->_next = newNode;
354 newNode->_next->_prev = newNode;
355 }
356};
357
360} // End of namespace Common
361
362#endif
Definition: list.h:44
t_T & front()
Definition: list.h:196
uint size_type
Definition: list.h:56
iterator begin()
Definition: list.h:266
iterator insert(iterator pos, t_T &&element)
Definition: list.h:94
void push_front(const t_T &element)
Definition: list.h:158
void pop_front()
Definition: list.h:184
size_type size() const
Definition: list.h:237
void emplace_front(TArgs &&...args)
Definition: list.h:153
List< t_T > & operator=(const List< t_T > &list)
Definition: list.h:216
iterator reverse_begin()
Definition: list.h:274
ListInternal::Iterator< t_T > iterator
Definition: list.h:52
void clear()
Definition: list.h:245
ListInternal::ConstIterator< t_T > const_iterator
Definition: list.h:53
void emplace(NodeBase *pos, TArgs &&... args)
Definition: list.h:321
const t_T & front() const
Definition: list.h:201
NodeBase _anchor
Definition: list.h:49
void emplace_back(TArgs &&...args)
Definition: list.h:169
void insert(NodeBase *pos, t_T &&element)
Definition: list.h:347
const_iterator reverse_begin() const
Definition: list.h:295
void push_back(t_T &&element)
Definition: list.h:179
ListInternal::Node< t_T > Node
Definition: list.h:47
NodeBase erase(NodeBase *pos)
Definition: list.h:308
iterator insert(iterator pos, const t_T &element)
Definition: list.h:86
const t_T & back() const
Definition: list.h:211
ListInternal::NodeBase NodeBase
Definition: list.h:46
const_iterator end() const
Definition: list.h:300
constexpr List()
Definition: list.h:62
iterator erase(iterator pos)
Definition: list.h:112
void insert(NodeBase *pos, const t_T &element)
Definition: list.h:334
iterator emplace(iterator pos, TArgs &&...args)
Definition: list.h:78
t_T & back()
Definition: list.h:206
void push_back(const t_T &element)
Definition: list.h:174
iterator reverse_erase(iterator pos)
Definition: list.h:121
iterator end()
Definition: list.h:279
void pop_back()
Definition: list.h:190
iterator erase(iterator first, iterator last)
Definition: list.h:131
void remove(const t_T &val)
Definition: list.h:142
bool empty() const
Definition: list.h:258
void insert(iterator pos, iterator2 first, iterator2 last)
Definition: list.h:103
List(const List< t_T > &list)
Definition: list.h:63
const_iterator begin() const
Definition: list.h:287
void push_front(t_T &&element)
Definition: list.h:163
t_T value_type
Definition: list.h:55
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
Definition: algorithm.h:29
Definition: list_intern.h:108
Definition: list_intern.h:54
Definition: list_intern.h:33
Definition: list_intern.h:42