diff --git a/src/base_def.h b/src/base_def.h index 56d9d71..efd7184 100644 --- a/src/base_def.h +++ b/src/base_def.h @@ -28,6 +28,18 @@ namespace MultiArrayTools { + template + void checkPtr(const MultiArray& in, bool check = false) + { + static MultiArray const* ptr = nullptr; + if(not check){ + ptr = ∈ + } + else { + assert(ptr == &in); + } + } + /*********************** * Provided Types * ***********************/ @@ -81,9 +93,17 @@ namespace MultiArrayTools template class MultiArrayOperationBase; + // multi_array_operation.h + template + class MutableMultiArrayOperationBase; + // multi_array_operation.h template class MultiArrayOperationRoot; + + // multi_array_operation.h + template + class ConstMultiArrayOperationRoot; // multi_array_operation.h template diff --git a/src/index_base.cc b/src/index_base.cc index eaf240d..66b8f1e 100644 --- a/src/index_base.cc +++ b/src/index_base.cc @@ -83,6 +83,7 @@ namespace MultiArrayTools void IndefinitIndexBase::setPos(size_t pos) { + //CHECK; //assert(not virt()); mPos = pos; if(linked()){ diff --git a/src/multi_array.cc b/src/multi_array.cc index 15b2f50..2021591 100644 --- a/src/multi_array.cc +++ b/src/multi_array.cc @@ -20,7 +20,10 @@ namespace MultiArrayTools template auto MultiArrayBase::begin() const -> decltype(Range().begin()) { - return mRange->begin(); + //VCHECK(not mRange); + auto i = mRange->begin(); + //CHECK; + return i; } template @@ -46,6 +49,7 @@ namespace MultiArrayTools template MultiArrayOperationRoot MultiArrayBase::operator()(const NameType& name, bool master) { + //CHECK; if(master){ return MultiArrayOperationRoot(*this, name); } @@ -54,6 +58,26 @@ namespace MultiArrayTools } } + template + template + ConstMultiArrayOperationRoot MultiArrayBase::operator()(const NameTypes&... str) const + { + return MultiArrayOperationRoot(*this, Name("master", str...)); + } + + template + template + ConstMultiArrayOperationRoot MultiArrayBase::operator()(const NameType& name, bool master) const + { + //CHECK; + if(master){ + return ConstMultiArrayOperationRoot(*this, name); + } + else { + return operator()(name); + } + } + template bool MultiArrayBase::isInit() const { diff --git a/src/multi_array.h b/src/multi_array.h index 6ea374d..25fec77 100644 --- a/src/multi_array.h +++ b/src/multi_array.h @@ -44,6 +44,12 @@ namespace MultiArrayTools template MultiArrayOperationRoot operator()(const NameType& name, bool master); + template + ConstMultiArrayOperationRoot operator()(const NameTypes&... str) const; + + template + ConstMultiArrayOperationRoot operator()(const NameType& name, bool master) const; + virtual bool isInit() const; protected: diff --git a/src/multi_array_operation.cc b/src/multi_array_operation.cc index 21043e3..da34107 100644 --- a/src/multi_array_operation.cc +++ b/src/multi_array_operation.cc @@ -20,6 +20,12 @@ namespace MultiArrayTools { return *mIibPtr; } + + template + void MultiArrayOperationBase::freeIndex() + { + mIibPtr->freeLinked(); + } /********************************* * MultiArrayOperationBase * @@ -28,12 +34,18 @@ namespace MultiArrayTools template void MultiArrayOperationRoot::performAssignment(const MultiArrayOperationBase& in) { + //CHECK; in.linkIndicesTo(MAOB::mIibPtr); + //CHECK; IndexType& iref = dynamic_cast(*MAOB::mIibPtr); + //CHECK; + const size_t endPos = mArrayRef.end().pos(); + std::cout << "assignment: " << endPos << "elements" << std::endl; for(iref = mArrayRef.begin().pos(); iref != mArrayRef.end(); ++iref){ - // build in vectorization later + std::cout << iref.pos() << '\r' << std::flush; get() = in.get(); } + //CHECK; MAOB::mIibPtr->freeLinked(); } @@ -64,7 +76,7 @@ namespace MultiArrayTools MultiArrayOperationRoot:: MultiArrayOperationRoot(MultiArrayBase& ma, const Name& nm) : - MultiArrayOperationBase(), + MutableMultiArrayOperationBase(), mArrayRef(ma), mIndex(mArrayRef.begin()), mNm(nm) @@ -86,6 +98,7 @@ namespace MultiArrayTools MultiArrayOperationRoot& MultiArrayOperationRoot::operator=(MultiArrayOperationRoot& in) { + //CHECK; maketurnSlice(in); if(mArrayRef.isSlice() and not mArrayRef.isInit()){ return makeSlice(in); @@ -99,6 +112,7 @@ namespace MultiArrayTools MultiArrayOperationRoot& MultiArrayOperationRoot::operator=(MultiArrayOperationRoot& in) { + //CHECK; if(mArrayRef.isSlice() and not mArrayRef.isInit()){ return makeSlice(in); } @@ -111,7 +125,9 @@ namespace MultiArrayTools const MultiArrayOperationRoot& MultiArrayOperationRoot::operator=(const MultiArrayOperationRoot& in) { + //CHECK; if(mArrayRef.isSlice() and not mArrayRef.isInit()){ + //CHECK; return makeConstSlice(in); } performAssignment(in); @@ -123,6 +139,7 @@ namespace MultiArrayTools MultiArrayOperationRoot& MultiArrayOperationRoot::operator=(const MultiArrayOperation& in) { + //CHECK; if(mArrayRef.isSlice() and not mArrayRef.isInit()){ // NO SLICE CREATION !!! (total array not initialized!!) // throw ! @@ -132,19 +149,12 @@ namespace MultiArrayTools return *this; } - /* template - template - MultiArrayOperation, MAOps...> - MultiArrayOperationRoot::operator()(Operation& op, const MAOps&... secs) const - { - return MultiArrayOperation, MAOps...>(op, *this, secs...); - }*/ - template template MultiArrayOperation, MAOps...> MultiArrayOperationRoot::operator()(const Operation& op, const MAOps&... secs) const { + //CHECK; return MultiArrayOperation, MAOps...>(op, *this, secs...); } @@ -177,12 +187,14 @@ namespace MultiArrayTools auto MultiArrayOperationRoot::operator/(const MAOp& sec) -> decltype(operator()(std::divides(), sec)) { + //CHECK; return operator()(std::divides(), sec); } template MultiArrayOperationRoot MultiArrayOperationRoot::copyThis() { + //CHECK; return MultiArrayOperationRoot(mArrayRef, mNm); } @@ -215,6 +227,7 @@ namespace MultiArrayTools MultiArrayOperationRoot& MultiArrayOperationRoot::operator/=(const MAOp& sec) { + //CHECK; return (*this) = copyThis() / sec; } @@ -275,6 +288,188 @@ namespace MultiArrayTools mIndex.copyPos(ind); return *this; } + + template + void MultiArrayOperationRoot::freeIndex() + { + MAOB::mIibPtr->freeLinked(); + mIndex = mArrayRef.begin(); + MAOB::mIibPtr = &mIndex; + MAOB::mIibPtr->name(mNm); + } + + /************************************** + * ConstMultiArrayOperationBase * + **************************************/ + + template + template + const ConstMultiArrayOperationRoot& + ConstMultiArrayOperationRoot::makeConstSlice(const ConstMultiArrayOperationRoot& in) const + { + ConstSlice& sl = dynamic_cast&>( mArrayRef ); + sl.set(in.mArrayRef, name(), dynamic_cast( in.index() ), in.name()); + return *this; + } + + + // CONST SLICE !!!!! + + template + ConstMultiArrayOperationRoot:: + ConstMultiArrayOperationRoot(const MultiArrayBase& ma, + const Name& nm) : + MultiArrayOperationBase(), + mArrayRef(ma), + mIndex(mArrayRef.begin()), + mNm(nm) + { + MAOB::mIibPtr = &mIndex; + MAOB::mIibPtr->name(nm); + //mIndex.name(nm); + } + + template + ConstMultiArrayOperationRoot:: + ConstMultiArrayOperationRoot(const MultiArrayOperationRoot& in) : + MultiArrayOperationBase(), + mArrayRef(in.getCont()), + mIndex(mArrayRef.begin()), + mNm(in.name()) + { + MAOB::mIibPtr = &mIndex; + MAOB::mIibPtr->name(mNm); + //mIndex.name(nm); + } + + template + const ConstMultiArrayOperationRoot& + ConstMultiArrayOperationRoot::operator=(const ConstMultiArrayOperationRoot& in) + { + //CHECK; + if(mArrayRef.isSlice() and not mArrayRef.isInit()){ + return makeConstSlice(in); + } + assert(0); + return *this; + } + + template + template + const ConstMultiArrayOperationRoot& + ConstMultiArrayOperationRoot::operator=(const ConstMultiArrayOperationRoot& in) + { + //CHECK; + if(mArrayRef.isSlice() and not mArrayRef.isInit()){ + //CHECK; + return makeConstSlice(in); + } + assert(0); + return *this; + } + + template + template + MultiArrayOperation, MAOps...> + ConstMultiArrayOperationRoot::operator()(const Operation& op, const MAOps&... secs) const + { + //CHECK; + return MultiArrayOperation, MAOps...>(op, *this, secs...); + } + + template + template + auto ConstMultiArrayOperationRoot::operator+(const MAOp& sec) const + -> decltype(operator()(std::plus(), sec)) + { + return operator()(std::plus(), sec); + } + + template + template + auto ConstMultiArrayOperationRoot::operator-(const MAOp& sec) const + -> decltype(operator()(std::minus(), sec)) + { + return operator()(std::minus(), sec); + } + + template + template + auto ConstMultiArrayOperationRoot::operator*(const MAOp& sec) const + -> decltype(operator()(std::multiplies(), sec)) + { + return operator()(std::multiplies(), sec); + } + + template + template + auto ConstMultiArrayOperationRoot::operator/(const MAOp& sec) const + -> decltype(operator()(std::divides(), sec)) + { + //CHECK; + return operator()(std::divides(), sec); + } + + template + ConstMultiArrayOperationRoot ConstMultiArrayOperationRoot::copyThis() const + { + //CHECK; + return ConstMultiArrayOperationRoot(mArrayRef, mNm); + } + + template + const MultiArrayBase& ConstMultiArrayOperationRoot::operator*() const + { + return mArrayRef; + } + + template + MultiArrayBase const* ConstMultiArrayOperationRoot::operator->() const + { + return &mArrayRef; + } + + template + size_t ConstMultiArrayOperationRoot::argNum() const + { + return 1; + } + + template + void ConstMultiArrayOperationRoot::linkIndicesTo(IndefinitIndexBase* target) const + { + MAOB::mIibPtr->linkTo(target); + } + + template + const T& ConstMultiArrayOperationRoot::get() const + { + //return mArrayRef[mIndex]; + return mArrayRef[*dynamic_cast(MAOB::mIibPtr)]; + } + + template + const Name& ConstMultiArrayOperationRoot::name() const + { + return mNm; + } + + template + const ConstMultiArrayOperationRoot& ConstMultiArrayOperationRoot::operator[](const IndexType& ind) const + { + mIndex.copyPos(ind); + return *this; + } + + template + void ConstMultiArrayOperationRoot::freeIndex() + { + MAOB::mIibPtr->freeLinked(); + mIndex = mArrayRef.begin(); + MAOB::mIibPtr = &mIndex; + MAOB::mIibPtr->name(mNm); + } + /***************************** * MultiArrayOperation * @@ -410,14 +605,15 @@ namespace MultiArrayTools { TupleIndicesLinker::linkTupleIndicesTo(mArgs, target); } - + + /* template T& MultiArrayOperation::get() { mVal = OperationCall:: template callOperation(mOp, mArgs); return mVal; - } + }*/ template const T& MultiArrayOperation::get() const diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h index fa3f988..8967ef9 100644 --- a/src/multi_array_operation.h +++ b/src/multi_array_operation.h @@ -17,22 +17,33 @@ namespace MultiArrayTools { public: - MultiArrayOperationBase() = default; + MultiArrayOperationBase() /*{ CHECK; }*/ = default; virtual ~MultiArrayOperationBase(); virtual size_t argNum() const = 0; const IndefinitIndexBase& index() const; virtual void linkIndicesTo(IndefinitIndexBase* target) const = 0; - virtual T& get() = 0; virtual const T& get() const = 0; + virtual void freeIndex(); + protected: IndefinitIndexBase* mIibPtr = nullptr; }; + + template + class MutableMultiArrayOperationBase : public MultiArrayOperationBase + { + public: + + MutableMultiArrayOperationBase() /*{ CHECK; }*/ = default; + virtual T& get() = 0; + }; + template - class MultiArrayOperationRoot : public MultiArrayOperationBase + class MultiArrayOperationRoot : public MutableMultiArrayOperationBase { public: @@ -89,6 +100,7 @@ namespace MultiArrayTools MultiArrayOperationRoot& operator/=(const MAOp& sec); const MultiArrayBase& operator*() const; + //MultiArrayBase& operator*(); MultiArrayBase const* operator->() const; virtual size_t argNum() const override; @@ -104,6 +116,10 @@ namespace MultiArrayTools const Name& name() const; + virtual void freeIndex() override; + + const MultiArrayBase& getCont() const { return mArrayRef; } + template friend class MultiArrayOperationRoot; @@ -121,6 +137,77 @@ namespace MultiArrayTools mutable IndexType mIndex; Name mNm; }; + + template + class ConstMultiArrayOperationRoot : public MultiArrayOperationBase + { + public: + + typedef MultiArrayOperationBase MAOB; + typedef typename Range::IndexType IndexType; + //typedef decltype(MultiArray().begin()) IndexType; + + ConstMultiArrayOperationRoot(const MultiArrayBase& ma, const Name& nm); + ConstMultiArrayOperationRoot(const MultiArrayOperationRoot& in); + + const ConstMultiArrayOperationRoot& operator=(const ConstMultiArrayOperationRoot& in); + + template + const ConstMultiArrayOperationRoot& operator=(const ConstMultiArrayOperationRoot& in); + + //template + //MultiArrayOperation, MAOps...> + //operator()(Operation& op, const MAOps&... secs) const; + + template + MultiArrayOperation, MAOps...> + operator()(const Operation& op, const MAOps&... secs) const; + + template + auto operator+(const MAOp& sec) const -> decltype(operator()(std::plus(), sec)); + + template + auto operator-(const MAOp& sec) const -> decltype(operator()(std::minus(), sec)); + + template + auto operator*(const MAOp& sec) const -> decltype(operator()(std::multiplies(), sec)); + + template + auto operator/(const MAOp& sec) const -> decltype(operator()(std::divides(), sec)); + + ConstMultiArrayOperationRoot copyThis() const; + + const MultiArrayBase& operator*() const; + //MultiArrayBase& operator*(); + MultiArrayBase const* operator->() const; + + virtual size_t argNum() const override; + + // set index -> implement !!!!! + const ConstMultiArrayOperationRoot& operator[](const IndexType& ind) const; + + virtual void linkIndicesTo(IndefinitIndexBase* target) const override; + + virtual const T& get() const override; + + const Name& name() const; + + virtual void freeIndex() override; + + const MultiArrayBase& getCont() const { return mArrayRef; } + + template + friend class MultiArrayOperationRoot; + + protected: + + template + const ConstMultiArrayOperationRoot& makeConstSlice(const ConstMultiArrayOperationRoot& in) const; + + MultiArrayBase const& mArrayRef; + mutable IndexType mIndex; + Name mNm; + }; template class MultiArrayOperation : public MultiArrayOperationBase @@ -157,7 +244,7 @@ namespace MultiArrayTools virtual void linkIndicesTo(IndefinitIndexBase* target) const override; - virtual T& get() override; + //virtual T& get() override; virtual const T& get() const override; protected: diff --git a/src/multi_range.cc b/src/multi_range.cc index 334891b..d89a655 100644 --- a/src/multi_range.cc +++ b/src/multi_range.cc @@ -585,12 +585,15 @@ namespace MultiArrayTools if(name == IIB::mName){ return this; } + for(size_t i = 0; i != sizeof...(Indices); ++i){ IndefinitIndexBase* iibPtr = get(i).getLinked(name); if(iibPtr != nullptr){ + //CHECK; return iibPtr; } } + //CHECK; return nullptr; } diff --git a/src/single_range.cc b/src/single_range.cc index f6b5d71..bbe203c 100644 --- a/src/single_range.cc +++ b/src/single_range.cc @@ -194,7 +194,7 @@ namespace MultiArrayTools #ifdef LORENTZ_DIMENSION return LORENTZ_DIMENSION; #else - return 4; + return 1; // 4 #endif } @@ -327,6 +327,8 @@ namespace MultiArrayTools template IndefinitIndexBase* SingleIndex::getLinked(const std::string& name) { + //VCHECK(name); + //VCHECK(IIB::mName); if(name == IIB::mName){ return this; } diff --git a/src/slice.cc b/src/slice.cc index 7d7ac40..2e17a66 100644 --- a/src/slice.cc +++ b/src/slice.cc @@ -104,4 +104,14 @@ namespace MultiArrayTools //mOwnIdx = i.pos(); return (*mMultiArrayPtr)[ mMAIdx ]; } + + + template + T& ConstSlice::operator[](const typename Range::IndexType& i) + { + assert(0); + mOwnIdx.copyPos(i); + //mOwnIdx = i.pos(); + return x; + } } diff --git a/src/slice.h b/src/slice.h index 09c66f8..a9ff66e 100644 --- a/src/slice.h +++ b/src/slice.h @@ -55,6 +55,7 @@ namespace MultiArrayTools ConstSlice(const Range& range); + virtual T& operator[](const typename Range::IndexType& i) override; virtual const T& operator[](const typename Range::IndexType& i) const override; //Slice& setSlicePos(const Index& slicePos); @@ -71,6 +72,9 @@ namespace MultiArrayTools const Name& MANm); private: + + // !!! + T x = static_cast(0); MultiArrayBase const* mMultiArrayPtr = nullptr; mutable typename Range::IndexType mOwnIdx;