rippled
Loading...
Searching...
No Matches
List.h
1#pragma once
2
3#include <iterator>
4
5namespace beast {
6
7template <typename, typename>
8class List;
9
10namespace detail {
11
14template <typename T, typename U>
16{
17 explicit CopyConst() = default;
18
20};
21
22template <typename T, typename U>
23struct CopyConst<T const, U>
24{
25 explicit CopyConst() = default;
26
27 using type = typename std::remove_const<U>::type const;
28};
31// This is the intrusive portion of the doubly linked list.
32// One derivation per list that the object may appear on
33// concurrently is required.
34//
35template <typename T, typename Tag>
37{
38private:
39 using value_type = T;
40
41 friend class List<T, Tag>;
42
43 template <typename>
44 friend class ListIterator;
45
48};
49
50//------------------------------------------------------------------------------
51
52template <typename N>
54{
55public:
62
63 ListIterator(N* node = nullptr) noexcept : m_node(node)
64 {
65 }
66
67 template <typename M>
68 ListIterator(ListIterator<M> const& other) noexcept : m_node(other.m_node)
69 {
70 }
71
72 template <typename M>
73 bool
74 operator==(ListIterator<M> const& other) const noexcept
75 {
76 return m_node == other.m_node;
77 }
78
79 template <typename M>
80 bool
81 operator!=(ListIterator<M> const& other) const noexcept
82 {
83 return !((*this) == other);
84 }
85
87 operator*() const noexcept
88 {
89 return dereference();
90 }
91
93 operator->() const noexcept
94 {
95 return &dereference();
96 }
97
99 operator++() noexcept
100 {
101 increment();
102 return *this;
103 }
104
106 operator++(int) noexcept
107 {
108 ListIterator result(*this);
109 increment();
110 return result;
111 }
112
114 operator--() noexcept
115 {
116 decrement();
117 return *this;
118 }
119
121 operator--(int) noexcept
122 {
123 ListIterator result(*this);
124 decrement();
125 return result;
126 }
127
128private:
130 dereference() const noexcept
131 {
132 return static_cast<reference>(*m_node);
133 }
134
135 void
136 increment() noexcept
137 {
138 m_node = m_node->m_next;
139 }
140
141 void
142 decrement() noexcept
143 {
144 m_node = m_node->m_prev;
145 }
146
148};
149
150} // namespace detail
151
256template <typename T, typename Tag = void>
257class List
258{
259public:
261
262 using value_type = T;
265 using const_pointer = value_type const*;
269
272
275 {
276 m_head.m_prev = nullptr; // identifies the head
277 m_tail.m_next = nullptr; // identifies the tail
278 clear();
279 }
280
281 List(List const&) = delete;
282 List&
283 operator=(List const&) = delete;
284
288 bool
289 empty() const noexcept
290 {
291 return size() == 0;
292 }
293
296 size() const noexcept
297 {
298 return m_size;
299 }
300
306 front() noexcept
307 {
308 return element_from(m_head.m_next);
309 }
310
316 front() const noexcept
317 {
318 return element_from(m_head.m_next);
319 }
320
326 back() noexcept
327 {
328 return element_from(m_tail.m_prev);
329 }
330
336 back() const noexcept
337 {
338 return element_from(m_tail.m_prev);
339 }
340
345 begin() noexcept
346 {
347 return iterator(m_head.m_next);
348 }
349
354 begin() const noexcept
355 {
356 return const_iterator(m_head.m_next);
357 }
358
363 cbegin() const noexcept
364 {
365 return const_iterator(m_head.m_next);
366 }
367
372 end() noexcept
373 {
374 return iterator(&m_tail);
375 }
376
381 end() const noexcept
382 {
383 return const_iterator(&m_tail);
384 }
385
390 cend() const noexcept
391 {
392 return const_iterator(&m_tail);
393 }
394
398 void
399 clear() noexcept
400 {
401 m_head.m_next = &m_tail;
402 m_tail.m_prev = &m_head;
403 m_size = 0;
404 }
405
413 insert(iterator pos, T& element) noexcept
414 {
415 Node* node = static_cast<Node*>(&element);
416 node->m_next = &*pos;
417 node->m_prev = node->m_next->m_prev;
418 node->m_next->m_prev = node;
419 node->m_prev->m_next = node;
420 ++m_size;
421 return iterator(node);
422 }
423
429 void
430 insert(iterator pos, List& other) noexcept
431 {
432 if (!other.empty())
433 {
434 Node* before = &*pos;
435 other.m_head.m_next->m_prev = before->m_prev;
436 before->m_prev->m_next = other.m_head.m_next;
437 other.m_tail.m_prev->m_next = before;
438 before->m_prev = other.m_tail.m_prev;
439 m_size += other.m_size;
440 other.clear();
441 }
442 }
443
450 erase(iterator pos) noexcept
451 {
452 Node* node = &*pos;
453 ++pos;
454 node->m_next->m_prev = node->m_prev;
455 node->m_prev->m_next = node->m_next;
456 --m_size;
457 return pos;
458 }
459
465 push_front(T& element) noexcept
466 {
467 return insert(begin(), element);
468 }
469
474 T&
475 pop_front() noexcept
476 {
477 T& element(front());
478 erase(begin());
479 return element;
480 }
481
487 push_back(T& element) noexcept
488 {
489 return insert(end(), element);
490 }
491
496 T&
497 pop_back() noexcept
498 {
499 T& element(back());
500 erase(--end());
501 return element;
502 }
503
505 void
506 swap(List& other) noexcept
507 {
508 List temp;
509 temp.append(other);
510 other.append(*this);
511 append(temp);
512 }
513
519 prepend(List& list) noexcept
520 {
521 return insert(begin(), list);
522 }
523
529 append(List& list) noexcept
530 {
531 return insert(end(), list);
532 }
533
540 iterator_to(T& element) const noexcept
541 {
542 return iterator(static_cast<Node*>(&element));
543 }
544
551 const_iterator_to(T const& element) const noexcept
552 {
553 return const_iterator(static_cast<Node const*>(&element));
554 }
555
556private:
558 element_from(Node* node) noexcept
559 {
560 return *(static_cast<pointer>(node));
561 }
562
564 element_from(Node const* node) const noexcept
565 {
566 return *(static_cast<const_pointer>(node));
567 }
568
569private:
573};
574
575} // namespace beast
Intrusive doubly linked list.
Definition List.h:258
value_type const * const_pointer
Definition List.h:265
Node m_head
Definition List.h:571
iterator iterator_to(T &element) const noexcept
Obtain an iterator from an element.
Definition List.h:540
detail::ListIterator< Node const > const_iterator
Definition List.h:271
Node m_tail
Definition List.h:572
std::size_t size_type
Definition List.h:267
iterator push_back(T &element) noexcept
Append an element at the end of the list.
Definition List.h:487
const_iterator begin() const noexcept
Obtain a const iterator to the beginning of the list.
Definition List.h:354
List & operator=(List const &)=delete
const_iterator cend() const noexcept
Obtain a const iterator to the end of the list.
Definition List.h:390
reference front() noexcept
Obtain a reference to the first element.
Definition List.h:306
void clear() noexcept
Clear the list.
Definition List.h:399
void insert(iterator pos, List &other) noexcept
Insert another list into this one.
Definition List.h:430
iterator begin() noexcept
Obtain an iterator to the beginning of the list.
Definition List.h:345
const_reference back() const noexcept
Obtain a const reference to the last element.
Definition List.h:336
detail::ListIterator< Node > iterator
Definition List.h:270
iterator insert(iterator pos, T &element) noexcept
Insert an element.
Definition List.h:413
iterator append(List &list) noexcept
Append another list at the end of this list.
Definition List.h:529
T value_type
Definition List.h:262
iterator end() noexcept
Obtain a iterator to the end of the list.
Definition List.h:372
const_reference element_from(Node const *node) const noexcept
Definition List.h:564
value_type * pointer
Definition List.h:263
const_reference front() const noexcept
Obtain a const reference to the first element.
Definition List.h:316
reference back() noexcept
Obtain a reference to the last element.
Definition List.h:326
const_iterator end() const noexcept
Obtain a const iterator to the end of the list.
Definition List.h:381
T & pop_back() noexcept
Remove the element at the end of the list.
Definition List.h:497
typename detail::ListNode< T, Tag > Node
Definition List.h:260
bool empty() const noexcept
Determine if the list is empty.
Definition List.h:289
void swap(List &other) noexcept
Swap contents with another list.
Definition List.h:506
value_type & reference
Definition List.h:264
const_iterator const_iterator_to(T const &element) const noexcept
Obtain a const iterator from an element.
Definition List.h:551
List(List const &)=delete
T & pop_front() noexcept
Remove the element at the beginning of the list.
Definition List.h:475
iterator push_front(T &element) noexcept
Insert an element at the beginning of the list.
Definition List.h:465
value_type const & const_reference
Definition List.h:266
size_type size() const noexcept
Returns the number of elements in the list.
Definition List.h:296
const_iterator cbegin() const noexcept
Obtain a const iterator to the beginning of the list.
Definition List.h:363
List()
Create an empty list.
Definition List.h:274
reference element_from(Node *node) noexcept
Definition List.h:558
iterator erase(iterator pos) noexcept
Remove an element.
Definition List.h:450
iterator prepend(List &list) noexcept
Insert another list at the beginning of this list.
Definition List.h:519
size_type m_size
Definition List.h:570
ListIterator & operator++() noexcept
Definition List.h:99
ListIterator & operator--() noexcept
Definition List.h:114
value_type * pointer
Definition List.h:59
reference operator*() const noexcept
Definition List.h:87
ListIterator operator++(int) noexcept
Definition List.h:106
typename beast::detail::CopyConst< N, typename N::value_type >::type value_type
Definition List.h:57
void decrement() noexcept
Definition List.h:142
reference dereference() const noexcept
Definition List.h:130
ListIterator operator--(int) noexcept
Definition List.h:121
bool operator!=(ListIterator< M > const &other) const noexcept
Definition List.h:81
value_type & reference
Definition List.h:60
pointer operator->() const noexcept
Definition List.h:93
bool operator==(ListIterator< M > const &other) const noexcept
Definition List.h:74
void increment() noexcept
Definition List.h:136
ListIterator(ListIterator< M > const &other) noexcept
Definition List.h:68
ListIterator(N *node=nullptr) noexcept
Definition List.h:63
ListNode * m_next
Definition List.h:46
ListNode * m_prev
Definition List.h:47
typename std::remove_const< U >::type const type
Definition List.h:27
Copy const attribute from T to U if present.
Definition List.h:16
typename std::remove_const< U >::type type
Definition List.h:19