aindex: inherit yindex + yrange: specialize RangeCast + corrections in DArray classes + more DArray tests

This commit is contained in:
Christian Zimmermann 2022-11-22 19:11:41 +01:00
parent a3d3d99c8d
commit 216bb3f56e
10 changed files with 115 additions and 242 deletions

View file

@ -9,206 +9,83 @@ namespace CNORXZ
{ {
template <typename T> template <typename T>
AIndex<T>::AIndex(const T* data, const RangePtr& range, SizeT pos): AIndex<T>::AIndex(const T* data, const RangePtr& range, SizeT lexpos) :
IndexInterface<AIndex<T>,DType>(pos), YIndex(range, lexpos),
mRangePtr(rangeCast<YRange>(range)), mIs(mRangePtr->dim()),
mBlockSizes(mRangePtr->dim()), mExternalControl(false),
mCData(data) mCData(data)
{ {}
assert(0);
// init ...!!!
}
template <typename T> template <typename T>
AIndex<T>& AIndex<T>::operator=(SizeT pos) AIndex<T>::AIndex(const T* data, const YIndex& yindex) :
{ YIndex(yindex),
IB::mPos = pos; mCData(data)
assert(0); {}
// sub inds... (LAZY!!!) !!!
return *this;
}
template <typename T>
AIndex<T>& AIndex<T>::operator++()
{
if(mExternalControl) this->sync();
assert(0);
// increment sub inds (LAZY!!!) !!!
++IB::mPos;
return *this;
}
template <typename T>
AIndex<T>& AIndex<T>::operator--()
{
if(mExternalControl) this->sync();
assert(0);
// decrement sub inds (LAZY!!!) !!!
--IB::mPos;
return *this;
}
template <typename T> template <typename T>
AIndex<T> AIndex<T>::operator+(Int n) const AIndex<T> AIndex<T>::operator+(Int n) const
{ {
assert(0); AIndex<T> o = *this;
// sub inds !!! o += n;
return AIndex<T>(mCData, mRangePtr, IB::mPos + n); return o;
} }
template <typename T> template <typename T>
AIndex<T> AIndex<T>::operator-(Int n) const AIndex<T> AIndex<T>::operator-(Int n) const
{ {
assert(0); AIndex<T> o = *this;
// sub inds !!! o -= n;
return AIndex<T>(mCData, mRangePtr, IB::mPos - n); return o;
}
template <typename T>
AIndex<T>& AIndex<T>::operator+=(Int n)
{
assert(0);
// sub inds !!!
IB::mPos += n;
return *this;
}
template <typename T>
AIndex<T>& AIndex<T>::operator-=(Int n)
{
assert(0);
// sub inds !!!
IB::mPos -= n;
return *this;
} }
template <typename T> template <typename T>
const T& AIndex<T>::operator*() const const T& AIndex<T>::operator*() const
{ {
assert(0);
// sub inds !!!
return mCData[IB::mPos]; return mCData[IB::mPos];
} }
template <typename T> template <typename T>
const T* AIndex<T>::operator->() const const T* AIndex<T>::operator->() const
{ {
assert(0); return mCData + IB::mPos;
// sub inds !!!
return mCData+IB::mPos;
} }
template <typename T> template <typename T>
int AIndex<T>::pp(PtrId idxPtrNum) BIndex<T>::BIndex(T* data, const RangePtr& range, SizeT lexpos) :
{ AIndex<T>(data, range, lexpos),
assert(0); mData(data)
// sub inds !!! {}
return 0;
}
template <typename T> template <typename T>
int AIndex<T>::mm(PtrId idxPtrNum) BIndex<T>::BIndex(T* data, const AIndex<T>& ai) :
{ AIndex<T>(data, ai.range(), ai.lex()),
assert(0); mData(data)
// sub inds !!! {}
return 0;
}
template <typename T>
SizeT AIndex<T>::dim() const
{
return mIs.size();
}
template <typename T>
RangePtr AIndex<T>::range() const
{
return mRangePtr;
}
template <typename T>
SizeT AIndex<T>::getStepSize(SizeT n) const
{
assert(0);
// sub inds !!!
return 0;
}
template <typename T>
String AIndex<T>::stringMeta() const
{
String out = "[";
auto it = mIs.begin();
for(; it != mIs.end()-1; ++it){
out += (*it)->stringMeta() + ",";
}
out += (*it)->stringMeta() + "]";
return out;
}
template <typename T>
DType AIndex<T>::meta() const
{
//this->sync();
Vector<DType> v(mIs.size());
std::transform(mIs.begin(), mIs.end(), v.begin(), [](auto& x) { return x->meta(); });
return DType(v);
}
template <typename T>
AIndex<T>& AIndex<T>::at(const DType& meta)
{
auto& v = std::any_cast<const Vector<DType>&>(meta.get());
assert(v.size() == mIs.size());
for(SizeT i = 0; i != mIs.size(); ++i){
mIs[i]->at(v[i]);
}
return *this;
}
/*
template <typename T>
DExpr AIndex<T>::ifor(SizeT step, DExpr ex) const
{
return mI->ifor(step, ex);
}
template <typename T>
DExpr AIndex<T>::iforh(SizeT step, DExpr ex) const
{
return mI->iforh(step, ex);
}
*/
template <typename T>
BIndex<T>::BIndex(T* data, const RangePtr& range, SizeT pos) :
AIndex<T>(data, range, pos), mData(data) {}
template <typename T>
BIndex<T>::BIndex(T* data, const AIndex<T>& ai, SizeT pos) :
AIndex<T>(data, ai.range(), pos), mData(data) {}
template <typename T> template <typename T>
BIndex<T> BIndex<T>::operator+(Int n) const BIndex<T> BIndex<T>::operator+(Int n) const
{ {
return BIndex<T>(mData, IB::mRangePtr, IB::mPos + n); BIndex<T> o = *this;
o += n;
return o;
} }
template <typename T> template <typename T>
BIndex<T> BIndex<T>::operator-(Int n) const BIndex<T> BIndex<T>::operator-(Int n) const
{ {
return BIndex<T>(mData, IB::mRangePtr, IB::mPos - n); BIndex<T> o = *this;
o -= n;
return o;
} }
template <typename T> template <typename T>
T& BIndex<T>::operator*() T& BIndex<T>::operator*()
{ {
return mData[AI::mPos]; return mData[IB::mPos];
} }
template <typename T> template <typename T>
T* BIndex<T>::operator->() T* BIndex<T>::operator->()
{ {
return mData+AI::mPos; return mData + IB::mPos;
} }
} }

View file

@ -10,51 +10,25 @@
namespace CNORXZ namespace CNORXZ
{ {
// rename: AIndex (A = Array) // AIndex (A = Array)
// implementation similar to YIndex
template <typename T> template <typename T>
class AIndex : public IndexInterface<AIndex<T>,DType> class AIndex : public YIndex
{ {
public: public:
typedef IndexInterface<AIndex<T>,DType> IB; typedef YIndex::IB IB;
using YIndex::operator=;
DEFAULT_MEMBERS(AIndex); 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<XIndexPtr>& 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) const; AIndex operator-(Int n) const;
AIndex& operator+=(Int n);
AIndex& operator-=(Int n);
const T& operator*() const; const T& operator*() const;
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: protected:
Sptr<YRange> mRangePtr;
Vector<XIndexPtr> mIs;
Vector<SizeT> mBlockSizes; // dim() elements only!!!
bool mExternalControl = false;
const T* mCData = nullptr; const T* mCData = nullptr;
}; };
@ -69,7 +43,7 @@ namespace CNORXZ
DEFAULT_MEMBERS(BIndex); DEFAULT_MEMBERS(BIndex);
BIndex(T* data, const RangePtr& range, SizeT pos = 0); BIndex(T* data, const RangePtr& range, SizeT pos = 0);
BIndex(T* data, const AIndex<T>& cci, SizeT pos = 0); BIndex(T* data, const AIndex<T>& cci);
BIndex operator+(Int n) const; BIndex operator+(Int n) const;
BIndex operator-(Int n) const; BIndex operator-(Int n) const;

View file

@ -11,15 +11,25 @@ namespace CNORXZ
***************/ ***************/
template <typename T> template <typename T>
DArray<T>::DArray(const RangePtr& range) : MDArrayBase<T>(range), mCont(range->size()) {} DArray<T>::DArray(const RangePtr& range) :
MDArrayBase<T>(range), mCont(range->size())
{}
template <typename T> template <typename T>
DArray<T>::DArray(const RangePtr& range, const Vector<T>& vec) : DArray<T>::DArray(const RangePtr& range, const Vector<T>& vec) :
MDArrayBase<T>(range), mCont(vec) {} MDArrayBase<T>(range), mCont(vec)
{}
template <typename T> template <typename T>
DArray<T>::DArray(const RangePtr& range, Vector<T>&& vec) : DArray<T>::DArray(const RangePtr& range, Vector<T>&& vec) :
MDArrayBase<T>(range), mCont(vec) {} MDArrayBase<T>(range), mCont(vec)
{}
template <typename T>
SizeT DArray<T>::pmax() const
{
return mCont.size();
}
template <typename T> template <typename T>
const T* DArray<T>::data() const const T* DArray<T>::data() const
@ -36,15 +46,13 @@ namespace CNORXZ
template <typename T> template <typename T>
typename DArray<T>::const_iterator DArray<T>::cbegin() const typename DArray<T>::const_iterator DArray<T>::cbegin() const
{ {
assert(0); return const_iterator(mCont.data(), AB::mRange);
return const_iterator();
} }
template <typename T> template <typename T>
typename DArray<T>::const_iterator DArray<T>::cend() const typename DArray<T>::const_iterator DArray<T>::cend() const
{ {
assert(0); return const_iterator(mCont.data(), AB::mRange, mCont.size());
return const_iterator();
} }
template <typename T> template <typename T>

View file

@ -11,29 +11,26 @@ namespace CNORXZ
class DArray : public MDArrayBase<T> class DArray : public MDArrayBase<T>
{ {
public: public:
typedef DArrayBase<T> AB; typedef DArrayBase<T> AB;
typedef typename AB::const_iterator const_iterator; typedef typename AB::const_iterator const_iterator;
using DArrayBase<T>::operator[]; using DArrayBase<T>::operator[];
using MDArrayBase<T>::operator[]; using MDArrayBase<T>::operator[];
private:
Vector<T> mCont;
public:
DEFAULT_MEMBERS(DArray); DEFAULT_MEMBERS(DArray);
DArray(const RangePtr& range); DArray(const RangePtr& range);
DArray(const RangePtr& range, const Vector<T>& vec); DArray(const RangePtr& range, const Vector<T>& vec);
DArray(const RangePtr& range, Vector<T>&& vec); DArray(const RangePtr& range, Vector<T>&& vec);
virtual SizeT pmax() const override;
virtual const T* data() const override; virtual const T* data() const override;
virtual T* data() override; virtual T* data() override;
virtual const_iterator cbegin() const override; virtual const_iterator cbegin() const override;
virtual const_iterator cend() const override; virtual const_iterator cend() const override;
virtual bool isView() const override; virtual bool isView() const override;
private:
Vector<T> mCont;
}; };
} }

