简体   繁体   English

boost::icl::interval 和 boost::numeric::interval 是否有可用的适配层?

[英]boost::icl::interval and boost::numeric::interval is there an adaption layer available?

I've been using boost::numeric::interval everywhere in my code.我一直在我的代码中到处使用boost::numeric::interval Now I've started using the boost interval container library .现在我已经开始使用boost interval 容器库了。 Does there exist an adaption header so I can put boost::numeric::interval intervals into boost::icl containers?是否存在适配 header 以便我可以将 boost::numeric::interval 间隔放入 boost::icl 容器中?

I've tried the following code as advised in the customization docs for boost::icl but it doesn't compile with many lines of template specialization failure messages.我已经按照 boost::icl 的定制文档中的建议尝试了以下代码,但它没有与多行模板专业化失败消息一起编译。 If you want to see the error messages you can try the live code at如果您想查看错误消息,可以在以下位置尝试实时代码

https://wandbox.org/permlink/P8VzcdbjQQzf43yU https://wandbox.org/permlink/P8VzcdbjQQzf43yU

The code I wrote is also below.我写的代码也在下面。

Adaption Code适配代码

#include <boost/icl/interval_set.hpp>
#include <boost/numeric/interval.hpp>

namespace boost{ namespace icl
{

// Class template interval_traits serves as adapter to register and customize your interval class
template<typename T>
struct interval_traits< boost::numeric::interval<T> >       
{                                                          

    typedef boost::numeric::interval<T>     interval_type;
    typedef T                               domain_type;                    
    typedef std::less<T>                    domain_compare;              

    static interval_type construct(const domain_type& lo, const domain_type& up)
    { return interval_type(lo, up); }
                                                            //3.2 Selection of values 
    static domain_type lower(const interval_type& inter_val){ return inter_val.lower(); };
    static domain_type upper(const interval_type& inter_val){ return inter_val.upper(); };
};


template<typename T>
struct interval_bound_type<boost::numeric::interval<T> >                                   //4.  Finally we define the interval borders.
{                                                                                          //    Choose between static_open         (lo..up)
    typedef interval_bound_type type;                                                      //                   static_left_open    (lo..up]
    BOOST_STATIC_CONSTANT(bound_type, value = boost::icl::interval_bounds::static_closed);
};                                                                                         //               and static_closed       [lo..up] 

}} // namespace boost icl

Test Code测试代码

int main()
{
    boost::numeric::interval<double> i = boost::numeric::hull(1.0,2.0);
    boost::icl::interval_set<double, std::less, boost::numeric::interval<double>> iSet;
    iSet.insert(i);
}

The solution is to use either解决方案是使用

boost::icl::interval_bounds::static_left_open
boost::icl::interval_bounds::static_right_open

like below像下面

template<typename T>
struct interval_bound_type<boost::numeric::interval<T> >                                   
{                                                                                          
    typedef interval_bound_type type;                                                      
    BOOST_STATIC_CONSTANT(bound_type, value = 
          boost::icl::interval_bounds::static_left_open);
};    

when definining the interval type.定义区间类型时。 The other options on interval_bounds do not work. interval_bounds 上的其他选项不起作用。

See https://wandbox.org/permlink/lP2Ddl3NaLs9Cn7b for a working example有关工作示例,请参见https://wandbox.org/permlink/lP2Ddl3NaLs9Cn7b

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

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