hpp-statistics: include/hpp/statistics/bin.hh Source File

hpp-statistics 9.0.2 Classes for doing statistics.

Loading...
Searching...
No Matches
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#ifndef HPP_STATISTICS_BIN_HH
18#define HPP_STATISTICS_BIN_HH
19
20#include <algorithm>
21#include <list>
22#include <ostream>
23
24#include "hpp/statistics/config.hh"
25#include "hpp/statistics/fwd.hh"
26
27namespace hpp {
28namespace statistics {
35 class HPP_STATISTICS_DLLAPI Bin {
36 public:
38 const std::size_t& freq() const { return freq_; }
39
42 std::size_t operator++() { return ++freq_; }
43
46 std::size_t operator++(int) { return freq_++; }
47
49 virtual std::ostream& print(std::ostream& os) const {
50 return printValue(os << freq() << " - ");
51 }
52
54 virtual std::ostream& printValue(std::ostream& os) const = 0;
55
56 protected:
58 Bin() : freq_(0) {}
59 virtual ~Bin() {}
60
61 private:
63 std::size_t freq_;
64};
65
66 inline std::ostream& operator<<(std::ostream& os,
67 const hpp::statistics::Bin& b) {
68 return b.print(os);
69}
70
74template <typename T>
75 class HPP_STATISTICS_DLLAPI Statistics {
76 public:
77 typedef typename std::list<T> Container;
78 typedef typename Container::iterator iterator;
79 typedef typename Container::const_iterator const_iterator;
80
85 virtual std::size_t freq(const T& bin) const;
86
91 virtual Proba_t relativeFreq(const T& bin) const;
92
95 std::size_t numberOfObservations() const { return counts_; }
96
98 unsigned int numberOfBins() const { return bins_.size(); }
99
101 virtual std::ostream& print(std::ostream& os) const;
102
103 const_iterator find(const T& bin) const;
104
105 template <typename U>
106 const_iterator find(const U& value) const;
107
110 const_iterator begin() const { return bins_.begin(); }
111
114 const_iterator end() const { return bins_.end(); }
115
117 void clear() { bins_.clear(); }
118
119 protected:
121 Statistics();
122
126 virtual T& increment(const T& bin) __attribute__((deprecated));
127
131 virtual iterator insert(const T& bin);
132
133 private:
134 Container bins_;
135
136 std::size_t counts_;
137};
138
139template <typename T>
140std::ostream& operator<<(std::ostream& os,
141 const hpp::statistics::Statistics<T>& ss);
142} // namespace statistics
143} // namespace hpp
144
146
147namespace hpp {
148namespace statistics {
149template <typename T>
150T& Statistics<T>::increment(const T& b) {
151 counts_++;
152 iterator it = bins_.begin();
153 for (; it != bins_.end(); it++) {
154 if (!(*it < b)) {
155 if (!(*it == b)) it = bins_.insert(it, b);
156 (*it)++;
157 return *it;
158 }
159 }
160 it = bins_.insert(it, b);
161 (*it)++;
162 return *it;
163}
164
165template <typename T>
166 typename Statistics<T>::iterator Statistics<T>::insert(const T& b) {
167 counts_++;
168 iterator it = bins_.begin();
169 for (; it != bins_.end(); it++) {
170 if (!(*it < b)) {
171 if (!(*it == b)) it = bins_.insert(it, b);
172 (*it)++;
173 return it;
174 }
175 }
176 it = bins_.insert(it, b);
177 (*it)++;
178 return it;
179}
180
181template <typename T>
182 typename Statistics<T>::const_iterator Statistics<T>::find(const T& b) const {
183 for (const_iterator it = bins_.begin(); it != bins_.end(); it++) {
184 if (*it < b) continue;
185 if (*it == b) return it;
186 break;
187 }
188 return bins_.end();
189}
190
191template <typename T>
192template <typename U>
193 typename Statistics<T>::const_iterator Statistics<T>::find(const U& v) const {
194 return find(T(v));
195}
196
197template <typename T>
198 size_t Statistics<T>::freq(const T& b) const {
199 const_iterator it = std::find(bins_.begin(), bins_.end(), b);
200 if (it == bins_.end()) {
201 return 0;
202 }
203 return it->freq();
204}
205
206template <typename T>
207 Proba_t Statistics<T>::relativeFreq(const T& b) const {
208 const_iterator it = std::find(bins_.begin(), bins_.end(), b);
209 if (it == bins_.end()) {
210 return 0;
211 }
212 return (Proba_t)it->freq() / (Proba_t)numberOfObservations();
213}
214
215template <typename T>
216Statistics<T>::Statistics() : bins_(), counts_(0) {}
217
218template <typename T>
219std::ostream& Statistics<T>::print(std::ostream& os) const {
220 const_iterator it;
221 for (it = begin(); it != end(); it++) {
222 it->print(os) << std::endl;
223 }
224 os << "Total number of observations: " << numberOfObservations();
225 return os;
226}
227
228template <typename T>
229std::ostream& operator<<(std::ostream& os,
230 const hpp::statistics::Statistics<T>& ss) {
231 return ss.print(os);
232}
233} // namespace statistics
234} // namespace hpp
235
236#endif // HPP_STATISTICS_BIN_HH
hpp::statistics::Bin
Definition bin.hh:35
hpp::statistics::Bin::operator++
std::size_t operator++()
Definition bin.hh:42
hpp::statistics::Bin::printValue
virtual std::ostream & printValue(std::ostream &os) const =0
Print the inner value of the bin.
hpp::statistics::Bin::print
virtual std::ostream & print(std::ostream &os) const
Print the bin.
Definition bin.hh:49
hpp::statistics::Bin::operator++
std::size_t operator++(int)
Definition bin.hh:46
hpp::statistics::Bin::Bin
Bin()
Constructor.
Definition bin.hh:58
hpp::statistics::Bin::freq
const std::size_t & freq() const
Return the number of element in the bin.
Definition bin.hh:38
hpp::statistics::Bin::~Bin
virtual ~Bin()
Definition bin.hh:59
hpp::statistics::Statistics
Definition bin.hh:75
hpp::statistics::Statistics::freq
virtual std::size_t freq(const T &bin) const
Definition bin.hh:198
hpp::statistics::Statistics::Container
std::list< T > Container
Definition bin.hh:77
hpp::statistics::Statistics::insert
virtual iterator insert(const T &bin)
Definition bin.hh:166
hpp::statistics::Statistics::end
const_iterator end() const
Definition bin.hh:114
hpp::statistics::Statistics::clear
void clear()
Remove all element.
Definition bin.hh:117
hpp::statistics::Statistics::relativeFreq
virtual Proba_t relativeFreq(const T &bin) const
Definition bin.hh:207
hpp::statistics::Statistics::find
const_iterator find(const T &bin) const
Definition bin.hh:182
hpp::statistics::Statistics::increment
virtual T & increment(const T &bin) __attribute__((deprecated))
Definition bin.hh:150
hpp::statistics::Statistics::const_iterator
Container::const_iterator const_iterator
Definition bin.hh:79
hpp::statistics::Statistics::iterator
Container::iterator iterator
Definition bin.hh:78
hpp::statistics::Statistics::print
virtual std::ostream & print(std::ostream &os) const
Put the results in a stream.
Definition bin.hh:219
hpp::statistics::Statistics::numberOfBins
unsigned int numberOfBins() const
Return the number of bins.
Definition bin.hh:98
hpp::statistics::Statistics::begin
const_iterator begin() const
Definition bin.hh:110
hpp::statistics::Statistics::Statistics
Statistics()
Constructor.
Definition bin.hh:216
hpp::statistics::Statistics::find
const_iterator find(const U &value) const
Definition bin.hh:193
hpp::statistics::Statistics::numberOfObservations
std::size_t numberOfObservations() const
Definition bin.hh:95
config.hh
HPP_STATISTICS_DLLAPI
#define HPP_STATISTICS_DLLAPI
Definition config.hh:88
fwd.hh
hpp::statistics::Proba_t
double Proba_t
Definition fwd.hh:22
hpp::statistics::operator<<
std::ostream & operator<<(std::ostream &os, const hpp::statistics::Bin &b)
Definition bin.hh:66
hpp
Implementation.