1 ///////////////////////////////////////////////////////////////////////////////
2 // weighted_sum.hpp
3 //
4 //  Copyright 2006 Eric Niebler, Olivier Gygi. Distributed under the Boost
5 //  Software License, Version 1.0. (See accompanying file
6 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SUM_HPP_EAN_28_10_2005
9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SUM_HPP_EAN_28_10_2005
10 
11 #include <boost/mpl/placeholders.hpp>
12 #include <boost/accumulators/framework/accumulator_base.hpp>
13 #include <boost/accumulators/framework/extractor.hpp>
14 #include <boost/accumulators/numeric/functional.hpp>
15 #include <boost/accumulators/framework/parameters/sample.hpp>
16 #include <boost/accumulators/framework/parameters/weight.hpp>
17 #include <boost/accumulators/framework/accumulators/external_accumulator.hpp>
18 #include <boost/accumulators/framework/depends_on.hpp>
19 #include <boost/accumulators/statistics_fwd.hpp>
20 
21 namespace boost { namespace accumulators
22 {
23 
24 namespace impl
25 {
26     ///////////////////////////////////////////////////////////////////////////////
27     // weighted_sum_impl
28     template<typename Sample, typename Weight, typename Tag>
29     struct weighted_sum_impl
30       : accumulator_base
31     {
32         typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
33 
34         // for boost::result_of
35         typedef weighted_sample result_type;
36 
37         template<typename Args>
weighted_sum_implboost::accumulators::impl::weighted_sum_impl38         weighted_sum_impl(Args const &args)
39           : weighted_sum_(
40                 args[parameter::keyword<Tag>::get() | Sample()]
41                   * numeric::one<Weight>::value
42             )
43         {
44         }
45 
46         template<typename Args>
operator ()boost::accumulators::impl::weighted_sum_impl47         void operator ()(Args const &args)
48         {
49             // what about overflow?
50             this->weighted_sum_ += args[parameter::keyword<Tag>::get()] * args[weight];
51         }
52 
resultboost::accumulators::impl::weighted_sum_impl53         result_type result(dont_care) const
54         {
55             return this->weighted_sum_;
56         }
57 
58     private:
59 
60         weighted_sample weighted_sum_;
61     };
62 
63 } // namespace impl
64 
65 ///////////////////////////////////////////////////////////////////////////////
66 // tag::weighted_sum
67 //
68 namespace tag
69 {
70     struct weighted_sum
71       : depends_on<>
72     {
73         /// INTERNAL ONLY
74         ///
75         typedef accumulators::impl::weighted_sum_impl<mpl::_1, mpl::_2, tag::sample> impl;
76     };
77 
78     template<typename VariateType, typename VariateTag>
79     struct weighted_sum_of_variates
80       : depends_on<>
81     {
82         /// INTERNAL ONLY
83         ///
84         typedef accumulators::impl::weighted_sum_impl<VariateType, mpl::_2, VariateTag> impl;
85     };
86 
87     struct abstract_weighted_sum_of_variates
88       : depends_on<>
89     {
90     };
91 }
92 
93 ///////////////////////////////////////////////////////////////////////////////
94 // extract::weighted_sum
95 //
96 namespace extract
97 {
98     extractor<tag::weighted_sum> const weighted_sum = {};
99     extractor<tag::abstract_weighted_sum_of_variates> const weighted_sum_of_variates = {};
100 
101     BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum)
102     BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum_of_variates)
103 }
104 
105 using extract::weighted_sum;
106 using extract::weighted_sum_of_variates;
107 
108 template<typename VariateType, typename VariateTag>
109 struct feature_of<tag::weighted_sum_of_variates<VariateType, VariateTag> >
110   : feature_of<tag::abstract_weighted_sum_of_variates>
111 {
112 };
113 
114 }} // namespace boost::accumulators
115 
116 #endif
117