$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
median_alt.hh
1 // Copyright (C) 2007, 2008, 2009, 2013 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_ACCU_STAT_MEDIAN_ALT_HH
28 # define MLN_ACCU_STAT_MEDIAN_ALT_HH
29 
33 
34 # include <mln/accu/internal/base.hh>
35 # include <mln/accu/histo.hh>
36 
37 
38 namespace mln
39 {
40 
41  namespace accu
42  {
43 
44  namespace stat
45  {
46 
47 
52  //
53  template <typename S>
54  struct median_alt : public mln::accu::internal::base< const mln_value(S)&, median_alt<S> >
55  {
56  typedef mln_value(S) argument;
57 
58  median_alt(const Value_Set<S>& s);
59 
62  void take(const argument& t);
63  void untake(const argument& t);
64  void init();
66 
68  const argument& to_result() const;
69 
72  bool is_valid() const;
73 
74  protected:
75 
76  histo<S> h_;
78  const S& s_;
79 
80  unsigned sum_minus_, sum_plus_;
81 
83  unsigned i_;
85  argument t_;
86 
87  // Auxiliary methods
88  void go_minus_();
89  void go_plus_();
90  };
91 
92  template <typename T>
93  struct median_alt_on : public median_alt< value::set<T> >
94  {
96  : median_alt< value::set<T> >(value::set<T>::the())
97  {
98  }
99  };
100 
101  } // end of mln::accu::stat
102 
103 
104  namespace meta
105  {
106 
107  namespace stat
108  {
109 
111 
112  template <typename T>
113  struct median_alt : public Meta_Accumulator< median_alt<T> >
114  {
115  median_alt(const Value_Set<T>& s_) : s(s_) {}
116 
117  struct with
118  {
120  };
121 
123  };
124 
125  } // end of namespace mln::accu::meta::stat
126 
127  } // end of namespace mln::accu::meta
128 
129 
130  template <typename T>
132  {
133  stat::median_alt<T> a(m.s);
134  return a;
135  }
136 
137 
138 # ifndef MLN_INCLUDE_ONLY
139 
140  namespace stat
141  {
142 
143  template <typename S>
144  inline
145  median_alt<S>::median_alt(const Value_Set<S>& s)
146  : h_(s),
147  s_(h_.vset())
148  {
149  init();
150  }
151 
152 
153  template <typename S>
154  inline
155  void
156  median_alt<S>::take(const argument& t)
157  {
158  // update h_
159  h_.take(t);
160 
161  // particular case:
162  // current state was initialization
163  if (h_[i_] == 0)
164  {
165  i_ = s_.index_of(t);
166  t_ = t;
167  return;
168  }
169 
170  // particular case:
171  // the median does not change
172  if (t == t_)
173  {
174  return;
175  }
176 
177  // general case:
178 
179  if (t < t_)
180  {
181  ++sum_minus_;
182  if (2 * sum_minus_ > h_.sum())
183  go_minus_();
184  }
185  else
186  // t > t_
187  {
188  ++sum_plus_;
189  if (2 * sum_plus_ > h_.sum())
190  go_plus_();
191  }
192  }
193 
194 
195  template <typename S>
196  inline
197  void
198  median_alt<S>::untake(const argument& t)
199  {
200  mln_precondition(h_(t) != 0);
201 
202  // update h_
203  h_.untake(t);
204 
205  // particular case:
206  // the only value has been removed
207  if (h_.sum() == 0)
208  {
209  init();
210  return;
211  }
212 
213  // general case:
214  if (t < t_)
215  {
216  --sum_minus_;
217  if (2 * sum_plus_ > h_.sum())
218  go_plus_();
219  }
220  else if (t > t_)
221  {
222  --sum_plus_;
223  if (2 * sum_minus_ > h_.sum())
224  go_minus_();
225  }
226  else
227  // t == t_
228  {
229  if (h_[i_] == 0)
230  {
231  // go to the heaviest side
232  if (sum_plus_ > sum_minus_)
233  go_plus_();
234  else
235  go_minus_(); // default when both sides are balanced
236  }
237  else
238  {
239  if (2 * sum_plus_ > h_.sum())
240  go_plus_();
241  else if (2 * sum_minus_ > h_.sum())
242  go_minus_();
243  // else no change
244  }
245  }
246  }
247 
248  template <typename S>
249  inline
250  void
251  median_alt<S>::init()
252  {
253  h_.init();
254  sum_minus_ = 0;
255  sum_plus_ = 0;
256  i_ = (mln_max(argument) - mln_min(argument)) / 2;
257  t_ = s_[i_];
258  }
259 
260  template <typename S>
261  inline
262  const typename median_alt<S>::argument&
263  median_alt<S>::to_result() const
264  {
265  return t_;
266  }
267 
268  template <typename S>
269  inline
270  bool
271  median_alt<S>::is_valid() const
272  {
273  return true;
274  }
275 
276  template <typename S>
277  inline
278  void
279  median_alt<S>::go_minus_()
280  {
281  do
282  {
283  sum_plus_ += h_[i_];
284  do
285  --i_;
286  while (h_[i_] == 0);
287  sum_minus_ -= h_[i_];
288  }
289  while (2 * sum_minus_ > h_.sum());
290  t_ = s_[i_];
291  }
292 
293 
294  template <typename S>
295  inline
296  void
297  median_alt<S>::go_plus_()
298  {
299  do
300  {
301  sum_minus_ += h_[i_];
302  do
303  ++i_;
304  while (h_[i_] == 0);
305  sum_plus_ -= h_[i_];
306  }
307  while (2 * sum_plus_ > h_.sum());
308  t_ = s_[i_];
309  }
310 
311  template <typename S>
312  inline
313  std::ostream& operator<<(std::ostream& ostr, const median_alt<S>& m)
314  {
315  return ostr << m.to_result();
316  }
317 
318  } // end of namespace mln::accu::stat
319 
320 # endif // ! MLN_INCLUDE_ONLY
321 
322  } // end of namespace mln::accu
323 
324 } // end of namespace mln
325 
326 
327 #endif // ! MLN_ACCU_STAT_MEDIAN_ALT_HH