Simplify CircularBuffer

This commit is contained in:
Matt 2019-02-03 22:34:48 +01:00
parent ecd4f61115
commit a26e9231cf
1 changed files with 8 additions and 22 deletions

View File

@ -44,13 +44,13 @@ public:
reference operator[](size_type idx)
{
idx = (_head + idx) % _elements.size();
idx = (_head + idx) % capacity();
return _elements[idx];
}
const_reference operator[](size_type idx) const
{
idx = (_head + idx) % _elements.size();
idx = (_head + idx) % capacity();
return _elements[idx];
}
@ -71,37 +71,23 @@ public:
return _size == 0;
}
size_type capacity() const
constexpr size_type capacity() const
{
return _elements.size();
}
void push_back(const value_type& val)
{
if (_size == 0)
if (_size == capacity())
{
_elements[_head] = val;
_tail = _head;
_size++;
}
else if (_size != _elements.size())
{
_tail++;
if (_tail == _elements.size())
_tail = 0;
_size++;
_elements[_tail] = val;
_head = (_head + 1) % capacity();
}
else
{
_head++;
if (_head == _elements.size())
_head = 0;
_tail++;
if (_tail == _elements.size())
_tail = 0;
_elements[_tail] = val;
_size++;
}
_elements[_tail] = val;
_tail = (_tail + 1) % capacity();
}
private: