slice + blockSize fix -> simple slice calls work so far

This commit is contained in:
Christian Zimmermann 2018-03-06 13:58:17 +01:00
parent dcb76cc450
commit fb6c5c47ae
6 changed files with 129 additions and 28 deletions

View file

@ -171,6 +171,8 @@ namespace MultiArrayTools
template <class Expr>
Expr loop(Expr exp) const;
T* data() const;
private:
MutableMultiArrayBase<T,Ranges...>& mArrayRef;
@ -470,6 +472,12 @@ namespace MultiArrayTools
return exp;
}
template <typename T, class... Ranges>
T* OperationRoot<T,Ranges...>::data() const
{
return mDataPtr + mIndex.pos();
}
/*******************
* Operation *
*******************/

View file

@ -56,6 +56,19 @@ namespace MultiArrayHelper
{
return std::get<N>(ot).loop( PackNum<N-1>::mkLoop(ot,exp) );
}
template <typename T, class Op, class... SRanges>
static void mkSliceBlocks(std::array<size_t, sizeof...(SRanges)+1>& blocks,
const ContainerIndex<T,typename SRanges::IndexType...>& index,
const Op& op, size_t total = 1)
{
const size_t tmp =
op.rootSteps(reinterpret_cast<std::intptr_t>
( index.template getPtr<N>().get() ) )
.val();
std::get<N+1>(blocks) = tmp;
PackNum<N-1>::mkSliceBlocks(blocks, index, op, total * tmp);
}
};
template<>
@ -98,6 +111,18 @@ namespace MultiArrayHelper
return std::get<0>(ot).loop( exp );
}
template <typename T, class Op, class... SRanges>
static void mkSliceBlocks(std::array<size_t, sizeof...(SRanges)+1>& blocks,
const ContainerIndex<T,typename SRanges::IndexType...>& index,
const Op& op, size_t total = 1)
{
const size_t tmp =
op.rootSteps(reinterpret_cast<std::intptr_t>
( index.template getPtr<0>().get() ) )
.val();
std::get<1>(blocks) = tmp;
std::get<0>(blocks) = total * tmp; // this is not correct, but not used so far ... !!!
}
};

View file

@ -346,7 +346,7 @@ namespace MultiArrayTools
ContainerIndex<T,Indices...>& ContainerIndex<T,Indices...>::at(const MetaType& metaPos)
{
RPackNum<sizeof...(Indices)-1>::setMeta(mIPack, metaPos);
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack);
IB::mPos = RPackNum<sizeof...(Indices)-1>::makePos(mIPack, mBlockSizes);
return *this;
}

View file

@ -46,7 +46,7 @@ namespace MultiArrayHelper
static void initBlockSizes(std::array<size_t,sizeof...(Indices)+1>& bs,
std::tuple<std::shared_ptr<Indices>...>& ip)
{
std::get<N>(bs) = RPackNum<sizeof...(Indices)-N-1>::blockSize(ip);
std::get<N>(bs) = RPackNum<sizeof...(Indices)-N>::blockSize(ip);
RPackNum<N-1>::initBlockSizes(bs, ip);
}
@ -183,7 +183,7 @@ namespace MultiArrayHelper
static inline size_t makePos(const std::tuple<std::shared_ptr<Indices>...>& iPtrTup,
const std::array<size_t,sizeof...(Indices)+1>& blockSize)
{
return RPackNum<N-1>::makePos(iPtrTup, blockSize) + std::get<N>(iPtrTup)->pos() * std::get<N>(blockSize);
return RPackNum<N-1>::makePos(iPtrTup, blockSize) + std::get<N>(iPtrTup)->pos() * std::get<N+1>(blockSize);
}
template <class Pack, class IndexType, class... Indices>
@ -197,7 +197,7 @@ namespace MultiArrayHelper
template <class... Indices>
static size_t blockSize(const std::tuple<std::shared_ptr<Indices>...>& pack)
{
return std::get<sizeof...(Indices)-N-1>(pack)->max() * RPackNum<N-1>::blockSize(pack);
return std::get<sizeof...(Indices)-N>(pack)->max() * RPackNum<N-1>::blockSize(pack);
}
@ -265,7 +265,7 @@ namespace MultiArrayHelper
static void initBlockSizes(std::array<size_t,sizeof...(Indices)+1>& bs,
std::tuple<std::shared_ptr<Indices>...>& ip)
{
std::get<0>(bs) = RPackNum<sizeof...(Indices)-1>::blockSize(ip);
std::get<0>(bs) = RPackNum<sizeof...(Indices)>::blockSize(ip);
}
template <class... Indices>
@ -370,7 +370,7 @@ namespace MultiArrayHelper
static inline size_t makePos(const std::tuple<std::shared_ptr<Indices>...>& iPtrTup,
const std::array<size_t,sizeof...(Indices)+1>& blockSize)
{
return std::get<0>(iPtrTup)->pos() * std::get<0>(blockSize);
return std::get<0>(iPtrTup)->pos() * std::get<1>(blockSize);
}
template <class Pack, class IndexType>
@ -382,7 +382,7 @@ namespace MultiArrayHelper
template <class... Indices>
static size_t blockSize(const std::tuple<std::shared_ptr<Indices>...>& pack)
{
return std::get<sizeof...(Indices)-1>(pack)->max();
return 1;
}
template <class... Ranges>

