proxsuite 0.6.7
The Advanced Proximal Optimization Toolbox
Loading...
Searching...
No Matches
dyn_index.hpp
Go to the documentation of this file.
1#ifndef VEG_META_INT_DYN_HPP_GC385NKBS
2#define VEG_META_INT_DYN_HPP_GC385NKBS
3
7#include "proxsuite/linalg/veg/util/compare.hpp"
9
10namespace proxsuite {
11namespace linalg {
12namespace veg {
13
14template<Ternary T>
15struct Boolean;
16
17template<>
19{
20 using type = maybe_c;
21
22 constexpr Boolean() = default;
23 constexpr Boolean /* NOLINT(hicpp-explicit-conversions) */ (bool _val)
24 VEG_NOEXCEPT : val{ _val }
25 {
26 }
27 template<Ternary T>
28 VEG_INLINE constexpr Boolean /* NOLINT(hicpp-explicit-conversions)
29 */
30 (Boolean<T> /*arg*/) VEG_NOEXCEPT : val(T == yes)
31 {
32 }
33
34 VEG_NODISCARD VEG_INLINE constexpr friend auto operator!(Boolean arg)
35 VEG_NOEXCEPT->Boolean
36 {
37 return { !arg.val };
38 }
39 VEG_NODISCARD VEG_INLINE explicit constexpr operator bool() const VEG_NOEXCEPT
40 {
41 return val;
42 }
43
44private:
45 bool val = false;
46};
47
48struct Dyn
49{
50 constexpr Dyn() = default;
51 constexpr Dyn /* NOLINT(hicpp-explicit-conversions) */ (isize val)
52 VEG_NOEXCEPT : m_val(val)
53 {
54 }
55 template<isize N>
56 constexpr Dyn /* NOLINT(hicpp-explicit-conversions) */ (Fix<N> /*arg*/)
57 VEG_NOEXCEPT : m_val(N)
58 {
59 }
60
61 VEG_NODISCARD VEG_INLINE explicit constexpr operator isize() const
63 {
64 return m_val;
65 }
67 {
68 return *this;
69 }
71 {
72 return Dyn{ -m_val };
73 }
74
75#define VEG_OP(Op, Name, TypeName) \
76 VEG_TEMPLATE((typename R), \
77 requires(VEG_CONCEPT(index<R>)), \
78 VEG_NODISCARD VEG_INLINE constexpr auto \
79 operator Op, \
80 (b, R)) \
81 const VEG_NOEXCEPT->typename _detail::binary_traits<Dyn, R>::TypeName \
82 { \
83 return _detail::binary_traits<Dyn, R>::Name##_fn(*this, b); \
84 } \
85 VEG_NOM_SEMICOLON
86
87 VEG_OP(+, add, Add);
88 VEG_OP(-, sub, Sub);
89 VEG_OP(*, mul, Mul);
90
91#undef VEG_OP
92
94 (typename R),
95 requires(VEG_CONCEPT(index<R>) &&
97 VEG_NODISCARD VEG_INLINE constexpr auto
98 operator/,
99 (b, R))
101 {
103 }
104
106 (typename R),
107 requires(VEG_CONCEPT(index<R>) &&
109 VEG_NODISCARD VEG_INLINE constexpr auto
110 operator%,
111 (b, R))
113 {
115 }
116
117#define VEG_CMP(Name, TypeName, Op) \
118 VEG_TEMPLATE((typename R), \
119 requires(VEG_CONCEPT(index<R>)), \
120 VEG_NODISCARD VEG_INLINE constexpr auto \
121 operator Op, /* NOLINT */ \
122 (b, R)) \
123 const VEG_NOEXCEPT->typename _detail::binary_traits<Dyn, R>::TypeName \
124 { \
125 return _detail::binary_traits<Dyn, R>::cmp_##Name##_fn(*this, b); \
126 } \
127 VEG_NOM_SEMICOLON
128
129 VEG_CMP(eq, CmpEq, ==);
130 VEG_CMP(neq, CmpNEq, !=);
131 VEG_CMP(lt, CmpLT, <);
132 VEG_CMP(le, CmpLE, <=);
133 VEG_CMP(gt, CmpGT, >);
134 VEG_CMP(ge, CmpGE, >=);
135
136#undef VEG_CMP
137private:
138 isize m_val = 0;
139};
140
141template<Ternary T>
143 Unsafe /*tag*/) VEG_NOEXCEPT
144{
145}
146template<Ternary T>
147VEG_INLINE constexpr Boolean<T>::Boolean // NOLINT(hicpp-explicit-conversions)
149 : Boolean(((void)VEG_INTERNAL_ASSERT_PRECONDITION(b.val == (T == yes)), b),
150 unsafe)
151{
152}
153
154template<isize N>
155VEG_INLINE constexpr Fix<N>::Fix(Dyn /*arg*/, Unsafe /*tag*/) VEG_NOEXCEPT
156{
157}
158template<isize N>
159VEG_INLINE constexpr Fix<N>::Fix // NOLINT(hicpp-explicit-conversions)
160 (Dyn arg) VEG_NOEXCEPT
161 : Fix((VEG_INTERNAL_ASSERT_PRECONDITION(isize(arg) == N), arg), unsafe)
162{
163}
164
165namespace _detail {
166
167template<>
169{
170#define VEG_OP(Name, TypeName, Op) \
171 using TypeName /* NOLINT(bugprone-macro-parentheses) */ = Dyn; \
172 VEG_NODISCARD VEG_INLINE static constexpr auto Name##_fn(Dyn a, Dyn b) \
173 VEG_NOEXCEPT->TypeName \
174 { \
175 return { isize(usize(isize(a)) Op usize(isize(b))) }; \
176 } \
177 static_assert(true, "")
178
179#define VEG_CMP(Name, TypeName, Op) \
180 using TypeName /* NOLINT(bugprone-macro-parentheses) */ = Boolean<maybe>; \
181 VEG_NODISCARD VEG_INLINE static constexpr auto Name##_fn(Dyn a, Dyn b) \
182 VEG_NOEXCEPT->TypeName \
183 { \
184 return (isize(a) Op isize(b)); \
185 } \
186 static_assert(true, "")
187
190 VEG_OP(mul, Mul, *);
197
198 using Div = Dyn;
199 using Mod = Dyn;
200
201 VEG_NODISCARD static constexpr auto div_fn(Dyn a, Dyn b) VEG_NOEXCEPT -> Div
202 {
204 isize(a) / isize(b);
205 }
206 VEG_NODISCARD static constexpr auto mod_fn(Dyn a, Dyn b) VEG_NOEXCEPT -> Mod
207 {
208
210 isize(a) % isize(b);
211 }
212
213#undef VEG_OP
214#undef VEG_CMP
215};
216
217template<isize N>
218struct binary_traits<Fix<N>, Dyn> : binary_traits<Dyn, Dyn>
219{};
220
221template<>
222struct binary_traits<Fix<0>, Dyn> : binary_traits<Dyn, Dyn>
223{
224 using Mul = Fix<0>;
226 constexpr VEG_INLINE static auto mul_fn(Fix<0> /*a*/,
227 Dyn /*b*/) VEG_NOEXCEPT -> Mul
228 {
229 return {};
230 }
231};
232
233template<isize N>
234struct binary_traits<Dyn, Fix<N>> : binary_traits<Dyn, Dyn>
235{
237 VEG_INLINE static constexpr auto mul_fn(Dyn a,
238 Fix<N> /*b*/) VEG_NOEXCEPT -> Mul
239 {
240 return binary_traits<Fix<N>, Dyn>::mul_fn({}, a);
241 }
242
245
246 VEG_NODISCARD VEG_INLINE static constexpr auto div_fn(Dyn a, Fix<N> /*b*/)
248 {
249 return Div(isize(a) / N);
250 }
251 VEG_NODISCARD VEG_INLINE static constexpr auto mod_fn(Dyn a, Fix<N> /*b*/)
253 {
254 return Mod(isize(a) % N);
255 }
256};
257
258} // namespace _detail
259
260inline namespace literals {
261VEG_INLINE constexpr auto
262operator"" _v(unsigned long long n) VEG_NOEXCEPT->Dyn
263{
264 return isize(n);
265}
266} // namespace literals
267
268template<>
269struct fmt::Debug<Boolean<maybe>>
270{
271 static void to_string(fmt::Buffer& out, Ref<Boolean<maybe>> val)
272 {
273 out.insert(out.size(), "maybe[", 6);
274 Debug<bool>::to_string(out, ref(bool(val.get())));
275 out.insert(out.size(), "]", 1);
276 }
277};
278
279template<>
280struct fmt::Debug<Dyn>
281{
282 static void to_string(fmt::Buffer& out, Ref<Dyn> val)
283 {
284 out.insert(out.size(), "Dyn[", 4);
285 Debug<isize>::to_string(out, ref(isize(val.get())));
286 out.insert(out.size(), "]", 1);
287 }
288};
289} // namespace veg
290} // namespace linalg
291} // namespace proxsuite
292
294#endif /* end of include guard VEG_META_INT_DYN_HPP_GC385NKBS */
#define VEG_OP(Op, Name, TypeName)
Definition dyn_index.hpp:75
#define VEG_CONCEPT(...)
Definition macros.hpp:1243
#define VEG_INLINE
Definition macros.hpp:118
typename _detail::_meta::conditional_< B >::template type< T, F > if_t
Definition core.hpp:83
meta::constant< Ternary, Ternary::maybe > maybe_c
Definition fix_index.hpp:68
constexpr auto maybe
Definition fix_index.hpp:65
constexpr auto yes
Definition fix_index.hpp:66
_detail::_meta::make_signed< usize >::Type isize
Definition typedefs.hpp:43
#define VEG_INTERNAL_ASSERT_PRECONDITION
Definition prologue.hpp:100
#define VEG_NODISCARD
Definition prologue.hpp:97
#define VEG_NOEXCEPT
Definition prologue.hpp:30
VEG_NODISCARD VEG_INLINE constexpr friend auto operator!(Boolean arg) VEG_NOEXCEPT -> Boolean
Definition dyn_index.hpp:34
constexpr Boolean() VEG_NOEXCEPT=default
VEG_NODISCARD VEG_INLINE constexpr auto operator-() const VEG_NOEXCEPT -> Dyn
Definition dyn_index.hpp:70
VEG_TEMPLATE((typename R), requires(VEG_CONCEPT(index< R >) &&VEG_CONCEPT(index< typename _detail::binary_traits< Dyn, R >::Mod >)), VEG_NODISCARD VEG_INLINE constexpr auto operator%,(b, R)) const VEG_NOEXCEPT -> typename _detail::binary_traits< Dyn, R >::Mod
constexpr Dyn()=default
VEG_TEMPLATE((typename R), requires(VEG_CONCEPT(index< R >) &&VEG_CONCEPT(index< typename _detail::binary_traits< Dyn, R >::Div >)), VEG_NODISCARD VEG_INLINE constexpr auto operator/,(b, R)) const VEG_NOEXCEPT -> typename _detail::binary_traits< Dyn, R >::Div
Definition dyn_index.hpp:93
VEG_NODISCARD VEG_INLINE constexpr auto operator+() const VEG_NOEXCEPT -> Dyn
Definition dyn_index.hpp:66
VEG_CMP(neq, CmpNEq, !=)
constexpr Fix() VEG_NOEXCEPT=default
VEG_NODISCARD VEG_INLINE constexpr auto get() const noexcept -> T const &
Definition ref.hpp:41
static VEG_NODISCARD constexpr auto div_fn(Dyn a, Dyn b) VEG_NOEXCEPT -> Div
static VEG_NODISCARD constexpr auto mod_fn(Dyn a, Dyn b) VEG_NOEXCEPT -> Mod
VEG_NODISCARD static VEG_INLINE constexpr auto div_fn(Dyn a, Fix< N >) VEG_NOEXCEPT -> Div
typename binary_traits< Fix< N >, Dyn >::Mul Mul
VEG_NODISCARD static VEG_INLINE constexpr auto mod_fn(Dyn a, Fix< N >) VEG_NOEXCEPT -> Mod
static VEG_INLINE constexpr auto mul_fn(Dyn a, Fix< N >) VEG_NOEXCEPT -> Mul
VEG_NODISCARD constexpr static VEG_INLINE auto mul_fn(Fix< 0 >, Dyn) VEG_NOEXCEPT -> Mul
static void to_string(fmt::Buffer &out, Ref< Boolean< maybe > > val)
static void to_string(fmt::Buffer &out, Ref< Dyn > val)