diff --git a/src/include/helper_tools.h b/src/include/helper_tools.h index e133713..e88f4b5 100644 --- a/src/include/helper_tools.h +++ b/src/include/helper_tools.h @@ -3,6 +3,7 @@ #define __helper_tools_h__ #include "base_def.h" +#include "slice.h" namespace MultiArrayTools { @@ -23,7 +24,9 @@ namespace MultiArrayTools auto mkMIndex(std::shared_ptr... indices) -> decltype( getIndex( mkMulti( indices.range()... ) ) ); - + template + auto mkMulti(std::tuple...> rangesTuple) + -> MultiRange; } /* ========================= * @@ -66,6 +69,15 @@ namespace MultiArrayTools (*mi)( indices... ); return mi; } + + template + auto mkMulti(std::tuple...> rangesTuple) + -> MultiRange + { + MultiRangeFactory mrf( rangesTuple ); + return std::dynamic_pointer_cast >( mrf.create() ); + } + } #endif diff --git a/src/include/multi_array_base.h b/src/include/multi_array_base.h index c8c93f5..b0fc699 100644 --- a/src/include/multi_array_base.h +++ b/src/include/multi_array_base.h @@ -54,8 +54,6 @@ namespace MultiArrayTools operator()(std::shared_ptr&... inds) const; virtual bool isInit() const; - - // slice function !!!!! protected: bool mInit = false; @@ -116,7 +114,7 @@ namespace MultiArrayTools { ContainerRangeFactory crf(ranges...); mRange = std::dynamic_pointer_cast >( crf.create() ); - mProtoI = std::make_shared( mRange ); + mProtoI = std::make_shared( mRange, reinterpret_cast(this) ); } template @@ -124,7 +122,7 @@ namespace MultiArrayTools { ContainerRangeFactory crf(space); mRange = std::dynamic_pointer_cast >( crf.create() ); - mProtoI = std::make_shared( mRange ); + mProtoI = std::make_shared( mRange, reinterpret_cast(this) ); } template diff --git a/src/include/ranges/container_range.h b/src/include/ranges/container_range.h index a17c4e4..6755c48 100644 --- a/src/include/ranges/container_range.h +++ b/src/include/ranges/container_range.h @@ -41,6 +41,7 @@ namespace MultiArrayTools IndexPack mIPack; std::array mBlockSizes; const T* mData; + std::intptr_t mObjPtrNum; public: @@ -48,10 +49,12 @@ namespace MultiArrayTools ContainerIndex& operator=(const ContainerIndex& in) = default; template - ContainerIndex(const std::shared_ptr& range); + ContainerIndex(const std::shared_ptr& range, + std::intptr_t objPtrNum); template ContainerIndex(const std::shared_ptr& range, + std::intptr_t objPtrNum, const std::array& blockSizes); template @@ -104,6 +107,9 @@ namespace MultiArrayTools auto iforh(Exprs exs) const -> decltype(RPackNum::mkForh(mIPack, exs)); + std::intptr_t container() const; + ContainerIndex& format(const std::array& blocks); + // Iterator Stuff ContainerIndex& setData(const T* data); @@ -212,8 +218,10 @@ namespace MultiArrayTools template template - ContainerIndex::ContainerIndex(const std::shared_ptr& range) : - IndexInterface,std::tuple >(range, 0) + ContainerIndex::ContainerIndex(const std::shared_ptr& range, + std::intptr_t objPtrNum) : + IndexInterface,std::tuple >(range, 0), + mObjPtrNum(objPtrNum) { RPackNum::construct(mIPack, *range); std::get(mBlockSizes) = 1; @@ -224,8 +232,10 @@ namespace MultiArrayTools template template ContainerIndex::ContainerIndex(const std::shared_ptr& range, + std::intptr_t objPtrNum, const std::array& blockSizes) : - IndexInterface,std::tuple >(range, 0) + IndexInterface,std::tuple >(range, 0), + mObjPtrNum(objPtrNum) { RPackNum::construct(mIPack, *range); mBlockSizes = blockSizes; @@ -421,6 +431,20 @@ namespace MultiArrayTools { return RPackNum::mkForh(mIPack, exs); } + + template + std::intptr_t ContainerIndex::container() const + { + return mObjPtrNum; + } + + template + ContainerIndex& ContainerIndex:: + format(const std::array& blocks) + { + mBlockSizes = blocks; + return *this; + } template ContainerIndex& ContainerIndex::setData(const T* data) diff --git a/src/include/ranges/multi_range.h b/src/include/ranges/multi_range.h index 4e8b730..8ce6396 100644 --- a/src/include/ranges/multi_range.h +++ b/src/include/ranges/multi_range.h @@ -31,6 +31,7 @@ namespace MultiArrayTools typedef std::tuple...> IndexPack; typedef std::tuple MetaType; typedef MultiRange RangeType; + typedef MultiIndex IType; static constexpr IndexType sType() { return IndexType::MULTI; } static constexpr size_t sDim() { return sizeof...(Indices); } @@ -143,7 +144,8 @@ namespace MultiArrayTools public: typedef RangeBase RB; typedef std::tuple...> SpaceType; - typedef MultiIndex IndexType; + typedef MultiIndex IndexType; + typedef MultiRange RangeType; //typedef typename RangeInterface >::IndexType IndexType; protected: diff --git a/src/include/ranges/range_types/classic_range.h b/src/include/ranges/range_types/classic_range.h index 5ff0649..807cc02 100644 --- a/src/include/ranges/range_types/classic_range.h +++ b/src/include/ranges/range_types/classic_range.h @@ -28,6 +28,7 @@ namespace MultiArrayTools public: typedef RangeBase RB; typedef typename RangeInterface >::IndexType IndexType; + typedef SingleRange RangeType; virtual size_t size() const override; virtual size_t dim() const override; diff --git a/src/include/ranges/range_types/spin_range.h b/src/include/ranges/range_types/spin_range.h index c9215f2..c21de74 100644 --- a/src/include/ranges/range_types/spin_range.h +++ b/src/include/ranges/range_types/spin_range.h @@ -32,6 +32,7 @@ namespace MultiArrayTools public: typedef RangeBase RB; typedef typename RangeInterface >::IndexType IndexType; + typedef SingleRange RangeType; virtual size_t size() const override; virtual size_t dim() const override; diff --git a/src/include/ranges/single_range.h b/src/include/ranges/single_range.h index 75e2b79..c23d10d 100644 --- a/src/include/ranges/single_range.h +++ b/src/include/ranges/single_range.h @@ -26,6 +26,7 @@ namespace MultiArrayTools typedef IndexInterface,U> IB; typedef U MetaType; typedef SingleRange RangeType; + typedef SingleIndex IType; //DEFAULT_MEMBERS_X(SingleIndex); @@ -92,6 +93,7 @@ namespace MultiArrayTools public: typedef RangeBase RB; typedef SingleIndex IndexType; + typedef SingleRange RangeType; //typedef typename RangeInterface >::IndexType IndexType; virtual size_t size() const override; diff --git a/src/include/slice.h b/src/include/slice.h index de6bfa0..f68d340 100644 --- a/src/include/slice.h +++ b/src/include/slice.h @@ -14,30 +14,30 @@ namespace MultiArrayTools typedef ContainerRange CRange; typedef MultiArrayBase MAB; - typedef typename CRange::IndexType IndexType; + typedef typename CRange::IndexType IType; DEFAULT_MEMBERS(Slice); - template // Range / Index <-> open / const - Slice(T* data, const RITypes&... ris); + // RITypes = Range XOR Index depending on whether const XOR opern + template + Slice(MutableMultiArrayBase& ma, + const std::shared_ptr&... ris); - virtual const T& operator[](const IndexType& i) const override; - virtual T& operator[](const IndexType& i) override; - virtual const T& at(const typename CRange::IndexType::MetaType& meta) const override; - virtual T& at(const typename CRange::IndexType::MetaType& meta) override; + virtual const T& operator[](const IType& i) const override; + virtual T& operator[](const IType& i) override; + virtual const T& at(const typename IType::MetaType& meta) const override; + virtual T& at(const typename IType::MetaType& meta) override; virtual const T* data() const override; virtual T* data() override; virtual bool isSlice() const override; - virtual IndexType begin() const override; - virtual IndexType end() const override; + virtual auto begin() const -> IType override; + virtual auto end() const -> IType override; private: T* mData; - size_t mStartPos; - std::array mBlockSizes; }; } // end namespace MultiArrayTools @@ -52,6 +52,17 @@ namespace MultiArrayTools namespace { + //size_t sum(size_t arg) + //{ + // return arg; + //} + + template + size_t sum(size_t arg, SizeTypes... args) + { + return arg + sum(args...); + } + template struct XX { @@ -61,6 +72,12 @@ namespace MultiArrayTools { return std::make_tuple(ri); } + + template + static size_t ri_to_start_pos(const std::shared_ptr& ri) + { + return 0; + } }; template <> @@ -72,52 +89,69 @@ namespace MultiArrayTools { return std::make_tuple(); } + + template + static size_t ri_to_start_pos(const std::shared_ptr& ri) + { + return ri->pos(); + } + }; template auto mkSliceRange(const std::shared_ptr&... ris) + -> decltype(std::tuple_cat(XX::ri_to_tuple(ris)...)) { return std::tuple_cat(XX::ri_to_tuple(ris)...); } + template + size_t mkStartPos(const std::shared_ptr&... ris) + { + return sum(ri_to_start_pos(ris)...); + } + } /************* * Slice * *************/ - /* + template - Slice::Slice(T* data, const RITypes&... ris) : + template + Slice::Slice(MutableMultiArrayBase& ma, + const std::shared_ptr&... ris) : MutableMultiArrayBase( mkSliceRange(ris...) ), - mData(data), - mStartPos(mkSliceStart(ris...)), - mBlockSizes(mkSliceBlocks(ris...)) {} - */ //!!!!! + mData(ma.data() + mkStartPos(ris...)) + { + MAB::mProtoI.format( mBlockSizes(mkSliceBlocks(ris...)) ); + } + template - const T& Slice::operator[](const IndexType& i) const + const T& Slice::operator[](const IType& i) const { assert(i.sliceMode()); // -> compare objects !!!!! + assert(i.container() == reinterpret_cast(this)); return mData[ i.pos() ]; } template - T& Slice::operator[](const IndexType& i) + T& Slice::operator[](const IType& i) { assert(i.sliceMode()); + assert(i.container() == reinterpret_cast(this)); return mData[ i.pos() ]; } template - const T& Slice::at(const typename CRange::IndexType::MetaType& meta) const + const T& Slice::at(const typename IType::MetaType& meta) const { - assert(i.sliceMode()); return mData[ begin().at(meta).pos() ]; } template - T& Slice::at(const typename CRange::IndexType::MetaType& meta) + T& Slice::at(const typename IType::MetaType& meta) { - assert(i.sliceMode()); return mData[ begin().at(meta).pos() ]; } @@ -140,18 +174,20 @@ namespace MultiArrayTools } template - IndexType Slice::begin() const + auto Slice::begin() const -> Slice::IType { - IndexType i(mRange, mBlockSizes); - i = mStartPos; + IType i = MAB::mProtoI; + i = 0; + //i = mStartPos; return i.setData(data()); } template - IndexType Slice::end() const + auto Slice::end() const -> Slice::IType { - IndexType i(mRange, mBlockSizes); - i = std::get(mBlockSizes); + IType i = MAB::mProtoI; + i = i.max(); // CHECK !!! + //i = std::get(mBlockSizes); return i.setData(data()); } diff --git a/src/tests/ranges/index_unit_test.cc b/src/tests/ranges/index_unit_test.cc index 41bac60..d4289f6 100644 --- a/src/tests/ranges/index_unit_test.cc +++ b/src/tests/ranges/index_unit_test.cc @@ -184,10 +184,10 @@ namespace { auto mi = mstrptr->begin(); //auto ci1 = cr1ptr->begin(); - CIndex ci1(cr1ptr); + CIndex ci1(cr1ptr, 0); ci1 = 0; //auto ci2 = cr2ptr->begin(); - CIndex ci2(cr2ptr); + CIndex ci2(cr2ptr, 0); ci2 = 0; EXPECT_EQ(ci1.max(), 16u);