const slice def class

This commit is contained in:
Christian Zimmermann 2018-09-13 13:28:40 +02:00
parent 405df0c426
commit 9b79c96eea
3 changed files with 95 additions and 33 deletions

View file

@ -67,6 +67,10 @@ namespace MultiArrayTools
template <typename T, class... SRanges> template <typename T, class... SRanges>
class SliceDef; class SliceDef;
// slice.h
template <typename T, class... SRanges>
class ConstSliceDef;
// map_range.h // map_range.h
template <class MapF, class... Indices> template <class MapF, class... Indices>
class MapIndex; class MapIndex;

View file

@ -502,7 +502,7 @@ namespace MultiArrayTools
-> ConstSlice<T,typename Indices::RangeType...> -> ConstSlice<T,typename Indices::RangeType...>
{ {
ConstSlice<T,typename Indices::RangeType...> out(inds->range()...); ConstSlice<T,typename Indices::RangeType...> out(inds->range()...);
out.define(inds...) = *this; out.define(inds...) = THIS();
return out; return out;
} }

View file

@ -34,10 +34,10 @@ namespace MultiArrayTools
virtual std::shared_ptr<MultiArrayBase<T,AnonymousRange> > anonymous(bool slice = false) const override; virtual std::shared_ptr<MultiArrayBase<T,AnonymousRange> > anonymous(bool slice = false) const override;
auto define(const std::shared_ptr<typename SRanges::IndexType>&... inds) auto define(const std::shared_ptr<typename SRanges::IndexType>&... inds)
-> SliceDef<T,SRanges...>; -> ConstSliceDef<T,SRanges...>;
private: private:
friend SliceDef<T,SRanges...>; friend ConstSliceDef<T,SRanges...>;
void format(const std::array<size_t,sizeof...(SRanges)+1>& blocks); void format(const std::array<size_t,sizeof...(SRanges)+1>& blocks);
@ -92,6 +92,39 @@ namespace MultiArrayTools
public: public:
typedef ContainerIndex<T,typename SRanges::IndexType...> IType; typedef ContainerIndex<T,typename SRanges::IndexType...> IType;
template <class Op>
static Slice<T,SRanges...> mkSlice( const typename Slice<T,SRanges...>::IndexType& ind,
const Op& op )
{
Slice<T,SRanges...> out(ind->range()->space(), &*ind);
std::array<size_t,sizeof...(SRanges)+1> ff;
for(size_t i = 0; i != sizeof...(SRanges)+1; ++i){
PackNum<sizeof...(SRanges)-1>::mkSliceBlocks(ff, ind, op);
}
out.format(ff);
return out;
}
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=(const OperationRoot<T,ORanges...>& op);
};
template <typename T, class... SRanges>
class ConstSliceDef
{
public:
typedef ContainerIndex<T,typename SRanges::IndexType...> IType;
template <class Op> template <class Op>
static ConstSlice<T,SRanges...> mkSlice( const typename ConstSlice<T,SRanges...>::IndexType& ind, static ConstSlice<T,SRanges...> mkSlice( const typename ConstSlice<T,SRanges...>::IndexType& ind,
const Op& op ) const Op& op )
@ -107,24 +140,30 @@ namespace MultiArrayTools
private: private:
IType mIndex; IType mIndex;
Slice<T,SRanges...>* mSlPtr = nullptr; ConstSlice<T,SRanges...>& mSl;
ConstSlice<T,SRanges...>* mCSlPtr = nullptr;
SliceDef() = default; ConstSliceDef() = default;
public: public:
SliceDef(ConstSlice<T,SRanges...>& csl, ConstSliceDef(ConstSlice<T,SRanges...>& csl,
const std::shared_ptr<typename SRanges::IndexType>&... inds); const std::shared_ptr<typename SRanges::IndexType>&... inds);
SliceDef(Slice<T,SRanges...>& sl,
const std::shared_ptr<typename SRanges::IndexType>&... inds);
template <class... ORanges> template <class... ORanges>
SliceDef& operator=(const OperationRoot<T,ORanges...>& op); ConstSliceDef& operator=(const ConstOperationRoot<T,ORanges...>& op);
template <class... ORanges>
ConstSliceDef& operator=(const OperationRoot<T,ORanges...>& op);
}; };
template <typename T, class Op, class... Ranges> template <typename T, class Op, class... Ranges>
ConstSlice<T,Ranges...> mkSlice( const typename ConstSlice<T,Ranges...>::IndexType& ind, ConstSlice<T,Ranges...> mkSlice( const typename ConstSlice<T,Ranges...>::IndexType& ind,
const Op& op ) const Op& op )
{
return ConstSliceDef<T,Ranges...>::mkSlice(ind, op);
}
template <typename T, class Op, class... Ranges>
Slice<T,Ranges...> mkSlice( const typename Slice<T,Ranges...>::IndexType& ind,
const Op& op )
{ {
return SliceDef<T,Ranges...>::mkSlice(ind, op); return SliceDef<T,Ranges...>::mkSlice(ind, op);
} }
@ -220,9 +259,9 @@ namespace MultiArrayTools
template <typename T, class... SRanges> template <typename T, class... SRanges>
auto ConstSlice<T,SRanges...>::define(const std::shared_ptr<typename SRanges::IndexType>&... inds) auto ConstSlice<T,SRanges...>::define(const std::shared_ptr<typename SRanges::IndexType>&... inds)
-> SliceDef<T,SRanges...> -> ConstSliceDef<T,SRanges...>
{ {
return SliceDef<T,SRanges...>(*this, inds...); return ConstSliceDef<T,SRanges...>(*this, inds...);
} }
@ -330,20 +369,12 @@ namespace MultiArrayTools
return SliceDef<T,SRanges...>(*this, inds...); return SliceDef<T,SRanges...>(*this, inds...);
} }
template <typename T, class... SRanges>
SliceDef<T,SRanges...>::SliceDef(ConstSlice<T,SRanges...>& csl,
const std::shared_ptr<typename SRanges::IndexType>&... inds) :
mIndex(csl.begin()),
mCSlPtr(&csl)
{
mIndex(inds...);
}
template <typename T, class... SRanges> template <typename T, class... SRanges>
SliceDef<T,SRanges...>::SliceDef(Slice<T,SRanges...>& sl, SliceDef<T,SRanges...>::SliceDef(Slice<T,SRanges...>& sl,
const std::shared_ptr<typename SRanges::IndexType>&... inds) : const std::shared_ptr<typename SRanges::IndexType>&... inds) :
mIndex(sl.begin()), mIndex(sl.begin()),
mSlPtr(&sl) mSl(sl)
{ {
mIndex(inds...); mIndex(inds...);
} }
@ -355,14 +386,41 @@ namespace MultiArrayTools
std::array<size_t,sizeof...(SRanges)+1> blocks; std::array<size_t,sizeof...(SRanges)+1> blocks;
PackNum<sizeof...(SRanges)-1>:: PackNum<sizeof...(SRanges)-1>::
template mkSliceBlocks<T,OperationRoot<T,ORanges...>,SRanges...>(blocks, mIndex, op); template mkSliceBlocks<T,OperationRoot<T,ORanges...>,SRanges...>(blocks, mIndex, op);
if(mCSlPtr){ mSl.format(blocks);
mCSlPtr->format(blocks); mSl.mData = op.data();
mCSlPtr->mData = op.data(); return *this;
} }
else {
mSlPtr->format(blocks); template <typename T, class... SRanges>
mSlPtr->mData = op.data(); ConstSliceDef<T,SRanges...>::ConstSliceDef(ConstSlice<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>
ConstSliceDef<T,SRanges...>& ConstSliceDef<T,SRanges...>::operator=(const ConstOperationRoot<T,ORanges...>& op)
{
std::array<size_t,sizeof...(SRanges)+1> blocks;
PackNum<sizeof...(SRanges)-1>::
template mkSliceBlocks<T,ConstOperationRoot<T,ORanges...>,SRanges...>(blocks, mIndex, op);
mSl.format(blocks);
mSl.mData = op.data();
return *this;
}
template <typename T, class... SRanges>
template <class... ORanges>
ConstSliceDef<T,SRanges...>& ConstSliceDef<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);
mSl.mData = op.data();
return *this; return *this;
} }