proxsuite 0.6.7
The Advanced Proximal Optimization Toolbox
Loading...
Searching...
No Matches
slice.hpp
Go to the documentation of this file.
1#ifndef VEG_SLICE_HPP_GKSTE2JDS
2#define VEG_SLICE_HPP_GKSTE2JDS
3
9#include <initializer_list>
10
11namespace proxsuite {
12namespace linalg {
13namespace veg {
14template<typename T, usize N>
15using CArray = T[N];
16
17namespace _detail {
18namespace _slice {
19namespace adl {
20struct AdlBase
21{};
22} // namespace adl
23} // namespace _slice
24} // namespace _detail
25
26template<typename T>
28{
29private:
30 T const* data = nullptr;
31 isize size = 0;
32
33public:
35 constexpr Slice() = default;
36
38 constexpr Slice(Unsafe /*tag*/,
39 FromRawParts /*tag*/,
40 T const* data_,
42 : data{ data_ }
43 , size{ count }
44 {
45 }
46
49 constexpr auto ptr() const VEG_NOEXCEPT -> T const* { return data; }
52 constexpr auto len() const VEG_NOEXCEPT -> isize { return size; }
53
56 constexpr auto operator[](isize idx) const VEG_NOEXCEPT->T const&
57 {
59 *(data + idx);
60 }
63 constexpr auto get_unchecked(Unsafe /*tag*/,
64 isize idx) const VEG_NOEXCEPT -> Ref<T>
65 {
66 return ref(*(data + idx));
67 }
68
71 {
74 tuplify,
76 unsafe,
77 FromRawParts{},
78 data,
79 idx,
80 },
82 unsafe,
83 FromRawParts{},
84 data + idx,
85 size - idx,
86 },
87 };
88 }
89
91 -> Slice<unsigned char>
92 {
93 return {
94 unsafe,
95 from_raw_parts,
96 reinterpret_cast<unsigned char const*>(data),
97 isize(sizeof(T)) * size,
98 };
99 }
100};
101
102template<typename T>
103struct SliceMut : private Slice<T>
104{
106 constexpr SliceMut() = default;
107
109 constexpr SliceMut(Unsafe /*tag*/,
110 FromRawParts /*tag*/,
111 T const* data_,
113 : Slice<T>{
114 unsafe,
115 from_raw_parts,
116 data_,
117 count,
118 }
119 {
120 }
121
122 using Slice<T>::ptr;
123 using Slice<T>::as_bytes;
124 using Slice<T>::split_at;
125 using Slice<T>::len;
126 using Slice<T>::get_unchecked;
127
129 {
130 return *this;
131 }
132
136 {
137 return const_cast<T&>(static_cast<Slice<T> const&>(*this)[idx]);
138 }
141 VEG_CPP14(constexpr) auto ptr_mut() VEG_NOEXCEPT->T*
142 {
143 return const_cast<T*>(ptr());
144 }
147 VEG_CPP14(constexpr)
148 auto get_mut_unchecked(Unsafe /*tag*/, isize idx) VEG_NOEXCEPT -> RefMut<T>
149 {
150 return mut(const_cast<T&>(*(this->data + idx)));
151 }
153 VEG_NOEXCEPT -> SliceMut<unsigned char>
154 {
155 return {
156 unsafe,
157 from_raw_parts,
158 reinterpret_cast<unsigned char*>(ptr_mut()),
159 isize(sizeof(T)) * len(),
160 };
161 }
162
165 {
168 tuplify,
170 unsafe,
171 from_raw_parts,
172 ptr_mut(),
173 idx,
174 },
176 unsafe,
177 from_raw_parts,
178 ptr_mut() + idx,
179 len() - idx,
180 },
181 };
182 }
183};
184
185namespace array {
186template<typename T, isize N>
187struct Array
188{
189 static_assert(N > 0, ".");
190 T _[static_cast<usize>(N)];
191
192 constexpr auto as_ref() const -> Slice<T>
193 {
194 return {
195 unsafe,
196 from_raw_parts,
197 static_cast<T const*>(_),
198 N,
199 };
200 }
201 VEG_CPP14(constexpr) auto as_mut() -> SliceMut<T>
202 {
203 return {
204 unsafe,
205 from_raw_parts,
206 static_cast<T*>(_),
207 N,
208 };
209 }
210};
211} // namespace array
212using array::Array;
213
214namespace nb {
216{
217 template<typename T>
218 VEG_CPP14(constexpr)
220 {
221 return {
222 unsafe,
223 from_raw_parts,
224 init_list.begin(),
225 isize(init_list.size()),
226 };
227 }
228};
229} // namespace nb
230VEG_NIEBLOID(init_list);
231
232template<typename T>
235} // namespace veg
236} // namespace linalg
237} // namespace proxsuite
238
240#endif /* end of include guard VEG_SLICE_HPP_GKSTE2JDS */
#define VEG_NIEBLOID(Name)
Definition macros.hpp:545
#define VEG_INLINE
Definition macros.hpp:118
_detail::_meta::make_signed< usize >::Type isize
Definition typedefs.hpp:43
decltype(sizeof(0)) usize
Definition macros.hpp:702
STL namespace.
#define VEG_INTERNAL_ASSERT_PRECONDITION
Definition prologue.hpp:100
#define VEG_NODISCARD
Definition prologue.hpp:97
#define VEG_NOEXCEPT
Definition prologue.hpp:30
#define VEG_CPP14(...)
Definition prologue.hpp:71
VEG_NODISCARD VEG_INLINE auto as_mut_bytes() VEG_NOEXCEPT -> SliceMut< unsigned char >
Definition slice.hpp:152
VEG_NODISCARD VEG_INLINE auto get_mut_unchecked(Unsafe, isize idx) VEG_NOEXCEPT -> RefMut< T >
Definition slice.hpp:148
VEG_INLINE constexpr SliceMut()=default
VEG_NODISCARD VEG_INLINE VEG_CPP14(constexpr) auto ptr_mut() VEG_NOEXCEPT -> T *
Definition slice.hpp:141
VEG_NODISCARD VEG_INLINE VEG_CPP14(constexpr) auto split_at_mut(isize idx) VEG_NOEXCEPT -> Tuple< SliceMut< T >, SliceMut< T > >
Definition slice.hpp:163
VEG_NODISCARD VEG_INLINE constexpr auto as_const() const noexcept -> Slice< T >
Definition slice.hpp:128
VEG_NODISCARD VEG_INLINE VEG_CPP14(constexpr) auto operator[](isize idx) VEG_NOEXCEPT -> T &
Definition slice.hpp:135
VEG_INLINE constexpr SliceMut(Unsafe, FromRawParts, T const *data_, isize count) VEG_NOEXCEPT
Definition slice.hpp:109
VEG_INLINE constexpr Slice(Unsafe, FromRawParts, T const *data_, isize count) VEG_NOEXCEPT
Definition slice.hpp:38
VEG_NODISCARD VEG_INLINE constexpr auto len() const VEG_NOEXCEPT -> isize
Definition slice.hpp:52
VEG_NODISCARD VEG_INLINE constexpr auto ptr() const VEG_NOEXCEPT -> T const *
Definition slice.hpp:49
VEG_NODISCARD VEG_INLINE constexpr auto get_unchecked(Unsafe, isize idx) const VEG_NOEXCEPT -> Ref< T >
Definition slice.hpp:63
VEG_INLINE constexpr Slice()=default
VEG_NODISCARD VEG_INLINE constexpr auto operator[](isize idx) const VEG_NOEXCEPT -> T const &
Definition slice.hpp:56
VEG_NODISCARD VEG_INLINE constexpr auto split_at(isize idx) const VEG_NOEXCEPT -> Tuple< Slice< T >, Slice< T > >
Definition slice.hpp:69
VEG_NODISCARD VEG_INLINE auto as_bytes() const VEG_NOEXCEPT -> Slice< unsigned char >
Definition slice.hpp:90
T _[static_cast< usize >(N)]
Definition slice.hpp:190
VEG_CPP14(constexpr) auto as_mut() -> SliceMut< T >
Definition slice.hpp:201
constexpr auto as_ref() const -> Slice< T >
Definition slice.hpp:192