Sure rolling_count_non_zero
sounds like an appropriate name for what you want (although it could have been somewhat more generic, maybe rolling_count_if
?).
Boost.accumulators users guide describes how to write your own accumulators, features, and extractors, simply following that description, the following seems to work:
namespace boost { namespace accumulators { namespace impl {
template<typename Sample>
struct rolling_count_non_zero : accumulator_base
{
typedef std::size_t result_type;
rolling_count_non_zero(dont_care) : cnt_() {}
template<typename Args>
void operator ()(Args const &args)
{
if(args[sample] != 0)
++cnt_;
if(is_rolling_window_plus1_full(args)
&& rolling_window_plus1(args).front() != 0 )
--cnt_;
}
template<typename Args>
result_type result(Args const &args) const { return cnt_; }
private:
std::size_t cnt_;
};
} namespace tag {
struct rolling_count_non_zero : depends_on< rolling_window_plus1 >
{
typedef accumulators::impl::rolling_count_non_zero< mpl::_1 > impl;
};
} namespace extract {
extractor<tag::rolling_count_non_zero> const rolling_count_non_zero = {};
}
using extract::rolling_count_non_zero;
}}
live demo: http://coliru.stacked-crooked.com/a/bc4bea090690f26d