diff --git a/include/stdex/interval.hpp b/include/stdex/interval.hpp index d3d2be61b..d529523d6 100644 --- a/include/stdex/interval.hpp +++ b/include/stdex/interval.hpp @@ -227,6 +227,54 @@ namespace stdex { return !operator ==(other); } + + /// + /// Extends the interval to another interval + /// + /// \note Makes a union of two intervals, so it covers [min(start, other.start), max(end, other.end)). + /// When either of the intervals is empty, the result is the non-empty one. + /// When both intervals are empty, the result is an empty interval. + /// + /// \param[in] other Interval to extend to + /// + /// \returns Resulting interval + /// + interval extend(_In_ const interval& other) + { + if (other.empty()) + return *this; + if (empty()) + return *this = other; + if (other.start < start) + start = other.start; + if (end < other.end) + end = other.end; + return *this; + } + + /// + /// Intersects the interval with another interval + /// + /// \note Makes an intersection of two intervals, so it covers [max(start, other.start), min(end, other.end)). + /// When intervals do not interlap, the result is an invalid interval. + /// When any of the intervals is empty, the result is an invalid (and empty by definition) interval. + /// + /// \param[in] other Interval to intersect with + /// + /// \returns Resulting interval + /// + interval intersect(_In_ const interval& other) + { + if (empty() || other.empty()) { + invalidate(); + return *this; + } + if (start < other.start) + start = other.start; + if (other.end < end) + end = other.end; + return *this; + } }; template >>