rolling_sum.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // rolling_sum.hpp
  3. //
  4. // Copyright 2008 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_ROLLING_SUM_HPP_EAN_26_12_2008
  8. #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_SUM_HPP_EAN_26_12_2008
  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/depends_on.hpp>
  15. #include <boost/accumulators/statistics_fwd.hpp>
  16. #include <boost/accumulators/statistics/rolling_window.hpp>
  17. namespace boost { namespace accumulators
  18. {
  19. namespace impl
  20. {
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // rolling_sum_impl
  23. // returns the sum of the samples in the rolling window
  24. template<typename Sample>
  25. struct rolling_sum_impl
  26. : accumulator_base
  27. {
  28. typedef Sample result_type;
  29. template<typename Args>
  30. rolling_sum_impl(Args const &args)
  31. : sum_(args[sample | Sample()])
  32. {}
  33. template<typename Args>
  34. void operator ()(Args const &args)
  35. {
  36. if(is_rolling_window_plus1_full(args))
  37. {
  38. this->sum_ -= rolling_window_plus1(args).front();
  39. }
  40. this->sum_ += args[sample];
  41. }
  42. template<typename Args>
  43. result_type result(Args const & /*args*/) const
  44. {
  45. return this->sum_;
  46. }
  47. // make this accumulator serializeable
  48. template<class Archive>
  49. void serialize(Archive & ar, const unsigned int file_version)
  50. {
  51. ar & sum_;
  52. }
  53. private:
  54. Sample sum_;
  55. };
  56. } // namespace impl
  57. ///////////////////////////////////////////////////////////////////////////////
  58. // tag::rolling_sum
  59. //
  60. namespace tag
  61. {
  62. struct rolling_sum
  63. : depends_on< rolling_window_plus1 >
  64. {
  65. /// INTERNAL ONLY
  66. ///
  67. typedef accumulators::impl::rolling_sum_impl< mpl::_1 > impl;
  68. #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
  69. /// tag::rolling_window::window_size named parameter
  70. static boost::parameter::keyword<tag::rolling_window_size> const window_size;
  71. #endif
  72. };
  73. } // namespace tag
  74. ///////////////////////////////////////////////////////////////////////////////
  75. // extract::rolling_sum
  76. //
  77. namespace extract
  78. {
  79. extractor<tag::rolling_sum> const rolling_sum = {};
  80. BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_sum)
  81. }
  82. using extract::rolling_sum;
  83. }} // namespace boost::accumulators
  84. #endif