slice code seems to compile

This commit is contained in:
Christian Zimmermann 2018-03-05 00:04:50 +01:00
parent 10f6f75552
commit 4df82f263e
9 changed files with 117 additions and 41 deletions

View file

@ -3,6 +3,7 @@
#define __helper_tools_h__ #define __helper_tools_h__
#include "base_def.h" #include "base_def.h"
#include "slice.h"
namespace MultiArrayTools namespace MultiArrayTools
{ {
@ -23,7 +24,9 @@ namespace MultiArrayTools
auto mkMIndex(std::shared_ptr<IndexTypes>... indices) auto mkMIndex(std::shared_ptr<IndexTypes>... indices)
-> decltype( getIndex( mkMulti( indices.range()... ) ) ); -> decltype( getIndex( mkMulti( indices.range()... ) ) );
template <class... RangeTypes>
auto mkMulti(std::tuple<std::shared_ptr<RangeTypes>...> rangesTuple)
-> MultiRange<RangeTypes...>;
} }
/* ========================= * /* ========================= *
@ -66,6 +69,15 @@ namespace MultiArrayTools
(*mi)( indices... ); (*mi)( indices... );
return mi; return mi;
} }
template <class... RangeTypes>
auto mkMulti(std::tuple<std::shared_ptr<RangeTypes>...> rangesTuple)
-> MultiRange<RangeTypes...>
{
MultiRangeFactory<RangeTypes...> mrf( rangesTuple );
return std::dynamic_pointer_cast<MultiRange<RangeTypes...> >( mrf.create() );
}
} }
#endif #endif

View file

@ -55,8 +55,6 @@ namespace MultiArrayTools
virtual bool isInit() const; virtual bool isInit() const;
// slice function !!!!!
protected: protected:
bool mInit = false; bool mInit = false;
std::shared_ptr<CRange> mRange; std::shared_ptr<CRange> mRange;
@ -116,7 +114,7 @@ namespace MultiArrayTools
{ {
ContainerRangeFactory<T,SRanges...> crf(ranges...); ContainerRangeFactory<T,SRanges...> crf(ranges...);
mRange = std::dynamic_pointer_cast<ContainerRange<T,SRanges...> >( crf.create() ); mRange = std::dynamic_pointer_cast<ContainerRange<T,SRanges...> >( crf.create() );
mProtoI = std::make_shared<IndexType>( mRange ); mProtoI = std::make_shared<IndexType>( mRange, reinterpret_cast<std::intptr_t>(this) );
} }
template <typename T, class... SRanges> template <typename T, class... SRanges>
@ -124,7 +122,7 @@ namespace MultiArrayTools
{ {
ContainerRangeFactory<T,SRanges...> crf(space); ContainerRangeFactory<T,SRanges...> crf(space);
mRange = std::dynamic_pointer_cast<ContainerRange<T,SRanges...> >( crf.create() ); mRange = std::dynamic_pointer_cast<ContainerRange<T,SRanges...> >( crf.create() );
mProtoI = std::make_shared<IndexType>( mRange ); mProtoI = std::make_shared<IndexType>( mRange, reinterpret_cast<std::intptr_t>(this) );
} }
template <typename T, class... SRanges> template <typename T, class... SRanges>

View file

@ -41,6 +41,7 @@ namespace MultiArrayTools
IndexPack mIPack; IndexPack mIPack;
std::array<size_t,sizeof...(Indices)+1> mBlockSizes; std::array<size_t,sizeof...(Indices)+1> mBlockSizes;
const T* mData; const T* mData;
std::intptr_t mObjPtrNum;
public: public:
@ -48,10 +49,12 @@ namespace MultiArrayTools
ContainerIndex& operator=(const ContainerIndex& in) = default; ContainerIndex& operator=(const ContainerIndex& in) = default;
template <class MRange> template <class MRange>
ContainerIndex(const std::shared_ptr<MRange>& range); ContainerIndex(const std::shared_ptr<MRange>& range,
std::intptr_t objPtrNum);
template <class MRange> template <class MRange>
ContainerIndex(const std::shared_ptr<MRange>& range, ContainerIndex(const std::shared_ptr<MRange>& range,
std::intptr_t objPtrNum,
const std::array<size_t,sizeof...(Indices)+1>& blockSizes); const std::array<size_t,sizeof...(Indices)+1>& blockSizes);
template <size_t N> template <size_t N>
@ -104,6 +107,9 @@ namespace MultiArrayTools
auto iforh(Exprs exs) const auto iforh(Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs)); -> decltype(RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs));
std::intptr_t container() const;
ContainerIndex& format(const std::array<size_t,sizeof...(Indices)+1>& blocks);
// Iterator Stuff // Iterator Stuff
ContainerIndex& setData(const T* data); ContainerIndex& setData(const T* data);
@ -212,8 +218,10 @@ namespace MultiArrayTools
template <typename T, class... Indices> template <typename T, class... Indices>
template <class MRange> template <class MRange>
ContainerIndex<T,Indices...>::ContainerIndex(const std::shared_ptr<MRange>& range) : ContainerIndex<T,Indices...>::ContainerIndex(const std::shared_ptr<MRange>& range,
IndexInterface<ContainerIndex<T,Indices...>,std::tuple<typename Indices::MetaType...> >(range, 0) std::intptr_t objPtrNum) :
IndexInterface<ContainerIndex<T,Indices...>,std::tuple<typename Indices::MetaType...> >(range, 0),
mObjPtrNum(objPtrNum)
{ {
RPackNum<sizeof...(Indices)-1>::construct(mIPack, *range); RPackNum<sizeof...(Indices)-1>::construct(mIPack, *range);
std::get<sizeof...(Indices)>(mBlockSizes) = 1; std::get<sizeof...(Indices)>(mBlockSizes) = 1;
@ -224,8 +232,10 @@ namespace MultiArrayTools
template <typename T, class... Indices> template <typename T, class... Indices>
template <class MRange> template <class MRange>
ContainerIndex<T,Indices...>::ContainerIndex(const std::shared_ptr<MRange>& range, ContainerIndex<T,Indices...>::ContainerIndex(const std::shared_ptr<MRange>& range,
std::intptr_t objPtrNum,
const std::array<size_t,sizeof...(Indices)+1>& blockSizes) : const std::array<size_t,sizeof...(Indices)+1>& blockSizes) :
IndexInterface<ContainerIndex<T,Indices...>,std::tuple<typename Indices::MetaType...> >(range, 0) IndexInterface<ContainerIndex<T,Indices...>,std::tuple<typename Indices::MetaType...> >(range, 0),
mObjPtrNum(objPtrNum)
{ {
RPackNum<sizeof...(Indices)-1>::construct(mIPack, *range); RPackNum<sizeof...(Indices)-1>::construct(mIPack, *range);
mBlockSizes = blockSizes; mBlockSizes = blockSizes;
@ -422,6 +432,20 @@ namespace MultiArrayTools
return RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs); return RPackNum<sizeof...(Indices)-1>::mkForh(mIPack, exs);
} }
template <typename T, class... Indices>
std::intptr_t ContainerIndex<T,Indices...>::container() const
{
return mObjPtrNum;
}
template <typename T, class... Indices>
ContainerIndex<T,Indices...>& ContainerIndex<T,Indices...>::
format(const std::array<size_t,sizeof...(Indices)+1>& blocks)
{
mBlockSizes = blocks;
return *this;
}
template <typename T, class... Indices> template <typename T, class... Indices>
ContainerIndex<T,Indices...>& ContainerIndex<T,Indices...>::setData(const T* data) ContainerIndex<T,Indices...>& ContainerIndex<T,Indices...>::setData(const T* data)
{ {

View file

@ -31,6 +31,7 @@ namespace MultiArrayTools
typedef std::tuple<std::shared_ptr<Indices>...> IndexPack; typedef std::tuple<std::shared_ptr<Indices>...> IndexPack;
typedef std::tuple<typename Indices::MetaType...> MetaType; typedef std::tuple<typename Indices::MetaType...> MetaType;
typedef MultiRange<typename Indices::RangeType...> RangeType; typedef MultiRange<typename Indices::RangeType...> RangeType;
typedef MultiIndex IType;
static constexpr IndexType sType() { return IndexType::MULTI; } static constexpr IndexType sType() { return IndexType::MULTI; }
static constexpr size_t sDim() { return sizeof...(Indices); } static constexpr size_t sDim() { return sizeof...(Indices); }
@ -144,6 +145,7 @@ namespace MultiArrayTools
typedef RangeBase RB; typedef RangeBase RB;
typedef std::tuple<std::shared_ptr<Ranges>...> SpaceType; typedef std::tuple<std::shared_ptr<Ranges>...> SpaceType;
typedef MultiIndex<typename Ranges::IndexType...> IndexType; typedef MultiIndex<typename Ranges::IndexType...> IndexType;
typedef MultiRange RangeType;
//typedef typename RangeInterface<MultiIndex<typename Ranges::IndexType...> >::IndexType IndexType; //typedef typename RangeInterface<MultiIndex<typename Ranges::IndexType...> >::IndexType IndexType;
protected: protected:

View file

@ -28,6 +28,7 @@ namespace MultiArrayTools
public: public:
typedef RangeBase RB; typedef RangeBase RB;
typedef typename RangeInterface<SingleIndex<size_t,SpaceType::NONE> >::IndexType IndexType; typedef typename RangeInterface<SingleIndex<size_t,SpaceType::NONE> >::IndexType IndexType;
typedef SingleRange<size_t,SpaceType::NONE> RangeType;
virtual size_t size() const override; virtual size_t size() const override;
virtual size_t dim() const override; virtual size_t dim() const override;

View file

@ -32,6 +32,7 @@ namespace MultiArrayTools
public: public:
typedef RangeBase RB; typedef RangeBase RB;
typedef typename RangeInterface<SingleIndex<size_t,SpaceType::SPIN> >::IndexType IndexType; typedef typename RangeInterface<SingleIndex<size_t,SpaceType::SPIN> >::IndexType IndexType;
typedef SingleRange<size_t,SpaceType::SPIN> RangeType;
virtual size_t size() const override; virtual size_t size() const override;
virtual size_t dim() const override; virtual size_t dim() const override;

View file

@ -26,6 +26,7 @@ namespace MultiArrayTools
typedef IndexInterface<SingleIndex<U,TYPE>,U> IB; typedef IndexInterface<SingleIndex<U,TYPE>,U> IB;
typedef U MetaType; typedef U MetaType;
typedef SingleRange<U,TYPE> RangeType; typedef SingleRange<U,TYPE> RangeType;
typedef SingleIndex IType;
//DEFAULT_MEMBERS_X(SingleIndex); //DEFAULT_MEMBERS_X(SingleIndex);
@ -92,6 +93,7 @@ namespace MultiArrayTools
public: public:
typedef RangeBase RB; typedef RangeBase RB;
typedef SingleIndex<U,TYPE> IndexType; typedef SingleIndex<U,TYPE> IndexType;
typedef SingleRange RangeType;
//typedef typename RangeInterface<SingleIndex<U,TYPE> >::IndexType IndexType; //typedef typename RangeInterface<SingleIndex<U,TYPE> >::IndexType IndexType;
virtual size_t size() const override; virtual size_t size() const override;

View file

@ -14,30 +14,30 @@ namespace MultiArrayTools
typedef ContainerRange<T,SRanges...> CRange; typedef ContainerRange<T,SRanges...> CRange;
typedef MultiArrayBase<T,SRanges...> MAB; typedef MultiArrayBase<T,SRanges...> MAB;
typedef typename CRange::IndexType IndexType; typedef typename CRange::IndexType IType;
DEFAULT_MEMBERS(Slice); DEFAULT_MEMBERS(Slice);
template <class... RITypes> // Range / Index <-> open / const // RITypes = Range XOR Index depending on whether const XOR opern
Slice(T* data, const RITypes&... ris); template <class... RITypes>
Slice(MutableMultiArrayBase<T,typename RITypes::RangeType...>& ma,
const std::shared_ptr<RITypes>&... ris);
virtual const T& operator[](const IndexType& i) const override; virtual const T& operator[](const IType& i) const override;
virtual T& operator[](const IndexType& i) override; virtual T& operator[](const IType& i) override;
virtual const T& at(const typename CRange::IndexType::MetaType& meta) const override; virtual const T& at(const typename IType::MetaType& meta) const override;
virtual T& at(const typename CRange::IndexType::MetaType& meta) override; virtual T& at(const typename IType::MetaType& meta) override;
virtual const T* data() const override; virtual const T* data() const override;
virtual T* data() override; virtual T* data() override;
virtual bool isSlice() const override; virtual bool isSlice() const override;
virtual IndexType begin() const override; virtual auto begin() const -> IType override;
virtual IndexType end() const override; virtual auto end() const -> IType override;
private: private:
T* mData; T* mData;
size_t mStartPos;
std::array<size_t,sizeof...(SRange)+1> mBlockSizes;
}; };
} // end namespace MultiArrayTools } // end namespace MultiArrayTools
@ -52,6 +52,17 @@ namespace MultiArrayTools
namespace namespace
{ {
//size_t sum(size_t arg)
//{
// return arg;
//}
template <typename... SizeTypes>
size_t sum(size_t arg, SizeTypes... args)
{
return arg + sum(args...);
}
template <bool ISINDEX> template <bool ISINDEX>
struct XX struct XX
{ {
@ -61,6 +72,12 @@ namespace MultiArrayTools
{ {
return std::make_tuple(ri); return std::make_tuple(ri);
} }
template <class RI>
static size_t ri_to_start_pos(const std::shared_ptr<RI>& ri)
{
return 0;
}
}; };
template <> template <>
@ -72,52 +89,69 @@ namespace MultiArrayTools
{ {
return std::make_tuple(); return std::make_tuple();
} }
template <class RI>
static size_t ri_to_start_pos(const std::shared_ptr<RI>& ri)
{
return ri->pos();
}
}; };
template <class... RITypes> template <class... RITypes>
auto mkSliceRange(const std::shared_ptr<RITypes>&... ris) auto mkSliceRange(const std::shared_ptr<RITypes>&... ris)
-> decltype(std::tuple_cat(XX<RITypes::ISINDEX>::ri_to_tuple(ris)...))
{ {
return std::tuple_cat(XX<RITypes::ISINDEX>::ri_to_tuple(ris)...); return std::tuple_cat(XX<RITypes::ISINDEX>::ri_to_tuple(ris)...);
} }
template <class... RITypes>
size_t mkStartPos(const std::shared_ptr<RITypes>&... ris)
{
return sum(ri_to_start_pos(ris)...);
}
} }
/************* /*************
* Slice * * Slice *
*************/ *************/
/*
template <typename T, class... SRanges> template <typename T, class... SRanges>
Slice<T,SRanges...>::Slice(T* data, const RITypes&... ris) : template <class... RITypes>
Slice<T,SRanges...>::Slice(MutableMultiArrayBase<T,typename RITypes::RangeType...>& ma,
const std::shared_ptr<RITypes>&... ris) :
MutableMultiArrayBase<T,SRanges...>( mkSliceRange(ris...) ), MutableMultiArrayBase<T,SRanges...>( mkSliceRange(ris...) ),
mData(data), mData(ma.data() + mkStartPos(ris...))
mStartPos(mkSliceStart(ris...)), {
mBlockSizes(mkSliceBlocks(ris...)) {} MAB::mProtoI.format( mBlockSizes(mkSliceBlocks(ris...)) );
*/ //!!!!! }
template <typename T, class... SRanges> template <typename T, class... SRanges>
const T& Slice<T,SRanges...>::operator[](const IndexType& i) const 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() ]; return mData[ i.pos() ];
} }
template <typename T, class... SRanges> template <typename T, class... SRanges>
T& Slice<T,SRanges...>::operator[](const IndexType& i) 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() ]; return mData[ i.pos() ];
} }
template <typename T, class... SRanges> template <typename T, class... SRanges>
const T& Slice<T,SRanges...>::at(const typename CRange::IndexType::MetaType& meta) const const T& Slice<T,SRanges...>::at(const typename IType::MetaType& meta) const
{ {
assert(i.sliceMode());
return mData[ begin().at(meta).pos() ]; return mData[ begin().at(meta).pos() ];
} }
template <typename T, class... SRanges> template <typename T, class... SRanges>
T& Slice<T,SRanges...>::at(const typename CRange::IndexType::MetaType& meta) T& Slice<T,SRanges...>::at(const typename IType::MetaType& meta)
{ {
assert(i.sliceMode());
return mData[ begin().at(meta).pos() ]; return mData[ begin().at(meta).pos() ];
} }
@ -140,18 +174,20 @@ namespace MultiArrayTools
} }
template <typename T, class... SRanges> template <typename T, class... SRanges>
IndexType Slice<T,SRanges...>::begin() const auto Slice<T,SRanges...>::begin() const -> Slice<T,SRanges...>::IType
{ {
IndexType i(mRange, mBlockSizes); IType i = MAB::mProtoI;
i = mStartPos; i = 0;
//i = mStartPos;
return i.setData(data()); return i.setData(data());
} }
template <typename T, class... SRanges> template <typename T, class... SRanges>
IndexType Slice<T,SRanges...>::end() const auto Slice<T,SRanges...>::end() const -> Slice<T,SRanges...>::IType
{ {
IndexType i(mRange, mBlockSizes); IType i = MAB::mProtoI;
i = std::get<sizeof...(SRanges)>(mBlockSizes); i = i.max(); // CHECK !!!
//i = std::get<sizeof...(SRanges)>(mBlockSizes);
return i.setData(data()); return i.setData(data());
} }

View file

@ -184,10 +184,10 @@ namespace {
auto mi = mstrptr->begin(); auto mi = mstrptr->begin();
//auto ci1 = cr1ptr->begin(); //auto ci1 = cr1ptr->begin();
CIndex ci1(cr1ptr); CIndex ci1(cr1ptr, 0);
ci1 = 0; ci1 = 0;
//auto ci2 = cr2ptr->begin(); //auto ci2 = cr2ptr->begin();
CIndex ci2(cr2ptr); CIndex ci2(cr2ptr, 0);
ci2 = 0; ci2 = 0;
EXPECT_EQ(ci1.max(), 16u); EXPECT_EQ(ci1.max(), 16u);