/////////////////////////////////////////////////////////////////////////////// // sample_cache.hpp // // Copyright 2007 Hirotaka Niitsuma. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_ACCUMULATORS_STATISTICS_SAMPLE_CACHE_HPP_DE_01_01_2007 #define BOOST_ACCUMULATORS_STATISTICS_SAMPLE_CACHE_HPP_DE_01_01_2007 #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace accumulators { /////////////////////////////////////////////////////////////////////////////// // cache_size and num_bins named parameters // BOOST_PARAMETER_NESTED_KEYWORD(tag, sample_cache_size, cache_size) namespace impl { /////////////////////////////////////////////////////////////////////////////// // sample_cache_impl template struct sample_cache_impl : accumulator_base { typedef typename numeric::functional::average::result_type float_type; typedef std::deque array_type; // for boost::result_of typedef iterator_range result_type; template sample_cache_impl(Args const &args) : cache_size(args[sample_cache_size]) , cache(cache_size) {} template void operator ()(Args const &args) { std::size_t cnt = count(args); // Fill up cache with cache_size first samples if (cnt <= this->cache_size) { this->cache[cnt - 1] = args[parameter::keyword::get()] ; }else{ if( this->cache_size >0) this->cache.pop_front(); this->cache.push_back(args[parameter::keyword::get()]); } } template result_type result(Args const &args) const { return make_iterator_range(this->cache); } private: std::size_t cache_size; // number of cached samples mutable array_type cache; // cache to store the first cache_size samples }; } // namespace impl /////////////////////////////////////////////////////////////////////////////// // tag::sample_cache // namespace tag { struct sample_cache : depends_on , sample_cache_size { /// INTERNAL ONLY typedef accumulators::impl::sample_cache_impl impl; #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED /// tag::sample_cache::cache_size named parameter /// tag::sample_cache::num_bins named parameter static boost::parameter::keyword const cache_size; #endif }; template struct sample_cache_of_variates : depends_on { /// INTERNAL ONLY typedef mpl::always > impl; }; } /////////////////////////////////////////////////////////////////////////////// // extract::sample_cache // namespace extract { extractor const sample_cache = {}; BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, sample_cache_of_variates, (typename)(typename)); } using extract::sample_cache; using extract::sample_cache_of_variates; }} // namespace boost::accumulators #endif