T* in BlockResult instead of std::vector

This commit is contained in:
Christian Zimmermann 2017-12-22 14:01:26 +01:00
parent edcbeb87eb
commit d8e62cc539
3 changed files with 148 additions and 39 deletions

View file

@ -39,13 +39,16 @@ namespace MultiArrayHelper
class BlockBase class BlockBase
{ {
public: public:
DEFAULT_MEMBERS(BlockBase); DEFAULT_MEMBERS(BlockBase);
BlockBase(size_t size); BlockBase(size_t size);
size_t size() const; size_t size() const;
bool init() const;
protected: protected:
size_t mSize = 0; size_t mSize = 0;
bool mInit = false;
}; };
template <typename T> template <typename T>
@ -133,16 +136,24 @@ namespace MultiArrayHelper
class BlockResult : public MutableBlockBase<T> class BlockResult : public MutableBlockBase<T>
{ {
public: public:
typedef BlockBase<T> BB; typedef BlockBase<T> BB;
using BB::init;
DEFAULT_MEMBERS(BlockResult); BlockResult();
BlockResult(const BlockResult& in);
BlockResult(BlockResult&& in);
BlockResult& operator=(const BlockResult& in);
BlockResult& operator=(BlockResult&& in);
BlockResult(size_t size); BlockResult(size_t size);
~BlockResult();
template <class BlockClass> template <class BlockClass>
BlockResult& operator=(const BlockClass& in); BlockResult& operator=(const BlockClass& in);
BlockResult& assign(size_t size, const T& val); BlockResult& init(size_t size);
BlockType type() const; BlockType type() const;
const T& operator[](size_t pos) const; const T& operator[](size_t pos) const;
@ -151,7 +162,7 @@ namespace MultiArrayHelper
size_t stepSize() const; size_t stepSize() const;
protected: protected:
std::vector<T> mRes; T* mResPtr = nullptr;
}; };
} // end namespace MultiArrayHelper } // end namespace MultiArrayHelper
@ -174,6 +185,7 @@ namespace MultiArrayHelper
{ {
static OpFunc f; static OpFunc f;
BlockResult<T> res(arg1.size()); BlockResult<T> res(arg1.size());
assert(res.init() and arg1.init() and arg2.init());
assert(arg1.size() == arg2.size()); assert(arg1.size() == arg2.size());
for(size_t i = 0; i != arg1.size(); ++i){ for(size_t i = 0; i != arg1.size(); ++i){
res[i] = f(arg1[i], arg2[i]); res[i] = f(arg1[i], arg2[i]);
@ -188,7 +200,7 @@ namespace MultiArrayHelper
void BlockBinaryOpSelf<T,OpFunc,BlockClass>::operator()(const BlockClass& arg) void BlockBinaryOpSelf<T,OpFunc,BlockClass>::operator()(const BlockClass& arg)
{ {
static OpFunc f; static OpFunc f;
if(mRes.size() == 0) { mRes.assign(arg.size(), static_cast<T>(0)); } assert(mRes.init() and arg.init());
assert(mRes.size() == arg.size()); assert(mRes.size() == arg.size());
for(size_t i = 0; i != arg.size(); ++i){ for(size_t i = 0; i != arg.size(); ++i){
mRes[i] = f(mRes[i], arg[i]); mRes[i] = f(mRes[i], arg[i]);
@ -201,7 +213,7 @@ namespace MultiArrayHelper
*****************/ *****************/
template <typename T> template <typename T>
BlockBase<T>::BlockBase(size_t size) : mSize(size) {} BlockBase<T>::BlockBase(size_t size) : mSize(size), mInit(size != 0) {}
template <typename T> template <typename T>
size_t BlockBase<T>::size() const size_t BlockBase<T>::size() const
@ -209,6 +221,12 @@ namespace MultiArrayHelper
return mSize; return mSize;
} }
template <typename T>
bool BlockBase<T>::init() const
{
return mInit;
}
/************************ /************************
* MutableBlockBase * * MutableBlockBase *
************************/ ************************/
@ -317,16 +335,86 @@ namespace MultiArrayHelper
*******************/ *******************/
template <typename T> template <typename T>
BlockResult<T>::BlockResult(size_t size) : BlockResult<T>::BlockResult() : MutableBlockBase<T>() {}
MutableBlockBase<T>(size),
mRes(size) {}
template <typename T>
BlockResult<T>::BlockResult(const BlockResult<T>& in) : MutableBlockBase<T>(in.size())
{
if(BB::mInit){
mResPtr = new T[BB::mSize];
}
for(size_t i = 0; i != BB::mSize; ++i){
mResPtr[i] = in.mResPtr[i];
}
}
template <typename T>
BlockResult<T>::BlockResult(BlockResult<T>&& in) : MutableBlockBase<T>(in.size())
{
if(BB::mInit){
mResPtr = in.mResPtr;
}
in.mSize = 0;
in.mInit = false;
in.mResPtr = nullptr;
}
template <typename T>
BlockResult<T>& BlockResult<T>::operator=(const BlockResult<T>& in)
{
BB::mSize = in.size();
BB::mInit = BB::mInit and BB::mSize != 0;
if(BB::mInit){
mResPtr = new T[BB::mSize];
}
for(size_t i = 0; i != BB::mSize; ++i){
mResPtr[i] = in.mResPtr[i];
}
return *this;
}
template <typename T>
BlockResult<T>& BlockResult<T>::operator=(BlockResult<T>&& in)
{
BB::mSize = in.size();
BB::mInit = BB::mInit and BB::mSize != 0;
if(BB::mInit){
mResPtr = in.mResPtr;
}
in.mSize = 0;
in.mInit = false;
in.mResPtr = nullptr;
return *this;
}
template <typename T>
BlockResult<T>::BlockResult(size_t size) :
MutableBlockBase<T>(size)
{
if(BB::mInit){
mResPtr = new T[BB::mSize];
}
for(size_t i = 0; i != BB::mSize; ++i){
mResPtr[i] = static_cast<T>( 0 );
}
}
template <typename T>
BlockResult<T>::~BlockResult()
{
delete[] mResPtr;
mResPtr = nullptr;
BB::mInit = false;
BB::mSize = 0;
}
template <typename T> template <typename T>
template <class BlockClass> template <class BlockClass>
BlockResult<T>& BlockResult<T>::operator=(const BlockClass& in) BlockResult<T>& BlockResult<T>::operator=(const BlockClass& in)
{ {
//CHECK; assert(BB::mInit);
for(size_t i = 0; i != BlockBase<T>::mSize; ++i){ assert(BB::mSize == in.size());
for(size_t i = 0; i != BB::mSize; ++i){
(*this)[i] = in[i]; (*this)[i] = in[i];
} }
return *this; return *this;
@ -341,14 +429,13 @@ 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 mRes[i]; return mResPtr[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 mRes[i];
} }
template <typename T> template <typename T>
@ -364,10 +451,20 @@ namespace MultiArrayHelper
} }
template <typename T> template <typename T>
BlockResult<T>& BlockResult<T>::assign(size_t size, const T& val) BlockResult<T>& BlockResult<T>::init(size_t size)
{ {
BB::mSize = size; BB::mSize = size;
mRes.assign(BB::mSize, val); delete[] mResPtr;
if(BB::mSize != 0){
BB::mInit = true;
mResPtr = new T[BB::mSize];
}
else {
BB::mInit = false;
}
for(size_t i = 0; i != BB::mSize; ++i){
mResPtr[i] = static_cast<T>( 0 );
}
return *this; return *this;
} }

View file

@ -96,7 +96,7 @@ namespace MultiArrayTools
MBlock<T>& get(); MBlock<T>& get();
const Block<T>& get() const; const Block<T>& get() const;
std::vector<BTSS> block(const std::shared_ptr<VIWB> blockIndex) const; std::vector<BTSS> block(const std::shared_ptr<VIWB> blockIndex, bool init = false) const;
const OperationMaster& block() const; const OperationMaster& block() const;
protected: protected:
@ -126,7 +126,7 @@ namespace MultiArrayTools
const Block<T>& get() const; const Block<T>& get() const;
std::vector<BTSS> block(const std::shared_ptr<VIWB> blockIndex) const; std::vector<BTSS> block(const std::shared_ptr<VIWB> blockIndex, bool init = false) const;
const ConstOperationRoot& block() const; const ConstOperationRoot& block() const;
protected: protected:
@ -157,7 +157,7 @@ namespace MultiArrayTools
const MBlock<T>& get() const; const MBlock<T>& get() const;
MBlock<T>& get(); MBlock<T>& get();
std::vector<BTSS> block(const std::shared_ptr<VIWB> blockIndex) const; std::vector<BTSS> block(const std::shared_ptr<VIWB> blockIndex, bool init = false) const;
const OperationRoot& block() const; const OperationRoot& block() const;
protected: protected:
@ -182,7 +182,7 @@ namespace MultiArrayTools
const BlockResult<T>& get() const; const BlockResult<T>& get() const;
std::vector<BTSS> block(const std::shared_ptr<VIWB> blockIndex) const; std::vector<BTSS> block(const std::shared_ptr<VIWB> blockIndex, bool init = false) const;
const Operation& block() const; const Operation& block() const;
protected: protected:
@ -202,7 +202,7 @@ namespace MultiArrayTools
const BlockResult<T>& get() const; const BlockResult<T>& get() const;
std::vector<BTSS> block(const std::shared_ptr<VIWB> blockIndex) const; std::vector<BTSS> block(const std::shared_ptr<VIWB> blockIndex, bool init = false) const;
const Contraction& block() const; const Contraction& block() const;
protected: protected:
@ -323,8 +323,8 @@ namespace MultiArrayTools
auto blockIndex = seekBlockIndex( make_viwb( mIndex ), second); auto blockIndex = seekBlockIndex( make_viwb( mIndex ), second);
std::intptr_t blockIndexNum = blockIndex->getPtrNum(); std::intptr_t blockIndexNum = blockIndex->getPtrNum();
block(blockIndex); block(blockIndex, true);
second.block(blockIndex); second.block(blockIndex, true);
//size_t cnt = 0; //size_t cnt = 0;
//std::clock_t cs = clock(); //std::clock_t cs = clock();
for(*mIndex = 0; mIndex->pos() != mIndex->max(); mIndex->pp(blockIndexNum) ){ for(*mIndex = 0; mIndex->pos() != mIndex->max(); mIndex->pp(blockIndexNum) ){
@ -354,10 +354,12 @@ namespace MultiArrayTools
} }
template <typename T, class OpClass, class... Ranges> template <typename T, class OpClass, class... Ranges>
std::vector<BTSS> OperationMaster<T,OpClass,Ranges...>::block(const std::shared_ptr<VIWB> blockIndex) const std::vector<BTSS> OperationMaster<T,OpClass,Ranges...>::block(const std::shared_ptr<VIWB> blockIndex, bool init) const
{ {
std::vector<BTSS> btv(1, getBlockType( make_viwb( mIndex ), blockIndex, true) ); std::vector<BTSS> btv(1, getBlockType( make_viwb( mIndex ), blockIndex, true) );
mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); if(init){
mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max());
}
return btv; return btv;
} }
@ -390,10 +392,12 @@ namespace MultiArrayTools
} }
template <typename T, class... Ranges> template <typename T, class... Ranges>
std::vector<BTSS> ConstOperationRoot<T,Ranges...>::block(const std::shared_ptr<VIWB> blockIndex) const std::vector<BTSS> ConstOperationRoot<T,Ranges...>::block(const std::shared_ptr<VIWB> blockIndex, bool init) const
{ {
std::vector<BTSS> btv(1, getBlockType( make_viwb( mIndex ), blockIndex, true) ); std::vector<BTSS> btv(1, getBlockType( make_viwb( mIndex ), blockIndex, true) );
mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); if(init){
mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max());
}
return btv; return btv;
} }
@ -440,10 +444,12 @@ namespace MultiArrayTools
} }
template <typename T, class... Ranges> template <typename T, class... Ranges>
std::vector<BTSS> OperationRoot<T,Ranges...>::block(const std::shared_ptr<VIWB> blockIndex) const std::vector<BTSS> OperationRoot<T,Ranges...>::block(const std::shared_ptr<VIWB> blockIndex, bool init) const
{ {
std::vector<BTSS> btv(1, getBlockType( make_viwb( mIndex ), blockIndex, true) ); std::vector<BTSS> btv(1, getBlockType( make_viwb( mIndex ), blockIndex, true) );
mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max()); if(init){
mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max());
}
return btv; return btv;
} }
@ -471,10 +477,13 @@ namespace MultiArrayTools
} }
template <typename T, class OpFunction, class... Ops> template <typename T, class OpFunction, class... Ops>
std::vector<BTSS> Operation<T,OpFunction,Ops...>::block(const std::shared_ptr<VIWB> blockIndex) const std::vector<BTSS> Operation<T,OpFunction,Ops...>::block(const std::shared_ptr<VIWB> blockIndex, bool init) const
{ {
std::vector<BTSS> btv; std::vector<BTSS> btv;
PackNum<sizeof...(Ops)-1>::makeBlockTypeVec(btv, mOps, blockIndex); PackNum<sizeof...(Ops)-1>::makeBlockTypeVec(btv, mOps, blockIndex, init);
if(init){
mRes.init(blockIndex->max());
}
return btv; return btv;
} }
@ -506,9 +515,12 @@ namespace MultiArrayTools
} }
template <typename T, class Op, class IndexType> template <typename T, class Op, class IndexType>
std::vector<BTSS> Contraction<T,Op,IndexType>::block(const std::shared_ptr<VIWB> blockIndex) const std::vector<BTSS> Contraction<T,Op,IndexType>::block(const std::shared_ptr<VIWB> blockIndex, bool init) const
{ {
return mOp.block(blockIndex); if(init){
mRes.init(blockIndex->max());
}
return mOp.block(blockIndex, init);
} }
template <typename T, class Op, class IndexType> template <typename T, class Op, class IndexType>

