diff options
author | kleines Filmröllchen <filmroellchen@serenityos.org> | 2022-04-03 17:41:21 +0200 |
---|---|---|
committer | Brian Gianforcaro <b.gianfo@gmail.com> | 2022-04-03 09:49:08 -0700 |
commit | 7a0cd6793e33a04187c2a6eafa6eaf8aea32b938 (patch) | |
tree | fbb1ba8f6d792e9b57e7b32af79e976c6e6d1667 /AK | |
parent | 9afc7d53791a78cf156c57666b466c82a139affb (diff) | |
download | serenity-7a0cd6793e33a04187c2a6eafa6eaf8aea32b938.zip |
AK: Add non-const iterator for CircularQueue
Diffstat (limited to 'AK')
-rw-r--r-- | AK/CircularQueue.h | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/AK/CircularQueue.h b/AK/CircularQueue.h index d4ab79d9b3..979989aa2f 100644 --- a/AK/CircularQueue.h +++ b/AK/CircularQueue.h @@ -64,6 +64,7 @@ public: } const T& at(size_t index) const { return elements()[(m_head + index) % Capacity]; } + T& at(size_t index) { return elements()[(m_head + index) % Capacity]; } const T& first() const { return at(0); } const T& last() const { return at(size() - 1); } @@ -90,9 +91,34 @@ public: size_t m_index { 0 }; }; + class Iterator { + public: + bool operator!=(Iterator const& other) { return m_index != other.m_index; } + Iterator& operator++() + { + ++m_index; + return *this; + } + + T& operator*() const { return m_queue.at(m_index); } + + private: + friend class CircularQueue; + Iterator(CircularQueue& queue, size_t const index) + : m_queue(queue) + , m_index(index) + { + } + CircularQueue& m_queue; + size_t m_index { 0 }; + }; + ConstIterator begin() const { return ConstIterator(*this, 0); } ConstIterator end() const { return ConstIterator(*this, size()); } + Iterator begin() { return Iterator(*this, 0); } + Iterator end() { return Iterator(*this, size()); } + size_t head_index() const { return m_head; } protected: |