9#include <initializer_list>
51 bool TrackSize =
true,
52 bool TrackLast =
true,
typename Allocator = PrimitiveAllocator>
101 const Allocator& alloc = Allocator())
106 for (; count > 0; --count, ++cur_it) {
123 template <InputIterator Iter>
125 const Allocator& alloc = Allocator())
130 for (; first != last; ++first) {
145 const Allocator& alloc = Allocator())
155 other.
cend(), other.alloc_) {}
165 , alloc_(
move(other.alloc_)) {
166 other.head_.next =
nullptr;
167 if constexpr (TrackSize) other.size_ = 0;
168 if constexpr (TrackLast) other.last_ = &other.head_;
201 swap(alloc_, other.alloc_);
205 swap(head_, other.head_);
206 if constexpr (TrackSize)
swap(size_, other.size_);
207 if constexpr (TrackLast)
swap(last_, other.last_);
261 return insertAfter(pos, init_list.begin(), init_list.end());
286 template <InputIterator Iter>
293 if (tmp.
isEmpty())
return pos.constCast();
319 template <
typename... Args>
354 template <
typename...
Args>
378 template <
typename...
Args>
446 return last.constCast();
486 template <
typename Cmp = less>
519 template <
typename Cmp = less>
529 if (
this == &
other)
return;
532 throw std::runtime_error(
533 "Try to merge linked list "
534 "allocated via different allocator");
538 return cmp(castToNode(
a)->val, castToNode(
b)->val);
553 prev->next =
a !=
nullptr ?
a :
b;
555 other.head_.next =
nullptr;
558 size_ +=
other.size_;
572 else if (last_ != &head_
613 if (
this == &
other ||
other.isEmpty())
return pos.constCast();
616 throw std::runtime_error(
617 "Try to transfer linked list "
618 "allocated via different allocator");
624 transferAfter(
pos.node_,
627 if (isIterToLast(
pos)) last_ =
other.last_;
633 size_ +=
other.size_;
683 throw std::runtime_error(
684 "Try to transfer linked list nodes "
685 "allocated via different allocator");
696 while (
end->next !=
last.node_) {
703 if (
this != &
other) {
710 if (isIterToLast(
pos)) last_ =
end;
729 while (
right !=
nullptr) {
791 return castToNode(
begin().node_);
810 while (
cur->next !=
nullptr) {
827 return head_.
next ==
nullptr;
852 return size_ ==
other.size_
883 BaseNode* transferAfter(BaseNode*
pos,
884 BaseNode*
begin, BaseNode*
end)
noexcept {
899 Node* castToNode(BaseNode*
ptr)
const noexcept {
return static_cast<Node*
>(
ptr); }
901 void initOptionalFields()
noexcept {
914 template <
typename...
Args>
933 void destroyNode(Node*
node)
noexcept {
977 BaseNode*
cur = &head_;
986 mutable BaseNode head_;
1004 typename Allocator = PrimitiveAllocator>
1013template <
typename T,
1018template <
typename T,
1023template <
typename T,
1031template <InputIterator Iter,
typename Allocator = PrimitiveAllocator>
Unifined interface for allocators.
Definition allocator.h:69
static void deallocate(Alloc &alloc, T *ptr, size_t n)
Deallocates memory under ptr through allocator.
Definition allocator.h:140
static void destroy(Alloc &alloc, T *ptr)
Destroys object throgh alloctor if destroy method is present. Otherwise calls ~T()
Definition allocator.h:107
static void construct(Alloc &alloc, T *ptr, Args &&... args)
Forwards given arguments to allocator's construct method if present. Otherwise uses placement new.
Definition allocator.h:85
Singly linked list.
Definition linked_list.h:53
void swap(LinkedList &other)
Effectively swaps content of the lists.
Definition linked_list.h:198
void reverse() noexcept
Reverses linked list.
Definition linked_list.h:725
LinkedList(std::initializer_list< value_type > init_list, const Allocator &alloc=Allocator())
Initialize list with the given initializer_list.
Definition linked_list.h:144
iterator eraseAfter(const_iterator first, const_iterator last) noexcept
Destroys elements in (first, last).
Definition linked_list.h:440
reference emplaceFront(Args &&... args)
Creates new node from given arguments in the start of the list.
Definition linked_list.h:355
const_iterator cbegin() const noexcept
Definition linked_list.h:838
const value_type & const_reference
Definition linked_list.h:70
LinkedList(Iter first, Iter last, const Allocator &alloc=Allocator())
Initialize list with the given range.
Definition linked_list.h:124
const_iterator beforeBegin() const noexcept
Definition linked_list.h:842
reference back() noexcept
Returns last element in the list.
Definition linked_list.h:767
const_iterator end() const noexcept
Definition linked_list.h:836
~LinkedList()
Definition linked_list.h:187
reference emplaceBack(Args &&... args)
Creates new node from given arguments in the end of the list.
Definition linked_list.h:379
const_iterator cend() const noexcept
Definition linked_list.h:839
iterator beforeBegin() noexcept
Definition linked_list.h:841
iterator beforeEnd() noexcept
Definition linked_list.h:845
T value_type
Definition linked_list.h:68
const_reference back() const noexcept
Returns last element in the list.
Definition linked_list.h:780
iterator spliceAfter(const_iterator pos, LinkedList &&other)
Transfers all elements from other to *this. other becomes empty.
Definition linked_list.h:612
void popBack() noexcept
Destroys last element in the list.
Definition linked_list.h:474
reference front() noexcept
Returns first element in the list.
Definition linked_list.h:749
Node * getRawFirstNode() const noexcept
Returns raw representation of the first list's node.
Definition linked_list.h:790
iterator spliceAfter(const_iterator pos, LinkedList &other, const_iterator first, const_iterator last)
See spliceAfter(const_iterator, LinkedList&&, const_iterator, const_iterator)
Definition linked_list.h:646
LinkedList(size_t count, const value_type &value=value_type(), const Allocator &alloc=Allocator())
Constructs count elements with given value.
Definition linked_list.h:100
auto operator<=>(const LinkedList &other) const noexcept
Definition linked_list.h:858
detail::LListIter< T, true > const_iterator
Definition linked_list.h:76
iterator spliceAfter(const_iterator pos, LinkedList &other)
See spliceAfter(const_iterator, LinkedList&&)
Definition linked_list.h:583
const_reference front() const noexcept
Returns first element in the list.
Definition linked_list.h:756
Allocator allocator_type
Definition linked_list.h:74
iterator spliceAfter(const_iterator pos, LinkedList &&other, const_iterator first, const_iterator last)
Transfers all elements in (first, last) from other to *this right after pos.
Definition linked_list.h:680
iterator end() noexcept
Definition linked_list.h:833
const_iterator beforeEnd() const noexcept
Definition linked_list.h:846
iterator insertAfter(const_iterator pos, value_type value)
Creates element and places it after the given iterator.
Definition linked_list.h:241
value_type * pointer
Definition linked_list.h:71
LinkedList & operator=(LinkedList other)
Copy/move assignment operator.
Definition linked_list.h:182
value_type & reference
Definition linked_list.h:69
size_t size_type
Definition linked_list.h:73
const_iterator cbeforeBegin() const noexcept
Definition linked_list.h:843
LinkedList(const Allocator &alloc)
Definition linked_list.h:82
void popFront() noexcept
Destroys first element in the list.
Definition linked_list.h:457
const_iterator begin() const noexcept
Definition linked_list.h:835
size_t size() const noexcept
Returns list's size.
Definition linked_list.h:805
iterator insertAfter(const_iterator pos, Iter first, Iter last)
Inserts elements from [first, last) in the list after pos.
Definition linked_list.h:287
detail::LListIter< T, false > iterator
Definition linked_list.h:75
allocator_type getAllocator() const
Definition linked_list.h:830
iterator insertAfter(const_iterator pos, std::initializer_list< T > init_list)
Inserts elements from initializer list in the list.
Definition linked_list.h:260
LinkedList(LinkedList &&other) noexcept(noexcept(Allocator(move(other.alloc_))))
Move constructor. No iterators are invalidated.
Definition linked_list.h:161
void merge(LinkedList &&other, Cmp cmp=Cmp())
Merges elements from 2 sorted lists into one sorted.
Definition linked_list.h:520
LinkedList(const LinkedList &other)
Copy constructor.
Definition linked_list.h:153
void clear() noexcept
Removes all elements in the list.
Definition linked_list.h:222
const_iterator cbeforeEnd() const noexcept
Definition linked_list.h:847
iterator eraseAfter(const_iterator pos) noexcept
Destroys element right after pos.
Definition linked_list.h:399
void merge(LinkedList &other, Cmp cmp=Cmp())
See merge(LinkedList&&, auto)
Definition linked_list.h:487
iterator begin() noexcept
Definition linked_list.h:832
iterator emplaceAfter(const_iterator pos, Args &&... args)
Creates new node from given arguments and places it after the given position.
Definition linked_list.h:320
bool isEmpty() const noexcept
Checks whether list has elements.
Definition linked_list.h:824
const value_type * const_pointer
Definition linked_list.h:72
LinkedList() noexcept(noexcept(Allocator()))
Definition linked_list.h:78
bool operator==(const LinkedList &other) const noexcept
Definition linked_list.h:850
Allocator-proxy for new/delete operators.
Definition allocator.h:22
Forward iterator for linked list.
Definition llist_iterator.h:27
Definition algo_base.h:14
constexpr remove_ref_t< T > && move(T &&value) noexcept
Convert a value to xvalue.
Definition move.h:19
Iter next(Iter it, typename IteratorTraits< Iter >::difference_type n=1)
Returns the n-th successor of given iterator.
Definition iterators.h:204
bool equal(Iter1 first1, Iter1 last1, Iter2 first2, Pred pred=Pred())
Checks whether given ranges are equal.
Definition algo_base.h:77
void swap(LinkedList< T, TrackSize, TrackLast, Allocator > &a, LinkedList< T, TrackSize, TrackLast, Allocator > &b)
See LinkedList::swap
Definition linked_list.h:1026
Iter prev(Iter it, typename IteratorTraits< Iter >::difference_type n=1)
Returns the n-th predecessor of given iterator. If n < 0, will chose n-th successor.
Definition iterators.h:221
constexpr T && forward(remove_ref_t< T > &value) noexcept
Forward a lvalue.
Definition move.h:33
auto lexicographical_compare_three_way(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, Cmp cmp=Cmp())
Compares ranges lexicographically using C++20 three-way comparation.
Definition algo_base.h:37
Definition llist_nodes.h:13
BaseNode * next
Definition llist_nodes.h:14
Definition llist_nodes.h:18