diff --git a/include/stdex/interval.hpp b/include/stdex/interval.hpp index c4c7fa84e..1a2b760fc 100644 --- a/include/stdex/interval.hpp +++ b/include/stdex/interval.hpp @@ -60,24 +60,6 @@ namespace stdex /// inline operator bool() const { return start <= end; } - /// - /// Are intervals identical? - /// - /// \param[in] other Other interval to compare against - /// - /// \returns true if intervals are identical or false otherwise - /// - inline bool operator==(const interval& other) const { return start == other.start && end == other.end; } - - /// - /// Are intervals different? - /// - /// \param[in] other Other interval to compare against - /// - /// \returns true if intervals are different or false otherwise - /// - inline bool operator!=(const interval& other) const { return !operator==(other); } - /// /// Is value in interval? /// @@ -91,3 +73,149 @@ namespace stdex template >> using interval_vector = std::vector, _Alloc>; } + +/// +/// Adds two intervals by components +/// +/// \param[in] a First interval +/// \param[in] a Second interval +/// +/// \returns Resulting interval +/// +template +inline stdex::interval operator+(_In_ const stdex::interval& a, _In_ const stdex::interval& b) +{ + return stdex::interval(a.start + b.start, a.end + b.end); +} + +/// +/// Moves interval towards the end by a number +/// +/// \param[in] i Interval to move +/// \param[in] x Amount to move for +/// +/// \returns Moved interval +/// +template +inline stdex::interval operator+(_In_ const stdex::interval& i, _In_ const T x) +{ + return stdex::interval(i.start + x, i.end + x); +} + +/// +/// Moves interval towards the end by one +/// +/// \param[in] i Interval to move +/// +/// \returns Moved interval +/// +template +inline stdex::interval operator++(_Inout_ stdex::interval& i) +{ + ++i.start; + ++i.end; + return i; +} + +/// +/// Moves interval towards the end by one +/// +/// \param[in] i Interval to move +/// +/// \returns Original interval +/// +template +inline stdex::interval operator++(_Inout_ stdex::interval& i, int) // Postfix increment operator. +{ + stdex::interval r = i; + ++i.start; + ++i.end; + return r; +} + +/// +/// Subtracts two intervals by components +/// +/// \param[in] a First interval +/// \param[in] a Second interval +/// +/// \returns Resulting interval +/// +template +inline stdex::interval operator-(_In_ const stdex::interval& a, _In_ const stdex::interval& b) +{ + return stdex::interval(a.start - b.start, a.end - b.end); +} + +/// +/// Moves interval towards the beginning by a number +/// +/// \param[in] i Interval to move +/// \param[in] x Amount to move for +/// +/// \returns Moved interval +/// +template +inline stdex::interval operator-(_In_ const stdex::interval& i, _In_ const T x) +{ + return stdex::interval(i.start - x, i.end - x); +} + +/// +/// Moves interval towards the begginning by one +/// +/// \param[in] i Interval to move +/// +/// \returns Moved interval +/// +template +inline stdex::interval operator--(_Inout_ stdex::interval& i) +{ + --i.start; + --i.end; + return i; +} + +/// +/// Moves interval towards the begginning by one +/// +/// \param[in] i Interval to move +/// +/// \returns Original interval +/// +template +inline stdex::interval operator--(_Inout_ stdex::interval& i, int) +{ + stdex::interval r = i; + --i.start; + --i.end; + return r; +} + +/// +/// Are intervals identical? +/// +/// \param[in] a First interval to compare +/// \param[in] b Second interval to compare +/// +/// \returns true if intervals are identical or false otherwise +/// +template +inline bool operator==(_In_ const stdex::interval& a, _In_ const stdex::interval& b) +{ + return a.start == b.start && a.end == b.end; +} + +/// +/// Are intervals different? +/// +/// \param[in] a First interval to compare +/// \param[in] b Second interval to compare +/// +/// \returns true if intervals are different or false otherwise +/// +template +inline bool operator!=(_In_ const stdex::interval& a, _In_ const stdex::interval& b) +{ + return a.start != b.start || a.end != b.end; +}