From a26e9231cf16b7fb69d9c36b34194cacbcb7fa74 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 3 Feb 2019 22:34:48 +0100 Subject: [PATCH] Simplify CircularBuffer --- src/openrct2/core/CircularBuffer.h | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/src/openrct2/core/CircularBuffer.h b/src/openrct2/core/CircularBuffer.h index c4d108418b..ff18ad7eae 100644 --- a/src/openrct2/core/CircularBuffer.h +++ b/src/openrct2/core/CircularBuffer.h @@ -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: