From 1b2ea12062354657e195620ad947520406e98c0c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 11 Mar 2021 14:12:33 +0100 Subject: [PATCH] AK: Add basic const iteration to IntrusiveList --- AK/IntrusiveList.h | 86 ++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 45 deletions(-) diff --git a/AK/IntrusiveList.h b/AK/IntrusiveList.h index bb27ce70e2..132d45ca51 100644 --- a/AK/IntrusiveList.h +++ b/AK/IntrusiveList.h @@ -60,14 +60,21 @@ public: class Iterator { public: - Iterator(); - Iterator(T* value); + Iterator() = default; + Iterator(T* value) + : m_value(value) + { + } - T& operator*() const; - T* operator->() const; - bool operator==(const Iterator& other) const; + T& operator*() const { return *m_value; } + T* operator->() const { return m_value; } + bool operator==(const Iterator& other) const { return other.m_value == m_value; } bool operator!=(const Iterator& other) const { return !(*this == other); } - Iterator& operator++(); + Iterator& operator++() + { + m_value = IntrusiveList::next(m_value); + return *this; + } Iterator& erase(); private: @@ -75,7 +82,32 @@ public: }; Iterator begin(); - Iterator end(); + Iterator end() { return Iterator {}; } + + class ConstIterator { + public: + ConstIterator() = default; + ConstIterator(const T* value) + : m_value(value) + { + } + + const T& operator*() const { return *m_value; } + const T* operator->() const { return m_value; } + bool operator==(const ConstIterator& other) const { return other.m_value == m_value; } + bool operator!=(const ConstIterator& other) const { return !(*this == other); } + ConstIterator& operator++() + { + m_value = IntrusiveList::next(const_cast(m_value)); + return *this; + } + + private: + const T* m_value { nullptr }; + }; + + ConstIterator begin() const; + ConstIterator end() const { return ConstIterator {}; } private: static T* next(T* current); @@ -97,42 +129,6 @@ private: IntrusiveListNode* m_prev = nullptr; }; -template -inline IntrusiveList::Iterator::Iterator() -{ -} - -template -inline IntrusiveList::Iterator::Iterator(T* value) - : m_value(value) -{ -} - -template -inline T& IntrusiveList::Iterator::operator*() const -{ - return *m_value; -} - -template -inline T* IntrusiveList::Iterator::operator->() const -{ - return m_value; -} - -template -inline bool IntrusiveList::Iterator::operator==(const Iterator& other) const -{ - return other.m_value == m_value; -} - -template -inline typename IntrusiveList::Iterator& IntrusiveList::Iterator::operator++() -{ - m_value = IntrusiveList::next(m_value); - return *this; -} - template inline typename IntrusiveList::Iterator& IntrusiveList::Iterator::erase() { @@ -265,9 +261,9 @@ inline typename IntrusiveList::Iterator IntrusiveList::beg } template -inline typename IntrusiveList::Iterator IntrusiveList::end() +inline typename IntrusiveList::ConstIterator IntrusiveList::begin() const { - return Iterator(); + return m_storage.m_first ? ConstIterator(node_to_value(*m_storage.m_first)) : ConstIterator(); } template