# stdex: include/stdex/interval.hpp Source File

Loading...
Searching...
No Matches
interval.hpp
/*
SPDX-License-Identifier: MIT
Copyright © 2023 Amebis
*/
#pragma once
#include "sal.hpp"
#include <vector>
namespace stdex
{
template <class T>
struct interval
{
T start;
T end;
inline interval() noexcept : start(1), end(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 operator bool() const { return start <= end; }
inline bool contains(_In_ T x) const { return start <= x && x < end; }
};
template <class T, class _Alloc = std::allocator<interval<T>>>
using interval_vector = std::vector<interval<T>, _Alloc>;
}
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);
}
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);
}
template <class T>
inline stdex::interval<T> operator++(_Inout_ stdex::interval<T>& i)
{
++i.start;
++i.end;
return i;
}
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;
}
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);
}
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);
}
template <class T>
inline stdex::interval<T> operator--(_Inout_ stdex::interval<T>& i)
{
--i.start;
--i.end;
return i;
}
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;
}
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;
}
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;
}
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::contains
bool contains(T x) const
Is value in interval?
Definition interval.hpp:70
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::size
T size() const
Returns interval size.
Definition interval.hpp:47
stdex::interval::end
T end
interval end
Definition interval.hpp:20
stdex::interval::interval
interval() noexcept
Constructs an invalid interval.
Definition interval.hpp:25
stdex::interval::start
T start
interval start
Definition interval.hpp:19