CNORXZ
Container with Native Operation Routines and Expressions
Loading...
Searching...
No Matches
prange.cc.h
Go to the documentation of this file.
1// -*- C++ -*-
12#ifndef __cxz_prange_cc_h__
13#define __cxz_prange_cc_h__
14
15#include "prange.h"
16#include "urange.h"
17
18namespace CNORXZ
19{
20 /*============+
21 | PIndex |
22 +============*/
23
24 template <class IndexT>
27 mRangePtr(rangeCast<RangeType>(range))//,
28 //mOrig(std::make_shared<IndexT>(mRangePtr->orig(),mRangePtr->parts()[pos]))
29 {
30 auto o = mRangePtr->orig();
31 auto p = mRangePtr->parts()[pos];
32 mOrig = std::make_shared<IndexT>(o,p);
33 }
34
35 template <class IndexT>
37 {
38 IB::mPos = lexpos;
39 *mOrig = mRangePtr->parts()[IB::mPos];
40 return *this;
41 }
42
43 template <class IndexT>
45 {
46 ++IB::mPos;
47 *mOrig = mRangePtr->parts()[IB::mPos];
48 return *this;
49 }
50
51 template <class IndexT>
53 {
54 --IB::mPos;
55 *mOrig = mRangePtr->parts()[IB::mPos];
56 return *this;
57 }
58
59 template <class IndexT>
61 {
62 return PIndex(mRangePtr, IB::mPos + n);
63 }
64
65 template <class IndexT>
67 {
68 return lex() - i.lex();
69 }
70
71 template <class IndexT>
73 {
74 return PIndex(mRangePtr, IB::mPos - n);
75 }
76
77 template <class IndexT>
79 {
80 IB::mPos += n;
81 *mOrig = mRangePtr->parts()[IB::mPos];
82 return *this;
83 }
84
85 template <class IndexT>
87 {
88 IB::mPos -= n;
89 *mOrig = mRangePtr->parts()[IB::mPos];
90 return *this;
91 }
92
93 template <class IndexT>
95 {
96 return IB::mPos;
97 }
98
99 template <class IndexT>
101 {
102 return UPos(mRangePtr->size());
103 }
104
105 template <class IndexT>
107 {
108 return UPos(mRangePtr->size());
109 }
110
111 template <class IndexT>
113 {
114 return IndexId<0>(this->ptrId());
115 }
116
117 template <class IndexT>
118 decltype(auto) PIndex<IndexT>::operator*() const
119 {
120 return **mOrig;
121 }
122
123 template <class IndexT>
125 {
126 return 1;
127 }
128
129 template <class IndexT>
131 {
132 return mRangePtr;
133 }
134
135 template <class IndexT>
136 template <SizeT I>
138 {
139 if(id == this->id()){
140 return UPos(1);
141 }
142 else {
143 return mOrig->stepSize(id);
144 }
145 }
146
147 template <class IndexT>
149 {
150 CXZ_ASSERT(last >= *this, "got last index position (" << last.lex()
151 << ") smaller than begin index position (" << lex() << ")");
152 auto oi = *orig();
153 auto olast = *last.orig();
154 const SizeT beginPos = oi.lex();
155 Vector<SizeT> parts(olast.lex()-beginPos+1);
156 for(auto i = oi; i != olast+1; ++i){
157 parts[i.lex()-beginPos] = i.lex();
158 }
159 auto x = CNORXZ::prange(mRangePtr->orig(), parts);
160 return x;
161 }
162
163 template <class IndexT>
164 decltype(auto) PIndex<IndexT>::deepFormat() const
165 {
166 return mOrig->deepFormat();
167 }
168
169 template <class IndexT>
170 decltype(auto) PIndex<IndexT>::deepMax() const
171 {
172 return mOrig->deepMax();
173 }
174
175 template <class IndexT>
177 {
178 CXZ_ASSERT(f[0]*s[0] == lmax().val(), "got wrong extension: " << f[0]*s[0]
179 << " vs " << lmax().val());
180 CXZ_ASSERT(CNORXZ::formatIsTrivial(f,s), "format is not trivial: f = " << toString(f)
181 << ", s = " << toString(s));
182 return *this;
183 }
184
185 template <class IndexT>
187 {
188 return mOrig->stringMeta();
189 }
190
191 template <class IndexT>
192 decltype(auto) PIndex<IndexT>::meta() const
193 {
194 return mOrig->meta();
195 }
196
197 template <class IndexT>
199 {
200 mOrig->at(metaPos);
201 mkPos();
202 return *this;
203 }
204
205 template <class IndexT>
206 decltype(auto) PIndex<IndexT>::xpr(const Sptr<PIndex<IndexT>>& _this) const
207 {
208 return poproot( _this, mRangePtr->parts(), mOrig->xpr(mOrig) );
209 }
210
211 template <class IndexT>
212 template <class Xpr, class F>
213 decltype(auto) PIndex<IndexT>::ifor(const Xpr& xpr, F&& f) const
214 {
215 return PFor<0,0,Xpr,F>(this->lmax().val(), this->id(), mOrig->id(),
216 mRangePtr->parts().data(), xpr, std::forward<F>(f));
217 }
218
219 template <class IndexT>
221 {
222 return mOrig->formatIsTrivial();
223 }
224
225 template <class IndexT>
227 {
228 mkPos();
229 return *this;
230 }
231
232 template <class IndexT>
234 {
235 mOrig = i;
236 mkPos();
237 return *this;
238 }
239
240 template <class IndexT>
242 {
243 return mOrig;
244 }
245
246 /*======================+
247 | PIndex (private) |
248 +======================*/
249
250 template <class IndexT>
252 {
253 const SizeT opos = mOrig->lex();
254 IB::mPos = 0;
255 for(const auto& x: mRangePtr->parts()){
256 if(x == opos){
257 return;
258 }
259 ++IB::mPos;
260 }
261 CXZ_ERROR("meta position '" << toString(mOrig->meta()) << "' not part of range");
262 }
263
264 /*=========================+
265 | PIndex (non-member) |
266 +=========================*/
267
268 template <class I, class I1>
269 decltype(auto) operator*(const Sptr<PIndex<I>>& a, const Sptr<I1>& b)
270 {
271 return iptrMul(a, b);
272 }
273
274 /*===================+
275 | PRangeFactory |
276 +===================*/
277
278 template <class RangeT>
280 mRange(range), mParts(_parts)
281 {}
282
283 template <class RangeT>
285 mRange(rangeCast<RangeT>(range)), mParts(_parts)
286 {}
287
288 template <class RangeT>
290 {
291 RangePtr purange = urange(mParts);
292 const Vector<Uuid> key = { mRange->id(), purange->id() };
293 const auto& info = typeid(PRange<RangeT>);
294 mProd = this->fromCreated(info, key);
295 if(mProd == nullptr) {
296 mProd = std::shared_ptr<PRange<RangeT>>( new PRange<RangeT>(mRange, mParts) );
297 this->addToCreated(info, key, mProd);
298 }
299 }
300
301 /*============+
302 | PRange |
303 +============*/
304
305 template <class RangeT>
307 {
308 return mParts.size()-1;
309 }
310
311 template <class RangeT>
313 {
314 return 1;
315 }
316
317 template <class RangeT>
319 {
320 return mRange->stringMeta( mParts[pos] );
321 }
322
323 template <class RangeT>
325 {
326 return typeid(PRange<RangeT>);
327 }
328
329 template <class RangeT>
331 {
332 return mRange->metaType();
333 }
334
335 template <class RangeT>
337 {
338 if(r->type() == type()){
339 Sptr<PRange<RangeT>> rx = std::dynamic_pointer_cast<PRange<RangeT>>(r);
340 if(rx->orig() == orig()){
341 Vector<SizeT> p(parts());
342 for(auto i: rx->parts()){
343 if(find(p.begin(), p.end(), i) != p.end()){
344 p.push_back(i);
345 }
346 }
347 return prange(orig(), p);
348 }
349 }
350 return derive()->extend(r);
351 }
352
353 template <class RangeT>
355 {
356 return mRange;
357 }
358
359 template <class RangeT>
361 {
362 return mParts;
363 }
364
365 template <class RangeT>
367 {
368 Vector<MetaType> meta(this->size());
369 auto i = mRange->begin();
370 SizeT j = 0;
371 for(const auto& p: mParts){
372 meta[j++] = *(i = p);
373 }
374 return URangeFactory<MetaType>( meta ).create();
375 }
376
377
378 /*======================+
379 | PRange (private) |
380 +======================*/
381
382 template <class RangeT>
384 mRange(range), mParts(_parts)
385 {
386 const auto max = std::max_element( mParts.begin(), mParts.end() );
387 mParts.push_back( *max+1 );
388 }
389
390 template <class RangeT>
391 Vector<Uuid> PRange<RangeT>::key() const
392 {
393 return Vector<Uuid> { mRange->id() };
394 }
395
396 /*==========================+
397 | non-member functions |
398 +==========================*/
399
400 template <class RangeT>
401 RangePtr prange(const Sptr<RangeT>& range, const Vector<SizeT>& parts)
402 {
403 return PRangeFactory<RangeT>(range,parts).create();
404 }
405
406}
407
408#endif
#define CXZ_ERROR(errmsg)
Definition assert.h:26
#define CXZ_ASSERT(statement, errmsg)
Definition assert.h:40
const Sptr< IndexT > & orig() const
Definition prange.cc.h:241
PIndex & operator-=(Int n)
Definition prange.cc.h:86
RangePtr prange(const PIndex< IndexT > &last) const
Definition prange.cc.h:148
SizeT lex() const
Definition prange.cc.h:94
UPos stepSize(const IndexId< I > &id) const
Definition prange.cc.h:137
UPos pmax() const
Definition prange.cc.h:100
SizeT dim() const
Definition prange.cc.h:124
PIndex operator-(Int n) const
Definition prange.cc.h:72
PIndex & operator--()
Definition prange.cc.h:52
String stringMeta() const
Definition prange.cc.h:186
UPos lmax() const
Definition prange.cc.h:106
PIndex & operator=(SizeT lexpos)
Definition prange.cc.h:36
PIndex & reformat(const Vector< SizeT > &f, const Vector< SizeT > &s)
Definition prange.cc.h:176
PIndex & operator+=(Int n)
Definition prange.cc.h:78
decltype(auto) deepMax() const
Definition prange.cc.h:170
decltype(auto) meta() const
Definition prange.cc.h:192
PIndex operator+(Int n) const
Definition prange.cc.h:60
IndexT::MetaType MetaType
Definition prange.h:33
PIndex & operator()()
Definition prange.cc.h:226
PIndex & operator++()
Definition prange.cc.h:44
bool formatIsTrivial() const
Definition prange.cc.h:220
PIndex & at(const MetaType &metaPos)
Definition prange.cc.h:198
IndexId< 0 > id() const
Definition prange.cc.h:112
decltype(auto) deepFormat() const
Definition prange.cc.h:164
PIndex(const RangePtr &range, SizeT pos=0)
Definition prange.cc.h:25
Sptr< RangeType > range() const
Definition prange.cc.h:130
RangePtr derive() const
Definition prange.cc.h:366
const Vector< SizeT > & parts() const
Definition prange.cc.h:360
virtual SizeT size() const override final
Definition prange.cc.h:306
virtual const TypeInfo & type() const override final
Definition prange.cc.h:324
virtual RangePtr extend(const RangePtr &r) const override final
Definition prange.cc.h:336
virtual SizeT dim() const override final
Definition prange.cc.h:312
virtual const TypeInfo & metaType() const override final
Definition prange.cc.h:330
virtual String stringMeta(SizeT pos) const override final
Definition prange.cc.h:318
Sptr< RangeT > orig() const
Definition prange.cc.h:354
std::string String
Definition types.h:42
std::type_info TypeInfo
Definition types.h:71
RangePtr urange(const Vector< MetaT > &space)
Definition urange.cc.h:467
constexpr decltype(auto) poproot(const Sptr< IndexT > &ind, const SizeT *parts, Op &&op)
uint64_t SizeT
Definition types.h:38
Sptr< RangeBase > RangePtr
Definition types.h:157
std::vector< T, Allocator< T > > Vector
Definition types.h:310
int32_t Int
Definition types.h:36
decltype(auto) xpr(const Sptr< I > &i)
Sptr< Range > rangeCast(const RangePtr r)
decltype(auto) iptrMul(const Sptr< I1 > &a, const Sptr< I2 > &b)
bool formatIsTrivial(const Vector< SizeT > &f, const Vector< SizeT > &s)
String toString(const T &a)
RangePtr prange(const Sptr< RangeT > &range, const Vector< SizeT > &parts)
Definition prange.cc.h:401
std::shared_ptr< T > Sptr
Definition types.h:48
PRange, PRangeFactory and PIndex declaration.
URange, URangeFactory and UIndex declaration.