diff --git a/src/include/array/array_base.cc.h b/src/include/array/array_base.cc.h index ca38cbf..fd9f3f4 100644 --- a/src/include/array/array_base.cc.h +++ b/src/include/array/array_base.cc.h @@ -69,7 +69,7 @@ namespace CNORXZ { auto ai = itLexSave(begin); auto aj = itLexSave(end); - return std::make_shared(subcube(ai,aj), this, ai.format(), ai.pos()); + return std::make_shared(ai.prange(aj), this, ai.format(), ai.pos()); } template diff --git a/src/include/ranges/crange.h b/src/include/ranges/crange.h index 2eaab0e..430a507 100644 --- a/src/include/ranges/crange.h +++ b/src/include/ranges/crange.h @@ -47,6 +47,8 @@ namespace CNORXZ CIndex& at(const SizeT& metaPos); COpRoot xpr(const Sptr& _this) const; + RangePtr prange(const CIndex& end) const; + SizeT deepFormat() const; /* template diff --git a/src/include/ranges/dindex.h b/src/include/ranges/dindex.h index 560ca50..c40ab33 100644 --- a/src/include/ranges/dindex.h +++ b/src/include/ranges/dindex.h @@ -54,6 +54,7 @@ namespace CNORXZ DType meta() const; DIndex& at(const DType& meta); DXpr xpr(const Sptr& _this) const; + RangePtr prange(const DIndex& end) const; Vector deepFormat() const; DXpr ifor(const DXpr& xpr, std::function&& f) const; diff --git a/src/include/ranges/index_base.h b/src/include/ranges/index_base.h index 7e280b2..d1454df 100644 --- a/src/include/ranges/index_base.h +++ b/src/include/ranges/index_base.h @@ -54,6 +54,8 @@ namespace CNORXZ decltype(auto) meta() const { return THIS().meta(); } I& at(const MetaType& meta) { return THIS().at(meta); } decltype(auto) xpr(const Sptr& _this) const { return THIS().xpr(_this); } + + RangePtr prange(const IndexInterface& end) const { return THIS().prange(end.THIS()); } /* template decltype(auto) formatTo(const Sptr& ind) const { return ind->formatFrom(THIS()); } diff --git a/src/include/ranges/mrange.cc.h b/src/include/ranges/mrange.cc.h index e34a560..f8ef5bf 100644 --- a/src/include/ranges/mrange.cc.h +++ b/src/include/ranges/mrange.cc.h @@ -479,6 +479,22 @@ namespace CNORXZ return mLexFormat; } + template + RangePtr GMIndex::prange(const MIndex& end) const + { + CXZ_ERROR("IMPLEMENT!!!"); + return nullptr; + /* + return ifor<0,NI> + ( [&](auto i) { + typedef typename std::remove_reference::type::RangeType RangeT; + return std::dynamic_pointer_cast( mIPack[i]->prange( *end.pack()[i] ) ); + }, + [](const auto&... e) { return mrange(e...); } + ); + */ + } + template auto GMIndex::deepFormat() const { diff --git a/src/include/ranges/mrange.h b/src/include/ranges/mrange.h index c9a20b6..051afb1 100644 --- a/src/include/ranges/mrange.h +++ b/src/include/ranges/mrange.h @@ -81,6 +81,7 @@ namespace CNORXZ const SPack& pack() const; const auto& format() const; const auto& lexFormat() const; + RangePtr prange(const MIndex& end) const; auto deepFormat() const; GMIndex& setFormat(const FormatT& bs); diff --git a/src/include/ranges/prange.cc.h b/src/include/ranges/prange.cc.h index 1efae1d..cfb4b18 100644 --- a/src/include/ranges/prange.cc.h +++ b/src/include/ranges/prange.cc.h @@ -123,6 +123,19 @@ namespace CNORXZ } } + template + RangePtr PIndex::prange(const PIndex& end) const + { + CXZ_ERROR("IMPLEMENT!!!"); + return nullptr; + } + + template + decltype(auto) PIndex::deepFormat() const + { + return mOrig->deepFormat(); + } + template String PIndex::stringMeta() const { @@ -208,7 +221,7 @@ namespace CNORXZ } ++IB::mPos; } - CXZ_ERROR("meta position '" << mOrig->meta() << "' not part of range"); + CXZ_ERROR("meta position '" << toString(mOrig->meta()) << "' not part of range"); } /*************************** @@ -327,22 +340,16 @@ namespace CNORXZ return Vector { mRange->id() }; } - /**************************** * non-member functions * ****************************/ - - template - RangePtr prange(const IndexInterface& begin, const IndexInterface& end) + + template + RangePtr prange(const Sptr& range, const Vector& parts) { - Vector parts(end-begin); - const SizeT off = begin.pos(); - for(auto i = begin.THIS(); i != end.THIS(); ++i){ - parts[i.pos()-off] = i.pos(); - } - return begin.range()->partial(parts); // implement!!!! - //return PRangeFactory(begin.range(), parts).create(); + return PRangeFactory(range,parts).create(); } + } #endif diff --git a/src/include/ranges/prange.h b/src/include/ranges/prange.h index 78961c8..7251bd7 100644 --- a/src/include/ranges/prange.h +++ b/src/include/ranges/prange.h @@ -41,6 +41,8 @@ namespace CNORXZ template UPos stepSize(const IndexId& id) const; + RangePtr prange(const PIndex& end) const; + decltype(auto) deepFormat() const; String stringMeta() const; decltype(auto) meta() const; @@ -118,8 +120,8 @@ namespace CNORXZ Vector mParts; }; - template - RangePtr prange(const IndexInterface& begin, const IndexInterface& end); + template + RangePtr prange(const Sptr& range, const Vector& parts); } // namespace CNORXZ diff --git a/src/include/ranges/srange.cc.h b/src/include/ranges/srange.cc.h index d5b1faa..5d27de6 100644 --- a/src/include/ranges/srange.cc.h +++ b/src/include/ranges/srange.cc.h @@ -141,6 +141,13 @@ namespace CNORXZ return coproot(mMetaPtr,_this); } + template + RangePtr SIndex::prange(const SIndex& end) const + { + CXZ_ERROR("IMPLEMENT!!!"); + return nullptr; + } + template SizeT SIndex::deepFormat() const { diff --git a/src/include/ranges/srange.h b/src/include/ranges/srange.h index 46feea8..9aafb39 100644 --- a/src/include/ranges/srange.h +++ b/src/include/ranges/srange.h @@ -48,6 +48,7 @@ namespace CNORXZ SIndex& at(const MetaT& metaPos); decltype(auto) xpr(const Sptr>& _this) const; + RangePtr prange(const SIndex& end) const; SizeT deepFormat() const; /* template diff --git a/src/include/ranges/urange.cc.h b/src/include/ranges/urange.cc.h index 82825c8..56f2fbb 100644 --- a/src/include/ranges/urange.cc.h +++ b/src/include/ranges/urange.cc.h @@ -6,6 +6,7 @@ #include #include "urange.h" +#include "prange.h" #include "index_mul.h" #include "xpr/for.h" #include "operation/op_types.h" @@ -17,136 +18,148 @@ namespace CNORXZ * UIndex * *****************/ - template - UIndex::UIndex(const RangePtr& range, SizeT pos) : - IndexInterface,MetaType>(pos), + template + UIndex::UIndex(const RangePtr& range, SizeT pos) : + IndexInterface,MetaT>(pos), mRangePtr(rangeCast(range)), mMetaPtr(&mRangePtr->get(0)) {} - template - UIndex& UIndex::operator=(size_t lexpos) + template + UIndex& UIndex::operator=(size_t lexpos) { IB::mPos = lexpos; return *this; } - template - UIndex& UIndex::operator++() + template + UIndex& UIndex::operator++() { ++IB::mPos; return *this; } - template - UIndex& UIndex::operator--() + template + UIndex& UIndex::operator--() { --IB::mPos; return *this; } - template - UIndex UIndex::operator+(Int n) const + template + UIndex UIndex::operator+(Int n) const { return UIndex(mRangePtr, IB::mPos + n); } - template - UIndex UIndex::operator-(Int n) const + template + UIndex UIndex::operator-(Int n) const { return UIndex(mRangePtr, IB::mPos - n); } - template - UIndex& UIndex::operator+=(Int n) + template + UIndex& UIndex::operator+=(Int n) { IB::mPos += n; return *this; } - template - UIndex& UIndex::operator-=(Int n) + template + UIndex& UIndex::operator-=(Int n) { IB::mPos -= n; return *this; } - template - SizeT UIndex::lex() const + template + SizeT UIndex::lex() const { return IB::mPos; } - template - UPos UIndex::pmax() const + template + UPos UIndex::pmax() const { return UPos(mRangePtr->size()); } - template - UPos UIndex::lmax() const + template + UPos UIndex::lmax() const { return UPos(mRangePtr->size()); } - template - IndexId<0> UIndex::id() const + template + IndexId<0> UIndex::id() const { return IndexId<0>(this->ptrId()); } - template - const MetaType& UIndex::operator*() const + template + const MetaT& UIndex::operator*() const { return mMetaPtr[IB::mPos]; } - template - String UIndex::stringMeta() const + template + String UIndex::stringMeta() const { return toString(this->meta()); } - template - const MetaType& UIndex::meta() const + template + const MetaT& UIndex::meta() const { return mMetaPtr[IB::mPos]; } - template - UIndex& UIndex::at(const MetaType& metaPos) + template + UIndex& UIndex::at(const MetaT& metaPos) { (*this) = mRangePtr->getMeta(metaPos); return *this; } - template - decltype(auto) UIndex::xpr(const Sptr>& _this) const + template + decltype(auto) UIndex::xpr(const Sptr>& _this) const { return coproot(mMetaPtr,_this); } - template - SizeT UIndex::deepFormat() const + template + RangePtr UIndex::prange(const UIndex& end) const + { + CXZ_ASSERT(end > *this, "got end index position smaller than begin index position"); + const SizeT beginPos = lex(); + Vector parts(end.lex()-beginPos); + for(auto i = *this; i != end; ++i){ + parts[i.lex()-beginPos] = i.lex(); + } + return CNORXZ::prange(mRangePtr, parts); + } + + template + SizeT UIndex::deepFormat() const { return 1; } - template - size_t UIndex::dim() const // = 1 + template + size_t UIndex::dim() const // = 1 { return 1; } - template - Sptr> UIndex::range() const + template + Sptr> UIndex::range() const { return mRangePtr; } - template + template template - decltype(auto) UIndex::stepSize(const IndexId& id) const + decltype(auto) UIndex::stepSize(const IndexId& id) const { if constexpr(I != 0){ return SPos<0>(); @@ -156,34 +169,34 @@ namespace CNORXZ } } /* - template + template template - decltype(auto) UIndex::formatFrom(const Index& ind) const + decltype(auto) UIndex::formatFrom(const Index& ind) const { return *this; } - template + template template - decltype(auto) UIndex::slice(const Sptr& ind) const + decltype(auto) UIndex::slice(const Sptr& ind) const { if(ind != nullptr){ if(ind->dim() != 0) { - return Sptr>(); + return Sptr>(); } } - return std::make_shared>(*this); + return std::make_shared>(*this); } */ - template + template template - decltype(auto) UIndex::ifor(const Xpr& xpr, F&& f) const + decltype(auto) UIndex::ifor(const Xpr& xpr, F&& f) const { return For<0,Xpr,F>(this->pmax().val(), this->id(), xpr, std::forward(f)); } - template - decltype(auto) operator*(const Sptr>& a, const Sptr& b) + template + decltype(auto) operator*(const Sptr>& a, const Sptr& b) { return iptrMul(a, b); } @@ -192,32 +205,32 @@ namespace CNORXZ * URangeFactory * **********************/ - template - URangeFactory::URangeFactory(const Vector& space) : + template + URangeFactory::URangeFactory(const Vector& space) : mSpace(space) {} - template - URangeFactory::URangeFactory(Vector&& space) : + template + URangeFactory::URangeFactory(Vector&& space) : mSpace(space) {} - template - URangeFactory::URangeFactory(const Vector& space, const RangePtr& ref) : + template + URangeFactory::URangeFactory(const Vector& space, const RangePtr& ref) : mSpace(space), mRef(ref) {} - template - URangeFactory::URangeFactory(Vector&& space, const RangePtr& ref) : + template + URangeFactory::URangeFactory(Vector&& space, const RangePtr& ref) : mSpace(space), mRef(ref) {} - template - void URangeFactory::make() + template + void URangeFactory::make() { - const auto& info = typeid(URange); + const auto& info = typeid(URange); if(mRef != nullptr) { mProd = this->fromCreated(info, {mRef->id()}); } if(mProd == nullptr){ - RangePtr key = mProd = std::shared_ptr> - ( new URange( std::move(mSpace) ) ); + RangePtr key = mProd = std::shared_ptr> + ( new URange( std::move(mSpace) ) ); if(mRef != nullptr) { key = mRef; } this->addToCreated(info, { key->id() }, mProd); } @@ -227,88 +240,88 @@ namespace CNORXZ * URange * ***************/ - template - URange::URange(const Vector& space) : - RangeInterface>(), + template + URange::URange(const Vector& space) : + RangeInterface>(), mSpace(space) { - std::sort(mSpace.begin(), mSpace.end(), std::less()); + std::sort(mSpace.begin(), mSpace.end(), std::less()); auto itdupl = std::adjacent_find(mSpace.begin(), mSpace.end()); CXZ_ASSERT(itdupl == mSpace.end(), "found duplicate: " << *itdupl); } - template - URange::URange(Vector&& space) : - RangeInterface>(), + template + URange::URange(Vector&& space) : + RangeInterface>(), mSpace(space) { - std::sort(mSpace.begin(), mSpace.end(), std::less()); + std::sort(mSpace.begin(), mSpace.end(), std::less()); auto itdupl = std::adjacent_find(mSpace.begin(), mSpace.end()); CXZ_ASSERT(itdupl == mSpace.end(), "found duplicate: " << toString(*itdupl)); } - template - const MetaType& URange::get(SizeT pos) const + template + const MetaT& URange::get(SizeT pos) const { return mSpace[pos]; } - template - const MetaType* URange::get() const + template + const MetaT* URange::get() const { return mSpace.data(); } - template - SizeT URange::getMeta(const MetaType& meta) const + template + SizeT URange::getMeta(const MetaT& meta) 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, meta, std::less()) - b; } - template - SizeT URange::size() const + template + SizeT URange::size() const { return mSpace.size(); } - template - SizeT URange::dim() const + template + SizeT URange::dim() const { return 1; } - template - String URange::stringMeta(SizeT pos) const + template + String URange::stringMeta(SizeT pos) const { return toString(this->get(pos)); } - template - const TypeInfo& URange::type() const + template + const TypeInfo& URange::type() const { - return typeid(URange); + return typeid(URange); } - template - const TypeInfo& URange::metaType() const + template + const TypeInfo& URange::metaType() const { - return typeid(MetaType); + return typeid(MetaT); } - template - RangePtr URange::extend(const RangePtr& r) const + template + RangePtr URange::extend(const RangePtr& r) const { - auto rx = rangeCast>(r); + auto rx = rangeCast>(r); auto space = mSpace; space.insert(space.end(), rx->mSpace.begin(), rx->mSpace.end()); - return URangeFactory( space ).create(); + return URangeFactory( space ).create(); } - template - Vector URange::key() const + template + Vector URange::key() const { return Vector { this->id() }; } @@ -317,30 +330,30 @@ namespace CNORXZ * Range Casts * *******************/ - template + template struct URangeCast { template - static inline Sptr> transform(const RangePtr& r) + static inline Sptr> transform(const RangePtr& r) { if(r->type() == typeid(URange)){ auto rr = std::dynamic_pointer_cast>(r); - Vector v(rr->size()); + Vector v(rr->size()); std::transform(rr->begin(), rr->end(), v.begin(), - [](const T& x) { return static_cast(x); } ); - return std::dynamic_pointer_cast> - ( URangeFactory(std::move(v)).create() ); + [](const T& x) { return static_cast(x); } ); + return std::dynamic_pointer_cast> + ( URangeFactory(std::move(v)).create() ); } else { return nullptr; } } - static inline Sptr> cast(const RangePtr& r) + static inline Sptr> cast(const RangePtr& r) { - static_assert(std::is_fundamental::value, "got non-fundamental type"); + static_assert(std::is_fundamental::value, "got non-fundamental type"); CXZ_ASSERT(r->dim() == 1, "range cast into URange: source range must have dim = 1, got " << r->dim()); - Sptr> o = nullptr; + Sptr> o = nullptr; o = transform(r); if(o) return o; o = transform(r); if(o) return o; o = transform(r); if(o) return o; @@ -415,11 +428,11 @@ namespace CNORXZ }; - template - Sptr> RangeCast>::func(const RangePtr& r) + template + Sptr> RangeCast>::func(const RangePtr& r) { - if constexpr(std::is_fundamental::value or is_vector::value){ - return URangeCast::cast(r); + if constexpr(std::is_fundamental::value or is_vector::value){ + return URangeCast::cast(r); } else { CXZ_ERROR("no range cast available for input range '" << r->type().name() << "'"); diff --git a/src/include/ranges/urange.h b/src/include/ranges/urange.h index f6ef6f1..92aeb78 100644 --- a/src/include/ranges/urange.h +++ b/src/include/ranges/urange.h @@ -55,6 +55,8 @@ namespace CNORXZ UIndex& at(const MetaT& metaPos); decltype(auto) xpr(const Sptr>& _this) const; + RangePtr prange(const UIndex& end) const; + SizeT deepFormat() const; /* template @@ -94,12 +96,13 @@ namespace CNORXZ RangePtr mRef; }; - template - class URange : public RangeInterface> + template + class URange : public RangeInterface> { public: typedef RangeBase RB; - typedef UIndex IndexType; + typedef UIndex IndexType; + typedef MetaT MetaType; friend URangeFactory; diff --git a/src/include/ranges/xindex.cc.h b/src/include/ranges/xindex.cc.h index 65df9e8..c9b44e6 100644 --- a/src/include/ranges/xindex.cc.h +++ b/src/include/ranges/xindex.cc.h @@ -127,6 +127,12 @@ namespace CNORXZ return mI->stepSize(id); } + template + RangePtr XIndex::prange(const XIndexPtr& end) const + { + return mI->prange( *std::dynamic_pointer_cast(end) ); + } + template Vector XIndex::deepFormat() const { diff --git a/src/include/ranges/xindex.h b/src/include/ranges/xindex.h index 93bb23d..ce79f44 100644 --- a/src/include/ranges/xindex.h +++ b/src/include/ranges/xindex.h @@ -38,6 +38,7 @@ namespace CNORXZ virtual SizeT dim() const = 0; virtual RangePtr range() const = 0; virtual UPos stepSize(const IndexId<0>& id) const = 0; + virtual RangePtr prange(const XIndexPtr& end) const = 0; virtual Vector deepFormat() const = 0; //virtual Vector pack() const = 0; //virtual Vector format() const = 0; @@ -92,6 +93,7 @@ namespace CNORXZ virtual SizeT dim() const override final; virtual RangePtr range() const override final; virtual UPos stepSize(const IndexId<0>& id) const override final; + virtual RangePtr prange(const XIndexPtr& end) const override final; virtual Vector deepFormat() const override final; //virtual Vector pack() const override final; //virtual Vector format() const override final; diff --git a/src/include/ranges/yrange.h b/src/include/ranges/yrange.h index 16b476c..6cfeb71 100644 --- a/src/include/ranges/yrange.h +++ b/src/include/ranges/yrange.h @@ -66,6 +66,7 @@ namespace CNORXZ YIndex& operator()(); const DPack& pack() const; + RangePtr prange(const YIndex& end) const; Vector deepFormat() const; const YFormat& format() const; const YFormat& lexFormat() const; @@ -160,10 +161,7 @@ namespace CNORXZ static Sptr func(const RangePtr& r); }; - template <> - RangePtr prange(const IndexInterface>& begin, - const IndexInterface>& end); - + } #endif diff --git a/src/lib/ranges/crange.cc b/src/lib/ranges/crange.cc index f3acd45..a42f413 100644 --- a/src/lib/ranges/crange.cc +++ b/src/lib/ranges/crange.cc @@ -1,5 +1,6 @@ #include "ranges/ranges.h" +#include "ranges/prange.h" #include "operation/operation.h" namespace CNORXZ @@ -115,6 +116,17 @@ namespace CNORXZ } return coproot(m.data(), _this); } + + RangePtr CIndex::prange(const CIndex& end) const + { + CXZ_ASSERT(end > *this, "got end index position smaller than begin index position"); + const SizeT beginPos = lex(); + Vector parts(end.lex() - beginPos); + for(auto i = *this; i != end; ++i){ + parts[i.lex()-beginPos] = i.lex(); + } + return CNORXZ::prange(mRangePtr, parts); + } SizeT CIndex::deepFormat() const { diff --git a/src/lib/ranges/dindex.cc b/src/lib/ranges/dindex.cc index 972042f..9188c53 100644 --- a/src/lib/ranges/dindex.cc +++ b/src/lib/ranges/dindex.cc @@ -167,6 +167,11 @@ namespace CNORXZ return mI->xpr(_this->xptr()); } + RangePtr DIndex::prange(const DIndex& end) const + { + return mI->prange( end.xptr() ); + } + Vector DIndex::deepFormat() const { return mI->deepFormat(); diff --git a/src/lib/ranges/yrange.cc b/src/lib/ranges/yrange.cc index 082fc22..cae29b8 100644 --- a/src/lib/ranges/yrange.cc +++ b/src/lib/ranges/yrange.cc @@ -368,6 +368,17 @@ namespace CNORXZ return mIs; } + RangePtr YIndex::prange(const YIndex& end) const + { + CXZ_ASSERT(dim() == end.dim(), "end index has different number of dimensions (" + << end.dim() << ") than begin index (" << dim() << ")"); + Vector v(dim()); + for(SizeT i = 0; i != dim(); ++i){ + v[i] = mIs[i]->prange( end.pack()[i] ); + } + return YRangeFactory(v).create(); + } + Vector YIndex::deepFormat() const { Vector> dfv(mIs.size()); @@ -561,17 +572,4 @@ namespace CNORXZ return std::dynamic_pointer_cast( YRangeFactory({r}).create() ); } - template <> - RangePtr prange(const IndexInterface>& begin, - const IndexInterface>& end) - { - CXZ_WARNING("YRange specialization"); // test - const SizeT dim = begin.range()->dim(); - Vector v(dim); - for(SizeT i = 0; i != dim; ++i){ - v[i] = prange( DIndex(begin.THIS().pack()[i]), DIndex(end.THIS().pack()[i]) ); - } - return YRangeFactory(v).create(); - } - }