pRC
multi-purpose Tensor Train library for C++
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
11#include <prc/core/log/log.hpp>
12#include <prc/pragma.hpp>
13
14namespace pRC
15{
16 template<class T, Size N>
18 {
19 public:
21 using Type = T;
23 using Dimension = typename 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")
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
87 {
88 deallocate();
89 mData = rhs.mData;
90 rhs.mData = nullptr;
91
92 return *this;
93 }
94
95 template<pRC::Allocation B, class R, If<IsConvertible<R, T>> = 0>
97 {
98 allocate();
99 *this = other;
100 }
101
102 template<class R, If<IsConvertible<R, T>> = 0>
103 constexpr CommonArray(CArray<R, N> const &other)
105 {
106 }
107
108 template<class... Rs, If<All<IsConvertible<Rs, T>...>> = 0,
109 If<IsSatisfied<(sizeof...(Rs) == N)>> = 0>
110 constexpr CommonArray(Rs const &...values)
112 {
113 }
114
115 template<pRC::Allocation B, class R, If<IsConvertible<R, T>> = 0>
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<class R, If<IsConvertible<R, T>> = 0>
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
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, If<IsConvertible<R, T>> = 0,
218 If<IsSatisfied<(sizeof...(Is) == N)>> = 0>
219 constexpr CommonArray(Sequence<Index, Is...> const,
220 CArray<R, N> const &other)
221 : CommonArray(Sequence<Index, Is...>(), other[Is]...)
222 {
223 }
224
225 template<Index... Is, class... Rs, If<All<IsConvertible<Rs, T>...>> = 0,
226 If<IsSatisfied<((sizeof...(Rs) == N) && (sizeof...(Is) == N))>> = 0>
227 constexpr CommonArray(Sequence<Index, Is...> const, Rs const &...values)
228 {
229 allocate();
230 ((operator[](Is) = values), ...);
231 }
232
233 auto allocate()
234 {
235 if constexpr(cDebugLevel >= DebugLevel::Low)
236 {
237 if(mData != nullptr)
238 {
239 Logging::error("Heap memory has already been allocated.");
240 }
241 }
242
243 mData = new(std::nothrow) StackArray<T, N>;
244 }
245
246 auto deallocate()
247 {
248 if(mData != nullptr)
249 {
250 delete mData;
251 mData = nullptr;
252 }
253 }
254
255 constexpr auto validate() const
256 {
257 if constexpr(cDebugLevel >= DebugLevel::Mid)
258 {
259 if(mData == nullptr)
260 {
262 "Accessing invalidated dynamically allocated memory.");
263 }
264 }
265 }
266
267 private:
268 StackArray<T, N> *mData = nullptr;
269 };
270}
271#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
constexpr CommonArray(CArray< R, N > const &other)
Definition array_heap.hpp:103
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
typename Sizes::Dimension Dimension
Definition array_heap.hpp:23
constexpr CommonArray(Rs const &...values)
Definition array_heap.hpp:110
T Type
Definition array_heap.hpp:21
static constexpr auto subscriptsToIndex(Subscripts< N > const &subscripts)
Definition array_heap.hpp:45
auto & operator=(CommonArray &&rhs) &
Definition array_heap.hpp:86
constexpr CommonArray(CommonArray< B, R, N > const &other)
Definition array_heap.hpp:96
CommonArray()
Definition array_heap.hpp:64
static constexpr auto size(Index const dimension)
Definition array_heap.hpp:30
constexpr auto & operator=(CommonArray< B, R, N > const &rhs) &
Definition array_heap.hpp:116
Constant< pRC::Allocation, pRC::Allocation::Heap > Allocation
Definition array_heap.hpp:20
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
static void error(Xs &&...args)
Definition log.hpp:14
Definition cholesky.hpp:18
static constexpr auto makeSeriesFor()
Definition sequence.hpp:367
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
std::size_t Size
Definition type_traits.hpp:20
std::integral_constant< T, V > Constant
Definition type_traits.hpp:34
static constexpr auto makeSeries()
Definition sequence.hpp:361
Constant< Bool, B > IsSatisfied
Definition type_traits.hpp:71
static constexpr Conditional< IsSatisfied< C >, RemoveConstReference< X >, X > copy(X &&a)
Definition copy.hpp:13
constexpr auto cDebugLevel
Definition config.hpp:46
Size Index
Definition type_traits.hpp:21
#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