How do I use/access a user argument in a boost accumulator?
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
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.
精彩评论