cMHN 1.2
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
10#include <prc/core/log/log.hpp>
11#include <prc/pragma.hpp>
12
13namespace pRC
14{
15 template<class T, Size N>
17 {
18 public:
19 static constexpr auto Allocation = pRC::Allocation::Stack;
20 using Type = T;
22
23 static constexpr auto Dimension = Sizes::Dimension;
24
25 static constexpr auto size()
26 {
27 return Sizes::size();
28 }
29
30 static constexpr auto size(Index const dimension)
31 {
32 return Sizes::size(dimension);
33 }
34
35 static constexpr auto indexToSubscripts(Index const index)
36 {
37 return Subscripts<N>(index);
38 }
39
40 static constexpr auto subscriptsToIndex(Index const subscripts)
41 {
42 return subscripts;
43 }
44
45 static constexpr auto subscriptsToIndex(Subscripts<N> const &subscripts)
46 {
47 return Index(subscripts);
48 }
49
50 private:
51 BEGIN_IGNORE_DIAGNOSTIC_GCC("-Wpedantic")
52 BEGIN_IGNORE_DIAGNOSTIC_CLANG("-Wzero-length-array")
53 template<class R, Size S>
54 using CArray = R[S];
57
58 static constexpr auto check([[maybe_unused]] Index const index)
59 {
60 if constexpr(cDebugLevel >= DebugLevel::Mid)
61 {
62 if(!(index < size()))
63 {
64 Logging::error("Array index out of range.");
65 }
66 }
67 }
68
69 public:
70 ~CommonArray() = default;
71 constexpr CommonArray(CommonArray const &) = default;
72 constexpr CommonArray(CommonArray &&) = default;
73 constexpr CommonArray &operator=(CommonArray const &) & = default;
74 constexpr CommonArray &operator=(CommonArray &&) & = default;
75 constexpr CommonArray() = default;
76
77 template<pRC::Allocation B, IsConvertible<T> R>
78 constexpr CommonArray(CommonArray<B, R, N> const &other)
79 {
80 *this = other;
81 }
82
83 template<IsConvertible<T> R>
84 constexpr CommonArray(CArray<R, N> const &other)
85 : CommonArray(makeSeries<Index, N>(), other)
86 {
87 }
88
89 template<IsConvertible<T>... Rs>
90 requires(sizeof...(Rs) == N)
91 constexpr CommonArray(Rs const &...values)
92 : mData{static_cast<T>(values)...}
93 {
94 }
95
96 template<pRC::Allocation B, IsConvertible<T> R>
97 constexpr auto &operator=(CommonArray<B, R, N> const &rhs) &
98 {
100 [this, &rhs](auto const i)
101 {
102 operator[](i) = rhs[i];
103 });
104 return *this;
105 }
106
107 template<IsConvertible<T> R>
108 constexpr auto &operator=(CArray<R, N> const &rhs) &
109 {
111 [this, &rhs](auto const i)
112 {
113 operator[](i) = rhs[i];
114 });
115 return *this;
116 }
117
118 constexpr decltype(auto) operator()(Index const subscript) &&
119 {
120 return move(*this)[subscript];
121 }
122
123 constexpr decltype(auto) operator()(Index const subscript) const &&
124 {
125 return move(*this)[subscript];
126 }
127
128 constexpr decltype(auto) operator()(Index const subscript) &
129 {
130 return operator[](subscript);
131 }
132
133 constexpr decltype(auto) operator()(Index const subscript) const &
134 {
135 return operator[](subscript);
136 }
137
138 constexpr decltype(auto) operator()(Subscripts<N> const &subscripts) &&
139 {
140 return move(*this)[Index(subscripts)];
141 }
142
143 constexpr decltype(auto) operator()(
144 Subscripts<N> const &subscripts) const &&
145 {
146 return move(*this)[Index(subscripts)];
147 }
148
149 constexpr decltype(auto) operator()(Subscripts<N> const &subscripts) &
150 {
151 return operator[](Index(subscripts));
152 }
153
154 constexpr decltype(auto) operator()(
155 Subscripts<N> const &subscripts) const &
156 {
157 return operator[](Index(subscripts));
158 }
159
160 constexpr decltype(auto) operator[](Index const index) &&
161 {
162 check(index);
163 return move(mData[index]);
164 }
165
166 constexpr decltype(auto) operator[](Index const index) const &&
167 {
168 check(index);
169 return move(mData[index]);
170 }
171
172 constexpr auto &operator[](Index const index) &
173 {
174 check(index);
175 return mData[index];
176 }
177
178 constexpr auto &operator[](Index const index) const &
179 {
180 check(index);
181 return mData[index];
182 }
183
184 constexpr auto data() && = delete;
185 constexpr auto data() const && = delete;
186
187 constexpr auto data() &
188 {
189 return mData;
190 }
191
192 constexpr auto data() const &
193 {
194 return mData;
195 }
196
197 private:
198 template<Index... Is, class R>
199 constexpr CommonArray(Sequence<Index, Is...> const,
200 CArray<R, N> const &other)
201 : mData{other[Is]...}
202 {
203 }
204
205 private:
206 BEGIN_IGNORE_DIAGNOSTIC_GCC("-Wpedantic")
207 alignas(alignment<sizeof(T) * N, alignof(T)>()) CArray<T, N> mData;
209 };
210}
211#endif // pRC_CORE_CONTAINER_ARRAY_STACK_H
constexpr auto & operator=(CArray< R, N > const &rhs) &
Definition array_stack.hpp:108
static constexpr auto size()
Definition array_stack.hpp:25
constexpr CommonArray & operator=(CommonArray &&) &=default
static constexpr auto indexToSubscripts(Index const index)
Definition array_stack.hpp:35
constexpr CommonArray(CommonArray const &)=default
constexpr CommonArray(CommonArray< B, R, N > const &other)
Definition array_stack.hpp:78
static constexpr auto subscriptsToIndex(Index const subscripts)
Definition array_stack.hpp:40
constexpr CommonArray(CArray< R, N > const &other)
Definition array_stack.hpp:84
constexpr CommonArray(Rs const &...values)
Definition array_stack.hpp:91
constexpr auto data() const &
Definition array_stack.hpp:192
static constexpr auto size(Index const dimension)
Definition array_stack.hpp:30
constexpr auto data() const &&=delete
constexpr CommonArray & operator=(CommonArray const &) &=default
static constexpr auto subscriptsToIndex(Subscripts< N > const &subscripts)
Definition array_stack.hpp:45
constexpr CommonArray(CommonArray &&)=default
constexpr auto & operator=(CommonArray< B, R, N > const &rhs) &
Definition array_stack.hpp:97
constexpr auto & operator[](Index const index) &
Definition array_stack.hpp:172
constexpr auto & operator[](Index const index) const &
Definition array_stack.hpp:178
Definition declarations.hpp:12
Definition value.hpp:12
Definition sequence.hpp:29
static constexpr Size Dimension
Definition sequence.hpp:47
static constexpr auto size()
Definition sequence.hpp:69
Definition subscripts.hpp:21
TN::Subscripts S
Definition externs_nonTT.hpp:9
pRC::Float<> T
Definition externs_nonTT.hpp:1
int i
Definition gmock-matchers-comparisons_test.cc:603
static void error(Xs &&...args)
Definition log.hpp:14
Definition cholesky.hpp:10
Size Index
Definition basics.hpp:32
std::size_t Size
Definition basics.hpp:31
static consteval auto alignment()
Definition allocation.hpp:30
static constexpr auto makeSeries()
Definition sequence.hpp:390
CommonArray(T const (&)[N]) -> CommonArray< Allocation::Stack, T, N >
constexpr auto cDebugLevel
Definition config.hpp:48
static constexpr auto range(F &&f, Xs &&...args)
Definition range.hpp:18
Allocation
Definition allocation.hpp:18
#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