1

我有一个自定义累加器的要点。我想知道如何从“参数包”中获取整数参数,或者这是否可能:

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
4

1 回答 1

1

我想到了。我在定义类之前添加了这个:BOOST_PARAMETER_KEYWORD(tag, quartile_number)

然后我可以在结果和提取方法中使用该关键字。

于 2011-09-13T15:43:21.663 回答