From 3be17c61171e4e147c1f5b064969d4e8ca619b50 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 10 Jul 2025 17:24:40 +0300 Subject: [PATCH] BAN: Add clear and access by index to CircularQueue --- BAN/include/BAN/CircularQueue.h | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/BAN/include/BAN/CircularQueue.h b/BAN/include/BAN/CircularQueue.h index 28554c5c..9400d81d 100644 --- a/BAN/include/BAN/CircularQueue.h +++ b/BAN/include/BAN/CircularQueue.h @@ -34,6 +34,11 @@ namespace BAN const T& back() const; T& back(); + const T& operator[](size_t index) const; + T& operator[](size_t index); + + void clear(); + size_type size() const { return m_size; } bool empty() const { return size() == 0; } bool full() const { return size() == capacity(); } @@ -53,8 +58,7 @@ namespace BAN template CircularQueue::~CircularQueue() { - for (size_type i = 0; i < m_size; i++) - element_at((m_first + i) % capacity())->~T(); + clear(); } template @@ -115,6 +119,28 @@ namespace BAN return *element_at((m_first + m_size - 1) % capacity()); } + template + const T& CircularQueue::operator[](size_t index) const + { + ASSERT(index < m_size); + return *element_at((m_first + index) % capacity()); + } + + template + T& CircularQueue::operator[](size_t index) + { + ASSERT(index < m_size); + return *element_at((m_first + index) % capacity()); + } + + template + void CircularQueue::clear() + { + for (size_type i = 0; i < m_size; i++) + element_at((m_first + i) % capacity())->~T(); + m_size = 0; + } + template const T* CircularQueue::element_at(size_type index) const {