2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file cyclic_buffer.h
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-01-31 21:01:32 -08:00
|
|
|
static const short CB_MAX_SIZE = 128;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2016-01-04 21:02:56 -08:00
|
|
|
#define BUFFER_MAX_VALUE 200123
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize = CB_MAX_SIZE>
|
2015-07-10 06:01:56 -07:00
|
|
|
class cyclic_buffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cyclic_buffer();
|
|
|
|
cyclic_buffer(int size);
|
|
|
|
//cpctor
|
|
|
|
cyclic_buffer(const cyclic_buffer& cb);
|
|
|
|
//dtor
|
|
|
|
~cyclic_buffer();
|
|
|
|
|
|
|
|
public:
|
|
|
|
//overloaded =operator
|
|
|
|
cyclic_buffer& operator=(const cyclic_buffer& rhCb);
|
|
|
|
|
|
|
|
public:
|
|
|
|
void add(T value);
|
|
|
|
T get(int index);
|
|
|
|
T sum(int length);
|
|
|
|
T maxValue(int length);
|
|
|
|
T minValue(int length);
|
|
|
|
void setSize(int size);
|
|
|
|
int getSize();
|
2016-01-04 21:02:56 -08:00
|
|
|
int getCount();
|
2015-07-10 06:01:56 -07:00
|
|
|
void clear();
|
2018-01-23 05:24:13 -08:00
|
|
|
volatile T elements[maxSize];
|
2016-03-10 19:02:54 -08:00
|
|
|
volatile int currentIndex;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
void baseC(int size);
|
2016-01-04 21:02:56 -08:00
|
|
|
/**
|
|
|
|
* number of elements added into this buffer, would be eventually bigger then size
|
|
|
|
*/
|
2015-07-10 06:01:56 -07:00
|
|
|
volatile int count;
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize>
|
|
|
|
cyclic_buffer<T, maxSize>::cyclic_buffer() {
|
|
|
|
baseC(maxSize);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize>
|
|
|
|
cyclic_buffer<T, maxSize>::cyclic_buffer(int size) {
|
2015-07-10 06:01:56 -07:00
|
|
|
baseC(size);
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize>
|
|
|
|
void cyclic_buffer<T, maxSize>::baseC(int size) {
|
2015-07-10 06:01:56 -07:00
|
|
|
currentIndex = 0;
|
|
|
|
setSize(size);
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize>
|
|
|
|
cyclic_buffer<T, maxSize>::cyclic_buffer(const cyclic_buffer& cb) {
|
2015-07-10 06:01:56 -07:00
|
|
|
//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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize>
|
|
|
|
cyclic_buffer<T, maxSize>::~cyclic_buffer() {
|
2015-07-10 06:01:56 -07:00
|
|
|
//No dynamic allocation - safe to leave
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
//template<typename T, size_t maxSize>
|
|
|
|
//cyclic_buffer& cyclic_buffer<T, maxSize>::operator=(const cyclic_buffer<T, maxSize>& rhCb) {
|
2015-07-10 06:01:56 -07:00
|
|
|
// //Deep copy
|
|
|
|
// currentIndex = rhCb.currentIndex;
|
|
|
|
// count = rhCb.count;
|
|
|
|
// for (int i = 0; i < size; ++i) {
|
|
|
|
// elements[i] = rhCb.elements[i];
|
|
|
|
// }
|
|
|
|
// return *this;
|
|
|
|
//}
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize>
|
|
|
|
void cyclic_buffer<T, maxSize>::add(T value) {
|
2016-03-09 21:04:40 -08:00
|
|
|
elements[currentIndex] = value;
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
++currentIndex;
|
|
|
|
if (currentIndex == size) {
|
|
|
|
currentIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize>
|
|
|
|
void cyclic_buffer<T, maxSize>::setSize(int size) {
|
2015-07-10 06:01:56 -07:00
|
|
|
clear();
|
2018-01-23 05:24:13 -08:00
|
|
|
this->size = size < maxSize ? size : maxSize;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize>
|
|
|
|
int cyclic_buffer<T, maxSize>::getSize() {
|
2015-07-10 06:01:56 -07:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize>
|
|
|
|
int cyclic_buffer<T, maxSize>::getCount() {
|
2016-01-04 21:02:56 -08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize>
|
|
|
|
T cyclic_buffer<T, maxSize>::get(int index) {
|
2015-07-10 06:01:56 -07:00
|
|
|
while (index < 0) {
|
|
|
|
index += size;
|
|
|
|
}
|
|
|
|
while (index >= size) {
|
|
|
|
index -= size;
|
|
|
|
}
|
|
|
|
return elements[index];
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize>
|
|
|
|
T cyclic_buffer<T, maxSize>::maxValue(int length) {
|
2015-07-10 06:01:56 -07:00
|
|
|
if (length > count) {
|
2016-01-04 21:02:56 -08:00
|
|
|
// not enough data in buffer
|
2015-07-10 06:01:56 -07:00
|
|
|
length = count;
|
|
|
|
}
|
|
|
|
int ci = currentIndex; // local copy to increase thread-safety
|
2016-01-04 21:02:56 -08:00
|
|
|
T result = -BUFFER_MAX_VALUE; // todo: better min value?
|
2015-07-10 06:01:56 -07:00
|
|
|
for (int i = 0; i < length; ++i) {
|
2016-03-09 21:04:40 -08:00
|
|
|
int index = ci - i - 1;
|
2015-07-10 06:01:56 -07:00
|
|
|
while (index < 0) {
|
|
|
|
index += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elements[index] > result) {
|
|
|
|
result = elements[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize>
|
|
|
|
T cyclic_buffer<T, maxSize>::minValue(int length) {
|
2015-07-10 06:01:56 -07:00
|
|
|
if (length > count) {
|
|
|
|
length = count;
|
|
|
|
}
|
|
|
|
int ci = currentIndex; // local copy to increase thread-safety
|
2016-01-04 21:02:56 -08:00
|
|
|
T result = +BUFFER_MAX_VALUE; // todo: better max value?
|
2015-07-10 06:01:56 -07:00
|
|
|
for (int i = 0; i < length; ++i) {
|
2016-03-09 21:04:40 -08:00
|
|
|
int index = ci - i - 1;
|
2015-07-10 06:01:56 -07:00
|
|
|
while (index < 0) {
|
|
|
|
index += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elements[index] < result) {
|
|
|
|
result = elements[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize>
|
|
|
|
T cyclic_buffer<T, maxSize>::sum(int length) {
|
2015-07-10 06:01:56 -07:00
|
|
|
if (length > count) {
|
|
|
|
length = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ci = currentIndex; // local copy to increase thread-safety
|
|
|
|
T result = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < length; ++i) {
|
2016-03-09 21:04:40 -08:00
|
|
|
int index = ci - i - 1;
|
2015-07-10 06:01:56 -07:00
|
|
|
while (index < 0) {
|
|
|
|
index += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
result += elements[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:24:13 -08:00
|
|
|
template<typename T, size_t maxSize>
|
|
|
|
void cyclic_buffer<T, maxSize>::clear() {
|
2015-07-10 06:01:56 -07:00
|
|
|
memset((void*) elements, 0, sizeof(elements)); // I would usually use static_cast, but due to the elements being volatile we cannot.
|
|
|
|
count = 0;
|
2016-01-04 21:02:56 -08:00
|
|
|
currentIndex = 0;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //CYCLIC_BUFFER_H
|