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_CONVOLVE_HH
00027 # define MLN_ACCU_CONVOLVE_HH
00028 
00032 
00033 # include <mln/accu/internal/base.hh>
00034 # include <mln/value/ops.hh>
00035 # include <mln/literal/zero.hh>
00036 
00037 
00038 namespace mln
00039 {
00040 
00041   namespace accu
00042   {
00043 
00044 
00051     
00052     template <typename T1, typename T2,
00053               typename R = mln_sum_product(T1, T2)>
00054     struct convolve : public mln::accu::internal::base< R, convolve<T1,T2,R> >,
00055                       private metal::converts_to< mln_sum_product(T1, T2),
00056                                                   R >::check_t
00057     {
00058       typedef std::pair<T1,T2> argument;
00059 
00060       convolve();
00061 
00064       void init();
00065       void take(const argument& t);
00066       void take(const T1& t1, const T2& t2);
00067       void take(const convolve<T1,T2,R>& other);
00069 
00071       R to_result() const;
00072 
00075       bool is_valid() const;
00076 
00077     protected:
00078 
00079       typedef mln_sum_product(T1, T2) S;
00080       S s_;
00081     };
00082 
00083 
00084 # ifndef MLN_INCLUDE_ONLY
00085 
00086     template <typename T1, typename T2, typename R>
00087     inline
00088     convolve<T1,T2,R>::convolve()
00089     {
00090       init();
00091     }
00092 
00093     template <typename T1, typename T2, typename R>
00094     inline
00095     void
00096     convolve<T1,T2,R>::init()
00097     {
00098       static S zero = literal::zero;
00099       s_ = zero;
00100     }
00101 
00102     template <typename T1, typename T2, typename R>
00103     inline
00104     void
00105     convolve<T1,T2,R>::take(const argument& t)
00106     {
00107       s_ = static_cast<S>(s_ + static_cast<S>(t.first) * static_cast<S>(t.second));
00108     }
00109 
00110     template <typename T1, typename T2, typename R>
00111     inline
00112     void
00113     convolve<T1,T2,R>::take(const T1& t1, const T2& t2)
00114     {
00115       s_ = static_cast<S>(s_ + static_cast<S>(t1) * static_cast<S>(t2));
00116     }
00117 
00118     template <typename T1, typename T2, typename R>
00119     inline
00120     void
00121     convolve<T1,T2,R>::take(const convolve<T1,T2,R>& other)
00122     {
00123       s_ = static_cast<S>(s_ + other.s_);
00124     }
00125 
00126     template <typename T1, typename T2, typename R>
00127     inline
00128     R
00129     convolve<T1,T2,R>::to_result() const
00130     {
00131       return s_;
00132     }
00133 
00134     template <typename T1, typename T2, typename R>
00135     inline
00136     bool
00137     convolve<T1,T2,R>::is_valid() const
00138     {
00139       return true;
00140     }
00141 
00142 # endif // ! MLN_INCLUDE_ONLY
00143 
00144   } 
00145 
00146 } 
00147 
00148 
00149 #endif // ! MLN_ACCU_CONVOLVE_HH