Virtual Testbed
Ship dynamics simulator for extreme conditions
mean.hh
1 #ifndef VTESTBED_BASE_MEAN_HH
2 #define VTESTBED_BASE_MEAN_HH
3 
4 #include <ostream>
5 
6 #include <vtestbed/base/blitz.hh>
7 
8 namespace vtb {
9 
10  namespace base {
11 
19  template <class T>
20  class Mean {
21 
22  public:
23  typedef T value_type;
24  typedef T& reference;
25  typedef const T& const_reference;
26  typedef int int_type;
27 
28  private:
29  T _sum{0};
30  int _count{0};
31 
32  public:
33 
34  inline int_type
35  count() const noexcept {
36  return this->_count;
37  }
38 
39  inline value_type
40  mean() const noexcept {
41  return this->_sum;
42  }
43 
44  inline void
45  update(T x) {
46  auto& sum = this->_sum;
47  auto& n = this->_count;
48  ++n;
49  sum += (x - sum) / n;
50  }
51 
52  inline void
53  clear() {
54  this->_sum = 0, this->_count = 0;
55  }
56 
57  inline friend std::ostream&
58  operator<<(std::ostream& out, const Mean& rhs) {
59  return out << rhs.mean();
60  }
61 
62  };
63 
64  }
65 
66 }
67 
68 #endif // vim:filetype=cpp
Estimates sample mean without overflows.
Definition: mean.hh:20
Main namespace.
Definition: convert.hh:9