[icl] How to find whether a single day intersects a set of intervals?
*Hi folks* * * *I have the following use case: I want to store a series of time intervals in an icl::interval_set and then determine whether a single point in time intersects one of these intervals.* * * *Here is my first stab at this: * icl::interval_set<Time> _badMonths; // Time is an internal class which defines all comparison operators std::vector<int> badMonths; // populated with YYMM integers (eg: 1206 for June 2012) for (int m : badMonths) { Time openInterval = Time(2000 + m / 100, m % 100, 1); // 1st day of the month Time closeInterval = Time(2000 + m / 100, m % 100 + 1, 1) - TimeSpan::Days(1); // last day of the month icl::interval<Time>::type month(openInterval, closeInterval); _badMonths.insert(month); } // it is now my hope that _badMonths will contain a set of intervals where each interval is a month *How do I now check whether a single Time instance intersects one of the intervals in the set? * Time timepoint; if (_badMonths &= timepoint) std::cout << timepoint << " intersects bad months" << std::endl; TIA Steve
2012/8/20 Steve Lorimer
Hi folks
I have the following use case: I want to store a series of time intervals in an icl::interval_set and then determine whether a single point in time intersects one of these intervals.
Here is my first stab at this:
icl::interval_set<Time> _badMonths; // Time is an internal class which defines all comparison operators
std::vector<int> badMonths; // populated with YYMM integers (eg: 1206 for June 2012)
for (int m : badMonths) { Time openInterval = Time(2000 + m / 100, m % 100, 1); // 1st day of the month Time closeInterval = Time(2000 + m / 100, m % 100 + 1, 1) - TimeSpan::Days(1); // last day of the month
icl::interval<Time>::type month(openInterval, closeInterval);
_badMonths.insert(month); }
// it is now my hope that _badMonths will contain a set of intervals where each interval is a month
_badMonths now is a set that equals the union of all inserted intervals. Consecutive months will coalesce to larger intervals. If you want to preserve the interval borders you might use icl::separate_interval_set or icl::split_interval_set.
How do I now check whether a single Time instance intersects one of the intervals in the set?
Time timepoint; if (_badMonths &= timepoint) std::cout << timepoint << " intersects bad months" << std::endl;
if (_badMonths &= timepoint) ... //won't compile but you can use one of the following methods: if( icl::contains(_badMonths, timepoint) ){...} if( icl::within(timepoint, _badMonths) ){...} if( icl::intersects(_badMonths, timepoint) ){...} if( _badMonths.find(timepoint) != _badMonths.end() ){...} if( !icl::is_empty(_badMonths & timepoint) ){...} if( !(_badMonths &= timepoint).empty() ){...} For further information don't hesitate to consult the documentation ;-) E.g. http://www.boost.org/doc/libs/1_51_0/libs/icl/doc/html/boost_icl/interface/f... http://www.boost.org/doc/libs/1_51_0/libs/icl/doc/html/boost_icl/function_re... http://www.boost.org/doc/libs/1_51_0/libs/icl/doc/html/boost_icl/function_re... HTH, Joachim -- Interval Container Library [Boost.Icl] http://www.joachim-faulhaber.de
participants (2)
-
Joachim Faulhaber
-
Steve Lorimer