Loading...
Searching...
No Matches
interval.hpp
/*
SPDX-License-Identifier: MIT
Copyright © 2023 Amebis
*/
#pragma once
#include "compat.hpp"
#include <vector>
namespace stdex
{
template <class T>
struct interval
{
T start;
T end;
inline interval() noexcept : start(static_cast<T>(1)), end(static_cast<T>(0)) {}
inline interval(_In_ T x) noexcept : start(x), end(x) {}
inline interval(_In_ T _start, _In_ T _end) noexcept : start(_start), end(_end) {}
inline T size() const { return start <= end ? end - start : 0; }
inline bool empty() const { return start >= end; }
inline void invalidate()
{
start = static_cast<T>(1);
end = static_cast<T>(0);
}
inline operator bool() const { return start <= end; }
inline bool contains(_In_ T x) const { return start <= x && x < end; }
inline interval<T> operator+(_In_ const interval<T>& other) const
{
return interval<T>(start + other.start, end + other.end);
}
inline interval<T> operator+(_In_ const T x) const
{
return interval<T>(start + x, end + x);
}
inline interval<T> operator++()
{
++start;
++end;
return *this;
}
inline interval<T> operator++(int) // Postfix increment operator.
{
interval<T> r = *this;
++start;
++end;
return r;
}
inline interval<T> operator-(_In_ const interval<T>& other) const
{
return interval<T>(start - other.start, end - other.end);
}
inline interval<T> operator-(_In_ const T x) const
{
return interval<T>(start - x, end - x);
}
inline interval<T> operator--()
{
--start;
--end;
return *this;
}
inline interval<T> operator--(int) // Postfix decrement operator.
{
interval<T> r = *this;
--start;
--end;
return r;
}
inline bool operator==(_In_ const interval<T>& other) const
{
return start == other.start && end == other.end;
}
inline bool operator!=(_In_ const interval<T>& other) const
{
return !operator ==(other);
}
};
template <class T, class _Alloc = std::allocator<interval<T>>>
using interval_vector = std::vector<interval<T>, _Alloc>;
}
stdex::interval
Numerical interval.
Definition interval.hpp:18
stdex::interval::interval
interval(T x) noexcept
Constructs a zero-size interval.
Definition interval.hpp:32
stdex::interval::operator-
interval< T > operator-(const T x) const
Moves interval towards the beginning by a number.
Definition interval.hpp:149
stdex::interval::contains
bool contains(T x) const
Is value in interval?
Definition interval.hpp:79
stdex::interval::operator++
interval< T > operator++(int)
Moves interval towards the end by one.
Definition interval.hpp:122
stdex::interval::empty
bool empty() const
Is interval empty?
Definition interval.hpp:54
stdex::interval::interval
interval(T _start, T _end) noexcept
Constructs an interval.
Definition interval.hpp:40
stdex::interval::operator+
interval< T > operator+(const T x) const
Moves interval towards the end by a number.
Definition interval.hpp:100
stdex::interval::operator+
interval< T > operator+(const interval< T > &other) const
Adds two intervals by components.
Definition interval.hpp:88
stdex::interval::size
T size() const
Returns interval size.
Definition interval.hpp:47
stdex::interval::end
T end
interval end
Definition interval.hpp:20
stdex::interval::operator==
bool operator==(const interval< T > &other) const
Are intervals identical?
Definition interval.hpp:186
stdex::interval::operator++
interval< T > operator++()
Moves interval towards the end by one.
Definition interval.hpp:110
stdex::interval::interval
interval() noexcept
Constructs an invalid interval.
Definition interval.hpp:25
stdex::interval::invalidate
void invalidate()
Invalidates interval.
Definition interval.hpp:59
stdex::interval::operator--
interval< T > operator--()
Moves interval towards the begginning by one.
Definition interval.hpp:159
stdex::interval::operator!=
bool operator!=(const interval< T > &other) const
Are intervals different?
Definition interval.hpp:198
stdex::interval::start
T start
interval start
Definition interval.hpp:19
stdex::interval::operator-
interval< T > operator-(const interval< T > &other) const
Subtracts two intervals by components.
Definition interval.hpp:137
stdex::interval::operator--
interval< T > operator--(int)
Moves interval towards the begginning by one.
Definition interval.hpp:171