|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// sorted_rolling_window.hpp |
| 3 | +// |
| 4 | +// Copyright 2018 Quentin Chateau. 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_SORTED_ROLLING_WINDOW_HPP_QC_20_12_2018 |
| 9 | +#define BOOST_ACCUMULATORS_STATISTICS_SORTED_ROLLING_WINDOW_HPP_QC_20_12_2018 |
| 10 | + |
| 11 | +#include <boost/container/set.hpp> |
| 12 | +#include <boost/range/iterator_range.hpp> |
| 13 | +#include <boost/accumulators/accumulators_fwd.hpp> |
| 14 | +#include <boost/accumulators/framework/extractor.hpp> |
| 15 | +#include <boost/accumulators/framework/depends_on.hpp> |
| 16 | +#include <boost/accumulators/framework/accumulator_base.hpp> |
| 17 | +#include <boost/accumulators/framework/parameters/sample.hpp> |
| 18 | +#include <boost/accumulators/framework/parameters/accumulator.hpp> |
| 19 | +#include <boost/accumulators/statistics_fwd.hpp> |
| 20 | +#include <boost/accumulators/statistics/rolling_window.hpp> |
| 21 | + |
| 22 | +namespace boost { namespace accumulators |
| 23 | +{ |
| 24 | + |
| 25 | +namespace impl |
| 26 | +{ |
| 27 | + /////////////////////////////////////////////////////////////////////////////// |
| 28 | + // sorted_rolling_window |
| 29 | + // stores the latest N samples, where N is specified at construction time |
| 30 | + // with the rolling_window_size named parameter. samples are sorted |
| 31 | + // on insersion |
| 32 | + template<typename Sample> |
| 33 | + struct sorted_rolling_window_impl |
| 34 | + : accumulator_base |
| 35 | + { |
| 36 | + typedef typename container::multiset<Sample>::const_iterator const_iterator; |
| 37 | + typedef iterator_range<const_iterator> result_type; |
| 38 | + |
| 39 | + template<typename Args> |
| 40 | + sorted_rolling_window_impl(Args const &) |
| 41 | + {} |
| 42 | + |
| 43 | + template<typename Args> |
| 44 | + void operator ()(Args const &args) |
| 45 | + { |
| 46 | + if(is_rolling_window_plus1_full(args)) |
| 47 | + { |
| 48 | + const_iterator it = this->sorted_buffer_.find(rolling_window_plus1(args).front()); |
| 49 | + this->sorted_buffer_.erase(it); |
| 50 | + } |
| 51 | + this->sorted_buffer_.insert(args[sample]); |
| 52 | + } |
| 53 | + |
| 54 | + result_type result(dont_care) const |
| 55 | + { |
| 56 | + return result_type(this->sorted_buffer_.begin(), this->sorted_buffer_.end()); |
| 57 | + } |
| 58 | + |
| 59 | + private: |
| 60 | + container::multiset<Sample> sorted_buffer_; |
| 61 | + }; |
| 62 | + |
| 63 | +} // namespace impl |
| 64 | + |
| 65 | +/////////////////////////////////////////////////////////////////////////////// |
| 66 | +// tag::sorted_rolling_window |
| 67 | +// |
| 68 | +namespace tag |
| 69 | +{ |
| 70 | + struct sorted_rolling_window |
| 71 | + : depends_on< rolling_window_plus1 > |
| 72 | + { |
| 73 | + /// INTERNAL ONLY |
| 74 | + /// |
| 75 | + typedef accumulators::impl::sorted_rolling_window_impl< mpl::_1 > impl; |
| 76 | + |
| 77 | + #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED |
| 78 | + /// tag::sorted_rolling_window::size named parameter |
| 79 | + static boost::parameter::keyword<tag::rolling_window_size> const window_size; |
| 80 | + #endif |
| 81 | + }; |
| 82 | + |
| 83 | +} // namespace tag |
| 84 | + |
| 85 | +/////////////////////////////////////////////////////////////////////////////// |
| 86 | +// extract::sorted_rolling_window |
| 87 | +// |
| 88 | +namespace extract |
| 89 | +{ |
| 90 | + extractor<tag::sorted_rolling_window> const sorted_rolling_window = {}; |
| 91 | + |
| 92 | + BOOST_ACCUMULATORS_IGNORE_GLOBAL(sorted_rolling_window) |
| 93 | +} |
| 94 | + |
| 95 | +using extract::sorted_rolling_window; |
| 96 | + |
| 97 | +}} // namespace boost::accumulators |
| 98 | + |
| 99 | +#endif |
0 commit comments