CNORXZ
Container with Native Operation Routines and Expressions
Loading...
Searching...
No Matches
srange.cc.h
Go to the documentation of this file.
1// -*- C++ -*-
12#ifndef __cxz_range_cc_h__
13#define __cxz_range_cc_h__
14
15#include "srange.h"
16#include "prange.h"
17
18namespace CNORXZ
19{
20 /*============+
21 | SIndex |
22 +============*/
23
24 template <typename MetaT, SizeT S>
27 mRangePtr(rangeCast<RangeType>(range)),
28 mMetaPtr(&mRangePtr->get(0))
29 {
30 CXZ_ASSERT(mRangePtr->size() == S, "got range of size " << mRangePtr->size()
31 << ", expected " << S);
32 }
33
34 template <typename MetaT, SizeT S>
36 {
37 IB::mPos = lexpos;
38 return *this;
39 }
40
41 template <typename MetaT, SizeT S>
43 {
44 ++IB::mPos;
45 return *this;
46 }
47
48 template <typename MetaT, SizeT S>
50 {
51 --IB::mPos;
52 return *this;
53 }
54
55 template <typename MetaT, SizeT S>
57 {
58 return SIndex(mRangePtr, IB::mPos + n);
59 }
60
61 template <typename MetaT, SizeT S>
63 {
64 return SIndex(mRangePtr, IB::mPos - n);
65 }
66
67 template <typename MetaT, SizeT S>
69 {
70 return lex() - i.lex();
71 }
72
73 template <typename MetaT, SizeT S>
75 {
76 IB::mPos += n;
77 return *this;
78 }
79
80 template <typename MetaT, SizeT S>
82 {
83 IB::mPos -= n;
84 return *this;
85 }
86
87 template <typename MetaT, SizeT S>
89 {
90 return IB::mPos;
91 }
92
93 template <typename MetaT, SizeT S>
95 {
96 return SPos<S>();
97 }
98
99 template <typename MetaT, SizeT S>
101 {
102 return SPos<S>();
103 }
104
105 template <typename MetaT, SizeT S>
107 {
108 return IndexId<0>(this->ptrId());
109 }
110
111 template <typename MetaT, SizeT S>
113 {
114 return mMetaPtr[IB::mPos];
115 }
116
117 template <typename MetaT, SizeT S>
119 {
120 return 1;
121 }
122
123 template <typename MetaT, SizeT S>
125 {
126 return mRangePtr;
127 }
128
129 template <typename MetaT, SizeT S>
130 template <SizeT I>
132 {
133 return UPos(id == this->id() ? 1 : 0);
134 }
135
136 template <typename MetaT, SizeT S>
138 {
139 return toString(this->meta());
140 }
141
142 template <typename MetaT, SizeT S>
144 {
145 return mMetaPtr[IB::mPos];
146 }
147
148 template <typename MetaT, SizeT S>
150 {
151 (*this) = mRangePtr->getMeta(metaPos);
152 return *this;
153 }
154
155 template <typename MetaT, SizeT S>
156 decltype(auto) SIndex<MetaT,S>::xpr(const Sptr<SIndex<MetaType,S>>& _this) const
157 {
158 return coproot(mMetaPtr,_this);
159 }
160
161 template <typename MetaT, SizeT S>
163 {
164 CXZ_ASSERT(last > *this, "got last index position smaller than begin index position");
165 const SizeT beginPos = lex();
166 Vector<SizeT> parts(last.lex()-beginPos+1);
167 for(auto i = *this; i != last+1; ++i){
168 parts[i.lex()-beginPos] = i.lex();
169 }
170 return CNORXZ::prange(mRangePtr, parts);
171 }
172
173 template <typename MetaT, SizeT S>
175 {
176 return 1;
177 }
178
179 template <typename MetaT, SizeT S>
181 {
182 return lmax().val();
183 }
184
185 template <typename MetaT, SizeT S>
187 {
188 CXZ_ASSERT(f[0]*s[0] == lmax().val(), "got wrong extension: " << f[0]*s[0]
189 << " vs " << lmax().val());
190 CXZ_ASSERT(CNORXZ::formatIsTrivial(f,s), "format is not trivial: f = " << toString(f)
191 << ", s = " << toString(s));
192 return *this;
193 }
194
195 template <typename MetaT, SizeT S>
196 template <class Xpr, class F>
197 decltype(auto) SIndex<MetaT,S>::ifor(const Xpr& xpr, F&& f) const
198 {
199 return SFor<S,0,Xpr,F>(this->id(), xpr, std::forward<F>(f));
200 }
201
202 template <typename MetaT, SizeT S>
204 {
205 return true;
206 }
207
208 template <typename MetaType, SizeT S, class I1>
209 decltype(auto) operator*(const Sptr<SIndex<MetaType,S>>& a, const Sptr<I1>& b)
210 {
211 return iptrMul(a, b);
212 }
213
214
215 /*===================+
216 | SRangeFactory |
217 +===================*/
218
219 template <typename MetaType, SizeT S>
220 SRangeFactory<MetaType,S>::SRangeFactory(const Arr<MetaType,S>& space) :
221 mSpace(space) {}
222
223 template <typename MetaType, SizeT S>
224 SRangeFactory<MetaType,S>::SRangeFactory(Arr<MetaType,S>&& space) :
225 mSpace(std::forward<Arr<MetaType,S>>(space)) {}
226
227 template <typename MetaType, SizeT S>
228 SRangeFactory<MetaType,S>::SRangeFactory(const Arr<MetaType,S>& space, const RangePtr& ref) :
229 mSpace(space), mRef(ref) {}
230
231 template <typename MetaType, SizeT S>
232 SRangeFactory<MetaType,S>::SRangeFactory(Arr<MetaType,S>&& space, const RangePtr& ref) :
233 mSpace(std::forward<Arr<MetaType,S>>(space)), mRef(ref) {}
234
235 template <typename MetaType, SizeT S>
236 void SRangeFactory<MetaType,S>::make()
237 {
238 const auto& info = typeid(SRange<MetaType,S>);
239 if(mRef != nullptr) {
240 mProd = this->fromCreated(info, {mRef->id()});
241 }
242 if(mProd == nullptr){
243 RangePtr key = mProd = std::shared_ptr<SRange<MetaType,S>>
244 ( new SRange<MetaType,S>( std::move(mSpace) ) );
245 if(mRef != nullptr) { key = mRef; }
246 this->addToCreated(info, { key->id() }, mProd);
247 }
248 }
249
250 /*============+
251 | SRange |
252 +============*/
253
254 template <typename MetaType, SizeT S>
255 SRange<MetaType,S>::SRange(const Arr<MetaType,S>& space) :
256 RangeInterface<SRange<MetaType,S>>(),
257 mSpace(space)
258 {
259 std::sort(mSpace.begin(), mSpace.end(), std::less<MetaType>());
260 auto itdupl = std::adjacent_find(mSpace.begin(), mSpace.end());
261 CXZ_ASSERT(itdupl == mSpace.end(), "found duplicate: " << *itdupl);
262 }
263
264 template <typename MetaType, SizeT S>
265 SRange<MetaType,S>::SRange(Arr<MetaType,S>&& space) :
266 RangeInterface<SRange<MetaType,S>>(),
267 mSpace(std::forward<Arr<MetaType,S>>(space))
268 {
269 std::sort(mSpace.begin(), mSpace.end(), std::less<MetaType>());
270 auto itdupl = std::adjacent_find(mSpace.begin(), mSpace.end());
271 CXZ_ASSERT(itdupl == mSpace.end(), "found duplicate: " << *itdupl);
272 }
273
274 template <typename MetaType, SizeT S>
276 {
277 return S;
278 }
279
280 template <typename MetaType, SizeT S>
282 {
283 return 1;
284 }
285
286 template <typename MetaType, SizeT S>
288 {
289 return toString(mSpace[pos]);
290 }
291
292 template <typename MetaType, SizeT S>
294 {
295 return typeid(SRange<MetaType,S>);
296 }
297
298 template <typename MetaType, SizeT S>
300 {
301 return typeid(MetaType);
302 }
303
304 template <typename MetaType, SizeT S>
306 {
307 // TODO: check for selected static sizes of SRange -> return SRange!!!
308 auto rx = rangeCast<URange<MetaType>>(r);
309 Vector<MetaType> space(mSpace.begin(), mSpace.end());
310 space.insert(space.end(), rx->begin(), rx->end());
311 return URangeFactory<MetaType>( space ).create();
312 }
313
314 template <typename MetaType, SizeT S>
316 {
317 return mSpace[pos];
318 }
319
320 template <typename MetaType, SizeT S>
322 {
323 return mSpace.data();
324 }
325
326 template <typename MetaType, SizeT S>
328 {
329 auto b = mSpace.begin();
330 auto e = mSpace.end();
331 return std::lower_bound(b, e, metaPos, std::less<MetaType>()) - b;
332 }
333
334 template <typename MetaType, SizeT S>
336 {
337 return Vector<Uuid> { this->id() };
338 }
339
340 /*=================+
341 | Range Casts |
342 +=================*/
343
344 template <typename MetaType, SizeT S>
345 Sptr<SRange<MetaType,S>> RangeCast<SRange<MetaType,S>>::func(const RangePtr& r)
346 {
347 Sptr<URange<MetaType>> tmp;
348 if(r->type() != typeid(URange<MetaType>)){
349 tmp = rangeCast<URange<MetaType>>(r);
350 }
351 else {
352 tmp = std::dynamic_pointer_cast<URange<MetaType>>(r);
353 }
354 CXZ_ASSERT(tmp->size() == S, "cannot cast range of size " << tmp->size()
355 << " into static range of size " << S);
356 Arr<MetaType,S> space;
357 std::copy(tmp->begin(), tmp->end(), space.begin());
358 return std::dynamic_pointer_cast<SRange<MetaType,S>>
359 ( SRangeFactory<MetaType,S>(std::move(space)).create() );
360 }
361
362}
363
364#endif
#define CXZ_ASSERT(statement, errmsg)
Definition assert.h:40
decltype(auto) begin() const
SIndex operator-(Int n) const
Definition srange.cc.h:62
SIndex & operator=(SizeT lexpos)
Definition srange.cc.h:35
String stringMeta() const
Definition srange.cc.h:137
bool formatIsTrivial() const
Definition srange.cc.h:203
SizeT deepFormat() const
Definition srange.cc.h:174
RangePtr prange(const SIndex< MetaType, S > &last) const
Definition srange.cc.h:162
UPos stepSize(const IndexId< I > &id) const
Definition srange.cc.h:131
SPos< S > pmax() const
Definition srange.cc.h:94
SIndex & operator+=(Int n)
Definition srange.cc.h:74
SizeT deepMax() const
Definition srange.cc.h:180
SIndex & reformat(const Vector< SizeT > &f, const Vector< SizeT > &s)
Definition srange.cc.h:186
Sptr< RangeType > range() const
Definition srange.cc.h:124
SIndex operator+(Int n) const
Definition srange.cc.h:56
SizeT dim() const
Definition srange.cc.h:118
SIndex & operator--()
Definition srange.cc.h:49
const MetaT & operator*() const
Definition srange.cc.h:112
SPos< S > lmax() const
Definition srange.cc.h:100
SizeT lex() const
Definition srange.cc.h:88
SIndex & operator++()
Definition srange.cc.h:42
SIndex & operator-=(Int n)
Definition srange.cc.h:81
SIndex(const RangePtr &range, SizeT pos=0)
Definition srange.cc.h:25
IndexId< 0 > id() const
Definition srange.cc.h:106
const MetaT & meta() const
Definition srange.cc.h:143
SIndex & at(const MetaT &metaPos)
Definition srange.cc.h:149
MetaT MetaType
Definition srange.h:188
std::string String
Definition types.h:42
std::type_info TypeInfo
Definition types.h:71
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
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)
std::array< T, N > Arr
Definition types.h:45
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.
SRange, SRangeFactory and SIndex declaration.