sfizz/sources/Range.h
2019-08-07 23:39:35 +02:00

83 lines
No EOL
2.3 KiB
C++

#pragma once
#include <type_traits>
#include <initializer_list>
#include <algorithm>
template<class Type>
class Range
{
static_assert(std::is_arithmetic<Type>::value, "The Type should be arithmetic");
public:
constexpr Range() = default;
// constexpr Range(std::initializer_list<Type> list)
// {
// switch(list.size())
// {
// case 0:
// break;
// case 1:
// _start = *list.begin();
// _end = _start;
// break;
// default:
// _start = *list.begin();
// _end = *(list.begin() + 1);
// }
// }
constexpr Range(Type start, Type end) noexcept
: _start(start), _end(std::max(start, end)) {}
~Range() = default;
Type getStart() const noexcept { return _start; }
Type getEnd() const noexcept { return _end; }
std::pair<Type, Type> getPair() const noexcept { return std::make_pair<Type, Type>(_start, _end); }
Range(const Range<Type>& range) = default;
Range(Range<Type>&& range) = default;
void setStart(Type start) noexcept
{
_start = start;
if (start > _end)
_end = start;
}
void setEnd(Type end) noexcept
{
_end = end;
if (end < _start)
_start = end;
}
Type clamp(Type value) const noexcept { return std::clamp(value, _start, _end); }
bool containsWithEnd(Type value) const noexcept { return (value >= _start && value <= _end); }
bool contains(Type value) const noexcept { return (value >= _start && value < _end); }
void shrinkIfSmaller( Type start, Type end)
{
if (start > end)
std::swap(start, end);
if (start > _start)
_start = start;
if (end < _end)
_end = end;
}
private:
Type _start { static_cast<Type>(0.0) };
Type _end { static_cast<Type>(0.0) };
};
template<class Type>
bool operator==(const Range<Type>& lhs, const Range<Type>& rhs)
{
return (lhs.getStart() == rhs.getStart()) && (lhs.getEnd() == rhs.getEnd());
}
template<class Type>
bool operator==(const Range<Type>& lhs, const std::pair<Type, Type>& rhs)
{
return (lhs.getStart() == rhs.first) && (lhs.getEnd() == rhs.second);
}
template<class Type>
bool operator==(const std::pair<Type, Type>& lhs, const Range<Type>& rhs)
{
return rhs == lhs;
}