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