interval: extend arithmetic
Additional operators allow moving intervals left and right. Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
parent
5558bd6ccc
commit
a13aba304a
@ -60,24 +60,6 @@ namespace stdex
|
|||||||
///
|
///
|
||||||
inline operator bool() const { return start <= end; }
|
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?
|
/// Is value in interval?
|
||||||
///
|
///
|
||||||
@ -91,3 +73,149 @@ namespace stdex
|
|||||||
template <class T, class _Alloc = std::allocator<interval<T>>>
|
template <class T, class _Alloc = std::allocator<interval<T>>>
|
||||||
using interval_vector = std::vector<interval<T>, _Alloc>;
|
using interval_vector = std::vector<interval<T>, _Alloc>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Adds two intervals by components
|
||||||
|
///
|
||||||
|
/// \param[in] a First interval
|
||||||
|
/// \param[in] a Second interval
|
||||||
|
///
|
||||||
|
/// \returns Resulting interval
|
||||||
|
///
|
||||||
|
template <class T>
|
||||||
|
inline stdex::interval<T> operator+(_In_ const stdex::interval<T>& a, _In_ const stdex::interval<T>& b)
|
||||||
|
{
|
||||||
|
return stdex::interval<T>(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 <class T>
|
||||||
|
inline stdex::interval<T> operator+(_In_ const stdex::interval<T>& i, _In_ const T x)
|
||||||
|
{
|
||||||
|
return stdex::interval<T>(i.start + x, i.end + x);
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Moves interval towards the end by one
|
||||||
|
///
|
||||||
|
/// \param[in] i Interval to move
|
||||||
|
///
|
||||||
|
/// \returns Moved interval
|
||||||
|
///
|
||||||
|
template <class T>
|
||||||
|
inline stdex::interval<T> operator++(_Inout_ stdex::interval<T>& i)
|
||||||
|
{
|
||||||
|
++i.start;
|
||||||
|
++i.end;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Moves interval towards the end by one
|
||||||
|
///
|
||||||
|
/// \param[in] i Interval to move
|
||||||
|
///
|
||||||
|
/// \returns Original interval
|
||||||
|
///
|
||||||
|
template <class T>
|
||||||
|
inline stdex::interval<T> operator++(_Inout_ stdex::interval<T>& i, int) // Postfix increment operator.
|
||||||
|
{
|
||||||
|
stdex::interval<T> 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 <class T>
|
||||||
|
inline stdex::interval<T> operator-(_In_ const stdex::interval<T>& a, _In_ const stdex::interval<T>& b)
|
||||||
|
{
|
||||||
|
return stdex::interval<T>(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 <class T>
|
||||||
|
inline stdex::interval<T> operator-(_In_ const stdex::interval<T>& i, _In_ const T x)
|
||||||
|
{
|
||||||
|
return stdex::interval<T>(i.start - x, i.end - x);
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Moves interval towards the begginning by one
|
||||||
|
///
|
||||||
|
/// \param[in] i Interval to move
|
||||||
|
///
|
||||||
|
/// \returns Moved interval
|
||||||
|
///
|
||||||
|
template <class T>
|
||||||
|
inline stdex::interval<T> operator--(_Inout_ stdex::interval<T>& i)
|
||||||
|
{
|
||||||
|
--i.start;
|
||||||
|
--i.end;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Moves interval towards the begginning by one
|
||||||
|
///
|
||||||
|
/// \param[in] i Interval to move
|
||||||
|
///
|
||||||
|
/// \returns Original interval
|
||||||
|
///
|
||||||
|
template <class T>
|
||||||
|
inline stdex::interval<T> operator--(_Inout_ stdex::interval<T>& i, int)
|
||||||
|
{
|
||||||
|
stdex::interval<T> 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 <class T>
|
||||||
|
inline bool operator==(_In_ const stdex::interval<T>& a, _In_ const stdex::interval<T>& 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 <class T>
|
||||||
|
inline bool operator!=(_In_ const stdex::interval<T>& a, _In_ const stdex::interval<T>& b)
|
||||||
|
{
|
||||||
|
return a.start != b.start || a.end != b.end;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user