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;
template <typename T>
template <typename T, class BlockClass>
class BlockBase;
template <typename T>
template <typename T, class BlockClass>
class MutableBlockBase;
@ -26,22 +26,6 @@ namespace MultiArrayHelper
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>
class BlockResult;

View file

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

View file

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