View file

@ -18,7 +18,7 @@ namespace MultiArrayTools
DEFAULT_MEMBERS(Slice);
Slice(T* data, const std::shared_ptr<SRanges>&... ranges);
Slice(const std::shared_ptr<SRanges>&... ranges, T* data = nullptr);
virtual const T& operator[](const IType& i) const override;
virtual T& operator[](const IType& i) override;
@ -33,11 +33,14 @@ namespace MultiArrayTools
virtual auto begin() const -> IType override;
virtual auto end() const -> IType override;
template <class... MARanges>
auto define(const std::shared_ptr<typename SRanges::IType>&... inds)
-> SliceDef<T,MARanges...>;
auto define(const std::shared_ptr<typename SRanges::IndexType>&... inds)
-> SliceDef<T,SRanges...>;
private:
friend SliceDef<T,SRanges...>;
void format(const std::array<size_t,sizeof...(SRanges)+1>& blocks);
T* mData;
};
@ -45,15 +48,20 @@ namespace MultiArrayTools
template <typename T, class... SRanges>
class SliceDef
{
private:
SliceDef() = default;
public:
typedef ContainerIndex<T,typename SRanges::IndexType...> IType;
private:
IType mIndex;
Slice<T,SRanges...>& mSl;
SliceDef() = default;
public:
SliceDef(Slice<T,SRanges...>& sl,
const std::shared_ptr<typename SRanges::IndexType>&... inds);
template <class... ORanges>
SliceDef& operator=(OperationRoot<T,ORanges...>& op);
SliceDef& operator=(const OperationRoot<T,ORanges...>& op);
};
} // end namespace MultiArrayTools
@ -70,14 +78,20 @@ namespace MultiArrayTools
*************/
template <typename T, class... SRanges>
Slice<T,SRanges...>::Slice(T* data, const std::shared_ptr<SRanges>&... ranges) :
void Slice<T,SRanges...>::format(const std::array<size_t,sizeof...(SRanges)+1>& blocks)
{
MAB::mProtoI->format(blocks);
}
template <typename T, class... SRanges>
Slice<T,SRanges...>::Slice(const std::shared_ptr<SRanges>&... ranges, T* data) :
MutableMultiArrayBase<T,SRanges...>(ranges...),
mData(data) {}
template <typename T, class... SRanges>
const T& Slice<T,SRanges...>::operator[](const IType& i) const
{
assert(i.sliceMode()); // -> compare objects !!!!!
//assert(i.sliceMode()); // -> compare objects !!!!!
assert(i.container() == reinterpret_cast<std::intptr_t>(this));
return mData[ i.pos() ];
}
@ -85,7 +99,7 @@ namespace MultiArrayTools
template <typename T, class... SRanges>
T& Slice<T,SRanges...>::operator[](const IType& i)
{
assert(i.sliceMode());
//assert(i.sliceMode());
assert(i.container() == reinterpret_cast<std::intptr_t>(this));
return mData[ i.pos() ];
}
@ -93,12 +107,16 @@ namespace MultiArrayTools
template <typename T, class... SRanges>
const T& Slice<T,SRanges...>::at(const typename IType::MetaType& meta) const
{
//auto x = begin().at(meta);
//VCHECK(x.pos());
return mData[ begin().at(meta).pos() ];
}
template <typename T, class... SRanges>
T& Slice<T,SRanges...>::at(const typename IType::MetaType& meta)
{
//auto x = begin().at(meta);
//VCHECK(x.pos());
return mData[ begin().at(meta).pos() ];
}
@ -123,7 +141,7 @@ namespace MultiArrayTools
template <typename T, class... SRanges>
auto Slice<T,SRanges...>::begin() const -> Slice<T,SRanges...>::IType
{
IType i = MAB::mProtoI;
IType i(*MAB::mProtoI);
i = 0;
//i = mStartPos;
return i.setData(data());
@ -132,19 +150,42 @@ namespace MultiArrayTools
template <typename T, class... SRanges>
auto Slice<T,SRanges...>::end() const -> Slice<T,SRanges...>::IType
{
IType i = MAB::mProtoI;
IType i(*MAB::mProtoI);
i = i.max(); // CHECK !!!
//i = std::get<sizeof...(SRanges)>(mBlockSizes);
return i.setData(data());
}
/*
SliceDef<T,SRanges...>::
SliceDef(Slice<T,SRanges...>& sl,
const std::shared_ptr<typename SRanges::IndexType>&... inds);
template <typename T, class... SRanges>
auto Slice<T,SRanges...>::define(const std::shared_ptr<typename SRanges::IndexType>&... inds)
-> SliceDef<T,SRanges...>
{
return SliceDef<T,SRanges...>(*this, inds...);
}
template <typename T, class... SRanges>
SliceDef<T,SRanges...>::SliceDef(Slice<T,SRanges...>& sl,
const std::shared_ptr<typename SRanges::IndexType>&... inds) :
mIndex(sl.begin()),
mSl(sl)
{
mIndex(inds...);
}
template <typename T, class... SRanges>
template <class... ORanges>
SliceDef<T,SRanges...>& SliceDef<T,SRanges...>::operator=(OperationRoot<T,ORanges...>& op);
*/
SliceDef<T,SRanges...>& SliceDef<T,SRanges...>::operator=(const OperationRoot<T,ORanges...>& op)
{
std::array<size_t,sizeof...(SRanges)+1> blocks;
PackNum<sizeof...(SRanges)-1>::
template mkSliceBlocks<T,OperationRoot<T,ORanges...>,SRanges...>(blocks, mIndex, op);
mSl.format(blocks);
//VCHECK(blocks[0]);
//VCHECK(blocks[1]);
mSl.mData = op.data();
return *this;
}
} // end namespace MultiArrayTools

