1///////////////////////////////////////////////////////////////////////////////
2// sum.hpp
3//
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)
7
8#ifndef BOOST_ACCUMULATORS_STATISTICS_SUM_HPP_EAN_28_10_2005
9#define BOOST_ACCUMULATORS_STATISTICS_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#include <boost/accumulators/statistics/count.hpp>
21
22namespace boost { namespace accumulators
23{
24
25namespace impl
26{
27 ///////////////////////////////////////////////////////////////////////////////
28 // sum_impl
29 template<typename Sample, typename Tag>
30 struct sum_impl
31 : accumulator_base
32 {
33 // for boost::result_of
34 typedef Sample result_type;
35
36 template<typename Args>
37 sum_impl(Args const &args)
38 : sum(args[parameter::keyword<Tag>::get() | Sample()])
39 {
40 }
41
42 template<typename Args>
43 void operator ()(Args const &args)
44 {
45 // what about overflow?
46 this->sum += args[parameter::keyword<Tag>::get()];
47 }
48
49 result_type result(dont_care) const
50 {
51 return this->sum;
52 }
53
54 template<class Archive>
55 void serialize(Archive & ar, const unsigned int file_version)
56 {
57 ar & sum;
58 }
59
60 private:
61 Sample sum;
62 };
63
64} // namespace impl
65
66///////////////////////////////////////////////////////////////////////////////
67// tag::sum
68// tag::sum_of_weights
69// tag::sum_of_variates
70//
71namespace tag
72{
73 struct sum
74 : depends_on<>
75 {
76 /// INTERNAL ONLY
77 ///
78 typedef accumulators::impl::sum_impl<mpl::_1, tag::sample> impl;
79 };
80
81 struct sum_of_weights
82 : depends_on<>
83 {
84 typedef mpl::true_ is_weight_accumulator;
85 /// INTERNAL ONLY
86 ///
87 typedef accumulators::impl::sum_impl<mpl::_2, tag::weight> impl;
88 };
89
90 template<typename VariateType, typename VariateTag>
91 struct sum_of_variates
92 : depends_on<>
93 {
94 /// INTERNAL ONLY
95 ///
96 typedef mpl::always<accumulators::impl::sum_impl<VariateType, VariateTag> > impl;
97 };
98
99 struct abstract_sum_of_variates
100 : depends_on<>
101 {
102 };
103}
104
105///////////////////////////////////////////////////////////////////////////////
106// extract::sum
107// extract::sum_of_weights
108// extract::sum_of_variates
109//
110namespace extract
111{
112 extractor<tag::sum> const sum = {};
113 extractor<tag::sum_of_weights> const sum_of_weights = {};
114 extractor<tag::abstract_sum_of_variates> const sum_of_variates = {};
115
116 BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum)
117 BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_weights)
118 BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_variates)
119}
120
121using extract::sum;
122using extract::sum_of_weights;
123using extract::sum_of_variates;
124
125// So that mean can be automatically substituted with
126// weighted_mean when the weight parameter is non-void.
127template<>
128struct as_weighted_feature<tag::sum>
129{
130 typedef tag::weighted_sum type;
131};
132
133template<>
134struct feature_of<tag::weighted_sum>
135 : feature_of<tag::sum>
136{};
137
138template<typename VariateType, typename VariateTag>
139struct feature_of<tag::sum_of_variates<VariateType, VariateTag> >
140 : feature_of<tag::abstract_sum_of_variates>
141{
142};
143
144}} // namespace boost::accumulators
145
146#endif
147

source code of include/boost/accumulators/statistics/sum.hpp