View file

@ -6,25 +6,31 @@
namespace CNORXZ namespace CNORXZ
{ {
/**************** /******************
* DArrayBase * * DArrayBase *
***************/ ******************/
template <typename T> template <typename T>
DArrayBase<T>::DArrayBase(const RangePtr& range) : mRange(range), mInit(true) {} DArrayBase<T>::DArrayBase(const RangePtr& range) :
mRange(rangeCast<YRange>(range))
{}
template <typename T> template <typename T>
template <typename I, typename M> template <typename I, typename M>
const T& DArrayBase<T>::operator[](const IndexInterface<I,M>& i) const const T& DArrayBase<T>::operator[](const IndexInterface<I,M>& i) const
{ {
return *(this->begin()+i); auto ai = this->begin() + i.lex();
return *ai;
} }
template <typename T> template <typename T>
template <typename I, typename M> template <typename I, typename M>
const T& DArrayBase<T>::at(const IndexInterface<I,M>& i) const const T& DArrayBase<T>::at(const IndexInterface<I,M>& 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 <typename T> template <typename T>
@ -51,11 +57,6 @@ namespace CNORXZ
return this->cend(); return this->cend();
} }
template <typename T>
bool DArrayBase<T>::isInit() const
{
return mInit;
}
/* /*
template <typename T> template <typename T>
template <typename I, typename M> template <typename I, typename M>
@ -65,25 +66,31 @@ namespace CNORXZ
} }
*/ */
/***************** /*******************
* MDArrayBase * * MDArrayBase *
****************/ *******************/
template <typename T> template <typename T>
MDArrayBase<T>::MDArrayBase(const RangePtr& range) : DArrayBase<T>(range) {} MDArrayBase<T>::MDArrayBase(const RangePtr& range) :
DArrayBase<T>(range)
{}
template <typename T> template <typename T>
template <typename I, typename M> template <typename I, typename M>
T& MDArrayBase<T>::operator[](const IndexInterface<I,M>& i) T& MDArrayBase<T>::operator[](const IndexInterface<I,M>& i)
{ {
return *(this->begin()+i); auto ai = this->begin() + i.lex();
return *ai;
} }
template <typename T> template <typename T>
template <typename I, typename M> template <typename I, typename M>
T& MDArrayBase<T>::at(const IndexInterface<I,M>& i) T& MDArrayBase<T>::at(const IndexInterface<I,M>& 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 <typename T> template <typename T>

View file

@ -21,7 +21,6 @@ namespace CNORXZ
protected: protected:
RangePtr mRange; RangePtr mRange;
bool mInit = false;
public: public:
@ -40,6 +39,7 @@ namespace CNORXZ
Sptr<DArrayBase<T>> sl(const IndexInterface<I,M>& i) const; Sptr<DArrayBase<T>> sl(const IndexInterface<I,M>& i) const;
virtual const T* data() const = 0; virtual const T* data() const = 0;
virtual SizeT pmax() const = 0; // max allocated postion of data() (exclusive!)
virtual SizeT size() const; virtual SizeT size() const;
virtual RangePtr range() const; virtual RangePtr range() const;
@ -49,7 +49,6 @@ namespace CNORXZ
virtual const_iterator cend() const = 0; virtual const_iterator cend() const = 0;
virtual bool isView() const = 0; virtual bool isView() const = 0;
virtual bool isInit() const;
//template <typename I, typename M> //template <typename I, typename M>
//ConstOperationRoot<T,I> operator()(const IndexPtr<I,M>& i) const; //ConstOperationRoot<T,I> operator()(const IndexPtr<I,M>& i) const;

View file

@ -113,6 +113,12 @@ namespace CNORXZ
Vector<RangePtr> mRVec; Vector<RangePtr> mRVec;
}; };
template <>
struct RangeCast<YRange>
{
static Sptr<YRange> func(const RangePtr& r);
};
} }
#endif #endif

