hpp-statistics  5.0.0
Classes for doing statistics.
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 
27 namespace hpp {
28 namespace statistics {
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 
74 template <typename T>
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 
139 template <typename T>
140 std::ostream& operator<<(std::ostream& os,
142 } // namespace statistics
143 } // namespace hpp
144 
146 
147 namespace hpp {
148 namespace statistics {
149 template <typename T>
150 T& 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 
165 template <typename T>
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 
181 template <typename T>
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 
191 template <typename T>
192 template <typename U>
194  return find(T(v));
195 }
196 
197 template <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 
206 template <typename T>
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 
215 template <typename T>
216 Statistics<T>::Statistics() : bins_(), counts_(0) {}
217 
218 template <typename T>
219 std::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 
228 template <typename T>
229 std::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
const_iterator begin() const
Definition: bin.hh:110
unsigned int numberOfBins() const
Return the number of bins.
Definition: bin.hh:98
const std::size_t & freq() const
Return the number of element in the bin.
Definition: bin.hh:38
virtual ~Bin()
Definition: bin.hh:59
Implementation.
Definition: main.hh:17
double Proba_t
Definition: fwd.hh:22
virtual std::ostream & print(std::ostream &os) const
Put the results in a stream.
Definition: bin.hh:219
std::size_t numberOfObservations() const
Definition: bin.hh:95
Container::const_iterator const_iterator
Definition: bin.hh:79
virtual T & increment(const T &bin) __attribute__((deprecated))
Definition: bin.hh:150
std::size_t operator++(int)
Definition: bin.hh:46
std::ostream & operator<<(std::ostream &os, const hpp::statistics::Statistics< T > &ss)
Definition: bin.hh:229
Definition: bin.hh:75
std::size_t operator++()
Definition: bin.hh:42
std::list< T > Container
Definition: bin.hh:77
virtual Proba_t relativeFreq(const T &bin) const
Definition: bin.hh:207
const_iterator find(const T &bin) const
Definition: bin.hh:182
Statistics()
Constructor.
Definition: bin.hh:216
#define HPP_STATISTICS_DLLAPI
Definition: config.hh:88
virtual iterator insert(const T &bin)
Definition: bin.hh:166
std::ostream & operator<<(std::ostream &os, const hpp::statistics::Bin &b)
Definition: bin.hh:66
Container::iterator iterator
Definition: bin.hh:78
virtual std::ostream & print(std::ostream &os) const
Print the bin.
Definition: bin.hh:49
void clear()
Remove all element.
Definition: bin.hh:117
const_iterator end() const
Definition: bin.hh:114
virtual std::size_t freq(const T &bin) const
Definition: bin.hh:198
Bin()
Constructor.
Definition: bin.hh:58
Definition: bin.hh:35