diff --git a/src/include/base/base.cc.h b/src/include/base/base.cc.h index 31b3622..8f547df 100644 --- a/src/include/base/base.cc.h +++ b/src/include/base/base.cc.h @@ -1,2 +1,3 @@ #include "dtype.cc.h" +#include "obj_handle.cc.h" diff --git a/src/include/base/base.h b/src/include/base/base.h index 9dc7a11..f0bfbe0 100644 --- a/src/include/base/base.h +++ b/src/include/base/base.h @@ -8,5 +8,7 @@ #include "macros.h" #include "assert.h" #include "types.h" +#include "obj_handle.h" +#include "dtype.h" #endif diff --git a/src/include/base/obj_handle.cc.h b/src/include/base/obj_handle.cc.h new file mode 100644 index 0000000..58a5084 --- /dev/null +++ b/src/include/base/obj_handle.cc.h @@ -0,0 +1,62 @@ + +#ifndef __cxz_obj_handle_cc_h__ +#define __cxz_obj_handle_cc_h__ + +#include "obj_handle.h" + +namespace CNORXZ +{ + + template + ObjHandle::ObjHandle() : mC(std::make_unique()) {} + + template + ObjHandle::ObjHandle(const T& a) : mC(std::make_unique(a)) {} + + template + ObjHandle::ObjHandle(const ObjHandle& a) : mC(std::make_unique(*a.mC)) {} + + template + ObjHandle::ObjHandle(ObjHandle&& a) : mC(a.mC) {} + + template + ObjHandle& ObjHandle::operator=(const ObjHandle& a) + { + mC = std::make_unique(*a.mC); + return *this; + } + + template + ObjHandle& ObjHandle::operator=(ObjHandle&& a) + { + mC = a.mC; + return *this; + } + + template + T& ObjHandle::operator*() + { + return *mC; + } + + template + T* ObjHandle::operator->() + { + return &*mC; + } + + template + const T& ObjHandle::operator*() const + { + return *mC; + } + + template + const T* ObjHandle::operator->() const + { + return &*mC; + } + +} + +#endif diff --git a/src/include/base/obj_handle.h b/src/include/base/obj_handle.h new file mode 100644 index 0000000..96a7f9b --- /dev/null +++ b/src/include/base/obj_handle.h @@ -0,0 +1,35 @@ + +#ifndef __cxz_obj_handle_h__ +#define __cxz_obj_handle_h__ + +#include "types.h" + +namespace CNORXZ +{ + + // Pointer wrapper for directly handling objects via abstract base class; + // Provides appropriate copy implementation + template + class ObjHandle + { + private: + std::unique_ptr mC; + + public: + + ObjHandle(); + ObjHandle(const T& a); + ObjHandle(const ObjHandle& a); + ObjHandle(ObjHandle&& a); + ObjHandle& operator=(const ObjHandle& a); + ObjHandle& operator=(ObjHandle&& a); + + T& operator*(); + T* operator->(); + const T& operator*() const; + const T* operator->() const; + }; + +} + +#endif diff --git a/src/include/base/types.h b/src/include/base/types.h index 9c5e93d..535641e 100644 --- a/src/include/base/types.h +++ b/src/include/base/types.h @@ -31,6 +31,12 @@ namespace CNORXZ template using Sptr = std::shared_ptr; + template + using Wptr = std::weak_ptr; + + template + using Uptr = std::unique_ptr; + template using Tuple = std::tuple; @@ -49,6 +55,10 @@ namespace CNORXZ // definition: base/dtype.h class DType; + // definition: base/obj_handle.h + template + class ObjHandle; + // definition: memory/allocator.h template class Allocator; diff --git a/src/include/ranges/crange.h b/src/include/ranges/crange.h index dccd37e..7390030 100644 --- a/src/include/ranges/crange.h +++ b/src/include/ranges/crange.h @@ -1,87 +1,106 @@ -//#ifdef include_range_type -//include_range_type(NONE,0) -//#else - -#ifndef include_range_type -#ifdef __cxz_ranges_header__ -// assert, that this is only used within range_types/header.h - #ifndef __cxz_range_type_classic_def__ #define __cxz_range_type_classic_def__ +#include "base/base.h" +#include "ranges/index_base.h" +#include "ranges/range_base.h" +#include "xfor/for_type.h" + namespace CNORXZ { - typedef GenSingleIndex ClassicIndex; - - template <> - class GenSingleRangeFactory : public RangeFactoryBase + class CIndex : public IndexInterface,SizeT> { public: - - typedef GenSingleRange oType; - GenSingleRangeFactory(size_t size = 0); - std::shared_ptr create(); + typedef IndexInterface,SizeT> IB; + typedef CRange RangeType; + + CIndex(const RangePtr& range); + CIndex& operator=(SizeT pos); + CIndex& operator++(); + CIndex& operator--(); + CIndex operator+(Int n) const; + CIndex operator-(Int n) const; + CIndex& operator+=(Int n); + CIndex& operator-=(Int n); + + SizeT operator*() const; + SizeT operator->() const; + + Int pp(PtrId idxPtrNum); + Int mm(PtrId idxPtrNum); + + SizeT dim() const; // = 1 + Sptr range() const; + SizeT getStepSize(SizeT n) const; + + String stringMeta() const; + SizeT meta() const; + CIndex& at(const SizeT& metaPos); + + template + auto ifor(SizeT step, Expr ex) const + -> For,Expr>; + + template + auto iforh(SizeT step, Expr ex) const + -> For,Expr,ForType::HIDDEN>; + + template + auto pifor(SizeT step, Expr ex) const + -> PFor,Expr>; + + private: + Sptr mRangePtr; }; - template <> - class GenSingleRange : public RangeInterface + + class CRangeFactory : public RangeFactoryBase + { + public: + typedef CRange oType; + + CRangeFactory(SizeT size); + CRangeFactory(SizeT size, RangePtr ref); + + protected: + virtual void make() override; + + private: + RangePtr mRef; + }; + + class CRange : public RangeInterface { public: typedef RangeBase RB; - typedef typename RangeInterface >::IndexType IndexType; - typedef GenSingleRange RangeType; - typedef size_t MetaType; - typedef GenSingleRangeFactory FType; + typedef CIndex IndexType; + typedef CRangeFactory FType; - virtual size_t size() const final; - virtual size_t dim() const final; + friend CRangeFactory; - virtual SpaceType spaceType() const final; - virtual DataHeader dataHeader() const final; - - virtual vector typeNum() const final; - virtual size_t cmeta(char* target, size_t pos) const final; - virtual size_t cmetaSize() const final; - virtual std::string stringMeta(size_t pos) const final; - virtual vector data() const final; - - size_t get(size_t pos) const; - size_t getMeta(size_t metaPos) const; - + virtual SizeT size() const final; + virtual SizeT dim() const final; + virtual String stringMeta(SizeT pos) const final; virtual IndexType begin() const final; virtual IndexType end() const final; - //virtual std::shared_ptr index() const final; - - friend GenSingleRangeFactory; - static constexpr bool defaultable = true; - static constexpr size_t ISSTATIC = 0; - static constexpr size_t SIZE = MUI; - static constexpr bool HASMETACONT = false; - - static GenSingleRangeFactory factory(size_t size = 0) - { return GenSingleRangeFactory(size); } - + SizeT get(SizeT pos) const; + SizeT getMeta(SizeT metaPos) const; + protected: - size_t mSize = 0; - - GenSingleRange() = default; - GenSingleRange(const GenSingleRange& in) = delete; + CRange() = default; + CRange(const CRange& in) = delete; + CRange(SizeT size); - GenSingleRange(size_t size); + SizeT mSize = 0; }; - typedef GenSingleRange ClassicRange; - typedef GenSingleRangeFactory ClassicRF; + template <> + Sptr rangeCast(const RangePtr& r); } - -#endif // #ifndef __cxz_range_type_classic_def__ - -#endif // #ifdef __cxz_ranges_header__ - -#endif // #ifdef include_range_type +#endif diff --git a/src/include/ranges/index_base.cc.h b/src/include/ranges/index_base.cc.h index f192ff1..a3ccfbb 100644 --- a/src/include/ranges/index_base.cc.h +++ b/src/include/ranges/index_base.cc.h @@ -18,14 +18,14 @@ namespace CNORXZ template IndexInterface::IndexInterface(const IndexInterface& in) : - mRangePtr(in.mRangePtr), mPos(in.mPos), mMax(in.mMax) + mPos(in.mPos) { mPtrNum = reinterpret_cast(this); } template IndexInterface::IndexInterface(IndexInterface&& in) : - mRangePtr(in.mRangePtr), mPos(in.mPos), mMax(in.mMax) + mPos(in.mPos) { mPtrNum = reinterpret_cast(this); } @@ -35,7 +35,6 @@ namespace CNORXZ { mRangePtr = in.mRangePtr; mPos = in.mPos; - mMax = in.mMax; return *this; } @@ -44,13 +43,12 @@ namespace CNORXZ { mRangePtr = in.mRangePtr; mPos = in.mPos; - mMax = in.mMax; return *this; } template - IndexInterface::IndexInterface(const RangePtr& range, SizeT pos) : - mRangePtr(range), mPos(pos), mMax(mRangePtr->size()) + IndexInterface::IndexInterface(SizeT pos) : + mPos(pos) { mPtrNum = reinterpret_cast(this); } @@ -58,13 +56,13 @@ namespace CNORXZ template bool IndexInterface::operator==(const IndexInterface& in) const { - return in.mPos == mPos and in.mRangePtr.get() == mRangePtr.get(); + return in.mPos == mPos and *in.range() == *range(); } template bool IndexInterface::operator!=(const IndexInterface& in) const { - return in.mPos != mPos or in.mRangePtr.get() != mRangePtr.get(); + return in.mPos != mPos or *in.range() != *range(); } template diff --git a/src/include/ranges/index_base.h b/src/include/ranges/index_base.h index 160d6e7..79d8d30 100644 --- a/src/include/ranges/index_base.h +++ b/src/include/ranges/index_base.h @@ -29,7 +29,7 @@ namespace CNORXZ Int operator-(const IndexInterface& i) const { return mPos - i.mPos; } SizeT pos() const; - SizeT max() const; + SizeT max() const { return THIS().max(); } PtrId ptrId() const; bool operator==(const IndexInterface& in) const; @@ -49,7 +49,7 @@ namespace CNORXZ SizeT getStepSize(SizeT n) const { return THIS().getStepSize(n); } String stringMeta() const { return THIS().stringMeta(); } - MetaType meta() const { return THIS().meta(); } + auto meta() const { return THIS().meta(); } I& at(const MetaType& meta) { return THIS().at(meta); } template @@ -70,11 +70,10 @@ namespace CNORXZ IndexInterface& operator=(const IndexInterface& in); IndexInterface(IndexInterface&& in); IndexInterface& operator=(IndexInterface&& in); - IndexInterface(const RangePtr& range, SizeT pos); + IndexInterface(SizeT pos); IndexPtr mRel = nullptr; SizeT mPos = 0; - SizeT mMax = 0; PtrId mPtrId = 0; }; diff --git a/src/include/ranges/range_base.cc.h b/src/include/ranges/range_base.cc.h new file mode 100644 index 0000000..8e40a06 --- /dev/null +++ b/src/include/ranges/range_base.cc.h @@ -0,0 +1,23 @@ + +#ifndef __cxz_range_base_cc_h__ +#define __cxz_range_base_cc_h__ + +#include "range_base.h" + +namespace CNORXZ +{ + template + Index RangeInterface::begin() const + { + return Index(RangePtr(RB::mThis), 0); + } + + template + Index RangeInterface::end() const + { + return Index(RangePtr(RB::mThis), this->size()); + } + +} + +#endif diff --git a/src/include/ranges/range_base.h b/src/include/ranges/range_base.h index ac31ea5..4afa25f 100644 --- a/src/include/ranges/range_base.h +++ b/src/include/ranges/range_base.h @@ -8,35 +8,6 @@ namespace CNORXZ { - SizeT indexId(); - - enum class SpaceType - { - NONE = 0, // meta data is that of a classic range, i.e. 0,1,2,...,N-1 - ANY = 1, // meta data is arbitrary, i.e. explicitly stored; range could be multiple -#define include_range_type(x,n) x = n, -#include "range_types/header.h" -#undef include_range_type - ANON = -1, // anonymous content - DYN = -3 // dynamic content - }; - - struct DataHeader - { - public: - static constexpr SizeT VERSION = 1; // fixed by version of this repository ! - private: - SizeT version = VERSION; - public: - int spaceType = static_cast( SpaceType::NONE ); - SizeT metaSize = 0; // size of meta data - int multiple = 0; // = 1 if multi range - int metaType = 0; // type of meta data - inline SizeT v() const { return version; } - }; - - typedef Sptr RangePtr; - class RangeFactoryBase { public: @@ -57,6 +28,7 @@ namespace CNORXZ private: // also add single ranges here (PtrId -> own) + // rangeCast: PtrId -> original Range static Map,RangePtr>> sCreated; }; @@ -77,21 +49,12 @@ namespace CNORXZ bool operator!=(const RangeBase& in) const; virtual TypeInfo type() const = 0; - PtrId id() const; - - XIndexPtr beginX() const; - XIndexPtr endX() const; - - virtual Vector typeNum() const = 0; - virtual SizeT cmeta(char* target, SizeT pos) const = 0; - virtual SizeT cmetaSize() const = 0; + virtual TypeInfo metaType() const = 0; virtual String stringMeta(SizeT pos) const = 0; - virtual Vector data() const = 0; // usefull when writing to files, etc... - virtual SpaceType spaceType() const = 0; - virtual DataHeader dataHeader() const = 0; - - virtual RangePtr sub(SizeT num) const { return RangePtr(); } + PtrId id() const; + XIndexPtr begin() const; + XIndexPtr end() const; friend RangeFactoryBase; @@ -101,30 +64,23 @@ namespace CNORXZ std::weak_ptr mThis; }; - template - inline Sptr mkIndexWrapper(const Index& i); - template class RangeInterface : public RangeBase { public: - //typedef typename Index::MetaType MetaType; typedef Index IndexType; - - static constexpr SpaceType STYPE = IndexType::STYPE; + typedef RangeBase RB; - virtual Index begin() const = 0; - virtual Index end() const = 0; - Sptr beginPtr() const { return std::make_shared(this->begin()); } - Sptr endPtr() const { return std::make_shared(this->end()); } - virtual Sptr aindex() const override final - { return mkIndexWrapper(this->begin()); } + Index begin() const; + Index end() const; protected: RangeInterface() = default; }; + template + Sptr rangeCast(const RangePtr r); RangePtr operator*(const RangePtr& a, const RangePtr& b); // -> Ptr to MultiRange } diff --git a/src/include/ranges/urange.cc.h b/src/include/ranges/urange.cc.h new file mode 100644 index 0000000..d255da2 --- /dev/null +++ b/src/include/ranges/urange.cc.h @@ -0,0 +1,255 @@ + +#ifndef __cxz_urange_cc_h__ +#define __cxz_urange_cc_h__ + +#include +#include + +#include "urange.h" + +namespace CNORXZ +{ + /***************** + * UIndex * + *****************/ + + namespace + { + template + Sptr> rc(const RangePtr r) + { + if(typeid(URange) == r.type()){ + return std::dynamic_pointer_cast>(r) + } + else { + return rangeCast>(r); + } + } + } + + template + UIndex::UIndex(const RangePtr& range) : + IndexInterface,MetaType>(0), + mRange(rc(range)), + mMetaPtr(&get(0)) + {} + + template + UIndex& UIndex::operator=(size_t pos) + { + IB::mPos = pos; + return *this; + } + + template + UIndex& UIndex::operator++() + { + ++IB::mPos; + return *this; + } + + template + UIndex& UIndex::operator--() + { + --IB::mPos; + return *this; + } + + template + int UIndex::pp(std::intptr_t idxPtrNum) + { + ++(*this); + return 1; + } + + template + int UIndex::mm(std::intptr_t idxPtrNum) + { + --(*this); + return 1; + } + + template + String UIndex::stringMeta() const + { + return toString(this->meta()); + } + + template + MetaType& UIndex::meta() const + { + return mSpace[IB::mPos]; + } + + template + UIndex& UIndex::at(const MetaType& metaPos) + { + (*this) = mRangePtr->getMeta(metaPos); + return *this; + } + + template + size_t UIndex::dim() // = 1 + { + return 1; + } + + template + Sptr> UIndex::range() + { + return mRangePtr; + } + + template + size_t UIndex::getStepSize(size_t n) + { + return 1; + } + + template + template + auto UIndex::ifor(size_t step, Expr ex) const + -> For,Expr> + { + return For,Expr>(this, step, ex); + } + + template + template + auto UIndex::iforh(size_t step, Expr ex) const + -> For,Expr,ForType::HIDDEN> + { + return For,Expr,ForType::HIDDEN>(this, step, ex); + } + + template + template + auto UIndex::pifor(size_t step, Expr ex) const + -> PFor,Expr> + { + return PFor,Expr>(this, step, ex); + } + + + /******************** + * SingleRange * + ********************/ + + template + URangeFactory::URangeFactory(const Vector& space) : + mSpace(space) {} + + template + URangeFactory::URangeFactory(Vector&& space) : + mSpace(space) {} + + template + URangeFactory::URangeFactory(const Vector& space, const RangePtr& ref) : + mSpace(space), mRef(ref) {} + + template + URangeFactory::URangeFactory(Vector&& space, const RangePtr& ref) : + mSpace(space), mRef(ref) {} + + template + void URangeFactory::make() + { + if(mRef != nullptr) { + mProd = this->fromCreated[typeid(oType)][mRef->id()]; + } + if(mProd == nullptr){ + RangePtr key = mProd = std::shared_ptr + ( new URange( std::move(mSpace) ) ); + if(mRef != nullptr) { key = mRef->id(); } + this->addToCreated(typeid(oType), { key }, mProd); + } + } + + /******************** + * SingleRange * + ********************/ + + template + URange::URange(const Vector& space) : + RangeInterface>(), + mSpace(space) + { + 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>(), + mSpace(space) + { + 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 + const MetaType& URange::get(size_t pos) const + { + return mSpace[pos]; + } + + template + SizeT URange::getMeta(const MetaType& meta) const + { + auto b = mSpace.begin(); + auto e = mSpace.end(); + return std::lower_bound(b, e, meta, std::less()) - b; + } + + template + SizeT URange::size() const + { + return mSpace.size(); + } + + template + SizeT URange::dim() const + { + return 1; + } + + template + String URange::stringMeta(SizeT pos) const + { + return toString(this->get(pos)); + } + + template + typename URange::IndexType URange::begin() const + { + UIndex i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + i = 0; + return i; + } + + template + typename URange::IndexType URange::end() const + { + UIndex i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + i = this->size(); + return i; + } + + /******************* + * Range Casts * + *******************/ + + template + Sptr> rangeCast>(const RangePtr& r) + { + CXZ_ERROR("to be implemented..."); + return nullptr; + } +} + +#endif diff --git a/src/include/ranges/urange.h b/src/include/ranges/urange.h index a06de4b..f62ca3b 100644 --- a/src/include/ranges/urange.h +++ b/src/include/ranges/urange.h @@ -40,7 +40,7 @@ namespace CNORXZ SizeT getStepSize(SizeT n) const; String stringMeta() const; - MetaType meta() const; + const MetaType& meta() const; UIndex& at(const MetaType& metaPos); template @@ -64,12 +64,18 @@ namespace CNORXZ class URangeFactory : public RangeFactoryBase { public: - URangeFactory() = delete; - URangeFactory(const Vector& space); - URangeFactory(Vector&& space); - + URangeFactory(const Vector& space); + URangeFactory(Vector&& space); + URangeFactory(const Vector& space, const RangePtr& ref); + URangeFactory(Vector&& space, const RangePtr& ref); + protected: - void make(); + URangeFactory() = default; + virtual void make() override; + + private: + Vector mSpace; + RangePtr mRef = nullptr; }; template @@ -80,342 +86,30 @@ namespace CNORXZ typedef UIndex IndexType; typedef URangeFactory FType; + friend URangeFactory; + virtual SizeT size() const final; virtual SizeT dim() const final; + virtual String stringMeta(SizeT pos) const final; + virtual IndexType begin() const final; + virtual IndexType end() const final; - virtual std::string stringMeta(SizeT pos) const final; const MetaType& get(SizeT pos) const; SizeT getMeta(const MetaType& metaPos) const; - virtual IndexType begin() const final; - virtual IndexType end() const final; - friend URangeFactory; - protected: URange() = delete; URange(const URange& in) = delete; - URange(const vector& space); - URange(vector&& space); + URange(const Vector& space); + URange(Vector&& space); - Vector mSpace; // SORTED!!! + Vector mSpace; }; + template + Sptr> rangeCast>(const RangePtr& r); } -/* ========================= * - * --- TEMPLATE CODE --- * - * ========================= */ - -namespace CNORXZ -{ - /***************** - * UIndex * - *****************/ - - template - GenSingleIndex::GenSingleIndex(const std::shared_ptr >& range) : - IndexInterface,U>(range, 0), - mExplicitRangePtr(std::dynamic_pointer_cast(IB::mRangePtr)), - mMetaPtr(MetaPtrHandle::RangeType::HASMETACONT>::set - ( dynamic_cast(IB::mRangePtr.get() ) ) ) {} - - template - IndexType GenSingleIndex::type() const - { - return IndexType::SINGLE; - } - - template - GenSingleIndex& GenSingleIndex::operator=(size_t pos) - { - IB::mPos = pos; - return *this; - } - - template - GenSingleIndex& GenSingleIndex::operator++() - { - ++IB::mPos; - return *this; - } - - template - GenSingleIndex& GenSingleIndex::operator--() - { - --IB::mPos; - return *this; - } - - template - int GenSingleIndex::pp(std::intptr_t idxPtrNum) - { - ++(*this); - return 1; - } - - template - int GenSingleIndex::mm(std::intptr_t idxPtrNum) - { - --(*this); - return 1; - } - - template - std::string GenSingleIndex::stringMeta() const - { - return std::dynamic_pointer_cast const>( IB::mRangePtr )->stringMeta(IB::mPos); - } - - template - U GenSingleIndex::meta() const - { - return MetaPtrHandle::RangeType::HASMETACONT>::getMeta - ( mMetaPtr, IB::mPos, mExplicitRangePtr ); - } - - template - const U* GenSingleIndex::metaPtr() const - { - return mMetaPtr; - } - - template - bool GenSingleIndex::isMeta(const U& metaPos) const - { - return mExplicitRangePtr->isMeta(metaPos); - } - - template - GenSingleIndex& GenSingleIndex::at(const U& metaPos) - { - (*this) = std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ); - return *this; - } - - template - size_t GenSingleIndex::posAt(const U& metaPos) const - { - return std::dynamic_pointer_cast const>( IB::mRangePtr )->getMeta( metaPos ); - } - - template - size_t GenSingleIndex::dim() // = 1 - { - return 1; - } - - template - bool GenSingleIndex::last() - { - return IB::mPos == IB::mMax - 1; - } - - template - bool GenSingleIndex::first() - { - return IB::mPos == 0; - } - - template - std::shared_ptr::RangeType> GenSingleIndex::range() - { - return mExplicitRangePtr; - } - - template - template - void GenSingleIndex::getPtr() {} - - template - size_t GenSingleIndex::getStepSize(size_t n) - { - return 1; - } - - template - template - auto GenSingleIndex::ifor(size_t step, Expr ex) const - -> For,Expr> - { - return For,Expr>(this, step, ex); - } - - template - template - auto GenSingleIndex::iforh(size_t step, Expr ex) const - -> For,Expr,ForType::HIDDEN> - { - return For,Expr,ForType::HIDDEN>(this, step, ex); - } - - template - template - auto GenSingleIndex::pifor(size_t step, Expr ex) const - -> PFor,Expr> - { - return PFor,Expr>(this, step, ex); - } - - - /******************** - * SingleRange * - ********************/ - - template - GenSingleRangeFactory::GenSingleRangeFactory(const vector& space) - { - mProd = std::shared_ptr( new GenSingleRange( space ) ); - } - - template - GenSingleRangeFactory::GenSingleRangeFactory(vector&& space) - { - mProd = std::shared_ptr( new GenSingleRange( space ) ); - } - - template - std::shared_ptr GenSingleRangeFactory::create() - { - setSelf(); - return mProd; - } - - /******************** - * SingleRange * - ********************/ - - template - GenSingleRange::GenSingleRange(const vector& space) : - RangeInterface >(), - mSpace(space), mMSpace(mSpace) - { - //for(size_t i = 0; i != mSpace.size(); ++i){ - // mMSpace[mSpace[i]] = i; - //} - } - - template - GenSingleRange::GenSingleRange(vector&& space) : - RangeInterface >(), - mSpace(space), mMSpace(mSpace) {} - - - template - const U& GenSingleRange::get(size_t pos) const - { - return mSpace[pos]; - } - - template - inline size_t getStatSizeOrDyn(size_t size) - { - return SIZE; - } - - template <> - inline size_t getStatSizeOrDyn(size_t size) - { - return size; - } - - template - size_t GenSingleRange::getMeta(const U& metaPos) const - { - return mMSpace.at(metaPos); - } - - template - size_t GenSingleRange::size() const - { - return getStatSizeOrDyn(mSpace.size()); - } - - template - size_t GenSingleRange::dim() const - { - return 1; - } - - template - bool GenSingleRange::isMeta(const U& metaPos) const - { - return mMSpace.count(metaPos) != 0; - } - - template - SpaceType GenSingleRange::spaceType() const - { - return TYPE; - } - - template - vector GenSingleRange::typeNum() const - { - return {NumTypeMap::num()}; - } - - template - size_t GenSingleRange::cmeta(char* target, size_t pos) const - { - return ToCMeta::apply(target, mSpace[pos]); - } - - template - size_t GenSingleRange::cmetaSize() const - { - return ToCMeta::size(mSpace[0]); - } - - template - std::string GenSingleRange::stringMeta(size_t pos) const - { - return xToString(get(pos)); - } - - template - vector GenSingleRange::data() const - { - DataHeader h = dataHeader(); - vector out; - out.reserve(h.metaSize + sizeof(DataHeader)); - char* hcp = reinterpret_cast(&h); - out.insert(out.end(), hcp, hcp + sizeof(DataHeader)); - stringCat(out, mSpace); - return out; - } - - template - DataHeader GenSingleRange::dataHeader() const - { - DataHeader h; - h.spaceType = static_cast( TYPE ); - h.metaSize = metaSize(mSpace); - h.metaType = NumTypeMap::num(); - h.multiple = 0; - return h; - } - - template - typename GenSingleRange::IndexType GenSingleRange::begin() const - { - GenSingleIndex i( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis ) ) ); - i = 0; - return i; - } - - template - typename GenSingleRange::IndexType GenSingleRange::end() const - { - GenSingleIndex i( std::dynamic_pointer_cast > - ( std::shared_ptr( RB::mThis ) ) ); - i = size(); - return i; - } -} - -#include "range_types/header.h" - #endif diff --git a/src/include/ranges/xfor/exttype.h b/src/include/ranges/xfor/exttype.h index 1ff460a..76a12bd 100644 --- a/src/include/ranges/xfor/exttype.h +++ b/src/include/ranges/xfor/exttype.h @@ -6,6 +6,9 @@ namespace CNORXZInternal { + + // Dynamic Ext: ObjHandl + // In For Expr: try to resolve until some upper limit struct None; @@ -20,7 +23,6 @@ namespace CNORXZInternal virtual size_t size() const = 0; virtual const size_t& val() const = 0; - //virtual size_t rootSteps() const = 0; virtual bool operator==(const ExtBase& in) const = 0; virtual bool operator==(size_t in) const = 0; @@ -33,7 +35,6 @@ namespace CNORXZInternal template const ExtType& expl() const; - virtual std::string stype() const = 0; }; typedef std::shared_ptr DExt; @@ -58,8 +59,6 @@ namespace CNORXZInternal virtual std::shared_ptr deepCopy() const override final { return std::make_shared>(mExt); } virtual size_t size() const override final { return sizeof(ExtType)/sizeof(size_t); } - //virtual size_t size() const override final { return ExtType::MExtSize(); } - //virtual size_t rootSteps() const override final; const ExtType& ext() const { return mExt; } virtual const size_t& val() const override final { return mExt.val(); } virtual void zero() override final { mExt.zero(); } @@ -75,9 +74,7 @@ namespace CNORXZInternal virtual DExt operator*(size_t in) const override final { return std::make_shared>( mExt * in ); } - virtual std::string stype() const override final { return std::string("T[") + mExt.stype() + "]"; } }; - //class DExtT; template DExt mkDExt(const ExtT& in) @@ -275,13 +272,7 @@ namespace CNORXZInternal DExtTX& operator=(const DExtTX& in) { mNext = in.mNext; mDExt = in.mDExt->deepCopy(); return *this; } DExtTX& operator=(DExtTX&& in) { mNext = in.mNext; mDExt = in.mDExt->deepCopy(); return *this; } explicit DExtTX(const DExt& in) : mDExt(in) {} - /* - template - DExtTX& operator=(const Y& y) { mDExt = std::make_shared>(y); return *this; } - template - DExtTX(const Y& y) : mDExt(std::make_shared>(y)) {} - */ bool operator==(const DExtTX& in) const { return *mDExt == *in.mDExt and mNext == in.mNext; } @@ -320,7 +311,6 @@ namespace CNORXZInternal template inline auto nn() const; - std::string stype() const { return std::string("D[") + mDExt->stype() + "," + mNext.stype() + "]"; } }; typedef DExtTX DExtT; diff --git a/src/include/ranges/xfor/xfor.h b/src/include/ranges/xfor/xfor.h index 3880b6c..62dd797 100644 --- a/src/include/ranges/xfor/xfor.h +++ b/src/include/ranges/xfor/xfor.h @@ -2,50 +2,49 @@ #ifndef __cxz_xfor_h__ #define __cxz_xfor_h__ -#include -#include -#include -#include "xfor/for_type.h" -#include "xfor/for_utils.h" -#include "xfor/exttype.h" - -#include "allocator.h" #include -#define VCHECK(a) std::cout << __FILE__ << ": @" << __LINE__ \ - << " in " << __func__ << ": " << #a << " = " << a << std::endl; +#include "base/base.h" +#include "for_type.h" +#include "for_utils.h" +#include "exttype.h" -namespace CNORXZInternal +namespace CNORXZ { - using namespace CNORXZ; // 'HIDDEN FOR' CLASS for nested for loops in contractions a.s.o. // (NO COUNTING OF MASTER POSITION !!!!!) - //typedef std::pair DExt; + template + class ExpressionInterface + { + public: + DEFAULT_MEMBERS(ExpressionInterface); + Expr& THIS() { return static_cast(*this); } + const Expr& THIS() const { return static_cast(*this); } - - class ExpressionBase + Sptr copy() const { THIS().copy(); } + + void operator(SizeT mlast, Pos last) { THIS()(mlast, last); } + void operator(SizeT mlast = 0) { THIS()(mlast); } + + Pos rootSteps(PtrId ptrId = 0) const { return THIS().rootSteps(ptrId); } + Pos extension() const { return THIS().extenrion(); } + }; + + class XprBase : public ExpressionInterface { public: - ExpressionBase() = default; - ExpressionBase(const ExpressionBase& in) = default; - ExpressionBase(ExpressionBase&& in) = default; - ExpressionBase& operator=(const ExpressionBase& in) = default; - ExpressionBase& operator=(ExpressionBase&& in) = default; + DEFAULT_MEMBERS(XprBase); - //virtual size_t divResid() const { return 1; } - virtual std::intptr_t vI() const { return 0; } + virtual Sptr copy() const = 0; - virtual std::shared_ptr deepCopy() const = 0; - - virtual void operator()(size_t mlast, DExt last) = 0; - virtual void operator()(size_t mlast = 0) = 0; + virtual void operator()(SizeT mlast, DExt last) = 0; + virtual void operator()(SizeT mlast) = 0; - virtual DExt dRootSteps(std::intptr_t iPtrNum = 0) const = 0; - virtual DExt dExtension() const = 0; - + virtual DExt rootSteps(PtrId iPtrNum = 0) const = 0; + virtual DExt extension() const = 0; }; @@ -77,80 +76,6 @@ namespace CNORXZInternal } }; - template - struct ForBound - { - template - static inline size_t bound(size_t bound) - { - return bound / DIV; - } - }; - - template <> - struct ForBound<1> - { - template - static constexpr size_t bound(size_t bound) - { - return BOUND / DIV; - } - }; - - - template - class SingleExpression : public ExpressionBase - { - private: - SingleExpression() = default; - - const IndexClass* mIndPtr; - size_t mSPos; - size_t mMax; - - Expr mExpr; - typedef decltype(mExpr.rootSteps()) ExtType; - ExtType mExt; - - mutable ExtType mRootSteps; - - public: - typedef ExpressionBase EB; - - static constexpr size_t LAYER = Expr::LAYER + 1; - static constexpr size_t SIZE = Expr::SIZE; - static constexpr size_t NHLAYER = Expr::NHLAYER + 1; - - SingleExpression(const SingleExpression& in) = default; - SingleExpression& operator=(const SingleExpression& in) = default; - SingleExpression(SingleExpression&& in) = default; - SingleExpression& operator=(SingleExpression&& in) = default; - - SingleExpression(const std::shared_ptr& indPtr, - Expr expr); - - SingleExpression(const IndexClass* indPtr, - Expr expr); - - virtual std::shared_ptr deepCopy() const override final - { - return std::make_shared>(*this); - } - - template - inline auto vec() const { return *this; } - - inline void operator()(size_t mlast, DExt last) override final; - inline void operator()(size_t mlast, ExtType last); - inline void operator()(size_t mlast = 0) override final; - - DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final; - DExt dExtension() const override final; - - auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; - auto extension() const -> ExtType; - }; - template class SubExpr : public ExpressionBase { @@ -177,26 +102,15 @@ namespace CNORXZInternal static constexpr size_t SIZE = Expr::SIZE + 1; static constexpr size_t NHLAYER = Expr::NHLAYER + 1; - SubExpr(const SubExpr& in) = default; - SubExpr& operator=(const SubExpr& in) = default; - SubExpr(SubExpr&& in) = default; - SubExpr& operator=(SubExpr&& in) = default; + DEFAULT_MEMBERS_X(SubExpr); - SubExpr(const std::shared_ptr& indPtr, + SubExpr(const Sptr& indPtr, std::intptr_t siptr, const vector* subset, Expr expr); SubExpr(const IndexClass* indPtr, std::intptr_t siptr, const vector* subset, Expr expr); - virtual std::shared_ptr deepCopy() const override final - { - return std::make_shared>(*this); - } - - template - inline auto vec() const { return *this; } - inline void operator()(size_t mlast, DExt last) override final; inline void operator()(size_t mlast, ExtType last) ; inline void operator()(size_t mlast = 0) override final; @@ -208,58 +122,6 @@ namespace CNORXZInternal auto extension() const -> ExtType; }; - template - struct MkVFor - { - template - using ptype = PFor; - - template - using type = For; - }; - - template <> - struct MkVFor<1,true> - { - template - using ptype = PFor; - - template - using type = For; - }; - - template - struct MkVExpr - { - template - static auto mk(const Expr& e) - { - return e.template vec(); - } - - template - static inline size_t divResid(const Expr& e) - { - return e.divResid(); - } - }; - - template <> - struct MkVExpr<1> - { - template - static auto mk(const Expr& e) - { - return e; // terminate - } - - template - static inline size_t divResid(const Expr& e) - { - return 0; - } - }; - template struct NHLayer { @@ -315,49 +177,20 @@ namespace CNORXZInternal static constexpr size_t SIZE = Expr::SIZE; static constexpr size_t MAX = RangeType::SIZE / DIV; static constexpr size_t NHLAYER = (FT == ForType::HIDDEN) ? 0 : Expr::NHLAYER + 1; + + DEFAULT_MEMBERS(For); - For(const For& in) = default; - For& operator=(const For& in) = default; - For(For&& in) = default; - For& operator=(For&& in) = default; - - For(const std::shared_ptr& indPtr, + For(const Sptr& indPtr, size_t step, Expr expr); For(const IndexClass* indPtr, size_t step, Expr expr); - virtual std::shared_ptr deepCopy() const override final - { - return std::make_shared>(*this); - } - - //virtual size_t divResid() const override final { return mMax % DIV + MkVExpr::divResid(mExpr); } - - virtual std::intptr_t vI() const override final - { - if(mStep == 1 and NHLAYER == 1 and mMax % DIV == 0){ - //if(mStep == 1 and mMax % DIV == 0){ - //VCHECK(LAYER); - //VCHECK(NHLAYER); - return reinterpret_cast(mIndPtr); - } - return mExpr.vI(); - } - - template - auto vec() const - { - typedef typename MkVFor(-1)>:: - template type::template mk(mExpr)),FT> oType; - return oType(mIndPtr,mStep,MkVExpr::template mk(mExpr)); - } - inline void operator()(size_t mlast, DExt last) override final; inline void operator()(size_t mlast, ExtType last) ; inline void operator()(size_t mlast = 0) override final; - PFor parallel() const; + PFor parallel() const; DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final; DExt dExtension() const override final; @@ -367,93 +200,12 @@ namespace CNORXZInternal }; - - template - class PFor : public ExpressionBase - { - private: - PFor() = default; - - typedef typename IndexClass::RangeType RangeType; - const IndexClass* mIndPtr; - size_t mSPos; - size_t mMax; - size_t mStep; - - Expr mExpr; - typedef decltype(mExpr.rootSteps()) ExtType; - ExtType mExt; - - mutable ExtType mRootSteps; - - public: - typedef ExpressionBase EB; - - static constexpr size_t LAYER = Expr::LAYER + 1; - static constexpr size_t SIZE = Expr::SIZE; - static constexpr size_t MAX = RangeType::SIZE / DIV; - static constexpr size_t NHLAYER = Expr::NHLAYER + 1; - - PFor(const PFor& in) = default; - PFor& operator=(const PFor& in) = default; - PFor(PFor&& in) = default; - PFor& operator=(PFor&& in) = default; - - PFor(const std::shared_ptr& indPtr, - size_t step, Expr expr); - - PFor(const IndexClass* indPtr, - size_t step, Expr expr); - - //virtual size_t divResid() const override final { return mMax % DIV + MkVExpr::divResid(mExpr); } - virtual std::intptr_t vI() const override final - { - if(mStep == 1 and NHLAYER == 1 and mMax % DIV == 0){ - //if(mStep == 1 and mMax % DIV == 0){ - //VCHECK(LAYER); - //VCHECK(LAYER); - return reinterpret_cast(mIndPtr); - } - return mExpr.vI(); - } - - template - auto vec() const - { - typedef typename MkVFor(-1)>:: - template ptype::template mk(mExpr))> oType; - return oType(mIndPtr,mStep,MkVExpr::template mk(mExpr)); - } - - virtual std::shared_ptr deepCopy() const override final - { - return std::make_shared>(*this); - } - - inline void operator()(size_t mlast, DExt last) override final; - inline void operator()(size_t mlast, ExtType last) ; - inline void operator()(size_t mlast = 0) override final; - - DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final; - DExt dExtension() const override final; - - auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; - auto extension() const -> ExtType; - - }; - - template - inline size_t exceptMax(size_t max) { return max; } - - template <> - inline size_t exceptMax<1>(size_t max) { return 1; } - class DynamicExpression : public ExpressionBase { private: size_t mThreadId = 0; - std::shared_ptr mNext; + Sptr mNext; DynamicExpression() : mThreadId(omp_get_thread_num()) {} public: @@ -486,7 +238,7 @@ namespace CNORXZInternal return *this; } - DynamicExpression(const std::shared_ptr& next) : + DynamicExpression(const Sptr& next) : mNext(next) {} @@ -498,14 +250,11 @@ namespace CNORXZInternal template DynamicExpression(Expr ex) : mNext( std::make_shared(ex) ) {} - virtual std::shared_ptr deepCopy() const override final + virtual Sptr deepCopy() const override final { return std::make_shared(*this); } - template - inline auto vec() const { return *this; } - inline void operator()(size_t mlast, DExt last) override final; inline void operator()(size_t mlast, DExtT last) { (*this)(mlast,last.get()); } inline void operator()(size_t mlast = 0) override final; @@ -518,51 +267,7 @@ namespace CNORXZInternal }; - - template - class ExpressionHolder : public ExpressionBase - { - private: - ExpressionHolder() = default; - - DynamicExpression mExpr; - typedef decltype(std::declval().rootSteps()) ExtType; - ExtType mExt; - - mutable ExtType mRootSteps; - - public: - typedef ExpressionBase EB; - - static constexpr size_t LAYER = Expr::LAYER + 1; - static constexpr size_t SIZE = Expr::SIZE; - static constexpr size_t NHLAYER = Expr::NHLAYER + 1; - - ExpressionHolder(const ExpressionHolder& in) = default; - ExpressionHolder(ExpressionHolder&& in) = default; - ExpressionHolder& operator=(const ExpressionHolder& in) = default; - ExpressionHolder& operator=(ExpressionHolder&& in) = default; - - ExpressionHolder(DynamicExpression expr); - - virtual std::shared_ptr deepCopy() const override final - { - return std::make_shared>(*this); - } - - inline void operator()(size_t mlast, DExt last) override final; - inline void operator()(size_t mlast, ExtType last) ; - inline void operator()(size_t mlast = 0) override final; - - DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final; - DExt dExtension() const override final; - - auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; - auto extension() const -> ExtType; - - }; - -} // namespace CNORXZInternal +} /* ========================= * * --- TEMPLATE CODE --- * @@ -583,7 +288,7 @@ namespace CNORXZInternal *****************/ template - For::For(const std::shared_ptr& indPtr, + For::For(const Sptr& indPtr, size_t step, Expr expr) : mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step), mExpr(expr), mExt(mExpr.rootSteps( reinterpret_cast( mIndPtr ))) @@ -680,7 +385,7 @@ namespace CNORXZInternal ******************/ template - PFor::PFor(const std::shared_ptr& indPtr, + PFor::PFor(const Sptr& indPtr, size_t step, Expr expr) : mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step), mExpr(expr), mExt(mExpr.rootSteps( reinterpret_cast( mIndPtr ))) @@ -786,7 +491,7 @@ namespace CNORXZInternal ************************/ template - SingleExpression::SingleExpression(const std::shared_ptr& indPtr, + SingleExpression::SingleExpression(const Sptr& indPtr, Expr expr) : mIndPtr(indPtr.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mExpr(expr), mExt(mExpr.rootSteps( reinterpret_cast( mIndPtr ))) @@ -873,7 +578,7 @@ namespace CNORXZInternal ****************/ template - SubExpr::SubExpr(const std::shared_ptr& indPtr, + SubExpr::SubExpr(const Sptr& indPtr, std::intptr_t siptr, const vector* subset, Expr expr) : mIndPtr(indPtr.get()), mSIPtr(siptr), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), diff --git a/src/include/runtime/COMMENT b/src/include/runtime/COMMENT new file mode 100644 index 0000000..a892d20 --- /dev/null +++ b/src/include/runtime/COMMENT @@ -0,0 +1 @@ +pre-compiled stuff e.g. for python wrappers diff --git a/src/lib/runtime/COMMENT b/src/lib/runtime/COMMENT new file mode 100644 index 0000000..a892d20 --- /dev/null +++ b/src/lib/runtime/COMMENT @@ -0,0 +1 @@ +pre-compiled stuff e.g. for python wrappers diff --git a/src/opt/python/COMMENT b/src/opt/python/COMMENT new file mode 100644 index 0000000..8874687 --- /dev/null +++ b/src/opt/python/COMMENT @@ -0,0 +1 @@ +coming soon: python wrappers