The ZX Ecosystem v5.1.0;_GUI_v3.1.0
Loading...
Searching...
No Matches
SimpleStatistics.h
Go to the documentation of this file.
1
2/* *************************************************************************/
20#ifndef SIMPLESTATISTICS
21#define SIMPLESTATISTICS
22
23#include <limits>
24#include <string>
25#include <sstream>
26#include "CppAddons.h"
27
28// This define (or any other element put here) is needed for doxygen to work ok
29// the nesting of groups in this file:
30// http://stackoverflow.com/questions/40343396/
31// doxygen-grouping-classes-in-several-nested-groups
32#define SIMPLESTATISTICS_ELEMENT_FOR_AVOIDING_ERROR_IN_NESTING_GROUPS_IN_DOXYGEN
33
34
35/* ***************************************************************************
36*
37* Overflow policies for the class SimpleStats.
38*
39***************************************************************************** */
55template <typename T>
57{
58 public:
59
60 SSOvPol_ThrowExc(unsigned *n) {}
62
63 bool full(void) { RUNTIMEEXCEP("Cannot hold more data"); }
65
66};
67
69template <typename T>
71{
72 public:
73
74 SSOvPol_IgnoreMoreData(unsigned *n) {}
76
77 bool full(void) { return(false); }
79
80};
81
83template <typename T>
85{
86 public:
87
88 SSOvPol_Reset(unsigned *nd):n_{nd} {}
90
91 bool full(void) { *n_ = 0; return(true); }
93
94 private:
95
96 unsigned * n_;
97};
98
101/* *****************************************************************************
102*
103* Template class: SimpleStats
104*
105*******************************************************************************/
106
108template <typename T, // type of the data
109 class OverflowPolicy = SSOvPol_ThrowExc<T>, // policy of overflow
110 unsigned MAXSIZE = std::numeric_limits<unsigned>::max() // max size
111 >
113{
114 public:
115
116 /* --------------------- Constructors ---------------------------- */
117
118 SimpleStats(void):ovpol_{OverflowPolicy{&n_}} { reset(); }
120 /* Copy constructors and assignments are generated implicitly by the
121 compiler. */
122
123 virtual ~SimpleStats(void) {}
125
126 /* --------------------- Methods ---------------------------- */
127
128 void reset(void) { n_=0; minimum_ = T{}; maximum_ = T{}; average_ = T{}; }
130
131 void addDatum(T v)
132 {
133 if ((n_ == MAXSIZE)&&(!ovpol_.full())) return;
134 if (n_==0)
135 {
136 minimum_=v;
137 maximum_=v;
138 average_=v;
139 }
140 else
141 {
142 if (v<minimum_) minimum_=v;
143 if (v>maximum_) maximum_=v;
144 T onebyn = static_cast<T>(1) / n_;
145 average_ = (static_cast<T>(1) - onebyn) * average_ + onebyn * v;
146 }
147 ++n_;
148 }
150
151 unsigned sampleSize(void) const noexcept { return(n_); }
153
154 T min(void) const noexcept { return(minimum_); }
156
157 T max(void) const noexcept { return(maximum_); }
159
160 T avg(void) const noexcept { return(average_); }
162
163 std::string to_string(void) const noexcept
164 {
165 std::ostringstream s;
166 s << "stats(" << n_ << "): min=" << minimum_ <<
167 ", max=" << maximum_ <<
168 ", avg=" << average_;
169 return(s.str());
170 }
172
173 private:
174
175 T minimum_, maximum_, average_;
176 unsigned n_;
177 OverflowPolicy ovpol_;
178};
179
180
181#endif
182 // SimpleStatistics
184
#define RUNTIMEEXCEP(txt)
Raise a runtime exception with the given std::string TXT + additional info.
Definition: CppAddons.h:93
SSOvPol_IgnoreMoreData(unsigned *n)
Default constructor.
SSOvPol_Reset(unsigned *nd)
Default constructor.
bool full(void)
Reset and force to take the current datum as the first new one.
bool full(void)
Discard any data after full.
Overflow policy for SimpleStats that ignores further data after full.
Overflow policy for SimpleStats that resets dataset & start afresh, if full.
T avg(void) const noexcept
Statistic: Average of the series.
std::string to_string(void) const noexcept
Return a string that describes the statistics collected so far.
SSOvPol_ThrowExc(unsigned *n)
Default constructor.
bool full(void)
Exception when full.
T max(void) const noexcept
Statistic: Maximum of the series.
virtual ~SimpleStats(void)
Destructor.
T min(void) const noexcept
Statistic: Minimum of the series.
SimpleStats(void)
Default constructor: an empty series.
unsigned sampleSize(void) const noexcept
Return the number of values in the series.
void reset(void)
Clear the series and the statistics.
void addDatum(T v)
Add a new value to the series and recompute statistics.
Overflow policy for SimpleStats that throws an exception if dataset is full.
Class that computes some statistics of a series of values of a given type.
@ T
Key in the 3rd half-row.