AK: Add non-const iterator for CircularQueue

This commit is contained in:
kleines Filmröllchen 2022-04-03 17:41:21 +02:00 committed by Brian Gianforcaro
parent 9afc7d5379
commit 7a0cd6793e
Notes: sideshowbarker 2024-07-17 14:32:46 +09:00

View File

@ -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: