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> |
38 | 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> |
47 | void operator ()(Args const &args) |
48 | { |
49 | // what about overflow? |
50 | this->weighted_sum_ += args[parameter::keyword<Tag>::get()] * args[weight]; |
51 | } |
52 | |
53 | result_type result(dont_care) const |
54 | { |
55 | return this->weighted_sum_; |
56 | } |
57 | |
58 | // make this accumulator serializeable |
59 | template<class Archive> |
60 | void serialize(Archive & ar, const unsigned int file_version) |
61 | { |
62 | ar & weighted_sum_; |
63 | } |
64 | |
65 | private: |
66 | |
67 | weighted_sample weighted_sum_; |
68 | }; |
69 | |
70 | } // namespace impl |
71 | |
72 | /////////////////////////////////////////////////////////////////////////////// |
73 | // tag::weighted_sum |
74 | // |
75 | namespace tag |
76 | { |
77 | struct weighted_sum |
78 | : depends_on<> |
79 | { |
80 | /// INTERNAL ONLY |
81 | /// |
82 | typedef accumulators::impl::weighted_sum_impl<mpl::_1, mpl::_2, tag::sample> impl; |
83 | }; |
84 | |
85 | template<typename VariateType, typename VariateTag> |
86 | struct weighted_sum_of_variates |
87 | : depends_on<> |
88 | { |
89 | /// INTERNAL ONLY |
90 | /// |
91 | typedef accumulators::impl::weighted_sum_impl<VariateType, mpl::_2, VariateTag> impl; |
92 | }; |
93 | |
94 | struct abstract_weighted_sum_of_variates |
95 | : depends_on<> |
96 | { |
97 | }; |
98 | } |
99 | |
100 | /////////////////////////////////////////////////////////////////////////////// |
101 | // extract::weighted_sum |
102 | // |
103 | namespace extract |
104 | { |
105 | extractor<tag::weighted_sum> const = {}; |
106 | extractor<tag::abstract_weighted_sum_of_variates> const = {}; |
107 | |
108 | BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum) |
109 | BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum_of_variates) |
110 | } |
111 | |
112 | using extract::weighted_sum; |
113 | using extract::weighted_sum_of_variates; |
114 | |
115 | template<typename VariateType, typename VariateTag> |
116 | struct feature_of<tag::weighted_sum_of_variates<VariateType, VariateTag> > |
117 | : feature_of<tag::abstract_weighted_sum_of_variates> |
118 | { |
119 | }; |
120 | |
121 | }} // namespace boost::accumulators |
122 | |
123 | #endif |
124 | |