Simplified details::circular_q by removing iterator support and updated tests

This commit is contained in:
gabime 2023-09-01 14:05:02 +03:00
parent aacea38f9d
commit a8b74096b2
2 changed files with 5 additions and 80 deletions

View File

@ -6,8 +6,6 @@
#include <vector> #include <vector>
#include <cassert> #include <cassert>
#include <iterator>
#include <cstddef>
namespace spdlog { namespace spdlog {
namespace details { namespace details {
@ -76,7 +74,7 @@ public:
} }
// Return number of elements actually stored // Return number of elements actually stored
[[nodiscard]] size_t size() const size_t size() const
{ {
if (tail_ >= head_) if (tail_ >= head_)
{ {
@ -97,13 +95,7 @@ public:
return v_[(head_ + idx) % max_items_]; return v_[(head_ + idx) % max_items_];
} }
// Return reference to item by index. // Pop item from front if not empty.
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()
{ {
if(!empty()) if(!empty())
@ -124,7 +116,7 @@ public:
{ {
return ((tail_ + 1) % max_items_) == head_; return ((tail_ + 1) % max_items_) == head_;
} }
return true; return false;
} }
[[nodiscard]] size_t overrun_counter() const [[nodiscard]] size_t overrun_counter() const
@ -137,73 +129,6 @@ public:
overrun_counter_ = 0; overrun_counter_ = 0;
} }
// Iterator support
class iterator
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T *;
using reference = T &;
explicit iterator(circular_q *circular_q = nullptr, size_t index = 0)
: cq_(circular_q)
, index_(index)
{}
reference operator*() const
{
return cq_->operator[](index_);
}
pointer operator->()
{
return &operator*();
}
// Prefix increment
iterator &operator++()
{
if(cq_ != nullptr)
{
index_ = (index_ + 1);
}
return *this;
}
// Postfix increment
iterator operator++(int)
{
iterator retval = *this;
++(*this);
return retval;
}
bool operator==(iterator other) const
{
return cq_ == other.cq_ && index_ == other.index_;
}
bool operator!=(iterator other) const
{
return index_ != other.index_ || cq_ != other.cq_;
}
private:
circular_q *cq_;
size_t index_;
};
iterator begin()
{
return iterator(this, 0);
}
iterator end()
{
return iterator(this, size());
}
private: private:
// copy from other&& and reset it to disabled state // copy from other&& and reset it to disabled state
void copy_moveable(circular_q &&other) noexcept void copy_moveable(circular_q &&other) noexcept

View File

@ -12,7 +12,7 @@ TEST_CASE("test_size", "[circular_q]")
REQUIRE(q.full() == false); REQUIRE(q.full() == false);
for (size_t i = 0; i < q_size; i++) for (size_t i = 0; i < q_size; i++)
{ {
q.push_back(std::move(i)); q.push_back(10);
} }
REQUIRE(q.size() == q_size); REQUIRE(q.size() == q_size);
q.push_back(999); q.push_back(999);
@ -26,7 +26,7 @@ TEST_CASE("test_rolling", "[circular_q]")
for (size_t i = 0; i < q_size + 2; i++) for (size_t i = 0; i < q_size + 2; i++)
{ {
q.push_back(std::move(i)); q.push_back(size_t{i}); // arg to push_back must be r value
} }
REQUIRE(q.size() == q_size); REQUIRE(q.size() == q_size);