Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:21

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // sum.hpp
0003 //
0004 //  Copyright 2005 Eric Niebler. Distributed under the Boost
0005 //  Software License, Version 1.0. (See accompanying file
0006 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 
0008 #ifndef BOOST_ACCUMULATORS_STATISTICS_SUM_HPP_EAN_28_10_2005
0009 #define BOOST_ACCUMULATORS_STATISTICS_SUM_HPP_EAN_28_10_2005
0010 
0011 #include <boost/mpl/placeholders.hpp>
0012 #include <boost/accumulators/framework/accumulator_base.hpp>
0013 #include <boost/accumulators/framework/extractor.hpp>
0014 #include <boost/accumulators/numeric/functional.hpp>
0015 #include <boost/accumulators/framework/parameters/sample.hpp>
0016 #include <boost/accumulators/framework/parameters/weight.hpp>
0017 #include <boost/accumulators/framework/accumulators/external_accumulator.hpp>
0018 #include <boost/accumulators/framework/depends_on.hpp>
0019 #include <boost/accumulators/statistics_fwd.hpp>
0020 #include <boost/accumulators/statistics/count.hpp>
0021 
0022 namespace boost { namespace accumulators
0023 {
0024 
0025 namespace impl
0026 {
0027     ///////////////////////////////////////////////////////////////////////////////
0028     // sum_impl
0029     template<typename Sample, typename Tag>
0030     struct sum_impl
0031       : accumulator_base
0032     {
0033         // for boost::result_of
0034         typedef Sample result_type;
0035 
0036         template<typename Args>
0037         sum_impl(Args const &args)
0038           : sum(args[parameter::keyword<Tag>::get() | Sample()])
0039         {
0040         }
0041 
0042         template<typename Args>
0043         void operator ()(Args const &args)
0044         {
0045             // what about overflow?
0046             this->sum += args[parameter::keyword<Tag>::get()];
0047         }
0048 
0049         result_type result(dont_care) const
0050         {
0051             return this->sum;
0052         }
0053 
0054         template<class Archive>
0055         void serialize(Archive & ar, const unsigned int /* file_version */)
0056         {
0057             ar & sum;
0058         }
0059 
0060     private:
0061         Sample sum;
0062     };
0063 
0064 } // namespace impl
0065 
0066 ///////////////////////////////////////////////////////////////////////////////
0067 // tag::sum
0068 // tag::sum_of_weights
0069 // tag::sum_of_variates
0070 //
0071 namespace tag
0072 {
0073     struct sum
0074       : depends_on<>
0075     {
0076         /// INTERNAL ONLY
0077         ///
0078         typedef accumulators::impl::sum_impl<mpl::_1, tag::sample> impl;
0079     };
0080 
0081     struct sum_of_weights
0082       : depends_on<>
0083     {
0084         typedef mpl::true_ is_weight_accumulator;
0085         /// INTERNAL ONLY
0086         ///
0087         typedef accumulators::impl::sum_impl<mpl::_2, tag::weight> impl;
0088     };
0089 
0090     template<typename VariateType, typename VariateTag>
0091     struct sum_of_variates
0092       : depends_on<>
0093     {
0094         /// INTERNAL ONLY
0095         ///
0096         typedef mpl::always<accumulators::impl::sum_impl<VariateType, VariateTag> > impl;
0097     };
0098 
0099     struct abstract_sum_of_variates
0100       : depends_on<>
0101     {
0102     };
0103 }
0104 
0105 ///////////////////////////////////////////////////////////////////////////////
0106 // extract::sum
0107 // extract::sum_of_weights
0108 // extract::sum_of_variates
0109 //
0110 namespace extract
0111 {
0112     extractor<tag::sum> const sum = {};
0113     extractor<tag::sum_of_weights> const sum_of_weights = {};
0114     extractor<tag::abstract_sum_of_variates> const sum_of_variates = {};
0115 
0116     BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum)
0117     BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_weights)
0118     BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_variates)
0119 }
0120 
0121 using extract::sum;
0122 using extract::sum_of_weights;
0123 using extract::sum_of_variates;
0124 
0125 // So that mean can be automatically substituted with
0126 // weighted_mean when the weight parameter is non-void.
0127 template<>
0128 struct as_weighted_feature<tag::sum>
0129 {
0130     typedef tag::weighted_sum type;
0131 };
0132 
0133 template<>
0134 struct feature_of<tag::weighted_sum>
0135   : feature_of<tag::sum>
0136 {};
0137 
0138 template<typename VariateType, typename VariateTag>
0139 struct feature_of<tag::sum_of_variates<VariateType, VariateTag> >
0140   : feature_of<tag::abstract_sum_of_variates>
0141 {
0142 };
0143 
0144 }} // namespace boost::accumulators
0145 
0146 #endif