AceCommon  1.4.6
Arduino library for low-level common functions and features with no external dependencies
GenericStats.h
1 /*
2 MIT License
3 
4 Copyright (c) 2021 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_COMMON_GENERIC_STATS_H
26 #define ACE_COMMON_GENERIC_STATS_H
27 
28 #include <stdint.h>
29 
30 namespace ace_common {
31 
43 template <typename T>
44 class GenericStats {
45  public:
47  GenericStats(): mCounter(0) {
48  reset();
49  }
50 
51  // Use default copy-constructor and assignment operator. Doxygen docs added
52  // to keep it quiet.
53 
55  GenericStats(const GenericStats&) = default;
56 
58  GenericStats& operator=(const GenericStats&) = default;
59 
64  void reset() {
65  mExpDecayAvg = 0;
66  mMin = 0;
67  mMax = 0;
68  mSum = 0;
69  mCount = 0;
70  }
71 
73  T getMax() const { return mMax; }
74 
76  T getMin() const { return mMin; }
77 
82  T getAvg() const { return (mCount > 0) ? mSum / mCount : 0; }
83 
85  T getExpDecayAvg() const { return mExpDecayAvg; }
86 
88  uint16_t getCount() const { return mCount; }
89 
95  uint16_t getCounter() const { return mCounter; }
96 
98  void update(T value) {
99  mSum += value;
100  if (mCount == 0 || value < mMin) {
101  mMin = value;
102  }
103  if (mCount == 0 || value > mMax) {
104  mMax = value;
105  }
106 
107  if (mCount == 0) {
108  mExpDecayAvg = value;
109  } else {
110  mExpDecayAvg = (mExpDecayAvg/2) + (value/2);
111  }
112 
113  mCount++;
114  mCounter++;
115  }
116 
117  private:
118  T mExpDecayAvg;
119  T mMin;
120  T mMax;
121  T mSum;
122  uint16_t mCount;
123  uint16_t mCounter;
124 };
125 
126 }
127 
128 #endif
ace_common::GenericStats::reset
void reset()
Reset the object to its initial state, except mCounter which is never reset.
Definition: GenericStats.h:64
ace_common::GenericStats::update
void update(T value)
Add the given value to the statistics.
Definition: GenericStats.h:98
ace_common::GenericStats::operator=
GenericStats & operator=(const GenericStats &)=default
Default assignment operator.
ace_common::GenericStats::getCount
uint16_t getCount() const
Number of times update() was called since last reset().
Definition: GenericStats.h:88
ace_common::GenericStats::getAvg
T getAvg() const
Return the average since the last reset().
Definition: GenericStats.h:82
ace_common::GenericStats::getExpDecayAvg
T getExpDecayAvg() const
An exponential decay average since the last reset().
Definition: GenericStats.h:85
ace_common::GenericStats
Helper class to collect timing statistics such as min, max, average, and exponential-decay average.
Definition: GenericStats.h:44
ace_common::GenericStats::getMin
T getMin() const
Return the minium since the last reset().
Definition: GenericStats.h:76
ace_common::GenericStats::GenericStats
GenericStats()
Constructor.
Definition: GenericStats.h:47
ace_common::GenericStats::getMax
T getMax() const
Return the maximum since the last reset().
Definition: GenericStats.h:73
ace_common::GenericStats::getCounter
uint16_t getCounter() const
Number of times update() was called from the beginning of time.
Definition: GenericStats.h:95