mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
Simplify logic for circular_buffer<T>::set_capacity
This commit is contained in:
parent
ea9bd9ee32
commit
4616450af1
@ -73,40 +73,20 @@ namespace sqlpp
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void circular_buffer<T>::set_capacity(std::size_t capacity)
|
||||
void circular_buffer<T>::set_capacity(std::size_t new_capacity)
|
||||
{
|
||||
if (capacity == _capacity)
|
||||
if (new_capacity == _capacity)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (_tail >= _head)
|
||||
{
|
||||
if (empty() == false)
|
||||
{
|
||||
std::rotate(_data.begin(), _data.begin() + _tail, _data.end());
|
||||
}
|
||||
_head = (capacity > _size) ? _size : 0;
|
||||
_tail = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (capacity < _head)
|
||||
{
|
||||
std::rotate(_data.begin(), _data.begin() + _tail, _data.begin() + _head);
|
||||
_head = (capacity > _size) ? _size : 0;
|
||||
_tail = 0;
|
||||
}
|
||||
else if (capacity == _head)
|
||||
{
|
||||
_head = 0;
|
||||
}
|
||||
}
|
||||
_data.resize(capacity);
|
||||
_capacity = capacity;
|
||||
if (_size > capacity)
|
||||
{
|
||||
_size = capacity;
|
||||
}
|
||||
// Reduce the number of cases by rotating `front()` to `_data.begin()`.
|
||||
std::rotate(_data.begin(), _data.begin() + _tail, _data.end());
|
||||
|
||||
_tail = 0;
|
||||
_head = (new_capacity > _size) ? _size : 0;
|
||||
_data.resize(new_capacity);
|
||||
_capacity = new_capacity;
|
||||
_size = std::min(_size, new_capacity);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
|
Loading…
Reference in New Issue
Block a user