SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Stat.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 #ifndef swcdb_Common_Stats_Stat_h
7 #define swcdb_Common_Stats_Stat_h
8 
9 
10 #include "swcdb/core/MutexAtomic.h"
11 
12 
13 namespace SWC { namespace Common { namespace Stats {
14 
15 
16 
17 template<typename ValueT>
19  ValueT count;
20  ValueT total;
21  ValueT min;
22  ValueT max;
23 
25  MinMaxAvgCount() noexcept : count(0), total(0), min(0), max(0) { }
26 
28  void add(ValueT v) noexcept {
29  if(std::numeric_limits<ValueT>::max() - total > v) {
30  ++count;
31  total += v;
32  }
33  if(v > max)
34  max = v;
35  if(!min || v < min)
36  min = v;
37  }
38 
40  ValueT avg() const noexcept {
41  return count ? (total/count) : 0;
42  }
43 
45  void gather(MinMaxAvgCount<ValueT>& to) noexcept {
46  to.count = count;
47  to.min = min;
48  to.max = max;
49  to.total = total;
50  reset();
51  }
52 
54  void reset() noexcept {
55  count = total = min = max = 0;
56  }
57 };
58 
59 
60 
61 template<typename ValueT>
63  public:
64 
66  MinMaxAvgCount_Safe() noexcept : m_mutex(), m_value() { }
67 
69  virtual ~MinMaxAvgCount_Safe() noexcept { }
70 
72  void add(ValueT v) noexcept {
74  m_value.add(v);
75  }
76 
78  ValueT count() const noexcept {
80  return m_value.count;
81  }
82 
84  ValueT total() const noexcept {
86  return m_value.total;
87  }
88 
90  ValueT avg() const noexcept {
92  return m_value.avg();
93  }
94 
96  ValueT max() const noexcept {
98  return m_value.max;
99  }
100 
102  ValueT min() const noexcept {
104  return m_value.min;
105  }
106 
108  void gather(MinMaxAvgCount<ValueT>& to) noexcept {
110  m_value.gather(to);
111  }
112 
114  void reset() noexcept {
116  m_value.reset();
117  }
118 
119  void print(std::ostream& out) const {
120  ValueT min, max, avg;
121  {
123  min = m_value.min;
124  max = m_value.max;
125  avg = m_value.avg();
126  }
127  out << "(min=" << min << " max=" << max << " avg=" << avg << ')';
128  }
129 
130  private:
133 };
134 
135 
137 
138 }}}
139 
140 #endif // swcdb_Common_Stats_Stat_h
SWC::Common::Stats::MinMaxAvgCount_Safe::m_mutex
Core::MutexAtomic m_mutex
Definition: Stat.h:131
SWC::Common::Stats::MinMaxAvgCount::gather
SWC_CAN_INLINE void gather(MinMaxAvgCount< ValueT > &to) noexcept
Definition: Stat.h:45
SWC::Common::Stats::MinMaxAvgCount::count
ValueT count
Definition: Stat.h:19
SWC::Common::Stats::MinMaxAvgCount::total
ValueT total
Definition: Stat.h:20
SWC::Common::Stats::MinMaxAvgCount_Safe::avg
SWC_CAN_INLINE ValueT avg() const noexcept
Definition: Stat.h:90
SWC::Common::Stats::MinMaxAvgCount_Safe::print
void print(std::ostream &out) const
Definition: Stat.h:119
SWC::Common::Stats::MinMaxAvgCount::avg
SWC_CAN_INLINE ValueT avg() const noexcept
Definition: Stat.h:40
SWC::Core::MutexAtomic::scope
Definition: MutexAtomic.h:77
SWC::Common::Stats::MinMaxAvgCount_Safe
Definition: Stat.h:62
SWC::Common::Stats::MinMaxAvgCount
Definition: Stat.h:18
SWC::Common::Stats::MinMaxAvgCount_Safe::total
SWC_CAN_INLINE ValueT total() const noexcept
Definition: Stat.h:84
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Core::MutexAtomic
Definition: MutexAtomic.h:17
SWC::Common::Stats::MinMaxAvgCount_Safe::~MinMaxAvgCount_Safe
virtual SWC_CAN_INLINE ~MinMaxAvgCount_Safe() noexcept
Definition: Stat.h:69
MutexAtomic.h
SWC::Common::Stats::MinMaxAvgCount::MinMaxAvgCount
SWC_CAN_INLINE MinMaxAvgCount() noexcept
Definition: Stat.h:25
SWC::Common::Stats::MinMaxAvgCount_Safe::max
SWC_CAN_INLINE ValueT max() const noexcept
Definition: Stat.h:96
SWC::Common::Stats::MinMaxAvgCount_Safe::gather
SWC_CAN_INLINE void gather(MinMaxAvgCount< ValueT > &to) noexcept
Definition: Stat.h:108
SWC::Common::Stats::MinMaxAvgCount::min
ValueT min
Definition: Stat.h:21
SWC::Common::Stats::MinMaxAvgCount::add
SWC_CAN_INLINE void add(ValueT v) noexcept
Definition: Stat.h:28
SWC::Common::Stats::MinMaxAvgCount_Safe::MinMaxAvgCount_Safe
SWC_CAN_INLINE MinMaxAvgCount_Safe() noexcept
Definition: Stat.h:66
SWC::Common::Stats::MinMaxAvgCount::max
ValueT max
Definition: Stat.h:22
SWC::Common::Stats::Stat
MinMaxAvgCount_Safe< uint64_t > Stat
Definition: Stat.h:136
SWC::Common::Stats::MinMaxAvgCount_Safe::m_value
MinMaxAvgCount< ValueT > m_value
Definition: Stat.h:132
SWC::Common::Stats::MinMaxAvgCount_Safe::min
SWC_CAN_INLINE ValueT min() const noexcept
Definition: Stat.h:102
SWC::Common::Stats::MinMaxAvgCount_Safe::reset
SWC_CAN_INLINE void reset() noexcept
Definition: Stat.h:114
SWC::Common::Stats::MinMaxAvgCount_Safe::count
SWC_CAN_INLINE ValueT count() const noexcept
Definition: Stat.h:78
SWC::Common::Stats::MinMaxAvgCount::reset
SWC_CAN_INLINE void reset() noexcept
Definition: Stat.h:54
SWC::Common::Stats::MinMaxAvgCount_Safe::add
SWC_CAN_INLINE void add(ValueT v) noexcept
Definition: Stat.h:72