Range: Added ::combine for overlapping range combinations

This commit is contained in:
Paul Beckingham 2016-06-01 07:32:22 -04:00
parent 9b4a70ea7b
commit d5b7afd008
2 changed files with 38 additions and 0 deletions

View file

@ -214,6 +214,43 @@ Range Range::intersect (const Range& other) const
return Range (Datetime (0), Datetime (0));
}
////////////////////////////////////////////////////////////////////////////////
// If the ranges do not overlap, the result is *this.
//
// this [----)
// other [----)
// result [-------)
//
// this [...
// other [----)
// result [...
//
// this [----)
// other [...
// result [...
//
// this [...
// other [...
// result [...
//
Range Range::combine (const Range& other) const
{
Range result {*this};
if (is_started () && other.is_started ())
{
// Start is hte earlier of the two.
result.start = std::min (result.start, other.start);
if (is_open () || other.is_open ())
result.end = {0};
else
result.end = std::max (result.end, other.end);
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
// Consider the following overlap cases:
//

View file

@ -48,6 +48,7 @@ public:
bool overlap (const Range&) const;
bool encloses (const Range&) const;
Range intersect (const Range&) const;
Range combine (const Range&) const;
std::vector <Range> subtract (const Range&) const;
time_t total () const;