00001 // Copyright (C) 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_MAX_SITE_HH 00027 # define MLN_ACCU_MAX_SITE_HH 00028 00035 00036 # include <mln/core/concept/meta_accumulator.hh> 00037 # include <mln/accu/internal/base.hh> 00038 # include <mln/util/pix.hh> 00039 00040 namespace mln 00041 { 00042 00043 namespace accu 00044 { 00045 00046 00051 // 00052 template <typename I> 00053 struct max_site : public mln::accu::internal::base< mln_psite(I), max_site<I> > 00054 { 00055 typedef mln::util::pix<I> argument; 00056 typedef mln_psite(I) result; 00057 00058 max_site(); 00059 00062 void init(); 00063 void take(const argument& t); 00064 void take(const max_site<I>& other); 00066 00068 mln_psite(I) to_result() const; 00069 operator mln_psite(I) () const; 00070 00072 mln_value(I) value_() const; 00073 00076 bool is_valid() const; 00077 00078 protected: 00079 bool is_valid_; 00080 mln_psite(I) max_p_; 00081 mln_value(I) max_v_; 00082 }; 00083 00084 00085 00086 namespace meta 00087 { 00088 00090 struct max_site : public Meta_Accumulator< max_site > 00091 { 00092 template <typename I> 00093 struct with 00094 { 00095 typedef accu::max_site<I> ret; 00096 }; 00097 }; 00098 00099 } // end of namespace mln::accu::meta 00100 00101 00102 # ifndef MLN_INCLUDE_ONLY 00103 00104 template <typename I> 00105 inline 00106 max_site<I>::max_site() 00107 { 00108 init(); 00109 } 00110 00111 template <typename I> 00112 inline 00113 void 00114 max_site<I>::init() 00115 { 00116 is_valid_ = false; 00117 max_v_ = mln_min(mln_value(I)); 00118 } 00119 00120 template <typename I> 00121 inline 00122 void max_site<I>::take(const argument& t) 00123 { 00124 if (t.v() > max_v_) 00125 { 00126 max_v_ = t.v(); 00127 max_p_ = t.p(); 00128 is_valid_ = true; 00129 } 00130 } 00131 00132 template <typename I> 00133 inline 00134 void 00135 max_site<I>::take(const max_site<I>& other) 00136 { 00137 mln_precondition(other.is_valid()); 00138 00139 if (other.value_() > max_v_) 00140 { 00141 max_v_ = other.value_(); 00142 max_p_ = other.to_result(); 00143 is_valid_ = true; 00144 } 00145 } 00146 00147 template <typename I> 00148 inline 00149 mln_psite(I) 00150 max_site<I>::to_result() const 00151 { 00152 mln_precondition(is_valid()); 00153 return max_p_; 00154 } 00155 00156 template <typename I> 00157 inline 00158 max_site<I>::operator mln_psite(I)() const 00159 { 00160 return to_result(); 00161 } 00162 00163 template <typename I> 00164 inline 00165 mln_value(I) 00166 max_site<I>::value_() const 00167 { 00168 return max_v_; 00169 } 00170 00171 template <typename I> 00172 inline 00173 bool 00174 max_site<I>::is_valid() const 00175 { 00176 return is_valid_; 00177 } 00178 00179 # endif // ! MLN_INCLUDE_ONLY 00180 00181 } // end of namespace mln::accu 00182 00183 } // end of namespace mln 00184 00185 00186 #endif // ! MLN_ACCU_MAX_SITE_HH