diff --git a/include/hpp/statistics/bin.hh b/include/hpp/statistics/bin.hh index 4512a8cb3770f1a0fbd26887f7e751a09aa26aee..3f76157497ddd2fe584042e7ac80e6d46ef34ba9 100644 --- a/include/hpp/statistics/bin.hh +++ b/include/hpp/statistics/bin.hh @@ -25,7 +25,7 @@ namespace hpp { namespace statistics { class Bin; - static std::ostream& operator<< (std::ostream& os, const Bin& b); + std::ostream& operator<< (std::ostream& os, const Bin& b); /// Abstract class representing a bin. /// diff --git a/include/hpp/statistics/success-bin.hh b/include/hpp/statistics/success-bin.hh index a6aea9486a2265c641afa73c318fbcca658cb45b..376eeea89e2c7f74e334deca60094f055899a88e 100644 --- a/include/hpp/statistics/success-bin.hh +++ b/include/hpp/statistics/success-bin.hh @@ -24,14 +24,15 @@ # include "hpp/statistics/config.hh" # include "hpp/statistics/bin.hh" -# define DEFINE_REASON_FAILURE(ID, STRING) \ - ::hpp::statistics::SuccessBin::Reason ID = ::hpp::statistics::SuccessBin::createReason ( STRING ); \ +# define HPP_DEFINE_REASON_FAILURE(ID, STRING) \ + const ::hpp::statistics::SuccessBin::Reason ID = \ + ::hpp::statistics::SuccessBin::createReason ( STRING ); \ struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n namespace hpp { namespace statistics { class SuccessStatistics; - static std::ostream& operator<< (std::ostream&, const SuccessStatistics&); + std::ostream& operator<< (std::ostream&, const SuccessStatistics&); /// This class count the number of success and failure. class HPP_STATISTICS_DLLAPI SuccessBin : public Bin @@ -40,10 +41,10 @@ namespace hpp { class Reason; /// The default reason for 'failure'. - static Reason REASON_UNKNOWN; + const static Reason REASON_UNKNOWN; /// Constructor - SuccessBin (const bool success, Reason r = REASON_UNKNOWN); + SuccessBin (const bool success, const Reason& r = REASON_UNKNOWN); /// Value of the bin. /// \return True is it counts "success", False otherwise. @@ -99,7 +100,7 @@ namespace hpp { Reason reason_; /// The reason for 'success'. - static Reason REASON_SUCCESS; + const static Reason REASON_SUCCESS; static unsigned int reasonID_last; std::ostream& printValue (std::ostream& os) const; @@ -108,6 +109,9 @@ namespace hpp { class HPP_STATISTICS_DLLAPI SuccessStatistics { public: + /// Constructor + SuccessStatistics (); + /// Add a 'success' void addSuccess (); @@ -117,8 +121,22 @@ namespace hpp { /// to define a new reason. void addFailure (const SuccessBin::Reason& r = SuccessBin::REASON_UNKNOWN); + /// Count the number of success. + unsigned int nbSuccess () const; + + /// Count the number of failure, in total. + unsigned int nbFailure () const; + + /// Count the number of a particular failure. + unsigned int nbFailure (const SuccessBin::Reason& r) const; + private: - std::set <SuccessBin> bins; + std::set <SuccessBin> bins_; + + unsigned int counts_; + + /// Increment the bin. + void increment (SuccessBin& b); /// Put the results in a stream. std::ostream& print (std::ostream& os) const; diff --git a/src/success-bin.cc b/src/success-bin.cc index 22b00f9b92f9409818be25dcb2ca6f518ab9edd0..b812e24e3898cecdf5672022c3dd1e33dfe793d5 100644 --- a/src/success-bin.cc +++ b/src/success-bin.cc @@ -20,22 +20,20 @@ namespace hpp { namespace statistics { - unsigned int reasonID_last = 0; - SuccessBin::Reason SuccessBin::REASON_SUCCESS = SuccessBin::createReason ("Success"); - SuccessBin::Reason SuccessBin::REASON_UNKNOWN = SuccessBin::createReason ("Unknown"); - - DEFINE_REASON_FAILURE (REASON_MAX_ITERATION, "Max iteration reached"); + unsigned int SuccessBin::reasonID_last = 0; + const SuccessBin::Reason SuccessBin::REASON_SUCCESS = SuccessBin::createReason ("Success"); + const SuccessBin::Reason SuccessBin::REASON_UNKNOWN = SuccessBin::createReason ("Unknown"); SuccessBin::Reason SuccessBin::createReason (const std::string& what) { return Reason (reasonID_last++, what); } - SuccessBin::SuccessBin (const bool success, Reason r) : + SuccessBin::SuccessBin (const bool success, const Reason& r) : success_ (success), freq_ (0), reason_(r) { if (success_) - r = REASON_SUCCESS; + reason_ = REASON_SUCCESS; } bool SuccessBin::isSuccess () const @@ -62,7 +60,7 @@ namespace hpp { { os << "Event "; if (success_) os << "'Success'"; - else os << "'Failure'"; + else os << "'Failure': " << reason_.what; return os; } @@ -81,33 +79,65 @@ namespace hpp { return reason_.id < other.reason ().id; } + SuccessStatistics::SuccessStatistics () : bins_(), counts_(0) + {} + void SuccessStatistics::addFailure (const SuccessBin::Reason& r) { - SuccessBin b (false, r); - std::set<SuccessBin>::iterator it = bins.find (b); - if (it == bins.end()) - bins.insert (b); - ((SuccessBin)(*it))++; + SuccessBin b = SuccessBin (false, r); + increment (b); } void SuccessStatistics::addSuccess () { - SuccessBin b (true); - std::set<SuccessBin>::iterator it = bins.find (b); - if (it == bins.end()) - bins.insert (b); - ((SuccessBin)(*it))++; + SuccessBin b = SuccessBin (true); + increment (b); + } + + void SuccessStatistics::increment (SuccessBin& b) + { + b++; + std::pair<std::set<SuccessBin>::iterator, bool> it = bins_.insert (b); + if (!it.second) { + // The bin exists already. We must copy and reinsert it. + SuccessBin bin = *(it.first); + bin ++; + bins_.erase (b); + bins_.insert (bin); + } + counts_++; + } + + unsigned int SuccessStatistics::nbSuccess () const + { + std::set<SuccessBin>::iterator it = bins_.find (SuccessBin(true)); + if (it != bins_.end()) + return it->freq(); + return 0; + } + + unsigned int SuccessStatistics::nbFailure () const + { + return counts_ - nbSuccess(); + } + + unsigned int SuccessStatistics::nbFailure (const SuccessBin::Reason& r) const + { + std::set<SuccessBin>::iterator it = bins_.find (SuccessBin(false, r)); + if (it != bins_.end()) + return it->freq(); + return 0; } std::ostream& SuccessStatistics::print (std::ostream& os) const { std::set <SuccessBin>::const_iterator it; - for (it = bins.begin(); it != bins.end(); it++) + for (it = bins_.begin(); it != bins_.end(); it++) os << (*it); return os; } - static std::ostream& operator<< (std::ostream& os, const SuccessStatistics& ss) + std::ostream& operator<< (std::ostream& os, const SuccessStatistics& ss) { return ss.print (os); }