xrpld
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
28};
29
30
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{
38 ListNode() = default;
39
40 using value_type = T;
41
42 friend T;
43 friend class List<T, Tag>;
44
45 template <typename>
46 friend class ListIterator;
47
48 ListNode* next_ = nullptr;
49 ListNode* prev_ = nullptr;
50};
51
52//------------------------------------------------------------------------------
53
54template <typename N>
56{
57public:
64
65 ListIterator(N* node = nullptr) noexcept : node_(node)
66 {
67 }
68
69 template <typename M>
70 ListIterator(ListIterator<M> const& other) noexcept : node_(other.node_)
71 {
72 }
73
74 template <typename M>
75 bool
76 operator==(ListIterator<M> const& other) const noexcept
77 {
78 return node_ == other.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:
131 [[nodiscard]] reference
132 dereference() const noexcept
133 {
134 return static_cast<reference>(*node_);
135 }
136
137 void
138 increment() noexcept
139 {
140 node_ = node_->next_;
141 }
142
143 void
144 decrement() noexcept
145 {
146 node_ = node_->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 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
290 [[nodiscard]] bool
291 empty() const noexcept
292 {
293 return size() == 0;
294 }
295
297 [[nodiscard]] size_type
298 size() const noexcept
299 {
300 return size_;
301 }
302
308 front() noexcept
309 {
310 return element_from(head_.next_);
311 }
312
317 [[nodiscard]] const_reference
318 front() const noexcept
319 {
320 return element_from(head_.next_);
321 }
322
328 back() noexcept
329 {
330 return element_from(tail_.prev_);
331 }
332
337 [[nodiscard]] const_reference
338 back() const noexcept
339 {
340 return element_from(tail_.prev_);
341 }
342
347 begin() noexcept
348 {
349 return iterator(head_.next_);
350 }
351
355 [[nodiscard]] const_iterator
356 begin() const noexcept
357 {
358 return const_iterator(head_.next_);
359 }
360
364 [[nodiscard]] const_iterator
365 cbegin() const noexcept
366 {
367 return const_iterator(head_.next_);
368 }
369
374 end() noexcept
375 {
376 return iterator(&tail_);
377 }
378
382 [[nodiscard]] const_iterator
383 end() const noexcept
384 {
385 return const_iterator(&tail_);
386 }
387
391 [[nodiscard]] const_iterator
392 cend() const noexcept
393 {
394 return const_iterator(&tail_);
395 }
396
400 void
401 clear() noexcept
402 {
403 head_.next_ = &tail_;
404 tail_.prev_ = &head_;
405 size_ = 0;
406 }
407
415 insert(iterator pos, T& element) noexcept
416 {
417 Node* node = static_cast<Node*>(&element);
418 node->next_ = &*pos;
419 node->prev_ = node->next_->prev_;
420 node->next_->prev_ = node;
421 node->prev_->next_ = node;
422 ++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.head_.next_->prev_ = before->prev_;
438 before->prev_->next_ = other.head_.next_;
439 other.tail_.prev_->next_ = before;
440 before->prev_ = other.tail_.prev_;
441 size_ += other.size_;
442 other.clear();
443 }
444 }
445
452 erase(iterator pos) noexcept
453 {
454 Node const* node = &*pos;
455 ++pos;
456 node->next_->prev_ = node->prev_;
457 node->prev_->next_ = node->next_;
458 --size_;
459 return pos;
460 }
461
467 pushFront(T& element) noexcept
468 {
469 return insert(begin(), element);
470 }
471
476 T&
477 popFront() noexcept
478 {
479 T& element(front());
480 erase(begin());
481 return element;
482 }
483
489 pushBack(T& element) noexcept
490 {
491 return insert(end(), element);
492 }
493
498 T&
499 popBack() 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 iteratorTo(T& element) const noexcept
543 {
544 return iterator(static_cast<Node*>(&element));
545 }
546
552 [[nodiscard]] const_iterator
553 constIteratorTo(T const& element) const noexcept
554 {
555 return const_iterator(static_cast<Node const*>(&element));
556 }
557
558private:
560 elementFrom(Node* node) noexcept
561 {
562 return *(static_cast<pointer>(node));
563 }
564
566 elementFrom(Node const* node) const noexcept
567 {
568 return *(static_cast<const_pointer>(node));
569 }
570
571private:
575};
576
577} // namespace beast
Intrusive doubly linked list.
Definition List.h:260
value_type const * const_pointer
Definition List.h:267
detail::ListIterator< Node const > const_iterator
Definition List.h:273
std::size_t size_type
Definition List.h:269
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 iteratorTo(T &element) const noexcept
Obtain an iterator from an element.
Definition List.h:542
iterator insert(iterator pos, T &element) noexcept
Insert an element.
Definition List.h:415
const_iterator constIteratorTo(T const &element) const noexcept
Obtain a const iterator from an element.
Definition List.h:553
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
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
std::ptrdiff_t difference_type
Definition List.h:270
const_iterator end() const noexcept
Obtain a const iterator to the end of the list.
Definition List.h:383
T & popBack() noexcept
Remove the element at the end of the list.
Definition List.h:499
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
List(List const &)=delete
iterator pushBack(T &element) noexcept
Append an element at the end of the list.
Definition List.h:489
reference elementFrom(Node *node) noexcept
Definition List.h:560
value_type const & const_reference
Definition List.h:268
const_reference elementFrom(Node const *node) const noexcept
Definition List.h:566
T & popFront() noexcept
Remove the element at the beginning of the list.
Definition List.h:477
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
iterator erase(iterator pos) noexcept
Remove an element.
Definition List.h:452
size_type size_
Definition List.h:572
iterator prepend(List &list) noexcept
Insert another list at the beginning of this list.
Definition List.h:521
iterator pushFront(T &element) noexcept
Insert an element at the beginning of the list.
Definition List.h:467
std::bidirectional_iterator_tag iterator_category
Definition List.h:58
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
void decrement() noexcept
Definition List.h:144
reference dereference() const noexcept
Definition List.h:132
std::size_t size_type
Definition List.h:63
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
std::ptrdiff_t difference_type
Definition List.h:60
void increment() noexcept
Definition List.h:138
ListIterator(ListIterator< M > const &other) noexcept
Definition List.h:70
beast::detail::CopyConst< N, typename N::value_type >::type value_type
Definition List.h:59
ListIterator(N *node=nullptr) noexcept
Definition List.h:65
friend class ListIterator
Definition List.h:46
ListNode * next_
Definition List.h:48
ListNode * prev_
Definition List.h:49
std::remove_const< U >::type const type
Definition List.h:27
std::remove_const_t< U > type
Definition List.h:19