Определение<operator+=
>(и<-=
>), вероятно, является наиболее важным методом реализации произвольных типов агрегации, определенных пользователем. Альтернативным способом выбора желаемой агрегации является инстанцирование шаблона класса интервал_map с соответствующимагрегация функтора. Для наиболее распространенных видов агрегацииiclпредоставляет такие функторы, как показано в примере.
Пример<partys_tallest_guests.cpp
>также демонстрирует разницу между<interval_map
>, который соединяет интервалы для равных связанных значений, и<split_interval_map
>, который сохраняет все границы вставленных интервалов.
#include <boost/icl/ptime.hpp>
#include <iostream>
#include <boost/icl/interval_map.hpp>
#include <boost/icl/split_interval_map.hpp>
using namespace std;
using namespace boost::posix_time;
using namespace boost::icl;
typedef interval_map<ptime, int, partial_absorber, less, inplace_max>
PartyHeightHistoryT;
typedef split_interval_map<ptime, int, partial_absorber, less, inplace_max>
PartyHeightSplitHistoryT;
void partys_height()
{
PartyHeightHistoryT tallest_guest;
tallest_guest +=
make_pair(
discrete_interval<ptime>::right_open(
time_from_string("2008-05-20 19:30"),
time_from_string("2008-05-20 23:00")),
180);
tallest_guest +=
make_pair(
discrete_interval<ptime>::right_open(
time_from_string("2008-05-20 20:10"),
time_from_string("2008-05-21 00:00")),
170);
tallest_guest +=
make_pair(
discrete_interval<ptime>::right_open(
time_from_string("2008-05-20 22:15"),
time_from_string("2008-05-21 00:30")),
200);
PartyHeightHistoryT::iterator height_ = tallest_guest.begin();
cout << "-------------- History of maximum guest height -------------------\n";
while(height_ != tallest_guest.end())
{
discrete_interval<ptime> when = height_->first;
int height = (*height_++).second;
cout << "[" << first(when) << " - " << upper(when) << ")"
<< ": " << height <<" cm = " << height/30.48 << " ft" << endl;
}
}
void partys_split_height()
{
PartyHeightSplitHistoryT tallest_guest;
tallest_guest +=
make_pair(
discrete_interval<ptime>::right_open(
time_from_string("2008-05-20 19:30"),
time_from_string("2008-05-20 23:00")),
180);
tallest_guest +=
make_pair(
discrete_interval<ptime>::right_open(
time_from_string("2008-05-20 20:10"),
time_from_string("2008-05-21 00:00")),
170);
tallest_guest +=
make_pair(
discrete_interval<ptime>::right_open(
time_from_string("2008-05-20 22:15"),
time_from_string("2008-05-21 00:30")),
200);
PartyHeightSplitHistoryT::iterator height_ = tallest_guest.begin();
cout << "\n";
cout << "-------- Split History of maximum guest height -------------------\n";
cout << "--- Same map as above but split for every interval insertion. ---\n";
while(height_ != tallest_guest.end())
{
discrete_interval<ptime> when = height_->first;
int height = (*height_++).second;
cout << "[" << first(when) << " - " << upper(when) << ")"
<< ": " << height <<" cm = " << height/30.48 << " ft" << endl;
}
}
int main()
{
cout << ">>Interval Container Library: Sample partys_tallest_guests.cpp <<\n";
cout << "------------------------------------------------------------------\n";
partys_height();
partys_split_height();
return 0;
}