View file

@ -186,6 +186,33 @@ namespace {
EXPECT_EQ( ma2.at('W') , 4.209 );
}
TEST_F(MATest_MDim, SliceTest1)
{
MultiArray<double,MATest_MDim::MRange,MATest_MDim::SRange> ma(mrptr, sr3ptr, vv);
Slice<double,MATest_MDim::SRange> sl(sr3ptr);
auto i = MAT::getIndex(sr3ptr);
auto mi = MAT::getIndex(mrptr);
mi->at( mkt('y', 'f') );
sl.define(i) = ma(mi, i);
EXPECT_EQ( sl.at('1'), 6.712 );
EXPECT_EQ( sl.at('2'), 6.408 );
EXPECT_EQ( sl.at('3'), 1.959 );
Slice<double,SRange> sl2(sr2ptr);
auto j = MAT::getIndex(sr3ptr);
auto mj = MAT::getIndex(mrptr);
mj->at( mkt('y','a') );
j->at('2');
auto jj = mj->template getPtr<1>();
sl2.define(jj) = ma(mj, j);
EXPECT_EQ( sl2.at('a'), 3.084 );
EXPECT_EQ( sl2.at('l'), 8.175 );
EXPECT_EQ( sl2.at('f'), 6.408 );
EXPECT_EQ( sl2.at('g'), 4.209 );
}
} // end namespace