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

View file

@ -96,7 +96,7 @@ namespace MultiArrayTools
MBlock<T>& get();
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;
protected:
@ -126,7 +126,7 @@ namespace MultiArrayTools
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;
protected:
@ -157,7 +157,7 @@ namespace MultiArrayTools
const MBlock<T>& get() const;
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;
protected:
@ -182,7 +182,7 @@ namespace MultiArrayTools
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;
protected:
@ -202,7 +202,7 @@ namespace MultiArrayTools
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;
protected:
@ -323,8 +323,8 @@ namespace MultiArrayTools
auto blockIndex = seekBlockIndex( make_viwb( mIndex ), second);
std::intptr_t blockIndexNum = blockIndex->getPtrNum();
block(blockIndex);
second.block(blockIndex);
block(blockIndex, true);
second.block(blockIndex, true);
//size_t cnt = 0;
//std::clock_t cs = clock();
for(*mIndex = 0; mIndex->pos() != mIndex->max(); mIndex->pp(blockIndexNum) ){
@ -354,10 +354,12 @@ namespace MultiArrayTools
}
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) );
if(init){
mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max());
}
return btv;
}
@ -390,10 +392,12 @@ namespace MultiArrayTools
}
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) );
if(init){
mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max());
}
return btv;
}
@ -440,10 +444,12 @@ namespace MultiArrayTools
}
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) );
if(init){
mBlock = makeBlock(mArrayRef.data(), btv[0].second, blockIndex->max());
}
return btv;
}
@ -471,10 +477,13 @@ namespace MultiArrayTools
}
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;
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;
}
@ -506,9 +515,12 @@ namespace MultiArrayTools
}
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>

View file

@ -20,11 +20,11 @@ namespace MultiArrayHelper
template <class... Ops>
static void makeBlockTypeVec(std::vector<std::pair<BlockType,size_t> >& btv,
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() );
PackNum<N-1>::makeBlockTypeVec(btv, ops, idxPtr);
PackNum<N-1>::makeBlockTypeVec(btv, ops, idxPtr, init);
}
template <typename T, class Func, class ArgTuple, class... Args>
@ -57,9 +57,9 @@ namespace MultiArrayHelper
template <class... Ops>
static void makeBlockTypeVec(std::vector<std::pair<BlockType,size_t> >& btv,
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() );
}