[wpiutil] Make circular buffer classes constexpr (#7232)

The circular buffer class that uses std::vector internally can be used
in a constant expression as long as it doesn't survive to runtime.
This commit is contained in:
Tyler Veness
2024-10-20 15:33:40 -07:00
committed by GitHub
parent dd72a78aa4
commit 5ac132f6a2
2 changed files with 80 additions and 70 deletions

View File

@@ -30,20 +30,20 @@ class static_circular_buffer {
using pointer = T*;
using reference = T&;
iterator(static_circular_buffer* buffer, size_t index)
constexpr iterator(static_circular_buffer* buffer, size_t index)
: m_buffer{buffer}, m_index{index} {}
iterator& operator++() {
constexpr iterator& operator++() {
++m_index;
return *this;
}
iterator operator++(int) {
constexpr iterator operator++(int) {
iterator retval = *this;
++(*this);
return retval;
}
bool operator==(const iterator&) const = default;
reference operator*() { return (*m_buffer)[m_index]; }
constexpr bool operator==(const iterator&) const = default;
constexpr reference operator*() { return (*m_buffer)[m_index]; }
private:
static_circular_buffer* m_buffer;
@@ -58,20 +58,20 @@ class static_circular_buffer {
using pointer = T*;
using const_reference = const T&;
const_iterator(const static_circular_buffer* buffer, size_t index)
constexpr const_iterator(const static_circular_buffer* buffer, size_t index)
: m_buffer{buffer}, m_index{index} {}
const_iterator& operator++() {
constexpr const_iterator& operator++() {
++m_index;
return *this;
}
const_iterator operator++(int) {
constexpr const_iterator operator++(int) {
const_iterator retval = *this;
++(*this);
return retval;
}
bool operator==(const const_iterator&) const = default;
const_reference operator*() const { return (*m_buffer)[m_index]; }
constexpr bool operator==(const const_iterator&) const = default;
constexpr const_reference operator*() const { return (*m_buffer)[m_index]; }
private:
const static_circular_buffer* m_buffer;
@@ -81,53 +81,53 @@ class static_circular_buffer {
/**
* Returns begin iterator.
*/
iterator begin() { return iterator(this, 0); }
constexpr iterator begin() { return iterator(this, 0); }
/**
* Returns end iterator.
*/
iterator end() {
constexpr iterator end() {
return iterator(this, ::wpi::static_circular_buffer<T, N>::size());
}
/**
* Returns begin iterator.
*/
const_iterator begin() const { return const_iterator(this, 0); }
constexpr const_iterator begin() const { return const_iterator(this, 0); }
/**
* Returns end iterator.
*/
const_iterator end() const {
constexpr const_iterator end() const {
return const_iterator(this, ::wpi::static_circular_buffer<T, N>::size());
}
/**
* Returns begin iterator.
*/
const_iterator cbegin() const { return const_iterator(this, 0); }
constexpr const_iterator cbegin() const { return const_iterator(this, 0); }
/**
* Returns end iterator.
*/
const_iterator cend() const {
constexpr const_iterator cend() const {
return const_iterator(this, ::wpi::static_circular_buffer<T, N>::size());
}
/**
* Returns number of elements in buffer
*/
size_t size() const { return m_length; }
constexpr size_t size() const { return m_length; }
/**
* Returns value at front of buffer
*/
T& front() { return (*this)[0]; }
constexpr T& front() { return (*this)[0]; }
/**
* Returns value at front of buffer
*/
const T& front() const { return (*this)[0]; }
constexpr const T& front() const { return (*this)[0]; }
/**
* Returns value at back of buffer
@@ -135,7 +135,7 @@ class static_circular_buffer {
* If there are no elements in the buffer, calling this function results in
* undefined behavior.
*/
T& back() { return m_data[(m_front + m_length - 1) % N]; }
constexpr T& back() { return m_data[(m_front + m_length - 1) % N]; }
/**
* Returns value at back of buffer
@@ -143,14 +143,16 @@ class static_circular_buffer {
* If there are no elements in the buffer, calling this function results in
* undefined behavior.
*/
const T& back() const { return m_data[(m_front + m_length - 1) % N]; }
constexpr const T& back() const {
return m_data[(m_front + m_length - 1) % N];
}
/**
* Push a new value onto the front of the buffer.
*
* The value at the back is overwritten if the buffer is full.
*/
void push_front(T value) {
constexpr void push_front(T value) {
m_front = ModuloDec(m_front);
m_data[m_front] = value;
@@ -165,7 +167,7 @@ class static_circular_buffer {
*
* The value at the front is overwritten if the buffer is full.
*/
void push_back(T value) {
constexpr void push_back(T value) {
m_data[(m_front + m_length) % N] = value;
if (m_length < N) {
@@ -183,7 +185,7 @@ class static_circular_buffer {
* The value at the back is overwritten if the buffer is full.
*/
template <class... Args>
void emplace_front(Args&&... args) {
constexpr void emplace_front(Args&&... args) {
m_front = ModuloDec(m_front);
m_data[m_front] = T{args...};
@@ -200,7 +202,7 @@ class static_circular_buffer {
* The value at the front is overwritten if the buffer is full.
*/
template <class... Args>
void emplace_back(Args&&... args) {
constexpr void emplace_back(Args&&... args) {
m_data[(m_front + m_length) % N] = T{args...};
if (m_length < N) {
@@ -217,7 +219,7 @@ class static_circular_buffer {
* If there are no elements in the buffer, calling this function results in
* undefined behavior.
*/
T pop_front() {
constexpr T pop_front() {
T& temp = m_data[m_front];
m_front = ModuloInc(m_front);
m_length--;
@@ -230,7 +232,7 @@ class static_circular_buffer {
* If there are no elements in the buffer, calling this function results in
* undefined behavior.
*/
T pop_back() {
constexpr T pop_back() {
m_length--;
return m_data[(m_front + m_length) % N];
}
@@ -238,7 +240,7 @@ class static_circular_buffer {
/**
* Empties internal buffer.
*/
void reset() {
constexpr void reset() {
m_front = 0;
m_length = 0;
}
@@ -246,12 +248,14 @@ class static_circular_buffer {
/**
* @return Element at index starting from front of buffer.
*/
T& operator[](size_t index) { return m_data[(m_front + index) % N]; }
constexpr T& operator[](size_t index) {
return m_data[(m_front + index) % N];
}
/**
* @return Element at index starting from front of buffer.
*/
const T& operator[](size_t index) const {
constexpr const T& operator[](size_t index) const {
return m_data[(m_front + index) % N];
}
@@ -269,14 +273,14 @@ class static_circular_buffer {
*
* @return The result of the modulo operation.
*/
size_t ModuloInc(size_t index) { return (index + 1) % N; }
constexpr size_t ModuloInc(size_t index) { return (index + 1) % N; }
/**
* Decrement an index modulo the length of the buffer.
*
* @return The result of the modulo operation.
*/
size_t ModuloDec(size_t index) {
constexpr size_t ModuloDec(size_t index) {
if (index == 0) {
return N - 1;
} else {