mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
[wpiutil] Improve wpi::circular_buffer iterators (#3410)
The implementation of wpi::circular_buffer has been effectively replaced with a dynamically sized copy of wpi::static_circular_buffer with a resize() member function.
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
namespace wpi {
|
||||
@@ -16,44 +17,284 @@ namespace wpi {
|
||||
template <class T>
|
||||
class circular_buffer {
|
||||
public:
|
||||
explicit circular_buffer(size_t size);
|
||||
explicit circular_buffer(size_t size) : m_data(size, T{}) {}
|
||||
|
||||
using value_type = T;
|
||||
using reference = value_type&;
|
||||
using const_reference = const value_type&;
|
||||
using pointer = value_type*;
|
||||
using size_type = size_t;
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using difference_type = std::ptrdiff_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;
|
||||
|
||||
size_type size() const;
|
||||
T& front();
|
||||
const T& front() const;
|
||||
T& back();
|
||||
const T& back() const;
|
||||
void push_front(T value);
|
||||
void push_back(T value);
|
||||
T pop_front();
|
||||
T pop_back();
|
||||
class iterator {
|
||||
public:
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using value_type = T;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = T*;
|
||||
using reference = T&;
|
||||
|
||||
iterator(circular_buffer* buffer, size_t index)
|
||||
: m_buffer(buffer), m_index(index) {}
|
||||
|
||||
iterator& operator++() {
|
||||
++m_index;
|
||||
return *this;
|
||||
}
|
||||
iterator operator++(int) {
|
||||
iterator retval = *this;
|
||||
++(*this);
|
||||
return retval;
|
||||
}
|
||||
bool operator==(const iterator& other) const {
|
||||
return m_buffer == other.m_buffer && m_index == other.m_index;
|
||||
}
|
||||
bool operator!=(const iterator& other) const { return !(*this == other); }
|
||||
reference operator*() { return (*m_buffer)[m_index]; }
|
||||
|
||||
private:
|
||||
circular_buffer* m_buffer;
|
||||
size_t m_index;
|
||||
};
|
||||
|
||||
class const_iterator {
|
||||
public:
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using value_type = T;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = T*;
|
||||
using const_reference = const T&;
|
||||
|
||||
const_iterator(const circular_buffer* buffer, size_t index)
|
||||
: m_buffer(buffer), m_index(index) {}
|
||||
|
||||
const_iterator& operator++() {
|
||||
++m_index;
|
||||
return *this;
|
||||
}
|
||||
const_iterator operator++(int) {
|
||||
const_iterator retval = *this;
|
||||
++(*this);
|
||||
return retval;
|
||||
}
|
||||
bool operator==(const const_iterator& other) const {
|
||||
return m_buffer == other.m_buffer && m_index == other.m_index;
|
||||
}
|
||||
bool operator!=(const const_iterator& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
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()); }
|
||||
|
||||
const_iterator begin() const { return const_iterator(this, 0); }
|
||||
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 {
|
||||
return const_iterator(this, ::wpi::circular_buffer<T>::size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns number of elements in buffer
|
||||
*/
|
||||
size_t size() const { return m_length; }
|
||||
|
||||
/**
|
||||
* Returns value at front of buffer
|
||||
*/
|
||||
T& front() { return (*this)[0]; }
|
||||
|
||||
/**
|
||||
* Returns value at front of buffer
|
||||
*/
|
||||
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()]; }
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
return m_data[(m_front + m_length - 1) % m_data.size()];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
if (m_data.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_front = ModuloDec(m_front);
|
||||
|
||||
m_data[m_front] = value;
|
||||
|
||||
if (m_length < m_data.size()) {
|
||||
m_length++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Push a new value onto the back of the buffer.
|
||||
*
|
||||
* The value at the front is overwritten if the buffer is full.
|
||||
*/
|
||||
void push_back(T value) {
|
||||
if (m_data.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_data[(m_front + m_length) % m_data.size()] = value;
|
||||
|
||||
if (m_length < m_data.size()) {
|
||||
m_length++;
|
||||
} else {
|
||||
// Increment front if buffer is full to maintain size
|
||||
m_front = ModuloInc(m_front);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Push a new value onto the front of the buffer that is constructed with the
|
||||
* provided constructor arguments.
|
||||
*
|
||||
* The value at the back is overwritten if the buffer is full.
|
||||
*/
|
||||
template <class... Args>
|
||||
void emplace_front(Args&&... args) {
|
||||
if (m_data.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_front = ModuloDec(m_front);
|
||||
|
||||
m_data[m_front] = T{args...};
|
||||
|
||||
if (m_length < m_data.size()) {
|
||||
m_length++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Push a new value onto the back of the buffer that is constructed with the
|
||||
* provided constructor arguments.
|
||||
*
|
||||
* The value at the front is overwritten if the buffer is full.
|
||||
*/
|
||||
template <class... Args>
|
||||
void emplace_back(Args&&... args) {
|
||||
if (m_data.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_data[(m_front + m_length) % m_data.size()] = T{args...};
|
||||
|
||||
if (m_length < m_data.size()) {
|
||||
m_length++;
|
||||
} else {
|
||||
// Increment front if buffer is full to maintain size
|
||||
m_front = ModuloInc(m_front);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pop value at front of buffer.
|
||||
*
|
||||
* If there are no elements in the buffer, calling this function results in
|
||||
* undefined behavior.
|
||||
*/
|
||||
T pop_front() {
|
||||
T& temp = m_data[m_front];
|
||||
m_front = ModuloInc(m_front);
|
||||
m_length--;
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pop value at back of buffer.
|
||||
*
|
||||
* If there are no elements in the buffer, calling this function results in
|
||||
* undefined behavior.
|
||||
*/
|
||||
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);
|
||||
void reset();
|
||||
|
||||
T& operator[](size_t index);
|
||||
const T& operator[](size_t index) const;
|
||||
/**
|
||||
* Empties internal buffer.
|
||||
*/
|
||||
void reset() {
|
||||
m_front = 0;
|
||||
m_length = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Element at index starting from front of buffer.
|
||||
*/
|
||||
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 {
|
||||
return m_data[(m_front + index) % m_data.size()];
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<T> m_data;
|
||||
|
||||
T zero_val{0};
|
||||
|
||||
// Index of element at front of buffer
|
||||
size_t m_front = 0;
|
||||
|
||||
// Number of elements used in buffer
|
||||
size_t m_length = 0;
|
||||
|
||||
size_t ModuloInc(size_t index);
|
||||
size_t ModuloDec(size_t index);
|
||||
/**
|
||||
* Increment an index modulo the length of the buffer.
|
||||
*
|
||||
* @return The result of the modulo operation.
|
||||
*/
|
||||
size_t ModuloInc(size_t index) { return (index + 1) % m_data.size(); }
|
||||
|
||||
/**
|
||||
* Decrement an index modulo the length of the buffer.
|
||||
*
|
||||
* @return The result of the modulo operation.
|
||||
*/
|
||||
size_t ModuloDec(size_t index) {
|
||||
if (index == 0) {
|
||||
return m_data.size() - 1;
|
||||
} else {
|
||||
return index - 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace wpi
|
||||
|
||||
@@ -4,134 +4,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "wpi/circular_buffer.h"
|
||||
|
||||
namespace wpi {
|
||||
|
||||
template <class T>
|
||||
circular_buffer<T>::circular_buffer(size_t size) : m_data(size, T{}) {}
|
||||
|
||||
/**
|
||||
* Returns number of elements in buffer
|
||||
*/
|
||||
template <class T>
|
||||
typename circular_buffer<T>::size_type circular_buffer<T>::size() const {
|
||||
return m_length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns value at front of buffer
|
||||
*/
|
||||
template <class T>
|
||||
T& circular_buffer<T>::front() {
|
||||
return (*this)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns value at front of buffer
|
||||
*/
|
||||
template <class T>
|
||||
const T& circular_buffer<T>::front() const {
|
||||
return (*this)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns value at back of buffer
|
||||
*/
|
||||
template <class T>
|
||||
T& circular_buffer<T>::back() {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return zero_val;
|
||||
}
|
||||
|
||||
return m_data[(m_front + m_length - 1) % m_data.size()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns value at back of buffer
|
||||
*/
|
||||
template <class T>
|
||||
const T& circular_buffer<T>::back() const {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return zero_val;
|
||||
}
|
||||
|
||||
return m_data[(m_front + m_length - 1) % m_data.size()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Push new value onto front of the buffer. The value at the back is overwritten
|
||||
* if the buffer is full.
|
||||
*/
|
||||
template <class T>
|
||||
void circular_buffer<T>::push_front(T value) {
|
||||
if (m_data.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_front = ModuloDec(m_front);
|
||||
|
||||
m_data[m_front] = value;
|
||||
|
||||
if (m_length < m_data.size()) {
|
||||
m_length++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Push new value onto back of the buffer. The value at the front is overwritten
|
||||
* if the buffer is full.
|
||||
*/
|
||||
template <class T>
|
||||
void circular_buffer<T>::push_back(T value) {
|
||||
if (m_data.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_data[(m_front + m_length) % m_data.size()] = value;
|
||||
|
||||
if (m_length < m_data.size()) {
|
||||
m_length++;
|
||||
} else {
|
||||
// Increment front if buffer is full to maintain size
|
||||
m_front = ModuloInc(m_front);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pop value at front of buffer.
|
||||
*/
|
||||
template <class T>
|
||||
T circular_buffer<T>::pop_front() {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return T{0};
|
||||
}
|
||||
|
||||
T& temp = m_data[m_front];
|
||||
m_front = ModuloInc(m_front);
|
||||
m_length--;
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pop value at back of buffer.
|
||||
*/
|
||||
template <class T>
|
||||
T circular_buffer<T>::pop_back() {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return T{0};
|
||||
}
|
||||
|
||||
m_length--;
|
||||
return m_data[(m_front + m_length) % m_data.size()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Resizes internal buffer to given size.
|
||||
*/
|
||||
@@ -185,54 +61,4 @@ void circular_buffer<T>::resize(size_t size) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets internal buffer contents to zero.
|
||||
*/
|
||||
template <class T>
|
||||
void circular_buffer<T>::reset() {
|
||||
std::fill(m_data.begin(), m_data.end(), T{0});
|
||||
m_front = 0;
|
||||
m_length = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Element at index starting from front of buffer.
|
||||
*/
|
||||
template <class T>
|
||||
T& circular_buffer<T>::operator[](size_t index) {
|
||||
return m_data[(m_front + index) % m_data.size()];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Element at index starting from front of buffer.
|
||||
*/
|
||||
template <class T>
|
||||
const T& circular_buffer<T>::operator[](size_t index) const {
|
||||
return m_data[(m_front + index) % m_data.size()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment an index modulo the length of the buffer.
|
||||
*
|
||||
* @return The result of the modulo operation.
|
||||
*/
|
||||
template <class T>
|
||||
size_t circular_buffer<T>::ModuloInc(size_t index) {
|
||||
return (index + 1) % m_data.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement an index modulo the length of the buffer.
|
||||
*
|
||||
* @return The result of the modulo operation.
|
||||
*/
|
||||
template <class T>
|
||||
size_t circular_buffer<T>::ModuloDec(size_t index) {
|
||||
if (index == 0) {
|
||||
return m_data.size() - 1;
|
||||
} else {
|
||||
return index - 1;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace wpi
|
||||
|
||||
@@ -16,9 +16,7 @@ namespace wpi {
|
||||
template <class T, size_t N>
|
||||
class static_circular_buffer {
|
||||
public:
|
||||
static_assert(N > 0, "The circular buffer size shouldn't be zero.");
|
||||
|
||||
constexpr static_circular_buffer() = default;
|
||||
static_assert(N > 0, "Circular buffer size cannot be zero.");
|
||||
|
||||
class iterator {
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user