diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-08-08 13:39:40 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-08-08 13:39:40 +0200 |
commit | bb9909548b3cb7e26e2b3f0e30249f779bcd3128 (patch) | |
tree | 177470260640bdf5b5b33711c5e812fa28f95c76 /AK/InlineLinkedList.h | |
parent | 318068fe1b2e5b5eccc8a89c41f78f12602f0cd3 (diff) | |
download | serenity-bb9909548b3cb7e26e2b3f0e30249f779bcd3128.zip |
AK: Add an iterator class for InlineLinkedList
This makes it possible to iterate over these with range-for. :^)
Diffstat (limited to 'AK/InlineLinkedList.h')
-rw-r--r-- | AK/InlineLinkedList.h | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/AK/InlineLinkedList.h b/AK/InlineLinkedList.h index 08e4fbdb26..87962372f2 100644 --- a/AK/InlineLinkedList.h +++ b/AK/InlineLinkedList.h @@ -6,6 +6,33 @@ namespace AK { template<typename T> +class InlineLinkedList; + +template<typename T> +class InlineLinkedListIterator { +public: + bool operator!=(const InlineLinkedListIterator& other) const { return m_node != other.m_node; } + bool operator==(const InlineLinkedListIterator& other) const { return m_node == other.m_node; } + InlineLinkedListIterator& operator++() + { + m_node = m_node->next(); + return *this; + } + T& operator*() { return *m_node; } + T* operator->() { return &m_node; } + bool is_end() const { return !m_node; } + static InlineLinkedListIterator universal_end() { return InlineLinkedListIterator(nullptr); } + +private: + friend InlineLinkedList<T>; + explicit InlineLinkedListIterator(T* node) + : m_node(node) + { + } + T* m_node; +}; + +template<typename T> class InlineLinkedListNode { public: InlineLinkedListNode(); @@ -77,6 +104,16 @@ public: return false; } + using Iterator = InlineLinkedListIterator<T>; + friend Iterator; + Iterator begin() { return Iterator(m_head); } + Iterator end() { return Iterator::universal_end(); } + + using ConstIterator = InlineLinkedListIterator<const T>; + friend ConstIterator; + ConstIterator begin() const { return ConstIterator(m_head); } + ConstIterator end() const { return ConstIterator::universal_end(); } + private: T* m_head { nullptr }; T* m_tail { nullptr }; |