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_MATH_COUNT_HH 00027 # define MLN_ACCU_MATH_COUNT_HH 00028 00032 00033 # include <mln/accu/internal/base.hh> 00034 # include <mln/core/concept/meta_accumulator.hh> 00035 00036 00037 namespace mln 00038 { 00039 00040 namespace accu 00041 { 00042 00043 namespace math 00044 { 00045 00046 // Forward declaration. 00047 template <typename T> 00048 struct count; 00049 00050 } // end of namespace mln::accu::math 00051 00052 00053 namespace meta 00054 { 00055 00056 namespace math 00057 { 00058 00060 struct count : public Meta_Accumulator< count > 00061 { 00062 template <typename T> 00063 struct with 00064 { 00065 typedef accu::math::count<T> ret; 00066 }; 00067 }; 00068 00069 } // end of namespace mln::accu::meta::math 00070 00071 } // end of namespace mln::accu::meta 00072 00073 } // end of namespace mln::accu 00074 00075 // Traits. 00076 namespace trait 00077 { 00078 00079 template <typename T> 00080 struct accumulator_< accu::math::count<T> > 00081 { 00082 typedef accumulator::has_untake::yes has_untake; 00083 typedef accumulator::has_set_value::yes has_set_value; 00084 typedef accumulator::has_stop::no has_stop; 00085 typedef accumulator::when_pix::use_pix when_pix; 00086 }; 00087 00088 } // end of namespace mln::trait 00089 00090 00091 namespace accu 00092 { 00093 00094 namespace math 00095 { 00096 00101 // 00102 template <typename T> 00103 struct count : public mln::accu::internal::base< unsigned , count<T> > 00104 { 00105 typedef T argument; 00106 00107 count(); 00108 00111 void init(); 00112 void take(const argument&); 00113 void take(const count<T>& other); 00114 00115 void untake(const argument&); 00116 void untake(const count<T>& other); 00117 00119 void set_value(unsigned c); 00121 00123 unsigned to_result() const; 00124 00127 bool is_valid() const; 00128 00129 protected: 00131 unsigned count_; 00132 }; 00133 00134 00135 # ifndef MLN_INCLUDE_ONLY 00136 00137 00138 00139 template <typename T> 00140 inline 00141 count<T>::count() 00142 { 00143 init(); 00144 } 00145 00146 template <typename T> 00147 inline 00148 void 00149 count<T>::init() 00150 { 00151 count_ = 0; 00152 } 00153 00154 template <typename T> 00155 inline 00156 void 00157 count<T>::take(const argument&) 00158 { 00159 ++count_; 00160 } 00161 00162 template <typename T> 00163 inline 00164 void 00165 count<T>::untake(const argument&) 00166 { 00167 mln_precondition(count_ > 0); 00168 --count_; 00169 } 00170 00171 template <typename T> 00172 inline 00173 void 00174 count<T>::take(const count<T>& other) 00175 { 00176 count_ += other.count_; 00177 } 00178 00179 template <typename T> 00180 inline 00181 void 00182 count<T>::untake(const count<T>& other) 00183 { 00184 mln_precondition(other.count_ <= count_); 00185 count_ -= other.count_; 00186 } 00187 00188 template <typename T> 00189 inline 00190 unsigned 00191 count<T>::to_result() const 00192 { 00193 return count_; 00194 } 00195 00196 template <typename T> 00197 inline 00198 void 00199 count<T>::set_value(unsigned c) 00200 { 00201 count_ = c; 00202 } 00203 00204 template <typename T> 00205 inline 00206 bool 00207 count<T>::is_valid() const 00208 { 00209 return true; 00210 } 00211 00212 # endif // ! MLN_INCLUDE_ONLY 00213 00214 } // end of namespace mln::accu::math 00215 00216 } // end of namespace mln::accu 00217 00218 } // end of namespace mln 00219 00220 00221 #endif // ! MLN_ACCU_MATH_COUNT_HH