View file

@ -375,4 +375,12 @@ namespace CNORXZ
YRange::YRange(Vector<RangePtr>&& rvec) : mRVec(std::forward<Vector<RangePtr>>(rvec)) {} YRange::YRange(Vector<RangePtr>&& rvec) : mRVec(std::forward<Vector<RangePtr>>(rvec)) {}
/*******************
* Range Casts *
*******************/
Sptr<YRange> RangeCast<YRange>::func(const RangePtr& r)
{
return std::dynamic_pointer_cast<YRange>( YRangeFactory({r}).create() );
}
} }

View file

@ -6,9 +6,10 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "cnorxz.h" #include "cnorxz.h"
#include "test_numbers.h"
namespace { namespace
{
using namespace CNORXZ; using namespace CNORXZ;
class DA_1D_Test : public ::testing::Test class DA_1D_Test : public ::testing::Test
@ -27,29 +28,20 @@ namespace {
TEST_F(DA_1D_Test, Basics) TEST_F(DA_1D_Test, Basics)
{ {
DArray<double> a(mCR1); DArray<Double> a(mCR1, ::CNORXZ::Test::Numbers::get(0,mSize));
auto crx = std::dynamic_pointer_cast<CRange>(mCR1);
EXPECT_EQ(a.size(), mSize); EXPECT_EQ(a.size(), mSize);
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);
} }
/*
TEST_F(DA_1D_Test, ReFormat)
{
swapFactory<SRF>( rfbptr, { 'a', 'c', 'e', 'g', 'i' } );
std::shared_ptr<SRange> sr2 = std::dynamic_pointer_cast<SRange>( rfbptr->create() );
Array<double,DA_1D_Test::SRange> ma(srptr, vv);
auto i = ma.cbegin();
EXPECT_EQ( ma[ i.at('x') ], 3.141);
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 } // end namespace
int main(int argc, char** argv) int main(int argc, char** argv)

View file

@ -1,4 +1,7 @@
#ifndef __test_numbers_h__
#define __test_numbers_h__
#include <fstream> #include <fstream>
//#include "base/types.h" //#include "base/types.h"
//#include "base/assert.h" //#include "base/assert.h"
@ -50,3 +53,5 @@ namespace CNORXZ
}; };
} }
} }
#endif