sum.hpp 3.8 KB

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