From 4616450af1f6c39f33963e9835531f4d99077623 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sun, 1 Oct 2023 15:10:15 +0200 Subject: [PATCH] Simplify logic for circular_buffer::set_capacity --- include/sqlpp11/detail/circular_buffer.h | 40 ++++++------------------ 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/include/sqlpp11/detail/circular_buffer.h b/include/sqlpp11/detail/circular_buffer.h index df540e81..994f8c35 100644 --- a/include/sqlpp11/detail/circular_buffer.h +++ b/include/sqlpp11/detail/circular_buffer.h @@ -73,40 +73,20 @@ namespace sqlpp } template - void circular_buffer::set_capacity(std::size_t capacity) + void circular_buffer::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