container_index -> a/b index

This commit is contained in:
Christian Zimmermann 2022-09-17 22:08:01 +02:00
parent 3044646b6a
commit 5a71d00863
12 changed files with 320 additions and 227 deletions

View file

@ -0,0 +1,216 @@
#ifndef __cxz_aindex_cc_h__
#define __cxz_aindex_cc_h__
#include "aindex.h"
#include "statics/static_for.h"
namespace CNORXZ
{
template <typename T>
AIndex<T>::AIndex(const T* data, const RangePtr& range, SizeT pos):
IndexInterface<AIndex<T>,DType>(pos),
mRangePtr(rangeCast<YRange>(range)), mIs(mRangePtr->dim()),
mBlockSizes(mRangePtr->dim()), mExternalControl(false),
mCData(data)
{
assert(0);
// init ...!!!
}
template <typename T>
AIndex<T>& AIndex<T>::operator=(SizeT pos)
{
IB::mPos = pos;
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>
AIndex<T> AIndex<T>::operator+(Int n) const
{
assert(0);
// sub inds !!!
return AIndex<T>(mCData, mRangePtr, IB::mPos + n);
}
template <typename T>
AIndex<T> AIndex<T>::operator-(Int n) const
{
assert(0);
// sub inds !!!
return AIndex<T>(mCData, mRangePtr, IB::mPos - n);
}
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>
const T& AIndex<T>::operator*() const
{
assert(0);
// sub inds !!!
return mCData[IB::mPos];
}
template <typename T>
const T* AIndex<T>::operator->() const
{
assert(0);
// sub inds !!!
return mCData+IB::mPos;
}
template <typename T>
int AIndex<T>::pp(PtrId idxPtrNum)
{
assert(0);
// sub inds !!!
return 0;
}
template <typename T>
int AIndex<T>::mm(PtrId idxPtrNum)
{
assert(0);
// 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>
BIndex<T> BIndex<T>::operator+(Int n) const
{
return BIndex<T>(mData, IB::mRangePtr, IB::mPos + n);
}
template <typename T>
BIndex<T> BIndex<T>::operator-(Int n) const
{
return BIndex<T>(mData, IB::mRangePtr, IB::mPos - n);
}
template <typename T>
T& BIndex<T>::operator*()
{
return mData[AI::mPos];
}
template <typename T>
T* BIndex<T>::operator->()
{
return mData+AI::mPos;
}
}
#endif

View file

@ -0,0 +1,87 @@
#ifndef __cxz_aindex_h__
#define __cxz_aindex_h__
#include "ranges/range_base.h"
#include "ranges/index_base.h"
#include "ranges/xfor/xfor.h"
#include "ranges/xindex.h"
#include "ranges/yrange.h"
namespace CNORXZ
{
// rename: AIndex (A = Array)
// implementation similar to YIndex
template <typename T>
class AIndex : public IndexInterface<AIndex<T>,DType>
{
public:
typedef IndexInterface<AIndex<T>,DType> IB;
DEFAULT_MEMBERS(AIndex);
AIndex(const T* data, const RangePtr& range, SizeT pos = 0);
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);
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<YRange> mRangePtr;
Vector<XIndexPtr> mIs;
Vector<SizeT> mBlockSizes; // dim() elements only!!!
bool mExternalControl = false;
const T* mCData = nullptr;
};
// BIndex (because B comes after A...)
template <typename T>
class BIndex : public AIndex<T>
{
public:
typedef AIndex<T> AI;
typedef typename AI::IB IB;
DEFAULT_MEMBERS(BIndex);
BIndex(T* data, const RangePtr& range, SizeT pos = 0);
BIndex(T* data, const AIndex<T>& cci, SizeT pos = 0);
BIndex operator+(Int n) const;
BIndex operator-(Int n) const;
T& operator*();
T* operator->();
private:
T* mData = nullptr;
};
}
#endif

View file

@ -2,5 +2,5 @@
//#include "access.cc.h" //#include "access.cc.h"
#include "darray_base.cc.h" #include "darray_base.cc.h"
#include "darray.cc.h" #include "darray.cc.h"
#include "dcontainer_index.cc.h" #include "aindex.cc.h"
//#include "functional_array.cc.h" //#include "functional_array.cc.h"

View file

@ -2,7 +2,7 @@
//#include "access.h" //#include "access.h"
#include "darray_base.h" #include "darray_base.h"
#include "darray.h" #include "darray.h"
#include "dcontainer_index.h" #include "aindex.h"
//#include "functional_array.h" //#include "functional_array.h"
#include "array.cc.h" #include "array.cc.h"

View file

@ -28,7 +28,7 @@ namespace CNORXZ
} }
template <typename T> template <typename T>
size_t DArrayBase<T>::size() const SizeT DArrayBase<T>::size() const
{ {
return mRange->size(); return mRange->size();
} }

