1 ///////////////////////////////////////////////////////////////////////////////
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)
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_WINDOW_HPP_EAN_26_12_2008
9 #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_WINDOW_HPP_EAN_26_12_2008
12 #include <boost/version.hpp>
13 #include <boost/assert.hpp>
14 #include <boost/circular_buffer.hpp>
15 #include <boost/range/iterator_range.hpp>
16 #include <boost/accumulators/framework/extractor.hpp>
17 #include <boost/accumulators/framework/depends_on.hpp>
18 #include <boost/accumulators/framework/accumulator_base.hpp>
19 #include <boost/accumulators/framework/parameters/sample.hpp>
20 #include <boost/accumulators/framework/parameters/accumulator.hpp>
21 #include <boost/accumulators/numeric/functional.hpp>
23 namespace boost
{ namespace accumulators
26 ///////////////////////////////////////////////////////////////////////////////
27 // tag::rolling_window::size named parameter
28 BOOST_PARAMETER_NESTED_KEYWORD(tag
, rolling_window_size
, window_size
)
32 ///////////////////////////////////////////////////////////////////////////////
33 // rolling_window_plus1_impl
34 // stores the latest N+1 samples, where N is specified at construction time
35 // with the rolling_window_size named parameter
36 template<typename Sample
>
37 struct rolling_window_plus1_impl
40 typedef typename circular_buffer
<Sample
>::const_iterator const_iterator
;
41 typedef iterator_range
<const_iterator
> result_type
;
43 template<typename Args
>
44 rolling_window_plus1_impl(Args
const & args
)
45 : buffer_(args
[rolling_window_size
] + 1)
48 #if BOOST_VERSION < 103600
49 // Before Boost 1.36, copying a circular buffer didn't copy
50 // it's capacity, and we need that behavior.
51 rolling_window_plus1_impl(rolling_window_plus1_impl
const &that
)
52 : buffer_(that
.buffer_
)
54 this->buffer_
.set_capacity(that
.buffer_
.capacity());
57 rolling_window_plus1_impl
&operator =(rolling_window_plus1_impl
const &that
)
59 this->buffer_
= that
.buffer_
;
60 this->buffer_
.set_capacity(that
.buffer_
.capacity());
64 template<typename Args
>
65 void operator ()(Args
const &args
)
67 this->buffer_
.push_back(args
[sample
]);
72 return this->buffer_
.full();
75 // The result of a shifted rolling window is the range including
76 // everything except the most recently added element.
77 result_type
result(dont_care
) const
79 return result_type(this->buffer_
.begin(), this->buffer_
.end());
83 circular_buffer
<Sample
> buffer_
;
86 template<typename Args
>
87 bool is_rolling_window_plus1_full(Args
const &args
)
89 return find_accumulator
<tag::rolling_window_plus1
>(args
[accumulator
]).full();
92 ///////////////////////////////////////////////////////////////////////////////
93 // rolling_window_impl
94 // stores the latest N samples, where N is specified at construction type
95 // with the rolling_window_size named parameter
96 template<typename Sample
>
97 struct rolling_window_impl
100 typedef typename circular_buffer
<Sample
>::const_iterator const_iterator
;
101 typedef iterator_range
<const_iterator
> result_type
;
103 rolling_window_impl(dont_care
)
106 template<typename Args
>
107 result_type
result(Args
const &args
) const
109 return rolling_window_plus1(args
).advance_begin(is_rolling_window_plus1_full(args
));
115 ///////////////////////////////////////////////////////////////////////////////
116 // tag::rolling_window_plus1
117 // tag::rolling_window
121 struct rolling_window_plus1
123 , tag::rolling_window_size
127 typedef accumulators::impl::rolling_window_plus1_impl
< mpl::_1
> impl
;
129 #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
130 /// tag::rolling_window::size named parameter
131 static boost::parameter::keyword
<tag::rolling_window_size
> const window_size
;
135 struct rolling_window
136 : depends_on
< rolling_window_plus1
>
140 typedef accumulators::impl::rolling_window_impl
< mpl::_1
> impl
;
142 #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
143 /// tag::rolling_window::size named parameter
144 static boost::parameter::keyword
<tag::rolling_window_size
> const window_size
;
150 ///////////////////////////////////////////////////////////////////////////////
151 // extract::rolling_window_plus1
152 // extract::rolling_window
156 extractor
<tag::rolling_window_plus1
> const rolling_window_plus1
= {};
157 extractor
<tag::rolling_window
> const rolling_window
= {};
160 using extract::rolling_window_plus1
;
161 using extract::rolling_window
;
163 }} // namespace boost::accumulators