2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2021-06-12 01:17:09 -07:00
|
|
|
package edu.wpi.first.util;
|
2015-10-30 16:01:57 -07:00
|
|
|
|
2022-12-26 14:37:53 -05:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** This is a simple circular buffer so we don't need to "bucket brigade" copy old values. */
|
2015-10-30 16:01:57 -07:00
|
|
|
public class CircularBuffer {
|
|
|
|
|
private double[] m_data;
|
2016-07-16 20:50:19 -07:00
|
|
|
|
|
|
|
|
// Index of element at front of buffer
|
2018-06-03 10:00:53 -07:00
|
|
|
private int m_front;
|
2016-07-16 20:50:19 -07:00
|
|
|
|
|
|
|
|
// Number of elements used in buffer
|
2018-06-03 10:00:53 -07:00
|
|
|
private int m_length;
|
2015-10-30 16:01:57 -07:00
|
|
|
|
2016-07-16 20:50:19 -07:00
|
|
|
/**
|
2017-07-28 22:24:05 -07:00
|
|
|
* Create a CircularBuffer with the provided size.
|
|
|
|
|
*
|
2016-07-16 20:50:19 -07:00
|
|
|
* @param size The size of the circular buffer.
|
|
|
|
|
*/
|
2015-10-30 16:01:57 -07:00
|
|
|
public CircularBuffer(int size) {
|
|
|
|
|
m_data = new double[size];
|
2022-12-26 14:37:53 -05:00
|
|
|
Arrays.fill(m_data, 0.0);
|
2015-10-30 16:01:57 -07:00
|
|
|
}
|
|
|
|
|
|
2017-11-22 17:04:57 -08:00
|
|
|
/**
|
|
|
|
|
* Returns number of elements in buffer.
|
|
|
|
|
*
|
|
|
|
|
* @return number of elements in buffer
|
|
|
|
|
*/
|
2022-04-25 17:58:12 -04:00
|
|
|
public int size() {
|
2017-11-22 17:04:57 -08:00
|
|
|
return m_length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get value at front of buffer.
|
|
|
|
|
*
|
|
|
|
|
* @return value at front of buffer
|
|
|
|
|
*/
|
2022-04-25 17:58:12 -04:00
|
|
|
public double getFirst() {
|
2017-11-22 17:04:57 -08:00
|
|
|
return m_data[m_front];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get value at back of buffer.
|
|
|
|
|
*
|
|
|
|
|
* @return value at back of buffer
|
|
|
|
|
*/
|
2022-04-25 17:58:12 -04:00
|
|
|
public double getLast() {
|
2017-11-22 17:04:57 -08:00
|
|
|
// 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];
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-30 16:01:57 -07:00
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Push new value onto front of the buffer. The value at the back is overwritten if the buffer is
|
|
|
|
|
* full.
|
2021-06-10 20:46:47 -07:00
|
|
|
*
|
|
|
|
|
* @param value The value to push.
|
2015-10-30 16:01:57 -07:00
|
|
|
*/
|
2017-11-22 17:04:57 -08:00
|
|
|
public void addFirst(double value) {
|
2015-10-30 16:01:57 -07:00
|
|
|
if (m_data.length == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_front = moduloDec(m_front);
|
|
|
|
|
|
|
|
|
|
m_data[m_front] = value;
|
|
|
|
|
|
|
|
|
|
if (m_length < m_data.length) {
|
|
|
|
|
m_length++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 12:07:40 -04:00
|
|
|
* Push new value onto back of the buffer. The value at the front is overwritten if the buffer is
|
|
|
|
|
* full.
|
2021-06-10 20:46:47 -07:00
|
|
|
*
|
|
|
|
|
* @param value The value to push.
|
2015-10-30 16:01:57 -07:00
|
|
|
*/
|
2017-11-22 17:04:57 -08:00
|
|
|
public void addLast(double value) {
|
2015-10-30 16:01:57 -07:00
|
|
|
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
|
|
|
|
|
*/
|
2017-11-22 17:04:57 -08:00
|
|
|
public double removeFirst() {
|
2015-10-30 16:01:57 -07:00
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
|
|
|
|
* Pop value at back of buffer.
|
|
|
|
|
*
|
|
|
|
|
* @return value at back of buffer
|
|
|
|
|
*/
|
2017-11-22 17:04:57 -08:00
|
|
|
public double removeLast() {
|
2015-10-30 16:01:57 -07:00
|
|
|
// 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];
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-20 19:43:34 -07:00
|
|
|
/**
|
|
|
|
|
* Resizes internal buffer to given size.
|
|
|
|
|
*
|
|
|
|
|
* <p>A new buffer is allocated because arrays are not resizable.
|
2021-06-10 20:46:47 -07:00
|
|
|
*
|
|
|
|
|
* @param size New buffer size.
|
2016-09-20 19:43:34 -07:00
|
|
|
*/
|
2022-04-25 17:58:12 -04:00
|
|
|
public void resize(int size) {
|
2016-09-20 19:43:34 -07:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Sets internal buffer contents to zero. */
|
2017-11-22 17:04:57 -08:00
|
|
|
public void clear() {
|
2022-12-26 14:37:53 -05:00
|
|
|
Arrays.fill(m_data, 0.0);
|
2015-10-30 16:01:57 -07:00
|
|
|
m_front = 0;
|
|
|
|
|
m_length = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-07-28 22:24:05 -07:00
|
|
|
* Get the element at the provided index relative to the start of the buffer.
|
|
|
|
|
*
|
2021-06-10 20:46:47 -07:00
|
|
|
* @param index Index into the buffer.
|
2016-07-16 20:50:19 -07:00
|
|
|
* @return Element at index starting from front of buffer.
|
2015-10-30 16:01:57 -07:00
|
|
|
*/
|
|
|
|
|
public double get(int index) {
|
|
|
|
|
return m_data[(m_front + index) % m_data.length];
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
|
|
|
|
* Increment an index modulo the length of the m_data buffer.
|
|
|
|
|
*
|
|
|
|
|
* @param index Index into the buffer.
|
|
|
|
|
*/
|
2015-10-30 16:01:57 -07:00
|
|
|
private int moduloInc(int index) {
|
|
|
|
|
return (index + 1) % m_data.length;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
|
|
|
|
* Decrement an index modulo the length of the m_data buffer.
|
|
|
|
|
*
|
|
|
|
|
* @param index Index into the buffer.
|
|
|
|
|
*/
|
2015-10-30 16:01:57 -07:00
|
|
|
private int moduloDec(int index) {
|
|
|
|
|
if (index == 0) {
|
|
|
|
|
return m_data.length - 1;
|
|
|
|
|
} else {
|
|
|
|
|
return index - 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|