hpp-statistics  4.9.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 <ostream>
21 # include <list>
22 # include <algorithm>
23 
24 # include "hpp/statistics/config.hh"
25 # include "hpp/statistics/fwd.hh"
26 
27 namespace hpp {
28  namespace statistics {
35  class HPP_STATISTICS_DLLAPI Bin
36  {
37  public:
39  const std::size_t& freq () const
40  {
41  return freq_;
42  }
43 
46  std::size_t operator ++()
47  {
48  return ++freq_;
49  }
50 
53  std::size_t operator ++(int)
54  {
55  return freq_++;
56  }
57 
59  virtual std::ostream& print (std::ostream& os) const
60  {
61  return printValue (os << freq () << " - ");
62  }
63 
65  virtual std::ostream& printValue (std::ostream& os) const = 0;
66 
67  protected:
69  Bin () : freq_ (0) {}
70 
71  private:
73  std::size_t freq_;
74  };
75 
76  inline std::ostream& operator<< (std::ostream& os, const hpp::statistics::Bin& b)
77  {
78  return b.print (os);
79  }
80 
84  template < typename T >
85  class HPP_STATISTICS_DLLAPI Statistics
86  {
87  public:
88  typedef typename std::list < T > Container;
89  typedef typename Container::iterator iterator;
90  typedef typename Container::const_iterator const_iterator;
91 
96  virtual std::size_t freq (const T& bin) const;
97 
102  virtual Proba_t relativeFreq (const T& bin) const;
103 
106  std::size_t numberOfObservations () const
107  {
108  return counts_;
109  }
110 
112  unsigned int numberOfBins () const
113  {
114  return bins_.size ();
115  }
116 
118  virtual std::ostream& print (std::ostream& os) const;
119 
120  const_iterator find (const T& bin) const;
121 
122  template < typename U > const_iterator find (const U& value) const;
123 
126  const_iterator begin() const
127  {
128  return bins_.begin();
129  }
130 
133  const_iterator end() const
134  {
135  return bins_.end();
136  }
137 
139  void clear ()
140  {
141  bins_.clear();
142  }
143 
144  protected:
146  Statistics();
147 
151  virtual T& increment (const T& bin) __attribute__ ((deprecated));
152 
156  virtual iterator insert (const T& bin);
157 
158  private:
159  Container bins_;
160 
161  std::size_t counts_;
162  };
163 
164  template < typename T >
165  std::ostream& operator<< (std::ostream& os, const hpp::statistics::Statistics <T>& ss);
166  } // namespace statistics
167 } // namespace hpp
168 
170 
171 namespace hpp {
172  namespace statistics {
173  template < typename T >
175  {
176  counts_++;
177  iterator it = bins_.begin ();
178  for (; it != bins_.end (); it++) {
179  if (! (*it < b)) {
180  if (! (*it == b))
181  it = bins_.insert (it, b);
182  (*it)++;
183  return *it;
184  }
185  }
186  it = bins_.insert (it, b);
187  (*it)++;
188  return *it;
189  }
190 
191  template < typename T >
193  {
194  counts_++;
195  iterator it = bins_.begin ();
196  for (; it != bins_.end (); it++) {
197  if (! (*it < b)) {
198  if (! (*it == b))
199  it = bins_.insert (it, b);
200  (*it)++;
201  return it;
202  }
203  }
204  it = bins_.insert (it, b);
205  (*it)++;
206  return it;
207  }
208 
209  template < typename T>
211  {
212  for (const_iterator it = bins_.begin ();
213  it != bins_.end (); it++) {
214  if (*it < b)
215  continue;
216  if (*it == b)
217  return it;
218  break;
219  }
220  return bins_.end ();
221  }
222 
223  template < typename T> template < typename U >
225  {
226  return find (T (v));
227  }
228 
229  template < typename T >
230  size_t Statistics <T>::freq (const T& b) const
231  {
232  const_iterator it = std::find (bins_.begin (), bins_.end (), b);
233  if (it == bins_.end ()) {
234  return 0;
235  }
236  return it->freq ();
237  }
238 
239  template < typename T >
241  {
242  const_iterator it = std::find (bins_.begin (), bins_.end (), b);
243  if (it == bins_.end ()) {
244  return 0;
245  }
246  return (Proba_t)it->freq () / (Proba_t)numberOfObservations();
247  }
248 
249  template < typename T >
250  Statistics <T>::Statistics () :bins_ (), counts_(0)
251  {}
252 
253  template < typename T >
254  std::ostream& Statistics<T>::print (std::ostream& os) const
255  {
256  const_iterator it;
257  for (it = begin(); it != end(); it++) {
258  it->print (os) << std::endl;
259  }
260  os << "Total number of observations: " << numberOfObservations ();
261  return os;
262  }
263 
264  template < typename T >
265  std::ostream& operator<< (std::ostream& os, const hpp::statistics::Statistics <T>& ss)
266  {
267  return ss.print (os);
268  }
269  } // namespace statistics
270 } // namespace hpp
271 
272 #endif // HPP_STATISTICS_BIN_HH
const_iterator begin() const
Definition: bin.hh:126
unsigned int numberOfBins() const
Return the number of bins.
Definition: bin.hh:112
const std::size_t & freq() const
Return the number of element in the bin.
Definition: bin.hh:39
Implementation.
double Proba_t
Definition: fwd.hh:22
virtual std::ostream & print(std::ostream &os) const
Put the results in a stream.
Definition: bin.hh:254
std::size_t numberOfObservations() const
Definition: bin.hh:106
Container::const_iterator const_iterator
Definition: bin.hh:90
Definition: bin.hh:85
std::ostream & operator<<(std::ostream &os, const hpp::statistics::Bin &b)
Definition: bin.hh:76
Container::iterator iterator
Definition: bin.hh:89
virtual std::ostream & print(std::ostream &os) const
Print the bin.
Definition: bin.hh:59
void clear()
Remove all element.
Definition: bin.hh:139
const_iterator end() const
Definition: bin.hh:133
virtual std::size_t freq(const T &bin) const
Definition: bin.hh:230
Bin()
Constructor.
Definition: bin.hh:69
std::list< T > Container
Definition: bin.hh:88
Definition: bin.hh:35