C++ 增压累加器滚动计数非零

C++ 增压累加器滚动计数非零,c++,boost,boost-accumulators,C++,Boost,Boost Accumulators,我正在尝试使用累加器查找给定每秒活动的统计数据。下面是我想计算的两个统计数据 已触发活动的次数 活动触发的总重量的总和 为了实现这一目标,我假设粒度为10毫秒,并考虑100个存储桶(每秒) 每当发生事件时,Activity线程都会插入累加器 空活动线程每10毫秒唤醒一次,在权重中插入0 下面是Psuedo代码 #include <boost/accumulators/accumulators.hpp> #include <boost/accumulators/sta

我正在尝试使用累加器查找给定每秒活动的统计数据。下面是我想计算的两个统计数据

  • 已触发活动的次数

  • 活动触发的总重量的总和

    为了实现这一目标,我假设粒度为10毫秒,并考虑100个存储桶(每秒)

  • 每当发生事件时,Activity线程都会插入累加器

  • 空活动线程每10毫秒唤醒一次,在权重中插入0

下面是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;
}
#包括
#包括
#包括
#包括
使用名称空间boost::累加器;
#定义最大重量100
#定义最大容量10
累加器设置acc(标签::滚动窗口::窗口大小=100);
void null_run()//每10毫秒调用一次
{
//互斥保护
acc(0);
}
无效活动\u已触发(整数权重)//由外部事件触发
{
//互斥保护
acc(重量);
如果(checkStatus()==false)
{
printf(“每秒到达停止点的最大数量”);
退出()
}
}
bool checkStatus()
{
int weights persec=滚动总和(acc);
//这里,我只想在非零可能是滚动的情况下得到计数??
int activitiespersec=滚动计数(acc);
如果(重量百分比>最大重量)
返回false;
if(活动性>最大活动性)
返回false;
返回true;
}

使用上述技术,我能够实现在最后一秒输入的重量,但我如何实现在最后一秒使用增压累加器触发活动的次数?

当然
滚动计数非零
听起来是您想要的合适名称(虽然它可能更通用一些,如果?,可能是
rolling\u count\u)

描述如何编写自己的累加器、功能部件和提取器,简单地按照该描述,以下内容似乎有效:

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;
}}
namespace boost{namespace累加器{namespace impl{
模板
结构滚动计数非零:累加器基数
{
typedef std::大小\u t结果\u类型;
滚动计数非零(不在乎):cnt_uu(){}
模板
void运算符()(Args常量和Args)
{
如果(参数[样本]!=0)
++碳纳米管;
如果(正在滚动窗口加上1)已满(args)
&&滚动窗口加1(args).front()!=0)
--碳纳米管;
}
模板
结果类型结果(Args const&Args)const{return cnt}
私人:
标准:尺寸;
};
}名称空间标记{
结构滚动计数非零:取决于
{
typedef累加器::impl::滚动计数\u非零impl;
};
}名称空间提取{
提取器常量滚动计数非零={};
}
使用extract::滚动计数非零;
}}

现场演示:

谢谢Cubbi。那会有用的。虽然我想知道我是否可以简单地使用现有的东西。可能我只需要实现简单数组并自己实现滚动计数。这比必须将其合并到项目库中要容易一些。尽管如你所说,如果有类似滚动计数的东西,那将非常好_如果在累加器中已经可用。我给Eric Niebler发了电子邮件,他说我必须实现自己的累加器。感谢代码。我认为我们不需要滚动窗口加1(args)。front()!=0;因为我们从不插入0。对吗?@Shanky front()是刚刚离开滚动窗口的元素