Move circular buffer class from wpilib to wpiutil (#1840)

This commit is contained in:
Tyler Veness
2019-08-24 21:35:19 -07:00
committed by Peter Johnson
parent 4cd8a56672
commit e3d86fee46
9 changed files with 26 additions and 28 deletions

View File

@@ -0,0 +1,186 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpiutil;
/**
* This is a simple circular buffer so we don't need to "bucket brigade" copy old values.
*/
public class CircularBuffer {
private double[] m_data;
// Index of element at front of buffer
private int m_front;
// Number of elements used in buffer
private int m_length;
/**
* Create a CircularBuffer with the provided size.
*
* @param size The size of the circular buffer.
*/
public CircularBuffer(int size) {
m_data = new double[size];
for (int i = 0; i < m_data.length; i++) {
m_data[i] = 0.0;
}
}
/**
* Returns number of elements in buffer.
*
* @return number of elements in buffer
*/
double size() {
return m_length;
}
/**
* Get value at front of buffer.
*
* @return value at front of buffer
*/
double getFirst() {
return m_data[m_front];
}
/**
* Get value at back of buffer.
*
* @return value at back of buffer
*/
double getLast() {
// If there are no elements in the buffer, do nothing
if (m_length == 0) {
return 0.0;
}
return m_data[(m_front + m_length - 1) % m_data.length];
}
/**
* Push new value onto front of the buffer. The value at the back is overwritten if the buffer is
* full.
*/
public void addFirst(double value) {
if (m_data.length == 0) {
return;
}
m_front = moduloDec(m_front);
m_data[m_front] = value;
if (m_length < m_data.length) {
m_length++;
}
}
/**
* Push new value onto back of the buffer. The value at the front is overwritten if the buffer is
* full.
*/
public void addLast(double value) {
if (m_data.length == 0) {
return;
}
m_data[(m_front + m_length) % m_data.length] = value;
if (m_length < m_data.length) {
m_length++;
} else {
// Increment front if buffer is full to maintain size
m_front = moduloInc(m_front);
}
}
/**
* Pop value at front of buffer.
*
* @return value at front of buffer
*/
public double removeFirst() {
// If there are no elements in the buffer, do nothing
if (m_length == 0) {
return 0.0;
}
double temp = m_data[m_front];
m_front = moduloInc(m_front);
m_length--;
return temp;
}
/**
* Pop value at back of buffer.
*/
public double removeLast() {
// If there are no elements in the buffer, do nothing
if (m_length == 0) {
return 0.0;
}
m_length--;
return m_data[(m_front + m_length) % m_data.length];
}
/**
* Resizes internal buffer to given size.
*
* <p>A new buffer is allocated because arrays are not resizable.
*/
void resize(int size) {
double[] newBuffer = new double[size];
m_length = Math.min(m_length, size);
for (int i = 0; i < m_length; i++) {
newBuffer[i] = m_data[(m_front + i) % m_data.length];
}
m_data = newBuffer;
m_front = 0;
}
/**
* Sets internal buffer contents to zero.
*/
public void clear() {
for (int i = 0; i < m_data.length; i++) {
m_data[i] = 0.0;
}
m_front = 0;
m_length = 0;
}
/**
* Get the element at the provided index relative to the start of the buffer.
*
* @return Element at index starting from front of buffer.
*/
public double get(int index) {
return m_data[(m_front + index) % m_data.length];
}
/**
* Increment an index modulo the length of the m_data buffer.
*/
private int moduloInc(int index) {
return (index + 1) % m_data.length;
}
/**
* Decrement an index modulo the length of the m_data buffer.
*/
private int moduloDec(int index) {
if (index == 0) {
return m_data.length - 1;
} else {
return index - 1;
}
}
}

View File

@@ -0,0 +1,62 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <cstddef>
#include <vector>
namespace wpi {
/**
* This is a simple circular buffer so we don't need to "bucket brigade" copy
* old values.
*/
template <class T>
class circular_buffer {
public:
explicit circular_buffer(size_t size);
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;
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;
private:
std::vector<T> m_data;
// 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);
};
} // namespace wpi
#include "wpi/circular_buffer.inc"

View File

@@ -0,0 +1,239 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <algorithm>
namespace wpi {
template <class T>
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 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 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 0;
}
m_length--;
return m_data[(m_front + m_length) % m_data.size()];
}
/**
* Resizes internal buffer to given size.
*/
template <class T>
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();
// If insertion location precedes front of buffer, push front index back
if (insertLocation <= m_front) {
m_front += size - m_data.size();
}
// Add elements to end of buffer
m_data.insert(m_data.begin() + insertLocation, size - m_data.size(), 0);
} else if (size < m_data.size()) {
/* 1) Shift element block start at "front" left as many blocks as were
* removed up to but not exceeding buffer[0]
* 2) Shrink buffer, which will remove even more elements automatically if
* necessary
*/
size_t elemsToRemove = m_data.size() - size;
auto frontIter = m_data.begin() + m_front;
if (m_front < elemsToRemove) {
/* Remove elements from end of buffer before shifting start of element
* block. Doing so saves a few copies.
*/
m_data.erase(frontIter + size, m_data.end());
// Shift start of element block to left
m_data.erase(m_data.begin(), frontIter);
// Update metadata
m_front = 0;
} else {
// Shift start of element block to left
m_data.erase(frontIter - elemsToRemove, frontIter);
// Update metadata
m_front -= elemsToRemove;
}
/* Length only changes during a shrink if all unused spaces have been
* removed. Length decreases as used spaces are removed to meet the
* required size.
*/
if (m_length > size) {
m_length = size;
}
}
}
/**
* Sets internal buffer contents to zero.
*/
template <class T>
void circular_buffer<T>::reset() {
std::fill(m_data.begin(), m_data.end(), 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

View File

@@ -0,0 +1,214 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpiutil;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class CircularBufferTest {
private final double[] m_values = {751.848, 766.366, 342.657, 234.252, 716.126,
132.344, 445.697, 22.727, 421.125, 799.913};
private final double[] m_addFirstOut = {799.913, 421.125, 22.727, 445.697, 132.344,
716.126, 234.252, 342.657};
private final double[] m_addLastOut = {342.657, 234.252, 716.126, 132.344, 445.697,
22.727, 421.125, 799.913};
@Test
void addFirstTest() {
CircularBuffer queue = new CircularBuffer(8);
for (double value : m_values) {
queue.addFirst(value);
}
for (int i = 0; i < m_addFirstOut.length; i++) {
assertEquals(m_addFirstOut[i], queue.get(i), 0.00005);
}
}
@Test
void addLastTest() {
CircularBuffer queue = new CircularBuffer(8);
for (double value : m_values) {
queue.addLast(value);
}
for (int i = 0; i < m_addLastOut.length; i++) {
assertEquals(m_addLastOut[i], queue.get(i), 0.00005);
}
}
@Test
void pushPopTest() {
CircularBuffer queue = new CircularBuffer(3);
// Insert three elements into the buffer
queue.addLast(1.0);
queue.addLast(2.0);
queue.addLast(3.0);
assertEquals(1.0, queue.get(0), 0.00005);
assertEquals(2.0, queue.get(1), 0.00005);
assertEquals(3.0, queue.get(2), 0.00005);
/*
* The buffer is full now, so pushing subsequent elements will overwrite the
* front-most elements.
*/
queue.addLast(4.0); // Overwrite 1 with 4
// The buffer now contains 2, 3, and 4
assertEquals(2.0, queue.get(0), 0.00005);
assertEquals(3.0, queue.get(1), 0.00005);
assertEquals(4.0, queue.get(2), 0.00005);
queue.addLast(5.0); // Overwrite 2 with 5
// The buffer now contains 3, 4, and 5
assertEquals(3.0, queue.get(0), 0.00005);
assertEquals(4.0, queue.get(1), 0.00005);
assertEquals(5.0, queue.get(2), 0.00005);
assertEquals(5.0, queue.removeLast(), 0.00005); // 5 is removed
// The buffer now contains 3 and 4
assertEquals(3.0, queue.get(0), 0.00005);
assertEquals(4.0, queue.get(1), 0.00005);
assertEquals(3.0, queue.removeFirst(), 0.00005); // 3 is removed
// Leaving only one element with value == 4
assertEquals(4.0, queue.get(0), 0.00005);
}
@Test
void resetTest() {
CircularBuffer queue = new CircularBuffer(5);
for (int i = 0; i < 6; i++) {
queue.addLast(i);
}
queue.clear();
for (int i = 0; i < 5; i++) {
assertEquals(0.0, queue.get(i), 0.00005);
}
}
@Test
@SuppressWarnings("PMD.ExcessiveMethodLength")
void resizeTest() {
CircularBuffer queue = new CircularBuffer(5);
/* Buffer contains {1, 2, 3, _, _}
* ^ front
*/
queue.addLast(1.0);
queue.addLast(2.0);
queue.addLast(3.0);
queue.resize(2);
assertEquals(1.0, queue.get(0), 0.00005);
assertEquals(2.0, queue.get(1), 0.00005);
queue.resize(5);
assertEquals(1.0, queue.get(0), 0.00005);
assertEquals(2.0, queue.get(1), 0.00005);
queue.clear();
/* Buffer contains {_, 1, 2, 3, _}
* ^ front
*/
queue.addLast(0.0);
queue.addLast(1.0);
queue.addLast(2.0);
queue.addLast(3.0);
queue.removeFirst();
queue.resize(2);
assertEquals(1.0, queue.get(0), 0.00005);
assertEquals(2.0, queue.get(1), 0.00005);
queue.resize(5);
assertEquals(1.0, queue.get(0), 0.00005);
assertEquals(2.0, queue.get(1), 0.00005);
queue.clear();
/* Buffer contains {_, _, 1, 2, 3}
* ^ front
*/
queue.addLast(0.0);
queue.addLast(0.0);
queue.addLast(1.0);
queue.addLast(2.0);
queue.addLast(3.0);
queue.removeFirst();
queue.removeFirst();
queue.resize(2);
assertEquals(1.0, queue.get(0), 0.00005);
assertEquals(2.0, queue.get(1), 0.00005);
queue.resize(5);
assertEquals(1.0, queue.get(0), 0.00005);
assertEquals(2.0, queue.get(1), 0.00005);
queue.clear();
/* Buffer contains {3, _, _, 1, 2}
* ^ front
*/
queue.addLast(3.0);
queue.addFirst(2.0);
queue.addFirst(1.0);
queue.resize(2);
assertEquals(1.0, queue.get(0), 0.00005);
assertEquals(2.0, queue.get(1), 0.00005);
queue.resize(5);
assertEquals(1.0, queue.get(0), 0.00005);
assertEquals(2.0, queue.get(1), 0.00005);
queue.clear();
/* Buffer contains {2, 3, _, _, 1}
* ^ front
*/
queue.addLast(2.0);
queue.addLast(3.0);
queue.addFirst(1.0);
queue.resize(2);
assertEquals(1.0, queue.get(0), 0.00005);
assertEquals(2.0, queue.get(1), 0.00005);
queue.resize(5);
assertEquals(1.0, queue.get(0), 0.00005);
assertEquals(2.0, queue.get(1), 0.00005);
// Test addLast() after resize
queue.addLast(3.0);
assertEquals(1.0, queue.get(0), 0.00005);
assertEquals(2.0, queue.get(1), 0.00005);
assertEquals(3.0, queue.get(2), 0.00005);
// Test addFirst() after resize
queue.addFirst(4.0);
assertEquals(4.0, queue.get(0), 0.00005);
assertEquals(1.0, queue.get(1), 0.00005);
assertEquals(2.0, queue.get(2), 0.00005);
assertEquals(3.0, queue.get(3), 0.00005);
}
}

View File

@@ -0,0 +1,209 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "wpi/circular_buffer.h" // NOLINT(build/include_order)
#include <array>
#include "gtest/gtest.h"
static const std::array<double, 10> values = {
{751.848, 766.366, 342.657, 234.252, 716.126, 132.344, 445.697, 22.727,
421.125, 799.913}};
static const std::array<double, 8> pushFrontOut = {
{799.913, 421.125, 22.727, 445.697, 132.344, 716.126, 234.252, 342.657}};
static const std::array<double, 8> pushBackOut = {
{342.657, 234.252, 716.126, 132.344, 445.697, 22.727, 421.125, 799.913}};
TEST(CircularBufferTest, PushFrontTest) {
wpi::circular_buffer<double> queue(8);
for (auto& value : values) {
queue.push_front(value);
}
for (size_t i = 0; i < pushFrontOut.size(); i++) {
EXPECT_EQ(pushFrontOut[i], queue[i]);
}
}
TEST(CircularBufferTest, PushBackTest) {
wpi::circular_buffer<double> queue(8);
for (auto& value : values) {
queue.push_back(value);
}
for (size_t i = 0; i < pushBackOut.size(); i++) {
EXPECT_EQ(pushBackOut[i], queue[i]);
}
}
TEST(CircularBufferTest, PushPopTest) {
wpi::circular_buffer<double> queue(3);
// Insert three elements into the buffer
queue.push_back(1.0);
queue.push_back(2.0);
queue.push_back(3.0);
EXPECT_EQ(1.0, queue[0]);
EXPECT_EQ(2.0, queue[1]);
EXPECT_EQ(3.0, queue[2]);
/*
* The buffer is full now, so pushing subsequent elements will overwrite the
* front-most elements.
*/
queue.push_back(4.0); // Overwrite 1 with 4
// The buffer now contains 2, 3 and 4
EXPECT_EQ(2.0, queue[0]);
EXPECT_EQ(3.0, queue[1]);
EXPECT_EQ(4.0, queue[2]);
queue.push_back(5.0); // Overwrite 2 with 5
// The buffer now contains 3, 4 and 5
EXPECT_EQ(3.0, queue[0]);
EXPECT_EQ(4.0, queue[1]);
EXPECT_EQ(5.0, queue[2]);
EXPECT_EQ(5.0, queue.pop_back()); // 5 is removed
// The buffer now contains 3 and 4
EXPECT_EQ(3.0, queue[0]);
EXPECT_EQ(4.0, queue[1]);
EXPECT_EQ(3.0, queue.pop_front()); // 3 is removed
// Leaving only one element with value == 4
EXPECT_EQ(4.0, queue[0]);
}
TEST(CircularBufferTest, ResetTest) {
wpi::circular_buffer<double> queue(5);
for (size_t i = 1; i < 6; i++) {
queue.push_back(i);
}
queue.reset();
for (size_t i = 0; i < 5; i++) {
EXPECT_EQ(0.0, queue[i]);
}
}
TEST(CircularBufferTest, ResizeTest) {
wpi::circular_buffer<double> queue(5);
/* Buffer contains {1, 2, 3, _, _}
* ^ front
*/
queue.push_back(1.0);
queue.push_back(2.0);
queue.push_back(3.0);
queue.resize(2);
EXPECT_EQ(1.0, queue[0]);
EXPECT_EQ(2.0, queue[1]);
queue.resize(5);
EXPECT_EQ(1.0, queue[0]);
EXPECT_EQ(2.0, queue[1]);
queue.reset();
/* Buffer contains {_, 1, 2, 3, _}
* ^ front
*/
queue.push_back(0.0);
queue.push_back(1.0);
queue.push_back(2.0);
queue.push_back(3.0);
queue.pop_front();
queue.resize(2);
EXPECT_EQ(1.0, queue[0]);
EXPECT_EQ(2.0, queue[1]);
queue.resize(5);
EXPECT_EQ(1.0, queue[0]);
EXPECT_EQ(2.0, queue[1]);
queue.reset();
/* Buffer contains {_, _, 1, 2, 3}
* ^ front
*/
queue.push_back(0.0);
queue.push_back(0.0);
queue.push_back(1.0);
queue.push_back(2.0);
queue.push_back(3.0);
queue.pop_front();
queue.pop_front();
queue.resize(2);
EXPECT_EQ(1.0, queue[0]);
EXPECT_EQ(2.0, queue[1]);
queue.resize(5);
EXPECT_EQ(1.0, queue[0]);
EXPECT_EQ(2.0, queue[1]);
queue.reset();
/* Buffer contains {3, _, _, 1, 2}
* ^ front
*/
queue.push_back(3.0);
queue.push_front(2.0);
queue.push_front(1.0);
queue.resize(2);
EXPECT_EQ(1.0, queue[0]);
EXPECT_EQ(2.0, queue[1]);
queue.resize(5);
EXPECT_EQ(1.0, queue[0]);
EXPECT_EQ(2.0, queue[1]);
queue.reset();
/* Buffer contains {2, 3, _, _, 1}
* ^ front
*/
queue.push_back(2.0);
queue.push_back(3.0);
queue.push_front(1.0);
queue.resize(2);
EXPECT_EQ(1.0, queue[0]);
EXPECT_EQ(2.0, queue[1]);
queue.resize(5);
EXPECT_EQ(1.0, queue[0]);
EXPECT_EQ(2.0, queue[1]);
// Test push_back() after resize
queue.push_back(3.0);
EXPECT_EQ(1.0, queue[0]);
EXPECT_EQ(2.0, queue[1]);
EXPECT_EQ(3.0, queue[2]);
// Test push_front() after resize
queue.push_front(4.0);
EXPECT_EQ(4.0, queue[0]);
EXPECT_EQ(1.0, queue[1]);
EXPECT_EQ(2.0, queue[2]);
EXPECT_EQ(3.0, queue[3]);
}