111 lines
No EOL
3.7 KiB
C++
111 lines
No EOL
3.7 KiB
C++
// Copyright (c) 2019, Paul Ferrand
|
|
// All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
|
// list of conditions and the following disclaimer.
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
#pragma once
|
|
#include <algorithm>
|
|
#include <initializer_list>
|
|
#include <type_traits>
|
|
|
|
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;
|
|
constexpr Type length() const { return _end - _start; }
|
|
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;
|
|
} |