CppCommon  1.0.4.1
C++ Common Library
list.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_CONTAINERS_LIST_H
10 #define CPPCOMMON_CONTAINERS_LIST_H
11 
12 #include <cassert>
13 #include <cstddef>
14 #include <iterator>
15 
16 namespace CppCommon {
17 
18 template <typename T>
19 class ListIterator;
20 template <typename T>
21 class ListConstIterator;
22 template <typename T>
23 class ListReverseIterator;
24 template <typename T>
25 class ListConstReverseIterator;
26 
27 
29 
154 template <typename T>
155 class List
156 {
157 public:
158  // Standard container type definitions
159  typedef T value_type;
161  typedef const value_type& const_reference;
162  typedef value_type* pointer;
163  typedef const value_type* const_pointer;
164  typedef ptrdiff_t difference_type;
165  typedef size_t size_type;
170 
172  struct Node
173  {
174  T* next;
175  T* prev;
176 
177  Node() : next(nullptr), prev(nullptr) {}
178  };
179 
180  List() noexcept : _size(0), _front(nullptr), _back(nullptr) {}
181  template <class InputIterator>
182  List(InputIterator first, InputIterator last) noexcept;
183  List(const List&) noexcept = default;
184  List(List&&) noexcept = default;
185  ~List() noexcept = default;
186 
187  List& operator=(const List&) noexcept = default;
188  List& operator=(List&&) noexcept = default;
189 
191  explicit operator bool() const noexcept { return !empty(); }
192 
194  bool empty() const noexcept { return _front == nullptr; }
195 
197  size_t size() const noexcept { return _size; }
198 
200  T* front() noexcept { return _front; }
201  const T* front() const noexcept { return _front; }
203  T* back() noexcept { return _back; }
204  const T* back() const noexcept { return _back; }
205 
207  iterator begin() noexcept;
208  const_iterator begin() const noexcept;
209  const_iterator cbegin() const noexcept;
211  iterator end() noexcept;
212  const_iterator end() const noexcept;
213  const_iterator cend() const noexcept;
214 
216  reverse_iterator rbegin() noexcept;
217  const_reverse_iterator rbegin() const noexcept;
218  const_reverse_iterator crbegin() const noexcept;
220  reverse_iterator rend() noexcept;
221  const_reverse_iterator rend() const noexcept;
222  const_reverse_iterator crend() const noexcept;
223 
225 
228  void push_front(T& item) noexcept;
230 
233  void push_back(T& item) noexcept;
235 
239  void push_next(T& base, T& item) noexcept;
241 
245  void push_prev(T& base, T& item) noexcept;
246 
248 
251  T* pop_front() noexcept;
253 
256  T* pop_back() noexcept;
258 
262  T* pop_current(T& base) noexcept;
264 
268  T* pop_next(T& base) noexcept;
270 
274  T* pop_prev(T& base) noexcept;
275 
277  void reverse() noexcept;
278 
280  void clear() noexcept;
281 
283  void swap(List& list) noexcept;
284  template <typename U>
285  friend void swap(List<U>& list1, List<U>& list2) noexcept;
286 
287 private:
288  size_t _size; // List size
289  T* _front; // List front node
290  T* _back; // List back node
291 };
292 
294 
297 template <typename T>
299 {
300  friend ListConstIterator<T>;
301 
302 public:
303  // Standard iterator type definitions
304  typedef T value_type;
306  typedef const value_type& const_reference;
307  typedef value_type* pointer;
308  typedef const value_type* const_pointer;
309  typedef ptrdiff_t difference_type;
310  typedef size_t size_type;
311  typedef std::bidirectional_iterator_tag iterator_category;
312 
313  ListIterator() noexcept : _node(nullptr) {}
314  explicit ListIterator(T* node) noexcept : _node(node) {}
315  ListIterator(const ListIterator& it) noexcept = default;
316  ListIterator(ListIterator&& it) noexcept = default;
317  ~ListIterator() noexcept = default;
318 
319  ListIterator& operator=(const ListIterator& it) noexcept = default;
320  ListIterator& operator=(ListIterator&& it) noexcept = default;
321 
322  friend bool operator==(const ListIterator& it1, const ListIterator& it2) noexcept
323  { return it1._node == it2._node; }
324  friend bool operator!=(const ListIterator& it1, const ListIterator& it2) noexcept
325  { return it1._node != it2._node; }
326 
327  ListIterator& operator++() noexcept;
328  ListIterator operator++(int) noexcept;
329 
330  reference operator*() noexcept;
331  pointer operator->() noexcept;
332 
334  explicit operator bool() const noexcept { return _node != nullptr; }
335 
337  void swap(ListIterator& it) noexcept;
338  template <typename U>
339  friend void swap(ListIterator<U>& it1, ListIterator<U>& it2) noexcept;
340 
341 private:
342  T* _node;
343 };
344 
346 
349 template <typename T>
351 {
352 public:
353  // Standard iterator type definitions
354  typedef T value_type;
356  typedef const value_type& const_reference;
357  typedef value_type* pointer;
358  typedef const value_type* const_pointer;
359  typedef ptrdiff_t difference_type;
360  typedef size_t size_type;
361  typedef std::bidirectional_iterator_tag iterator_category;
362 
363  ListConstIterator() noexcept : _node(nullptr) {}
364  explicit ListConstIterator(const T* node) noexcept : _node(node) {}
365  ListConstIterator(const ListIterator<T>& it) noexcept : _node(it._node) {}
366  ListConstIterator(const ListConstIterator& it) noexcept = default;
367  ListConstIterator(ListConstIterator&& it) noexcept = default;
368  ~ListConstIterator() noexcept = default;
369 
370  ListConstIterator& operator=(const ListIterator<T>& it) noexcept
371  { _node = it._node; return *this; }
372  ListConstIterator& operator=(const ListConstIterator& it) noexcept = default;
373  ListConstIterator& operator=(ListConstIterator&& it) noexcept = default;
374 
375  friend bool operator==(const ListConstIterator& it1, const ListConstIterator& it2) noexcept
376  { return it1._node == it2._node; }
377  friend bool operator!=(const ListConstIterator& it1, const ListConstIterator& it2) noexcept
378  { return it1._node != it2._node; }
379 
380  ListConstIterator& operator++() noexcept;
381  ListConstIterator operator++(int) noexcept;
382 
383  const_reference operator*() const noexcept;
384  const_pointer operator->() const noexcept;
385 
387  explicit operator bool() const noexcept { return _node != nullptr; }
388 
390  void swap(ListConstIterator& it) noexcept;
391  template <typename U>
392  friend void swap(ListConstIterator<U>& it1, ListConstIterator<U>& it2) noexcept;
393 
394 private:
395  const T* _node;
396 };
397 
399 
402 template <typename T>
404 {
406 
407 public:
408  // Standard iterator type definitions
409  typedef T value_type;
411  typedef const value_type& const_reference;
412  typedef value_type* pointer;
413  typedef const value_type* const_pointer;
414  typedef ptrdiff_t difference_type;
415  typedef size_t size_type;
416  typedef std::bidirectional_iterator_tag iterator_category;
417 
418  ListReverseIterator() noexcept : _node(nullptr) {}
419  explicit ListReverseIterator(T* node) noexcept : _node(node) {}
420  ListReverseIterator(const ListReverseIterator& it) noexcept = default;
421  ListReverseIterator(ListReverseIterator&& it) noexcept = default;
422  ~ListReverseIterator() noexcept = default;
423 
424  ListReverseIterator& operator=(const ListReverseIterator& it) noexcept = default;
425  ListReverseIterator& operator=(ListReverseIterator&& it) noexcept = default;
426 
427  friend bool operator==(const ListReverseIterator& it1, const ListReverseIterator& it2) noexcept
428  { return it1._node == it2._node; }
429  friend bool operator!=(const ListReverseIterator& it1, const ListReverseIterator& it2) noexcept
430  { return it1._node != it2._node; }
431 
432  ListReverseIterator& operator++() noexcept;
433  ListReverseIterator operator++(int) noexcept;
434 
435  reference operator*() noexcept;
436  pointer operator->() noexcept;
437 
439  explicit operator bool() const noexcept { return _node != nullptr; }
440 
442  void swap(ListReverseIterator& it) noexcept;
443  template <typename U>
444  friend void swap(ListReverseIterator<U>& it1, ListReverseIterator<U>& it2) noexcept;
445 
446 private:
447  T* _node;
448 };
449 
451 
454 template <typename T>
456 {
457 public:
458  // Standard iterator type definitions
459  typedef T value_type;
461  typedef const value_type& const_reference;
462  typedef value_type* pointer;
463  typedef const value_type* const_pointer;
464  typedef ptrdiff_t difference_type;
465  typedef size_t size_type;
466  typedef std::bidirectional_iterator_tag iterator_category;
467 
468  ListConstReverseIterator() noexcept : _node(nullptr) {}
469  explicit ListConstReverseIterator(const T* node) noexcept : _node(node) {}
470  ListConstReverseIterator(const ListReverseIterator<T>& it) noexcept : _node(it._node) {}
471  ListConstReverseIterator(const ListConstReverseIterator& it) noexcept = default;
473  ~ListConstReverseIterator() noexcept = default;
474 
475  ListConstReverseIterator& operator=(const ListReverseIterator<T>& it) noexcept
476  { _node = it._node; return *this; }
479 
480  friend bool operator==(const ListConstReverseIterator& it1, const ListConstReverseIterator& it2) noexcept
481  { return it1._node == it2._node; }
482  friend bool operator!=(const ListConstReverseIterator& it1, const ListConstReverseIterator& it2) noexcept
483  { return it1._node != it2._node; }
484 
485  ListConstReverseIterator& operator++() noexcept;
486  ListConstReverseIterator operator++(int) noexcept;
487 
488  const_reference operator*() const noexcept;
489  const_pointer operator->() const noexcept;
490 
492  explicit operator bool() const noexcept { return _node != nullptr; }
493 
495  void swap(ListConstReverseIterator& it) noexcept;
496  template <typename U>
498 
499 private:
500  const T* _node;
501 };
502 
505 } // namespace CppCommon
506 
507 #include "list.inl"
508 
509 #endif // CPPCOMMON_CONTAINERS_LIST_H
Intrusive list constant iterator.
Definition: list.h:351
friend bool operator!=(const ListConstIterator &it1, const ListConstIterator &it2) noexcept
Definition: list.h:377
~ListConstIterator() noexcept=default
ListConstIterator & operator=(ListConstIterator &&it) noexcept=default
const value_type & const_reference
Definition: list.h:356
friend void swap(ListConstIterator< U > &it1, ListConstIterator< U > &it2) noexcept
const value_type * const_pointer
Definition: list.h:358
ListConstIterator(const ListIterator< T > &it) noexcept
Definition: list.h:365
value_type * pointer
Definition: list.h:357
ListConstIterator(const T *node) noexcept
Definition: list.h:364
ListConstIterator(const ListConstIterator &it) noexcept=default
std::bidirectional_iterator_tag iterator_category
Definition: list.h:361
ptrdiff_t difference_type
Definition: list.h:359
friend bool operator==(const ListConstIterator &it1, const ListConstIterator &it2) noexcept
Definition: list.h:375
ListConstIterator(ListConstIterator &&it) noexcept=default
value_type & reference
Definition: list.h:355
ListConstIterator() noexcept
Definition: list.h:363
ListConstIterator & operator=(const ListConstIterator &it) noexcept=default
Intrusive list constant reverse iterator.
Definition: list.h:456
~ListConstReverseIterator() noexcept=default
ListConstReverseIterator(const ListConstReverseIterator &it) noexcept=default
const value_type * const_pointer
Definition: list.h:463
ListConstReverseIterator(ListConstReverseIterator &&it) noexcept=default
friend bool operator!=(const ListConstReverseIterator &it1, const ListConstReverseIterator &it2) noexcept
Definition: list.h:482
ListConstReverseIterator & operator=(const ListConstReverseIterator &it) noexcept=default
friend bool operator==(const ListConstReverseIterator &it1, const ListConstReverseIterator &it2) noexcept
Definition: list.h:480
std::bidirectional_iterator_tag iterator_category
Definition: list.h:466
ListConstReverseIterator & operator=(ListConstReverseIterator &&it) noexcept=default
ListConstReverseIterator(const T *node) noexcept
Definition: list.h:469
friend void swap(ListConstReverseIterator< U > &it1, ListConstReverseIterator< U > &it2) noexcept
const value_type & const_reference
Definition: list.h:461
ListConstReverseIterator(const ListReverseIterator< T > &it) noexcept
Definition: list.h:470
Intrusive list container.
Definition: list.h:156
const_reverse_iterator crbegin() const noexcept
Definition: list.inl:68
const value_type * const_pointer
Definition: list.h:163
ListReverseIterator< T > reverse_iterator
Definition: list.h:168
const_reverse_iterator crend() const noexcept
Definition: list.inl:86
void push_next(T &base, T &item) noexcept
Push a new item as a next to the given one.
Definition: list.inl:118
iterator end() noexcept
Get the end list iterator.
Definition: list.inl:38
ptrdiff_t difference_type
Definition: list.h:164
const T * back() const noexcept
Definition: list.h:204
const T * front() const noexcept
Definition: list.h:201
size_t size() const noexcept
Get the list size.
Definition: list.h:197
List() noexcept
Definition: list.h:180
T * front() noexcept
Get the front list item.
Definition: list.h:200
bool empty() const noexcept
Is the list empty?
Definition: list.h:194
void push_back(T &item) noexcept
Push a new item into the back of the list.
Definition: list.inl:105
value_type & reference
Definition: list.h:160
ListConstIterator< T > const_iterator
Definition: list.h:167
reverse_iterator rend() noexcept
Get the reverse end list iterator.
Definition: list.inl:74
reverse_iterator rbegin() noexcept
Get the reverse begin list iterator.
Definition: list.inl:56
value_type * pointer
Definition: list.h:162
ListConstReverseIterator< T > const_reverse_iterator
Definition: list.h:169
const_iterator cend() const noexcept
Definition: list.inl:50
T * pop_back() noexcept
Pop the item from the back of the list.
Definition: list.inl:162
List(const List &) noexcept=default
void push_prev(T &base, T &item) noexcept
Push a new item as a previous to the given one.
Definition: list.inl:131
T * pop_front() noexcept
Pop the item from the front of the list.
Definition: list.inl:144
ListIterator< T > iterator
Definition: list.h:166
void swap(List &list) noexcept
Swap two instances.
Definition: list.inl:261
iterator begin() noexcept
Get the begin list iterator.
Definition: list.inl:20
T * back() noexcept
Get the back list item.
Definition: list.h:203
const_iterator cbegin() const noexcept
Definition: list.inl:32
T * pop_current(T &base) noexcept
Pop the given item from the list.
Definition: list.inl:180
T * pop_prev(T &base) noexcept
Pop the previous item of the given one from the list.
Definition: list.inl:216
void push_front(T &item) noexcept
Push a new item into the front of the list.
Definition: list.inl:92
void clear() noexcept
Clear the list.
Definition: list.inl:253
void reverse() noexcept
Reverse the list.
Definition: list.inl:234
T * pop_next(T &base) noexcept
Pop the next item of the given one from the list.
Definition: list.inl:198
List(List &&) noexcept=default
const value_type & const_reference
Definition: list.h:161
size_t size_type
Definition: list.h:165
Intrusive list iterator.
Definition: list.h:299
value_type & reference
Definition: list.h:305
ptrdiff_t difference_type
Definition: list.h:309
friend bool operator!=(const ListIterator &it1, const ListIterator &it2) noexcept
Definition: list.h:324
const value_type * const_pointer
Definition: list.h:308
std::bidirectional_iterator_tag iterator_category
Definition: list.h:311
value_type * pointer
Definition: list.h:307
~ListIterator() noexcept=default
friend void swap(ListIterator< U > &it1, ListIterator< U > &it2) noexcept
const value_type & const_reference
Definition: list.h:306
ListIterator(const ListIterator &it) noexcept=default
ListIterator() noexcept
Definition: list.h:313
ListIterator(ListIterator &&it) noexcept=default
ListIterator(T *node) noexcept
Definition: list.h:314
Intrusive list reverse iterator.
Definition: list.h:404
ListReverseIterator() noexcept
Definition: list.h:418
value_type & reference
Definition: list.h:410
ListReverseIterator(T *node) noexcept
Definition: list.h:419
ListReverseIterator(const ListReverseIterator &it) noexcept=default
const value_type & const_reference
Definition: list.h:411
friend void swap(ListReverseIterator< U > &it1, ListReverseIterator< U > &it2) noexcept
~ListReverseIterator() noexcept=default
ListReverseIterator(ListReverseIterator &&it) noexcept=default
const value_type * const_pointer
Definition: list.h:413
friend bool operator!=(const ListReverseIterator &it1, const ListReverseIterator &it2) noexcept
Definition: list.h:429
std::bidirectional_iterator_tag iterator_category
Definition: list.h:416
Intrusive list container inline implementation.
C++ Common project definitions.
Definition: token_bucket.h:15
List node.
Definition: list.h:173
T * prev
Pointer to the previous list node.
Definition: list.h:175
T * next
Pointer to the next list node.
Definition: list.h:174