diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/generic/intrlist.h | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/generic/intrlist.h')
-rw-r--r-- | util/generic/intrlist.h | 422 |
1 files changed, 211 insertions, 211 deletions
diff --git a/util/generic/intrlist.h b/util/generic/intrlist.h index b5d3f2051b..e1a5b57b32 100644 --- a/util/generic/intrlist.h +++ b/util/generic/intrlist.h @@ -1,101 +1,101 @@ #pragma once - -#include "utility.h" - -#include <util/system/yassert.h> -#include <iterator> - -struct TIntrusiveListDefaultTag {}; - -/* - * two-way linked list - */ + +#include "utility.h" + +#include <util/system/yassert.h> +#include <iterator> + +struct TIntrusiveListDefaultTag {}; + +/* + * two-way linked list + */ template <class T, class Tag = TIntrusiveListDefaultTag> class TIntrusiveListItem { private: using TListItem = TIntrusiveListItem<T, Tag>; - + public: inline TIntrusiveListItem() noexcept - : Next_(this) - , Prev_(Next_) - { - } - + : Next_(this) + , Prev_(Next_) + { + } + inline ~TIntrusiveListItem() { Unlink(); } - + public: - Y_PURE_FUNCTION inline bool Empty() const noexcept { + Y_PURE_FUNCTION inline bool Empty() const noexcept { return (Prev_ == this) && (Next_ == this); } - + inline void Unlink() noexcept { if (Empty()) { return; - } - + } + Prev_->SetNext(Next_); Next_->SetPrev(Prev_); - + SetEnd(); } - + inline void LinkBefore(TListItem* before) noexcept { Unlink(); LinkBeforeNoUnlink(before); } - + inline void LinkBeforeNoUnlink(TListItem* before) noexcept { TListItem* const after = before->Prev(); - + after->SetNext(this); SetPrev(after); SetNext(before); before->SetPrev(this); } - + inline void LinkBefore(TListItem& before) noexcept { LinkBefore(&before); } - + inline void LinkAfter(TListItem* after) noexcept { Unlink(); LinkBeforeNoUnlink(after->Next()); } - + inline void LinkAfter(TListItem& after) noexcept { LinkAfter(&after); } - + public: inline TListItem* Prev() noexcept { return Prev_; } - + inline const TListItem* Prev() const noexcept { return Prev_; } - + inline TListItem* Next() noexcept { return Next_; } - + inline const TListItem* Next() const noexcept { return Next_; } - + public: inline void SetEnd() noexcept { Prev_ = this; Next_ = Prev_; } - + inline void SetNext(TListItem* item) noexcept { Next_ = item; } - + inline void SetPrev(TListItem* item) noexcept { Prev_ = item; } @@ -104,25 +104,25 @@ public: inline T* Node() noexcept { return static_cast<T*>(this); } - + inline const T* Node() const noexcept { return static_cast<const T*>(this); } - + private: inline TIntrusiveListItem(const TIntrusiveListItem&) = delete; inline TIntrusiveListItem& operator=(const TIntrusiveListItem&) = delete; private: - TListItem* Next_; - TListItem* Prev_; -}; - + TListItem* Next_; + TListItem* Prev_; +}; + template <class T, class Tag> class TIntrusiveList { private: using TListItem = TIntrusiveListItem<T, Tag>; - + template <class TListItem, class TNode> class TIteratorBase { public: @@ -130,7 +130,7 @@ private: using TReference = TNode&; using TPointer = TNode*; - using iterator_category = std::bidirectional_iterator_tag; + using iterator_category = std::bidirectional_iterator_tag; using difference_type = ptrdiff_t; using value_type = TNode; @@ -139,82 +139,82 @@ private: inline TIteratorBase() noexcept : Item_(nullptr) - { - } - + { + } + template <class TListItem_, class TNode_> inline TIteratorBase(const TIteratorBase<TListItem_, TNode_>& right) noexcept : Item_(right.Item()) { } - + inline TIteratorBase(TItem* item) noexcept : Item_(item) { } - + inline TItem* Item() const noexcept { return Item_; } - + inline void Next() noexcept { Item_ = Item_->Next(); } - + inline void Prev() noexcept { Item_ = Item_->Prev(); } - + template <class TListItem_, class TNode_> inline bool operator==(const TIteratorBase<TListItem_, TNode_>& right) const noexcept { return Item() == right.Item(); } - + template <class TListItem_, class TNode_> inline bool operator!=(const TIteratorBase<TListItem_, TNode_>& right) const noexcept { return Item() != right.Item(); } - + inline TIteratorBase& operator++() noexcept { Next(); - + return *this; } - + inline TIteratorBase operator++(int) noexcept { - TIteratorBase ret(*this); - + TIteratorBase ret(*this); + Next(); - + return ret; } - + inline TIteratorBase& operator--() noexcept { Prev(); - + return *this; } - + inline TIteratorBase operator--(int) noexcept { - TIteratorBase ret(*this); - + TIteratorBase ret(*this); + Prev(); - + return ret; } - + inline TReference operator*() const noexcept { return *Item_->Node(); } - + inline TPointer operator->() const noexcept { return Item_->Node(); } - + private: - TItem* Item_; + TItem* Item_; }; - + template <class TIterator> class TReverseIteratorBase { public: @@ -230,13 +230,13 @@ private: using pointer = typename TIterator::pointer; inline TReverseIteratorBase() noexcept = default; - + template <class TIterator_> inline TReverseIteratorBase(const TReverseIteratorBase<TIterator_>& right) noexcept : Current_(right.Base()) { } - + inline explicit TReverseIteratorBase(TIterator item) noexcept : Current_(item) { @@ -248,7 +248,7 @@ private: inline TItem* Item() const noexcept { TIterator ret = Current_; - + return (--ret).Item(); } @@ -272,65 +272,65 @@ private: inline TReverseIteratorBase& operator++() noexcept { Next(); - + return *this; } - + inline TReverseIteratorBase operator++(int) noexcept { - TReverseIteratorBase ret(*this); - + TReverseIteratorBase ret(*this); + Next(); - + return ret; } - + inline TReverseIteratorBase& operator--() noexcept { Prev(); - + return *this; } - + inline TReverseIteratorBase operator--(int) noexcept { - TReverseIteratorBase ret(*this); - + TReverseIteratorBase ret(*this); + Prev(); - + return ret; } - + inline TReference operator*() const noexcept { TIterator ret = Current_; - + return *--ret; } inline TPointer operator->() const noexcept { TIterator ret = Current_; - + return &*--ret; } private: TIterator Current_; }; - + public: using TIterator = TIteratorBase<TListItem, T>; using TConstIterator = TIteratorBase<const TListItem, const T>; - + using TReverseIterator = TReverseIteratorBase<TIterator>; using TConstReverseIterator = TReverseIteratorBase<TConstIterator>; - + using iterator = TIterator; using const_iterator = TConstIterator; - + using reverse_iterator = TReverseIterator; using const_reverse_iterator = TConstReverseIterator; public: inline void Swap(TIntrusiveList& right) noexcept { TIntrusiveList temp; - + temp.Append(right); Y_ASSERT(right.Empty()); right.Append(*this); @@ -338,7 +338,7 @@ public: this->Append(temp); Y_ASSERT(temp.Empty()); } - + public: inline TIntrusiveList() noexcept = default; @@ -354,17 +354,17 @@ public: } inline explicit operator bool() const noexcept { - return !Empty(); - } - - Y_PURE_FUNCTION inline bool Empty() const noexcept { + return !Empty(); + } + + Y_PURE_FUNCTION inline bool Empty() const noexcept { return End_.Empty(); } - + inline size_t Size() const noexcept { return std::distance(Begin(), End()); } - + inline void Remove(TListItem* item) noexcept { item->Unlink(); } @@ -372,40 +372,40 @@ public: inline void Clear() noexcept { End_.Unlink(); } - + public: inline TIterator Begin() noexcept { return ++End(); } - + inline TIterator End() noexcept { - return TIterator(&End_); + return TIterator(&End_); } - + inline TConstIterator Begin() const noexcept { return ++End(); } - + inline TConstIterator End() const noexcept { - return TConstIterator(&End_); + return TConstIterator(&End_); } - + inline TReverseIterator RBegin() noexcept { - return TReverseIterator(End()); + return TReverseIterator(End()); } - + inline TReverseIterator REnd() noexcept { - return TReverseIterator(Begin()); + return TReverseIterator(Begin()); } - + inline TConstReverseIterator RBegin() const noexcept { - return TConstReverseIterator(End()); + return TConstReverseIterator(End()); } - + inline TConstReverseIterator REnd() const noexcept { - return TConstReverseIterator(Begin()); + return TConstReverseIterator(Begin()); } - + inline TConstIterator CBegin() const noexcept { return Begin(); } @@ -483,27 +483,27 @@ public: inline void PushBack(TListItem* item) noexcept { item->LinkBefore(End_); } - + inline void PushFront(TListItem* item) noexcept { item->LinkAfter(End_); } - + inline T* PopBack() noexcept { TListItem* const ret = End_.Prev(); - + ret->Unlink(); - + return ret->Node(); } - + inline T* PopFront() noexcept { TListItem* const ret = End_.Next(); - + ret->Unlink(); - + return ret->Node(); } - + inline void Append(TIntrusiveList& list) noexcept { Cut(list.Begin(), list.End(), End()); } @@ -511,79 +511,79 @@ public: inline static void Cut(TIterator begin, TIterator end, TIterator pasteBefore) noexcept { if (begin == end) { return; - } - + } + TListItem* const cutFront = begin.Item(); TListItem* const gapBack = end.Item(); - + TListItem* const gapFront = cutFront->Prev(); TListItem* const cutBack = gapBack->Prev(); - + gapFront->SetNext(gapBack); gapBack->SetPrev(gapFront); TListItem* const pasteBack = pasteBefore.Item(); TListItem* const pasteFront = pasteBack->Prev(); - + pasteFront->SetNext(cutFront); cutFront->SetPrev(pasteFront); - + cutBack->SetNext(pasteBack); pasteBack->SetPrev(cutBack); } - + public: template <class TFunctor> - inline void ForEach(TFunctor&& functor) { + inline void ForEach(TFunctor&& functor) { TIterator i = Begin(); - + while (i != End()) { functor(&*(i++)); } } template <class TFunctor> - inline void ForEach(TFunctor&& functor) const { + inline void ForEach(TFunctor&& functor) const { TConstIterator i = Begin(); while (i != End()) { functor(&*(i++)); - } + } } - + template <class TComparer> - inline void QuickSort(TComparer&& comparer) { + inline void QuickSort(TComparer&& comparer) { if (Begin() == End() || ++Begin() == End()) { return; } - + T* const pivot = PopFront(); TIntrusiveList bigger; TIterator i = Begin(); - + while (i != End()) { if (comparer(*pivot, *i)) { bigger.PushBack(&*i++); } else { ++i; - } + } } - + this->QuickSort(comparer); bigger.QuickSort(comparer); - + PushBack(pivot); Append(bigger); } - + private: inline TIntrusiveList(const TIntrusiveList&) = delete; inline TIntrusiveList& operator=(const TIntrusiveList&) = delete; private: TListItem End_; -}; - +}; + template <class T, class D, class Tag> class TIntrusiveListWithAutoDelete: public TIntrusiveList<T, Tag> { public: @@ -592,7 +592,7 @@ public: using TReverseIterator = typename TIntrusiveList<T, Tag>::TReverseIterator; using TConstReverseIterator = typename TIntrusiveList<T, Tag>::TConstReverseIterator; - + using iterator = TIterator; using const_iterator = TConstIterator; @@ -631,62 +631,62 @@ public: inline static void Cut(TIterator begin, TIterator end, TIterator pasteBefore) noexcept { TIntrusiveList<T, Tag>::Cut(begin, end, pasteBefore); } -}; - -/* - * one-way linked list - */ +}; + +/* + * one-way linked list + */ template <class T, class Tag = TIntrusiveListDefaultTag> -class TIntrusiveSListItem { +class TIntrusiveSListItem { private: using TListItem = TIntrusiveSListItem<T, Tag>; - + public: inline TIntrusiveSListItem() noexcept : Next_(nullptr) - { + { } - + inline ~TIntrusiveSListItem() = default; - + inline bool IsEnd() const noexcept { return Next_ == nullptr; - } - + } + inline TListItem* Next() noexcept { return Next_; } - + inline const TListItem* Next() const noexcept { return Next_; } - + inline void SetNext(TListItem* item) noexcept { Next_ = item; } - + public: inline T* Node() noexcept { return static_cast<T*>(this); } - + inline const T* Node() const noexcept { return static_cast<const T*>(this); } - + private: - TListItem* Next_; -}; - + TListItem* Next_; +}; + template <class T, class Tag> -class TIntrusiveSList { +class TIntrusiveSList { private: using TListItem = TIntrusiveSListItem<T, Tag>; - + public: template <class TListItem, class TNode> class TIteratorBase { - public: + public: using TItem = TListItem; using TReference = TNode&; using TPointer = TNode*; @@ -701,94 +701,94 @@ public: : Item_(item) { } - + inline void Next() noexcept { Item_ = Item_->Next(); } - + inline bool operator==(const TIteratorBase& right) const noexcept { return Item_ == right.Item_; } - + inline bool operator!=(const TIteratorBase& right) const noexcept { return Item_ != right.Item_; } - + inline TIteratorBase& operator++() noexcept { Next(); - + return *this; } - + inline TIteratorBase operator++(int) noexcept { TIteratorBase ret(*this); - + Next(); - + return ret; } - + inline TNode& operator*() noexcept { return *Item_->Node(); } - + inline TNode* operator->() noexcept { return Item_->Node(); } - + private: - TListItem* Item_; + TListItem* Item_; }; - + public: using TIterator = TIteratorBase<TListItem, T>; using TConstIterator = TIteratorBase<const TListItem, const T>; - + using iterator = TIterator; using const_iterator = TConstIterator; - + public: inline TIntrusiveSList() noexcept : Begin_(nullptr) - { + { } - + inline void Swap(TIntrusiveSList& right) noexcept { - DoSwap(Begin_, right.Begin_); + DoSwap(Begin_, right.Begin_); } - + inline explicit operator bool() const noexcept { - return !Empty(); - } - - Y_PURE_FUNCTION inline bool Empty() const noexcept { + return !Empty(); + } + + Y_PURE_FUNCTION inline bool Empty() const noexcept { return Begin_ == nullptr; } - + inline size_t Size() const noexcept { return std::distance(Begin(), End()); } - + inline void Clear() noexcept { Begin_ = nullptr; } - + inline TIterator Begin() noexcept { - return TIterator(Begin_); + return TIterator(Begin_); } - + inline TIterator End() noexcept { return TIterator(nullptr); } - + inline TConstIterator Begin() const noexcept { - return TConstIterator(Begin_); + return TConstIterator(Begin_); } - + inline TConstIterator End() const noexcept { return TConstIterator(nullptr); } - + inline TConstIterator CBegin() const noexcept { return Begin(); } @@ -797,7 +797,7 @@ public: return End(); } - //compat methods + //compat methods inline iterator begin() noexcept { return Begin(); } @@ -836,13 +836,13 @@ public: item->SetNext(Begin_); Begin_ = item; } - + inline T* PopFront() noexcept { Y_ASSERT(Begin_); - + TListItem* const ret = Begin_; Begin_ = Begin_->Next(); - + return ret->Node(); } @@ -851,22 +851,22 @@ public: while (!Empty()) { temp.PushFront(PopFront()); - } - + } + this->Swap(temp); } - + template <class TFunctor> inline void ForEach(TFunctor&& functor) const noexcept(noexcept(functor(std::declval<TListItem>().Node()))) { TListItem* i = Begin_; - + while (i) { TListItem* const next = i->Next(); functor(i->Node()); i = next; - } + } } - + private: - TListItem* Begin_; -}; + TListItem* Begin_; +}; |