Median.h: Sort values on median retrieval instead live on adding

This commit is contained in:
MBulli
2020-04-08 15:29:22 +02:00
parent 2f1698c4cc
commit 6cda6beedf

View File

@@ -30,17 +30,18 @@ namespace Stats {
/** add the given scalar value to the median calculation */
void add(const Scalar value) {
const auto idx = std::upper_bound( sorted.begin(), sorted.end(), value );
sorted.insert( idx, value );
sorted.push_back(value);
}
/** get the median of all added values */
Scalar get() const {
Scalar get() {
// sanity check
Assert::isNot0(sorted.size(), "add elements first!");
std::sort(sorted.begin(), sorted.end());
if (sorted.size() % 2 == 1) { // odd
const int idx = sorted.size()/2;