hpp-statistics  4.9.0
Classes for doing statistics.
success-bin.hh
Go to the documentation of this file.
1 // Copyright (c) 2014, LAAS-CNRS
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4 // This file is part of hpp-statistics.
5 // hpp-statistics is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // hpp-statistics is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // hpp-statistics. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #ifndef HPP_STATISTICS_SUCCESSBIN_HH
19 # define HPP_STATISTICS_SUCCESSBIN_HH
20 
21 # include <iostream>
22 # include <set>
23 
24 # include <hpp/util/debug.hh>
25 
26 # include "hpp/statistics/config.hh"
27 # include "hpp/statistics/bin.hh"
28 # include "hpp/statistics/fwd.hh"
29 
30 # define HPP_DEFINE_REASON_FAILURE(ID, STRING) \
31  const ::hpp::statistics::SuccessBin::Reason ID = \
32  ::hpp::statistics::SuccessBin::createReason ( STRING ); \
33  struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n
34 
35 namespace hpp {
36  namespace statistics {
38  class HPP_STATISTICS_DLLAPI SuccessBin : public Bin
39  {
40  public:
44  struct Reason {
45  std::size_t id;
46  std::string what;
47  Reason (std::size_t a_id, std::string a_what) :
48  id (a_id), what (a_what) {}
49  };
50 
52  const static Reason REASON_UNKNOWN;
53 
55  SuccessBin (const bool success, const Reason& r = REASON_UNKNOWN) :
56  Bin(), success_ (success), reason_(r)
57  {
58  if (success_)
59  reason_ = REASON_SUCCESS;
60  }
61 
64  inline bool isSuccess () const
65  {
66  return success_;
67  }
68 
70  inline const Reason& reason () const
71  {
72  return reason_;
73  }
74 
76  inline const std::string& reasonString () const;
77 
81  inline bool operator == (const SuccessBin& other) const
82  {
83  return reason_.id == other.reason().id;
84  }
85 
89  inline bool operator < (const SuccessBin& other) const
90  {
91  return reason_.id < other.reason ().id;
92  }
93 
96  static Reason createReason (const std::string& what)
97  {
98  return Reason (reasonID_last++, what);
99  }
100 
101  private:
102  bool success_;
103  size_t freq_;
104  Reason reason_;
105 
107  const static Reason REASON_SUCCESS;
108  static std::size_t reasonID_last;
109 
110  inline std::ostream& printValue (std::ostream& os) const
111  {
112  os << "Event ";
113  if (success_) os << "'Success'";
114  else os << "'Failure': " << reason_.what;
115  return os;
116  }
117  };
118 
119  class HPP_STATISTICS_DLLAPI SuccessStatistics :
120  public Statistics < SuccessBin >
121  {
122  public:
124 
126  SuccessStatistics (const std::string name = "",
127  const std::size_t& logRatio = 2)
128  : name_ (name), logRatio_ (logRatio)
129  {}
130 
133  : name_ (other.name_), logRatio_ (other.logRatio_)
134  {}
135 
137  void addSuccess ()
138  {
139  insert (SuccessBin (true));
140  }
141 
147  {
148  insert (SuccessBin (false, r));
149 #ifdef HPP_DEBUG
150  isLowRatio (true);
151 #endif
152  }
153 
154  inline bool isLowRatio (const bool autoPrint = false) const
155  {
156  bool lowRatio = (logRatio_ * nbSuccess () < numberOfObservations());
157  if (autoPrint && lowRatio)
158  hppDout (info, name_ << ":\n" << *this);
159  return lowRatio;
160  }
161 
163  std::size_t nbSuccess () const
164  {
165  return freq (SuccessBin(true));
166  }
167 
169  std::size_t nbFailure () const
170  {
171  return numberOfObservations() - nbSuccess();
172  }
173 
175  std::size_t nbFailure (const SuccessBin::Reason& r) const
176  {
177  return freq (SuccessBin (false, r));
178  }
179 
180  std::string name_;
181 
183  std::size_t logRatio_;
184  };
185  } // namespace statistics
186 } // namespace hpp
187 
188 #endif // HPP_STATISTICS_SUCCESSBIN_HH
void addFailure(const SuccessBin::Reason &r=SuccessBin::REASON_UNKNOWN)
Definition: success-bin.hh:146
const Reason & reason() const
If this bin represents &#39;failure&#39;, returns the reason.
Definition: success-bin.hh:70
std::string what
Definition: success-bin.hh:46
std::size_t id
Definition: success-bin.hh:45
#define hppDout(channel, data)
Implementation.
static const Reason REASON_UNKNOWN
The default reason for &#39;failure&#39;.
Definition: success-bin.hh:52
std::size_t nbFailure() const
Count the number of failure, in total.
Definition: success-bin.hh:169
SuccessStatistics(const std::string name="", const std::size_t &logRatio=2)
Constructor.
Definition: success-bin.hh:126
bool isLowRatio(const bool autoPrint=false) const
Definition: success-bin.hh:154
Definition: bin.hh:85
Definition: success-bin.hh:44
This class count the number of success and failure.
Definition: success-bin.hh:38
static Reason createReason(const std::string &what)
Definition: success-bin.hh:96
std::size_t nbFailure(const SuccessBin::Reason &r) const
Count the number of a particular failure.
Definition: success-bin.hh:175
Definition: success-bin.hh:119
void addSuccess()
Add a &#39;success&#39;.
Definition: success-bin.hh:137
SuccessBin(const bool success, const Reason &r=REASON_UNKNOWN)
Constructor.
Definition: success-bin.hh:55
Reason(std::size_t a_id, std::string a_what)
Definition: success-bin.hh:47
bool isSuccess() const
Definition: success-bin.hh:64
std::string name_
Definition: success-bin.hh:180
Statistics< SuccessBin > Parent
Definition: success-bin.hh:123
std::size_t nbSuccess() const
Count the number of success.
Definition: success-bin.hh:163
Definition: bin.hh:35
std::size_t logRatio_
If nbSuccess() * logRatio < numberOfObservations(), write to log.
Definition: success-bin.hh:183
SuccessStatistics(const SuccessStatistics &other)
Copy Constructor.
Definition: success-bin.hh:132