boost interval_map vs split_interval_map



我很难理解interval_map &split_interval_map,我实现了&结果是一样的。下面是分割间隔映射的代码。

#include <iostream>
#include <boost/icl/split_interval_map.hpp>
#include <boost/icl/interval_map.hpp>
using namespace std;
using namespace boost::icl;

/* The most simple example of an interval_map is an overlap counter.
   If intervals are added that are associated with the value 1,
   all overlaps of added intervals are counted as a result in the
   associated values. 
*/
typedef split_interval_map<int, int> OverlapCounterT;
void print_overlaps(const OverlapCounterT& counter)
{
    for(OverlapCounterT::const_iterator it = counter.begin(); it != counter.end(); it++)
    {
        discrete_interval<int> itv  = (*it).first;
        int overlaps_count = (*it).second;
        if(overlaps_count == 1)
            cout << "in interval " << itv << " intervals do not overlap" << endl;
        else
            cout << "in interval " << itv << ": "<< overlaps_count << " intervals overlap" << endl;
    }
}
void overlap_counter()
{
    OverlapCounterT overlap_counter;
    discrete_interval<int> inter_val;
    inter_val = discrete_interval<int>::right_open(4,9);
    cout << "adding   " << inter_val << endl;
    overlap_counter += make_pair(inter_val, 1);
    print_overlaps(overlap_counter);
    inter_val = discrete_interval<int>::right_open(6,9);
    cout << "adding   " << inter_val << endl;
    overlap_counter += make_pair(inter_val, 1);
    print_overlaps(overlap_counter);

    inter_val = discrete_interval<int>::right_open(1,9);
    cout << "adding   " << inter_val << endl;
    overlap_counter += make_pair(inter_val, 1);
    print_overlaps(overlap_counter);

}
int main()
{
    cout << ">>Interval Container Library: Sample overlap_counter.cpp <<n";
    cout << "-----------------------------------------------------------n";
    overlap_counter();
    return 0;
}

输出为:

adding   [4,9)
in interval [4,9) intervals do not overlap
adding   [6,9)
in interval [4,6) intervals do not overlap
in interval [6,9): 2 intervals overlap
adding   [1,9)
in interval [1,4) intervals do not overlap
in interval [4,6): 2 intervals overlap
in interval [6,9): 3 intervals overlap

同样,在上面的代码中,我将split_interval_map更改为interval_map &输出是一样的。我是不是漏掉了什么?

当您插入紧挨着的间隔时,差异变得明显。"法线"映射会合并它们,分裂不会(所以它保留所有的间隔边界)。

参见相关文档:组合样式

示例:

Live On Coliru

#include <iostream>
#include <boost/icl/split_interval_map.hpp>
#include <boost/icl/interval_map.hpp>
namespace icl = boost::icl;
template <typename Map> void test() {
    std::cout << __PRETTY_FUNCTION__ << ":n";
    Map s { { Map::interval_type::right_open(2,10), 3 } };
    std::cout << s << "n";
    s.add({ {10,12}, 3 });
    std::cout << s << "n";
}
int main()
{
    test<icl::interval_map<int, int> >();
    test<icl::split_interval_map<int, int> >();
}

打印

void test() [with Map = boost::icl::interval_map<int, int>]:
{([2,10)->3)}
{([2,12)->3)}
void test() [with Map = boost::icl::split_interval_map<int, int>]:
{([2,10)->3)}
{([2,10)->3)([10,12)->3)}

相关内容

  • 没有找到相关文章

最新更新