hpp-statistics  4.13.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 
60  private:
62  std::size_t freq_;
63 };
64 
65 inline std::ostream& operator<<(std::ostream& os,
66  const hpp::statistics::Bin& b) {
67  return b.print(os);
68 }
69 
73 template <typename T>
75  public:
76  typedef typename std::list<T> Container;
77  typedef typename Container::iterator iterator;
78  typedef typename Container::const_iterator const_iterator;
79 
84  virtual std::size_t freq(const T& bin) const;
85 
90  virtual Proba_t relativeFreq(const T& bin) const;
91 
94  std::size_t numberOfObservations() const { return counts_; }
95 
97  unsigned int numberOfBins() const { return bins_.size(); }
98 
100  virtual std::ostream& print(std::ostream& os) const;
101 
102  const_iterator find(const T& bin) const;
103 
104  template <typename U>
105  const_iterator find(const U& value) const;
106 
109  const_iterator begin() const { return bins_.begin(); }
110 
113  const_iterator end() const { return bins_.end(); }
114 
116  void clear() { bins_.clear(); }
117 
118  protected:
120  Statistics();
121 
125  virtual T& increment(const T& bin) __attribute__((deprecated));
126 
130  virtual iterator insert(const T& bin);
131 
132  private:
133  Container bins_;
134 
135  std::size_t counts_;
136 };
137 
138 template <typename T>
139 std::ostream& operator<<(std::ostream& os,
141 } // namespace statistics
142 } // namespace hpp
143 
145 
146 namespace hpp {
147 namespace statistics {
148 template <typename T>
149 T& Statistics<T>::increment(const T& b) {
150  counts_++;
151  iterator it = bins_.begin();
152  for (; it != bins_.end(); it++) {
153  if (!(*it < b)) {
154  if (!(*it == b)) it = bins_.insert(it, b);
155  (*it)++;
156  return *it;
157  }
158  }
159  it = bins_.insert(it, b);
160  (*it)++;
161  return *it;
162 }
163 
164 template <typename T>
166  counts_++;
167  iterator it = bins_.begin();
168  for (; it != bins_.end(); it++) {
169  if (!(*it < b)) {
170  if (!(*it == b)) it = bins_.insert(it, b);
171  (*it)++;
172  return it;
173  }
174  }
175  it = bins_.insert(it, b);
176  (*it)++;
177  return it;
178 }
179 
180 template <typename T>
182  for (const_iterator it = bins_.begin(); it != bins_.end(); it++) {
183  if (*it < b) continue;
184  if (*it == b) return it;
185  break;
186  }
187  return bins_.end();
188 }
189 
190 template <typename T>
191 template <typename U>
193  return find(T(v));
194 }
195 
196 template <typename T>
197 size_t Statistics<T>::freq(const T& b) const {
198  const_iterator it = std::find(bins_.begin(), bins_.end(), b);
199  if (it == bins_.end()) {
200  return 0;
201  }
202  return it->freq();
203 }
204 
205 template <typename T>
207  const_iterator it = std::find(bins_.begin(), bins_.end(), b);
208  if (it == bins_.end()) {
209  return 0;
210  }
211  return (Proba_t)it->freq() / (Proba_t)numberOfObservations();
212 }
213 
214 template <typename T>
215 Statistics<T>::Statistics() : bins_(), counts_(0) {}
216 
217 template <typename T>
218 std::ostream& Statistics<T>::print(std::ostream& os) const {
219  const_iterator it;
220  for (it = begin(); it != end(); it++) {
221  it->print(os) << std::endl;
222  }
223  os << "Total number of observations: " << numberOfObservations();
224  return os;
225 }
226 
227 template <typename T>
228 std::ostream& operator<<(std::ostream& os,
229  const hpp::statistics::Statistics<T>& ss) {
230  return ss.print(os);
231 }
232 } // namespace statistics
233 } // namespace hpp
234 
235 #endif // HPP_STATISTICS_BIN_HH
const_iterator begin() const
Definition: bin.hh:109
unsigned int numberOfBins() const
Return the number of bins.
Definition: bin.hh:97
const std::size_t & freq() const
Return the number of element in the bin.
Definition: bin.hh:38
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:218
std::size_t numberOfObservations() const
Definition: bin.hh:94
Container::const_iterator const_iterator
Definition: bin.hh:78
virtual T & increment(const T &bin) __attribute__((deprecated))
Definition: bin.hh:149
std::size_t operator++(int)
Definition: bin.hh:46
std::ostream & operator<<(std::ostream &os, const hpp::statistics::Statistics< T > &ss)
Definition: bin.hh:228
Definition: bin.hh:74
std::size_t operator++()
Definition: bin.hh:42
std::list< T > Container
Definition: bin.hh:76
virtual Proba_t relativeFreq(const T &bin) const
Definition: bin.hh:206
const_iterator find(const T &bin) const
Definition: bin.hh:181
Statistics()
Constructor.
Definition: bin.hh:215
#define HPP_STATISTICS_DLLAPI
Definition: config.hh:64
virtual iterator insert(const T &bin)
Definition: bin.hh:165
std::ostream & operator<<(std::ostream &os, const hpp::statistics::Bin &b)
Definition: bin.hh:65
Container::iterator iterator
Definition: bin.hh:77
virtual std::ostream & print(std::ostream &os) const
Print the bin.
Definition: bin.hh:49
void clear()
Remove all element.
Definition: bin.hh:116
const_iterator end() const
Definition: bin.hh:113
virtual std::size_t freq(const T &bin) const
Definition: bin.hh:197
Bin()
Constructor.
Definition: bin.hh:58
Definition: bin.hh:35