Parasol Planning Library (PPL)
Average.h
Go to the documentation of this file.
1 #ifndef PMPL_AVERAGE_H_
2 #define PMPL_AVERAGE_H_
3 
4 #include <cstddef>
5 
6 
11 template <typename T>
12 class Average final
13 {
14 
17 
18  size_t m_count{0};
19  T m_total{T()};
20 
22 
23  public:
24 
27 
31  Average& operator+=(const T& _value) noexcept;
32 
37  Average& AddSummedValues(const T& _sum, const size_t _count) noexcept;
38 
42 
44  T Get() const noexcept;
45 
47  const T& Sum() const noexcept;
48 
50  size_t Count() const noexcept;
51 
53 
54 };
55 
56 /*-------------------------------- Modifiers ---------------------------------*/
57 
58 template <typename T>
59 inline
60 Average<T>&
61 Average<T>::
62 operator+=(const T& _value) noexcept {
63  m_total += _value;
64  ++m_count;
65  return *this;
66 }
67 
68 
69 template <typename T>
70 inline
73 AddSummedValues(const T& _sum, const size_t _count) noexcept {
74  m_total += _sum;
75  m_count += _count;
76  return *this;
77 }
78 
79 /*--------------------------------- Queries ----------------------------------*/
80 
81 template <typename T>
82 inline
83 T
85 Get() const noexcept {
86  return m_total / m_count;
87 }
88 
89 
90 template <typename T>
91 inline
92 const T&
94 Sum() const noexcept {
95  return m_total;
96 }
97 
98 
99 template <typename T>
100 inline
101 size_t
103 Count() const noexcept {
104  return m_count;
105 }
106 
107 /*----------------------------------------------------------------------------*/
108 
109 #endif
Definition: Average.h:13
Average & AddSummedValues(const T &_sum, const size_t _count) noexcept
Definition: Average.h:73
T Get() const noexcept
Get the average.
Definition: Average.h:85
size_t Count() const noexcept
Get the number of elements included.
Definition: Average.h:103
Average & operator+=(const T &_value) noexcept
Definition: Average.h:62
const T & Sum() const noexcept
Get the total sum.
Definition: Average.h:94