static polymorphism in Block* + remove unused code

This commit is contained in:
Christian Zimmermann 2018-01-04 11:43:45 +01:00
parent 2116abc556
commit 0bbbdf7168
3 changed files with 96 additions and 71 deletions

View file

@ -10,11 +10,11 @@ namespace MultiArrayHelper
class BlockBinaryOp; class BlockBinaryOp;
template <typename T> template <typename T, class BlockClass>
class BlockBase; class BlockBase;
template <typename T> template <typename T, class BlockClass>
class MutableBlockBase; class MutableBlockBase;
@ -26,22 +26,6 @@ namespace MultiArrayHelper
class MBlock; class MBlock;
template <typename T>
class BlockValue;
template <typename T>
class MBlockValue;
template <typename T>
class SplitBlock;
template <typename T>
class MSplitBlock;
template <typename T> template <typename T>
class BlockResult; class BlockResult;

View file

@ -46,18 +46,32 @@ namespace MultiArrayHelper
// EVERYTHING IN HERE MUST N O T BE VITUAL !! // EVERYTHING IN HERE MUST N O T BE VITUAL !!
template <typename T> template <typename T, class BlockClass>
class BlockBase class BlockBase
{ {
public: public:
DEFAULT_MEMBERS(BlockBase); const BlockClass& THIS() const { return static_cast<BlockClass const&>(*this); }
BlockBase(size_t size); BlockClass& THIS() { return static_cast<BlockClass&>(*this); }
static BlockType sType() { return BlockClass::sType(); }
size_t size() const; size_t size() const;
bool init() const; bool init() const;
protected: BlockType type() const { return THIS().type(); }
const T& operator[](size_t pos) const { return THIS()[pos]; }
BlockClass& set(size_t npos) { return THIS().set(npos); }
size_t stepSize() const { return THIS().stepSize(); }
private:
friend BlockClass;
friend MutableBlockBase<T,BlockClass>;
DEFAULT_MEMBERS(BlockBase);
BlockBase(size_t size);
size_t mSize = 0; size_t mSize = 0;
bool mInit = false; bool mInit = false;
}; };
@ -93,20 +107,32 @@ namespace MultiArrayHelper
} }
template <typename T> template <typename T, class BlockClass>
class MutableBlockBase : public BlockBase<T> class MutableBlockBase : public BlockBase<T,BlockClass>
{ {
public: public:
typedef BlockBase<T,BlockClass> BB;
T& operator[](size_t pos) { return BB::THIS()[pos]; }
private:
friend BlockClass;
DEFAULT_MEMBERS(MutableBlockBase); DEFAULT_MEMBERS(MutableBlockBase);
MutableBlockBase(size_t size); MutableBlockBase(size_t size);
}; };
template <typename T> template <typename T>
class Block : public BlockBase<T> class Block : public BlockBase<T,Block<T> >
{ {
public: public:
typedef BlockBase<T,Block<T> > BB;
static BlockType sType() { return BlockType::BLOCK; }
DEFAULT_MEMBERS(Block); DEFAULT_MEMBERS(Block);
Block(const T* data, size_t begPos, size_t size, size_t stepSize); Block(const T* data, size_t begPos, size_t size, size_t stepSize);
@ -122,13 +148,16 @@ namespace MultiArrayHelper
}; };
template <class BlockClass> template <class BlockClass>
class BlockArray : public BlockBase<BlockClass> class BlockArray : public BlockBase<BlockClass,BlockArray<BlockClass> >
{ {
typedef BlockBase<BlockClass,BlockArray<BlockClass> > BB;
static BlockType sType() { return BlockType::ARRAY; }
DEFAULT_MEMBERS(BlockArray); DEFAULT_MEMBERS(BlockArray);
template <typename... Args> BlockArray(const BlockClass& block, size_t size, size_t stepSize);
BlockArray(size_t stepSize, const Args&... args);
BlockType type() const; BlockType type() const;
BlockClass& operator[](size_t pos); BlockClass& operator[](size_t pos);
@ -136,15 +165,20 @@ namespace MultiArrayHelper
size_t stepSize() const; size_t stepSize() const;
protected: protected:
BlockClass mBlock; const BlockClass& mBlock;
size_t mStepSize; // total stepSize !! size_t mStepSize; // total stepSize !!
}; };
template <typename T> template <typename T>
class MBlock : public MutableBlockBase<T> class MBlock : public MutableBlockBase<T,MBlock<T> >
{ {
public: public:
typedef BlockBase<T,MBlock<T> > BB;
static BlockType sType() { return BlockType::BLOCK; }
DEFAULT_MEMBERS(MBlock); DEFAULT_MEMBERS(MBlock);
MBlock(T* data, size_t begPos, size_t size, size_t stepSize); MBlock(T* data, size_t begPos, size_t size, size_t stepSize);
@ -164,12 +198,14 @@ namespace MultiArrayHelper
}; };
template <typename T> template <typename T>
class BlockResult : public MutableBlockBase<T> class BlockResult : public MutableBlockBase<T,BlockResult<T> >
{ {
public: public:
typedef BlockBase<T> BB; typedef BlockBase<T,BlockResult<T> > BB;
using BB::init; using BB::init;
static BlockType sType() { return BlockType::RESULT; }
BlockResult(); BlockResult();
BlockResult(const BlockResult& in); BlockResult(const BlockResult& in);
@ -196,6 +232,7 @@ namespace MultiArrayHelper
protected: protected:
T* mResPtr = nullptr; T* mResPtr = nullptr;
T* mBegPtr = nullptr;
}; };
} // end namespace MultiArrayHelper } // end namespace MultiArrayHelper
@ -261,17 +298,17 @@ namespace MultiArrayHelper
* BlockBase * * BlockBase *
*****************/ *****************/
template <typename T> template <typename T, class BlockClass>
BlockBase<T>::BlockBase(size_t size) : mSize(size), mInit(size != 0) {} BlockBase<T,BlockClass>::BlockBase(size_t size) : mSize(size), mInit(size != 0) {}
template <typename T> template <typename T, class BlockClass>
size_t BlockBase<T>::size() const size_t BlockBase<T,BlockClass>::size() const
{ {
return mSize; return mSize;
} }
template <typename T> template <typename T, class BlockClass>
bool BlockBase<T>::init() const bool BlockBase<T,BlockClass>::init() const
{ {
return mInit; return mInit;
} }
@ -280,8 +317,8 @@ namespace MultiArrayHelper
* MutableBlockBase * * MutableBlockBase *
************************/ ************************/
template <typename T> template <typename T, class BlockClass>
MutableBlockBase<T>::MutableBlockBase(size_t size) : BlockBase<T>(size) {} MutableBlockBase<T,BlockClass>::MutableBlockBase(size_t size) : BlockBase<T,BlockClass>(size) {}
/************* /*************
@ -291,7 +328,7 @@ namespace MultiArrayHelper
template <typename T> template <typename T>
Block<T>::Block(const T* data, Block<T>::Block(const T* data,
size_t begPos, size_t size, size_t stepSize) : size_t begPos, size_t size, size_t stepSize) :
BlockBase<T>(size), BlockBase<T,Block>(size),
mData(data), mData(data),
mBegPtr(data + begPos), mBegPtr(data + begPos),
mStepSize(stepSize) {} mStepSize(stepSize) {}
@ -329,9 +366,9 @@ namespace MultiArrayHelper
******************/ ******************/
template <class BlockClass> template <class BlockClass>
template <typename... Args> BlockArray<BlockClass>::BlockArray(const BlockClass& block, size_t size, size_t stepSize) :
BlockArray<BlockClass>::BlockArray(size_t stepSize, const Args&... args) : BlockBase<BlockClass,BlockArray<BlockClass> >(size),
mBlock(args...), mStepSize(stepSize) {} mBlock(block), mStepSize(stepSize) {}
template <class BlockClass> template <class BlockClass>
BlockType BlockArray<BlockClass>::type() const BlockType BlockArray<BlockClass>::type() const
@ -366,7 +403,7 @@ namespace MultiArrayHelper
template <typename T> template <typename T>
MBlock<T>::MBlock(T* data, MBlock<T>::MBlock(T* data,
size_t begPos, size_t size, size_t stepSize) : size_t begPos, size_t size, size_t stepSize) :
MutableBlockBase<T>(size), MutableBlockBase<T,MBlock>(size),
mData(data), mData(data),
mBegPtr(data + begPos), mBegPtr(data + begPos),
mStepSize(stepSize) {} mStepSize(stepSize) {}
@ -375,7 +412,7 @@ namespace MultiArrayHelper
template <class BlockClass> template <class BlockClass>
MBlock<T>& MBlock<T>::operator=(const BlockClass& in) MBlock<T>& MBlock<T>::operator=(const BlockClass& in)
{ {
for(size_t i = 0; i != BlockBase<T>::mSize; ++i){ for(size_t i = 0; i != BB::mSize; ++i){
(*this)[i] = in[i]; (*this)[i] = in[i];
} }
return *this; return *this;
@ -420,10 +457,10 @@ namespace MultiArrayHelper
*******************/ *******************/
template <typename T> template <typename T>
BlockResult<T>::BlockResult() : MutableBlockBase<T>() {} BlockResult<T>::BlockResult() : MutableBlockBase<T,BlockResult>() {}
template <typename T> template <typename T>
BlockResult<T>::BlockResult(const BlockResult<T>& in) : MutableBlockBase<T>(in.size()) BlockResult<T>::BlockResult(const BlockResult<T>& in) : MutableBlockBase<T,BlockResult>(in.size())
{ {
if(BB::mInit){ if(BB::mInit){
mResPtr = new T[BB::mSize]; mResPtr = new T[BB::mSize];
@ -431,17 +468,20 @@ namespace MultiArrayHelper
for(size_t i = 0; i != BB::mSize; ++i){ for(size_t i = 0; i != BB::mSize; ++i){
mResPtr[i] = in.mResPtr[i]; mResPtr[i] = in.mResPtr[i];
} }
mBegPtr = mResPtr;
} }
template <typename T> template <typename T>
BlockResult<T>::BlockResult(BlockResult<T>&& in) : MutableBlockBase<T>(in.size()) BlockResult<T>::BlockResult(BlockResult<T>&& in) : MutableBlockBase<T,BlockResult>(in.size())
{ {
if(BB::mInit){ if(BB::mInit){
mResPtr = in.mResPtr; mResPtr = in.mResPtr;
mBegPtr = mResPtr;
} }
in.mSize = 0; in.mSize = 0;
in.mInit = false; in.mInit = false;
in.mResPtr = nullptr; in.mResPtr = nullptr;
in.mBegPtr = nullptr;
} }
template <typename T> template <typename T>
@ -455,6 +495,7 @@ namespace MultiArrayHelper
for(size_t i = 0; i != BB::mSize; ++i){ for(size_t i = 0; i != BB::mSize; ++i){
mResPtr[i] = in.mResPtr[i]; mResPtr[i] = in.mResPtr[i];
} }
mBegPtr = mResPtr;
return *this; return *this;
} }
@ -465,6 +506,7 @@ namespace MultiArrayHelper
BB::mInit = BB::mInit and BB::mSize != 0; BB::mInit = BB::mInit and BB::mSize != 0;
if(BB::mInit){ if(BB::mInit){
mResPtr = in.mResPtr; mResPtr = in.mResPtr;
mBegPtr = mResPtr;
} }
in.mSize = 0; in.mSize = 0;
in.mInit = false; in.mInit = false;
@ -475,7 +517,7 @@ namespace MultiArrayHelper
template <typename T> template <typename T>
template <typename... ArgTypes> template <typename... ArgTypes>
BlockResult<T>::BlockResult(size_t size, const ArgTypes&... args) : BlockResult<T>::BlockResult(size_t size, const ArgTypes&... args) :
MutableBlockBase<T>(size) MutableBlockBase<T,BlockResult>(size)
{ {
if(BB::mInit){ if(BB::mInit){
mResPtr = new T[BB::mSize](args...); mResPtr = new T[BB::mSize](args...);
@ -483,6 +525,7 @@ namespace MultiArrayHelper
for(size_t i = 0; i != BB::mSize; ++i){ for(size_t i = 0; i != BB::mSize; ++i){
mResPtr[i] = static_cast<T>( 0 ); mResPtr[i] = static_cast<T>( 0 );
} }
mBegPtr = mResPtr;
} }
template <typename T> template <typename T>
@ -490,6 +533,7 @@ namespace MultiArrayHelper
{ {
delete[] mResPtr; delete[] mResPtr;
mResPtr = nullptr; mResPtr = nullptr;
mBegPtr = nullptr;
BB::mInit = false; BB::mInit = false;
BB::mSize = 0; BB::mSize = 0;
} }
@ -515,18 +559,19 @@ namespace MultiArrayHelper
template <typename T> template <typename T>
const T& BlockResult<T>::operator[](size_t i) const const T& BlockResult<T>::operator[](size_t i) const
{ {
return mResPtr[i]; return mBegPtr[i];
} }
template <typename T> template <typename T>
T& BlockResult<T>::operator[](size_t i) T& BlockResult<T>::operator[](size_t i)
{ {
return mResPtr[i]; return mBegPtr[i];
} }
template <typename T> template <typename T>
BlockResult<T>& BlockResult<T>::set(size_t npos) BlockResult<T>& BlockResult<T>::set(size_t npos)
{ {
mBegPtr = mResPtr + npos;
return *this; return *this;
} }
@ -552,6 +597,7 @@ namespace MultiArrayHelper
for(size_t i = 0; i != BB::mSize; ++i){ for(size_t i = 0; i != BB::mSize; ++i){
mResPtr[i] = static_cast<T>( 0 ); mResPtr[i] = static_cast<T>( 0 );
} }
mBegPtr = mResPtr;
return *this; return *this;
} }

View file

@ -99,7 +99,8 @@ namespace MultiArrayTools
typedef OperationBase<T> OB; typedef OperationBase<T> OB;
typedef ContainerRange<Ranges...> CRange; typedef ContainerRange<Ranges...> CRange;
typedef typename MultiRange<Ranges...>::IndexType IndexType; typedef typename MultiRange<Ranges...>::IndexType IndexType;
typedef MBlock<T> bType;
OperationMaster(MutableMultiArrayBase<T,Ranges...>& ma, const OpClass& second, OperationMaster(MutableMultiArrayBase<T,Ranges...>& ma, const OpClass& second,
std::shared_ptr<typename CRange::IndexType>& index); std::shared_ptr<typename CRange::IndexType>& index);
@ -121,7 +122,7 @@ namespace MultiArrayTools
MutableMultiArrayBase<T,Ranges...>& mArrayRef; MutableMultiArrayBase<T,Ranges...>& mArrayRef;
std::shared_ptr<IndexType> mIndex; std::shared_ptr<IndexType> mIndex;
IndexInfo mIInfo; IndexInfo mIInfo;
mutable MBlock<T> mBlock; mutable bType mBlock;
}; };
@ -136,6 +137,7 @@ namespace MultiArrayTools
typedef OperationTemplate<T,ConstOperationRoot<T,Ranges...> > OT; typedef OperationTemplate<T,ConstOperationRoot<T,Ranges...> > OT;
typedef ContainerRange<Ranges...> CRange; typedef ContainerRange<Ranges...> CRange;
typedef typename CRange::IndexType IndexType; typedef typename CRange::IndexType IndexType;
typedef Block<T> bType;
ConstOperationRoot(const MultiArrayBase<T,Ranges...>& ma, ConstOperationRoot(const MultiArrayBase<T,Ranges...>& ma,
const std::shared_ptr<typename Ranges::IndexType>&... indices); const std::shared_ptr<typename Ranges::IndexType>&... indices);
@ -154,7 +156,7 @@ namespace MultiArrayTools
MultiArrayBase<T,Ranges...> const& mArrayRef; MultiArrayBase<T,Ranges...> const& mArrayRef;
std::shared_ptr<IndexType> mIndex; std::shared_ptr<IndexType> mIndex;
IndexInfo mIInfo; IndexInfo mIInfo;
mutable Block<T> mBlock; mutable bType mBlock;
}; };
template <typename T, class... Ranges> template <typename T, class... Ranges>
@ -168,7 +170,8 @@ namespace MultiArrayTools
typedef OperationTemplate<T,OperationRoot<T,Ranges...> > OT; typedef OperationTemplate<T,OperationRoot<T,Ranges...> > OT;
typedef ContainerRange<Ranges...> CRange; typedef ContainerRange<Ranges...> CRange;
typedef typename CRange::IndexType IndexType; typedef typename CRange::IndexType IndexType;
typedef MBlock<T> bType;
OperationRoot(MutableMultiArrayBase<T,Ranges...>& ma, OperationRoot(MutableMultiArrayBase<T,Ranges...>& ma,
const std::shared_ptr<typename Ranges::IndexType>&... indices); const std::shared_ptr<typename Ranges::IndexType>&... indices);
@ -179,8 +182,6 @@ namespace MultiArrayTools
MBlock<T>& get(); MBlock<T>& get();
OperationRoot& set(const IndexInfo* blockIndex); OperationRoot& set(const IndexInfo* blockIndex);
OperationRoot& set(std::shared_ptr<VIWB> blockIndex);
std::vector<BTSS> block(const IndexInfo* blockIndex, bool init = false) const; std::vector<BTSS> block(const IndexInfo* blockIndex, bool init = false) const;
const OperationRoot& block() const; const OperationRoot& block() const;
@ -193,8 +194,7 @@ namespace MultiArrayTools
MutableMultiArrayBase<T,Ranges...>& mArrayRef; MutableMultiArrayBase<T,Ranges...>& mArrayRef;
std::shared_ptr<IndexType> mIndex; std::shared_ptr<IndexType> mIndex;
IndexInfo mIInfo; IndexInfo mIInfo;
mutable MBlock<T> mBlock; mutable bType mBlock;
std::shared_ptr<VIWB> mBlockIndex; // predefine to save time
const IndexInfo* mBlockII; // predefine to save time const IndexInfo* mBlockII; // predefine to save time
}; };
@ -208,6 +208,7 @@ namespace MultiArrayTools
typedef OperationBase<T> OB; typedef OperationBase<T> OB;
typedef OperationTemplate<T,Operation<T,OpFunction,Ops...> > OT; typedef OperationTemplate<T,Operation<T,OpFunction,Ops...> > OT;
typedef OpFunction F; typedef OpFunction F;
typedef BlockResult<T> bType;
Operation(const Ops&... ops); Operation(const Ops&... ops);
@ -218,7 +219,7 @@ namespace MultiArrayTools
protected: protected:
std::tuple<Ops const&...> mOps; std::tuple<Ops const&...> mOps;
mutable BlockResult<T> mRes; mutable bType mRes;
}; };
template <typename T, class Op, class IndexType> template <typename T, class Op, class IndexType>
@ -228,6 +229,7 @@ namespace MultiArrayTools
typedef T value_type; typedef T value_type;
typedef OperationTemplate<T,Contraction<T,Op,IndexType> > OT; typedef OperationTemplate<T,Contraction<T,Op,IndexType> > OT;
typedef BlockResult<T> bType;
Contraction(const Op& op, std::shared_ptr<IndexType> ind); Contraction(const Op& op, std::shared_ptr<IndexType> ind);
@ -240,7 +242,8 @@ namespace MultiArrayTools
const Op& mOp; const Op& mOp;
std::shared_ptr<IndexType> mInd; std::shared_ptr<IndexType> mInd;
mutable BlockResult<T> mRes; //mutable BlockArray<Op::bType> mInBlock;
mutable bType mRes;
}; };
} }
@ -499,7 +502,7 @@ namespace MultiArrayTools
const std::shared_ptr<typename Ranges::IndexType>&... indices) : const std::shared_ptr<typename Ranges::IndexType>&... indices) :
//OperationTemplate<T,OperationRoot<T,Ranges...> >(this), //OperationTemplate<T,OperationRoot<T,Ranges...> >(this),
mArrayRef(ma), mIndex( mkIndex( ma, indices... ) ), mIInfo(*mIndex), mArrayRef(ma), mIndex( mkIndex( ma, indices... ) ), mIInfo(*mIndex),
mBlockIndex(nullptr), mBlockII(nullptr) mBlockII(nullptr)
{} {}
template <typename T, class... Ranges> template <typename T, class... Ranges>
@ -539,14 +542,6 @@ namespace MultiArrayTools
return mBlock; return mBlock;
} }
template <typename T, class... Ranges>
OperationRoot<T,Ranges...>&
OperationRoot<T,Ranges...>::set(std::shared_ptr<VIWB> blockIndex)
{
mBlockIndex = blockIndex;
return *this;
}
template <typename T, class... Ranges> template <typename T, class... Ranges>
OperationRoot<T,Ranges...>& OperationRoot<T,Ranges...>&
OperationRoot<T,Ranges...>::set(const IndexInfo* blockIndex) OperationRoot<T,Ranges...>::set(const IndexInfo* blockIndex)