4#include "acme/prototype/mathematics/numeric_info.h"
10 template <
typename A,
typename B >
11 inline bool eq(
const A & a,
const B & b)
16 template < prototype_
signed SIGNED, prototype_
unsigned UNSIGNED >
17 inline bool eq(SIGNED s, UNSIGNED u)
19 return s < 0 ? false : (UNSIGNED)s == u;
33 template < prototype_
unsigned UNSIGNED, prototype_
signed SIGNED >
34 inline bool lt(UNSIGNED u, SIGNED s)
36 return s < 0 ? false : u < (UNSIGNED)s;
39 template < prototype_
signed SIGNED, prototype_
unsigned UNSIGNED >
40 inline bool lt(SIGNED s, UNSIGNED u)
42 return s < 0 ? true : (UNSIGNED)s < u;
46 template < prototype_
signed SIGNED1, prototype_
signed SIGNED2 >
47 inline bool lt(SIGNED1 s1, SIGNED2 s2)
54 template < prototype_
unsigned UNSIGNED1, prototype_
unsigned UNSIGNED2 >
55 inline bool lt(UNSIGNED1 u1, UNSIGNED2 u2)
58 return u1 > ::numeric_info<UNSIGNED2>::maximum() ? false : (UNSIGNED2)u1 < u2;
66 inline bool lt(ulong u,
long i)
68 return i < 0 ? false : u < (::u32) i;
73 template <
typename A,
typename B >
74 inline bool le(A a, B b)
79 template < prototype_
signed SIGNED, prototype_
unsigned UNSIGNED >
80 inline bool le(SIGNED s, UNSIGNED u)
82 return s < 0 ? true : ((UNSIGNED)s) <= u;
93 template < prototype_
signed SIGNED, prototype_
unsigned UNSIGNED >
94 inline bool gt(SIGNED i, UNSIGNED u)
97 return i < 0 ? false : ((UNSIGNED)i) > u;
102 template < prototype_
unsigned UNSIGNED, prototype_
signed SIGNED >
103 inline bool gt(UNSIGNED u, SIGNED s)
106 return s < 0 ? true : u > ((UNSIGNED) s);
110 template < prototype_
signed SIGNED1, prototype_
signed SIGNED2 >
111 inline bool gt(SIGNED1 s1, SIGNED2 s2)
119 template < prototype_
unsigned UNSIGNED1, prototype_
unsigned UNSIGNED2 >
120 inline bool gt(UNSIGNED1 u1, UNSIGNED2 u2)
127 template <
typename A,
typename B >
128 inline bool ge(A a, B b)
133 template < prototype_
signed SIGNED, prototype_
unsigned UNSIGNED >
134 inline bool ge(SIGNED s, UNSIGNED u)
137 return s < 0 ? false : ((UNSIGNED)s) >= u;
142 template < prototype_
unsigned UNSIGNED1, prototype_
unsigned UNSIGNED2 >
143 inline bool ge(UNSIGNED1 u1, UNSIGNED2 u2)
148 template < prototype_
signed SIGNED1, prototype_
signed SIGNED2 >
149 inline bool ge(SIGNED1 s1, SIGNED2 s2)
155 template < prototype_
unsigned UNSIGNED, prototype_
signed SIGNED >
156 inline bool ge(UNSIGNED u, SIGNED s)
158 return s < 0 ? true : u >= (UNSIGNED)s;
165 template <
typename T1,
typename T2 >
166 inline bool is_equal(
const T1& t1,
const T2& t2, ::f64 dMargin,
bool bIn)
169 ::f64 delta = (::f64)(t1 - t2);
174 return delta >= (-dMargin / 2.0) && delta <= dMargin;
180 return delta >= (-dMargin) && delta <= dMargin * 2.0;
188 template <
typename OUTER1,
typename INNER1,
typename INNER2,
typename OUTER2 >
189 inline bool is_centered(
const OUTER1& outer1,
const INNER1& inner1,
const INNER2& inner2,
const OUTER2& outer2)
192 auto delta1 = abs(outer1 - inner1);
194 auto delta2 = abs(inner2 - outer2);
196 auto delta = (delta1 + delta2) / 3;
198 return abs(delta1 - delta2) < delta;
211template <
typename TYPE,
typename MINIMUM,
typename MAXIMUM >
212[[nodiscard]]
constexpr auto constrained(TYPE t, MINIMUM tMinimum, MAXIMUM tMaximum)
215 return minimum(tMaximum, maximum(tMinimum, t));
219template <
typename TYPE,
typename MINIMUM,
typename MAXIMUM >
220constexpr TYPE & constrain(TYPE & t, MINIMUM tMinimum, MAXIMUM tMaximum)
223 return t = constrained(t, tMinimum, tMaximum);
228template <
typename TYPE,
typename MINIMUM,
typename MAXIMUM >
229[[nodiscard]]
constexpr auto sort_constrained(TYPE t, MINIMUM tMinimum, MAXIMUM tMaximum)
232 return tMinimum < tMaximum ? constrain(tMinimum, tMaximum) : constrain(tMaximum, tMinimum);
237template <
typename TYPE,
typename MINIMUM,
typename MAXIMUM >
238constexpr TYPE & sort_constrain(TYPE & t, MINIMUM tMinimum, MAXIMUM tMaximum)
241 return t = sort_constrained(t, tMinimum, tMaximum);
247template <
typename TYPE >
248[[nodiscard]]
constexpr auto u16_constrained(TYPE t)
251 return (::u16) constrain(t, 0, UINT16_MAX);
256template <
typename TYPE >
257[[nodiscard]]constexpr ::u32 u32_constrained(TYPE t)
260 return (::u32) constrain(t, 0, UINT32_MAX);
269 enum_domain m_edomain;
273 constexpr domain_id() : m_edomain(e_domain_none), m_iId(-1){}
274 constexpr domain_id(enum_domain edomain, ::i32 iId) : m_edomain(edomain), m_iId(iId){}
275 constexpr domain_id(
const domain_id & domainid) : m_edomain(domainid.m_edomain), m_iId(domainid.m_iId){}
277 inline constexpr bool operator == (const ::domain_id & domainid)
const
280 return ::comparison::tuple
282 [&]() {
return m_edomain == domainid.m_edomain; },
283 [&]() {
return m_iId == domainid.m_iId; }
289 inline constexpr ::std::strong_ordering operator <=>(const ::domain_id & domainid)
const
292 return ::comparison::tuple
294 [&]() {
return m_edomain <=> domainid.m_edomain; },
295 [&]() {
return m_iId <=> domainid.m_iId; }
301 inline constexpr bool is_ok()
const
304 return m_edomain > e_domain_none && m_iId > 0;
309 inline constexpr bool is_nok()
const