proxsuite 0.7.3
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 operator/,
98 (b, R))
100 {
102 }
103
105 (typename R),
106 requires(VEG_CONCEPT(index<R>) &&
108 VEG_NODISCARD VEG_INLINE constexpr auto
109 operator%,
110 (b, R))
112 {
114 }
115
116#define VEG_CMP(Name, TypeName, Op) \
117 VEG_TEMPLATE((typename R), \
118 requires(VEG_CONCEPT(index<R>)), \
119 VEG_NODISCARD VEG_INLINE constexpr auto \
120 operator Op, /* NOLINT */ \
121 (b, R)) \
122 const VEG_NOEXCEPT->typename _detail::binary_traits<Dyn, R>::TypeName \
123 { \
124 return _detail::binary_traits<Dyn, R>::cmp_##Name##_fn(*this, b); \
125 } \
126 VEG_NOM_SEMICOLON
127
128 VEG_CMP(eq, CmpEq, ==);
129 VEG_CMP(neq, CmpNEq, !=);
130 VEG_CMP(lt, CmpLT, <);
131 VEG_CMP(le, CmpLE, <=);
132 VEG_CMP(gt, CmpGT, >);
133 VEG_CMP(ge, CmpGE, >=);
134
135#undef VEG_CMP
136private:
137 isize m_val = 0;
138};
139
140template<Ternary T>
142 Unsafe /*tag*/) VEG_NOEXCEPT
143{
144}
145template<Ternary T>
146VEG_INLINE constexpr Boolean<T>::Boolean // NOLINT(hicpp-explicit-conversions)
148 : Boolean(((void)VEG_INTERNAL_ASSERT_PRECONDITION(b.val == (T == yes)), b),
149 unsafe)
150{
151}
152
153template<isize N>
154VEG_INLINE constexpr Fix<N>::Fix(Dyn /*arg*/, Unsafe /*tag*/) VEG_NOEXCEPT
155{
156}
157template<isize N>
158VEG_INLINE constexpr Fix<N>::Fix // NOLINT(hicpp-explicit-conversions)
159 (Dyn arg) VEG_NOEXCEPT
160 : Fix((VEG_INTERNAL_ASSERT_PRECONDITION(isize(arg) == N), arg), unsafe)
161{
162}
163
164namespace _detail {
165
166template<>
168{
169#define VEG_OP(Name, TypeName, Op) \
170 using TypeName /* NOLINT(bugprone-macro-parentheses) */ = Dyn; \
171 VEG_NODISCARD VEG_INLINE static constexpr auto Name##_fn(Dyn a, Dyn b) \
172 VEG_NOEXCEPT -> TypeName \
173 { \
174 return { isize(usize(isize(a)) Op usize(isize(b))) }; \
175 } \
176 static_assert(true, "")
177
178#define VEG_CMP(Name, TypeName, Op) \
179 using TypeName /* NOLINT(bugprone-macro-parentheses) */ = Boolean<maybe>; \
180 VEG_NODISCARD VEG_INLINE static constexpr auto Name##_fn(Dyn a, Dyn b) \
181 VEG_NOEXCEPT -> TypeName \
182 { \
183 return (isize(a) Op isize(b)); \
184 } \
185 static_assert(true, "")
186
187 VEG_OP(add, Add, +);
188 VEG_OP(sub, Sub, -);
189 VEG_OP(mul, Mul, *);
190 VEG_CMP(cmp_eq, CmpEq, ==);
191 VEG_CMP(cmp_neq, CmpNEq, !=);
192 VEG_CMP(cmp_lt, CmpLT, <);
193 VEG_CMP(cmp_le, CmpLE, <=);
194 VEG_CMP(cmp_gt, CmpGT, >);
195 VEG_CMP(cmp_ge, CmpGE, >=);
196
197 using Div = Dyn;
198 using Mod = Dyn;
199
200 VEG_NODISCARD static constexpr auto div_fn(Dyn a, Dyn b) VEG_NOEXCEPT -> Div
201 {
203 isize(a) / isize(b);
204 }
205 VEG_NODISCARD static constexpr auto mod_fn(Dyn a, Dyn b) VEG_NOEXCEPT -> Mod
206 {
207
209 isize(a) % isize(b);
210 }
211
212#undef VEG_OP
213#undef VEG_CMP
214};
215
216template<isize N>
217struct binary_traits<Fix<N>, Dyn> : binary_traits<Dyn, Dyn>
218{};
219
220template<>
221struct binary_traits<Fix<0>, Dyn> : binary_traits<Dyn, Dyn>
222{
223 using Mul = Fix<0>;
225 constexpr VEG_INLINE static auto mul_fn(Fix<0> /*a*/, Dyn /*b*/) VEG_NOEXCEPT
226 -> Mul
227 {
228 return {};
229 }
230};
231
232template<isize N>
233struct binary_traits<Dyn, Fix<N>> : binary_traits<Dyn, Dyn>
234{
235 using Mul = typename binary_traits<Fix<N>, Dyn>::Mul;
236 VEG_INLINE static constexpr auto mul_fn(Dyn a, Fix<N> /*b*/) VEG_NOEXCEPT
237 -> Mul
238 {
239 return binary_traits<Fix<N>, Dyn>::mul_fn({}, a);
240 }
241
244
245 VEG_NODISCARD VEG_INLINE static constexpr auto div_fn(Dyn a, Fix<N> /*b*/)
247 {
248 return Div(isize(a) / N);
249 }
250 VEG_NODISCARD VEG_INLINE static constexpr auto mod_fn(Dyn a, Fix<N> /*b*/)
252 {
253 return Mod(isize(a) % N);
254 }
255};
256
257} // namespace _detail
258
259inline namespace literals {
260VEG_INLINE constexpr auto
261operator""_v(unsigned long long n) VEG_NOEXCEPT->Dyn
262{
263 return isize(n);
264}
265} // namespace literals
266
267template<>
268struct fmt::Debug<Boolean<maybe>>
269{
270 static void to_string(fmt::Buffer& out, Ref<Boolean<maybe>> val)
271 {
272 out.insert(out.size(), "maybe[", 6);
273 Debug<bool>::to_string(out, ref(bool(val.get())));
274 out.insert(out.size(), "]", 1);
275 }
276};
277
278template<>
279struct fmt::Debug<Dyn>
280{
281 static void to_string(fmt::Buffer& out, Ref<Dyn> val)
282 {
283 out.insert(out.size(), "Dyn[", 4);
284 Debug<isize>::to_string(out, ref(isize(val.get())));
285 out.insert(out.size(), "]", 1);
286 }
287};
288} // namespace veg
289} // namespace linalg
290} // namespace proxsuite
291
293#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:1238
#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)