From 216bb3f56e448856c73a21f1839e9d367ced74d9 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 22 Nov 2022 19:11:41 +0100 Subject: [PATCH] aindex: inherit yindex + yrange: specialize RangeCast + corrections in DArray classes + more DArray tests --- src/include/array/aindex.cc.h | 185 +++++------------------------ src/include/array/aindex.h | 42 ++----- src/include/array/darray.cc.h | 22 ++-- src/include/array/darray.h | 9 +- src/include/array/darray_base.cc.h | 41 ++++--- src/include/array/darray_base.h | 3 +- src/include/ranges/yrange.h | 6 + src/lib/ranges/yrange.cc | 8 ++ src/tests/darray_unit_test.cc | 36 +++--- src/tests/test_numbers.h | 5 + 10 files changed, 115 insertions(+), 242 deletions(-) diff --git a/src/include/array/aindex.cc.h b/src/include/array/aindex.cc.h index 3929ee0..13d40c1 100644 --- a/src/include/array/aindex.cc.h +++ b/src/include/array/aindex.cc.h @@ -9,206 +9,83 @@ namespace CNORXZ { template - AIndex::AIndex(const T* data, const RangePtr& range, SizeT pos): - IndexInterface,DType>(pos), - mRangePtr(rangeCast(range)), mIs(mRangePtr->dim()), - mBlockSizes(mRangePtr->dim()), mExternalControl(false), + AIndex::AIndex(const T* data, const RangePtr& range, SizeT lexpos) : + YIndex(range, lexpos), mCData(data) - { - assert(0); - // init ...!!! - } - - template - AIndex& AIndex::operator=(SizeT pos) - { - IB::mPos = pos; - assert(0); - // sub inds... (LAZY!!!) !!! - return *this; - } + {} template - AIndex& AIndex::operator++() - { - if(mExternalControl) this->sync(); - assert(0); - // increment sub inds (LAZY!!!) !!! - ++IB::mPos; - return *this; - } - - template - AIndex& AIndex::operator--() - { - if(mExternalControl) this->sync(); - assert(0); - // decrement sub inds (LAZY!!!) !!! - --IB::mPos; - return *this; - } + AIndex::AIndex(const T* data, const YIndex& yindex) : + YIndex(yindex), + mCData(data) + {} template AIndex AIndex::operator+(Int n) const { - assert(0); - // sub inds !!! - return AIndex(mCData, mRangePtr, IB::mPos + n); + AIndex o = *this; + o += n; + return o; } - + template AIndex AIndex::operator-(Int n) const { - assert(0); - // sub inds !!! - return AIndex(mCData, mRangePtr, IB::mPos - n); - } - - template - AIndex& AIndex::operator+=(Int n) - { - assert(0); - // sub inds !!! - IB::mPos += n; - return *this; - } - - template - AIndex& AIndex::operator-=(Int n) - { - assert(0); - // sub inds !!! - IB::mPos -= n; - return *this; + AIndex o = *this; + o -= n; + return o; } template const T& AIndex::operator*() const { - assert(0); - // sub inds !!! return mCData[IB::mPos]; } template const T* AIndex::operator->() const { - assert(0); - // sub inds !!! - return mCData+IB::mPos; - } - - template - int AIndex::pp(PtrId idxPtrNum) - { - assert(0); - // sub inds !!! - return 0; + return mCData + IB::mPos; } template - int AIndex::mm(PtrId idxPtrNum) - { - assert(0); - // sub inds !!! - return 0; - } + BIndex::BIndex(T* data, const RangePtr& range, SizeT lexpos) : + AIndex(data, range, lexpos), + mData(data) + {} template - SizeT AIndex::dim() const - { - return mIs.size(); - } - - template - RangePtr AIndex::range() const - { - return mRangePtr; - } - - template - SizeT AIndex::getStepSize(SizeT n) const - { - assert(0); - // sub inds !!! - return 0; - } - - template - String AIndex::stringMeta() const - { - String out = "["; - auto it = mIs.begin(); - for(; it != mIs.end()-1; ++it){ - out += (*it)->stringMeta() + ","; - } - out += (*it)->stringMeta() + "]"; - return out; - } - - template - DType AIndex::meta() const - { - //this->sync(); - Vector v(mIs.size()); - std::transform(mIs.begin(), mIs.end(), v.begin(), [](auto& x) { return x->meta(); }); - return DType(v); - } - - template - AIndex& AIndex::at(const DType& meta) - { - auto& v = std::any_cast&>(meta.get()); - assert(v.size() == mIs.size()); - for(SizeT i = 0; i != mIs.size(); ++i){ - mIs[i]->at(v[i]); - } - return *this; - } - /* - template - DExpr AIndex::ifor(SizeT step, DExpr ex) const - { - return mI->ifor(step, ex); - } - - template - DExpr AIndex::iforh(SizeT step, DExpr ex) const - { - return mI->iforh(step, ex); - } - */ - - template - BIndex::BIndex(T* data, const RangePtr& range, SizeT pos) : - AIndex(data, range, pos), mData(data) {} - - template - BIndex::BIndex(T* data, const AIndex& ai, SizeT pos) : - AIndex(data, ai.range(), pos), mData(data) {} + BIndex::BIndex(T* data, const AIndex& ai) : + AIndex(data, ai.range(), ai.lex()), + mData(data) + {} template BIndex BIndex::operator+(Int n) const { - return BIndex(mData, IB::mRangePtr, IB::mPos + n); + BIndex o = *this; + o += n; + return o; } template BIndex BIndex::operator-(Int n) const { - return BIndex(mData, IB::mRangePtr, IB::mPos - n); + BIndex o = *this; + o -= n; + return o; } template T& BIndex::operator*() { - return mData[AI::mPos]; + return mData[IB::mPos]; } template T* BIndex::operator->() { - return mData+AI::mPos; + return mData + IB::mPos; } } diff --git a/src/include/array/aindex.h b/src/include/array/aindex.h index 1fde22c..f4d94b0 100644 --- a/src/include/array/aindex.h +++ b/src/include/array/aindex.h @@ -10,51 +10,25 @@ namespace CNORXZ { - // rename: AIndex (A = Array) - // implementation similar to YIndex + // AIndex (A = Array) template - class AIndex : public IndexInterface,DType> + class AIndex : public YIndex { public: - typedef IndexInterface,DType> IB; + typedef YIndex::IB IB; + using YIndex::operator=; DEFAULT_MEMBERS(AIndex); - AIndex(const T* data, const RangePtr& range, SizeT pos = 0); + AIndex(const T* data, const RangePtr& range, SizeT lexpos = 0); + AIndex(const T* data, const YIndex& yindex); - AIndex& sync(); // recalculate 'IB::mPos' when externalControl == true - AIndex& operator()(const Vector& inds); // control via external indice - AIndex& operator()(); // -> sync; just to shorten the code - - AIndex& operator=(SizeT pos); - AIndex& operator++(); - AIndex& operator--(); AIndex operator+(Int n) const; AIndex operator-(Int n) const; - AIndex& operator+=(Int n); - AIndex& operator-=(Int n); - + const T& operator*() const; const T* operator->() const; - Int pp(PtrId idxPtrNum); - Int mm(PtrId idxPtrNum); - - SizeT dim() const; - RangePtr range() const; - SizeT getStepSize(SizeT n) const; - - String stringMeta() const; - DType meta() const; - AIndex& at(const DType& meta); - - //DExpr ifor(SizeT step, DExpr ex) const; - //DExpr iforh(SizeT step, DExpr ex) const; - protected: - Sptr mRangePtr; - Vector mIs; - Vector mBlockSizes; // dim() elements only!!! - bool mExternalControl = false; const T* mCData = nullptr; }; @@ -69,7 +43,7 @@ namespace CNORXZ DEFAULT_MEMBERS(BIndex); BIndex(T* data, const RangePtr& range, SizeT pos = 0); - BIndex(T* data, const AIndex& cci, SizeT pos = 0); + BIndex(T* data, const AIndex& cci); BIndex operator+(Int n) const; BIndex operator-(Int n) const; diff --git a/src/include/array/darray.cc.h b/src/include/array/darray.cc.h index a77fae9..54af068 100644 --- a/src/include/array/darray.cc.h +++ b/src/include/array/darray.cc.h @@ -11,16 +11,26 @@ namespace CNORXZ ***************/ template - DArray::DArray(const RangePtr& range) : MDArrayBase(range), mCont(range->size()) {} + DArray::DArray(const RangePtr& range) : + MDArrayBase(range), mCont(range->size()) + {} template DArray::DArray(const RangePtr& range, const Vector& vec) : - MDArrayBase(range), mCont(vec) {} + MDArrayBase(range), mCont(vec) + {} template DArray::DArray(const RangePtr& range, Vector&& vec) : - MDArrayBase(range), mCont(vec) {} + MDArrayBase(range), mCont(vec) + {} + template + SizeT DArray::pmax() const + { + return mCont.size(); + } + template const T* DArray::data() const { @@ -36,15 +46,13 @@ namespace CNORXZ template typename DArray::const_iterator DArray::cbegin() const { - assert(0); - return const_iterator(); + return const_iterator(mCont.data(), AB::mRange); } template typename DArray::const_iterator DArray::cend() const { - assert(0); - return const_iterator(); + return const_iterator(mCont.data(), AB::mRange, mCont.size()); } template diff --git a/src/include/array/darray.h b/src/include/array/darray.h index e6e71f1..10ba083 100644 --- a/src/include/array/darray.h +++ b/src/include/array/darray.h @@ -11,29 +11,26 @@ namespace CNORXZ class DArray : public MDArrayBase { public: - typedef DArrayBase AB; typedef typename AB::const_iterator const_iterator; using DArrayBase::operator[]; using MDArrayBase::operator[]; - private: - Vector mCont; - - public: - DEFAULT_MEMBERS(DArray); DArray(const RangePtr& range); DArray(const RangePtr& range, const Vector& vec); DArray(const RangePtr& range, Vector&& vec); + virtual SizeT pmax() const override; virtual const T* data() const override; virtual T* data() override; virtual const_iterator cbegin() const override; virtual const_iterator cend() const override; virtual bool isView() const override; + private: + Vector mCont; }; } diff --git a/src/include/array/darray_base.cc.h b/src/include/array/darray_base.cc.h index a64e8df..5a390eb 100644 --- a/src/include/array/darray_base.cc.h +++ b/src/include/array/darray_base.cc.h @@ -6,25 +6,31 @@ namespace CNORXZ { - /**************** - * DArrayBase * - ***************/ + /****************** + * DArrayBase * + ******************/ template - DArrayBase::DArrayBase(const RangePtr& range) : mRange(range), mInit(true) {} + DArrayBase::DArrayBase(const RangePtr& range) : + mRange(rangeCast(range)) + {} template template const T& DArrayBase::operator[](const IndexInterface& i) const { - return *(this->begin()+i); + auto ai = this->begin() + i.lex(); + return *ai; } template template const T& DArrayBase::at(const IndexInterface& i) const { - return *this->begin().plus(i); + CXZ_ASSERT(i.lex() < this->size(), "index out of range"); + // check further compatibility of index/range format!!! + auto ai = this->begin() + i.lex(); + return *ai; } template @@ -51,11 +57,6 @@ namespace CNORXZ return this->cend(); } - template - bool DArrayBase::isInit() const - { - return mInit; - } /* template template @@ -65,25 +66,31 @@ namespace CNORXZ } */ - /***************** - * MDArrayBase * - ****************/ + /******************* + * MDArrayBase * + *******************/ template - MDArrayBase::MDArrayBase(const RangePtr& range) : DArrayBase(range) {} + MDArrayBase::MDArrayBase(const RangePtr& range) : + DArrayBase(range) + {} template template T& MDArrayBase::operator[](const IndexInterface& i) { - return *(this->begin()+i); + auto ai = this->begin() + i.lex(); + return *ai; } template template T& MDArrayBase::at(const IndexInterface& i) { - return *this->begin().plus(i); + CXZ_ASSERT(i.pos() < this->pmax(), "index out of range"); + // check further compatibility of index/range format!!! + auto ai = this->begin() + i.lex(); + return *ai; } template diff --git a/src/include/array/darray_base.h b/src/include/array/darray_base.h index c7491bf..bf6ca02 100644 --- a/src/include/array/darray_base.h +++ b/src/include/array/darray_base.h @@ -21,7 +21,6 @@ namespace CNORXZ protected: RangePtr mRange; - bool mInit = false; public: @@ -40,6 +39,7 @@ namespace CNORXZ Sptr> sl(const IndexInterface& i) const; virtual const T* data() const = 0; + virtual SizeT pmax() const = 0; // max allocated postion of data() (exclusive!) virtual SizeT size() const; virtual RangePtr range() const; @@ -49,7 +49,6 @@ namespace CNORXZ virtual const_iterator cend() const = 0; virtual bool isView() const = 0; - virtual bool isInit() const; //template //ConstOperationRoot operator()(const IndexPtr& i) const; diff --git a/src/include/ranges/yrange.h b/src/include/ranges/yrange.h index 61599dc..4a235f2 100644 --- a/src/include/ranges/yrange.h +++ b/src/include/ranges/yrange.h @@ -113,6 +113,12 @@ namespace CNORXZ Vector mRVec; }; + + template <> + struct RangeCast + { + static Sptr func(const RangePtr& r); + }; } #endif diff --git a/src/lib/ranges/yrange.cc b/src/lib/ranges/yrange.cc index 1000f71..d23b7ef 100644 --- a/src/lib/ranges/yrange.cc +++ b/src/lib/ranges/yrange.cc @@ -375,4 +375,12 @@ namespace CNORXZ YRange::YRange(Vector&& rvec) : mRVec(std::forward>(rvec)) {} + /******************* + * Range Casts * + *******************/ + + Sptr RangeCast::func(const RangePtr& r) + { + return std::dynamic_pointer_cast( YRangeFactory({r}).create() ); + } } diff --git a/src/tests/darray_unit_test.cc b/src/tests/darray_unit_test.cc index f030153..cf31d54 100644 --- a/src/tests/darray_unit_test.cc +++ b/src/tests/darray_unit_test.cc @@ -6,9 +6,10 @@ #include "gtest/gtest.h" #include "cnorxz.h" +#include "test_numbers.h" -namespace { - +namespace +{ using namespace CNORXZ; class DA_1D_Test : public ::testing::Test @@ -27,29 +28,20 @@ namespace { TEST_F(DA_1D_Test, Basics) { - DArray a(mCR1); + DArray a(mCR1, ::CNORXZ::Test::Numbers::get(0,mSize)); + auto crx = std::dynamic_pointer_cast(mCR1); EXPECT_EQ(a.size(), mSize); - } - /* - TEST_F(DA_1D_Test, ReFormat) - { - swapFactory( rfbptr, { 'a', 'c', 'e', 'g', 'i' } ); - std::shared_ptr sr2 = std::dynamic_pointer_cast( rfbptr->create() ); - Array ma(srptr, vv); - auto i = ma.cbegin(); - EXPECT_EQ( ma[ i.at('x') ], 3.141); + auto ei = crx->end(); + EXPECT_EQ(ei.lex(), mSize); + for(auto i = crx->begin(); i != ei; ++i){ + EXPECT_EQ(i.lex(), i.pos()); + EXPECT_EQ(a[i], a.data()[i.lex()]); + EXPECT_EQ(a.at(i), a.data()[i.lex()]); + } + EXPECT_THROW({a.at(ei);}, std::runtime_error); + } - auto ma2 = ma.format( sr2 ); - auto j = ma2.cbegin(); - - EXPECT_EQ( ma[ j.at('a') ], 3.141); - EXPECT_EQ( ma[ j.at('c') ], 2.718); - EXPECT_EQ( ma[ j.at('e') ], 1.618); - EXPECT_EQ( ma[ j.at('g') ], 0.693); - EXPECT_EQ( ma[ j.at('i') ], 0.577); - } - */ } // end namespace int main(int argc, char** argv) diff --git a/src/tests/test_numbers.h b/src/tests/test_numbers.h index 9d7d5ec..2a66357 100644 --- a/src/tests/test_numbers.h +++ b/src/tests/test_numbers.h @@ -1,4 +1,7 @@ +#ifndef __test_numbers_h__ +#define __test_numbers_h__ + #include //#include "base/types.h" //#include "base/assert.h" @@ -50,3 +53,5 @@ namespace CNORXZ }; } } + +#endif