Chris@16
|
1 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 // rolling_sum.hpp
|
Chris@16
|
3 //
|
Chris@16
|
4 // Copyright 2008 Eric Niebler. Distributed under the Boost
|
Chris@16
|
5 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7
|
Chris@16
|
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_SUM_HPP_EAN_26_12_2008
|
Chris@16
|
9 #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_SUM_HPP_EAN_26_12_2008
|
Chris@16
|
10
|
Chris@16
|
11 #include <boost/mpl/placeholders.hpp>
|
Chris@16
|
12 #include <boost/accumulators/framework/accumulator_base.hpp>
|
Chris@16
|
13 #include <boost/accumulators/framework/extractor.hpp>
|
Chris@16
|
14 #include <boost/accumulators/numeric/functional.hpp>
|
Chris@16
|
15 #include <boost/accumulators/framework/parameters/sample.hpp>
|
Chris@16
|
16 #include <boost/accumulators/framework/depends_on.hpp>
|
Chris@16
|
17 #include <boost/accumulators/statistics_fwd.hpp>
|
Chris@16
|
18 #include <boost/accumulators/statistics/rolling_window.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost { namespace accumulators
|
Chris@16
|
21 {
|
Chris@16
|
22 namespace impl
|
Chris@16
|
23 {
|
Chris@16
|
24 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
25 // rolling_sum_impl
|
Chris@16
|
26 // returns the sum of the samples in the rolling window
|
Chris@16
|
27 template<typename Sample>
|
Chris@16
|
28 struct rolling_sum_impl
|
Chris@16
|
29 : accumulator_base
|
Chris@16
|
30 {
|
Chris@16
|
31 typedef Sample result_type;
|
Chris@16
|
32
|
Chris@16
|
33 template<typename Args>
|
Chris@16
|
34 rolling_sum_impl(Args const &args)
|
Chris@16
|
35 : sum_(args[sample | Sample()])
|
Chris@16
|
36 {}
|
Chris@16
|
37
|
Chris@16
|
38 template<typename Args>
|
Chris@16
|
39 void operator ()(Args const &args)
|
Chris@16
|
40 {
|
Chris@16
|
41 if(is_rolling_window_plus1_full(args))
|
Chris@16
|
42 {
|
Chris@16
|
43 this->sum_ -= rolling_window_plus1(args).front();
|
Chris@16
|
44 }
|
Chris@16
|
45 this->sum_ += args[sample];
|
Chris@16
|
46 }
|
Chris@16
|
47
|
Chris@16
|
48 template<typename Args>
|
Chris@101
|
49 result_type result(Args const & /*args*/) const
|
Chris@16
|
50 {
|
Chris@16
|
51 return this->sum_;
|
Chris@16
|
52 }
|
Chris@16
|
53
|
Chris@16
|
54 private:
|
Chris@16
|
55 Sample sum_;
|
Chris@16
|
56 };
|
Chris@16
|
57 } // namespace impl
|
Chris@16
|
58
|
Chris@16
|
59 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
60 // tag::rolling_sum
|
Chris@16
|
61 //
|
Chris@16
|
62 namespace tag
|
Chris@16
|
63 {
|
Chris@16
|
64 struct rolling_sum
|
Chris@16
|
65 : depends_on< rolling_window_plus1 >
|
Chris@16
|
66 {
|
Chris@16
|
67 /// INTERNAL ONLY
|
Chris@16
|
68 ///
|
Chris@16
|
69 typedef accumulators::impl::rolling_sum_impl< mpl::_1 > impl;
|
Chris@16
|
70
|
Chris@16
|
71 #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
Chris@16
|
72 /// tag::rolling_window::window_size named parameter
|
Chris@16
|
73 static boost::parameter::keyword<tag::rolling_window_size> const window_size;
|
Chris@16
|
74 #endif
|
Chris@16
|
75 };
|
Chris@16
|
76 } // namespace tag
|
Chris@16
|
77
|
Chris@16
|
78 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
79 // extract::rolling_sum
|
Chris@16
|
80 //
|
Chris@16
|
81 namespace extract
|
Chris@16
|
82 {
|
Chris@16
|
83 extractor<tag::rolling_sum> const rolling_sum = {};
|
Chris@16
|
84
|
Chris@16
|
85 BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_sum)
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@16
|
88 using extract::rolling_sum;
|
Chris@16
|
89 }} // namespace boost::accumulators
|
Chris@16
|
90
|
Chris@16
|
91 #endif
|