-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanafunctors.h
78 lines (61 loc) · 1.16 KB
/
anafunctors.h
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef ANAFUNCTORS_H
#define ANAFUNCTORS_H
#include <cmath>
template<class SAMPLE>
struct AnalysisBase
{
typedef SAMPLE sample_t;
virtual double analyse(const SAMPLE & s) const = 0;
double operator()(const SAMPLE & s) const
{ return this->analyse(s); }
};
template<class SAMPLE, class T>
struct AnalysisWrapper
{
typedef SAMPLE sample_t;
typedef T (sample_t::* func_t)() const;
template<func_t FUNC>
struct Analysis : public AnalysisBase<SAMPLE>
{
double analyse(const SAMPLE & s) const
{
return (s.*FUNC)();
}
};
template<func_t FUNC>
static AnalysisBase<SAMPLE> * create()
{
return new Analysis<FUNC>();
}
};
struct Aggregate
{
protected:
size_t _n;
double _mean, _sqr_sum, _std;
public:
Aggregate()
: _n(0), _mean(0), _sqr_sum(0), _std(0)
{}
void operator()(double value)
{
if (value != value)
return;
_n++;
_mean += value;
_sqr_sum += value * value;
}
void analyse()
{
if (_n<2)
return;
const double ssq = _sqr_sum - _mean*_mean/_n;
_std = sqrt(ssq / (_n-1));
_mean /= _n;
}
double mean() const
{ return _mean; }
double std() const
{ return _std; }
};
#endif // ANAFUNCTORS_H