some fixes (expressions) + dynamic <-> anonymous cast utilities

This commit is contained in:
Christian Zimmermann 2018-11-03 22:03:35 +01:00
parent 69e4e4bf8e
commit 3c653535bc
8 changed files with 137 additions and 5 deletions

View file

@ -131,4 +131,73 @@ namespace MultiArrayTools
}
}
namespace
{
template <size_t N>
struct CopyRanges
{
template <class Space1, class Space2>
static inline void exec(const Space1& space1, Space2& space2)
{
std::get<N>(space2) = std::get<N>(space1);
CopyRanges<N-1>::exec(space1,space2);
}
};
template <>
struct CopyRanges<0>
{
template <class Space1, class Space2>
static inline void exec(const Space1& space1, Space2& space2)
{
std::get<0>(space2) = std::get<0>(space1);
}
};
}
template <typename T, class EC, class Range1, class... RangeTypes>
auto anonToDynView(const MultiArray<T,Range1,RangeTypes...,AnonymousRange>& ma)
-> ConstSlice<T,Range1,RangeTypes...,DynamicRange<EC>>
{
constexpr size_t LAST = sizeof...(RangeTypes)+1;
DynamicRangeFactory<EC> drf(rptr<LAST>(ma)->orig());
std::tuple<std::shared_ptr<Range1>,std::shared_ptr<RangeTypes>...,
std::shared_ptr<DynamicRange<EC>>> mNSpace;
CopyRanges<LAST-1>::exec(ma.range()->space(),mNSpace);
std::get<LAST>(mNSpace) = createExplicit( drf );
return ConstSlice<T,Range1,RangeTypes...,DynamicRange<EC>>(mNSpace, ma.data());
}
template <typename T, class EC, class Range1, class... RangeTypes>
auto dynToAnonMove(MultiArray<T,Range1,RangeTypes...,DynamicRange<EC>>&& ma)
-> MultiArray<T,Range1,RangeTypes...,AnonymousRange>
{
constexpr size_t LAST = sizeof...(RangeTypes)+1;
AnonymousRangeFactory arf(rptr<LAST>(ma)->orig());
std::tuple<std::shared_ptr<Range1>,std::shared_ptr<RangeTypes>...,
std::shared_ptr<AnonymousRange>> mNSpace;
CopyRanges<LAST-1>::exec(ma.range()->space(),mNSpace);
std::get<LAST>(mNSpace) = createExplicit( arf );
return ma.format(mNSpace);
}
template <typename T, class EC>
auto anonToDynView(const MultiArray<T,AnonymousRange>& ma)
-> ConstSlice<T,DynamicRange<EC>>
{
DynamicRangeFactory<EC> drf(rptr<0>(ma)->orig());
auto mNSpace = std::make_tuple( createExplicit( drf ) );
return ConstSlice<T,DynamicRange<EC>>(mNSpace, ma.data());
}
template <typename T, class EC>
auto dynToAnonMove(MultiArray<T,DynamicRange<EC>>&& ma)
-> MultiArray<T,AnonymousRange>
{
AnonymousRangeFactory arf(rptr<0>(ma)->orig());
auto mNSpace = std::make_tuple( createExplicit( arf ) );
return ma.format(mNSpace);
}
}

View file

