diff options
author | Peter Elliott <pelliott@ualberta.ca> | 2021-10-07 20:06:51 -0600 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-10-08 11:40:06 +0200 |
commit | 39baadcddfe8e54fea14994957d2b2a6edfa30ae (patch) | |
tree | f95ea6afa4ca95f6ddedf609077f3e92e6b49b14 /AK/CircularQueue.h | |
parent | c27f91142de1d2d041fbc0077ec4602fd8e04a7b (diff) | |
download | serenity-39baadcddfe8e54fea14994957d2b2a6edfa30ae.zip |
AK: Calculate CircularQueue::end() correctly (for real this time)
Both my approach and the previous approach were wrong for different
cases. I've changed the Iterators index from storage-relative to
queue-relative, and it's much simpler and more obviously correct.
fixes #10383
Diffstat (limited to 'AK/CircularQueue.h')
-rw-r--r-- | AK/CircularQueue.h | 10 |
1 files changed, 4 insertions, 6 deletions
diff --git a/AK/CircularQueue.h b/AK/CircularQueue.h index e477c54f98..8d996c972b 100644 --- a/AK/CircularQueue.h +++ b/AK/CircularQueue.h @@ -73,13 +73,11 @@ public: bool operator!=(const ConstIterator& other) { return m_index != other.m_index; } ConstIterator& operator++() { - m_index = (m_index + 1) % Capacity; - if (m_index == m_queue.m_head) - m_index = m_queue.m_size; + ++m_index; return *this; } - const T& operator*() const { return m_queue.elements()[m_index]; } + const T& operator*() const { return m_queue.at(m_index); } private: friend class CircularQueue; @@ -92,8 +90,8 @@ public: size_t m_index { 0 }; }; - ConstIterator begin() const { return ConstIterator(*this, m_head); } - ConstIterator end() const { return ConstIterator(*this, (m_head + size()) % Capacity); } + ConstIterator begin() const { return ConstIterator(*this, 0); } + ConstIterator end() const { return ConstIterator(*this, size()); } size_t head_index() const { return m_head; } |