mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
CircularBuffer now uses an idiomatic interface in C++ and Java (#421)
This commit is contained in:
committed by
Peter Johnson
parent
029246ed28
commit
85157a56c3
@@ -12,8 +12,8 @@
|
||||
|
||||
#include <llvm/ArrayRef.h>
|
||||
|
||||
#include "CircularBuffer.h"
|
||||
#include "Filter.h"
|
||||
#include "circular_buffer.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
@@ -89,8 +89,8 @@ class LinearDigitalFilter : public Filter {
|
||||
double PIDGet() override;
|
||||
|
||||
private:
|
||||
CircularBuffer<double> m_inputs;
|
||||
CircularBuffer<double> m_outputs;
|
||||
circular_buffer<double> m_inputs;
|
||||
circular_buffer<double> m_outputs;
|
||||
std::vector<double> m_inputGains;
|
||||
std::vector<double> m_outputGains;
|
||||
};
|
||||
|
||||
@@ -17,16 +17,29 @@ namespace frc {
|
||||
* old values.
|
||||
*/
|
||||
template <class T>
|
||||
class CircularBuffer {
|
||||
class circular_buffer {
|
||||
public:
|
||||
explicit CircularBuffer(size_t size);
|
||||
explicit circular_buffer(size_t size);
|
||||
|
||||
void PushFront(T value);
|
||||
void PushBack(T value);
|
||||
T PopFront();
|
||||
T PopBack();
|
||||
void Resize(size_t size);
|
||||
void Reset();
|
||||
typedef T value_type;
|
||||
typedef value_type& reference;
|
||||
typedef const value_type& const_reference;
|
||||
typedef value_type* pointer;
|
||||
typedef size_t size_type;
|
||||
typedef std::forward_iterator_tag iterator_category;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
|
||||
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();
|
||||
void resize(size_t size);
|
||||
void reset();
|
||||
|
||||
T& operator[](size_t index);
|
||||
const T& operator[](size_t index) const;
|
||||
@@ -46,4 +59,4 @@ class CircularBuffer {
|
||||
|
||||
} // namespace frc
|
||||
|
||||
#include "CircularBuffer.inc"
|
||||
#include "circular_buffer.inc"
|
||||
@@ -12,14 +12,64 @@
|
||||
namespace frc {
|
||||
|
||||
template <class T>
|
||||
CircularBuffer<T>::CircularBuffer(size_t size) : m_data(size, 0) {}
|
||||
circular_buffer<T>::circular_buffer(size_t size) : m_data(size, 0) {}
|
||||
|
||||
/**
|
||||
* 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 0;
|
||||
}
|
||||
|
||||
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 0;
|
||||
}
|
||||
|
||||
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 CircularBuffer<T>::PushFront(T value) {
|
||||
void circular_buffer<T>::push_front(T value) {
|
||||
if (m_data.size() == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -38,7 +88,7 @@ void CircularBuffer<T>::PushFront(T value) {
|
||||
* if the buffer is full.
|
||||
*/
|
||||
template <class T>
|
||||
void CircularBuffer<T>::PushBack(T value) {
|
||||
void circular_buffer<T>::push_back(T value) {
|
||||
if (m_data.size() == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -57,7 +107,7 @@ void CircularBuffer<T>::PushBack(T value) {
|
||||
* Pop value at front of buffer.
|
||||
*/
|
||||
template <class T>
|
||||
T CircularBuffer<T>::PopFront() {
|
||||
T circular_buffer<T>::pop_front() {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return 0;
|
||||
@@ -73,7 +123,7 @@ T CircularBuffer<T>::PopFront() {
|
||||
* Pop value at back of buffer.
|
||||
*/
|
||||
template <class T>
|
||||
T CircularBuffer<T>::PopBack() {
|
||||
T circular_buffer<T>::pop_back() {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return 0;
|
||||
@@ -87,7 +137,7 @@ T CircularBuffer<T>::PopBack() {
|
||||
* Resizes internal buffer to given size.
|
||||
*/
|
||||
template <class T>
|
||||
void CircularBuffer<T>::Resize(size_t size) {
|
||||
void circular_buffer<T>::resize(size_t size) {
|
||||
if (size > m_data.size()) {
|
||||
// Find end of buffer
|
||||
size_t insertLocation = (m_front + m_length) % m_data.size();
|
||||
@@ -140,7 +190,7 @@ void CircularBuffer<T>::Resize(size_t size) {
|
||||
* Sets internal buffer contents to zero.
|
||||
*/
|
||||
template <class T>
|
||||
void CircularBuffer<T>::Reset() {
|
||||
void circular_buffer<T>::reset() {
|
||||
std::fill(m_data.begin(), m_data.end(), 0);
|
||||
m_front = 0;
|
||||
m_length = 0;
|
||||
@@ -150,7 +200,7 @@ void CircularBuffer<T>::Reset() {
|
||||
* @return Element at index starting from front of buffer.
|
||||
*/
|
||||
template <class T>
|
||||
T& CircularBuffer<T>::operator[](size_t index) {
|
||||
T& circular_buffer<T>::operator[](size_t index) {
|
||||
return m_data[(m_front + index) % m_data.size()];
|
||||
}
|
||||
|
||||
@@ -158,7 +208,7 @@ T& CircularBuffer<T>::operator[](size_t index) {
|
||||
* @return Element at index starting from front of buffer.
|
||||
*/
|
||||
template <class T>
|
||||
const T& CircularBuffer<T>::operator[](size_t index) const {
|
||||
const T& circular_buffer<T>::operator[](size_t index) const {
|
||||
return m_data[(m_front + index) % m_data.size()];
|
||||
}
|
||||
|
||||
@@ -168,7 +218,7 @@ const T& CircularBuffer<T>::operator[](size_t index) const {
|
||||
* @return The result of the modulo operation.
|
||||
*/
|
||||
template <class T>
|
||||
size_t CircularBuffer<T>::ModuloInc(size_t index) {
|
||||
size_t circular_buffer<T>::ModuloInc(size_t index) {
|
||||
return (index + 1) % m_data.size();
|
||||
}
|
||||
|
||||
@@ -178,7 +228,7 @@ size_t CircularBuffer<T>::ModuloInc(size_t index) {
|
||||
* @return The result of the modulo operation.
|
||||
*/
|
||||
template <class T>
|
||||
size_t CircularBuffer<T>::ModuloDec(size_t index) {
|
||||
size_t circular_buffer<T>::ModuloDec(size_t index) {
|
||||
if (index == 0) {
|
||||
return m_data.size() - 1;
|
||||
} else {
|
||||
Reference in New Issue
Block a user