HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
int_interval.hpp
1
2#include <concepts>
3#include <limits>
4
5#pragma once
6
7namespace hi {
8inline namespace v1 {
9
10template<std::signed_integral T>
11struct interval {
12 using value_type = T;
13
14 constexpr static value_type min = std::numeric_limits<value_type>::min();
15 constexpr static value_type max = std::numeric_limits<value_type>::max();
16
17 value_type lo = lowest;
18 value_type hi = heighest;
19
20 constexpr interval() noexcept = default;
21 constexpr interval(interval const &) noexcept = default;
22 constexpr interval(interval &&) noexcept = default;
23 constexpr interval &operator=(interval const &) noexcept = default;
24 constexpr interval &operator=(interval &&) noexcept = default;
25 [[nodiscard]] constexpr bool operator==(interval const &, interval const &) noexcept = default;
26
27 constexpr interval(value_type lo, value_type hi) noexcept : lo(lo), hi(hi)
28 {
29 hi_axiom(lo <= hi);
30 }
31
32 constexpr interval(value_type rhs) noexcept : lo(rhs), hi(rhs)
33 {
34 }
35
38 [[nodiscard]] constexpr bool finite() const noexcept
39 {
40 return lo != min and hi != max;
41 }
42
43 constexpr interval &operator++() noexcept
44 {
45 lo = lo == min ? min : saturated_inc(lo);
46 hi = saturated_inc(hi);
47 return *this;
48 }
49
50 constexpr interval &operator--() noexcept
51 {
52 lo = saturated_dec(lo);
53 hi = hi == max ? max : saturated_dec(hi);
54 return *this;
55 }
56
57 constexpr interval operator++(int) noexcept
58 {
59 auto r = *this;
60 ++(*this);
61 return r;
62 }
63
64 constexpr interval operator--(int) noexcept
65 {
66 auto r = *this;
67 --(*this);
68 return r;
69 }
70
71 constexpr interval &operator+=(interval const &rhs) noexcept
72 {
73 return *this = *this + rhs;
74 }
75
76 constexpr interval &operator-=(interval const &rhs) noexcept
77 {
78 return *this = *this - rhs;
79 }
80
81 [[nodiscard]] constexpr friend interval operator-(interval const &rhs) noexcept
82 {
83 hilet r_lo = lhs.hi == max ? min : saturated_neg(lhs.hi);
84 hilet r_hi = lhs.lo == min ? max : saturated_neg(lhs.lo);
85 return {r_lo, r_hi};
86 }
87
88 [[nodiscard]] constexpr friend interval operator+(interval const &lhs, interval const &rhs) noexcept
89 {
90 hilet r_lo = lhs.lo == min or rhs.lo == min ? min : saturated_add(lhs.lo, rhs.lo);
91 hilet r_hi = lhs.hi == max or rhs.hi == max ? max : saturated_add(lhs.hi, rhs.hi);
92 return {r_lo, r_hi};
93 }
94
95 [[nodiscard]] constexpr friend interval operator-(interval const &lhs, interval const &rhs) noexcept
96 {
97 hilet r_lo = lhs.lo == min or rhs.hi == min ? max : saturated_sub(lhs.lo, rhs.hi);
98 hilet r_hi = lhs.hi == max or rhs.lo == min ? max : saturated_sub(lhs.hi, rhs.lo);
99 return {r_lo, r_hi};
100 }
101
102 [[nodiscard]] constexpr friend interval abs(interval const &rhs) noexcept
103 {
104 auto r_lo = saturated_abs(rhs.lo);
105 auto r_hi = saturated_abs(rhs.hi);
106 if (r_lo > r_hi) {
107 std::swap(r_lo, r_hi);
108 }
109 return {r_lo, r_hi};
110 }
111
112 [[nodiscard]] constexpr friend interval min(interval const &lhs,interval const &rhs) noexcept
113 {
114 auto r_lo = std::min(lhs.lo, rhs.lo);
115 auto r_hi = lhs.hi == max or rhs.hi == max ? max : std::min(lhs.hi, rhs.hi);
116 return {r_lo, r_hi};
117 }
118
119 [[nodiscard]] constexpr friend interval max(interval const &lhs,interval const &rhs) noexcept
120 {
121 auto r_lo = lhs.lo == min or rhs.lo == min ? min : std::max(lhs.lo, rhs.lo);
122 auto r_hi = std::max(lhs.hi, rhs.hi);
123 return {r_lo, r_hi};
124 }
125};
126
127}}
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
Definition int_interval.hpp:11
constexpr bool finite() const noexcept
The interval is finite,.
Definition int_interval.hpp:38
T max(T... args)
T min(T... args)
T swap(T... args)