Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cValueStats.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cValueStats_H
7 #define _INC_cValueStats_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cValueRange.h"
15 
16 namespace GrayLib
17 {
19 
20  template<typename TYPE = double >
21  class GRAYLIB_LINK cValueStats : public cValueRange<TYPE>
22  {
27 
28  typedef cValueRange<TYPE> SUPER_t;
29 
30  protected:
31  const UINT m_uSamplesMax;
34 
35  protected:
36  void AddStatW(TYPE nVal, TYPE nWeight)
37  {
40  if (nWeight >= ((TYPE)1))
41  {
42  m_nValAvg = SUPER_t::m_Lo = SUPER_t::m_Hi = nVal; // InitX()
43  }
44  else
45  {
46  m_nValAvg = (m_nValAvg * (((TYPE)1) - nWeight)) + (nVal * nWeight);
47  }
48  }
49 
50  public:
51  cValueStats(TYPE nValAvgStartingEst = 0, UINT uSamplesMax = 100)
52  : cValueRange<TYPE>(nValAvgStartingEst, nValAvgStartingEst)
53  , m_uSamplesMax(uSamplesMax)
54  , m_uSamplesQty(0)
55  , m_nValAvg(nValAvgStartingEst)
56  {
57  }
58 
59  TYPE get_ValAvg() const
60  {
61  return m_nValAvg;
62  }
63  void put_ValAvg(TYPE nValAvg)
64  {
65  m_nValAvg = nValAvg;
66  }
67 
68  void AddStat(TYPE nVal)
69  {
72  if (m_uSamplesQty < m_uSamplesMax)
73  {
74  m_uSamplesQty++;
75  }
76  AddStatW(nVal, ((TYPE)1) / (TYPE)(m_uSamplesQty));
77  this->UnionValue(nVal); // adjust min/max as well
78  }
79  };
80 
81  template<typename TYPE = double >
83  {
90 
91  typedef cValueStats<TYPE> SUPER_t;
92 
93  private:
94  cTimeSys m_tLast;
95  TYPE m_nLastVal;
96 
97  public:
98  cValueStatsRate(TYPE nValAvgEst = 0, UINT uSamplesMax = cTimeSys::k_FREQ)
99  : cValueStats<TYPE>(nValAvgEst, uSamplesMax * cTimeSys::k_FREQ)
100  , m_nLastVal(0)
101  {
103  ASSERT(uSamplesMax > 0);
104  }
105 
107  {
108  m_tLast.InitTime();
109  this->m_uSamplesQty = 0;
110  }
111  bool AddStat(TYPE nVal)
112  {
115  if (m_tLast.isTimeValid()) // first value is useless?
116  {
117  TIMESYSD_t tDiff = tNow.get_TimeSys() - m_tLast.get_TimeSys();
118  if (tDiff <= 0) // 2 values at the same time make no sense.
119  return false;
120  if (this->m_uSamplesQty < this->m_uSamplesMax)
121  {
122  this->m_uSamplesQty += tDiff;
123  }
124  else
125  {
126  // I didn't get a sample for too long!
127  // Make this value represent the entire time period.
128  this->m_uSamplesQty = this->m_uSamplesMax;
129  }
130  SUPER_t::AddStatW(nVal, (TYPE)(tDiff) / (TYPE)(this->m_uSamplesQty));
131  }
132  else
133  {
134  this->m_nValAvg = nVal; // First value is only useful like this.
135  }
136  m_tLast = tNow;
137  this->UnionValue(nVal); // min/max should be for the m_nValAvg??
138  return true;
139  }
140  bool AddStatDiff(TYPE nVal)
141  {
144  if (!AddStat(m_tLast.isTimeValid() ? (nVal - m_nLastVal) : 0))
145  return false;
146  m_nLastVal = nVal;
147  return true;
148  }
149  bool AddStatDiffX(TYPE nVal)
150  {
153  if (nVal < m_nLastVal)
154  {
155  m_nLastVal = 0;
156  }
157  return AddStatDiff(nVal);
158  }
160  };
161 
162 #ifdef GRAY_DLL // force implementation/instantiate for DLL/SO.
163  template class GRAYLIB_LINK cValueStats<float>;
164  template class GRAYLIB_LINK cValueStats<double>;
165  template class GRAYLIB_LINK cValueStatsRate<double>;
166 #endif
167 
168 };
169 #endif // _INC_cValueStats_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define TYPE
Definition: StrT.cpp:38
#define ASSERT(exp)
Definition: cDebugAssert.h:87
Definition: cValueRange.h:21
Definition: cValueStats.h:83
bool AddStat(TYPE nVal)
Definition: cValueStats.h:111
bool AddStatDiffX(TYPE nVal)
Definition: cValueStats.h:149
bool AddStatDiff(TYPE nVal)
Definition: cValueStats.h:140
UNITTEST2_PREDEF(cValueStats)
cValueStatsRate(TYPE nValAvgEst=0, UINT uSamplesMax=cTimeSys::k_FREQ)
Definition: cValueStats.h:98
void ResetStatsTime()
Definition: cValueStats.h:106
Definition: cValueStats.h:22
const UINT m_uSamplesMax
average is calculated over n samples.
Definition: cValueStats.h:31
UINT m_uSamplesQty
current number of samples I have processed. for AddStat().
Definition: cValueStats.h:32
void AddStat(TYPE nVal)
Definition: cValueStats.h:68
void put_ValAvg(TYPE nValAvg)
Definition: cValueStats.h:63
TYPE get_ValAvg() const
Definition: cValueStats.h:59
TYPE m_nValAvg
running average.
Definition: cValueStats.h:33
cValueStats(TYPE nValAvgStartingEst=0, UINT uSamplesMax=100)
Definition: cValueStats.h:51
void AddStatW(TYPE nVal, TYPE nWeight)
Definition: cValueStats.h:36
Definition: cTimeSys.h:93
void InitTime(TIMESYS_t t=k_CLEAR) noexcept
Definition: cTimeSys.h:152
static const TIMESYS_t k_FREQ
milliSec per Sec
Definition: cTimeSys.h:100
TIMESYS_t get_TimeSys() const noexcept
Definition: cTimeSys.h:147
static TIMESYS_t GetTimeNow() noexcept
Definition: cTimeSys.h:121
bool isTimeValid() const noexcept
Definition: cTimeSys.h:143
Definition: cMesh.h:22
UNITTEST2_PREDEF(cQuadtree)
INT32 TIMESYSD_t
Time delta. signed milli-Seconds Span. cTimeSys::k_DMAX, cTimeSys::k_INF = MAILSLOT_WAIT_FOREVER.
Definition: cTimeSys.h:28