Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE) 00002 // 00003 // This file is part of Olena. 00004 // 00005 // Olena is free software: you can redistribute it and/or modify it under 00006 // the terms of the GNU General Public License as published by the Free 00007 // Software Foundation, version 2 of the License. 00008 // 00009 // Olena is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // As a special exception, you may use this file as part of a free 00018 // software project without restriction. Specifically, if other files 00019 // instantiate templates or use macros or inline functions from this 00020 // file, or you compile this file and link it with other files to produce 00021 // an executable, this file does not by itself cause the resulting 00022 // executable to be covered by the GNU General Public License. This 00023 // exception does not however invalidate any other reasons why the 00024 // executable file might be covered by the GNU General Public License. 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 //operator T () const; 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 } // end of namespace mln::accu::meta 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 //FIXME: Not generic 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 //update return value 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 // if nb referents of occurrence i > nb referents of t_ 00178 if (v[i] > h_(t_)) 00179 t_ = h_.vset()[i]; // t_ <- current 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 } // end of namespace mln::accu 00213 00214 } // end of namespace mln 00215 00216 00217 #endif // ! MLN_ACCU_MAJ_H_HH