CNORXZ
Container with Native Operation Routines and Expressions
Loading...
Searching...
No Matches
array_base.cc.h
Go to the documentation of this file.
1// -*- C++ -*-
12#ifndef __cxz_array_base_cc_h__
13#define __cxz_array_base_cc_h__
14
15#include "array_base.h"
16#include "slice.h"
17
18namespace CNORXZ
19{
20 /*=======================================================+
21 | Implementations for CArrayBase member functions |
22 +=======================================================*/
23
24 template <typename T>
26 mRange(rangeCast<YRange>(range))
27 {}
28
29 template <typename T>
30 template <typename I, typename M>
32 {
33 if(formatIsTrivial()){
34 return data()[i.lex()];
35 }
36 else {
37 auto ai = itLex(i);
38 return *ai;
39 }
40 }
41
42 template <typename T>
43 template <typename I, typename M>
44 const T& CArrayBase<T>::at(const IndexInterface<I,M>& i) const
45 {
46 auto ai = itLexSave(i);
47 return *ai;
48 }
49
50 template <typename T>
51 template <class... Indices>
52 const T& CArrayBase<T>::operator[](const SPack<Indices...>& pack) const
53 {
55 return data()[pack.lex()];
56 }
57 else {
58 auto ai = itLex(pack);
59 return *ai;
60 }
61 }
62
63 template <typename T>
64 template <class... Indices>
65 const T& CArrayBase<T>::at(const SPack<Indices...>& pack) const
66 {
67 auto ai = itLexSave(pack);
68 return *ai;
69 }
70
71 template <typename T>
72 const T& CArrayBase<T>::operator[](const DPack& pack) const
73 {
74 auto ai = itLex(pack);
75 return *ai;
76 }
77
78 template <typename T>
79 const T& CArrayBase<T>::at(const DPack& pack) const
80 {
81 auto ai = itLexSave(pack);
82 return *ai;
83 }
84
85 template <typename T>
86 template <typename I, typename M>
88 const IndexInterface<I,M>& end) const
89 {
90 auto ai = itLexSave(begin);
91 auto aj = itLexSave(end);
92 return std::make_shared<CSlice<T>>(ai.prange(aj), this, ai.format(), ai.pos());
93 }
94
95 template <typename T>
97 {
98 return mRange->size();
99 }
101 template <typename T>
103 {
104 return mRange;
105 }
106
107 template <typename T>
109 {
110 return this->cbegin();
111 }
112
113 template <typename T>
116 return this->cend();
117 }
118
119 template <typename T>
120 template <class Index>
122 {
123 if(formatIsTrivial()){
124 // assert that index format is trivial and has correct extensions
125 CXZ_ASSERT(i->formatIsTrivial(),
126 "got non-trivial index for container with trivial format");
127 this->checkFormatCompatibility(*i);
128 return coproot(*this, i);
130 else {
131 if(i->formatIsTrivial()){
132 // try to apply container format.
133 // if the reformat changes the index type in any manner
134 // the format is not applicable:
135 if constexpr(std::is_same<decltype(i->reformat( Vector<SizeT>(), Vector<SizeT>() )),Index>::value){
136 auto beg = begin();
137 auto aformat = beg.deepFormat();
138 auto amax = beg.deepMax();
139 auto fi = i->reformat( aformat, amax );
140 return coproot(*this, moveToPtr( fi ) );
141 }
142 else {
143 this->checkFormatCompatibility(*i);
144 return coproot(*this, i);
145 }
146 }
147 else {
148 // check if format is compatible
149 this->checkFormatCompatibility(*i);
150 return coproot(*this, i);
151 }
152 }
153 }
154
155 template <typename T>
156 template <class... Indices>
157 inline decltype(auto) CArrayBase<T>::operator()(const SPack<Indices...>& pack) const
158 {
159 return operator()(mindexPtr(pack));
160 }
161
162 template <typename T>
163 inline decltype(auto) CArrayBase<T>::operator()(const DPack& pack) const
164 {
165 return operator()(yindexPtr(pack));
167
168 /*=================================================================+
169 | Implementations for protected CArrayBase member functions |
170 +=================================================================*/
171
172 template <typename T>
173 template <class Acc>
175 {
176 return begin() + acc.lex();
177 }
178
179 template <typename T>
180 template <class Acc>
182 {
183 CXZ_ASSERT(acc.lex() < this->size(), "index out of range");
184 // check further compatibility of index/range format!!!
185 return begin() + acc.lex();
186 }
187
188 template <typename T>
189 template <class Acc>
191 {
192 auto j = begin();
193 CXZ_ASSERT(acc.lmax().val() == j.lmax().val(),
194 "got index of iteration space size = " << acc.lmax().val()
195 << ", expected size = " << acc.lmax().val());
196 Vector<SizeT> f1 = toVec(acc.deepFormat());
197 Vector<SizeT> f2 = j.deepFormat();
198 std::sort(f1.begin(),f1.end());
199 std::sort(f2.begin(),f2.end());
200 SizeT i1 = 0;
201 SizeT i2 = 0;
202 CXZ_ASSERT(f1[i1] == f2[i2], "obtained format " << toString(f1)
203 << ", which is incompatible to target format " << toString(f2));
204 ++i1;
205 ++i2;
206 while(i1 < f1.size() and i2 < f2.size()){
207 if(f1[i1] < f2[i2]) {
208 if(++i1 == f1.size()) break;
209 CXZ_ASSERT(f1[i1] <= f2[i2], "obtained format " << toString(f1)
210 << ", which is incompatible to target format " << toString(f2));
211 }
212 else if(f1[i1] > f2[i2]) {
213 if(++i2 == f2.size()) break;
214 CXZ_ASSERT(f1[i1] >= f2[i2], "obtained format " << toString(f1)
215 << ", which is incompatible to target format " << toString(f2));
216 }
217 else {
218 ++i1;
219 ++i2;
221 }
222 }
223
224 /*======================================================+
225 | Implementations for ArrayBase member functions |
226 +======================================================*/
227
228 template <typename T>
230 CArrayBase<T>(range)
231 {}
232
233 template <typename T>
234 template <typename I, typename M>
236 {
237 if(this->formatIsTrivial()){
238 return data()[i.lex()];
239 }
240 else {
241 auto ai = itLex(i);
242 return *ai;
244 }
245
246 template <typename T>
247 template <typename I, typename M>
249 {
250 auto ai = itLexSave(i);
251 return *ai;
253
254 template <typename T>
255 template <class... Indices>
258 if(this->formatIsTrivial()){
259 return data()[pack.lex()];
260 }
261 else {
262 auto ai = itLex(pack);
263 return *ai;
264 }
265 }
266
267 template <typename T>
268 template <class... Indices>
270 {
271 auto ai = itLexSave(pack);
272 return *ai;
274
275 template <typename T>
277 {
278 auto ai = itLex(pack);
279 return *ai;
281
282 template <typename T>
283 T& ArrayBase<T>::at(const DPack& pack)
284 {
285 auto ai = itLexSave(pack);
286 return *ai;
287 }
288
289 template <typename T>
290 template <typename I, typename M>
292 const IndexInterface<I,M>& end)
293 {
294 auto ai = itLexSave(begin);
295 auto aj = itLexSave(end);
296 return std::make_shared<Slice<T>>(ai.prange(aj), this, ai.format(), ai.pos());
297 }
298
299 template <typename T>
301 {
302 return iterator(this->data(), this->cbegin());
303 }
304
305 template <typename T>
307 {
308 return iterator(this->data(), this->cend());
309 }
310
311 template <typename T>
312 template <class Index>
314 {
315 if(this->formatIsTrivial()){
316 // assert that index format is trivial and has correct extensions
317 CXZ_ASSERT(i->formatIsTrivial(),
318 "got non-trivial index for container with trivial format");
319 this->checkFormatCompatibility(*i);
320 return oproot(*this, i);
321 }
322 else {
323 if(i->formatIsTrivial()){
324 // try to apply container format.
325 // if the reformat changes the index type in any manner
326 // the format is not applicable:
327 if constexpr(std::is_same<decltype(i->reformat( Vector<SizeT>(), Vector<SizeT>() )),Index>::value){
328 auto beg = begin();
329 auto aformat = beg.deepFormat();
330 auto amax = beg.deepMax();
331 auto fi = i->reformat( aformat, amax );
332 return oproot(*this, moveToPtr( fi ) );
333 }
334 else {
335 this->checkFormatCompatibility(*i);
336 return oproot(*this, i);
337 }
338 }
339 else {
340 // check if format is compatible
341 this->checkFormatCompatibility(*i);
342 return oproot(*this, i);
343 }
344 }
345 }
346
347 template <typename T>
348 template <class... Indices>
349 inline decltype(auto) ArrayBase<T>::operator()(const SPack<Indices...>& pack)
350 {
351 return operator()(mindexPtr(pack));
352 }
353
354 template <typename T>
355 inline decltype(auto) ArrayBase<T>::operator()(const DPack& pack)
356 {
357 return operator()(yindexPtr(pack));
358 }
359
360 /*================================================================+
361 | Implementations for protected ArrayBase member functions |
362 +================================================================*/
363
364 template <typename T>
365 template <class Acc>
367 {
368 return begin() + acc.lex();
369 }
370
371 template <typename T>
372 template <class Acc>
374 {
375 CXZ_ASSERT(acc.lex() < this->size(), "index out of range");
376 // check further compatibility of index/range format!!!
377 return begin() + acc.lex();
378 }
379
380}
381
382#endif
Array base class declarations.
#define CXZ_ASSERT(statement, errmsg)
Definition assert.h:40
Sptr< ArrayBase< T > > sl(const IndexInterface< I, M > &begin, const IndexInterface< I, M > &end)
iterator itLex(const Acc &acc)
OpRoot< T, Index > operator()(const Sptr< Index > &i)
T & operator[](const IndexInterface< I, M > &i)
virtual iterator end()
virtual iterator begin()
ArrayBase(const RangePtr &range)
T & at(const IndexInterface< I, M > &i)
iterator itLexSave(const Acc &acc)
COpRoot< T, Index > operator()(const Sptr< Index > &i) const
CArrayBase(const RangePtr &range)
virtual RangePtr range() const
const T & operator[](const IndexInterface< I, M > &i) const
virtual const_iterator begin() const
Sptr< CArrayBase< T > > sl(const IndexInterface< I, M > &begin, const IndexInterface< I, M > &end) const
const T & at(const IndexInterface< I, M > &i) const
const_iterator itLex(const Acc &acc) const
void checkFormatCompatibility(const Acc &acc) const
const_iterator itLexSave(const Acc &acc) const
virtual const_iterator end() const
virtual SizeT size() const
SizeT lex() const
Definition index_base.h:94
SizeT lex() const
Sptr< I > moveToPtr(I &&i)
uint64_t SizeT
Definition types.h:38
constexpr decltype(auto) coproot(const CArrayBase< T > &a, const Sptr< IndexT > &ind)
Sptr< RangeBase > RangePtr
Definition types.h:157
Sptr< YIndex > yindexPtr(const DPack &is)
Definition yrange.cc:534
std::vector< T, Allocator< T > > Vector
Definition types.h:310
Sptr< Range > rangeCast(const RangePtr r)
bool formatIsTrivial(const Vector< SizeT > &f, const Vector< SizeT > &s)
String toString(const T &a)
constexpr decltype(auto) mindexPtr(const SPack< Indices... > &pack)
Definition mrange.cc.h:629
std::shared_ptr< T > Sptr
Definition types.h:48
Vector< T > toVec(const Arr< T, N > &a)
Definition utils.h:22
constexpr decltype(auto) oproot(ArrayBase< T > &a, const Sptr< IndexT > &ind)
Slice declarations.