$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
max_h.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_MAX_H_HH
27 # define MLN_ACCU_STAT_MAX_H_HH
28 
32 
33 # include <mln/accu/internal/base.hh>
34 # include <mln/core/concept/meta_accumulator.hh>
35 # include <mln/accu/histo.hh>
36 # include <mln/util/pix.hh>
37 
38 
39 namespace mln
40 {
41 
42  // Forward declaration.
43  namespace accu {
44  namespace stat {
45  template <typename V> struct max_h;
46  }
47  }
48 
49 
50  // Traits.
51 
52  namespace trait
53  {
54 
55  template <typename V>
56  struct accumulator_< accu::stat::max_h<V> >
57  {
58  typedef accumulator::has_untake::yes has_untake;
59  typedef accumulator::has_set_value::no has_set_value;
60  typedef accumulator::has_stop::no has_stop;
61  typedef accumulator::when_pix::use_v when_pix;
62  };
63 
64  } // end of namespace mln::trait
65 
66 
67  namespace accu
68  {
69 
70  namespace meta
71  {
72 
73  namespace stat
74  {
75 
77  struct max_h : public Meta_Accumulator< max_h >
78  {
79  template <typename T>
80  struct with
81  {
83  };
84  };
85 
86  } // end of namespace mln::accu::meta::stat
87 
88  } // end of namespace mln::accu::meta
89 
90 
91  namespace stat
92  {
93 
98  //
99  template <typename V>
100  struct max_h : public mln::accu::internal::base< const V&, max_h<V> >
101  {
102  typedef V argument;
103 
104  max_h();
105 
108  void init();
109  void take(const argument& t);
110  void take_as_init_(const argument& t);
111  void take(const max_h<V>& other);
112  void untake(const argument& t);
114 
115  unsigned card() const { return h_.sum(); }
116 
118  const argument& to_result() const;
119 
120  const accu::histo<V>& histo() const;
121 
124  bool is_valid() const;
125 
126  void debug_print_() const;
127 
128  protected:
129 
131  const value::set<V>& s_; // derived from h_
132 
133  mutable unsigned sum_; // number of taken values > t_
134  mutable unsigned i_; // the current max index ('current' means 'last known')
135  mutable argument t_; // the current max argument
136 
137  mutable bool valid_; // validity of the current indent / argument
138  // when valid_ is false, an update of i_ and t_ is required
139 
140  // Dev note: we can have at the same time (sum_ == 0) and
141  // (valid_ == false) because of the 'untake' method.
142 
143  // Auxiliary methods
144  void update_() const;
145  void go_minus_() const;
146  void go_plus_() const;
147 
148  void invariant_() const;
149  };
150 
151 
152  template <typename I> struct max_h< util::pix<I> >;
153 
154 # ifndef MLN_INCLUDE_ONLY
155 
156  template <typename V>
157  inline
158  void
159  max_h<V>::invariant_() const
160  {
161  // valid_ => (sum_ == 0)
162  mln_invariant(! valid_ || (sum_ == 0));
163  }
164 
165  template <typename V>
166  inline
168  : h_(),
169  s_(h_.vset())
170  {
171  init();
172  invariant_();
173  }
174 
175  template <typename V>
176  inline
177  void
178  max_h<V>::take(const argument& t)
179  {
180  if (h_.sum() == 0)
181  {
182  this->take_as_init_(t);
183  return;
184  }
185  h_.take(t);
186  if (t > t_)
187  {
188  ++sum_;
189  valid_ = false;
190  }
191  invariant_();
192  }
193 
194  template <typename V>
195  inline
196  void
197  max_h<V>::take(const max_h<V>& other)
198  {
199  // h_
200  h_.take(other.h_);
201  for (unsigned i = this->card() - 1; i > i_; --i)
202  sum_ += other.h_[i];
203  if (valid_ && sum_ != 0)
204  valid_ = false;
205  // FIXME: Optimize.
206  invariant_();
207  }
208 
209  template <typename V>
210  inline
211  void
212  max_h<V>::untake(const argument& t)
213  {
214  mln_precondition(h_(t) != 0);
215  h_.untake(t);
216  if (h_.sum() == 0)
217  {
218  init();
219  return;
220  }
221  if (t > t_)
222  {
223  mln_invariant(sum_ >= 1);
224  --sum_;
225  valid_ = false;
226  }
227  else
228  if (t == t_ && h_[i_] == 0)
229  valid_ = false;
230  invariant_();
231  }
232 
233  template <typename V>
234  inline
235  void
236  max_h<V>::update_() const
237  {
238  if (sum_ != 0)
239  go_plus_();
240  else
241  if (h_[i_] == 0)
242  go_minus_();
243  valid_ = true;
244 
245  mln_postcondition(sum_ == 0);
246  mln_postcondition(h_[i_] != 0);
247  for (unsigned j = i_ + 1; j < h_.nvalues(); ++j)
248  mln_postcondition(h_[j] == 0);
249  }
250 
251  template <typename V>
252  inline
253  void
254  max_h<V>::go_plus_() const
255  {
256  do
257  {
258  ++i_;
259  if (h_[i_] != 0)
260  sum_ -= h_[i_];
261  }
262  while (sum_ != 0);
263  t_ = s_[i_];
264  }
265 
266  template <typename V>
267  inline
268  void
269  max_h<V>::go_minus_() const
270  {
271  do
272  --i_;
273  while (h_[i_] == 0);
274  t_ = s_[i_];
275  }
276 
277  template <typename V>
278  inline
279  void
281  {
282  h_.init();
283  sum_ = 0;
284  i_ = mln_min(argument);
285  t_ = s_[i_];
286  valid_ = true;
287  }
288 
289  template <typename V>
290  inline
291  void
292  max_h<V>::take_as_init_(const argument& t)
293  {
294  h_.take(t);
295  sum_ = 0;
296  i_ = s_.index_of(t);
297  t_ = t;
298  valid_ = true;
299  }
300 
301  template <typename V>
302  inline
303  const typename max_h<V>::argument&
304  max_h<V>::to_result() const
305  {
306  if (! valid_)
307  update_();
308  invariant_();
309  return t_;
310  }
311 
312  template <typename V>
313  inline
314  const accu::histo<V>&
315  max_h<V>::histo() const
316  {
317  return h_;
318  }
319 
320  template <typename V>
321  inline
322  bool
323  max_h<V>::is_valid() const
324  {
325  return true;
326  }
327 
328  template <typename V>
329  inline
330  void
331  max_h<V>::debug_print_() const
332  {
333  std::cout << "h={" << h_ << "} h.sum = " << h_.sum() << ' ';
334  std::cout << "sum=" << sum_ << ' '
335  << "valid=" << valid_ << ' '
336  << "i=" << i_ << ' '
337  << "t=" << t_ << std::endl;
338  }
339 
340  template <typename V>
341  inline
342  std::ostream& operator<<(std::ostream& ostr, const max_h<V>& m)
343  {
344  return ostr << m.to_result();
345  }
346 
347 # endif // ! MLN_INCLUDE_ONLY
348 
349  } // end of namespace mln::accu::stat
350 
351  } // end of namespace mln::accu
352 
353 } // end of namespace mln
354 
355 
356 #endif // ! MLN_ACCU_STAT_MAX_H_HH