00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef MLN_ACCU_STAT_RANK_HIGH_QUANT_HH
00027 # define MLN_ACCU_STAT_RANK_HIGH_QUANT_HH
00028
00032
00033 # include <vector>
00034 # include <mln/accu/internal/base.hh>
00035 # include <mln/core/concept/meta_accumulator.hh>
00036 # include <mln/trait/value_.hh>
00037 # include <mln/util/pix.hh>
00038
00039
00040 namespace mln
00041 {
00042
00043 namespace accu
00044 {
00045
00046 namespace stat
00047 {
00048
00049
00051
00056 template <typename T>
00057 struct rank_high_quant : public mln::accu::internal::base< const T&, rank_high_quant<T> >
00058 {
00059 typedef T argument;
00060
00061 rank_high_quant(unsigned k, unsigned n);
00062
00065 void init();
00066 void take_as_init_(const argument& t);
00067 void take(const argument& t);
00068 void take(const rank_high_quant<T>& other);
00069 void sort();
00071
00073 const T& to_result() const;
00074
00077 bool is_valid() const;
00078
00079 protected:
00080
00081 std::vector<T> elts_;
00082 bool is_sorted_;
00083 unsigned k_;
00084 unsigned n_;
00085 };
00086
00087
00088 template <typename I> struct rank_high_quant< util::pix<I> >;
00089
00090
00091 }
00092
00093
00094 namespace meta
00095 {
00096
00097 namespace stat
00098 {
00099
00101
00102 struct rank_high_quant : public Meta_Accumulator< rank_high_quant >
00103 {
00104 rank_high_quant(unsigned k_, unsigned n_) : k(k_), n(n_) {}
00105
00106 template <typename T>
00107 struct with
00108 {
00109 typedef accu::stat::rank_high_quant<T> ret;
00110 };
00111
00112 unsigned k;
00113 unsigned n;
00114 };
00115
00116 }
00117
00118 }
00119
00120
00121 template <typename T>
00122 stat::rank_high_quant<T> unmeta(const meta::stat::rank_high_quant& m, T)
00123 {
00124 stat::rank_high_quant<T> a(m.k, m.n);
00125 return a;
00126 }
00127
00128
00129 namespace stat
00130 {
00131
00132 # ifndef MLN_INCLUDE_ONLY
00133
00134 template <typename T>
00135 inline
00136 rank_high_quant<T>::rank_high_quant(unsigned k, unsigned n)
00137 : k_(k),
00138 n_(n),
00139 is_sorted_(false)
00140 {
00141 mln_assertion(k_ < n_);
00142 init();
00143 }
00144
00145 template <typename T>
00146 inline
00147 void
00148 rank_high_quant<T>::init()
00149 {
00150 elts_.clear();
00151 }
00152
00153 template <typename T>
00154 inline
00155 void rank_high_quant<T>::take_as_init_(const argument& t)
00156 {
00157 elts_.push_back(t);
00158 is_sorted_ = false;
00159 }
00160
00161 template <typename T>
00162 inline
00163 void rank_high_quant<T>::take(const argument& t)
00164 {
00165 elts_.push_back(t);
00166 is_sorted_ = false;
00167 }
00168
00169 template <typename T>
00170 inline
00171 void
00172 rank_high_quant<T>::take(const rank_high_quant<T>& other)
00173 {
00174 elts_.insert(elts_.end(),
00175 other.elts_.begin(),
00176 other.elts_.end());
00177 is_sorted_ = false;
00178 }
00179
00180 template <typename T>
00181 inline
00182 const T&
00183 rank_high_quant<T>::to_result() const
00184 {
00185 const_cast<rank_high_quant<T>&>(*this).sort();
00186
00187 if (n_ == elts_.size())
00188 return elts_[k_];
00189 else
00190
00191 return elts_[(elts_.size() * k_) / n_];
00192 }
00193
00194 template <typename T>
00195 inline
00196 bool
00197 rank_high_quant<T>::is_valid() const
00198 {
00199 return true;
00200 }
00201
00202 template <typename T>
00203 inline
00204 void
00205 rank_high_quant<T>::sort()
00206 {
00207 if (! is_sorted_)
00208 {
00209 is_sorted_ = true;
00210 std::sort(elts_.begin(), elts_.end());
00211 }
00212 }
00213
00214 # endif // ! MLN_INCLUDE_ONLY
00215
00216 }
00217
00218 }
00219
00220 }
00221
00222
00223
00224 #endif // ! MLN_ACCU_STAT_RANK_HIGH_QUANT_HH