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_MATH_SUM_HH
00027 # define MLN_ACCU_MATH_SUM_HH
00028
00032
00033 # include <mln/core/concept/meta_accumulator.hh>
00034 # include <mln/accu/internal/base.hh>
00035
00036 # include <mln/util/pix.hh>
00037
00038 # include <mln/trait/value_.hh>
00039 # include <mln/value/builtin/all.hh>
00040 # include <mln/literal/zero.hh>
00041
00042
00043 namespace mln
00044 {
00045
00046 namespace accu
00047 {
00048
00049 namespace math
00050 {
00051
00052
00053 template <typename T, typename S>
00054 struct sum;
00055
00056 }
00057
00058 namespace meta
00059 {
00060
00061 namespace math
00062 {
00063
00065 struct sum : public Meta_Accumulator< sum >
00066 {
00067 template <typename T, typename S = mln_sum(T)>
00068 struct with
00069 {
00070 typedef accu::math::sum<T, S> ret;
00071 };
00072 };
00073
00074 }
00075
00076 }
00077
00078 }
00079
00080
00081 namespace trait
00082 {
00083
00084 template <typename T, typename S>
00085 struct accumulator_< accu::math::sum<T,S> >
00086 {
00087 typedef accumulator::has_untake::yes has_untake;
00088 typedef accumulator::has_set_value::yes has_set_value;
00089 typedef accumulator::has_stop::no has_stop;
00090 typedef accumulator::when_pix::not_ok when_pix;
00091 };
00092
00093 }
00094
00095
00096 namespace accu
00097 {
00098
00099 namespace math
00100 {
00101
00109
00110 template <typename T, typename S = mln_sum(T)>
00111 struct sum : public mln::accu::internal::base< const S&, sum<T,S> >
00112 {
00113 typedef T argument;
00114
00115 sum();
00116
00119 void init();
00120 void take(const argument& t);
00121 void take_as_init_(const argument& t);
00122 void take(const sum<T,S>& other);
00123
00124 void untake(const argument& t);
00125 void untake(const sum<T,S>& other);
00126 void set_value(const S& s);
00128
00130 const S& to_result() const;
00131
00134 bool is_valid() const;
00135
00136 protected:
00137
00138 S s_;
00139 };
00140
00141
00142 template <typename I, typename S>
00143 struct sum< util::pix<I>, S >;
00144
00145
00146 # ifndef MLN_INCLUDE_ONLY
00147
00148
00149
00150 template <typename T, typename S>
00151 inline
00152 sum<T,S>::sum()
00153 {
00154 init();
00155 }
00156
00157 template <typename T, typename S>
00158 inline
00159 void
00160 sum<T,S>::init()
00161 {
00162 s_ = literal::zero;
00163 }
00164
00165 template <typename T, typename S>
00166 inline
00167 void sum<T,S>::take(const argument& t)
00168 {
00169 s_ += static_cast<S>(t);
00170 }
00171
00172 template <typename T, typename S>
00173 inline
00174 void sum<T,S>::untake(const argument& t)
00175 {
00176 s_ -= static_cast<S>(t);
00177 }
00178
00179 template <typename T, typename S>
00180 inline
00181 void sum<T,S>::take_as_init_(const argument& t)
00182 {
00183 s_ = static_cast<S>(t);
00184 }
00185
00186 template <typename T, typename S>
00187 inline
00188 void
00189 sum<T,S>::take(const sum<T,S>& other)
00190 {
00191 s_ += other.s_;
00192 }
00193
00194 template <typename T, typename S>
00195 inline
00196 void
00197 sum<T,S>::untake(const sum<T,S>& other)
00198 {
00199 s_ -= other.s_;
00200 }
00201
00202 template <typename T, typename S>
00203 inline
00204 const S&
00205 sum<T,S>::to_result() const
00206 {
00207 return s_;
00208 }
00209
00210 template <typename T, typename S>
00211 inline
00212 void
00213 sum<T,S>::set_value(const S& s)
00214 {
00215 s_ = s;
00216 }
00217
00218 template <typename T, typename S>
00219 inline
00220 bool
00221 sum<T,S>::is_valid() const
00222 {
00223 return true;
00224 }
00225
00226 # endif // ! MLN_INCLUDE_ONLY
00227
00228 }
00229
00230 }
00231
00232 }
00233
00234
00235 #endif // ! MLN_ACCU_MATH_SUM_HH