-
Notifications
You must be signed in to change notification settings - Fork 41
/
MeasurementSeries.hpp
59 lines (54 loc) · 1.35 KB
/
MeasurementSeries.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once
#include <algorithm>
#include <numeric>
#include <vector>
class MeasurementSeries {
public:
void add(double v) { data.push_back(v); }
double value() {
if (data.size() == 0)
return 0.0;
if (data.size() == 1)
return data[0];
if (data.size() == 2)
return (data[0] + data[1]) / 2.0;
std::sort(begin(data), end(data));
return std::accumulate(begin(data) + 1, end(data) - 1, 0.0) /
(data.size() - 2);
}
double median() {
if (data.size() == 0)
return 0.0;
if (data.size() == 1)
return data[0];
if (data.size() == 2)
return (data[0] + data[1]) / 2.0;
std::sort(begin(data), end(data));
if (data.size() % 2 == 0) {
return (data[data.size() / 2] + data[data.size() / 2 + 1]) / 2;
}
return data[data.size() / 2];
}
double minValue() {
if (data.size() == 0)
return 0.0;
std::sort(begin(data), end(data));
return *begin(data);
}
double maxValue() {
if (data.size() == 0)
return 0.0;
std::sort(begin(data), end(data));
return data.back();
}
double spread() {
if (data.size() <= 1)
return 0.0;
if (data.size() == 2)
return abs(data[0] - data[1]) / value();
std::sort(begin(data), end(data));
return abs(*(begin(data)) - *(end(data) - 1)) / value();
}
private:
std::vector<double> data;
};