sum.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. private:
  47. Sample sum;
  48. };
  49. } // namespace impl
  50. ///////////////////////////////////////////////////////////////////////////////
  51. // tag::sum
  52. // tag::sum_of_weights
  53. // tag::sum_of_variates
  54. //
  55. namespace tag
  56. {
  57. struct sum
  58. : depends_on<>
  59. {
  60. /// INTERNAL ONLY
  61. ///
  62. typedef accumulators::impl::sum_impl<mpl::_1, tag::sample> impl;
  63. };
  64. struct sum_of_weights
  65. : depends_on<>
  66. {
  67. typedef mpl::true_ is_weight_accumulator;
  68. /// INTERNAL ONLY
  69. ///
  70. typedef accumulators::impl::sum_impl<mpl::_2, tag::weight> impl;
  71. };
  72. template<typename VariateType, typename VariateTag>
  73. struct sum_of_variates
  74. : depends_on<>
  75. {
  76. /// INTERNAL ONLY
  77. ///
  78. typedef mpl::always<accumulators::impl::sum_impl<VariateType, VariateTag> > impl;
  79. };
  80. struct abstract_sum_of_variates
  81. : depends_on<>
  82. {
  83. };
  84. }
  85. ///////////////////////////////////////////////////////////////////////////////
  86. // extract::sum
  87. // extract::sum_of_weights
  88. // extract::sum_of_variates
  89. //
  90. namespace extract
  91. {
  92. extractor<tag::sum> const sum = {};
  93. extractor<tag::sum_of_weights> const sum_of_weights = {};
  94. extractor<tag::abstract_sum_of_variates> const sum_of_variates = {};
  95. BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum)
  96. BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_weights)
  97. BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_variates)
  98. }
  99. using extract::sum;
  100. using extract::sum_of_weights;
  101. using extract::sum_of_variates;
  102. // So that mean can be automatically substituted with
  103. // weighted_mean when the weight parameter is non-void.
  104. template<>
  105. struct as_weighted_feature<tag::sum>
  106. {
  107. typedef tag::weighted_sum type;
  108. };
  109. template<>
  110. struct feature_of<tag::weighted_sum>
  111. : feature_of<tag::sum>
  112. {};
  113. template<typename VariateType, typename VariateTag>
  114. struct feature_of<tag::sum_of_variates<VariateType, VariateTag> >
  115. : feature_of<tag::abstract_sum_of_variates>
  116. {
  117. };
  118. }} // namespace boost::accumulators
  119. #endif