1 #ifndef CYCLE_PTR_DETAIL_LLIST_H 2 #define CYCLE_PTR_DETAIL_LLIST_H 7 #include <initializer_list> 9 namespace cycle_ptr::detail {
15 template<
typename>
class link;
23 template<
typename T,
typename Tag>
27 static_assert(std::is_base_of_v<
link<Tag>, T>,
28 "Require T to derive from link<Tag>.");
58 auto operator=(
const llist&) ->
llist& =
delete;
70 template<
typename Iter>
88 return this->pred_ ==
this;
97 return static_cast<size_type>(std::distance(
begin(),
end()));
113 return iterator(std::addressof(elem));
122 return const_iterator(std::addressof(elem));
152 return *std::prev(
end());
162 return *std::prev(
end());
169 return iterator(this->succ_);
183 return const_iterator(this->succ_);
190 return iterator(
this);
204 return const_iterator(
this);
269 auto insert(const_iterator pos, T& v) noexcept -> iterator;
276 template<
typename Iter>
277 auto insert(const_iterator pos, Iter b, Iter e) -> iterator;
306 auto erase(const_iterator b) -> iterator;
311 auto erase(const_iterator b, const_iterator e) -> iterator;
318 auto splice(const_iterator pos,
llist& other) noexcept -> void;
325 auto splice(const_iterator pos,
llist& other, const_iterator elem) noexcept -> void;
332 auto splice(const_iterator pos,
llist& other, const_iterator other_begin, const_iterator other_end) noexcept -> void;
346 template<
typename Tag>
348 template<
typename,
typename>
friend class llist;
352 constexpr
link() noexcept = default;
357 assert((pred_ ==
nullptr && succ_ ==
nullptr)
358 || (pred_ ==
this && succ_ ==
this));
361 ~link() noexcept = default;
373 constexpr
link([[maybe_unused]]
const link& other) noexcept
406 return pred_ !=
nullptr;
411 link* pred_ =
nullptr;
413 link* succ_ =
nullptr;
420 template<
typename T,
typename Tag>
422 template<
typename,
typename>
friend class llist;
444 assert(ptr !=
nullptr);
450 constexpr
iterator() noexcept = default;
454 auto operator*() const
457 assert(link_ !=
nullptr);
458 return *static_cast<T*>(link_);
466 assert(link_ !=
nullptr);
467 return static_cast<T*>(link_);
475 assert(link_ !=
nullptr);
476 link_ = link_->succ_;
485 assert(link_ !=
nullptr);
486 link_ = link_->pred_;
514 return link_ == other.link_;
521 return !(*
this == other);
528 return link_ == other.link_;
535 return !(*
this == other);
546 template<
typename T,
typename Tag>
548 template<
typename,
typename>
friend class llist;
570 assert(ptr !=
nullptr);
597 auto operator*() const
600 assert(link_ !=
nullptr);
601 return *static_cast<const T*>(link_);
609 assert(link_ !=
nullptr);
610 return static_cast<const T*>(link_);
618 assert(link_ !=
nullptr);
619 link_ = link_->succ_;
628 assert(link_ !=
nullptr);
629 link_ = link_->pred_;
657 return link_ == other.link_;
664 return !(*
this == other);
671 return link_ == other.link_;
678 return !(*
this == other);
687 template<
typename T,
typename Tag>
691 assert(pos.link_ !=
nullptr);
693 link<Tag>*
const vlink = std::addressof(v);
694 assert(vlink->succ_ ==
nullptr && vlink->pred_ ==
nullptr);
698 vlink->pred_ = std::exchange(succ->pred_, vlink);
699 vlink->succ_ = std::exchange(pred->succ_, vlink);
700 return iterator(vlink);
703 template<
typename T,
typename Tag>
704 template<
typename Iter>
707 assert(pos.link_ !=
nullptr);
708 if (b == e)
return iterator(
const_cast<link<Tag>*
>(pos.link_));
710 iterator result = insert(pos, *b);
711 while (++b != e) insert(pos, *b);
715 template<
typename T,
typename Tag>
719 return erase(b, std::next(b));
722 template<
typename T,
typename Tag>
725 assert(b.link_ !=
nullptr && e.link_ !=
nullptr);
727 if (b == e)
return iterator(
const_cast<link<Tag>*
>(e.link_));
730 assert(pred->succ_ == b.link_);
740 l->pred_ = l->succ_ =
nullptr;
742 return iterator(succ);
745 template<
typename T,
typename Tag>
749 assert(&other !=
this);
750 splice(pos, other, other.begin(), other.end());
753 template<
typename T,
typename Tag>
757 assert(pos.link_ !=
nullptr && elem.link_ !=
nullptr);
758 if (elem.link_ == pos.link_)
return;
759 splice(pos, other, elem, std::next(elem));
762 template<
typename T,
typename Tag>
767 assert(pos.link_ !=
nullptr && other_begin.link_ !=
nullptr && other_end.link_ !=
nullptr);
768 for (const_iterator i = other_begin; i != other_end; ++i)
770 for (const_iterator i = other_begin; i != other_end; ++i)
771 assert(other.end() != i);
774 if (other_begin == other_end)
return;
775 if (pos == other_end) {
776 assert(
this == &other);
781 link<Tag>*
const my_pred = my_succ->pred_;
783 link<Tag>*
const other_pred = other_first->pred_;
785 link<Tag>*
const other_last = other_succ->pred_;
787 my_succ->pred_ = other_last;
788 my_pred->succ_ = other_first;
790 other_last->succ_ = my_succ;
791 other_first->pred_ = my_pred;
793 other_succ->pred_ = other_pred;
794 other_pred->succ_ = other_succ;
auto operator!=(const iterator &other) const noexcept -> bool
Inequality comparator.
Definition: llist.h:675
auto operator->() const noexcept -> const T *
Indirection operation.
Definition: llist.h:606
Internally used datastructure for llist.
Definition: llist.h:15
auto operator!=(const const_iterator &other) const noexcept -> bool
Inequality comparator.
Definition: llist.h:532
~link() noexcept
Destructor.
Definition: llist.h:356
std::intptr_t difference_type
Difference type of the iterator.
Definition: llist.h:433
auto begin() const noexcept -> const_iterator
Return iterator to first element in the list.
Definition: llist.h:173
const T * pointer
Pointer type of the list.
Definition: llist.h:557
auto pop_back() -> T &
Unlink the last element in the list.
Definition: llist.h:295
Const iterator for llist.
Definition: llist.h:547
constexpr link() noexcept=default
Default constructor.
auto operator--() noexcept -> iterator &
Move iterator position backward.
Definition: llist.h:482
static auto iterator_to(const T &elem) noexcept -> const_iterator
Create iterator to element.
Definition: llist.h:119
llist(Iter b, Iter e)
Range constructor.
Definition: llist.h:71
Iterator for llist.
Definition: llist.h:421
auto empty() const noexcept -> bool
Test if this list is empty.
Definition: llist.h:85
~llist() noexcept
Destructor.
Definition: llist.h:79
auto operator++(int) noexcept -> iterator
Advance iterator.
Definition: llist.h:492
Tag used to indicate a link is to be the root of a linked list.
Definition: llist.h:13
auto operator--() noexcept -> const_iterator &
Move iterator position backward.
Definition: llist.h:625
auto operator--(int) noexcept -> iterator
Move iterator position backward.
Definition: llist.h:502
auto operator!=(const iterator &other) const noexcept -> bool
Inequality comparator.
Definition: llist.h:518
auto operator!=(const const_iterator &other) const noexcept -> bool
Inequality comparator.
Definition: llist.h:661
auto operator==(const const_iterator &other) const noexcept -> bool
Equality comparator.
Definition: llist.h:525
auto rend() const noexcept -> const_reverse_iterator
Return iterator past the last element in reverse iteration.
Definition: llist.h:236
auto front() -> T &
Reference to first item in this list.
Definition: llist.h:129
auto crend() const noexcept -> const_reverse_iterator
Return iterator past the last element in reverse iteration.
Definition: llist.h:243
auto back() const -> const T &
Reference to last item in this list.
Definition: llist.h:159
auto back() -> T &
Reference to last item in this list.
Definition: llist.h:149
auto erase(const_iterator b) -> iterator
Erase element from the list.
Definition: llist.h:716
auto operator==(const const_iterator &other) const noexcept -> bool
Equality comparator.
Definition: llist.h:654
std::intptr_t difference_type
Difference type of the iterator.
Definition: llist.h:559
auto end() const noexcept -> const_iterator
Return iterator past the last element in the list.
Definition: llist.h:194
auto push_front(T &v) noexcept -> void
Link element into the list, as the first item.
Definition: llist.h:259
auto cbegin() const noexcept -> const_iterator
Return iterator to first element in the list.
Definition: llist.h:180
std::bidirectional_iterator_tag iterator_category
Iterator is a bidirectional iterator.
Definition: llist.h:435
auto begin() noexcept -> iterator
Return iterator to first element in the list.
Definition: llist.h:166
Intrusive linked list.
Definition: llist.h:24
constexpr auto operator=([[maybe_unused]] const link &other) noexcept -> link &
Assignment.
Definition: llist.h:385
auto operator++() noexcept -> iterator &
Advance iterator.
Definition: llist.h:472
auto rbegin() noexcept -> reverse_iterator
Return iterator to first element in reverse iteration.
Definition: llist.h:208
auto insert(const_iterator pos, T &v) noexcept -> iterator
Link element into the list.
Definition: llist.h:688
auto splice(const_iterator pos, llist &other) noexcept -> void
Splice elements from list.
Definition: llist.h:746
auto cend() const noexcept -> const_iterator
Return iterator past the last element in the list.
Definition: llist.h:201
T & reference
Reference type of the list.
Definition: llist.h:429
llist(llist &&other) noexcept
Move constructor.
Definition: llist.h:61
auto push_back(T &v) noexcept -> void
Link element into the list, as the last item.
Definition: llist.h:251
std::bidirectional_iterator_tag iterator_category
Iterator is a bidirectional iterator.
Definition: llist.h:561
T value_type
Value type of the list.
Definition: llist.h:427
auto operator->() const noexcept -> T *
Indirection operation.
Definition: llist.h:463
auto operator==(const iterator &other) const noexcept -> bool
Equality comparator.
Definition: llist.h:668
auto size() const noexcept -> size_type
Compute the size of the list.
Definition: llist.h:94
auto crbegin() const noexcept -> const_reverse_iterator
Return iterator to first element in reverse iteration.
Definition: llist.h:222
auto rend() noexcept -> reverse_iterator
Return iterator past the last element in reverse iteration.
Definition: llist.h:229
constexpr link([[maybe_unused]] const link &other) noexcept
Constructor.
Definition: llist.h:373
auto rbegin() const noexcept -> const_reverse_iterator
Return iterator to first element in reverse iteration.
Definition: llist.h:215
constexpr auto operator=(const iterator &other) noexcept -> const_iterator &
Conversion assignment.
Definition: llist.h:588
T value_type
Value type of the list.
Definition: llist.h:553
auto operator++(int) noexcept -> const_iterator
Advance iterator.
Definition: llist.h:635
auto clear() noexcept -> void
Unlink all elements from the list.
Definition: llist.h:101
llist() noexcept
Default constructor.
Definition: llist.h:53
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator type.
Definition: llist.h:48
auto front() const -> const T &
Reference to first item in this list.
Definition: llist.h:139
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse const_iterator type.
Definition: llist.h:50
auto operator++() noexcept -> const_iterator &
Advance iterator.
Definition: llist.h:615
std::uintptr_t size_type
Size type of the list.
Definition: llist.h:42
const T & reference
Reference type of the list.
Definition: llist.h:555
auto operator--(int) noexcept -> const_iterator
Move iterator position backward.
Definition: llist.h:645
auto end() noexcept -> iterator
Return iterator past the last element in the list.
Definition: llist.h:187
auto pop_front() -> T &
Unlink the first element in the list.
Definition: llist.h:283
static auto iterator_to(T &elem) noexcept -> iterator
Create iterator to element.
Definition: llist.h:110
auto operator==(const iterator &other) const noexcept -> bool
Equality comparator.
Definition: llist.h:511
T * pointer
Pointer type of the list.
Definition: llist.h:431
constexpr auto linked() const noexcept -> bool
Test if this is linked into a linked list.
Definition: llist.h:403