xrpld
Loading...
Searching...
No Matches
List.h
1#pragma once
2
3#include <cstddef>
4#include <iterator>
5#include <type_traits>
6
7namespace beast {
8
9template <typename, typename>
10class List;
11
12namespace detail {
13
18template <typename T, typename U>
20{
21 explicit CopyConst() = default;
22
24};
25
26template <typename T, typename U>
27struct CopyConst<T const, U>
28{
29 explicit CopyConst() = default;
30
32};
33
34
35// This is the intrusive portion of the doubly linked list.
36// One derivation per list that the object may appear on
37// concurrently is required.
38//
39template <typename T, typename Tag>
41{
42 ListNode() = default;
43
44 using value_type = T;
45
46 friend T;
47 friend class List<T, Tag>;
48
49 template <typename>
50 friend class ListIterator;
51
52 ListNode* next_ = nullptr;
53 ListNode* prev_ = nullptr;
54};
55
56//------------------------------------------------------------------------------
57
58template <typename N>
60{
61public:
68
69 ListIterator(N* node = nullptr) noexcept : node_(node)
70 {
71 }
72
73 template <typename M>
74 ListIterator(ListIterator<M> const& other) noexcept : node_(other.node_)
75 {
76 }
77
78 template <typename M>
79 bool
80 operator==(ListIterator<M> const& other) const noexcept
81 {
82 return node_ == other.node_;
83 }
84
86 operator*() const noexcept
87 {
88 return dereference();
89 }
90
92 operator->() const noexcept
93 {
94 return &dereference();
95 }
96
98 operator++() noexcept
99 {
100 increment();
101 return *this;
102 }
103
105 operator++(int) noexcept
106 {
107 ListIterator result(*this);
108 increment();
109 return result;
110 }
111
113 operator--() noexcept
114 {
115 decrement();
116 return *this;
117 }
118
120 operator--(int) noexcept
121 {
122 ListIterator result(*this);
123 decrement();
124 return result;
125 }
126
127private:
128 [[nodiscard]] reference
129 dereference() const noexcept
130 {
131 return static_cast<reference>(*node_);
132 }
133
134 void
135 increment() noexcept
136 {
137 node_ = node_->next_;
138 }
139
140 void
141 decrement() noexcept
142 {
143 node_ = node_->prev_;
144 }
145
147};
148
149} // namespace detail
150
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
277 {
278 head_.prev_ = nullptr; // identifies the head
279 tail_.next_ = nullptr; // identifies the tail
280 clear();
281 }
282
283 List(List const&) = delete;
284 List&
285 operator=(List const&) = delete;
286
291 [[nodiscard]] bool
292 empty() const noexcept
293 {
294 return size() == 0;
295 }
296
300 [[nodiscard]] size_type
301 size() const noexcept
302 {
303 return size_;
304 }
305
312 front() noexcept
313 {
314 return element_from(head_.next_);
315 }
316
322 [[nodiscard]] const_reference
323 front() const noexcept
324 {
325 return element_from(head_.next_);
326 }
327
334 back() noexcept
335 {
336 return element_from(tail_.prev_);
337 }
338
344 [[nodiscard]] const_reference
345 back() const noexcept
346 {
347 return element_from(tail_.prev_);
348 }
349
355 begin() noexcept
356 {
357 return iterator(head_.next_);
358 }
359
364 [[nodiscard]] const_iterator
365 begin() const noexcept
366 {
367 return const_iterator(head_.next_);
368 }
369
374 [[nodiscard]] const_iterator
375 cbegin() const noexcept
376 {
377 return const_iterator(head_.next_);
378 }
379
385 end() noexcept
386 {
387 return iterator(&tail_);
388 }
389
394 [[nodiscard]] const_iterator
395 end() const noexcept
396 {
397 return const_iterator(&tail_);
398 }
399
404 [[nodiscard]] const_iterator
405 cend() const noexcept
406 {
407 return const_iterator(&tail_);
408 }
409
414 void
415 clear() noexcept
416 {
417 head_.next_ = &tail_;
418 tail_.prev_ = &head_;
419 size_ = 0;
420 }
421
430 insert(iterator pos, T& element) noexcept
431 {
432 Node* node = static_cast<Node*>(&element);
433 node->next_ = &*pos;
434 node->prev_ = node->next_->prev_;
435 node->next_->prev_ = node;
436 node->prev_->next_ = node;
437 ++size_;
438 return iterator(node);
439 }
440
447 void
448 insert(iterator pos, List& other) noexcept
449 {
450 if (!other.empty())
451 {
452 Node* before = &*pos;
453 other.head_.next_->prev_ = before->prev_;
454 before->prev_->next_ = other.head_.next_;
455 other.tail_.prev_->next_ = before;
456 before->prev_ = other.tail_.prev_;
457 size_ += other.size_;
458 other.clear();
459 }
460 }
461
469 erase(iterator pos) noexcept
470 {
471 Node const* node = &*pos;
472 ++pos;
473 node->next_->prev_ = node->prev_;
474 node->prev_->next_ = node->next_;
475 --size_;
476 return pos;
477 }
478
485 pushFront(T& element) noexcept
486 {
487 return insert(begin(), element);
488 }
489
495 T&
496 popFront() noexcept
497 {
498 T& element(front());
499 erase(begin());
500 return element;
501 }
502
509 pushBack(T& element) noexcept
510 {
511 return insert(end(), element);
512 }
513
519 T&
520 popBack() noexcept
521 {
522 T& element(back());
523 erase(--end());
524 return element;
525 }
526
530 void
531 swap(List& other) noexcept
532 {
533 List temp;
534 temp.append(other);
535 other.append(*this);
536 append(temp);
537 }
538
545 prepend(List& list) noexcept
546 {
547 return insert(begin(), list);
548 }
549
556 append(List& list) noexcept
557 {
558 return insert(end(), list);
559 }
560
568 iteratorTo(T& element) const noexcept
569 {
570 return iterator(static_cast<Node*>(&element));
571 }
572
579 [[nodiscard]] const_iterator
580 constIteratorTo(T const& element) const noexcept
581 {
582 return const_iterator(static_cast<Node const*>(&element));
583 }
584
585private:
587 elementFrom(Node* node) noexcept
588 {
589 return *(static_cast<pointer>(node));
590 }
591
593 elementFrom(Node const* node) const noexcept
594 {
595 return *(static_cast<const_pointer>(node));
596 }
597
598private:
602};
603
604} // namespace beast
Intrusive doubly linked list.
Definition List.h:258
value_type const * const_pointer
Definition List.h:265
detail::ListIterator< Node const > const_iterator
Definition List.h:271
std::size_t size_type
Definition List.h:267
const_iterator begin() const noexcept
Obtain a const iterator to the beginning of the list.
Definition List.h:365
List & operator=(List const &)=delete
const_iterator cend() const noexcept
Obtain a const iterator to the end of the list.
Definition List.h:405
reference front() noexcept
Obtain a reference to the first element.
Definition List.h:312
void clear() noexcept
Clear the list.
Definition List.h:415
void insert(iterator pos, List &other) noexcept
Insert another list into this one.
Definition List.h:448
iterator begin() noexcept
Obtain an iterator to the beginning of the list.
Definition List.h:355
const_reference back() const noexcept
Obtain a const reference to the last element.
Definition List.h:345
detail::ListIterator< Node > iterator
Definition List.h:270
iterator iteratorTo(T &element) const noexcept
Obtain an iterator from an element.
Definition List.h:568
iterator insert(iterator pos, T &element) noexcept
Insert an element.
Definition List.h:430
const_iterator constIteratorTo(T const &element) const noexcept
Obtain a const iterator from an element.
Definition List.h:580
iterator append(List &list) noexcept
Append another list at the end of this list.
Definition List.h:556
T value_type
Definition List.h:262
iterator end() noexcept
Obtain a iterator to the end of the list.
Definition List.h:385
value_type * pointer
Definition List.h:263
const_reference front() const noexcept
Obtain a const reference to the first element.
Definition List.h:323
reference back() noexcept
Obtain a reference to the last element.
Definition List.h:334
std::ptrdiff_t difference_type
Definition List.h:268
const_iterator end() const noexcept
Obtain a const iterator to the end of the list.
Definition List.h:395
T & popBack() noexcept
Remove the element at the end of the list.
Definition List.h:520
detail::ListNode< T, Tag > Node
Definition List.h:260
bool empty() const noexcept
Determine if the list is empty.
Definition List.h:292
void swap(List &other) noexcept
Swap contents with another list.
Definition List.h:531
value_type & reference
Definition List.h:264
List(List const &)=delete
iterator pushBack(T &element) noexcept
Append an element at the end of the list.
Definition List.h:509
reference elementFrom(Node *node) noexcept
Definition List.h:587
value_type const & const_reference
Definition List.h:266
const_reference elementFrom(Node const *node) const noexcept
Definition List.h:593
T & popFront() noexcept
Remove the element at the beginning of the list.
Definition List.h:496
size_type size() const noexcept
Returns the number of elements in the list.
Definition List.h:301
const_iterator cbegin() const noexcept
Obtain a const iterator to the beginning of the list.
Definition List.h:375
List()
Create an empty list.
Definition List.h:276
iterator erase(iterator pos) noexcept
Remove an element.
Definition List.h:469
size_type size_
Definition List.h:599
iterator prepend(List &list) noexcept
Insert another list at the beginning of this list.
Definition List.h:545
iterator pushFront(T &element) noexcept
Insert an element at the beginning of the list.
Definition List.h:485
std::bidirectional_iterator_tag iterator_category
Definition List.h:62
ListIterator & operator++() noexcept
Definition List.h:98
ListIterator & operator--() noexcept
Definition List.h:113
value_type * pointer
Definition List.h:65
reference operator*() const noexcept
Definition List.h:86
ListIterator operator++(int) noexcept
Definition List.h:105
void decrement() noexcept
Definition List.h:141
reference dereference() const noexcept
Definition List.h:129
std::size_t size_type
Definition List.h:67
ListIterator operator--(int) noexcept
Definition List.h:120
value_type & reference
Definition List.h:66
pointer operator->() const noexcept
Definition List.h:92
bool operator==(ListIterator< M > const &other) const noexcept
Definition List.h:80
std::ptrdiff_t difference_type
Definition List.h:64
void increment() noexcept
Definition List.h:135
ListIterator(ListIterator< M > const &other) noexcept
Definition List.h:74
beast::detail::CopyConst< N, typename N::value_type >::type value_type
Definition List.h:63
ListIterator(N *node=nullptr) noexcept
Definition List.h:69
friend class ListIterator
Definition List.h:50
ListNode * next_
Definition List.h:52
ListNode * prev_
Definition List.h:53
std::remove_const< U >::type const type
Definition List.h:31
std::remove_const_t< U > type
Definition List.h:23