MinMax.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * MinMax.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include <WString.h>
14 #include <Print.h>
15 #include <algorithm>
16 
17 namespace Profiling
18 {
22 template <typename T> class MinMax : public Printable
23 {
24 public:
25  MinMax(const String& title) : title(title)
26  {
27  }
28 
29  const String& getTitle() const
30  {
31  return title;
32  }
33 
34  void clear();
35 
36  void update(T value);
37 
38  T getMin() const
39  {
40  return minVal;
41  }
42 
43  T getMax() const
44  {
45  return maxVal;
46  }
47 
48  T getTotal() const
49  {
50  return total;
51  }
52 
53  T getAverage() const;
54 
55  unsigned getCount() const
56  {
57  return count;
58  }
59 
60  size_t printTo(Print& p) const override;
61 
62 private:
63  String title;
64  unsigned count = 0;
65  T total = 0;
66  T minVal = 0;
67  T maxVal = 0;
68 };
69 
70 template <typename T> void MinMax<T>::clear()
71 {
72  count = 0;
73  total = minVal = maxVal = 0;
74 }
75 
76 template <typename T> void MinMax<T>::update(T value)
77 {
78  if(count == 0) {
79  minVal = value;
80  maxVal = value;
81  } else {
82  minVal = std::min(minVal, value);
83  maxVal = std::max(maxVal, value);
84  }
85  total += value;
86  ++count;
87 }
88 
89 template <typename T> T MinMax<T>::getAverage() const
90 {
91  return (count == 0) ? 0 : (total / count);
92 }
93 
94 template <typename T> size_t MinMax<T>::printTo(Print& p) const
95 {
96  auto res = p.print(title);
97  res += p.print(": count=");
98  res += p.print(count);
99  res += p.print(", total=");
100  res += p.print(total);
101  res += p.print(", min=");
102  res += p.print(minVal);
103  res += p.print(", max=");
104  res += p.print(maxVal);
105  res += p.print(", average=");
106  res += p.print(getAverage());
107  return res;
108 }
109 
111 
112 } // namespace Profiling
unsigned getCount() const
Definition: MinMax.h:55
T getTotal() const
Definition: MinMax.h:48
size_t print(char c)
Prints a single character to output stream.
Definition: Print.h:97
T getAverage() const
Definition: MinMax.h:89
MinMax(const String &title)
Definition: MinMax.h:25
The String class.
Definition: WString.h:136
Provides formatted output to stream.
Definition: Print.h:36
void clear()
Definition: MinMax.h:70
Definition: CpuUsage.h:4
void update(T value)
Definition: MinMax.h:76
T getMin() const
Definition: MinMax.h:38
Definition: Printable.h:42
size_t printTo(Print &p) const override
Definition: MinMax.h:94
T getMax() const
Definition: MinMax.h:43
const String & getTitle() const
Definition: MinMax.h:29
Class to track minimum and maximum values of a set of data, with average, total and count...
Definition: MinMax.h:22