1 ///////////////////////////////////////////////////////////////////////////////
4 // Copyright 2005 Eric Niebler. 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)
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_MEAN_HPP_EAN_28_10_2005
9 #define BOOST_ACCUMULATORS_STATISTICS_MEAN_HPP_EAN_28_10_2005
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/depends_on.hpp>
17 #include <boost/accumulators/statistics_fwd.hpp>
18 #include <boost/accumulators/statistics/count.hpp>
19 #include <boost/accumulators/statistics/sum.hpp>
21 namespace boost
{ namespace accumulators
26 ///////////////////////////////////////////////////////////////////////////////
29 template<typename Sample
, typename SumFeature
>
33 // for boost::result_of
34 typedef typename
numeric::functional::average
<Sample
, std::size_t>::result_type result_type
;
36 mean_impl(dont_care
) {}
38 template<typename Args
>
39 result_type
result(Args
const &args
) const
41 extractor
<SumFeature
> sum
;
42 return numeric::average(sum(args
), count(args
));
46 template<typename Sample
, typename Tag
>
47 struct immediate_mean_impl
50 // for boost::result_of
51 typedef typename
numeric::functional::average
<Sample
, std::size_t>::result_type result_type
;
53 template<typename Args
>
54 immediate_mean_impl(Args
const &args
)
55 : mean(numeric::average(args
[sample
| Sample()], numeric::one
<std::size_t>::value
))
59 template<typename Args
>
60 void operator ()(Args
const &args
)
62 std::size_t cnt
= count(args
);
63 this->mean
= numeric::average(
64 (this->mean
* (cnt
- 1)) + args
[parameter::keyword
<Tag
>::get()]
69 result_type
result(dont_care
) const
80 ///////////////////////////////////////////////////////////////////////////////
82 // tag::immediate_mean
83 // tag::mean_of_weights
84 // tag::immediate_mean_of_weights
85 // tag::mean_of_variates
86 // tag::immediate_mean_of_variates
91 : depends_on
<count
, sum
>
95 typedef accumulators::impl::mean_impl
<mpl::_1
, sum
> impl
;
102 typedef accumulators::impl::immediate_mean_impl
<mpl::_1
, tag::sample
> impl
;
104 struct mean_of_weights
105 : depends_on
<count
, sum_of_weights
>
107 typedef mpl::true_ is_weight_accumulator
;
110 typedef accumulators::impl::mean_impl
<mpl::_2
, sum_of_weights
> impl
;
112 struct immediate_mean_of_weights
115 typedef mpl::true_ is_weight_accumulator
;
118 typedef accumulators::impl::immediate_mean_impl
<mpl::_2
, tag::weight
> impl
;
120 template<typename VariateType
, typename VariateTag
>
121 struct mean_of_variates
122 : depends_on
<count
, sum_of_variates
<VariateType
, VariateTag
> >
126 typedef mpl::always
<accumulators::impl::mean_impl
<VariateType
, sum_of_variates
<VariateType
, VariateTag
> > > impl
;
128 template<typename VariateType
, typename VariateTag
>
129 struct immediate_mean_of_variates
134 typedef mpl::always
<accumulators::impl::immediate_mean_impl
<VariateType
, VariateTag
> > impl
;
138 ///////////////////////////////////////////////////////////////////////////////
140 // extract::mean_of_weights
141 // extract::mean_of_variates
145 extractor
<tag::mean
> const mean
= {};
146 extractor
<tag::mean_of_weights
> const mean_of_weights
= {};
147 BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag
, mean_of_variates
, (typename
)(typename
))
151 using extract::mean_of_weights
;
152 using extract::mean_of_variates
;
154 // mean(lazy) -> mean
156 struct as_feature
<tag::mean(lazy
)>
158 typedef tag::mean type
;
161 // mean(immediate) -> immediate_mean
163 struct as_feature
<tag::mean(immediate
)>
165 typedef tag::immediate_mean type
;
168 // mean_of_weights(lazy) -> mean_of_weights
170 struct as_feature
<tag::mean_of_weights(lazy
)>
172 typedef tag::mean_of_weights type
;
175 // mean_of_weights(immediate) -> immediate_mean_of_weights
177 struct as_feature
<tag::mean_of_weights(immediate
)>
179 typedef tag::immediate_mean_of_weights type
;
182 // mean_of_variates<VariateType, VariateTag>(lazy) -> mean_of_variates<VariateType, VariateTag>
183 template<typename VariateType
, typename VariateTag
>
184 struct as_feature
<tag::mean_of_variates
<VariateType
, VariateTag
>(lazy
)>
186 typedef tag::mean_of_variates
<VariateType
, VariateTag
> type
;
189 // mean_of_variates<VariateType, VariateTag>(immediate) -> immediate_mean_of_variates<VariateType, VariateTag>
190 template<typename VariateType
, typename VariateTag
>
191 struct as_feature
<tag::mean_of_variates
<VariateType
, VariateTag
>(immediate
)>
193 typedef tag::immediate_mean_of_variates
<VariateType
, VariateTag
> type
;
196 // for the purposes of feature-based dependency resolution,
197 // immediate_mean provides the same feature as mean
199 struct feature_of
<tag::immediate_mean
>
200 : feature_of
<tag::mean
>
204 // for the purposes of feature-based dependency resolution,
205 // immediate_mean provides the same feature as mean
207 struct feature_of
<tag::immediate_mean_of_weights
>
208 : feature_of
<tag::mean_of_weights
>
212 // for the purposes of feature-based dependency resolution,
213 // immediate_mean provides the same feature as mean
214 template<typename VariateType
, typename VariateTag
>
215 struct feature_of
<tag::immediate_mean_of_variates
<VariateType
, VariateTag
> >
216 : feature_of
<tag::mean_of_variates
<VariateType
, VariateTag
> >
220 // So that mean can be automatically substituted with
221 // weighted_mean when the weight parameter is non-void.
223 struct as_weighted_feature
<tag::mean
>
225 typedef tag::weighted_mean type
;
229 struct feature_of
<tag::weighted_mean
>
230 : feature_of
<tag::mean
>
233 // So that immediate_mean can be automatically substituted with
234 // immediate_weighted_mean when the weight parameter is non-void.
236 struct as_weighted_feature
<tag::immediate_mean
>
238 typedef tag::immediate_weighted_mean type
;
242 struct feature_of
<tag::immediate_weighted_mean
>
243 : feature_of
<tag::immediate_mean
>
246 // So that mean_of_weights<> can be automatically substituted with
247 // weighted_mean_of_variates<> when the weight parameter is non-void.
248 template<typename VariateType
, typename VariateTag
>
249 struct as_weighted_feature
<tag::mean_of_variates
<VariateType
, VariateTag
> >
251 typedef tag::weighted_mean_of_variates
<VariateType
, VariateTag
> type
;
254 template<typename VariateType
, typename VariateTag
>
255 struct feature_of
<tag::weighted_mean_of_variates
<VariateType
, VariateTag
> >
256 : feature_of
<tag::mean_of_variates
<VariateType
, VariateTag
> >
260 // So that immediate_mean_of_weights<> can be automatically substituted with
261 // immediate_weighted_mean_of_variates<> when the weight parameter is non-void.
262 template<typename VariateType
, typename VariateTag
>
263 struct as_weighted_feature
<tag::immediate_mean_of_variates
<VariateType
, VariateTag
> >
265 typedef tag::immediate_weighted_mean_of_variates
<VariateType
, VariateTag
> type
;
268 template<typename VariateType
, typename VariateTag
>
269 struct feature_of
<tag::immediate_weighted_mean_of_variates
<VariateType
, VariateTag
> >
270 : feature_of
<tag::immediate_mean_of_variates
<VariateType
, VariateTag
> >
274 ////////////////////////////////////////////////////////////////////////////
275 //// droppable_accumulator<mean_impl>
276 //// need to specialize droppable lazy mean to cache the result at the
277 //// point the accumulator is dropped.
280 //template<typename Sample, typename SumFeature>
281 //struct droppable_accumulator<impl::mean_impl<Sample, SumFeature> >
282 // : droppable_accumulator_base<
283 // with_cached_result<impl::mean_impl<Sample, SumFeature> >
286 // template<typename Args>
287 // droppable_accumulator(Args const &args)
288 // : droppable_accumulator::base(args)
293 }} // namespace boost::accumulators