mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 12:17:35 +00:00
AK: Add non-const iterator for CircularQueue
This commit is contained in:
parent
9afc7d5379
commit
7a0cd6793e
1 changed files with 26 additions and 0 deletions
|
@ -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…
Add table
Add a link
Reference in a new issue