Chris@16
|
1 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 // rolling_window.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_WINDOW_HPP_EAN_26_12_2008
|
Chris@16
|
9 #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_WINDOW_HPP_EAN_26_12_2008
|
Chris@16
|
10
|
Chris@16
|
11 #include <cstddef>
|
Chris@16
|
12 #include <boost/version.hpp>
|
Chris@16
|
13 #include <boost/assert.hpp>
|
Chris@16
|
14 #include <boost/circular_buffer.hpp>
|
Chris@16
|
15 #include <boost/range/iterator_range.hpp>
|
Chris@16
|
16 #include <boost/accumulators/accumulators_fwd.hpp>
|
Chris@16
|
17 #include <boost/accumulators/framework/extractor.hpp>
|
Chris@16
|
18 #include <boost/accumulators/framework/depends_on.hpp>
|
Chris@16
|
19 #include <boost/accumulators/framework/accumulator_base.hpp>
|
Chris@16
|
20 #include <boost/accumulators/framework/parameters/sample.hpp>
|
Chris@16
|
21 #include <boost/accumulators/framework/parameters/accumulator.hpp>
|
Chris@16
|
22 #include <boost/accumulators/numeric/functional.hpp>
|
Chris@16
|
23 #include <boost/accumulators/statistics_fwd.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 namespace boost { namespace accumulators
|
Chris@16
|
26 {
|
Chris@16
|
27
|
Chris@16
|
28 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
29 // tag::rolling_window::size named parameter
|
Chris@16
|
30 BOOST_PARAMETER_NESTED_KEYWORD(tag, rolling_window_size, window_size)
|
Chris@16
|
31
|
Chris@16
|
32 BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_window_size)
|
Chris@16
|
33
|
Chris@16
|
34 namespace impl
|
Chris@16
|
35 {
|
Chris@16
|
36 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
37 // rolling_window_plus1_impl
|
Chris@16
|
38 // stores the latest N+1 samples, where N is specified at construction time
|
Chris@16
|
39 // with the rolling_window_size named parameter
|
Chris@16
|
40 template<typename Sample>
|
Chris@16
|
41 struct rolling_window_plus1_impl
|
Chris@16
|
42 : accumulator_base
|
Chris@16
|
43 {
|
Chris@16
|
44 typedef typename circular_buffer<Sample>::const_iterator const_iterator;
|
Chris@16
|
45 typedef iterator_range<const_iterator> result_type;
|
Chris@16
|
46
|
Chris@16
|
47 template<typename Args>
|
Chris@16
|
48 rolling_window_plus1_impl(Args const & args)
|
Chris@16
|
49 : buffer_(args[rolling_window_size] + 1)
|
Chris@16
|
50 {}
|
Chris@16
|
51
|
Chris@16
|
52 #if BOOST_VERSION < 103600
|
Chris@16
|
53 // Before Boost 1.36, copying a circular buffer didn't copy
|
Chris@16
|
54 // it's capacity, and we need that behavior.
|
Chris@16
|
55 rolling_window_plus1_impl(rolling_window_plus1_impl const &that)
|
Chris@16
|
56 : buffer_(that.buffer_)
|
Chris@16
|
57 {
|
Chris@16
|
58 this->buffer_.set_capacity(that.buffer_.capacity());
|
Chris@16
|
59 }
|
Chris@16
|
60
|
Chris@16
|
61 rolling_window_plus1_impl &operator =(rolling_window_plus1_impl const &that)
|
Chris@16
|
62 {
|
Chris@16
|
63 this->buffer_ = that.buffer_;
|
Chris@16
|
64 this->buffer_.set_capacity(that.buffer_.capacity());
|
Chris@16
|
65 }
|
Chris@16
|
66 #endif
|
Chris@16
|
67
|
Chris@16
|
68 template<typename Args>
|
Chris@16
|
69 void operator ()(Args const &args)
|
Chris@16
|
70 {
|
Chris@16
|
71 this->buffer_.push_back(args[sample]);
|
Chris@16
|
72 }
|
Chris@16
|
73
|
Chris@16
|
74 bool full() const
|
Chris@16
|
75 {
|
Chris@16
|
76 return this->buffer_.full();
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 // The result of a shifted rolling window is the range including
|
Chris@16
|
80 // everything except the most recently added element.
|
Chris@16
|
81 result_type result(dont_care) const
|
Chris@16
|
82 {
|
Chris@16
|
83 return result_type(this->buffer_.begin(), this->buffer_.end());
|
Chris@16
|
84 }
|
Chris@16
|
85
|
Chris@16
|
86 private:
|
Chris@16
|
87 circular_buffer<Sample> buffer_;
|
Chris@16
|
88 };
|
Chris@16
|
89
|
Chris@16
|
90 template<typename Args>
|
Chris@16
|
91 bool is_rolling_window_plus1_full(Args const &args)
|
Chris@16
|
92 {
|
Chris@16
|
93 return find_accumulator<tag::rolling_window_plus1>(args[accumulator]).full();
|
Chris@16
|
94 }
|
Chris@16
|
95
|
Chris@16
|
96 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
97 // rolling_window_impl
|
Chris@16
|
98 // stores the latest N samples, where N is specified at construction type
|
Chris@16
|
99 // with the rolling_window_size named parameter
|
Chris@16
|
100 template<typename Sample>
|
Chris@16
|
101 struct rolling_window_impl
|
Chris@16
|
102 : accumulator_base
|
Chris@16
|
103 {
|
Chris@16
|
104 typedef typename circular_buffer<Sample>::const_iterator const_iterator;
|
Chris@16
|
105 typedef iterator_range<const_iterator> result_type;
|
Chris@16
|
106
|
Chris@16
|
107 rolling_window_impl(dont_care)
|
Chris@16
|
108 {}
|
Chris@16
|
109
|
Chris@16
|
110 template<typename Args>
|
Chris@16
|
111 result_type result(Args const &args) const
|
Chris@16
|
112 {
|
Chris@16
|
113 return rolling_window_plus1(args).advance_begin(is_rolling_window_plus1_full(args));
|
Chris@16
|
114 }
|
Chris@16
|
115 };
|
Chris@16
|
116
|
Chris@16
|
117 } // namespace impl
|
Chris@16
|
118
|
Chris@16
|
119 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
120 // tag::rolling_window_plus1
|
Chris@16
|
121 // tag::rolling_window
|
Chris@16
|
122 //
|
Chris@16
|
123 namespace tag
|
Chris@16
|
124 {
|
Chris@16
|
125 struct rolling_window_plus1
|
Chris@16
|
126 : depends_on<>
|
Chris@16
|
127 , tag::rolling_window_size
|
Chris@16
|
128 {
|
Chris@16
|
129 /// INTERNAL ONLY
|
Chris@16
|
130 ///
|
Chris@16
|
131 typedef accumulators::impl::rolling_window_plus1_impl< mpl::_1 > impl;
|
Chris@16
|
132
|
Chris@16
|
133 #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
Chris@16
|
134 /// tag::rolling_window::size named parameter
|
Chris@16
|
135 static boost::parameter::keyword<tag::rolling_window_size> const window_size;
|
Chris@16
|
136 #endif
|
Chris@16
|
137 };
|
Chris@16
|
138
|
Chris@16
|
139 struct rolling_window
|
Chris@16
|
140 : depends_on< rolling_window_plus1 >
|
Chris@16
|
141 {
|
Chris@16
|
142 /// INTERNAL ONLY
|
Chris@16
|
143 ///
|
Chris@16
|
144 typedef accumulators::impl::rolling_window_impl< mpl::_1 > impl;
|
Chris@16
|
145
|
Chris@16
|
146 #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
|
Chris@16
|
147 /// tag::rolling_window::size named parameter
|
Chris@16
|
148 static boost::parameter::keyword<tag::rolling_window_size> const window_size;
|
Chris@16
|
149 #endif
|
Chris@16
|
150 };
|
Chris@16
|
151
|
Chris@16
|
152 } // namespace tag
|
Chris@16
|
153
|
Chris@16
|
154 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
155 // extract::rolling_window_plus1
|
Chris@16
|
156 // extract::rolling_window
|
Chris@16
|
157 //
|
Chris@16
|
158 namespace extract
|
Chris@16
|
159 {
|
Chris@16
|
160 extractor<tag::rolling_window_plus1> const rolling_window_plus1 = {};
|
Chris@16
|
161 extractor<tag::rolling_window> const rolling_window = {};
|
Chris@16
|
162
|
Chris@16
|
163 BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_window_plus1)
|
Chris@16
|
164 BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_window)
|
Chris@16
|
165 }
|
Chris@16
|
166
|
Chris@16
|
167 using extract::rolling_window_plus1;
|
Chris@16
|
168 using extract::rolling_window;
|
Chris@16
|
169
|
Chris@16
|
170 }} // namespace boost::accumulators
|
Chris@16
|
171
|
Chris@16
|
172 #endif
|