[英]boost accumulator rolling count non zero
I am trying to use accumulators to find out statistics for a given activity per sec. 我试图使用累加器来查找每秒给定活动的统计数据。 Below are the two stats I would like to compute 以下是我想要计算的两个统计数据
Sum of the total weight triggered by the activity. 活动触发的总重量总和。
To achieve I have assumed a granularity by 10 msecs and considering 100 buckets ( per sec ). 为了实现我假设粒度为10毫秒并考虑100个桶(每秒)。
Psuedo code below 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;
}
Using above technique i am able to achieve weight entered in the last second but how do I achieve how many times has the activity been triggered in the last sec using boost accumulators ? 使用上述技术我能够实现在最后一秒输入的重量但是如何使用增压累加器实现在最后一秒触发活动的次数?
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
?). 当然, rolling_count_non_zero
听起来像是你想要的合适的名字(尽管它可能更通用,也许是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: Boost.accumulators用户指南描述了如何编写自己的累加器,功能和提取器,只需按照该描述,以下似乎工作:
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 现场演示: http : //coliru.stacked-crooked.com/a/bc4bea090690f26d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.