auto-sync
This commit is contained in:
parent
5c22dd9db4
commit
de6d5eb7ca
|
@ -33,7 +33,7 @@ public:
|
|||
private:
|
||||
// float array[MAX_ACCEL_ARRAY_SIZE];
|
||||
float diffEnrichment;
|
||||
cyclic_buffer cb;
|
||||
cyclic_buffer<float> cb;
|
||||
};
|
||||
|
||||
void initAccelEnrichment(void);
|
||||
|
|
|
@ -76,7 +76,7 @@ static IgnitionEvent *iHead = NULL;
|
|||
*/
|
||||
//static EventQueue triggerEventsQueue;
|
||||
|
||||
static cyclic_buffer ignitionErrorDetection;
|
||||
static cyclic_buffer<int> ignitionErrorDetection;
|
||||
|
||||
static Logging *logger;
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ EXTERN_ENGINE
|
|||
// todo: better name for this constant
|
||||
#define HELPER_PERIOD 100000
|
||||
|
||||
static cyclic_buffer errorDetection;
|
||||
static cyclic_buffer<int> errorDetection;
|
||||
|
||||
#if ! EFI_PROD_CODE
|
||||
bool printGapRatio = false;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* @file cyclic_buffer.c
|
||||
* @file cyclic_buffer.cpp
|
||||
* @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
|
||||
|
@ -11,85 +11,3 @@
|
|||
|
||||
#include "cyclic_buffer.h"
|
||||
#include <string.h>
|
||||
|
||||
cyclic_buffer::cyclic_buffer() {
|
||||
baseC(CB_MAX_SIZE);
|
||||
}
|
||||
|
||||
cyclic_buffer::cyclic_buffer(int size) {
|
||||
baseC(size);
|
||||
}
|
||||
|
||||
void cyclic_buffer::baseC(int size) {
|
||||
currentIndex = 0;
|
||||
setSize(size);
|
||||
}
|
||||
|
||||
//cpctor
|
||||
cyclic_buffer::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];
|
||||
}
|
||||
}
|
||||
|
||||
//dtor
|
||||
cyclic_buffer::~cyclic_buffer() {
|
||||
//No dynamic allocation - safe to leave
|
||||
}
|
||||
|
||||
//overloaded =operator
|
||||
cyclic_buffer& cyclic_buffer::operator=(const cyclic_buffer& rhCb) {
|
||||
//Deep copy
|
||||
currentIndex = rhCb.currentIndex;
|
||||
count = rhCb.count;
|
||||
for (int i = 0; i < size; ++i) {
|
||||
elements[i] = rhCb.elements[i];
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void cyclic_buffer::add(int value) {
|
||||
++currentIndex;
|
||||
if (currentIndex == size) {
|
||||
currentIndex = 0;
|
||||
}
|
||||
elements[currentIndex] = value;
|
||||
|
||||
++count;
|
||||
}
|
||||
|
||||
void cyclic_buffer::setSize(int size) {
|
||||
clear();
|
||||
this->size = size;
|
||||
}
|
||||
|
||||
void cyclic_buffer::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;
|
||||
}
|
||||
|
||||
int cyclic_buffer::sum(int length) {
|
||||
if (length > count) {
|
||||
length = count;
|
||||
}
|
||||
|
||||
int ci = currentIndex; // local copy to increase thread-safety
|
||||
int result = 0;
|
||||
|
||||
for (int i = 0; i < length; ++i) {
|
||||
int index = ci - i;
|
||||
while (index < 0) {
|
||||
index += size;
|
||||
}
|
||||
|
||||
result += elements[index];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,8 +10,11 @@
|
|||
#ifndef CYCLIC_BUFFER_H
|
||||
#define CYCLIC_BUFFER_H
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static const short CB_MAX_SIZE = 64;
|
||||
|
||||
template<typename T>
|
||||
class cyclic_buffer
|
||||
{
|
||||
public:
|
||||
|
@ -28,6 +31,7 @@ class cyclic_buffer
|
|||
|
||||
public:
|
||||
void add(int value);
|
||||
int get(int index);
|
||||
int sum(int length);
|
||||
void setSize(int size);
|
||||
void clear();
|
||||
|
@ -40,4 +44,97 @@ class cyclic_buffer
|
|||
int size;
|
||||
};
|
||||
|
||||
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>
|
||||
void cyclic_buffer<T>::add(int value) {
|
||||
++currentIndex;
|
||||
if (currentIndex == size) {
|
||||
currentIndex = 0;
|
||||
}
|
||||
elements[currentIndex] = value;
|
||||
|
||||
++count;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void cyclic_buffer<T>::setSize(int size) {
|
||||
clear();
|
||||
this->size = size;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int cyclic_buffer<T>::get(int index) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int cyclic_buffer<T>::sum(int length) {
|
||||
if (length > count) {
|
||||
length = count;
|
||||
}
|
||||
|
||||
int ci = currentIndex; // local copy to increase thread-safety
|
||||
int result = 0;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#endif //CYCLIC_BUFFER_H
|
||||
|
|
|
@ -37,7 +37,7 @@ void testCrc(void) {
|
|||
assertEqualsM("crc32", 0xd3d99e8b, crc32(A, 1));
|
||||
}
|
||||
|
||||
static cyclic_buffer sb;
|
||||
static cyclic_buffer<int> sb;
|
||||
|
||||
void testOverflow64Counter(void) {
|
||||
print("*************************************** testOverflow64Counter\r\n");
|
||||
|
|
Loading…
Reference in New Issue