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_MAJ_H_HH
00027 # define MLN_ACCU_MAJ_H_HH
00028
00032
00033
00034 # include <mln/core/concept/meta_accumulator.hh>
00035 # include <mln/accu/internal/base.hh>
00036 # include <mln/trait/value_.hh>
00037 # include <mln/util/pix.hh>
00038 # include <mln/accu/histo.hh>
00039 # include <vector>
00040
00041
00042 namespace mln
00043 {
00044
00045 namespace accu
00046 {
00047
00048
00050
00056 template <typename T>
00057 struct maj_h : public mln::accu::internal::base< const T& , maj_h<T> >
00058 {
00059 typedef T argument;
00060
00061 maj_h();
00062
00065 void init();
00066 void take(const argument& t);
00067 void untake(const argument& t);
00068 void take(const maj_h<T>& other);
00070
00072 const T& to_result() const;
00073
00074
00075
00078 bool is_valid() const;
00079
00080 void update_() const;
00081
00082 mutable bool valid_;
00083
00084 const accu::histo<T>& histo() const;
00085
00086 protected:
00087
00088 mutable argument t_;
00089 mutable accu::histo<T> h_;
00090 };
00091
00092
00093 template <typename I> struct maj_h< util::pix<I> >;
00094
00095
00096 namespace meta
00097 {
00098
00100
00101 struct maj_h : public Meta_Accumulator< maj_h >
00102 {
00103 template <typename T>
00104 struct with
00105 {
00106 typedef accu::maj_h<T> ret;
00107 };
00108 };
00109
00110 }
00111
00112
00113
00114 # ifndef MLN_INCLUDE_ONLY
00115
00116 template <typename T>
00117 inline
00118 maj_h<T>::maj_h()
00119 {
00120 init();
00121 valid_ = true;
00122
00123 t_ = literal::zero;
00124 }
00125
00126 template <typename T>
00127 inline
00128 void
00129 maj_h<T>::init()
00130 {
00131 h_.init();
00132 }
00133
00134 template <typename T>
00135 inline
00136 void
00137 maj_h<T>::take(const argument& t)
00138 {
00139 h_.take(t);
00140
00141
00142 if (h_(t) > h_(t_))
00143 t_ = t;
00144 }
00145
00146 template <typename T>
00147 inline
00148 void
00149 maj_h<T>::untake(const argument& t)
00150 {
00151 h_.untake(t);
00152
00153 if (valid_)
00154 valid_ = false;
00155 }
00156
00157 template <typename T>
00158 inline
00159 void
00160 maj_h<T>::take(const maj_h<T>& other)
00161 {
00162 h_.take(other.h_);
00163
00164 if (valid_)
00165 valid_ = false;
00166 }
00167
00168 template <typename T>
00169 inline
00170 void
00171 maj_h<T>::update_() const
00172 {
00173 const std::vector<unsigned>& v = h_.vect();
00174
00175 for(unsigned i = 0; i != v.size(); i++)
00176 {
00177
00178 if (v[i] > h_(t_))
00179 t_ = h_.vset()[i];
00180 }
00181 valid_ = true;
00182 }
00183
00184 template <typename T>
00185 inline
00186 const T&
00187 maj_h<T>::to_result() const
00188 {
00189 if (not valid_)
00190 update_();
00191 return t_;
00192 }
00193
00194 template <typename T>
00195 inline
00196 bool
00197 maj_h<T>::is_valid() const
00198 {
00199 return true;
00200 }
00201
00202 template <typename V>
00203 inline
00204 const accu::histo<V>&
00205 maj_h<V>::histo() const
00206 {
00207 return h_;
00208 }
00209
00210 # endif // ! MLN_INCLUDE_ONLY
00211
00212 }
00213
00214 }
00215
00216
00217 #endif // ! MLN_ACCU_MAJ_H_HH