【问题标题】:boost::icl::interval and boost::numeric::interval is there an adaption layer available?boost::icl::interval 和 boost::numeric::interval 是否有可用的适配层?
【发布时间】:2019-10-15 23:06:53
【问题描述】:

我一直在我的代码中到处使用boost::numeric::interval。现在我开始使用boost interval container library。是否存在适配标头,因此我可以将 boost::numeric::interval 间隔放入 boost::icl 容器中?

我已经按照customization docs for boost::icl 中的建议尝试了以下代码,但它无法通过多行模板专业化失败消息进行编译。如果您想查看错误消息,可以在

尝试实时代码

https://wandbox.org/permlink/P8VzcdbjQQzf43yU

我写的代码也在下面。

适配代码

#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

测试代码

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);
}

【问题讨论】:

    标签: c++ boost-icl boost-interval


    【解决方案1】:

    解决方案是使用任一

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

    如下图

    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);
    };    
    

    在定义区间类型时。 interval_bounds 上的其他选项不起作用。

    有关工作示例,请参阅 https://wandbox.org/permlink/lP2Ddl3NaLs9Cn7b

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      相关资源
      最近更新 更多