简体   繁体   English

boost icl interval_map interval_set templates

[英]boost icl interval_map interval_set templates


I've started work with boost:icl library which is really comprehensive and convenient. 我已经开始使用boost:icl库,它非常全面和方便。 I'm using mostly two types of intervals from boost, boost::icl::interval_set and boost::icl::interval_map. 我主要使用boost,boost :: icl :: interval_set和boost :: icl :: interval_map两种类型的间隔。 Some times I have to manipulate with just interval part of both types, and I don't interesting in doing replicate functions. 有时我必须只使用两种类型的间隔部分进行操作,而且我没有兴趣进行复制功能。

So here is my example: 所以这是我的例子:

template<typename T>
void UniqSegmentsInA(T &a,T &b,T &c,int max_gap=200)
{
typename T::iterator __it1 = a.begin();
typename T::iterator __it2 = b.begin();
bool _checking_=true;

while(__it1 != a.end() && __it2 != b.end())
{
    typename T::interval_type __itv1  = getFirst(*__it1);
    typename T::interval_type __itv2  = getFirst(*__it2);

    if(__itv1.upper()>__itv2.upper())
    {
        /*if segments intersect and we should move second segment then segment A should not be included in output*/
        if(intersects(T::interval_type::closed(__itv1.lower()-max_gap,__itv1.upper()+max_gap),__itv2)) _checking_=false;
        __it2++;
    }
    else
    {
        if(!intersects(T::interval_type::closed(__itv1.lower()-max_gap,__itv1.upper()+max_gap),__itv2) && _checking_)
        {
            c.add((*__it1));
        }
        _checking_=true;
        __it1++;
    }
}
if(__it1 != a.end() && !_checking_) __it1++;
while(__it1 != a.end())
{
    c.add(*__it1);
    __it1++;
}
}

getFirst() is a helper function when it is a map then it returns .first when it is a set then just an iterator. getFirst()是一个辅助函数,当它是一个映射然后它返回.first它是一个集合然后只是一个迭代器。

bicl::interval_map<int,int>::interval_type inline getFirst(bicl::interval_map<int,int>::segment_type a)
{
return a.first;
}

bicl::interval_set<int>::interval_type inline getFirst(bicl::interval_set<int>::segment_type a)
{
return a;
}

So this example works like expected. 所以这个例子就像预期的那样。 What I want to improve is getFirst() function and I don't want hardcode types of domain and codomain in interval_map/set. 我想改进的是getFirst()函数,我不想在interval_map / set中使用域和codomain的硬编码类型。 I've tried something like this: 我尝试过这样的事情:

template<typename T>
typename bicl::interval_map<T,T>::interval_type inline getFirst(typename bicl::interval_map<T,T>::segment_type a)
{
return a.first;
}

Then I changed: 然后我改变了:

    typename T::interval_type __itv1  = getFirst<typename T::domain_type>(*__it1);
    typename T::interval_type __itv2  = getFirst<typename T::domain_type>(*__it2);

but it does not work and compiler gives an error: 但它不起作用,编译器给出错误:

src/Intersections.cpp:324:35: error: no matching function for call to ‘getFirst(const std::pair<const boost::icl::discrete_interval<int, std::less>, int>&)’
src/Intersections.cpp:324:35: note: candidates are:
src/Intersections.cpp:52:56: note: template<class T> typename boost::icl::interval_map<T, T>::interval_type getFirst(typename boost::icl::interval_map<T, T>::segment_type)
src/Intersections.cpp:57:56: note: boost::icl::interval_set<int>::interval_type getFirst(boost::icl::interval_set<int>::segment_type)
src/Intersections.cpp:57:56: note:   no known conversion for argument 1 from ‘const std::pair<const boost::icl::discrete_interval<int, std::less>, int>’ to ‘boost::icl::interval_set<int>::segment_type {aka boost::icl::discrete_interval<int, std::less>}’

So question is, is it possible in this example do not hardcode domain and codomain types in getFirst() function? 所以问题是,在这个例子中是否有可能不在getFirst()函数中硬编码域和codomain类型?

Thanks, Andrey 谢谢,安德烈

You can use the function template 您可以使用功能模板

key_value

for that purpose. 为了这个目的。 It is defined in 它的定义是

boost\icl\concept\set_value.hpp
and
boost\icl\concept\map_value.hpp

like this 像这样

template<class Type, class Iterator>
inline typename enable_if<is_set<Type>, const typename Type::key_type>::type&
key_value(Iterator it_)
{
    return *it_;
}

template<class Type, class Iterator>
inline typename enable_if<is_map<Type>, const typename Type::key_type>::type&
key_value(Iterator it_)
{
    return (*it_).first;
}

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

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