rippled
Loading...
Searching...
No Matches
List.h
1#ifndef BEAST_INTRUSIVE_LIST_H_INCLUDED
2#define BEAST_INTRUSIVE_LIST_H_INCLUDED
3
4#include <iterator>
5
6namespace beast {
7
8template <typename, typename>
9class List;
10
11namespace detail {
12
15template <typename T, typename U>
17{
18 explicit CopyConst() = default;
19
21};
22
23template <typename T, typename U>
24struct CopyConst<T const, U>
25{
26 explicit CopyConst() = default;
27
28 using type = typename std::remove_const<U>::type const;
29};
32// This is the intrusive portion of the doubly linked list.
33// One derivation per list that the object may appear on
34// concurrently is required.
35//
36template <typename T, typename Tag>
38{
39private:
40 using value_type = T;
41
42 friend class List<T, Tag>;
43
44 template <typename>
45 friend class ListIterator;
46
49};
50
51//------------------------------------------------------------------------------
52
53template <typename N>
55{
56public:
58 using value_type =
64
65 ListIterator(N* node = nullptr) noexcept : m_node(node)
66 {
67 }
68
69 template <typename M>
70 ListIterator(ListIterator<M> const& other) noexcept : m_node(other.m_node)
71 {
72 }
73
74 template <typename M>
75 bool
76 operator==(ListIterator<M> const& other) const noexcept
77 {
78 return m_node == other.m_node;
79 }
80
81 template <typename M>
82 bool
83 operator!=(ListIterator<M> const& other) const noexcept
84 {
85 return !((*this) == other);
86 }
87
89 operator*() const noexcept
90 {
91 return dereference();
92 }
93
95 operator->() const noexcept
96 {
97 return &dereference();
98 }
99
101 operator++() noexcept
102 {
103 increment();
104 return *this;
105 }
106
108 operator++(int) noexcept
109 {
110 ListIterator result(*this);
111 increment();
112 return result;
113 }
114
116 operator--() noexcept
117 {
118 decrement();
119 return *this;
120 }
121
123 operator--(int) noexcept
124 {
125 ListIterator result(*this);
126 decrement();
127 return result;
128 }
129
130private:
132 dereference() const noexcept
133 {
134 return static_cast<reference>(*m_node);
135 }
136
137 void
138 increment() noexcept
139 {
140 m_node = m_node->m_next;
141 }
142
143 void
144 decrement() noexcept
145 {
146 m_node = m_node->m_prev;
147 }
148
150};
151
152} // namespace detail
153
258template <typename T, typename Tag = void>
259class List
260{
261public:
263
264 using value_type = T;
267 using const_pointer = value_type const*;
271
274
277 {
278 m_head.m_prev = nullptr; // identifies the head
279 m_tail.m_next = nullptr; // identifies the tail
280 clear();
281 }
282
283 List(List const&) = delete;
284 List&
285 operator=(List const&) = delete;
286
290 bool
291 empty() const noexcept
292 {
293 return size() == 0;
294 }
295
298 size() const noexcept
299 {
300 return m_size;
301 }
302
308 front() noexcept
309 {
310 return element_from(m_head.m_next);
311 }
312
318 front() const noexcept
319 {
320 return element_from(m_head.m_next);
321 }
322
328 back() noexcept
329 {
330 return element_from(m_tail.m_prev);
331 }
332
338 back() const noexcept
339 {
340 return element_from(m_tail.m_prev);
341 }
342
347 begin() noexcept
348 {
349 return iterator(m_head.m_next);
350 }
351
356 begin() const noexcept
357 {
358 return const_iterator(m_head.m_next);
359 }
360
365 cbegin() const noexcept
366 {
367 return const_iterator(m_head.m_next);
368 }
369
374 end() noexcept
375 {
376 return iterator(&m_tail);
377 }
378
383 end() const noexcept
384 {
385 return const_iterator(&m_tail);
386 }
387
392 cend() const noexcept
393 {
394 return const_iterator(&m_tail);
395 }
396
400 void
401 clear() noexcept
402 {
403 m_head.m_next = &m_tail;
404 m_tail.m_prev = &m_head;
405 m_size = 0;
406 }
407
415 insert(iterator pos, T& element) noexcept
416 {
417 Node* node = static_cast<Node*>(&element);
418 node->m_next = &*pos;
419 node->m_prev = node->m_next->m_prev;
420 node->m_next->m_prev = node;
421 node->m_prev->m_next = node;
422 ++m_size;
423 return iterator(node);
424 }
425
431 void
432 insert(iterator pos, List& other) noexcept
433 {
434 if (!other.empty())
435 {
436 Node* before = &*pos;
437 other.m_head.m_next->m_prev = before->m_prev;
438 before->m_prev->m_next = other.m_head.m_next;
439 other.m_tail.m_prev->m_next = before;
440 before->m_prev = other.m_tail.m_prev;
441 m_size += other.m_size;
442 other.clear();
443 }
444 }
445
452 erase(iterator pos) noexcept
453 {
454 Node* node = &*pos;
455 ++pos;
456 node->m_next->m_prev = node->m_prev;
457 node->m_prev->m_next = node->m_next;
458 --m_size;
459 return pos;
460 }
461
467 push_front(T& element) noexcept
468 {
469 return insert(begin(), element);
470 }
471
476 T&
477 pop_front() noexcept
478 {
479 T& element(front());
480 erase(begin());
481 return element;
482 }
483
489 push_back(T& element) noexcept
490 {
491 return insert(end(), element);
492 }
493
498 T&
499 pop_back() noexcept
500 {
501 T& element(back());
502 erase(--end());
503 return element;
504 }
505
507 void
508 swap(List& other) noexcept
509 {
510 List temp;
511 temp.append(other);
512 other.append(*this);
513 append(temp);
514 }
515
521 prepend(List& list) noexcept
522 {
523 return insert(begin(), list);
524 }
525
531 append(List& list) noexcept
532 {
533 return insert(end(), list);
534 }
535
542 iterator_to(T& element) const noexcept
543 {
544 return iterator(static_cast<Node*>(&element));
545 }
546
553 const_iterator_to(T const& element) const noexcept
554 {
555 return const_iterator(static_cast<Node const*>(&element));
556 }
557
558private:
560 element_from(Node* node) noexcept
561 {
562 return *(static_cast<pointer>(node));
563 }
564
566 element_from(Node const* node) const noexcept
567 {
568 return *(static_cast<const_pointer>(node));
569 }
570
571private:
575};
576
577} // namespace beast
578
579#endif
Intrusive doubly linked list.
Definition List.h:260
value_type const * const_pointer
Definition List.h:267
Node m_head
Definition List.h:573
iterator iterator_to(T &element) const noexcept
Obtain an iterator from an element.
Definition List.h:542
detail::ListIterator< Node const > const_iterator
Definition List.h:273
Node m_tail
Definition List.h:574
std::size_t size_type
Definition List.h:269
iterator push_back(T &element) noexcept
Append an element at the end of the list.
Definition List.h:489
const_iterator begin() const noexcept
Obtain a const iterator to the beginning of the list.
Definition List.h:356
List & operator=(List const &)=delete
const_iterator cend() const noexcept
Obtain a const iterator to the end of the list.
Definition List.h:392
reference front() noexcept
Obtain a reference to the first element.
Definition List.h:308
void clear() noexcept
Clear the list.
Definition List.h:401
void insert(iterator pos, List &other) noexcept
Insert another list into this one.
Definition List.h:432
iterator begin() noexcept
Obtain an iterator to the beginning of the list.
Definition List.h:347
const_reference back() const noexcept
Obtain a const reference to the last element.
Definition List.h:338
detail::ListIterator< Node > iterator
Definition List.h:272
iterator insert(iterator pos, T &element) noexcept
Insert an element.
Definition List.h:415
iterator append(List &list) noexcept
Append another list at the end of this list.
Definition List.h:531
T value_type
Definition List.h:264
iterator end() noexcept
Obtain a iterator to the end of the list.
Definition List.h:374
const_reference element_from(Node const *node) const noexcept
Definition List.h:566
value_type * pointer
Definition List.h:265
const_reference front() const noexcept
Obtain a const reference to the first element.
Definition List.h:318
reference back() noexcept
Obtain a reference to the last element.
Definition List.h:328
const_iterator end() const noexcept
Obtain a const iterator to the end of the list.
Definition List.h:383
T & pop_back() noexcept
Remove the element at the end of the list.
Definition List.h:499
typename detail::ListNode< T, Tag > Node
Definition List.h:262
bool empty() const noexcept
Determine if the list is empty.
Definition List.h:291
void swap(List &other) noexcept
Swap contents with another list.
Definition List.h:508
value_type & reference
Definition List.h:266
const_iterator const_iterator_to(T const &element) const noexcept
Obtain a const iterator from an element.
Definition List.h:553
List(List const &)=delete
T & pop_front() noexcept
Remove the element at the beginning of the list.
Definition List.h:477
iterator push_front(T &element) noexcept
Insert an element at the beginning of the list.
Definition List.h:467
value_type const & const_reference
Definition List.h:268
size_type size() const noexcept
Returns the number of elements in the list.
Definition List.h:298
const_iterator cbegin() const noexcept
Obtain a const iterator to the beginning of the list.
Definition List.h:365
List()
Create an empty list.
Definition List.h:276
reference element_from(Node *node) noexcept
Definition List.h:560
iterator erase(iterator pos) noexcept
Remove an element.
Definition List.h:452
iterator prepend(List &list) noexcept
Insert another list at the beginning of this list.
Definition List.h:521
size_type m_size
Definition List.h:572
ListIterator & operator++() noexcept
Definition List.h:101
ListIterator & operator--() noexcept
Definition List.h:116
value_type * pointer
Definition List.h:61
reference operator*() const noexcept
Definition List.h:89
ListIterator operator++(int) noexcept
Definition List.h:108
typename beast::detail::CopyConst< N, typename N::value_type >::type value_type
Definition List.h:59
void decrement() noexcept
Definition List.h:144
reference dereference() const noexcept
Definition List.h:132
ListIterator operator--(int) noexcept
Definition List.h:123
bool operator!=(ListIterator< M > const &other) const noexcept
Definition List.h:83
value_type & reference
Definition List.h:62
pointer operator->() const noexcept
Definition List.h:95
bool operator==(ListIterator< M > const &other) const noexcept
Definition List.h:76
void increment() noexcept
Definition List.h:138
ListIterator(ListIterator< M > const &other) noexcept
Definition List.h:70
ListIterator(N *node=nullptr) noexcept
Definition List.h:65
ListNode * m_next
Definition List.h:47
ListNode * m_prev
Definition List.h:48
typename std::remove_const< U >::type const type
Definition List.h:28
Copy const attribute from T to U if present.
Definition List.h:17
typename std::remove_const< U >::type type
Definition List.h:20