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