View file

@ -8,7 +8,7 @@
#include <algorithm> #include <algorithm>
#include "base/base.h" #include "base/base.h"
#include "dcontainer_index.h" #include "aindex.h"
//#include "operation/" //#include "operation/"
namespace CNORXZ namespace CNORXZ
@ -17,7 +17,7 @@ namespace CNORXZ
class DArrayBase class DArrayBase
{ {
public: public:
typedef DConstContainerIndex<T> const_iterator; typedef AIndex<T> const_iterator;
protected: protected:
RangePtr mRange; RangePtr mRange;
@ -37,10 +37,10 @@ namespace CNORXZ
const T& at(const IndexInterface<I,M>& i) const; const T& at(const IndexInterface<I,M>& i) const;
template <typename I, typename M> template <typename I, typename M>
DArrayBase 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 size_t size() const; virtual SizeT size() const;
virtual RangePtr range() const; virtual RangePtr range() const;
virtual const_iterator begin() const; virtual const_iterator begin() const;
@ -61,7 +61,7 @@ namespace CNORXZ
public: public:
typedef DArrayBase<T> DAB; typedef DArrayBase<T> DAB;
typedef typename DAB::const_iterator const_iterator; typedef typename DAB::const_iterator const_iterator;
typedef DContainerIndex<T> iterator; typedef BIndex<T> iterator;
using DAB::operator[]; using DAB::operator[];
using DAB::at; using DAB::at;
@ -82,7 +82,7 @@ namespace CNORXZ
T& at(const IndexInterface<I,M>& i); T& at(const IndexInterface<I,M>& i);
template <typename I, typename M> template <typename I, typename M>
std::shared_ptr<DArrayBase<T>> sl(const IndexInterface<I,M>& i); Sptr<MDArrayBase<T>> sl(const IndexInterface<I,M>& i);
virtual T* data() = 0; virtual T* data() = 0;

View file

@ -1,134 +0,0 @@
#ifndef __cxz_dcontainer_index_cc_h__
#define __cxz_dcontainer_index_cc_h__
#include "dcontainer_index.h"
#include "statics/static_for.h"
namespace CNORXZ
{
template <typename T>
DConstContainerIndex<T>::DConstContainerIndex(const T* data, const RangePtr& range):
mI(range->begin()), mCData(data)
{
assert(0);
}
template <typename T>
DConstContainerIndex<T>& DConstContainerIndex<T>::operator=(SizeT pos)
{
(*mI) = pos;
IB::mPos = mI->pos();
return *this;
}
template <typename T>
DConstContainerIndex<T>& DConstContainerIndex<T>::operator++()
{
++(*mI);
IB::mPos = mI->pos();
return *this;
}
template <typename T>
DConstContainerIndex<T>& DConstContainerIndex<T>::operator--()
{
--(*mI);
IB::mPos = mI->pos();
return *this;
}
template <typename T>
int DConstContainerIndex<T>::pp(PtrId idxPtrNum)
{
return mI->pp(idxPtrNum);
}
template <typename T>
int DConstContainerIndex<T>::mm(PtrId idxPtrNum)
{
return mI->mm(idxPtrNum);
}
template <typename T>
SizeT DConstContainerIndex<T>::dim() const
{
return mI->dim();
}
template <typename T>
SizeT DConstContainerIndex<T>::getStepSize(SizeT n) const
{
return mI->getStepSize(n); // dim() elements only!!!
}
template <typename T>
String DConstContainerIndex<T>::stringMeta() const
{
return mI->stringMeta();
}
template <typename T>
DType DConstContainerIndex<T>::meta() const
{
return mI->meta();
}
template <typename T>
DConstContainerIndex<T>& DConstContainerIndex<T>::at(const DType& meta)
{
mI->at(meta);
IB::mPos = mI->pos();
return *this;
}
/*
template <typename T>
DExpr DConstContainerIndex<T>::ifor(SizeT step, DExpr ex) const
{
return mI->ifor(step, ex);
}
template <typename T>
DExpr DConstContainerIndex<T>::iforh(SizeT step, DExpr ex) const
{
return mI->iforh(step, ex);
}
*/
template <typename T>
const T& DConstContainerIndex<T>::operator*() const
{
//this->sync();
return mCData[mI->pos()];
}
template <typename T>
const T* DConstContainerIndex<T>::operator->() const
{
//this->sync();
return mCData+mI->pos();
}
template <typename T>
DContainerIndex<T>::DContainerIndex(T* data, const RangePtr& range) :
DConstContainerIndex<T>(range), mData(data) {}
template <typename T>
DContainerIndex<T>::DContainerIndex(T* data, const DConstContainerIndex<T>& cci) :
DConstContainerIndex<T>(data, cci.range()), mData(data) {}
template <typename T>
T& DContainerIndex<T>::operator*()
{
return mData[CCI::mI->pos()];
}
template <typename T>
T* DContainerIndex<T>::operator->()
{
return mData+CCI::mI->pos();
}
}
#endif

View file

@ -1,74 +0,0 @@
#ifndef __cxz_dcontainer_index_h__
#define __cxz_dcontainer_index_h__
#include "ranges/range_base.h"
#include "ranges/index_base.h"
#include "ranges/xfor/xfor.h"
#include "ranges/xindex.h"
#include "ranges/yrange.h"
namespace CNORXZ
{
// rename: AIndex (A = Array)
template <typename T>
class DConstContainerIndex : public IndexInterface<DConstContainerIndex<T>,DType>
{
public:
typedef IndexInterface<DConstContainerIndex<T>,DType> IB;
protected:
YIndexPtr mI;
const T* mCData = nullptr;
public:
DEFAULT_MEMBERS(DConstContainerIndex);
DConstContainerIndex(const T* data, const RangePtr& range);
DConstContainerIndex& operator=(SizeT pos);
DConstContainerIndex& operator++();
DConstContainerIndex& operator--();
int pp(PtrId idxPtrNum);
int mm(PtrId idxPtrNum);
SizeT dim() const;
SizeT getStepSize(SizeT n) const;
String stringMeta() const;
DType meta() const;
DType metaPtr() const;
DConstContainerIndex& at(const DType& meta);
//DExpr ifor(SizeT step, DExpr ex) const;
//DExpr iforh(SizeT step, DExpr ex) const;
const T& operator*() const;
const T* operator->() const;
DConstContainerIndex& sync(); // recalculate 'IB::mPos' when externalControl == true
DConstContainerIndex& operator()(const std::vector<XIndexPtr>& inds); // control via external indice
DConstContainerIndex& operator()(); // -> sync; just to shorten the code
};
template <typename T>
class DContainerIndex : public DConstContainerIndex<T>
{
public:
typedef DConstContainerIndex<T> CCI;
typedef typename CCI::IB IB;
private:
T* mData = nullptr;
public:
DEFAULT_MEMBERS(DContainerIndex);
DContainerIndex(T* data, const RangePtr& range);
DContainerIndex(T* data, const DConstContainerIndex<T>& cci);
T& operator*();
T* operator->();
};
}
#endif

View file

@ -59,11 +59,14 @@ namespace CNORXZ
S = Static S = Static
P = Partial = Sub P = Partial = Sub
C = Classic C = Classic
M = Multi or !const M = Multi (Index,Ranges) or !const (Container)
U = One(=Uni) dimensional U = One(=Uni) dimensional
N = None = Null N = None = Null
E = Extension (SSE,AVX,etc dof)
T = Thread T = Thread
R = Rank R = Rank
A = (const) Array
B = (mutable) Array
***/ ***/
// definition: base/dtype.h // definition: base/dtype.h

View file

@ -0,0 +1 @@

View file

@ -24,7 +24,7 @@ namespace CNORXZ
YIndex(const RangePtr& range, SizeT pos = 0); YIndex(const RangePtr& range, SizeT pos = 0);
YIndex(const RangePtr& range, const Vector<XIndexPtr>& is, SizeT pos = 0); YIndex(const RangePtr& range, const Vector<XIndexPtr>& is, SizeT pos = 0);
YIndex& sync(); YIndex& sync(); // remove!!!
YIndex& operator=(SizeT pos); YIndex& operator=(SizeT pos);
YIndex& operator++(); YIndex& operator++();
@ -56,7 +56,6 @@ namespace CNORXZ
Sptr<YRange> mRangePtr; Sptr<YRange> mRangePtr;
Vector<XIndexPtr> mIs; Vector<XIndexPtr> mIs;
Vector<SizeT> mBlockSizes; // dim() elements only!!! Vector<SizeT> mBlockSizes; // dim() elements only!!!
bool mExternalControl = false;
}; };

