简体   繁体   English

升压累加器是否支持计算相邻值之间的最小差异?

[英]Do boost accumulators support the calculation of minimum difference between adjacent values?

Assumption here is that I am receiving many values and I do not want to store them in the vector.这里的假设是我收到了很多值,我不想将它们存储在向量中。 So I would like to use something like boost accumulators.所以我想使用像升压累加器这样的东西。 But in docs I can not find something like the logic I want.但是在文档中我找不到我想要的逻辑。

Is there a way for me to have accumulator so that when called with有没有办法让我拥有累加器,以便在调用时

5, 10 , 12 , 15 , 27 it will output 2(minimal difference between two adjacent values, 10 and 12). 5, 10 , 12 , 15 , 27 它将输出 2(两个相邻值之间的最小差异,10 和 12)。

I know I can keep the last variable by myself and just use the acc(current-last) with tag::min, but I prefer to leave that to library if possible.我知道我可以自己保留最后一个变量,只需将 acc(current-last) 与 tag::min 一起使用,但如果可能,我更愿意将其留给库。

Not with the accumulators that are part of Boost, but you can write your own.不是使用属于 Boost 一部分的累加器,但您可以编写自己的累加器。

The accumulator itself will look something like this.累加器本身看起来像这样。 Note that this version only handles an increasing sequence of values correctly, but does not required an initial sample.请注意,此版本仅正确处理递增的值序列,但不需要初始样本。 If you have different requirements you can tune this.如果你有不同的要求,你可以调整它。

namespace boost {                           
namespace accumulators {                    
namespace impl {                            

template<typename Sample>
struct min_adjacent_difference_accumulator  
  : accumulator_base                        
{
    using result_type = Sample;
   
    template<typename Args>                 
    min_adjacent_difference_accumulator(Args const & args)
      : last_seen(args[sample | std::optional<Sample>{}])        
    {                                       
    }                                       
   
    template<typename Args>                 
    void operator ()(Args const & args)     
    {
        Sample new_value = args[sample];
        if (last_seen)
            min_diff = std::min(min_diff, new_value - *last_seen);
                                  
        last_seen = args[sample];
    }

    result_type result(dont_care) const     
    {                                       
        return min_diff;                   
    }
 private:
    std::optional<Sample> last_seen;
    Sample min_diff = std::numeric_limits<Sample>::max();
 };

 }}}

Putting it into the boost::accumulators::impl namespace is recommended in the documentation.文档中建议将其放入boost::accumulators::impl命名空间。

Next, we need a tag接下来,我们需要一个标签

namespace boost { namespace accumulators { namespace tag {

struct min_adjacent_difference                         
  : depends_on<>
{
    using impl = accumulators::impl::min_adjacent_difference_accumulator< mpl::_1 >;
};

}}}

and an extractor和一个提取器

namespace boost {
namespace accumulators {               
namespace extract {                    

inline extractor<tag::min_adjacent_difference> const min_adjacent_difference = {}; 
                                    
}
using extract::min_adjacent_difference;                    
                                    
}}

You can then use it like any of the accumulators provided by boost.然后,您可以像使用 boost 提供的任何累加器一样使用它。 You can check out an interactive version on compiler explorer .您可以在compiler explorer上查看交互式版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM