diff --git a/src/include/base/macros.h b/src/include/base/macros.h index 297d97c..fda41c2 100644 --- a/src/include/base/macros.h +++ b/src/include/base/macros.h @@ -32,6 +32,12 @@ #define SP_DEFAULT_MEMBERS_X(__spec__,__class_name__) SP_DEFAULT_COPY(__spec__,__class_name__); SP_DEFAULT_MOVE(__spec__,__class_name__) #define SP_DEFAULT_MEMBERS(__spec__,__class_name__) SP_DEFAULT_C(__spec__,__class_name__); SP_DEFAULT_MEMBERS_X(__spec__,__class_name__) +#define INDEX_RANDOM_ACCESS_ITERATOR_DEFS(__meta_type__) typedef std::random_access_iterator_tag iterator_category; \ + typedef SizeT difference_type; \ + typedef __meta_type__ value_type; \ + typedef const __meta_type__* pointer; \ + typedef const __meta_type__& reference + #define CXZ_CVAL_FALSE static constexpr bool value = false #define CXZ_CVAL_TRUE static constexpr bool value = true diff --git a/src/include/ranges/crange.h b/src/include/ranges/crange.h index d69593f..71302af 100644 --- a/src/include/ranges/crange.h +++ b/src/include/ranges/crange.h @@ -17,6 +17,8 @@ namespace CNORXZ typedef CRange RangeType; typedef SizeT MetaType; + INDEX_RANDOM_ACCESS_ITERATOR_DEFS(MetaType); + DEFAULT_MEMBERS(CIndex); CIndex(const RangePtr& range, SizeT pos = 0); CIndex& operator=(SizeT lexpos); diff --git a/src/include/ranges/eindex.cc.h b/src/include/ranges/eindex.cc.h index d9adc46..8129616 100644 --- a/src/include/ranges/eindex.cc.h +++ b/src/include/ranges/eindex.cc.h @@ -22,7 +22,7 @@ namespace CNORXZ return EFor(mLI->id(), xpr, std::forward(f)); } - template + template decltype(auto) operator*(const Sptr>& a, const Sptr& b) { return iptrMul(a, b); @@ -31,16 +31,16 @@ namespace CNORXZ template decltype(auto) eindexPtr(const Sptr,L>>& i) { - return std::make_shared>(i); + return std::make_shared>(i); } - template + template decltype(auto) eindexPtr(const Sptr>& i) { return eindexPtr( lindexPtr( i ) ); } - template + template decltype(auto) eindexPtr(const Sptr>& i, CSizeT l) { return eindexPtr( i ); @@ -49,16 +49,19 @@ namespace CNORXZ template decltype(auto) eplex(const Sptr& i) { - const SizeT isize = i->lmax().val() + const SizeT isize = i->lmax().val(); CXZ_ASSERT(isize % S == 0, "index max (= " << isize << " ) not dividable by extension size = " << S); - auto ci = std::make_shared( CRangeFactory(isize/L).create() ); - auto ei = eindexPtr( std::make_shared>( SRangeFactory().create() ) ); + auto ci = std::make_shared( CRangeFactory(isize/S).create() ); + auto m = iter<0,S>([](auto i) { return i; }, + [](const auto&... e) { return Arr{ e... }; } ); + auto ei = eindexPtr( std::make_shared> + ( SRangeFactory(m).create() ) ); if constexpr(L2 == 0){ return spackp(ci,ei); } else { - return spackp(lindex(ci),ei); + return spackp(lindexPtr(ci),ei); } } diff --git a/src/include/ranges/eindex.h b/src/include/ranges/eindex.h index ad14c58..83dc201 100644 --- a/src/include/ranges/eindex.h +++ b/src/include/ranges/eindex.h @@ -18,8 +18,7 @@ namespace CNORXZ typedef typename LIndex,L>::RangeType RangeType; DEFAULT_MEMBERS(EIndex); - EIndex(const LIndex,L>& i); - EIndex(LIndex,L>&& i); + EIndex(const Sptr,L>>& i); template decltype(auto) ifor(const Xpr& xpr, F&& f) const; @@ -28,16 +27,22 @@ namespace CNORXZ Sptr,L>> mLI; }; - template - decltype(auto) operator*(const Sptr>& a, const Sptr& b); + template + struct is_index> + { + static constexpr bool value = true; + }; + + template + decltype(auto) operator*(const Sptr>& a, const Sptr& b); - template + template decltype(auto) eindexPtr(const Sptr,L>>& i); - template + template decltype(auto) eindexPtr(const Sptr>& i); - template + template decltype(auto) eindexPtr(const Sptr>& i, CSizeT l); template diff --git a/src/include/ranges/index_format.cc.h b/src/include/ranges/index_format.cc.h index 07d1b55..440086a 100644 --- a/src/include/ranges/index_format.cc.h +++ b/src/include/ranges/index_format.cc.h @@ -68,10 +68,10 @@ namespace CNORXZ template constexpr GMFormat::GMFormat(const FormatT& f) { - static_assert(f.size() == size(), "try to assign format of wrong dimension"); - iter<0,sizeof...(PosT)>( [&](auto i) { mB[i] = f[i]; }, NoF{} ); + CXZ_ASSERT(f.size() == size(), "try to assign format of wrong dimension"); + iter<0,sizeof...(PosT)>( [&](auto i) { std::get(mB) = f[i].val(); }, NoF{} ); } - + template const Tuple& GMFormat::all() const { diff --git a/src/include/ranges/lindex.cc.h b/src/include/ranges/lindex.cc.h index 55c99ea..26046af 100644 --- a/src/include/ranges/lindex.cc.h +++ b/src/include/ranges/lindex.cc.h @@ -44,7 +44,7 @@ namespace CNORXZ template decltype(auto) lindexPtr(const Sptr& i) { - return LIndex( i ); + return std::make_shared>( i ); } template diff --git a/src/include/ranges/lindex.h b/src/include/ranges/lindex.h index 4fa9160..aa66f0e 100644 --- a/src/include/ranges/lindex.h +++ b/src/include/ranges/lindex.h @@ -29,6 +29,11 @@ namespace CNORXZ Sptr mI; }; + template + struct is_index> + { + static constexpr bool value = is_index::value; + }; template decltype(auto) operator*(const Sptr>& a, const Sptr& b); diff --git a/src/include/ranges/mrange.h b/src/include/ranges/mrange.h index 18775f4..79661fc 100644 --- a/src/include/ranges/mrange.h +++ b/src/include/ranges/mrange.h @@ -27,10 +27,11 @@ namespace CNORXZ typedef MRange RangeType; static constexpr SizeT NI = sizeof...(Indices); + INDEX_RANDOM_ACCESS_ITERATOR_DEFS(MetaType); constexpr GMIndex() = default; constexpr GMIndex(GMIndex&& i) = default; constexpr GMIndex& operator=(GMIndex&& i) = default; - + // no defaults: constexpr GMIndex(const GMIndex& i); constexpr GMIndex& operator=(const GMIndex& i); @@ -103,7 +104,7 @@ namespace CNORXZ Sptr mRange; SPack mIPack; - typedef RemoveRef{}))> LexFormatT; + typedef RemoveRef{}))> LexFormatT; LexFormatT mLexFormat; FormatT mFormat; SizeT mLex; diff --git a/src/include/ranges/ranges.cc.h b/src/include/ranges/ranges.cc.h index 71e3beb..4225dff 100644 --- a/src/include/ranges/ranges.cc.h +++ b/src/include/ranges/ranges.cc.h @@ -4,10 +4,12 @@ #include "mrange.cc.h" #include "xindex.cc.h" #include "urange.cc.h" +#include "srange.cc.h" #include "crange.cc.h" #include "prange.cc.h" #include "dindex.cc.h" #include "lindex.cc.h" +#include "eindex.cc.h" #include "index_mul.cc.h" #include "index_pack.cc.h" #include "index_format.cc.h" diff --git a/src/include/ranges/ranges.h b/src/include/ranges/ranges.h index 422a2a1..7423b69 100644 --- a/src/include/ranges/ranges.h +++ b/src/include/ranges/ranges.h @@ -2,12 +2,15 @@ #include "range_base.h" #include "index_base.h" #include "mrange.h" -#include "crange.h" -#include "prange.h" #include "xindex.h" #include "yrange.h" +#include "urange.h" +#include "srange.h" +#include "crange.h" +#include "prange.h" #include "dindex.h" #include "lindex.h" +#include "eindex.h" #include "index_mul.h" #include "index_pack.h" #include "index_format.h" diff --git a/src/include/ranges/srange.cc.h b/src/include/ranges/srange.cc.h index 59f9cce..85598a3 100644 --- a/src/include/ranges/srange.cc.h +++ b/src/include/ranges/srange.cc.h @@ -10,8 +10,8 @@ namespace CNORXZ * SIndex * **************/ - template - SIndex::SIndex(const RangePtr& range, SizeT pos) : + template + SIndex::SIndex(const RangePtr& range, SizeT pos) : IndexInterface,MetaT>(pos), mRangePtr(rangeCast(range)), mMetaPtr(&mRangePtr->get(0)) @@ -20,137 +20,137 @@ namespace CNORXZ << ", expected " << S); } - template - SIndex& SIndex::operator=(SizeT lexpos) + template + SIndex& SIndex::operator=(SizeT lexpos) { IB::mPos = lexpos; return *this; } - template - SIndex& SIndex::operator++() + template + SIndex& SIndex::operator++() { ++IB::mPos; return *this; } - template - SIndex& SIndex::operator--() + template + SIndex& SIndex::operator--() { --IB::mPos; return *this; } - template - SIndex SIndex::operator+(Int n) const + template + SIndex SIndex::operator+(Int n) const { return SIndex(mRangePtr, IB::mPos + n); } - template - SIndex SIndex::operator-(Int n) const + template + SIndex SIndex::operator-(Int n) const { return SIndex(mRangePtr, IB::mPos - n); } - template - SIndex& SIndex::operator+=(Int n) + template + SIndex& SIndex::operator+=(Int n) { IB::mPos += n; return *this; } - template - SIndex& SIndex::operator-=(Int n) + template + SIndex& SIndex::operator-=(Int n) { IB::mPos -= n; return *this; } - template - SizeT SIndex::lex() const + template + SizeT SIndex::lex() const { return IB::mPos; } - template - SPos SIndex::pmax() const + template + SPos SIndex::pmax() const { return SPos(); } - template - SPos SIndex::lmax() const + template + SPos SIndex::lmax() const { return SPos(); } - template - IndexId<0> SIndex::id() const + template + IndexId<0> SIndex::id() const { return IndexId<0>(this->ptrId()); } - template - const MetaT& SIndex::operator*() const + template + const MetaT& SIndex::operator*() const { return mMetaPtr[IB::mPos]; } - template - SizeT SIndex::dim() const + template + SizeT SIndex::dim() const { return 1; } - template - Sptr SIndex::range() const + template + Sptr> SIndex::range() const { return mRangePtr; } - template + template template - UPos SIndex::stepSize(const IndexId& id) const + UPos SIndex::stepSize(const IndexId& id) const { return UPos(id == this->id() ? 1 : 0); } - template - String SIndex::stringMeta() const + template + String SIndex::stringMeta() const { return toString(this->meta()); } - template - const MetaT& SIndex::meta() const + template + const MetaT& SIndex::meta() const { return mMetaPtr[IB::mPos]; } - template - SIndex& SIndex::at(const MetaT& metaPos) + template + SIndex& SIndex::at(const MetaT& metaPos) { (*this) = mRangePtr->getMeta(metaPos); return *this; } - template - decltype(auto) SIndex::xpr(const Sptr>& _this) const + template + decltype(auto) SIndex::xpr(const Sptr>& _this) const { return coproot(mMetaPtr,_this); } - template + template template - decltype(auto) SIndex::reformat(const Sptr& ind) const + decltype(auto) SIndex::reformat(const Sptr& ind) const { return ind; } - template + template template - decltype(auto) SIndex::slice(const Sptr& ind) const + decltype(auto) SIndex::slice(const Sptr& ind) const { if(ind != nullptr){ if(ind->dim() != 0) { @@ -160,9 +160,9 @@ namespace CNORXZ return std::make_shared>(*this); } - template + template template - decltype(auto) SIndex::ifor(const Xpr& xpr, F&& f) const + decltype(auto) SIndex::ifor(const Xpr& xpr, F&& f) const { return SFor(this->id(), xpr, std::forward(f)); } @@ -183,7 +183,7 @@ namespace CNORXZ mSpace(space) {} template - SRangeFactory::SRangeFactory(Arr&& space) + SRangeFactory::SRangeFactory(Arr&& space) : mSpace(std::forward>(space)) {} template @@ -233,57 +233,67 @@ namespace CNORXZ CXZ_ASSERT(itdupl == mSpace.end(), "found duplicate: " << *itdupl); } + template SizeT SRange::size() const { return S; } + template SizeT SRange::dim() const { return 1; } + template String SRange::stringMeta(SizeT pos) const { return toString(mSpace[pos]); } + template const TypeInfo& SRange::type() const { return typeid(SRange); } + template const TypeInfo& SRange::metaType() const { return typeid(MetaType); } + template RangePtr SRange::extend(const RangePtr& r) const { // TODO: check for selected static sizes of SRange -> return SRange!!! auto rx = rangeCast>(r); Vector space(mSpace.begin(), mSpace.end()); - space.insert(space.end(), rx->mSpace.begin(), rx->mSpace.end()); + space.insert(space.end(), rx->begin(), rx->end()); return URangeFactory( space ).create(); } - const MetaType& SRange::get(SizeT pos) const; + template + const MetaType& SRange::get(SizeT pos) const { return mSpace[pos]; } - const MetaType* SRange::get() const; + template + const MetaType* SRange::get() const { return mSpace.data(); } - SizeT SRange::getMeta(const MetaType& metaPos) const; + template + SizeT SRange::getMeta(const MetaType& metaPos) const { auto b = mSpace.begin(); auto e = mSpace.end(); - return std::lower_bound(b, e, meta, std::less()) - b; + return std::lower_bound(b, e, metaPos, std::less()) - b; } + template Vector SRange::key() const { return Vector { this->id() }; @@ -298,10 +308,10 @@ namespace CNORXZ { Sptr> tmp; if(r->type() != typeid(URange)){ - tmp = castRange>(r); + tmp = rangeCast>(r); } else { - tmp = r; + tmp = std::dynamic_pointer_cast>(r); } CXZ_ASSERT(tmp->size() == S, "cannot cast range of size " << tmp->size() << " into static range of size " << S); diff --git a/src/include/ranges/srange.h b/src/include/ranges/srange.h index 2ba3a4a..13dc4bf 100644 --- a/src/include/ranges/srange.h +++ b/src/include/ranges/srange.h @@ -10,7 +10,7 @@ namespace CNORXZ { - template + template class SIndex : public IndexInterface,MetaT> { public: @@ -18,6 +18,8 @@ namespace CNORXZ typedef SRange RangeType; typedef MetaT MetaType; + INDEX_RANDOM_ACCESS_ITERATOR_DEFS(MetaType); + DEFAULT_MEMBERS(SIndex); SIndex(const RangePtr& range, SizeT pos = 0); SIndex& operator=(SizeT lexpos); @@ -119,6 +121,7 @@ namespace CNORXZ { static Sptr> func(const RangePtr& r); }; + } #endif diff --git a/src/include/ranges/urange.h b/src/include/ranges/urange.h index ef24abb..46293bb 100644 --- a/src/include/ranges/urange.h +++ b/src/include/ranges/urange.h @@ -8,6 +8,8 @@ #include "ranges/range_base.h" #include "xpr/xpr.h" +#include + namespace CNORXZ { @@ -20,13 +22,18 @@ namespace CNORXZ typedef URange RangeType; typedef MetaT MetaType; + INDEX_RANDOM_ACCESS_ITERATOR_DEFS(MetaType); + DEFAULT_MEMBERS(UIndex); UIndex(const RangePtr& range, SizeT pos = 0); + + void swap(UIndex& i) {}; // !!! UIndex& operator=(SizeT lexpos); UIndex& operator++(); UIndex& operator--(); UIndex operator+(Int n) const; UIndex operator-(Int n) const; + SizeT operator-(const UIndex& i) const { return lex() - i.lex(); } // !!! UIndex& operator+=(Int n); UIndex& operator-=(Int n); @@ -62,6 +69,9 @@ namespace CNORXZ const MetaT* mMetaPtr; }; + template + void swap(UIndex& a, UIndex& b) { a.swap(b); } + template decltype(auto) operator*(const Sptr>& a, const Sptr& b); diff --git a/src/include/ranges/yrange.h b/src/include/ranges/yrange.h index f674da4..3800425 100644 --- a/src/include/ranges/yrange.h +++ b/src/include/ranges/yrange.h @@ -19,10 +19,11 @@ namespace CNORXZ typedef YRange RangeType; typedef Vector MetaType; + INDEX_RANDOM_ACCESS_ITERATOR_DEFS(MetaType); YIndex() = default; YIndex(YIndex&& i) = default; YIndex& operator=(YIndex&& i) = default; - + // no defaults: YIndex(const YIndex& i); YIndex& operator=(const YIndex& i); diff --git a/src/tests/operation_unit_test.cc b/src/tests/operation_unit_test.cc index 308fb56..a8a4144 100644 --- a/src/tests/operation_unit_test.cc +++ b/src/tests/operation_unit_test.cc @@ -98,9 +98,10 @@ namespace { protected: - typedef MIndex,EIndex> MCCI; + typedef MIndex,EIndex> MCCI1; + typedef MIndex,EIndex,CIndex> MCCI2; - OpCont_CR_CR_Test() + OpCont_CR_CR_Test2() { mSize1 = 12; mSize2 = 11; @@ -115,8 +116,8 @@ namespace mCI1j = std::make_shared(cr1); mCI2i = std::make_shared(cr2); mCI2j = std::make_shared(cr2); - mCC1i1j = mindexPtr(mCI1i*eplex(mCI1j,4,1,2)); - mCC1j1i = mindexPtr(eplex(mCI1j,4,1,2)*mCI1i); + mCC1i1j = mindexPtr(mCI1i*eplex<4,1,2>(mCI1j)); + mCC1j1i = mindexPtr(eplex<4,1,2>(mCI1j)*mCI1i); mOC1i1j.init(mCC1i1j); mOR1j1i.init(mData11.data(), mCC1j1i); mOR1i1j.init(mData11.data(), mCC1i1j); @@ -132,11 +133,11 @@ namespace Sptr mCI1j; Sptr mCI2i; Sptr mCI2j; - Sptr mCC1i1j; - Sptr mCC1j1i; - OpCont mOC1i1j; - COpRoot mOR1j1i; - COpRoot mOR1i1j; + Sptr mCC1i1j; + Sptr mCC1j1i; + OpCont mOC1i1j; + COpRoot mOR1j1i; + COpRoot mOR1i1j; }; TEST_F(OpCont_CR_Test, Basics)