acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
compare.h
1#pragma once
2
3
4#include "acme/prototype/mathematics/numeric_info.h"
5
6
7namespace comparison
8{
9
10 template < typename A, typename B >
11 inline bool eq(const A & a, const B & b)
12 {
13 return a == b;
14 }
15
16 template < prototype_signed SIGNED, prototype_unsigned UNSIGNED >
17 inline bool eq(SIGNED s, UNSIGNED u)
18 {
19 return s < 0 ? false : (UNSIGNED)s == u;
20 }
21
22
23
24// template < typename A, typename B >
25// inline bool lt(const A & a, const B & b)
26// {
27//
28// return a < b;
29//
30// }
31
32
33 template < prototype_unsigned UNSIGNED, prototype_signed SIGNED >
34 inline bool lt(UNSIGNED u, SIGNED s)
35 {
36 return s < 0 ? false : u < (UNSIGNED)s;
37 }
38
39 template < prototype_signed SIGNED, prototype_unsigned UNSIGNED >
40 inline bool lt(SIGNED s, UNSIGNED u)
41 {
42 return s < 0 ? true : (UNSIGNED)s < u;
43 }
44
45
46 template < prototype_signed SIGNED1, prototype_signed SIGNED2 >
47 inline bool lt(SIGNED1 s1, SIGNED2 s2)
48 {
49
50 return s1 < s2;
51
52 }
53
54 template < prototype_unsigned UNSIGNED1, prototype_unsigned UNSIGNED2 >
55 inline bool lt(UNSIGNED1 u1, UNSIGNED2 u2)
56 {
57
58 return u1 > ::numeric_info<UNSIGNED2>::maximum() ? false : (UNSIGNED2)u1 < u2;
59
60 }
61
62
63
64
65 template < >
66 inline bool lt(ulong u, long i)
67 {
68 return i < 0 ? false : u < (::u32) i;
69 }
70
71
72
73 template < typename A, typename B >
74 inline bool le(A a, B b)
75 {
76 return a <= b;
77 }
78
79 template < prototype_signed SIGNED, prototype_unsigned UNSIGNED >
80 inline bool le(SIGNED s, UNSIGNED u)
81 {
82 return s < 0 ? true : ((UNSIGNED)s) <= u;
83 }
84
85
86// template < typename A, typename B >
87// inline bool gt(const A & a, const B & b)
88// {
89// return b < a;
90// }
91
92
93 template < prototype_signed SIGNED, prototype_unsigned UNSIGNED >
94 inline bool gt(SIGNED i, UNSIGNED u)
95 {
96
97 return i < 0 ? false : ((UNSIGNED)i) > u;
98
99 }
100
101
102 template < prototype_unsigned UNSIGNED, prototype_signed SIGNED >
103 inline bool gt(UNSIGNED u, SIGNED s)
104 {
105
106 return s < 0 ? true : u > ((UNSIGNED) s);
107
108 }
109
110 template < prototype_signed SIGNED1, prototype_signed SIGNED2 >
111 inline bool gt(SIGNED1 s1, SIGNED2 s2)
112 {
113
114 return s2 < s1;
115
116 }
117
118
119 template < prototype_unsigned UNSIGNED1, prototype_unsigned UNSIGNED2 >
120 inline bool gt(UNSIGNED1 u1, UNSIGNED2 u2)
121 {
122
123 return u2 < u1;
124
125 }
126
127 template < typename A, typename B >
128 inline bool ge(A a, B b)
129 {
130 return a >= b;
131 }
132
133 template < prototype_signed SIGNED, prototype_unsigned UNSIGNED >
134 inline bool ge(SIGNED s, UNSIGNED u)
135 {
136
137 return s < 0 ? false : ((UNSIGNED)s) >= u;
138
139 }
140
141
142 template < prototype_unsigned UNSIGNED1, prototype_unsigned UNSIGNED2 >
143 inline bool ge(UNSIGNED1 u1, UNSIGNED2 u2)
144 {
145 return u1 >= u2;
146 }
147
148 template < prototype_signed SIGNED1, prototype_signed SIGNED2 >
149 inline bool ge(SIGNED1 s1, SIGNED2 s2)
150 {
151 return s1 >= s2;
152 }
153
154
155 template < prototype_unsigned UNSIGNED, prototype_signed SIGNED >
156 inline bool ge(UNSIGNED u, SIGNED s)
157 {
158 return s < 0 ? true : u >= (UNSIGNED)s;
159 }
160
161
162
163
164
165 template < typename T1, typename T2 >
166 inline bool is_equal(const T1& t1, const T2& t2, ::f64 dMargin, bool bIn)
167 {
168
169 ::f64 delta = (::f64)(t1 - t2);
170
171 if (bIn)
172 {
173
174 return delta >= (-dMargin / 2.0) && delta <= dMargin;
175
176 }
177 else
178 {
179
180 return delta >= (-dMargin) && delta <= dMargin * 2.0;
181
182 }
183
184
185 }
186
187
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)
190 {
191
192 auto delta1 = abs(outer1 - inner1);
193
194 auto delta2 = abs(inner2 - outer2);
195
196 auto delta = (delta1 + delta2) / 3;
197
198 return abs(delta1 - delta2) < delta;
199
200 }
201
202
203
204} // namespace comparison
205
206
207
208
209
210
211template < typename TYPE, typename MINIMUM, typename MAXIMUM >
212[[nodiscard]]constexpr auto constrained(TYPE t, MINIMUM tMinimum, MAXIMUM tMaximum)
213{
214
215 return minimum(tMaximum, maximum(tMinimum, t));
216
217}
218
219template < typename TYPE, typename MINIMUM, typename MAXIMUM >
220constexpr TYPE & constrain(TYPE & t, MINIMUM tMinimum, MAXIMUM tMaximum)
221{
222
223 return t = constrained(t, tMinimum, tMaximum);
224
225}
226
227
228template < typename TYPE, typename MINIMUM, typename MAXIMUM >
229[[nodiscard]]constexpr auto sort_constrained(TYPE t, MINIMUM tMinimum, MAXIMUM tMaximum)
230{
231
232 return tMinimum < tMaximum ? constrain(tMinimum, tMaximum) : constrain(tMaximum, tMinimum);
233
234}
235
236
237template < typename TYPE, typename MINIMUM, typename MAXIMUM >
238constexpr TYPE & sort_constrain(TYPE & t, MINIMUM tMinimum, MAXIMUM tMaximum)
239{
240
241 return t = sort_constrained(t, tMinimum, tMaximum);
242
243}
244
245
246
247template < typename TYPE >
248[[nodiscard]]constexpr auto u16_constrained(TYPE t)
249{
250
251 return (::u16) constrain(t, 0, UINT16_MAX);
252
253}
254
255
256template < typename TYPE >
257[[nodiscard]]constexpr ::u32 u32_constrained(TYPE t)
258{
259
260 return (::u32) constrain(t, 0, UINT32_MAX);
261
262}
263
264
265struct domain_id
266{
267
268
269 enum_domain m_edomain;
270 ::i32 m_iId;
271
272
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){}
276
277 inline constexpr bool operator == (const ::domain_id & domainid) const
278 {
279
280 return ::comparison::tuple
281 (
282 [&]() { return m_edomain == domainid.m_edomain; },
283 [&]() { return m_iId == domainid.m_iId; }
284 );
285
286 }
287
288
289 inline constexpr ::std::strong_ordering operator <=>(const ::domain_id & domainid) const
290 {
291
292 return ::comparison::tuple
293 (
294 [&]() { return m_edomain <=> domainid.m_edomain; },
295 [&]() { return m_iId <=> domainid.m_iId; }
296 );
297
298 }
299
300
301 inline constexpr bool is_ok() const
302 {
303
304 return m_edomain > e_domain_none && m_iId > 0;
305
306 }
307
308
309 inline constexpr bool is_nok() const
310 {
311
312 return !is_ok();
313
314 }
315
316
317};
318
319
320