Updated details::circular_q and added some tests for it

This commit is contained in:
gabime 2023-09-01 12:49:15 +03:00
parent 820baa886c
commit aacea38f9d

View File

@ -90,17 +90,26 @@ public:
// Return const reference to item by index. // Return const reference to item by index.
// If index is out of range 0…size()-1, the behavior is undefined. // If index is out of range 0…size()-1, the behavior is undefined.
const T &at(size_t i) const const T &operator[](size_t idx) const
{ {
assert(i < size()); assert(idx < size());
return v_[(head_ + i) % max_items_]; assert(max_items_ > 0);
return v_[(head_ + idx) % max_items_];
} }
// Pop item from front. // Return reference to item by index.
// If there are no elements in the container, the behavior is undefined. T &operator[](size_t idx)
{
return const_cast<T &>(static_cast<const circular_q &>(*this)[idx]);
}
// Pop item from front if exists.
void pop_front() void pop_front()
{ {
head_ = (head_ + 1) % max_items_; if(!empty())
{
head_ = (head_ + 1) % max_items_;
}
} }
[[nodiscard]] bool empty() const [[nodiscard]] bool empty() const
@ -115,7 +124,7 @@ public:
{ {
return ((tail_ + 1) % max_items_) == head_; return ((tail_ + 1) % max_items_) == head_;
} }
return false; return true;
} }
[[nodiscard]] size_t overrun_counter() const [[nodiscard]] size_t overrun_counter() const
@ -145,7 +154,7 @@ public:
reference operator*() const reference operator*() const
{ {
return cq_->v_[(cq_->head_ + index_) % cq_->max_items_]; return cq_->operator[](index_);
} }
pointer operator->() pointer operator->()
@ -153,15 +162,17 @@ public:
return &operator*(); return &operator*();
} }
// Prefix increment
iterator &operator++() iterator &operator++()
{ {
if (cq_ != nullptr) if(cq_ != nullptr)
{ {
index_ = (index_ + 1) % cq_->max_items_; index_ = (index_ + 1);
} }
return *this; return *this;
} }
// Postfix increment
iterator operator++(int) iterator operator++(int)
{ {
iterator retval = *this; iterator retval = *this;