如何在升压累加器中使用/访问用户参数?
我有一个自定义累加器的要点。我想知道如何从“参数包”中获取整数参数,或者这是否可能:
namespace boost { namespace accumulators { namespace impl {
template<typename Sample>
struct quartile_accumulator : accumulator_base
{
typedef Sample result_type;
quartile(dont_care) : isSorted(false) {}
void operator ()(Sample &value)
{
buffer_.push_back(value);
isSorted = false;
}
template<typename Args>
result_type result(const Args& args) const
{
int numQuartile = args[quartile]; // how to make this work?
BOOST_ASSERT(buffer_.size() >= 4);
BOOST_ASSERT(numQuartile >= 1);
BOOST_ASSERT(numQuartile < 4);
if(!isSorted)
{
std::sort(buffer_.begin(), buffer_.end());
isSorted = true;
}
size_t quartileSize = (size_t) buffer_.size()/4;
if(numQuartile == 2)
return buffer_[quartileSize*2];
else if(numQuartile == 3)
return buffer_[quartileSize*3];
return buffer_[quartileSize];
}
private:
std::vector<Sample> buffer_;
mutable bool isSorted;
};
} // impl
namespace tag
{
struct quartile : depends_on<>
{
typedef impl::quartile_accumulator<mpl::_1> impl;
};
}
namespace extract { extractor<tag::quartile> const quartile = {}; }
using extract::quartile;
}} // namespace boost::accumulators
-
// My desired syntax:
accumulator_set<double, stats<tag::quartile> > values;
// accumulate values
extract::quartile(values, 1); // 1st quartile
extract::quartile(values, 2); // median
extract::quartile(values, 3); // 3rd quartile
I have the gist of a custom accumulator. I want to know how to get an integer argument from the "argument pack", or if this is even possible:
namespace boost { namespace accumulators { namespace impl {
template<typename Sample>
struct quartile_accumulator : accumulator_base
{
typedef Sample result_type;
quartile(dont_care) : isSorted(false) {}
void operator ()(Sample &value)
{
buffer_.push_back(value);
isSorted = false;
}
template<typename Args>
result_type result(const Args& args) const
{
int numQuartile = args[quartile]; // how to make this work?
BOOST_ASSERT(buffer_.size() >= 4);
BOOST_ASSERT(numQuartile >= 1);
BOOST_ASSERT(numQuartile < 4);
if(!isSorted)
{
std::sort(buffer_.begin(), buffer_.end());
isSorted = true;
}
size_t quartileSize = (size_t) buffer_.size()/4;
if(numQuartile == 2)
return buffer_[quartileSize*2];
else if(numQuartile == 3)
return buffer_[quartileSize*3];
return buffer_[quartileSize];
}
private:
std::vector<Sample> buffer_;
mutable bool isSorted;
};
} // impl
namespace tag
{
struct quartile : depends_on<>
{
typedef impl::quartile_accumulator<mpl::_1> impl;
};
}
namespace extract { extractor<tag::quartile> const quartile = {}; }
using extract::quartile;
}} // namespace boost::accumulators
-
// My desired syntax:
accumulator_set<double, stats<tag::quartile> > values;
// accumulate values
extract::quartile(values, 1); // 1st quartile
extract::quartile(values, 2); // median
extract::quartile(values, 3); // 3rd quartile
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了。我在定义类之前添加以下内容:
BOOST_PARAMETER_KEYWORD(tag, quartile_number)
然后我可以在结果和提取方法中使用该关键字。
I figured it out. I add this before defining the class:
BOOST_PARAMETER_KEYWORD(tag, quartile_number)
And then I can use that keyword in the result and extract method.