mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-26 04:35:41 +03:00
AK: Add non-const iterator for CircularQueue
This commit is contained in:
parent
9afc7d5379
commit
7a0cd6793e
Notes:
sideshowbarker
2024-07-17 14:32:46 +09:00
Author: https://github.com/kleinesfilmroellchen Commit: https://github.com/SerenityOS/serenity/commit/7a0cd6793e Pull-request: https://github.com/SerenityOS/serenity/pull/13469 Reviewed-by: https://github.com/linusg
@ -64,6 +64,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
const T& at(size_t index) const { return elements()[(m_head + index) % Capacity]; }
|
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& first() const { return at(0); }
|
||||||
const T& last() const { return at(size() - 1); }
|
const T& last() const { return at(size() - 1); }
|
||||||
@ -90,9 +91,34 @@ public:
|
|||||||
size_t m_index { 0 };
|
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 begin() const { return ConstIterator(*this, 0); }
|
||||||
ConstIterator end() const { return ConstIterator(*this, size()); }
|
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; }
|
size_t head_index() const { return m_head; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
Loading…
Reference in New Issue
Block a user