View file

@ -10,7 +10,7 @@ namespace CNORXZ
YIndex::YIndex(const RangePtr& range, SizeT pos) : YIndex::YIndex(const RangePtr& range, SizeT pos) :
IndexInterface<YIndex,DType>(pos), IndexInterface<YIndex,DType>(pos),
mRangePtr(rangeCast<YRange>(range)), mIs(mRangePtr->dim()), mRangePtr(rangeCast<YRange>(range)), mIs(mRangePtr->dim()),
mBlockSizes(mRangePtr->dim()), mExternalControl(false) mBlockSizes(mRangePtr->dim())
{ {
assert(0); assert(0);
// init ...!!! // init ...!!!
@ -19,7 +19,7 @@ namespace CNORXZ
YIndex::YIndex(const RangePtr& range, const Vector<XIndexPtr>& is, SizeT pos) : YIndex::YIndex(const RangePtr& range, const Vector<XIndexPtr>& is, SizeT pos) :
IndexInterface<YIndex,DType>(pos), IndexInterface<YIndex,DType>(pos),
mRangePtr(rangeCast<YRange>(range)), mIs(is), mRangePtr(rangeCast<YRange>(range)), mIs(is),
mBlockSizes(mRangePtr->dim()), mExternalControl(false) mBlockSizes(mRangePtr->dim())
{ {
CXZ_ASSERT(mIs.size() == mRangePtr->dim(), "obtained wrong number of indices"); CXZ_ASSERT(mIs.size() == mRangePtr->dim(), "obtained wrong number of indices");
assert(0); assert(0);
@ -42,7 +42,6 @@ namespace CNORXZ
YIndex& YIndex::operator++() YIndex& YIndex::operator++()
{ {
if(mExternalControl) this->sync();
assert(0); assert(0);
// increment sub inds (LAZY!!!) !!! // increment sub inds (LAZY!!!) !!!
++mPos; ++mPos;
@ -51,7 +50,6 @@ namespace CNORXZ
YIndex& YIndex::operator--() YIndex& YIndex::operator--()
{ {
if(mExternalControl) this->sync();
assert(0); assert(0);
// decrement sub inds (LAZY!!!) !!! // decrement sub inds (LAZY!!!) !!!
--mPos; --mPos;
@ -91,14 +89,12 @@ namespace CNORXZ
DType YIndex::operator*() const DType YIndex::operator*() const
{ {
assert(0); assert(0);
// sub inds !!!
return DType(); return DType();
} }
DType YIndex::operator->() const DType YIndex::operator->() const
{ {
assert(0); assert(0);
// sub inds !!!
return DType(); return DType();
} }
@ -141,7 +137,6 @@ namespace CNORXZ
DType YIndex::meta() const DType YIndex::meta() const
{ {
//this->sync();
Vector<DType> v(mIs.size()); Vector<DType> v(mIs.size());
std::transform(mIs.begin(), mIs.end(), v.begin(), [](auto& x) { return x->meta(); }); std::transform(mIs.begin(), mIs.end(), v.begin(), [](auto& x) { return x->meta(); });
return DType(v); return DType(v);