View file

@ -20,11 +20,11 @@ namespace MultiArrayHelper
template <class... Ops> template <class... Ops>
static void makeBlockTypeVec(std::vector<std::pair<BlockType,size_t> >& btv, static void makeBlockTypeVec(std::vector<std::pair<BlockType,size_t> >& btv,
const std::tuple<Ops...>& ops, const std::tuple<Ops...>& ops,
std::shared_ptr<VIWB> idxPtr) std::shared_ptr<VIWB> idxPtr, bool init)
{ {
auto subvec = std::move( std::get<N>(ops).block(idxPtr) ); auto subvec = std::move( std::get<N>(ops).block(idxPtr, init) );
btv.insert(btv.end(), subvec.begin(), subvec.end() ); btv.insert(btv.end(), subvec.begin(), subvec.end() );
PackNum<N-1>::makeBlockTypeVec(btv, ops, idxPtr); PackNum<N-1>::makeBlockTypeVec(btv, ops, idxPtr, init);
} }
template <typename T, class Func, class ArgTuple, class... Args> template <typename T, class Func, class ArgTuple, class... Args>
@ -57,9 +57,9 @@ namespace MultiArrayHelper
template <class... Ops> template <class... Ops>
static void makeBlockTypeVec(std::vector<std::pair<BlockType,size_t> >& btv, static void makeBlockTypeVec(std::vector<std::pair<BlockType,size_t> >& btv,
const std::tuple<Ops...>& ops, const std::tuple<Ops...>& ops,
std::shared_ptr<VIWB> idxPtr) std::shared_ptr<VIWB> idxPtr, bool init)
{ {
auto subvec = std::move( std::get<0>(ops).block(idxPtr) ); auto subvec = std::move( std::get<0>(ops).block(idxPtr, init) );
btv.insert(btv.end(), subvec.begin(), subvec.end() ); btv.insert(btv.end(), subvec.begin(), subvec.end() );
} }