diff --git a/src/include/functional_multi_array.h b/src/include/functional_multi_array.h new file mode 100644 index 0000000..3d5cde1 --- /dev/null +++ b/src/include/functional_multi_array.h @@ -0,0 +1,79 @@ + +#ifndef __functional_multi_array__ +#define __functional_multi_array__ + +#include "multi_array_base.h" + +namespace MultiArrayTools +{ + + template + class FunctionalMultiArray : public MultiArrayBase + { + public: + + typedef ContainerRange CRange; + typedef MultiArrayBase MAB; + typedef typename MultiArrayBase::const_iterator const_iterator; + typedef typename CRange::IndexType IndexType; + + DEFAULT_MEMBERS(FunctionalMultiArray); + FunctionalMultiArray(const std::shared_ptr&... ranges, const Function& func); + + virtual const T& operator[](const IndexType& i) const override; + + virtual bool isConst() const override; + virtual bool isSlice() const override; + + private: + mutable T mVal; + Function mFunc; + }; + + +} // namespace MultiArrayTools + +/* ========================= * + * --- TEMPLATE CODE --- * + * ========================= */ + +namespace MultiArrayTools +{ + + /**************************** + * FunctionalMultiArray * + ****************************/ + + /* + template + FunctionalMultiArray::FunctionalMultiArray(const Range& range) : + MultiArrayBase(range), mFunc() {} + */ + template + FunctionalMultiArray::FunctionalMultiArray(const std::shared_ptr&... ranges, + const Function& func) : + MultiArrayBase(ranges...), mFunc(func) {} + + template + const T& FunctionalMultiArray::operator[](const typename CRange::IndexType& i) const + { + mVal = mFunc(i); + return mVal; + } + + template + bool FunctionalMultiArray::isConst() const + { + return true; + } + + template + bool FunctionalMultiArray::isSlice() const + { + return false; + } + + +} // namespace MultiArrayTools + +#endif diff --git a/src/include/multi_array.h b/src/include/multi_array.h index 5a92811..c7e07de 100644 --- a/src/include/multi_array.h +++ b/src/include/multi_array.h @@ -3,101 +3,11 @@ #ifndef __multi_array_h__ #define __multi_array_h__ -#include -#include -#include -#include -#include - -#include "base_def.h" -#include "mbase_def.h" - -#include "ranges/rheader.h" +#include "multi_array_base.h" namespace MultiArrayTools { - // Explicitely specify subranges in template argument !!! - template - class MultiArrayBase - { - public: - - typedef T value_type; - typedef ContainerRange CRange; - typedef typename CRange::IndexType IndexType; - - DEFAULT_MEMBERS(MultiArrayBase); - MultiArrayBase(const std::shared_ptr&... ranges); - - virtual ~MultiArrayBase() = default; - - virtual const T& operator[](const IndexType& i) const = 0; - //virtual const T& operator[](const typename CRange::IndexType& i) const = 0; - virtual const T& at(const typename CRange::IndexType::MetaType& meta) const = 0; - - virtual const T* data() const = 0; - virtual const std::vector& datav() const = 0; - - virtual size_t size() const; - virtual bool isSlice() const = 0; - - virtual IndexType begin() const; - virtual IndexType end() const; - - virtual IndexType beginIndex() const; - virtual IndexType endIndex() const; - - virtual const std::shared_ptr& range() const; - - virtual bool isConst() const; - - virtual ConstOperationRoot - operator()(std::shared_ptr&... inds) const; - - virtual bool isInit() const; - - protected: - bool mInit = false; - std::shared_ptr mRange; - - }; - - template - class MutableMultiArrayBase : public MultiArrayBase - { - public: - - typedef ContainerRange CRange; - //typedef typename MultiArrayBase::const_iterator const_iterator; - typedef MultiArrayBase MAB; - typedef typename CRange::IndexType IndexType; - - using MultiArrayBase::operator[]; - using MultiArrayBase::at; - using MultiArrayBase::data; - using MultiArrayBase::datav; - using MultiArrayBase::begin; - using MultiArrayBase::end; - - DEFAULT_MEMBERS(MutableMultiArrayBase); - MutableMultiArrayBase(const std::shared_ptr&... ranges); - - virtual T& operator[](const IndexType& i) = 0; - virtual T& at(const typename CRange::IndexType::MetaType& meta) = 0; - - virtual T* data() = 0; - virtual std::vector& datav() = 0; - - //virtual IndexType begin(); - //virtual IndexType end(); - - virtual bool isConst() const override; - - virtual ConstOperationRoot - operator()(std::shared_ptr&... inds) const override; - virtual OperationRoot operator()(std::shared_ptr&... inds); - }; template class MultiArray : public MutableMultiArrayBase @@ -106,8 +16,6 @@ namespace MultiArrayTools typedef ContainerRange CRange; typedef MultiArrayBase MAB; - //typedef typename MultiArrayBase::const_iterator const_iterator; - //typedef typename MutableMultiArrayBase::iterator iterator; typedef typename CRange::IndexType IndexType; DEFAULT_MEMBERS(MultiArray); @@ -141,13 +49,6 @@ namespace MultiArrayTools virtual const T* data() const override; virtual T* data() override; - - virtual const std::vector& datav() const override; - virtual std::vector& datav() override; - - // virtual void manipulate(ManipulatorBase& mb, - // const typename CRange::IndexType& manBegin, - // const typename CRange::IndexType& manEnd); template friend class MultiArray; @@ -155,30 +56,6 @@ namespace MultiArrayTools private: std::vector mCont; }; - - template - class FunctionalMultiArray : public MultiArrayBase - { - public: - - typedef ContainerRange CRange; - typedef MultiArrayBase MAB; - typedef typename MultiArrayBase::const_iterator const_iterator; - typedef typename CRange::IndexType IndexType; - - DEFAULT_MEMBERS(FunctionalMultiArray); - //FunctionalMultiArray(const CRange& range); - FunctionalMultiArray(const std::shared_ptr&... ranges, const Function& func); - - virtual const T& operator[](const IndexType& i) const override; - - virtual bool isConst() const override; - virtual bool isSlice() const override; - - protected: - mutable T mVal; - Function mFunc; - }; } @@ -189,122 +66,6 @@ namespace MultiArrayTools namespace MultiArrayTools { - /********************** - * MultiArrayBase * - **********************/ - - template - MultiArrayBase::MultiArrayBase(const std::shared_ptr&... ranges) - { - ContainerRangeFactory crf(ranges...); - mRange = std::dynamic_pointer_cast >( crf.create() ); - } - - template - size_t MultiArrayBase::size() const - { - return mRange->size(); - } - - template - typename MultiArrayBase::IndexType MultiArrayBase::begin() const - { - auto i = mRange->begin(); - return i.setData(data()); - } - - template - typename MultiArrayBase::IndexType MultiArrayBase::end() const - { - auto i = mRange->end(); - return i.setData(data()); - } - - template - typename MultiArrayBase::IndexType - MultiArrayBase::beginIndex() const - { - auto i = mRange->begin(); - return i.setData(data()); - } - - template - typename MultiArrayBase::IndexType - MultiArrayBase::endIndex() const - { - auto i = mRange->end(); - return i.setData(data()); - } - - template - const std::shared_ptr::CRange>& - MultiArrayBase::range() const - { - return mRange; - } - - template - bool MultiArrayBase::isConst() const - { - return true; - } - - template - ConstOperationRoot - MultiArrayBase::operator()(std::shared_ptr&... inds) const - { - return ConstOperationRoot(*this, inds...); - } - - template - bool MultiArrayBase::isInit() const - { - return mInit; - } - - /****************************** - * MutableMultiArrayBase * - ******************************/ - - template - MutableMultiArrayBase::MutableMultiArrayBase(const std::shared_ptr&... ranges) : - MultiArrayBase(ranges...) {} - /* - template - typename MutableMultiArrayBase::IndexType MutableMultiArrayBase::begin() - { - auto i = mRange->begin(); - return i.setData(data()); - } - - template - typename MutableMultiArrayBase::IndexType MutableMultiArrayBase::end() - { - auto i = mRange->end(); - return i.setData(data()); - } - */ - template - bool MutableMultiArrayBase::isConst() const - { - return false; - } - - template - OperationRoot - MutableMultiArrayBase::operator()(std::shared_ptr&... inds) - { - return OperationRoot(*this, inds...); - } - - template - ConstOperationRoot - MutableMultiArrayBase::operator()(std::shared_ptr&... inds) const - { - return ConstOperationRoot(*this, inds...); - } - - /******************* * MultiArray * *******************/ @@ -423,65 +184,6 @@ namespace MultiArrayTools { return mCont.data(); } - - template - const std::vector& MultiArray::datav() const - { - return mCont; - } - - template - std::vector& MultiArray::datav() - { - return mCont; - } - - - /* - template - void MultiArray::manipulate(ManipulatorBase& mb, - const typename Range::IndexType& manBegin, - const typename Range::IndexType& manEnd) - { - mb.setup(mCont, manBegin.pos(), manEnd.pos()); - mb.execute(); - } - */ - - - /**************************** - * FunctionalMultiArray * - ****************************/ - - /* - template - FunctionalMultiArray::FunctionalMultiArray(const Range& range) : - MultiArrayBase(range), mFunc() {} - */ - template - FunctionalMultiArray::FunctionalMultiArray(const std::shared_ptr&... ranges, - const Function& func) : - MultiArrayBase(ranges...), mFunc(func) {} - - template - const T& FunctionalMultiArray::operator[](const typename CRange::IndexType& i) const - { - mVal = mFunc(i); - return mVal; - } - - template - bool FunctionalMultiArray::isConst() const - { - return true; - } - - template - bool FunctionalMultiArray::isSlice() const - { - return false; - } - } #endif diff --git a/src/include/multi_array_base.h b/src/include/multi_array_base.h new file mode 100644 index 0000000..a96b21b --- /dev/null +++ b/src/include/multi_array_base.h @@ -0,0 +1,239 @@ + +#ifndef __multi_array_base_h__ +#define __multi_array_base_h__ + +#include +#include +#include +#include +#include + +#include "base_def.h" +#include "mbase_def.h" + +#include "ranges/rheader.h" + +namespace MultiArrayTools +{ + + // Explicitely specify subranges in template argument !!! + template + class MultiArrayBase + { + public: + + typedef T value_type; + typedef ContainerRange CRange; + typedef typename CRange::IndexType IndexType; + + DEFAULT_MEMBERS(MultiArrayBase); + MultiArrayBase(const std::shared_ptr&... ranges); + MultiArrayBase(const typename CRange::SpaceType& space); + + virtual ~MultiArrayBase() = default; + + virtual const T& operator[](const IndexType& i) const = 0; + virtual const T& at(const typename CRange::IndexType::MetaType& meta) const = 0; + + virtual const T* data() const = 0; + + virtual size_t size() const; + virtual bool isSlice() const = 0; + + virtual IndexType begin() const; + virtual IndexType end() const; + + virtual IndexType beginIndex() const; + virtual IndexType endIndex() const; + + virtual const std::shared_ptr& range() const; + + virtual bool isConst() const; + + virtual ConstOperationRoot + operator()(std::shared_ptr&... inds) const; + + virtual bool isInit() const; + + // slice function !!!!! + + protected: + bool mInit = false; + std::shared_ptr mRange; + + }; + + template + class MutableMultiArrayBase : public MultiArrayBase + { + public: + + typedef ContainerRange CRange; + typedef MultiArrayBase MAB; + typedef typename CRange::IndexType IndexType; + + using MultiArrayBase::operator[]; + using MultiArrayBase::at; + using MultiArrayBase::data; + using MultiArrayBase::begin; + using MultiArrayBase::end; + + DEFAULT_MEMBERS(MutableMultiArrayBase); + MutableMultiArrayBase(const std::shared_ptr&... ranges); + MutableMultiArrayBase(const typename CRange::SpaceType& space); + + virtual T& operator[](const IndexType& i) = 0; + virtual T& at(const typename CRange::IndexType::MetaType& meta) = 0; + + virtual T* data() = 0; + + //virtual IndexType begin(); + //virtual IndexType end(); + + virtual bool isConst() const override; + + virtual ConstOperationRoot + operator()(std::shared_ptr&... inds) const override; + virtual OperationRoot operator()(std::shared_ptr&... inds); + }; + + +} // end namespace MultiArrayTools + +/* ========================= * + * --- TEMPLATE CODE --- * + * ========================= */ + +namespace MultiArrayTools +{ + + /********************** + * MultiArrayBase * + **********************/ + + template + MultiArrayBase::MultiArrayBase(const std::shared_ptr&... ranges) + { + ContainerRangeFactory crf(ranges...); + mRange = std::dynamic_pointer_cast >( crf.create() ); + } + + template + MultiArrayBase::MultiArrayBase(const typename CRange::SpaceType& space) + { + ContainerRangeFactory crf(space); + mRange = std::dynamic_pointer_cast >( crf.create() ); + } + + template + size_t MultiArrayBase::size() const + { + return mRange->size(); + } + + template + typename MultiArrayBase::IndexType MultiArrayBase::begin() const + { + auto i = mRange->begin(); + return i.setData(data()); + } + + template + typename MultiArrayBase::IndexType MultiArrayBase::end() const + { + auto i = mRange->end(); + return i.setData(data()); + } + + template + typename MultiArrayBase::IndexType + MultiArrayBase::beginIndex() const + { + auto i = mRange->begin(); + return i.setData(data()); + } + + template + typename MultiArrayBase::IndexType + MultiArrayBase::endIndex() const + { + auto i = mRange->end(); + return i.setData(data()); + } + + template + const std::shared_ptr::CRange>& + MultiArrayBase::range() const + { + return mRange; + } + + template + bool MultiArrayBase::isConst() const + { + return true; + } + + template + ConstOperationRoot + MultiArrayBase::operator()(std::shared_ptr&... inds) const + { + return ConstOperationRoot(*this, inds...); + } + + template + bool MultiArrayBase::isInit() const + { + return mInit; + } + + + /****************************** + * MutableMultiArrayBase * + ******************************/ + + template + MutableMultiArrayBase::MutableMultiArrayBase(const std::shared_ptr&... ranges) : + MultiArrayBase(ranges...) {} + + template + MutableMultiArrayBase::MutableMultiArrayBase(const typename CRange::SpaceType& space) : + MultiArrayBase(space) {} + /* + template + typename MutableMultiArrayBase::IndexType MutableMultiArrayBase::begin() + { + auto i = mRange->begin(); + return i.setData(data()); + } + + template + typename MutableMultiArrayBase::IndexType MutableMultiArrayBase::end() + { + auto i = mRange->end(); + return i.setData(data()); + } + */ + template + bool MutableMultiArrayBase::isConst() const + { + return false; + } + + template + OperationRoot + MutableMultiArrayBase::operator()(std::shared_ptr&... inds) + { + return OperationRoot(*this, inds...); + } + + template + ConstOperationRoot + MutableMultiArrayBase::operator()(std::shared_ptr&... inds) const + { + return ConstOperationRoot(*this, inds...); + } + +} // end namespace MultiArrayTools + +#endif diff --git a/src/include/multi_array_header.h b/src/include/multi_array_header.h index 9dd1e2b..ab6021d 100644 --- a/src/include/multi_array_header.h +++ b/src/include/multi_array_header.h @@ -12,7 +12,9 @@ //#include "container_range.h" //#include "block.h" #include "multi_array_operation.h" +#include "multi_array_base.h" #include "multi_array.h" +#include "functional_multi_array.h" #include "helper_tools.h" //#include "slice.h" //#include "manipulator.h" diff --git a/src/include/pack_num.h b/src/include/pack_num.h index 9631565..eed17c4 100644 --- a/src/include/pack_num.h +++ b/src/include/pack_num.h @@ -13,7 +13,7 @@ namespace MultiArrayHelper { - + template struct PackNum { diff --git a/src/include/ranges/container_range.h b/src/include/ranges/container_range.h index e0d9713..6e8dd0d 100644 --- a/src/include/ranges/container_range.h +++ b/src/include/ranges/container_range.h @@ -7,7 +7,6 @@ #include #include -//#include "base_def.h" #include "ranges/range_base.h" #include "ranges/index_base.h" @@ -32,9 +31,10 @@ namespace MultiArrayTools static IndexType sType() { return IndexType::CONT; } static size_t sDim() { return sizeof...(Indices); } static size_t totalDim() { return mkTotalDim(); } - + private: + bool mNonTrivialBlocks = false; bool mExternControl = false; IndexPack mIPack; std::array mBlockSizes; @@ -46,6 +46,10 @@ namespace MultiArrayTools template ContainerIndex(const std::shared_ptr& range); + + template + ContainerIndex(const std::shared_ptr& range, + const std::array& blockSizes); template auto get() const -> decltype( *std::get( mIPack ) )&; @@ -71,13 +75,14 @@ namespace MultiArrayTools int pp(std::intptr_t idxPtrNum); int mm(std::intptr_t idxPtrNum); - MetaType meta(); + MetaType meta() const; ContainerIndex& at(const MetaType& metaPos); - size_t dim(); - bool first(); - bool last(); - + size_t dim() const; + bool first() const; + bool last() const; + bool sliceMode() const; + std::shared_ptr range(); template @@ -148,7 +153,6 @@ namespace MultiArrayTools typedef RangeBase RB; typedef std::tuple...> SpaceType; typedef ContainerIndex IndexType; - //typedef typename RangeInterface >::IndexType IndexType; protected: ContainerRange() = default; @@ -207,11 +211,24 @@ namespace MultiArrayTools IndexInterface,std::tuple >(range, 0) { RPackNum::construct(mIPack, *range); - IB::mPos = RPackNum::makePos(mIPack); std::get(mBlockSizes) = 1; RPackNum::initBlockSizes(mBlockSizes, mIPack); + IB::mPos = RPackNum::makePos(mIPack, mBlockSizes); } + template + template + ContainerIndex::ContainerIndex(const std::shared_ptr& range, + const std::array& blockSizes) : + IndexInterface,std::tuple >(range, 0) + { + RPackNum::construct(mIPack, *range); + mBlockSizes = blockSizes; + IB::mPos = RPackNum::makePos(mIPack, mBlockSizes); + mNonTrivialBlocks = true; + } + + template ContainerIndex& ContainerIndex::sync() { @@ -303,7 +320,7 @@ namespace MultiArrayTools } template - typename ContainerIndex::MetaType ContainerIndex::meta() + typename ContainerIndex::MetaType ContainerIndex::meta() const { MetaType metaTuple; RPackNum::getMetaPos(metaTuple, mIPack); @@ -319,23 +336,29 @@ namespace MultiArrayTools } template - size_t ContainerIndex::dim() + size_t ContainerIndex::dim() const { return sizeof...(Indices); } template - bool ContainerIndex::first() + bool ContainerIndex::first() const { return IB::pos() == 0; } template - bool ContainerIndex::last() + bool ContainerIndex::last() const { return IB::pos() == IB::mMax - 1; } + template + bool ContainerIndex::sliceMode() const + { + return mNonTrivialBlocks; + } + template std::shared_ptr::RangeType> ContainerIndex::range() diff --git a/src/include/ranges/index_base.h b/src/include/ranges/index_base.h index 0fc2b2e..38c552c 100644 --- a/src/include/ranges/index_base.h +++ b/src/include/ranges/index_base.h @@ -27,6 +27,8 @@ namespace MultiArrayTools I& THIS() { return static_cast(*this); } I const& THIS() const { return static_cast(*this); } + + static constexpr bool ISINDEX = true; ~IndexInterface() = default; diff --git a/src/include/ranges/range_base.h b/src/include/ranges/range_base.h index e10f847..d8e3c32 100644 --- a/src/include/ranges/range_base.h +++ b/src/include/ranges/range_base.h @@ -50,6 +50,8 @@ namespace MultiArrayTools { public: + static constexpr bool ISINDEX = false; + virtual ~RangeBase() = default; virtual size_t size() const = 0; diff --git a/src/include/ranges/rpack_num.h b/src/include/ranges/rpack_num.h index c547e2e..b765ab4 100644 --- a/src/include/ranges/rpack_num.h +++ b/src/include/ranges/rpack_num.h @@ -177,6 +177,13 @@ namespace MultiArrayHelper return std::get(iPtrTup)->pos() + RPackNum::makePos(iPtrTup) * std::get(iPtrTup)->max(); } + template + static inline size_t makePos(const std::tuple...>& iPtrTup, + const std::array& blockSize) + { + return RPackNum::makePos(iPtrTup, blockSize) + std::get(iPtrTup)->pos() * std::get(blockSize); + } + template static void swapIndices(Pack& ipack, const std::shared_ptr& nind, const std::shared_ptr&... ninds) @@ -353,7 +360,14 @@ namespace MultiArrayHelper { return std::get<0>(iPtrTup)->pos(); } - + + template + static inline size_t makePos(const std::tuple...>& iPtrTup, + const std::array& blockSize) + { + return std::get<0>(iPtrTup)->pos() * std::get<0>(blockSize); + } + template static void swapIndices(Pack& ipack, const std::shared_ptr& nind) { diff --git a/src/include/slice.h b/src/include/slice.h new file mode 100644 index 0000000..de6bfa0 --- /dev/null +++ b/src/include/slice.h @@ -0,0 +1,162 @@ + +#ifndef __slice_h__ +#define __slice_h__ + +#include "multi_array_base.h" + +namespace MultiArrayTools +{ + + template + class Slice : public MutableMultiArrayBase + { + public: + + typedef ContainerRange CRange; + typedef MultiArrayBase MAB; + typedef typename CRange::IndexType IndexType; + + DEFAULT_MEMBERS(Slice); + + template // Range / Index <-> open / const + Slice(T* data, const RITypes&... 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* data() const override; + virtual T* data() override; + + virtual bool isSlice() const override; + + virtual IndexType begin() const override; + virtual IndexType end() const override; + + private: + T* mData; + size_t mStartPos; + std::array mBlockSizes; + }; + +} // end namespace MultiArrayTools + +/* ========================= * + * --- TEMPLATE CODE --- * + * ========================= */ + +namespace MultiArrayTools +{ + + namespace + { + + template + struct XX + { + template + static auto ri_to_tuple(const std::shared_ptr& ri) + -> std::tuple + { + return std::make_tuple(ri); + } + }; + + template <> + struct XX + { + template + static auto ri_to_tuple(const std::shared_ptr& ri) + -> std::tuple<> + { + return std::make_tuple(); + } + }; + + template + auto mkSliceRange(const std::shared_ptr&... ris) + { + return std::tuple_cat(XX::ri_to_tuple(ris)...); + } + + } + + /************* + * Slice * + *************/ + /* + template + Slice::Slice(T* data, const RITypes&... ris) : + MutableMultiArrayBase( mkSliceRange(ris...) ), + mData(data), + mStartPos(mkSliceStart(ris...)), + mBlockSizes(mkSliceBlocks(ris...)) {} + */ //!!!!! + template + const T& Slice::operator[](const IndexType& i) const + { + assert(i.sliceMode()); // -> compare objects !!!!! + return mData[ i.pos() ]; + } + + template + T& Slice::operator[](const IndexType& i) + { + assert(i.sliceMode()); + return mData[ i.pos() ]; + } + + template + const T& Slice::at(const typename CRange::IndexType::MetaType& meta) const + { + assert(i.sliceMode()); + return mData[ begin().at(meta).pos() ]; + } + + template + T& Slice::at(const typename CRange::IndexType::MetaType& meta) + { + assert(i.sliceMode()); + return mData[ begin().at(meta).pos() ]; + } + + template + const T* Slice::data() const + { + return mData; + } + + template + T* Slice::data() + { + return mData; + } + + template + bool Slice::isSlice() const + { + return true; + } + + template + IndexType Slice::begin() const + { + IndexType i(mRange, mBlockSizes); + i = mStartPos; + return i.setData(data()); + } + + template + IndexType Slice::end() const + { + IndexType i(mRange, mBlockSizes); + i = std::get(mBlockSizes); + return i.setData(data()); + } + + + +} // end namespace MultiArrayTools + +#endif