我正在尝试使用累加器来查找每秒给定活动的统计信息。以下是我想要计算的两个统计数据
活动触发的总重量总和。
要实现我假设粒度为10毫秒并考虑100个桶(每秒)。
下面的Psuedo代码
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_count.hpp>
#include <boost/accumulators/statistics/rolling_sum.hpp>
using namespace boost::accumulators;
#define MAX_WEIGHT 100
#define MAX_ACIVITY 10
accumulator_set<double, features<tag::rolling_count, tag::rolling_sum> > acc(tag::rolling_window::window_size = 100);
void null_run()//invoked every 10 msecs
{
//mutex protected
acc(0);
}
void activity_triggered(int weight) // triggered by an external event
{
//mutex protected
acc(weight);
if (checkStatus() == false)
{
printf("Max quantity per sec reached stop ");
exit()
}
}
bool checkStatus()
{
int weightPerSec = rolling_sum(acc);
//here I would like to get the count only if non zero may be rolling_count_non_zero()??
int acitivitiesPersec = rolling_count(acc);
if (weightPerSec > MAX_WEIGHT)
return false;
if (acitivitiesPersec > MAX_ACTIVITY)
return false;
return true;
}
使用上述技术我能够达到最后一秒输入的重量,但是如何使用增压累加器实现在最后一秒触发活动的次数?
答案 0 :(得分:2)
当然rolling_count_non_zero
听起来像是你想要的合适的名字(尽管它可能更通用,可能是rolling_count_if
?)。
Boost.accumulators users guide描述了如何编写自己的累加器,功能和提取器,只需遵循该描述,以下似乎有效:
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;
}}