1


我已经开始使用 boost:icl 库,它非常全面和方便。我主要使用 boost::icl::interval_set 和 boost::icl::interval_map 的两种类型的间隔。有时我必须仅使用两种类型的间隔部分进行操作,而且我对执行复制功能不感兴趣。

所以这是我的例子:

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() 当它是一个映射时是一个辅助函数,然后它返回 .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;
}

所以这个例子像预期的那样工作。我想要改进的是 getFirst() 函数,我不希望在 interval_map/set 中硬编码域和 codomain 类型。我试过这样的事情:

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

然后我改变了:

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

但它不起作用,编译器给出错误:

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>}’

所以问题是,在这个例子中是否有可能不在 getFirst() 函数中对域和 codomain 类型进行硬编码?

谢谢,安德烈

4

1 回答 1

3

您可以使用函数模板

key_value

为了这个目的。它定义在

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

像这样

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;
}
于 2012-09-26T21:17:18.233 回答