94 lines
2.1 KiB
C++
94 lines
2.1 KiB
C++
/*
|
|
SPDX-License-Identifier: MIT
|
|
Copyright © 2023 Amebis
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "sal.hpp"
|
|
#include <vector>
|
|
|
|
namespace stdex
|
|
{
|
|
///
|
|
/// Numerical interval
|
|
///
|
|
template <class T>
|
|
struct interval
|
|
{
|
|
T start; ///< interval start
|
|
T end; ///< interval end
|
|
|
|
///
|
|
/// Constructs an invalid interval
|
|
///
|
|
inline interval() noexcept : start(1), end(0) {}
|
|
|
|
///
|
|
/// Constructs a zero-size interval
|
|
///
|
|
/// \param[in] x Interval start and end value
|
|
///
|
|
inline interval(_In_ T x) noexcept : start(x), end(x) {}
|
|
|
|
///
|
|
/// Constructs an interval
|
|
///
|
|
/// \param[in] _start Interval start value
|
|
/// \param[in] _end Interval end value
|
|
///
|
|
inline interval(_In_ T _start, _In_ T _end) noexcept : start(_start), end(_end) {}
|
|
|
|
///
|
|
/// Returns interval size
|
|
///
|
|
/// \returns Interval size or 0 if interval is invalid
|
|
///
|
|
inline T size() const { return start <= end ? end - start : 0; }
|
|
|
|
///
|
|
/// Is interval empty?
|
|
///
|
|
/// \returns true if interval is empty or false otherwise
|
|
///
|
|
inline bool empty() const { return start >= end; }
|
|
|
|
///
|
|
/// Is interval valid?
|
|
///
|
|
/// \returns true if interval is valid or false otherwise
|
|
///
|
|
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?
|
|
///
|
|
/// \param[in] x Value to test
|
|
///
|
|
/// \returns true if x is in [start, end) or false otherwise
|
|
///
|
|
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>;
|
|
}
|