new walker (control+path)
added new sanity checks fixed minor errors added corresponding test-cases added moving-median
This commit is contained in:
65
math/MovingMedian.h
Normal file
65
math/MovingMedian.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#ifndef MOVINGMEDIAN_H
|
||||
#define MOVINGMEDIAN_H
|
||||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
template <typename T> class MovingMedian {
|
||||
|
||||
private:
|
||||
|
||||
/** up to "size" elements */
|
||||
std::vector<T> values;
|
||||
|
||||
/** the number of elements to find the median within */
|
||||
int size;
|
||||
|
||||
public:
|
||||
|
||||
/** ctor */
|
||||
MovingMedian(const int size) : size(size) {;}
|
||||
|
||||
/** add a new value */
|
||||
void add(const T val) {
|
||||
|
||||
// add new value
|
||||
values.push_back(val);
|
||||
|
||||
// too many values?
|
||||
if ((int) values.size() > size) {
|
||||
values.erase(values.begin());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** get the current median */
|
||||
T get() const {
|
||||
|
||||
// create a sorted copy (slow, but works)
|
||||
std::vector<T> copy = values;
|
||||
std::sort(copy.begin(), copy.end());
|
||||
|
||||
// get the median
|
||||
if (values.size() % 2 != 0) {
|
||||
return values[(values.size() + 0) / 2];
|
||||
} else {
|
||||
return (values[values.size() / 2 - 1] + values[values.size() / 2 + 0]) / 2;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/** get the number of used entries */
|
||||
int getNumUsed() const {
|
||||
return (int) values.size();
|
||||
}
|
||||
|
||||
/** get number of entries to get the median for */
|
||||
int getSize() const {
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // MOVINGMEDIAN_H
|
||||
6
math/TestMovingMedian.h
Normal file
6
math/TestMovingMedian.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef TESTMOVINGMEDIAN_H
|
||||
#define TESTMOVINGMEDIAN_H
|
||||
|
||||
|
||||
|
||||
#endif // TESTMOVINGMEDIAN_H
|
||||
Reference in New Issue
Block a user