cMHN 1.1
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
array_stack.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_CONTAINER_ARRAY_STACK_H
4#define pRC_CORE_CONTAINER_ARRAY_STACK_H
5
11#include <prc/core/log/log.hpp>
12#include <prc/pragma.hpp>
13
14namespace pRC
15{
16 template<class T, Size N>
18
19 template<class T, Size N>
21 {
22 public:
24 using Type = T;
26 using Dimension = typename Sizes::Dimension;
27
28 static constexpr auto size()
29 {
30 return Sizes::size();
31 }
32
33 static constexpr auto size(Index const dimension)
34 {
35 return Sizes::size(dimension);
36 }
37
38 static constexpr auto indexToSubscripts(Index const index)
39 {
40 return Subscripts<N>(index);
41 }
42
43 static constexpr auto subscriptsToIndex(Index const subscripts)
44 {
45 return subscripts;
46 }
47
48 static constexpr auto subscriptsToIndex(Subscripts<N> const &subscripts)
49 {
50 return Index(subscripts);
51 }
52
53 private:
54 BEGIN_IGNORE_DIAGNOSTIC_GCC("-Wpedantic")
57 using CArray = R[S];
60
62 {
63 if constexpr(cDebugLevel >= DebugLevel::Mid)
64 {
65 if(!(index < size()))
66 {
67 Logging::error("Array index out of range.");
68 }
69 }
70 }
71
72 public:
73 ~CommonArray() = default;
74 constexpr CommonArray(CommonArray const &) = default;
75 constexpr CommonArray(CommonArray &&) = default;
76 constexpr CommonArray &operator=(CommonArray const &) & = default;
77 constexpr CommonArray &operator=(CommonArray &&) & = default;
78 constexpr CommonArray() = default;
79
80 template<pRC::Allocation B, class R, If<IsConvertible<R, T>> = 0>
82 {
83 *this = other;
84 }
85
86 template<class R, If<IsConvertible<R, T>> = 0>
87 constexpr CommonArray(CArray<R, N> const &other)
89 {
90 }
91
92 template<class... Rs, If<All<IsConvertible<Rs, T>...>> = 0,
93 If<IsSatisfied<(sizeof...(Rs) == N)>> = 0>
94 constexpr CommonArray(Rs const &...values)
95 : mData{static_cast<T>(values)...}
96 {
97 }
98
99 template<pRC::Allocation B, class R, If<IsConvertible<R, T>> = 0>
100 constexpr auto &operator=(CommonArray<B, R, N> const &rhs) &
101 {
103 [this, &rhs](auto const i)
104 {
105 operator[](i) = rhs[i];
106 });
107 return *this;
108 }
109
110 template<class R, If<IsConvertible<R, T>> = 0>
111 constexpr auto &operator=(CArray<R, N> const &rhs) &
112 {
114 [this, &rhs](auto const i)
115 {
116 operator[](i) = rhs[i];
117 });
118 return *this;
119 }
120
121 constexpr decltype(auto) operator()(Index const subscript) &&
122 {
123 return move(*this)[subscript];
124 }
125
126 constexpr decltype(auto) operator()(Index const subscript) const &&
127 {
128 return move(*this)[subscript];
129 }
130
131 constexpr decltype(auto) operator()(Index const subscript) &
132 {
133 return operator[](subscript);
134 }
135
136 constexpr decltype(auto) operator()(Index const subscript) const &
137 {
138 return operator[](subscript);
139 }
140
141 constexpr decltype(auto) operator()(Subscripts<N> const &subscripts) &&
142 {
143 return move(*this)[Index(subscripts)];
144 }
145
146 constexpr decltype(auto) operator()(
147 Subscripts<N> const &subscripts) const &&
148 {
149 return move(*this)[Index(subscripts)];
150 }
151
152 constexpr decltype(auto) operator()(Subscripts<N> const &subscripts) &
153 {
154 return operator[](Index(subscripts));
155 }
156
157 constexpr decltype(auto) operator()(
158 Subscripts<N> const &subscripts) const &
159 {
160 return operator[](Index(subscripts));
161 }
162
163 constexpr decltype(auto) operator[](Index const index) &&
164 {
165 check(index);
166 return move(mData[index]);
167 }
168
169 constexpr decltype(auto) operator[](Index const index) const &&
170 {
171 check(index);
172 return move(mData[index]);
173 }
174
175 constexpr auto &operator[](Index const index) &
176 {
177 check(index);
178 return mData[index];
179 }
180
181 constexpr auto &operator[](Index const index) const &
182 {
183 check(index);
184 return mData[index];
185 }
186
187 constexpr auto data() && = delete;
188 constexpr auto data() const && = delete;
189
191 {
192 return mData;
193 }
194
195 constexpr auto data() const &
196 {
197 return mData;
198 }
199
200 private:
201 template<Index... Is, class R, If<IsConvertible<R, T>> = 0,
202 If<IsSatisfied<(sizeof...(Is) == N)>> = 0>
203 constexpr CommonArray(Sequence<Index, Is...> const,
204 CArray<R, N> const &other)
205 : mData{other[Is]...}
206 {
207 }
208
209 private:
210 BEGIN_IGNORE_DIAGNOSTIC_GCC("-Wpedantic")
211 alignas(alignment<sizeof(T) * N, alignof(T)>()) CArray<T, N> mData;
213 };
214}
215#endif // pRC_CORE_CONTAINER_ARRAY_STACK_H
constexpr CommonArray(CArray< R, N > const &other)
Definition array_stack.hpp:87
constexpr auto & operator=(CommonArray< B, R, N > const &rhs) &
Definition array_stack.hpp:100
constexpr CommonArray(Rs const &...values)
Definition array_stack.hpp:94
constexpr auto & operator=(CArray< R, N > const &rhs) &
Definition array_stack.hpp:111
static constexpr auto size()
Definition array_stack.hpp:28
constexpr CommonArray & operator=(CommonArray &&) &=default
static constexpr auto indexToSubscripts(Index const index)
Definition array_stack.hpp:38
constexpr CommonArray(CommonArray const &)=default
static constexpr auto subscriptsToIndex(Index const subscripts)
Definition array_stack.hpp:43
constexpr auto data() const &
Definition array_stack.hpp:195
Constant< pRC::Allocation, pRC::Allocation::Stack > Allocation
Definition array_stack.hpp:23
static constexpr auto size(Index const dimension)
Definition array_stack.hpp:33
constexpr auto data() const &&=delete
constexpr CommonArray(CommonArray< B, R, N > const &other)
Definition array_stack.hpp:81
constexpr CommonArray & operator=(CommonArray const &) &=default
static constexpr auto subscriptsToIndex(Subscripts< N > const &subscripts)
Definition array_stack.hpp:48
typename Sizes::Dimension Dimension
Definition array_stack.hpp:26
constexpr CommonArray(CommonArray &&)=default
constexpr auto & operator[](Index const index) &
Definition array_stack.hpp:175
constexpr auto & operator[](Index const index) const &
Definition array_stack.hpp:181
Definition type_traits.hpp:49
Definition sequence.hpp:56
static constexpr auto size()
Definition sequence.hpp:88
Constant< Size, sizeof...(Ns)> Dimension
Definition sequence.hpp:74
Definition sequence.hpp:34
Definition subscripts.hpp:20
TN::Subscripts S
Definition externs_nonTT.hpp:9
pRC::Float<> T
Definition externs_nonTT.hpp:1
static void error(Xs &&...args)
Definition log.hpp:14
Definition cholesky.hpp:18
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
Size Index
Definition type_traits.hpp:21
std::size_t Size
Definition type_traits.hpp:20
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
Constant< Bool, B > IsSatisfied
Definition type_traits.hpp:71
static constexpr auto makeSeries()
Definition sequence.hpp:351
CommonArray(T const (&)[N]) -> CommonArray< Allocation::Stack, T, N >
constexpr auto cDebugLevel
Definition config.hpp:46
std::integral_constant< T, V > Constant
Definition type_traits.hpp:34
static constexpr auto alignment()
Definition allocation.hpp:34
#define BEGIN_IGNORE_DIAGNOSTIC_CLANG(warning)
Definition pragma.hpp:45
#define END_IGNORE_DIAGNOSTIC_CLANG
Definition pragma.hpp:46
#define BEGIN_IGNORE_DIAGNOSTIC_GCC(warning)
Definition pragma.hpp:42
#define END_IGNORE_DIAGNOSTIC_GCC
Definition pragma.hpp:43