@ -56,13 +56,29 @@ namespace MultiArrayTools
-> std::shared_ptr<Range>;
template <size_t N, class MArray>
auto prtr(const MArray& ma)
auto rptr(const MArray& ma)
-> decltype(ma.template getRangePtr<N>());
template <class EC, class MArray>
auto dynamic(const MArray& ma, bool slice = false)
-> std::shared_ptr<MultiArrayBase<typename MArray::value_type,DynamicRange<EC>>>;
template <typename T, class EC, class Range1, class... RangeTypes>
auto anonToDynView(const MultiArray<T,Range1,RangeTypes...,AnonymousRange>& ma)
-> ConstSlice<T,Range1,RangeTypes...,DynamicRange<EC>>;
template <typename T, class EC, class Range1, class... RangeTypes>
auto dynToAnonMove(MultiArray<T,Range1,RangeTypes...,DynamicRange<EC>>&& ma)
-> MultiArray<T,Range1,RangeTypes...,AnonymousRange>;
template <typename T, class EC>
auto anonToDynView(const MultiArray<T,AnonymousRange>& ma)
-> ConstSlice<T,DynamicRange<EC>>;
template <typename T, class EC>
auto dynToAnonMove(MultiArray<T,DynamicRange<EC>>&& ma)
-> MultiArray<T,AnonymousRange>;
template <class IndexType>
inline void For(const std::shared_ptr<IndexType>& ind, const std::function<void(void)>& ll)
{

View file

@ -152,9 +152,18 @@ namespace MultiArrayTools
template <class... SRanges2>
MultiArray<T,SRanges2...> MultiArray<T,SRanges...>::format(const std::shared_ptr<SRanges2>&... nrs)
{
MAB::mInit = false;
return MultiArray<T,SRanges2...>( nrs... , std::move(mCont) );
}
template <typename T, class... SRanges>
template <class... SRanges2>
MultiArray<T,SRanges2...> MultiArray<T,SRanges...>::format(const std::tuple<std::shared_ptr<SRanges2>...>& nrs)
{
MAB::mInit = false;
return MultiArray<T,SRanges2...>( nrs , std::move(mCont) );
}
template <typename T, class... SRanges>
const T* MultiArray<T,SRanges...>::data() const
{

View file

@ -67,6 +67,9 @@ namespace MultiArrayTools
template <class... SRanges2>
MultiArray<T,SRanges2...> format(const std::shared_ptr<SRanges2>&... nrs); // reformat array using 'nr' which in
// total must have the same size as mRange
template <class... SRanges2>
MultiArray<T,SRanges2...> format(const std::tuple<std::shared_ptr<SRanges2>...>& nrs);
virtual const T* data() const override;
virtual T* data() override;

View file

@ -36,6 +36,8 @@ namespace MultiArrayTools
template <class... RangeTypes>
AnonymousRangeFactory(std::shared_ptr<RangeTypes>... origs);
AnonymousRangeFactory(const std::vector<std::shared_ptr<RangeBase>>& origs);
template <class Range>
void append(std::shared_ptr<Range> r);
@ -110,6 +112,8 @@ namespace MultiArrayTools
template <class... RangeTypes>
SingleRange(std::shared_ptr<RangeTypes>... origs);
SingleRange(const std::vector<std::shared_ptr<RangeBase>>& origs);
size_t mSize = 1;
bool mEmpty = true;
@ -218,7 +222,7 @@ namespace MultiArrayTools
mEmpty = false;
}
}
template <class Range>
std::shared_ptr<Range> SingleRange<size_t,SpaceType::ANON>::fullsub(size_t num) const
{

View file

@ -13,6 +13,15 @@ namespace MultiArrayTools
{
MAB::mProtoI->format(blocks);
}
template <typename T, class... SRanges>
ConstSlice<T,SRanges...>::ConstSlice(const std::tuple<std::shared_ptr<SRanges>...>& ranges,
const T* data) :
MultiArrayBase<T,SRanges...>(ranges),
mData(data)
{
MAB::mInit = true;
}
template <typename T, class... SRanges>
ConstSlice<T,SRanges...>::ConstSlice(const std::shared_ptr<SRanges>&... ranges, const T* data) :

View file

@ -20,6 +20,8 @@ namespace MultiArrayTools
DEFAULT_MEMBERS(ConstSlice);
ConstSlice(const std::tuple<std::shared_ptr<SRanges>...>& ranges,
const T* data = nullptr);
ConstSlice(const std::shared_ptr<SRanges>&... ranges, const T* data = nullptr);
ConstSlice(const MultiArrayBase<T,AnonymousRange>& ma, SIZET<SRanges>... sizes);

View file

@ -11,8 +11,14 @@ namespace MultiArrayTools
{
mProd = std::shared_ptr<oType>( new AnonymousRange() );
}
std::map<std::shared_ptr<RangeBase>,std::vector<std::intptr_t> > AnonymousRangeFactory::mAleadyCreated;
AnonymousRangeFactory::AnonymousRangeFactory(const std::vector<std::shared_ptr<RangeBase>>& origs)
{
mProd = std::shared_ptr<oType>( new AnonymousRange( origs ) );
}
std::shared_ptr<RangeBase> AnonymousRangeFactory::checkIfCreated(const std::vector<std::shared_ptr<RangeBase> >& pvec)
{
@ -157,7 +163,21 @@ namespace MultiArrayTools
{
return mOrig;
}
SingleRange<size_t,SpaceType::ANON>::SingleRange(const std::vector<std::shared_ptr<RangeBase>>& origs) :
RangeInterface<AnonymousIndex>(),
mOrig(origs)
{
mSize = 1;
for(auto& x: mOrig){
mSize *= x->size();
}
if(mOrig.size()){
mEmpty = false;
}
}
/*****************
* Functions *
*****************/