rusefi-1/firmware/util/containers/cyclic_buffer.h

180 lines
4.0 KiB
C
Raw Normal View History

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
*/
2021-11-24 17:26:40 -08:00
#pragma once
2015-07-10 06:01:56 -07:00
#include <limits>
2015-07-10 06:01:56 -07:00
#include <string.h>
#include <stdint.h>
2015-07-10 06:01:56 -07:00
2016-01-31 21:01:32 -08:00
static const short CB_MAX_SIZE = 128;
2015-07-10 06:01:56 -07:00
template<typename T, size_t maxSize = CB_MAX_SIZE>
2015-07-10 06:01:56 -07:00
class cyclic_buffer
{
public:
cyclic_buffer();
explicit cyclic_buffer(int size);
2015-07-10 06:01:56 -07:00
public:
void add(T value);
2019-01-15 18:03:45 -08:00
T get(int index) const;
2021-11-24 19:17:29 -08:00
T sum(size_t length) const;
T maxValue(size_t length) const;
T minValue(size_t length) const;
void setSize(size_t size);
2019-01-15 18:03:45 -08:00
bool contains(T value) const;
int getSize() const;
int getCount() const;
2015-07-10 06:01:56 -07:00
void clear();
volatile T elements[maxSize];
volatile uint16_t currentIndex;
2015-07-10 06:01:56 -07:00
2020-09-07 08:31:29 -07:00
protected:
uint16_t size;
2016-01-04 21:02:56 -08:00
/**
* number of elements added into this buffer, would be eventually bigger then size
*/
2021-11-24 19:17:29 -08:00
volatile size_t count;
2015-07-10 06:01:56 -07:00
};
template<typename T, size_t maxSize>
cyclic_buffer<T, maxSize>::cyclic_buffer() : cyclic_buffer(maxSize) {
2015-07-10 06:01:56 -07:00
}
template<typename T, size_t maxSize>
cyclic_buffer<T, maxSize>::cyclic_buffer(int size) {
2015-07-10 06:01:56 -07:00
setSize(size);
}
template<typename T, size_t maxSize>
void cyclic_buffer<T, maxSize>::add(T value) {
// Too lazy to make this thread safe, but at the very least let's never let currentIndex
// become invalid. And yes I did see a crash due to an overrun here.
uint16_t idx = currentIndex;
2021-12-01 17:46:16 -08:00
((T &)elements[idx]) = value;
2016-03-09 21:04:40 -08:00
if (++idx == size) {
idx = 0;
2015-07-10 06:01:56 -07:00
}
currentIndex = idx;
2015-07-10 06:01:56 -07:00
2022-02-26 19:58:51 -08:00
count = count + 1;
2015-07-10 06:01:56 -07:00
}
2019-01-12 11:01:13 -08:00
template<typename T, size_t maxSize>
2019-01-15 18:03:45 -08:00
bool cyclic_buffer<T, maxSize>::contains(T value) const {
2019-01-12 11:01:13 -08:00
for (int i = 0; i < currentIndex ; i++) {
if (elements[i] == value) {
2021-11-22 14:52:03 -08:00
return true;
2019-01-12 11:01:13 -08:00
}
}
2021-11-22 14:52:03 -08:00
return false;
2019-01-12 11:01:13 -08:00
}
template<typename T, size_t maxSize>
2021-11-24 19:17:29 -08:00
void cyclic_buffer<T, maxSize>::setSize(size_t size) {
2015-07-10 06:01:56 -07:00
clear();
this->size = size < maxSize ? size : maxSize;
2015-07-10 06:01:56 -07:00
}
template<typename T, size_t maxSize>
2019-01-15 18:03:45 -08:00
int cyclic_buffer<T, maxSize>::getSize() const {
2015-07-10 06:01:56 -07:00
return size;
}
template<typename T, size_t maxSize>
2019-01-15 18:03:45 -08:00
int cyclic_buffer<T, maxSize>::getCount() const {
2016-01-04 21:02:56 -08:00
return count;
}
template<typename T, size_t maxSize>
2019-01-15 18:03:45 -08:00
T cyclic_buffer<T, maxSize>::get(int index) const {
2015-07-10 06:01:56 -07:00
while (index < 0) {
index += size;
}
while (index >= size) {
index -= size;
}
return elements[index];
}
template<typename T, size_t maxSize>
2021-11-24 19:17:29 -08:00
T cyclic_buffer<T, maxSize>::maxValue(size_t length) const {
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
T result = std::numeric_limits<T>::min();
2021-11-24 19:17:29 -08:00
for (size_t 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;
}
template<typename T, size_t maxSize>
2021-11-24 19:17:29 -08:00
T cyclic_buffer<T, maxSize>::minValue(size_t length) const {
2015-07-10 06:01:56 -07:00
if (length > count) {
length = count;
}
int ci = currentIndex; // local copy to increase thread-safety
T result = std::numeric_limits<T>::max();
2021-11-24 19:17:29 -08:00
for (size_t 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;
}
template<typename T, size_t maxSize>
2021-11-24 19:17:29 -08:00
T cyclic_buffer<T, maxSize>::sum(size_t length) const {
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;
2021-11-24 19:17:29 -08:00
for (size_t 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;
}
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
}