Я испытываю трудности с пониманием 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 & вывод был такой же. Есть что-то, чего мне не хватает?
Разница становится очевидной, когда вы вставляете интервалы, которые находятся рядом. «Обычная» карта объединит их, расщепление не сделает (поэтому она сохраняет все границы интервалов).
Смотрите также соответствующие документы: Объединение Стилей
Образец:
#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)}