fome-fw/firmware/util/cyclic_buffer.h

198 lines
3.8 KiB
C
Raw Normal View History

2014-08-29 07:52:33 -07:00
/**
* @file cyclic_buffer.h
2015-03-27 19:06:23 -07:00
* @brief A cyclic buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end.
*
* http://en.wikipedia.org/wiki/Circular_buffer
2014-08-29 07:52:33 -07:00
*
* @date Dec 8, 2013
* @author Andrey Belomutskiy, Daniel Hill
*
* Daniel Hill - Modified to use C++ - Mar 2, 2014
*/
#ifndef CYCLIC_BUFFER_H
#define CYCLIC_BUFFER_H
2015-03-27 17:05:29 -07:00
#include <string.h>
2015-03-23 06:10:56 -07:00
static const short CB_MAX_SIZE = 64;
2014-08-29 07:52:33 -07:00
2015-03-27 17:05:29 -07:00
template<typename T>
2014-08-29 07:52:33 -07:00
class cyclic_buffer
{
public:
2015-03-23 06:10:56 -07:00
cyclic_buffer();
cyclic_buffer(int size);
2014-08-29 07:52:33 -07:00
//cpctor
cyclic_buffer(const cyclic_buffer& cb);
//dtor
~cyclic_buffer();
public:
//overloaded =operator
cyclic_buffer& operator=(const cyclic_buffer& rhCb);
public:
2015-03-27 19:06:23 -07:00
void add(T value);
2015-03-27 20:08:14 -07:00
T get(int index);
2015-03-27 19:06:23 -07:00
T sum(int length);
2015-03-28 08:10:55 -07:00
T maxValue(int length);
2015-03-28 13:05:01 -07:00
T minValue(int length);
2015-03-23 06:10:56 -07:00
void setSize(int size);
2015-03-27 20:08:14 -07:00
int getSize();
2014-08-29 07:52:33 -07:00
void clear();
private:
2015-03-25 18:07:56 -07:00
void baseC(int size);
2015-03-27 19:06:23 -07:00
volatile T elements[CB_MAX_SIZE];
2014-08-29 07:52:33 -07:00
volatile int currentIndex;
volatile int count;
2015-03-23 06:10:56 -07:00
int size;
2014-08-29 07:52:33 -07:00
};
2015-03-27 17:05:29 -07:00
template<typename T>
cyclic_buffer<T>::cyclic_buffer() {
baseC(CB_MAX_SIZE);
}
template<typename T>
cyclic_buffer<T>::cyclic_buffer(int size) {
baseC(size);
}
template<typename T>
void cyclic_buffer<T>::baseC(int size) {
currentIndex = 0;
setSize(size);
}
template<typename T>
cyclic_buffer<T>::cyclic_buffer(const cyclic_buffer& cb) {
//Deep copy the data
currentIndex = cb.currentIndex;
count = cb.count;
size = cb.size;
for (int i = 0; i < size; ++i) {
elements[i] = cb.elements[i];
}
}
template<typename T>
cyclic_buffer<T>::~cyclic_buffer() {
//No dynamic allocation - safe to leave
}
//template<typename T>
//cyclic_buffer& cyclic_buffer<T>::operator=(const cyclic_buffer<T>& rhCb) {
// //Deep copy
// currentIndex = rhCb.currentIndex;
// count = rhCb.count;
// for (int i = 0; i < size; ++i) {
// elements[i] = rhCb.elements[i];
// }
// return *this;
//}
template<typename T>
2015-03-27 19:06:23 -07:00
void cyclic_buffer<T>::add(T value) {
2015-03-27 17:05:29 -07:00
++currentIndex;
if (currentIndex == size) {
currentIndex = 0;
}
elements[currentIndex] = value;
++count;
}
template<typename T>
void cyclic_buffer<T>::setSize(int size) {
clear();
2015-03-28 15:09:48 -07:00
this->size = size < CB_MAX_SIZE ? size : CB_MAX_SIZE;
2015-03-27 17:05:29 -07:00
}
template<typename T>
2015-03-27 20:08:14 -07:00
int cyclic_buffer<T>::getSize() {
return size;
}
template<typename T>
T cyclic_buffer<T>::get(int index) {
while (index < 0) {
index += size;
}
while (index >= size) {
index -= size;
}
return elements[index];
2015-03-27 17:05:29 -07:00
}
2015-03-28 08:10:55 -07:00
template<typename T>
T cyclic_buffer<T>::maxValue(int length) {
if (length > count) {
length = count;
}
int ci = currentIndex; // local copy to increase thread-safety
2015-03-28 13:05:01 -07:00
T result = -2000000; // todo: better min value?
2015-03-28 08:10:55 -07:00
for (int i = 0; i < length; ++i) {
int index = ci - i;
while (index < 0) {
index += size;
}
if (elements[index] > result) {
result = elements[index];
}
}
2015-03-28 13:05:01 -07:00
return result;
}
template<typename T>
T cyclic_buffer<T>::minValue(int length) {
if (length > count) {
length = count;
}
int ci = currentIndex; // local copy to increase thread-safety
T result = +2000000; // todo: better max value?
for (int i = 0; i < length; ++i) {
int index = ci - i;
while (index < 0) {
index += size;
}
2015-03-28 08:10:55 -07:00
2015-03-28 13:05:01 -07:00
if (elements[index] < result) {
result = elements[index];
}
}
2015-03-28 08:10:55 -07:00
return result;
}
2015-03-27 17:05:29 -07:00
template<typename T>
2015-03-27 19:06:23 -07:00
T cyclic_buffer<T>::sum(int length) {
2015-03-27 17:05:29 -07:00
if (length > count) {
length = count;
}
int ci = currentIndex; // local copy to increase thread-safety
2015-03-27 19:06:23 -07:00
T result = 0;
2015-03-27 17:05:29 -07:00
for (int i = 0; i < length; ++i) {
int index = ci - i;
while (index < 0) {
index += size;
}
result += elements[index];
}
return result;
}
template<typename T>
void cyclic_buffer<T>::clear() {
memset((void*) elements, 0, sizeof(elements)); // I would usually use static_cast, but due to the elements being volatile we cannot.
count = 0;
count = 0;
}
2014-08-29 07:52:33 -07:00
#endif //CYCLIC_BUFFER_H