cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
array_heap.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_CONTAINER_ARRAY_HEAP_H
4#define pRC_CORE_CONTAINER_ARRAY_HEAP_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::Heap;
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 public:
60 {
61 deallocate();
62 }
63
65 {
66 allocate();
67 }
68
70 {
71 allocate();
72 *this = other;
73 }
74
76 : mData(other.mData)
77 {
78 other.mData = nullptr;
79 }
80
81 auto &operator=(CommonArray const &rhs) &
82 {
83 return operator= <Allocation, T>(rhs);
84 }
85
86 auto &operator=(CommonArray &&rhs) &
87 {
88 deallocate();
89 mData = rhs.mData;
90 rhs.mData = nullptr;
91
92 return *this;
93 }
94
95 template<pRC::Allocation B, IsConvertible<T> R>
96 constexpr CommonArray(CommonArray<B, R, N> const &other)
97 {
98 allocate();
99 *this = other;
100 }
101
102 template<IsConvertible<T> R>
103 constexpr CommonArray(CArray<R, N> const &other)
104 : CommonArray(makeSeries<Index, N>(), other)
105 {
106 }
107
108 template<IsConvertible<T>... Rs>
109 requires(sizeof...(Rs) == N)
110 constexpr CommonArray(Rs const &...values)
111 : CommonArray(makeSeriesFor<Index, Rs...>(), values...)
112 {
113 }
114
115 template<pRC::Allocation B, IsConvertible<T> R>
116 constexpr auto &operator=(CommonArray<B, R, N> const &rhs) &
117 {
119 [this, &rhs](auto const i)
120 {
121 operator[](i) = rhs[i];
122 });
123 return *this;
124 }
125
126 template<IsConvertible<T> R>
127 constexpr auto &operator=(CArray<R, N> const &rhs) &
128 {
130 [this, &rhs](auto const i)
131 {
132 operator[](i) = rhs[i];
133 });
134 return *this;
135 }
136
137 constexpr decltype(auto) operator()(Index const subscript) &&
138 {
139 return move(*this)[subscript];
140 }
141
142 constexpr decltype(auto) operator()(Index const subscript) const &&
143 {
144 return move(*this)[subscript];
145 }
146
147 constexpr decltype(auto) operator()(Index const subscript) &
148 {
149 return operator[](subscript);
150 }
151
152 constexpr decltype(auto) operator()(Index const subscript) const &
153 {
154 return operator[](subscript);
155 }
156
157 constexpr decltype(auto) operator()(Subscripts<N> const &subscripts) &&
158 {
159 return move(*this)[Index(subscripts)];
160 }
161
162 constexpr decltype(auto) operator()(
163 Subscripts<N> const &subscripts) const &&
164 {
165 return move(*this)[Index(subscripts)];
166 }
167
168 constexpr decltype(auto) operator()(Subscripts<N> const &subscripts) &
169 {
170 return operator[](Index(subscripts));
171 }
172
173 constexpr decltype(auto) operator()(
174 Subscripts<N> const &subscripts) const &
175 {
176 return operator[](Index(subscripts));
177 }
178
179 constexpr decltype(auto) operator[](Index const index) &&
180 {
181 validate();
182 return move(*mData)[index];
183 }
184
185 constexpr decltype(auto) operator[](Index const index) const &&
186 {
187 validate();
188 return move(*mData)[index];
189 }
190
191 constexpr decltype(auto) operator[](Index const index) &
192 {
193 validate();
194 return (*mData)[index];
195 }
196
197 constexpr decltype(auto) operator[](Index const index) const &
198 {
199 validate();
200 return (*mData)[index];
201 }
202
203 constexpr auto data() && = delete;
204 constexpr auto data() const && = delete;
205
206 constexpr auto data() &
207 {
208 return mData->data();
209 }
210
211 constexpr auto data() const &
212 {
213 return mData->data();
214 }
215
216 private:
217 template<Index... Is, class R>
218 constexpr CommonArray(Sequence<Index, Is...> const,
219 CArray<R, N> const &other)
220 : CommonArray(Sequence<Index, Is...>(), other[Is]...)
221 {
222 }
223
224 template<Index... Is, class... Rs>
225 constexpr CommonArray(Sequence<Index, Is...> const, Rs const &...values)
226 {
227 allocate();
228 ((operator[](Is) = values), ...);
229 }
230
231 auto allocate()
232 {
233 if constexpr(cDebugLevel >= DebugLevel::Low)
234 {
235 if(mData != nullptr)
236 {
237 Logging::error("Heap memory has already been allocated.");
238 }
239 }
240
241 mData = new(std::nothrow) StackArray<T, N>;
242 }
243
244 auto deallocate()
245 {
246 if(mData != nullptr)
247 {
248 delete mData;
249 mData = nullptr;
250 }
251 }
252
253 constexpr auto validate() const
254 {
255 if constexpr(cDebugLevel >= DebugLevel::Mid)
256 {
257 if(mData == nullptr)
258 {
260 "Accessing invalidated dynamically allocated memory.");
261 }
262 }
263 }
264
265 private:
266 StackArray<T, N> *mData = nullptr;
267 };
268}
269#endif // pRC_CORE_CONTAINER_ARRAY_HEAP_H
constexpr auto data() const &&=delete
static constexpr auto indexToSubscripts(Index const index)
Definition array_heap.hpp:35
CommonArray(CommonArray const &other)
Definition array_heap.hpp:69
static constexpr auto size()
Definition array_heap.hpp:25
CommonArray(CommonArray &&other)
Definition array_heap.hpp:75
static constexpr auto subscriptsToIndex(Index const subscripts)
Definition array_heap.hpp:40
constexpr auto & operator=(CArray< R, N > const &rhs) &
Definition array_heap.hpp:127
constexpr auto data() const &
Definition array_heap.hpp:211
auto & operator=(CommonArray const &rhs) &
Definition array_heap.hpp:81
constexpr CommonArray(CommonArray< B, R, N > const &other)
Definition array_heap.hpp:96
static constexpr auto subscriptsToIndex(Subscripts< N > const &subscripts)
Definition array_heap.hpp:45
auto & operator=(CommonArray &&rhs) &
Definition array_heap.hpp:86
constexpr auto & operator=(CommonArray< B, R, N > const &rhs) &
Definition array_heap.hpp:116
CommonArray()
Definition array_heap.hpp:64
static constexpr auto size(Index const dimension)
Definition array_heap.hpp:30
constexpr CommonArray(Rs const &...values)
Definition array_heap.hpp:110
constexpr CommonArray(CArray< R, N > const &other)
Definition array_heap.hpp:103
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 constexpr auto makeSeriesFor()
Definition sequence.hpp:399
static constexpr auto makeSeries()
Definition sequence.hpp:390
CommonArray(T const (&)[N]) -> CommonArray< Allocation::Stack, T, N >
CommonArray< Allocation::Stack, T, Ns... > StackArray
Definition declarations.hpp:15
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