| Directory: | ./ |
|---|---|
| File: | include/hpp/statistics/success-bin.hh |
| Date: | 2025-05-06 12:03:36 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 23 | 29 | 79.3% |
| Branches: | 10 | 20 | 50.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | #ifndef HPP_STATISTICS_SUCCESSBIN_HH | ||
| 18 | #define HPP_STATISTICS_SUCCESSBIN_HH | ||
| 19 | |||
| 20 | #include <hpp/util/debug.hh> | ||
| 21 | #include <iostream> | ||
| 22 | #include <set> | ||
| 23 | |||
| 24 | #include "hpp/statistics/bin.hh" | ||
| 25 | #include "hpp/statistics/config.hh" | ||
| 26 | #include "hpp/statistics/fwd.hh" | ||
| 27 | |||
| 28 | #define HPP_DEFINE_REASON_FAILURE(ID, STRING) \ | ||
| 29 | const ::hpp::statistics::SuccessBin::Reason ID = \ | ||
| 30 | ::hpp::statistics::SuccessBin::createReason(STRING); \ | ||
| 31 | struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n | ||
| 32 | |||
| 33 | namespace hpp { | ||
| 34 | namespace statistics { | ||
| 35 | /// This class count the number of success and failure. | ||
| 36 | class HPP_STATISTICS_DLLAPI SuccessBin : public Bin { | ||
| 37 | public: | ||
| 38 | /// In case of failure, you can provide a reason. | ||
| 39 | /// Use macro DEFINE_REASON_FAILURE (REASON_NAME, Reason string) | ||
| 40 | /// to define a new reason. | ||
| 41 | struct Reason { | ||
| 42 | std::size_t id; | ||
| 43 | std::string what; | ||
| 44 | 3 | Reason(std::size_t a_id, std::string a_what) : id(a_id), what(a_what) {} | |
| 45 | }; | ||
| 46 | |||
| 47 | /// The default reason for 'failure'. | ||
| 48 | const static Reason REASON_UNKNOWN; | ||
| 49 | |||
| 50 | /// Constructor | ||
| 51 | 104 | SuccessBin(const bool success, const Reason& r = REASON_UNKNOWN) | |
| 52 |
1/2✓ Branch 2 taken 104 times.
✗ Branch 3 not taken.
|
104 | : Bin(), success_(success), reason_(r) { |
| 53 |
3/4✓ Branch 0 taken 47 times.
✓ Branch 1 taken 57 times.
✓ Branch 3 taken 47 times.
✗ Branch 4 not taken.
|
104 | if (success_) reason_ = REASON_SUCCESS; |
| 54 | 104 | } | |
| 55 | |||
| 56 | /// Value of the bin. | ||
| 57 | /// \return True is it counts "success", False otherwise. | ||
| 58 | inline bool isSuccess() const { return success_; } | ||
| 59 | |||
| 60 | /// If this bin represents 'failure', returns the reason. | ||
| 61 | 281 | inline const Reason& reason() const { return reason_; } | |
| 62 | |||
| 63 | /// If this bin represents 'failure', returns the reason as a string. | ||
| 64 | inline const std::string& reasonString() const; | ||
| 65 | |||
| 66 | /// Check for equality. | ||
| 67 | /// \return True if both are 'success' or if they are both 'failure' | ||
| 68 | /// with the same Reason. | ||
| 69 | 105 | inline bool operator==(const SuccessBin& other) const { | |
| 70 | 105 | return reason_.id == other.reason().id; | |
| 71 | } | ||
| 72 | |||
| 73 | /// Comparison | ||
| 74 | /// \return the comparison of their reason id. | ||
| 75 | /// 'success' has a reason id of INT_MIN. | ||
| 76 | 176 | inline bool operator<(const SuccessBin& other) const { | |
| 77 | 176 | return reason_.id < other.reason().id; | |
| 78 | } | ||
| 79 | |||
| 80 | /// Create a new Reason | ||
| 81 | /// \param what The text associated with the reason. | ||
| 82 | 3 | static Reason createReason(const std::string& what) { | |
| 83 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | return Reason(reasonID_last++, what); |
| 84 | } | ||
| 85 | |||
| 86 | private: | ||
| 87 | bool success_; | ||
| 88 | size_t freq_; | ||
| 89 | Reason reason_; | ||
| 90 | |||
| 91 | /// The reason for 'success'. | ||
| 92 | const static Reason REASON_SUCCESS; | ||
| 93 | static std::size_t reasonID_last; | ||
| 94 | |||
| 95 | ✗ | inline std::ostream& printValue(std::ostream& os) const { | |
| 96 | ✗ | os << "Event "; | |
| 97 | ✗ | if (success_) | |
| 98 | ✗ | os << "'Success'"; | |
| 99 | else | ||
| 100 | ✗ | os << "'Failure': " << reason_.what; | |
| 101 | ✗ | return os; | |
| 102 | } | ||
| 103 | }; | ||
| 104 | |||
| 105 | class HPP_STATISTICS_DLLAPI SuccessStatistics : public Statistics<SuccessBin> { | ||
| 106 | public: | ||
| 107 | typedef Statistics<SuccessBin> Parent; | ||
| 108 | |||
| 109 | /// Constructor | ||
| 110 | 1 | SuccessStatistics(const std::string name = "", | |
| 111 | 2 | const std::size_t& logRatio = 2) | |
| 112 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | : name_(name), logRatio_(logRatio) {} |
| 113 | |||
| 114 | /// Copy Constructor | ||
| 115 | SuccessStatistics(const SuccessStatistics& other) | ||
| 116 | : name_(other.name_), logRatio_(other.logRatio_) {} | ||
| 117 | |||
| 118 | /// Add a 'success' | ||
| 119 |
1/2✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
|
45 | void addSuccess() { insert(SuccessBin(true)); } |
| 120 | |||
| 121 | /// Add a 'failure' | ||
| 122 | /// \param r the reason of the 'failure' | ||
| 123 | /// \note Use macro DEFINE_REASON_FAILURE (REASON_NAME, 'Reason details') | ||
| 124 | /// to define a new reason. | ||
| 125 | 55 | void addFailure(const SuccessBin::Reason& r = SuccessBin::REASON_UNKNOWN) { | |
| 126 |
1/2✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
|
55 | insert(SuccessBin(false, r)); |
| 127 | #ifdef HPP_DEBUG | ||
| 128 | isLowRatio(true); | ||
| 129 | #endif | ||
| 130 | 55 | } | |
| 131 | |||
| 132 | inline bool isLowRatio(const bool autoPrint = false) const { | ||
| 133 | bool lowRatio = (logRatio_ * nbSuccess() < numberOfObservations()); | ||
| 134 | if (autoPrint && lowRatio) hppDout(info, name_ << ":\n" << *this); | ||
| 135 | return lowRatio; | ||
| 136 | } | ||
| 137 | |||
| 138 | /// Count the number of success. | ||
| 139 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | std::size_t nbSuccess() const { return freq(SuccessBin(true)); } |
| 140 | |||
| 141 | /// Count the number of failure, in total. | ||
| 142 | 1 | std::size_t nbFailure() const { return numberOfObservations() - nbSuccess(); } | |
| 143 | |||
| 144 | /// Count the number of a particular failure. | ||
| 145 | 2 | std::size_t nbFailure(const SuccessBin::Reason& r) const { | |
| 146 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | return freq(SuccessBin(false, r)); |
| 147 | } | ||
| 148 | |||
| 149 | std::string name_; | ||
| 150 | |||
| 151 | /// If nbSuccess() * logRatio < numberOfObservations(), write to log. | ||
| 152 | std::size_t logRatio_; | ||
| 153 | }; | ||
| 154 | } // namespace statistics | ||
| 155 | } // namespace hpp | ||
| 156 | |||
| 157 | #endif // HPP_STATISTICS_SUCCESSBIN_HH | ||
| 158 |