c++boost-iclboost-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. Now I've started using the boost interval container library. Does there exist an adaption header so I can put boost::numeric::interval intervals into boost::icl containers?

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. If you want to see the error messages you can try the live code at

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


Solution

  • 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.

    See https://wandbox.org/permlink/lP2Ddl3NaLs9Cn7b for a working example