$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
min_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_MIN_H_HH
27 # define MLN_ACCU_STAT_MIN_H_HH
28 
32 
33 # include <mln/accu/internal/base.hh>
34 # include <mln/accu/histo.hh>
35 # include <mln/value/set.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 min_h;
46  }
47  }
48 
49 
50  // Traits.
51 
52  namespace trait
53  {
54 
55  template <typename V>
56  struct accumulator_< accu::stat::min_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 min_h : public Meta_Accumulator< min_h >
78  {
79  template <typename T>
80  struct with
81  {
83  };
84  };
85 
86  } // end of namespace mln::meta::stat
87 
88  } // end of namespace mln::meta
89 
90 
91  namespace stat
92  {
93 
98  //
99  template <typename V>
100  struct min_h : public mln::accu::internal::base< const V& , min_h<V> >
101  {
102  typedef V argument;
103 
104  min_h();
105 
108  void init();
109  void take(const argument& t);
110  void take_as_init_(const argument& t);
111  void take(const min_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_;
134  mutable bool valid_;
135  mutable unsigned i_; // the min index
136  mutable argument t_; // the min value
137 
138  // Auxiliary methods
139  void update_() const;
140  void go_minus_() const;
141  void go_plus_() const;
142  };
143 
144 
145 
146  template <typename I> struct min_h< util::pix<I> >;
147 
148 
149 # ifndef MLN_INCLUDE_ONLY
150 
151  template <typename V>
152  inline
153  min_h<V>::min_h()
154  : h_(),
155  s_(h_.vset())
156  {
157  init();
158  }
159 
160  template <typename V>
161  inline
162  void
163  min_h<V>::take(const argument& t)
164  {
165  if (h_.sum() == 0)
166  {
167  this->take_as_init_(t);
168  return;
169  }
170  h_.take(t);
171  if (t < t_)
172  {
173  ++sum_;
174  valid_ = false;
175  }
176  }
177 
178  template <typename V>
179  inline
180  void
181  min_h<V>::take(const min_h<V>& other)
182  {
183  // h_
184  h_.take(other.h_);
185  for (unsigned i = 0; i < i_; ++i)
186  sum_ += other.h_[i];
187  valid_ = false;
188  // FIXME: Optimize.
189  }
190 
191  template <typename V>
192  inline
193  void
194  min_h<V>::untake(const argument& t)
195  {
196  mln_precondition(h_(t) != 0);
197  h_.untake(t);
198  if (h_.sum() == 0)
199  {
200  init();
201  return;
202  }
203  if (t < t_)
204  {
205  mln_invariant(sum_ >= 1);
206  --sum_;
207  valid_ = false;
208  }
209  else
210  if (t == t_ && h_[i_] == 0)
211  valid_ = false;
212  }
213 
214  template <typename V>
215  inline
216  void
217  min_h<V>::update_() const
218  {
219  if (sum_ != 0)
220  go_minus_();
221  else
222  if (h_[i_] == 0)
223  go_plus_();
224  valid_ = true;
225  }
226 
227  template <typename V>
228  inline
229  void
230  min_h<V>::go_minus_() const
231  {
232  do
233  {
234  --i_;
235  if (h_[i_] != 0)
236  sum_ -= h_[i_];
237  }
238  while (sum_ != 0);
239  t_ = s_[i_];
240  }
241 
242  template <typename V>
243  inline
244  void
245  min_h<V>::go_plus_() const
246  {
247  do
248  ++i_;
249  while (h_[i_] == 0);
250  t_ = s_[i_];
251  }
252 
253  template <typename V>
254  inline
255  void
257  {
258  h_.init();
259  sum_ = 0;
260  i_ = mln_max(argument);
261  t_ = s_[i_];
262  valid_ = true;
263  }
264 
265  template <typename V>
266  inline
267  void
268  min_h<V>::take_as_init_(const argument& t)
269  {
270  h_.take(t);
271  sum_ = 0;
272  i_ = s_.index_of(t);
273  t_ = t;
274  valid_ = true;
275  }
276 
277  template <typename V>
278  inline
279  const typename min_h<V>::argument&
280  min_h<V>::to_result() const
281  {
282  if (! valid_)
283  update_();
284  return t_;
285  }
286 
287  template <typename V>
288  inline
289  const accu::histo<V>&
290  min_h<V>::histo() const
291  {
292  return h_;
293  }
294 
295  template <typename V>
296  inline
297  bool
298  min_h<V>::is_valid() const
299  {
300  return true;
301  }
302 
303 
304  template <typename V>
305  inline
306  void
307  min_h<V>::debug_print_() const
308  {
309  std::cout << "h={" << h_ << "} ";
310  std::cout << "sum=" << sum_ << ' '
311  << "valid=" << valid_ << ' '
312  << "i=" << i_ << ' '
313  << "t=" << t_ << std::endl;
314  }
315 
316  template <typename V>
317  inline
318  std::ostream& operator<<(std::ostream& ostr, const min_h<V>& m)
319  {
320  return ostr << m.to_result();
321  }
322 
323 # endif // ! MLN_INCLUDE_ONLY
324 
325 
326  } // end of namespace mln::accu::stat
327 
328  } // end of namespace mln::accu
329 
330 } // end of namespace mln
331 
332 #endif // ! MLN_ACCU_STAT_MIN_H_HH