auto-sync

This commit is contained in:
rusEfi 2015-03-28 15:05:01 -05:00
parent dda2f921a8
commit 339f8b6165
2 changed files with 23 additions and 1 deletions

View File

@ -37,6 +37,7 @@ class cyclic_buffer
T get(int index); T get(int index);
T sum(int length); T sum(int length);
T maxValue(int length); T maxValue(int length);
T minValue(int length);
void setSize(int size); void setSize(int size);
int getSize(); int getSize();
void clear(); void clear();
@ -131,7 +132,7 @@ T cyclic_buffer<T>::maxValue(int length) {
length = count; length = count;
} }
int ci = currentIndex; // local copy to increase thread-safety int ci = currentIndex; // local copy to increase thread-safety
T result = 0; // todo: better min value? T result = -2000000; // todo: better min value?
for (int i = 0; i < length; ++i) { for (int i = 0; i < length; ++i) {
int index = ci - i; int index = ci - i;
while (index < 0) { while (index < 0) {
@ -142,7 +143,26 @@ T cyclic_buffer<T>::maxValue(int length) {
result = elements[index]; result = elements[index];
} }
} }
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;
}
if (elements[index] < result) {
result = elements[index];
}
}
return result; return result;
} }

View File

@ -73,6 +73,8 @@ void testCyclicBuffer(void) {
assertEquals(4, sb.maxValue(3)); assertEquals(4, sb.maxValue(3));
assertEquals(4, sb.maxValue(113)); assertEquals(4, sb.maxValue(113));
assertEquals(2, sb.minValue(3));
assertEquals(1, sb.minValue(113));
} }
} }