$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
accu/stat/mean.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_ACCU_STAT_MEAN_HH
27 # define MLN_ACCU_STAT_MEAN_HH
28 
34 
35 # include <mln/accu/internal/base.hh>
36 # include <mln/accu/math/count.hh>
37 # include <mln/accu/math/sum.hh>
38 
39 
40 namespace mln
41 {
42 
43  namespace accu
44  {
45 
46  namespace stat
47  {
48 
49  // Forward declaration.
50  template <typename T, typename S, typename M>
51  struct mean;
52 
53  } // end of namespace mln::accu::stat
54 
55 
56  // Meta.
57 
58  namespace meta
59  {
60 
61  namespace stat
62  {
63 
65  struct mean : public Meta_Accumulator< mean >
66  {
67  template < typename T,
68  typename S = mln_sum(T),
69  typename M = S >
70  struct with
71  {
73  };
74  };
75 
76  } // end of namespace mln::accu::meta::stat
77 
78  } // end of namespace mln::accu::meta
79 
80  } // end of namespace mln::accu
81 
82 
83  // Traits.
84 
85  namespace trait
86  {
87 
88  template <typename T, typename S, typename M>
89  struct accumulator_< accu::stat::mean<T, S, M> >
90  {
91  typedef accumulator::has_untake::yes has_untake;
92  typedef accumulator::has_set_value::no has_set_value;
93  typedef accumulator::has_stop::no has_stop;
94  typedef accumulator::when_pix::use_v when_pix;
95  };
96 
97  } // end of namespace mln::trait
98 
99 
100  namespace accu
101  {
102 
103  namespace stat
104  {
105 
107 
116  template <typename T,
117  typename S = mln_sum(T),
118  typename M = S>
119  struct mean : public mln::accu::internal::base< M , mean<T,S,M> >
120  {
121  typedef T argument;
122  typedef M result;
123 
124  mean();
125 
128  void init();
129  void take(const argument& t);
130  void take(const mean<T,S,M>& other);
131  void untake(const argument& t);
132  void untake(const mean<T,S,M>& other);
134 
136  M to_result() const;
137  operator M () const;
138 
141  bool is_valid() const;
142 
144  mln_result(accu::math::count<T>) count() const;
145 
147  mln_result(accu::math::sum<T>) sum() const;
148 
149  protected:
150 
151  accu::math::count<T> count_;
152  accu::math::sum<T,S> sum_;
153  };
154 
155 
156 
157  template <typename I, typename S, typename M>
158  struct mean< util::pix<I>, S,M >;
159 
160 
161 # ifndef MLN_INCLUDE_ONLY
162 
163  template <typename T, typename S, typename M>
164  inline
166  {
167  init();
168  }
169 
170  template <typename T, typename S, typename M>
171  inline
172  void
174  {
175  count_.init();
176  sum_.init();
177  }
178 
179  template <typename T, typename S, typename M>
180  inline
181  void mean<T,S,M>::take(const argument& t)
182  {
183  count_.take(t);
184  sum_.take(t);
185  }
186 
187  template <typename T, typename S, typename M>
188  inline
189  void
190  mean<T,S,M>::take(const mean<T,S,M>& other)
191  {
192  count_.take(other.count_);
193  sum_.take(other.sum_);
194  }
195 
196  template <typename T, typename S, typename M>
197  inline
198  void mean<T,S,M>::untake(const argument& t)
199  {
200  count_.untake(t);
201  sum_.untake(t);
202  }
203 
204  template <typename T, typename S, typename M>
205  inline
206  void
207  mean<T,S,M>::untake(const mean<T,S,M>& other)
208  {
209  count_.untake(other.count_);
210  sum_.untake(other.sum_);
211  }
212 
213  template <typename T, typename S, typename M>
214  inline
215  M
216  mean<T,S,M>::to_result() const
217  {
218  unsigned n = count_.to_result();
219  if (n == 0u)
220  return M(); // Safety.
221  return static_cast<M>(sum_.to_result() / n);
222  }
223 
224  template <typename T, typename S, typename M>
225  inline
226  mean<T,S,M>::operator M() const
227  {
228  return to_result();
229  }
230 
231  template <typename T, typename S, typename M>
232  inline
233  bool
234  mean<T,S,M>::is_valid() const
235  {
236  return count_.to_result() != 0;
237  }
238 
239  template <typename T, typename S, typename M>
240  inline
241  mln_result(accu::math::count<T>)
242  mean<T,S,M>::count() const
243  {
244  return count_.to_result();
245  }
246 
247 
248  template <typename T, typename S, typename M>
249  inline
250  mln_result(accu::math::sum<T>)
251  mean<T,S,M>::sum() const
252  {
253  return sum_.to_result();
254  }
255 
256 # endif // ! MLN_INCLUDE_ONLY
257 
258  } // end of namespace mln::accu::stat
259 
260  } // end of namespace mln::accu
261 
262 } // end of namespace mln
263 
264 #endif // ! MLN_ACCU_STAT_MEAN_HH