-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,138 +1,144 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace WPILib | ||
namespace WPILib | ||
{ | ||
/// <summary> | ||
/// This is a simple circular buffer so we don't need to "bucket brigade" | ||
/// copy old values. | ||
/// </summary> | ||
public class CircularBuffer<T> | ||
{ | ||
private T[] m_data; | ||
private readonly T[] m_data; | ||
private int m_front = 0; | ||
private int m_back = 0; | ||
private int m_size = 0; | ||
private int m_length = 0; | ||
|
||
public CircularBuffer(int size) | ||
{ | ||
m_data = new T[size]; | ||
for(int i = 0; i < size; i++) | ||
for (int i = 0; i < size; i++) | ||
{ | ||
m_data[i] = default(T); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Push new value onto front of the buffer. The value at the back | ||
/// is overriden if the buffer is full. | ||
/// </summary> | ||
/// <param name="value">The value to push to the front.</param> | ||
public void PushFront(T value) | ||
{ | ||
if (m_data.Length == 0) return; | ||
|
||
// If buffer is full, decrement back index so front doesn't underrun it. | ||
if (m_front == m_back && m_size > 0) | ||
{ | ||
if (m_front == 0) | ||
{ | ||
m_front = m_data.Length - 1; | ||
} | ||
else | ||
{ | ||
m_front--; | ||
} | ||
|
||
m_back = m_front; | ||
} | ||
else | ||
{ | ||
if (m_front == 0) | ||
{ | ||
m_front = m_data.Length - 1; | ||
} | ||
else | ||
{ | ||
m_front--; | ||
} | ||
|
||
m_size++; | ||
} | ||
m_front = ModuloDec(m_front); | ||
|
||
m_data[m_front] = value; | ||
|
||
if (m_length < m_data.Length) | ||
{ | ||
m_length++; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Push new value onto the back of the buffer. The value at the front is | ||
/// overriden if the buffer is full. | ||
/// </summary> | ||
/// <param name="value"></param> | ||
public void PushBack(T value) | ||
{ | ||
if (m_data.Length == 0) return; | ||
|
||
m_data[m_back] = value; | ||
m_data[(m_front + m_length) % m_data.Length] = value; | ||
|
||
//If buffer is full, advance front index so back doesn't overrun it. | ||
if (m_front == m_back && m_size > 0) | ||
if (m_length < m_data.Length) | ||
{ | ||
m_front = (m_front + 1) % m_data.Length; | ||
m_back = m_front; | ||
m_length++; | ||
} | ||
else | ||
{ | ||
m_back = (m_back + 1) % m_data.Length; | ||
m_size++; | ||
//Increment front if buffer is full to maintain size. | ||
m_front = ModuloInc(m_front); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Pop value at front of buffer. | ||
/// </summary> | ||
/// <returns>Value at front of buffer</returns> | ||
public T PopFront() | ||
{ | ||
if (m_size == 0) return default(T); | ||
// If there are no elements in the buffer, do nothing | ||
if (m_length == 0) | ||
{ | ||
|
||
return default(T); | ||
} | ||
|
||
m_size--; | ||
|
||
T temp = m_data[m_front]; | ||
m_front = (m_front + 1) & m_data.Length; | ||
m_front = ModuloInc(m_front); | ||
m_length--; | ||
return temp; | ||
} | ||
|
||
/// <summary> | ||
/// Pop value at back of buffer. | ||
/// </summary> | ||
/// <returns>The value at back of buffer.</returns> | ||
public T PopBack() | ||
{ | ||
if (m_size == 0) return default(T); | ||
|
||
m_size--; | ||
|
||
if (m_back == 0) | ||
// If there are no elements in the buffer, do nothing | ||
if (m_length == 0) | ||
{ | ||
m_back = m_data.Length - 1; | ||
} | ||
else | ||
{ | ||
m_back--; | ||
return default(T); | ||
} | ||
|
||
return m_data[m_back]; | ||
} | ||
|
||
public int Size() | ||
{ | ||
return m_size; | ||
m_length--; | ||
return m_data[(m_front + m_length) % m_data.Length]; | ||
} | ||
|
||
/// <summary> | ||
/// Resets the buffer back to default values. | ||
/// </summary> | ||
public void Reset() | ||
{ | ||
for (int i = 0; i < m_data.Length; i++) | ||
{ | ||
m_data[i] = default(T); | ||
} | ||
|
||
m_front = 0; | ||
m_length = 0; | ||
} | ||
|
||
public T this[int i] | ||
/// <summary> | ||
/// Gets the element at index starting from front of buffer. | ||
/// </summary> | ||
/// <param name="i">The index to get</param> | ||
/// <returns>The value at index.</returns> | ||
public T this[int i] => m_data[(m_front + i) % m_data.Length]; | ||
|
||
/// <summary> | ||
/// Gets the element at index starting from front of buffer. | ||
/// </summary> | ||
/// <param name="index">The index to get</param> | ||
/// <returns>The value at index.</returns> | ||
public T Get(int index) => this[index]; | ||
|
||
private int ModuloInc(int index) | ||
{ | ||
get | ||
{ | ||
return m_data[(m_front + i) % m_data.Length]; | ||
} | ||
return (index + 1) % m_data.Length; | ||
} | ||
|
||
public T Get(int index) | ||
private int ModuloDec(int index) | ||
{ | ||
return m_data[(m_front + index) % m_data.Length]; | ||
if (index == 0) | ||
{ | ||
return m_data.Length - 1; | ||
} | ||
else | ||
{ | ||
return index - 1; | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.