From 010ee71d9345564614e3de0bc6c234fcd7f9a834 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Sun, 1 Oct 2023 23:16:36 +0200 Subject: [PATCH] interval: make operators a class friend This makes them better discoverable by MSVC from various non-root namespaces. Signed-off-by: Simon Rozman --- include/stdex/interval.hpp | 268 +++++++++++++++++-------------------- 1 file changed, 122 insertions(+), 146 deletions(-) diff --git a/include/stdex/interval.hpp b/include/stdex/interval.hpp index 890a59ba2..ba526315f 100644 --- a/include/stdex/interval.hpp +++ b/include/stdex/interval.hpp @@ -68,154 +68,130 @@ namespace stdex /// \returns true if x is in [start, end) or false otherwise /// inline bool contains(_In_ T x) const { return start <= x && x < end; } + + /// + /// Adds two intervals by components + /// + /// \param[in] other Second interval + /// + /// \returns Resulting interval + /// + inline interval operator+(_In_ const interval& other) const + { + return interval(start + other.start, end + other.end);i. + } + + /// + /// Moves interval towards the end by a number + /// + /// \param[in] x Amount to move for + /// + /// \returns Moved interval + /// + inline interval operator+(_In_ const T x) const + { + return interval(start + x, end + x); + } + + /// + /// Moves interval towards the end by one + /// + /// \returns Moved interval + /// + inline interval operator++() + { + ++start; + ++end; + return *this; + } + + /// + /// Moves interval towards the end by one + /// + /// \returns Original interval + /// + inline interval operator++(int) // Postfix increment operator. + { + interval r = *this; + ++start; + ++end; + return r; + } + + /// + /// Subtracts two intervals by components + /// + /// \param[in] other Second interval + /// + /// \returns Resulting interval + /// + inline interval operator-(_In_ const interval& other) const + { + return interval(start - other.start, end - other.end); + } + + /// + /// Moves interval towards the beginning by a number + /// + /// \param[in] x Amount to move for + /// + /// \returns Moved interval + /// + inline interval operator-(_In_ const T x) const + { + return interval(start - x, end - x); + } + + /// + /// Moves interval towards the begginning by one + /// + /// \returns Moved interval + /// + inline interval operator--() + { + --start; + --end; + return *this; + } + + /// + /// Moves interval towards the begginning by one + /// + /// \returns Original interval + /// + inline interval operator--(int) // Postfix decrement operator. + { + interval r = *this; + --start; + --end; + return r; + } + + /// + /// Are intervals identical? + /// + /// \param[in] other Second interval to compare + /// + /// \returns true if intervals are identical or false otherwise + /// + inline bool operator==(_In_ const interval& other) const + { + return start == other.start && end == other.end; + } + + /// + /// Are intervals different? + /// + /// \param[in] other Second interval to compare + /// + /// \returns true if intervals are different or false otherwise + /// + inline bool operator!=(_In_ const interval& other) const + { + return !operator ==(other); + } }; template >> using interval_vector = std::vector, _Alloc>; } - -/// -/// Adds two intervals by components -/// -/// \param[in] a First interval -/// \param[in] b 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,out] 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,out] 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] b 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,out] 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,out] 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; -}