$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
rank_high_quant.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_RANK_HIGH_QUANT_HH
27 # define MLN_ACCU_STAT_RANK_HIGH_QUANT_HH
28 
32 
33 # include <vector>
34 # include <mln/accu/internal/base.hh>
35 # include <mln/core/concept/meta_accumulator.hh>
36 # include <mln/trait/value_.hh>
37 # include <mln/util/pix.hh>
38 
39 
40 namespace mln
41 {
42 
43  namespace accu
44  {
45 
46  namespace stat
47  {
48 
49 
51 
56  template <typename T>
57  struct rank_high_quant : public mln::accu::internal::base< const T&, rank_high_quant<T> >
58  {
59  typedef T argument;
60 
61  rank_high_quant(unsigned k, unsigned n);
62 
65  void init();
66  void take_as_init_(const argument& t);
67  void take(const argument& t);
68  void take(const rank_high_quant<T>& other);
69  void sort();
71 
73  const T& to_result() const;
74 
77  bool is_valid() const;
78 
79  protected:
80 
81  std::vector<T> elts_;
82  bool is_sorted_;
83  unsigned k_; // 0 <= k_ < n
84  unsigned n_;
85  };
86 
87 
88  template <typename I> struct rank_high_quant< util::pix<I> >;
89 
90 
91  } // end of mln::accu::stat
92 
93 
94  namespace meta
95  {
96 
97  namespace stat
98  {
99 
101 
102  struct rank_high_quant : public Meta_Accumulator< rank_high_quant >
103  {
104  rank_high_quant(unsigned k_, unsigned n_) : k(k_), n(n_) {}
105 
106  template <typename T>
107  struct with
108  {
110  };
111 
112  unsigned k;
113  unsigned n;
114  };
115 
116  } // end of namespace mln::accu::meta::stat
117 
118  } // end of namespace mln::accu::meta
119 
120 
121  template <typename T>
123  {
124  stat::rank_high_quant<T> a(m.k, m.n);
125  return a;
126  }
127 
128 
129  namespace stat
130  {
131 
132 # ifndef MLN_INCLUDE_ONLY
133 
134  template <typename T>
135  inline
136  rank_high_quant<T>::rank_high_quant(unsigned k, unsigned n)
137  : k_(k),
138  n_(n),
139  is_sorted_(false)
140  {
141  mln_assertion(k_ < n_);
142  init();
143  }
144 
145  template <typename T>
146  inline
147  void
148  rank_high_quant<T>::init()
149  {
150  elts_.clear();
151  }
152 
153  template <typename T>
154  inline
155  void rank_high_quant<T>::take_as_init_(const argument& t)
156  {
157  elts_.push_back(t);
158  is_sorted_ = false;
159  }
160 
161  template <typename T>
162  inline
163  void rank_high_quant<T>::take(const argument& t)
164  {
165  elts_.push_back(t);
166  is_sorted_ = false;
167  }
168 
169  template <typename T>
170  inline
171  void
172  rank_high_quant<T>::take(const rank_high_quant<T>& other)
173  {
174  elts_.insert(elts_.end(),
175  other.elts_.begin(),
176  other.elts_.end());
177  is_sorted_ = false;
178  }
179 
180  template <typename T>
181  inline
182  const T&
183  rank_high_quant<T>::to_result() const
184  {
185  const_cast<rank_high_quant<T>&>(*this).sort();
186 
187  if (n_ == elts_.size())
188  return elts_[k_];
189  else
190  // FIXME : This alternative is used to handle images edges.
191  return elts_[(elts_.size() * k_) / n_];
192  }
193 
194  template <typename T>
195  inline
196  bool
197  rank_high_quant<T>::is_valid() const
198  {
199  return true;
200  }
201 
202  template <typename T>
203  inline
204  void
205  rank_high_quant<T>::sort()
206  {
207  if (! is_sorted_)
208  {
209  is_sorted_ = true;
210  std::sort(elts_.begin(), elts_.end());
211  }
212  }
213 
214 # endif // ! MLN_INCLUDE_ONLY
215 
216  } // end of namespace mln::accu::stat
217 
218  } // end of namespace mln::accu
219 
220 } // end of namespace mln
221 
222 // #include <mln/accu/stat/rank_bool.hh> ??
223 
224 #endif // ! MLN_ACCU_STAT_RANK_HIGH_QUANT_HH