Ultra 0.9.4
Loading...
Searching...
No Matches
interval.h
Go to the documentation of this file.
1
12
13#if !defined(ULTRA_INTERVAL_H)
14#define ULTRA_INTERVAL_H
15
16#include "utility/assert.h"
17
18#include <concepts>
19#include <type_traits>
20#include <utility>
21
22namespace ultra
23{
24
25template<class A> concept ArithmeticScalar =
26 std::is_arithmetic_v<A> && !std::same_as<A, bool>;
27
28
35template<ArithmeticScalar T>
36struct interval
37{
38 constexpr interval(T m, T s) noexcept : min(m), sup(s)
39 {
40 Expects(m < s);
41 }
42
43 template<std::floating_point T1, std::floating_point T2>
44 requires std::floating_point<T>
45 constexpr interval(T1 m, T2 s) : interval(static_cast<T>(m),
46 static_cast<T>(s))
47 {
48 Expects(m < s);
49 }
50
51 template<std::integral T1, std::integral T2>
52 requires std::integral<T>
53 constexpr interval(T1 m, T2 s) : interval(static_cast<T>(m),
54 static_cast<T>(s))
55 {
56 Expects(std::cmp_less(m, s));
57 Expects(std::in_range<T>(m));
58 Expects(std::in_range<T>(s));
59 }
60
61 template<std::floating_point T1, std::floating_point T2>
62 requires std::floating_point<T>
63 explicit constexpr interval(const std::pair<T1, T2> &p)
64 : interval(static_cast<T>(p.first), static_cast<T>(p.second))
65 {
66 Expects(p.first < p.second);
67 }
68
69 template<std::integral T1, std::integral T2>
70 requires std::integral<T>
71 explicit constexpr interval(const std::pair<T1, T2> &p)
72 : interval(static_cast<T>(p.first), static_cast<T>(p.second))
73 {
74 Expects(std::cmp_less(p.first, p.second));
75 Expects(std::in_range<T>(p.first));
76 Expects(std::in_range<T>(p.second));
77 }
78
79 [[nodiscard]] friend constexpr bool operator==(
80 const interval &, const interval &) noexcept = default;
81
82 [[nodiscard]] bool is_valid() const noexcept
83 {
84 return min < sup;
85 }
86
87 T min;
88 T sup;
89}; // class interval
90
91} // namespace ultra
92
93#endif
Definition interval.h:25