Click
Loading...
Searching...
No Matches
list.hh
Go to the documentation of this file.
1#ifndef CLICK_LIST_HH
2#define CLICK_LIST_HH 1
3/*
4 * list.hh -- List template
5 * Eddie Kohler
6 *
7 * Copyright (c) 2008 Meraki, Inc.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software")
11 * to deal in the Software without restriction, subject to the conditions
12 * listed in the Click LICENSE file. These conditions include: you must
13 * preserve this copyright notice, and you cannot mention the copyright
14 * holders in advertising related to the Software without their permission.
15 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
16 * notice is a summary of the Click LICENSE file; the license in that file is
17 * legally binding.
18 */
19CLICK_DECLS
20#define LIST_HEAD_MARKER 0 /* ((T *) 1) */
21
26template <typename T> class List_member;
27template <typename T, List_member<T> T::*member> class List;
28
101template <typename T>
102class List_member { public:
103
106 : _next(), _prev() {
107 }
108
110 T *next() {
111 return _next;
112 }
114 const T *next() const {
115 return _next;
116 }
117
119 T *prev() {
120 return _prev != LIST_HEAD_MARKER ? _prev : 0;
121 }
123 const T *prev() const {
124 return _prev != LIST_HEAD_MARKER ? _prev : 0;
125 }
126
127 private:
128
129 T *_next;
130 T *_prev;
131
132 List_member(const List_member<T> &x);
133 List_member<T> &operator=(const List_member<T> &x);
134 template <typename X, List_member<X> X::*member> friend class List;
135
136};
137
138template <typename T, List_member<T> T::*member>
139class List { public:
140
141 typedef T *pointer;
142 typedef const T *const_pointer;
143 class const_iterator;
144 class iterator;
145 typedef size_t size_type;
146
149 : _head(0), _tail(0) {
150 }
151
152
155 return iterator(_head, this);
156 }
159 return const_iterator(_head, this);
160 }
164 return iterator(this);
165 }
168 return const_iterator(this);
169 }
170
171
174 bool empty() const {
175 return _head == 0;
176 }
177
180 size_type size() const {
181 size_type n = 0;
182 for (T *x = _head; x; x = (x->*member)._next)
183 ++n;
184 return n;
185 }
186
187
191 pointer front() {
192 return _head;
193 }
195 const_pointer front() const {
196 return _head;
197 }
201 pointer back() {
202 return _tail;
203 }
205 const_pointer back() const {
206 return _tail;
207 }
208
209
213 void push_front(pointer x) {
214 assert(x && isolated(x));
215 if (((x->*member)._next = _head))
216 (_head->*member)._prev = x;
217 else
218 _tail = x;
219 _head = x;
220 (_head->*member)._prev = LIST_HEAD_MARKER;
221 }
222
226 void push_back(pointer x) {
227 assert(x && !(x->*member)._next && !(x->*member)._prev);
228 if (((x->*member)._prev = _tail))
229 (_tail->*member)._next = x;
230 else {
231 _head = x;
232 (_head->*member)._prev = LIST_HEAD_MARKER;
233 }
234 _tail = x;
235 }
236
239 void pop_front() {
240 assert(_head);
241 pointer x = _head;
242 if ((_head = (x->*member)._next) != LIST_HEAD_MARKER)
243 (_head->*member)._prev = LIST_HEAD_MARKER;
244 else
245 _head = _tail = 0;
246 (x->*member)._next = (x->*member)._prev = 0;
247 }
248
251 void pop_back() {
252 assert(_head);
253 pointer x = _tail;
254 if ((_tail = (x->*member)._prev) != LIST_HEAD_MARKER)
255 (_tail->*member)._next = 0;
256 else
257 _head = _tail = 0;
258 (x->*member)._next = (x->*member)._prev = 0;
259 }
260
261
266 void insert(pointer pos, pointer x) {
267 assert(x && isolated(x));
268 T **pprev = (pos ? &(pos->*member)._prev : &_tail);
269 if (((x->*member)._prev = *pprev) != LIST_HEAD_MARKER)
270 ((x->*member)._prev->*member)._next = x;
271 else
272 _head = x;
273 *pprev = x;
274 (x->*member)._next = pos;
275 }
276
282 iterator insert(iterator it, pointer x) {
283 insert(it.get(), x);
284 return iterator(x);
285 }
286
292 template <typename InputIterator>
293 void insert(iterator it, InputIterator first, InputIterator last) {
294 while (first != last) {
295 insert(it, *first);
296 ++first;
297 }
298 }
299
300
304 void erase(pointer x) {
305 assert(x);
306 T *n = (x->*member)._next, *p = (x->*member)._prev;
307 if (n)
308 (n->*member)._prev = p;
309 else
310 _tail = (p != LIST_HEAD_MARKER ? p : 0);
311 if (p != LIST_HEAD_MARKER)
312 (p->*member)._next = n;
313 else
314 _head = n;
315 (x->*member)._next = (x->*member)._prev = 0;
316 }
317
323 assert(it);
324 iterator next = iterator((it.get()->*member)._next);
325 erase(it.get());
326 return next;
327 }
328
334 while (first != last)
335 first = erase(first);
336 return first;
337 }
338
341 void clear() {
342 while (T *x = _head) {
343 _head = (x->*member)._next;
344 (x->*member)._next = (x->*member)._prev = 0;
345 }
346 _tail = 0;
347 }
348
354 void __clear() {
355 _head = _tail = 0;
356 }
357
358
361 T *h = x._head, *t = x._tail;
362 x._head = _head, x._tail = _tail;
363 _head = h, _tail = t;
364 }
365
366
370 bool isolated(const_pointer x) {
371 return !(x->*member)._next && !(x->*member)._prev && x != _head;
372 }
373
375 bool contains(const_pointer x) const {
376 if (!isolated(x))
377 for (const_pointer *it = _head; it; it = (it->*member).next())
378 if (x == it)
379 return true;
380 return false;
381 }
382
383
386 class const_iterator { public:
389 : _x(), _list() {
390 }
392 const_iterator(const T *x)
393 : _x(const_cast<T *>(x)), _list() {
394 }
397 : _x(), _list(list) {
398 }
400 const_iterator(const T *x, const List<T, member> *list)
401 : _x(const_cast<T *>(x)), _list(list) {
402 }
403 typedef bool (const_iterator::*unspecified_bool_type)() const;
405 operator unspecified_bool_type() const {
406 return _x != 0 ? &const_iterator::live : 0;
407 }
409 bool live() const {
410 return _x != 0;
411 }
413 const T *get() const {
414 return _x;
415 }
417 const T *operator->() const {
418 return _x;
419 }
421 const T &operator*() const {
422 return *_x;
423 }
425 void operator++() {
426 assert(_x);
427 _x = (_x->*member).next();
428 }
430 void operator++(int) {
431 ++*this;
432 }
434 void operator--() {
435 assert(_x ? (bool) (_x->*member).prev() : _list && _list->back());
436 if (_x)
437 _x = (_x->*member).prev();
438 else
439 _x = const_cast<T *>(_list->back());
440 }
442 void operator--(int) {
443 --*this;
444 }
449 for (; x > 0; --x)
450 ++*this;
451 for (; x < 0; ++x)
452 --*this;
453 return *this;
454 }
459 for (; x > 0; --x)
460 --*this;
461 for (; x < 0; ++x)
462 ++*this;
463 return *this;
464 }
467 const_iterator it(*this);
468 return it += x;
469 }
472 const_iterator it(*this);
473 return it -= x;
474 }
477 return _x == x._x;
478 }
481 return _x != x._x;
482 }
483 private:
484 T *_x;
485 const List<T, member> *_list;
486 friend class iterator;
487 };
488
491 class iterator : public const_iterator { public:
494 : const_iterator() {
495 }
498 : const_iterator(x) {
499 }
502 : const_iterator(list) {
503 }
506 : const_iterator(x, list) {
507 }
509 T *get() const {
510 return this->_x;
511 }
513 T *operator->() const {
514 return this->_x;
515 }
517 T &operator*() const {
518 return *this->_x;
519 }
524 for (; x > 0; --x)
525 ++*this;
526 for (; x < 0; ++x)
527 --*this;
528 return *this;
529 }
534 for (; x > 0; --x)
535 --*this;
536 for (; x < 0; ++x)
537 ++*this;
538 return *this;
539 }
541 iterator operator+(int x) const {
542 iterator it(*this);
543 return it += x;
544 }
546 iterator operator-(int x) const {
547 iterator it(*this);
548 return it -= x;
549 }
550 };
551
552 private:
553
554 T *_head;
555 T *_tail;
556
557 List(const List<T, member> &x);
558 List<T, member> &operator=(const List<T, member> &x);
559
560};
561
562#undef LIST_HEAD_MARKER
563CLICK_ENDDECLS
564#endif /* CLICK_LIST_HH */
Const iterator type for List.
Definition list.hh:386
const_iterator & operator+=(int x)
Move this iterator forward by x positions.
Definition list.hh:448
void operator++(int)
Advance this iterator to the next element.
Definition list.hh:430
const_iterator & operator-=(int x)
Move this iterator backward by x positions.
Definition list.hh:458
const T & operator*() const
Return the current list element.
Definition list.hh:421
void operator--()
Advance this iterator to the previous element.
Definition list.hh:434
const_iterator()
Construct an invalid iterator.
Definition list.hh:388
const_iterator(const T *x, const List< T, member > *list)
Construct an iterator pointing at x in list.
Definition list.hh:400
const T * get() const
Return the current list element or null.
Definition list.hh:413
const T * operator->() const
Return the current list element or null.
Definition list.hh:417
bool operator!=(const_iterator x) const
Test if this iterator does not equal x.
Definition list.hh:480
const_iterator operator-(int x) const
Return an iterator x positions behind.
Definition list.hh:471
bool live() const
Test if this iterator points to a valid list element.
Definition list.hh:409
bool operator==(const_iterator x) const
Test if this iterator equals x.
Definition list.hh:476
void operator++()
Advance this iterator to the next element.
Definition list.hh:425
const_iterator(const T *x)
Construct an iterator pointing at x.
Definition list.hh:392
const_iterator(const List< T, member > *list)
Construct an end iterator for list.
Definition list.hh:396
const_iterator operator+(int x) const
Return an iterator x positions ahead.
Definition list.hh:466
void operator--(int)
Advance this iterator to the previous element.
Definition list.hh:442
Iterator type for List.
Definition list.hh:491
iterator operator-(int x) const
Return an iterator x positions behind.
Definition list.hh:546
iterator(T *x, List< T, member > *list)
Construct an iterator pointing at x in list.
Definition list.hh:505
T & operator*() const
Return the current list element.
Definition list.hh:517
iterator()
Construct an invalid iterator.
Definition list.hh:493
T * get() const
Return the current list element or null.
Definition list.hh:509
iterator(List< T, member > *list)
Construct an end iterator for list.
Definition list.hh:501
iterator(T *x)
Construct an iterator pointing at x.
Definition list.hh:497
iterator & operator+=(int x)
Move this iterator forward by x positions.
Definition list.hh:523
iterator & operator-=(int x)
Move this iterator backward by x positions.
Definition list.hh:533
T * operator->() const
Return the current list element or null.
Definition list.hh:513
iterator operator+(int x) const
Return an iterator x positions ahead.
Definition list.hh:541
Member of classes to be placed on a List.
Definition list.hh:102
const T * prev() const
Definition list.hh:123
T * next()
Return the next element in the list.
Definition list.hh:110
const T * next() const
Definition list.hh:114
T * prev()
Return the previous element in the list.
Definition list.hh:119
List_member()
Construct an isolated List_member.
Definition list.hh:105
Doubly-linked list template.
Definition list.hh:139
iterator end()
Return an iterator for the end of the list.
Definition list.hh:163
void pop_back()
Remove the element at the end of the list.
Definition list.hh:251
iterator insert(iterator it, pointer x)
Insert an element before it.
Definition list.hh:282
void push_front(pointer x)
Insert a new element at the beginning of the list.
Definition list.hh:213
const_iterator end() const
Definition list.hh:167
void push_back(pointer x)
Insert a new element at the end of the list.
Definition list.hh:226
void insert(pointer pos, pointer x)
Insert an element before pos.
Definition list.hh:266
bool contains(const_pointer x) const
Check if x is a member of this list.
Definition list.hh:375
void insert(iterator it, InputIterator first, InputIterator last)
Insert the elements in [first, last) before it.
Definition list.hh:293
void erase(pointer x)
Remove x from the list.
Definition list.hh:304
void pop_front()
Remove the element at the beginning of the list.
Definition list.hh:239
iterator erase(iterator first, iterator last)
Remove the elements in [first, last) from the list.
Definition list.hh:333
iterator erase(iterator it)
Remove the element pointed to by it from the list.
Definition list.hh:322
iterator begin()
Return an iterator for the first element in the list.
Definition list.hh:154
const_iterator begin() const
Definition list.hh:158
size_type size() const
Return the number of elements in the list.
Definition list.hh:180
bool isolated(const_pointer x)
Check if x is isolated.
Definition list.hh:370
void swap(List< T, member > &x)
Exchange list contents with list x.
Definition list.hh:360
pointer back()
Return the last element in the list.
Definition list.hh:201
void __clear()
Remove all elements from the list.
Definition list.hh:354
bool empty() const
Return true iff size() == 0.
Definition list.hh:174
List()
Construct an empty list.
Definition list.hh:148
void clear()
Remove all elements from the list.
Definition list.hh:341
const_pointer front() const
Definition list.hh:195
const_pointer back() const
Definition list.hh:205
pointer front()
Return the first element in the list.
Definition list.hh:191