Skip to content

Commit

Permalink
[wpiutil] Make circular buffer classes constexpr (wpilibsuite#7232)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
calcmogul authored Oct 20, 2024
1 parent dd72a78 commit 5ac132f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 70 deletions.
82 changes: 44 additions & 38 deletions wpiutil/src/main/native/include/wpi/circular_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ class circular_buffer {
*
* @param size Maximum number of buffer elements.
*/
explicit circular_buffer(size_t size) : m_data(size, T{}) {}
constexpr explicit circular_buffer(size_t size) : m_data(size, T{}) {}

circular_buffer(const circular_buffer&) = default;
circular_buffer& operator=(const circular_buffer&) = default;
circular_buffer(circular_buffer&&) = default;
circular_buffer& operator=(circular_buffer&&) = default;
constexpr circular_buffer(const circular_buffer&) = default;
constexpr circular_buffer& operator=(const circular_buffer&) = default;
constexpr circular_buffer(circular_buffer&&) = default;
constexpr circular_buffer& operator=(circular_buffer&&) = default;

class iterator {
public:
Expand All @@ -39,20 +39,20 @@ class circular_buffer {
using pointer = T*;
using reference = T&;

iterator(circular_buffer* buffer, size_t index)
constexpr iterator(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:
circular_buffer* m_buffer;
Expand All @@ -67,69 +67,73 @@ class circular_buffer {
using pointer = T*;
using const_reference = const T&;

const_iterator(const circular_buffer* buffer, size_t index)
constexpr const_iterator(const 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 circular_buffer* m_buffer;
size_t m_index;
};

iterator begin() { return iterator(this, 0); }
iterator end() { return iterator(this, ::wpi::circular_buffer<T>::size()); }
constexpr iterator begin() { return iterator(this, 0); }
constexpr iterator end() {
return iterator(this, ::wpi::circular_buffer<T>::size());
}

const_iterator begin() const { return const_iterator(this, 0); }
const_iterator end() const {
constexpr const_iterator begin() const { return const_iterator(this, 0); }
constexpr const_iterator end() const {
return const_iterator(this, ::wpi::circular_buffer<T>::size());
}

const_iterator cbegin() const { return const_iterator(this, 0); }
const_iterator cend() const {
constexpr const_iterator cbegin() const { return const_iterator(this, 0); }
constexpr const_iterator cend() const {
return const_iterator(this, ::wpi::circular_buffer<T>::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
*
* 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) % m_data.size()]; }
constexpr T& back() {
return m_data[(m_front + m_length - 1) % m_data.size()];
}

/**
* Returns value at back of buffer
*
* If there are no elements in the buffer, calling this function results in
* undefined behavior.
*/
const T& back() const {
constexpr const T& back() const {
return m_data[(m_front + m_length - 1) % m_data.size()];
}

Expand All @@ -138,7 +142,7 @@ class circular_buffer {
*
* The value at the back is overwritten if the buffer is full.
*/
void push_front(T value) {
constexpr void push_front(T value) {
if (m_data.size() == 0) {
return;
}
Expand All @@ -157,7 +161,7 @@ class 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) {
if (m_data.size() == 0) {
return;
}
Expand All @@ -179,7 +183,7 @@ class 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) {
if (m_data.size() == 0) {
return;
}
Expand All @@ -200,7 +204,7 @@ class 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) {
if (m_data.size() == 0) {
return;
}
Expand All @@ -221,7 +225,7 @@ class 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--;
Expand All @@ -234,15 +238,15 @@ class 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) % m_data.size()];
}

/**
* Resizes internal buffer to given size.
*/
void resize(size_t size) {
constexpr void resize(size_t size) {
if (size > m_data.size()) {
// Find end of buffer
size_t insertLocation = (m_front + m_length) % m_data.size();
Expand Down Expand Up @@ -294,22 +298,22 @@ class circular_buffer {
/**
* Empties internal buffer.
*/
void reset() {
constexpr void reset() {
m_front = 0;
m_length = 0;
}

/**
* @return Element at index starting from front of buffer.
*/
T& operator[](size_t index) {
constexpr T& operator[](size_t index) {
return m_data[(m_front + index) % m_data.size()];
}

/**
* @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) % m_data.size()];
}

Expand All @@ -328,15 +332,17 @@ class circular_buffer {
* @param index Index into the buffer.
* @return The incremented index.
*/
size_t ModuloInc(size_t index) { return (index + 1) % m_data.size(); }
constexpr size_t ModuloInc(size_t index) {
return (index + 1) % m_data.size();
}

/**
* Decrement an index modulo the size of the buffer.
*
* @param index Index into the buffer.
* @return The decremented index.
*/
size_t ModuloDec(size_t index) {
constexpr size_t ModuloDec(size_t index) {
if (index == 0) {
return m_data.size() - 1;
} else {
Expand Down
Loading

0 comments on commit 5ac132f

Please sign in to comment.