mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-04 03:11: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
@@ -31,11 +31,43 @@ public class CircularBuffer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 pushFront(double value) {
|
||||
public void addFirst(double value) {
|
||||
if (m_data.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -53,7 +85,7 @@ public class CircularBuffer {
|
||||
* Push new value onto back of the buffer. The value at the front is overwritten if the buffer is
|
||||
* full.
|
||||
*/
|
||||
public void pushBack(double value) {
|
||||
public void addLast(double value) {
|
||||
if (m_data.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -73,7 +105,7 @@ public class CircularBuffer {
|
||||
*
|
||||
* @return value at front of buffer
|
||||
*/
|
||||
public double popFront() {
|
||||
public double removeFirst() {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return 0.0;
|
||||
@@ -89,7 +121,7 @@ public class CircularBuffer {
|
||||
/**
|
||||
* Pop value at back of buffer.
|
||||
*/
|
||||
public double popBack() {
|
||||
public double removeLast() {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return 0.0;
|
||||
@@ -117,7 +149,7 @@ public class CircularBuffer {
|
||||
/**
|
||||
* Sets internal buffer contents to zero.
|
||||
*/
|
||||
public void reset() {
|
||||
public void clear() {
|
||||
for (int i = 0; i < m_data.length; i++) {
|
||||
m_data[i] = 0.0;
|
||||
}
|
||||
|
||||
@@ -151,8 +151,8 @@ public class LinearDigitalFilter extends Filter {
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
m_inputs.reset();
|
||||
m_outputs.reset();
|
||||
m_inputs.clear();
|
||||
m_outputs.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,7 +165,7 @@ public class LinearDigitalFilter extends Filter {
|
||||
double retVal = 0.0;
|
||||
|
||||
// Rotate the inputs
|
||||
m_inputs.pushFront(pidGetSource());
|
||||
m_inputs.addFirst(pidGetSource());
|
||||
|
||||
// Calculate the new value
|
||||
for (int i = 0; i < m_inputGains.length; i++) {
|
||||
@@ -176,7 +176,7 @@ public class LinearDigitalFilter extends Filter {
|
||||
}
|
||||
|
||||
// Rotate the outputs
|
||||
m_outputs.pushFront(retVal);
|
||||
m_outputs.addFirst(retVal);
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@@ -15,9 +15,9 @@ import static org.junit.Assert.assertEquals;
|
||||
public class CircularBufferTest {
|
||||
private double[] m_values = {751.848, 766.366, 342.657, 234.252, 716.126,
|
||||
132.344, 445.697, 22.727, 421.125, 799.913};
|
||||
private double[] m_pushFrontOut = {799.913, 421.125, 22.727, 445.697, 132.344,
|
||||
private double[] m_addFirstOut = {799.913, 421.125, 22.727, 445.697, 132.344,
|
||||
716.126, 234.252, 342.657};
|
||||
private double[] m_pushBackOut = {342.657, 234.252, 716.126, 132.344, 445.697,
|
||||
private double[] m_addLastOut = {342.657, 234.252, 716.126, 132.344, 445.697,
|
||||
22.727, 421.125, 799.913};
|
||||
|
||||
@BeforeClass
|
||||
@@ -26,28 +26,28 @@ public class CircularBufferTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pushFrontTest() {
|
||||
public void addFirstTest() {
|
||||
CircularBuffer queue = new CircularBuffer(8);
|
||||
|
||||
for (double value : m_values) {
|
||||
queue.pushFront(value);
|
||||
queue.addFirst(value);
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_pushFrontOut.length; i++) {
|
||||
assertEquals(m_pushFrontOut[i], queue.get(i), 0.00005);
|
||||
for (int i = 0; i < m_addFirstOut.length; i++) {
|
||||
assertEquals(m_addFirstOut[i], queue.get(i), 0.00005);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pushBackTest() {
|
||||
public void addLastTest() {
|
||||
CircularBuffer queue = new CircularBuffer(8);
|
||||
|
||||
for (double value : m_values) {
|
||||
queue.pushBack(value);
|
||||
queue.addLast(value);
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_pushBackOut.length; i++) {
|
||||
assertEquals(m_pushBackOut[i], queue.get(i), 0.00005);
|
||||
for (int i = 0; i < m_addLastOut.length; i++) {
|
||||
assertEquals(m_addLastOut[i], queue.get(i), 0.00005);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,9 +56,9 @@ public class CircularBufferTest {
|
||||
CircularBuffer queue = new CircularBuffer(3);
|
||||
|
||||
// Insert three elements into the buffer
|
||||
queue.pushBack(1.0);
|
||||
queue.pushBack(2.0);
|
||||
queue.pushBack(3.0);
|
||||
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);
|
||||
@@ -69,27 +69,27 @@ public class CircularBufferTest {
|
||||
* front-most elements.
|
||||
*/
|
||||
|
||||
queue.pushBack(4.0); // Overwrite 1 with 4
|
||||
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.pushBack(5.0); // Overwrite 2 with 5
|
||||
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.popBack(), 0.00005); // 5 is removed
|
||||
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.popFront(), 0.00005); // 3 is removed
|
||||
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);
|
||||
@@ -100,10 +100,10 @@ public class CircularBufferTest {
|
||||
CircularBuffer queue = new CircularBuffer(5);
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
queue.pushBack(i);
|
||||
queue.addLast(i);
|
||||
}
|
||||
|
||||
queue.reset();
|
||||
queue.clear();
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
assertEquals(0.0, queue.get(i), 0.00005);
|
||||
@@ -117,9 +117,9 @@ public class CircularBufferTest {
|
||||
/* Buffer contains {1, 2, 3, _, _}
|
||||
* ^ front
|
||||
*/
|
||||
queue.pushBack(1.0);
|
||||
queue.pushBack(2.0);
|
||||
queue.pushBack(3.0);
|
||||
queue.addLast(1.0);
|
||||
queue.addLast(2.0);
|
||||
queue.addLast(3.0);
|
||||
|
||||
queue.resize(2);
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
@@ -129,16 +129,16 @@ public class CircularBufferTest {
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
assertEquals(2.0, queue.get(1), 0.00005);
|
||||
|
||||
queue.reset();
|
||||
queue.clear();
|
||||
|
||||
/* Buffer contains {_, 1, 2, 3, _}
|
||||
* ^ front
|
||||
*/
|
||||
queue.pushBack(0.0);
|
||||
queue.pushBack(1.0);
|
||||
queue.pushBack(2.0);
|
||||
queue.pushBack(3.0);
|
||||
queue.popFront();
|
||||
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);
|
||||
@@ -148,18 +148,18 @@ public class CircularBufferTest {
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
assertEquals(2.0, queue.get(1), 0.00005);
|
||||
|
||||
queue.reset();
|
||||
queue.clear();
|
||||
|
||||
/* Buffer contains {_, _, 1, 2, 3}
|
||||
* ^ front
|
||||
*/
|
||||
queue.pushBack(0.0);
|
||||
queue.pushBack(0.0);
|
||||
queue.pushBack(1.0);
|
||||
queue.pushBack(2.0);
|
||||
queue.pushBack(3.0);
|
||||
queue.popFront();
|
||||
queue.popFront();
|
||||
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);
|
||||
@@ -169,14 +169,14 @@ public class CircularBufferTest {
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
assertEquals(2.0, queue.get(1), 0.00005);
|
||||
|
||||
queue.reset();
|
||||
queue.clear();
|
||||
|
||||
/* Buffer contains {3, _, _, 1, 2}
|
||||
* ^ front
|
||||
*/
|
||||
queue.pushBack(3.0);
|
||||
queue.pushFront(2.0);
|
||||
queue.pushFront(1.0);
|
||||
queue.addLast(3.0);
|
||||
queue.addFirst(2.0);
|
||||
queue.addFirst(1.0);
|
||||
|
||||
queue.resize(2);
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
@@ -186,14 +186,14 @@ public class CircularBufferTest {
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
assertEquals(2.0, queue.get(1), 0.00005);
|
||||
|
||||
queue.reset();
|
||||
queue.clear();
|
||||
|
||||
/* Buffer contains {2, 3, _, _, 1}
|
||||
* ^ front
|
||||
*/
|
||||
queue.pushBack(2.0);
|
||||
queue.pushBack(3.0);
|
||||
queue.pushFront(1.0);
|
||||
queue.addLast(2.0);
|
||||
queue.addLast(3.0);
|
||||
queue.addFirst(1.0);
|
||||
|
||||
queue.resize(2);
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
@@ -203,14 +203,14 @@ public class CircularBufferTest {
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
assertEquals(2.0, queue.get(1), 0.00005);
|
||||
|
||||
// Test pushBack() after resize
|
||||
queue.pushBack(3.0);
|
||||
// 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 pushFront() after resize
|
||||
queue.pushFront(4.0);
|
||||
// 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);
|
||||
|
||||
Reference in New Issue
Block a user