diff --git a/src/include/multi_array.h b/src/include/multi_array.h index 7a2fbcf..5a92811 100644 --- a/src/include/multi_array.h +++ b/src/include/multi_array.h @@ -24,51 +24,8 @@ namespace MultiArrayTools public: typedef T value_type; - typedef ContainerRange CRange; + typedef ContainerRange CRange; typedef typename CRange::IndexType IndexType; - - class const_iterator : public std::iterator - { - public: - - DEFAULT_MEMBERS(const_iterator); - - const_iterator(const MultiArrayBase& ma); - const_iterator(const MultiArrayBase& ma, const typename CRange::IndexType& index); - - // Requirements: - bool operator==(const const_iterator& it) const; - bool operator!=(const const_iterator& it) const; - - const T& operator*() const; - T const* operator->() const; - - const_iterator& operator++(); - const_iterator operator++(int); - const_iterator& operator--(); - const_iterator operator--(int); - - const_iterator& operator+=(int diff); - const_iterator& operator-=(int diff); - const_iterator operator+(int num) const; - const_iterator operator-(int num) const; - - int operator-(const const_iterator& it) const; - - const T& operator[](int num) const; - - bool operator<(const const_iterator& it) const; - bool operator>(const const_iterator& it) const; - bool operator<=(const const_iterator& it) const; - bool operator>=(const const_iterator& it) const; - - // Multi Array specific: - typename ContainerRange::IndexType index() const; - - protected: - MultiArrayBase const* mMAPtr = nullptr; - size_t mPos; - }; DEFAULT_MEMBERS(MultiArrayBase); MultiArrayBase(const std::shared_ptr&... ranges); @@ -85,8 +42,8 @@ namespace MultiArrayTools virtual size_t size() const; virtual bool isSlice() const = 0; - virtual const_iterator begin() const; - virtual const_iterator end() const; + virtual IndexType begin() const; + virtual IndexType end() const; virtual IndexType beginIndex() const; virtual IndexType endIndex() const; @@ -111,58 +68,11 @@ namespace MultiArrayTools { public: - typedef ContainerRange CRange; - typedef typename MultiArrayBase::const_iterator const_iterator; + typedef ContainerRange CRange; + //typedef typename MultiArrayBase::const_iterator const_iterator; typedef MultiArrayBase MAB; typedef typename CRange::IndexType IndexType; - class iterator : public std::iterator, - public std::iterator - { - public: - - DEFAULT_MEMBERS(iterator); - - iterator(MutableMultiArrayBase& ma); - iterator(MutableMultiArrayBase& ma, const IndexType& index); - - // Requirements: - bool operator==(const iterator& it) const; - bool operator!=(const iterator& it) const; - - const T& operator*() const; - T const* operator->() const; - T& operator*(); - T* operator->(); - - iterator& operator++(); - iterator operator++(int); - iterator& operator--(); - iterator operator--(int); - - iterator& operator+=(int diff); - iterator& operator-=(int diff); - iterator operator+(int num) const; - iterator operator-(int num) const; - - int operator-(const iterator& it) const; - - const T& operator[](int num) const; - T& operator[](int num); - - bool operator<(const iterator& it) const; - bool operator>(const iterator& it) const; - bool operator<=(const iterator& it) const; - bool operator>=(const iterator& it) const; - - // Multi Array specific: - typename CRange::IndexType index() const; - - protected: - MutableMultiArrayBase* mMAPtr = nullptr; - size_t mPos; - }; - using MultiArrayBase::operator[]; using MultiArrayBase::at; using MultiArrayBase::data; @@ -179,8 +89,8 @@ namespace MultiArrayTools virtual T* data() = 0; virtual std::vector& datav() = 0; - virtual iterator begin(); - virtual iterator end(); + //virtual IndexType begin(); + //virtual IndexType end(); virtual bool isConst() const override; @@ -194,10 +104,10 @@ namespace MultiArrayTools { public: - typedef ContainerRange CRange; + typedef ContainerRange CRange; typedef MultiArrayBase MAB; - typedef typename MultiArrayBase::const_iterator const_iterator; - typedef typename MutableMultiArrayBase::iterator iterator; + //typedef typename MultiArrayBase::const_iterator const_iterator; + //typedef typename MutableMultiArrayBase::iterator iterator; typedef typename CRange::IndexType IndexType; DEFAULT_MEMBERS(MultiArray); @@ -251,7 +161,7 @@ namespace MultiArrayTools { public: - typedef ContainerRange CRange; + typedef ContainerRange CRange; typedef MultiArrayBase MAB; typedef typename MultiArrayBase::const_iterator const_iterator; typedef typename CRange::IndexType IndexType; @@ -278,146 +188,6 @@ namespace MultiArrayTools namespace MultiArrayTools { - /************************************** - * MultiArrayBase::const_iterator * - **************************************/ - - template - MultiArrayBase::const_iterator::const_iterator(const MultiArrayBase& ma): - mMAPtr(&ma), mPos(0) { } - - template - MultiArrayBase::const_iterator::const_iterator(const MultiArrayBase& ma, - const typename CRange::IndexType& index): - mMAPtr(&ma), mPos(index.pos()) { } - - template - bool MultiArrayBase::const_iterator::operator==(const const_iterator& it) const - { - return mMAPtr == it.mMAPtr and mPos == it.mPos; - } - - template - bool MultiArrayBase::const_iterator::operator!=(const const_iterator& it) const - { - return mMAPtr != it.mMAPtr or mPos != it.mPos; - } - - template - const T& MultiArrayBase::const_iterator::operator*() const - { - return mMAPtr->data()[mPos]; - } - - template - T const* MultiArrayBase::const_iterator::operator->() const - { - return &mMAPtr->data()[mPos]; - } - - template - typename MultiArrayBase::const_iterator& MultiArrayBase::const_iterator::operator++() - { - ++mPos; - return *this; - } - - template - typename MultiArrayBase::const_iterator MultiArrayBase::const_iterator::operator++(int) - { - const_iterator tmp(*this); - ++mPos; - return tmp; - } - - template - typename MultiArrayBase::const_iterator& MultiArrayBase::const_iterator::operator--() - { - --mPos; - return *this; - } - - template - typename MultiArrayBase::const_iterator MultiArrayBase::const_iterator::operator--(int) - { - const_iterator tmp(*this); - --mPos; - return tmp; - } - - template - typename MultiArrayBase::const_iterator& MultiArrayBase::const_iterator::operator+=(int diff) - { - mPos += diff; - return *this; - } - - template - typename MultiArrayBase::const_iterator& MultiArrayBase::const_iterator::operator-=(int diff) - { - mPos -= diff; - return *this; - } - - template - typename MultiArrayBase::const_iterator MultiArrayBase::const_iterator::operator+(int num) const - { - const_iterator tmp(*this); - tmp += num; - return tmp; - } - - template - typename MultiArrayBase::const_iterator MultiArrayBase::const_iterator::operator-(int num) const - { - const_iterator tmp(*this); - tmp -= num; - } - - template - int MultiArrayBase::const_iterator::operator-(const const_iterator& it) const - { - return mPos - it.mPos; - } - - template - const T& MultiArrayBase::const_iterator::operator[](int num) const - { - return *(operator+(num)); - } - - template - bool MultiArrayBase::const_iterator::operator<(const const_iterator& it) const - { - return mPos < it.mPos; - } - - template - bool MultiArrayBase::const_iterator::operator>(const const_iterator& it) const - { - return mPos > it.mPos; - } - - template - bool MultiArrayBase::const_iterator::operator<=(const const_iterator& it) const - { - return mPos <= it.mPos; - } - - template - bool MultiArrayBase::const_iterator::operator>=(const const_iterator& it) const - { - return mPos >= it.mPos; - } - - template - typename MultiArrayBase::IndexType - MultiArrayBase::const_iterator::index() const - { - auto i = mMAPtr->beginIndex(); - i = mPos; - return i; - } /********************** * MultiArrayBase * @@ -426,8 +196,8 @@ namespace MultiArrayTools template MultiArrayBase::MultiArrayBase(const std::shared_ptr&... ranges) { - ContainerRangeFactory crf(ranges...); - mRange = std::dynamic_pointer_cast >( crf.create() ); + ContainerRangeFactory crf(ranges...); + mRange = std::dynamic_pointer_cast >( crf.create() ); } template @@ -437,29 +207,33 @@ namespace MultiArrayTools } template - typename MultiArrayBase::const_iterator MultiArrayBase::begin() const + typename MultiArrayBase::IndexType MultiArrayBase::begin() const { - return const_iterator(*this, beginIndex()); + auto i = mRange->begin(); + return i.setData(data()); } template - typename MultiArrayBase::const_iterator MultiArrayBase::end() const + typename MultiArrayBase::IndexType MultiArrayBase::end() const { - return const_iterator(*this, endIndex()); + auto i = mRange->end(); + return i.setData(data()); } template typename MultiArrayBase::IndexType MultiArrayBase::beginIndex() const { - return mRange->begin(); + auto i = mRange->begin(); + return i.setData(data()); } template typename MultiArrayBase::IndexType MultiArrayBase::endIndex() const { - return mRange->end(); + auto i = mRange->end(); + return i.setData(data()); } template @@ -487,167 +261,6 @@ namespace MultiArrayTools { return mInit; } - - /**************************************** - * MutableMultiArrayBase::iterator * - ****************************************/ - - template - MutableMultiArrayBase::iterator::iterator(MutableMultiArrayBase& ma): - mMAPtr(&ma), mPos(0) - { } - - template - MutableMultiArrayBase::iterator::iterator(MutableMultiArrayBase& ma, - const typename CRange::IndexType& index): - mMAPtr(&ma), mPos(index.pos()) - { } - - template - bool MutableMultiArrayBase::iterator::operator==(const iterator& it) const - { - return mMAPtr == it.mMAPtr and mPos == it.mPos; - } - - template - bool MutableMultiArrayBase::iterator::operator!=(const iterator& it) const - { - return mMAPtr != it.mMAPtr or mPos != it.mPos; - } - - template - const T& MutableMultiArrayBase::iterator::operator*() const - { - return mMAPtr->data()[mPos]; - } - - template - T const* MutableMultiArrayBase::iterator::operator->() const - { - return &mMAPtr->data()[mPos]; - } - - template - T& MutableMultiArrayBase::iterator::operator*() - { - return mMAPtr->data()[mPos]; - } - - template - T* MutableMultiArrayBase::iterator::operator->() - { - return &mMAPtr->data()[mPos]; - } - - template - typename MutableMultiArrayBase::iterator& MutableMultiArrayBase::iterator::operator++() - { - ++mPos; - return *this; - } - - template - typename MutableMultiArrayBase::iterator MutableMultiArrayBase::iterator::operator++(int) - { - iterator tmp(*this); - ++mPos; - return tmp; - } - - template - typename MutableMultiArrayBase::iterator& MutableMultiArrayBase::iterator::operator--() - { - --mPos; - return *this; - } - - template - typename MutableMultiArrayBase::iterator MutableMultiArrayBase::iterator::operator--(int) - { - iterator tmp(*this); - --mPos; - return tmp; - } - - template - typename MutableMultiArrayBase::iterator& MutableMultiArrayBase::iterator::operator+=(int diff) - { - mPos += diff; - return *this; - } - - template - typename MutableMultiArrayBase::iterator& MutableMultiArrayBase::iterator::operator-=(int diff) - { - mPos -= diff; - return *this; - } - - template - typename MutableMultiArrayBase::iterator MutableMultiArrayBase::iterator::operator+(int num) const - { - iterator tmp(*this); - tmp += num; - return tmp; - } - - template - typename MutableMultiArrayBase::iterator MutableMultiArrayBase::iterator::operator-(int num) const - { - iterator tmp(*this); - tmp -= num; - } - - template - int MutableMultiArrayBase::iterator::operator-(const iterator& it) const - { - return mPos - it.mPos; - } - - template - const T& MutableMultiArrayBase::iterator::operator[](int num) const - { - return *(operator+(num)); - } - - template - T& MutableMultiArrayBase::iterator::operator[](int num) - { - return *(operator+(num)); - } - - template - bool MutableMultiArrayBase::iterator::operator<(const iterator& it) const - { - return mPos < it.mPos; - } - - template - bool MutableMultiArrayBase::iterator::operator>(const iterator& it) const - { - return mPos > it.mPos; - } - - template - bool MutableMultiArrayBase::iterator::operator<=(const iterator& it) const - { - return mPos <= it.mPos; - } - - template - bool MutableMultiArrayBase::iterator::operator>=(const iterator& it) const - { - return mPos >= it.mPos; - } - - template - typename MutableMultiArrayBase::IndexType - MutableMultiArrayBase::iterator::index() const - { - auto i = mMAPtr->beginIndex(); - i = mPos; - return i; - } /****************************** * MutableMultiArrayBase * @@ -656,19 +269,21 @@ namespace MultiArrayTools template MutableMultiArrayBase::MutableMultiArrayBase(const std::shared_ptr&... ranges) : MultiArrayBase(ranges...) {} - + /* template - typename MutableMultiArrayBase::iterator MutableMultiArrayBase::begin() + typename MutableMultiArrayBase::IndexType MutableMultiArrayBase::begin() { - return iterator(*this, MAB::beginIndex()); + auto i = mRange->begin(); + return i.setData(data()); } template - typename MutableMultiArrayBase::iterator MutableMultiArrayBase::end() + typename MutableMultiArrayBase::IndexType MutableMultiArrayBase::end() { - return iterator(*this, MAB::endIndex()); + auto i = mRange->end(); + return i.setData(data()); } - + */ template bool MutableMultiArrayBase::isConst() const { diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index 38bc5f3..b0a2fc3 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -94,7 +94,7 @@ namespace MultiArrayTools typedef T value_type; typedef OperationBase OB; - typedef ContainerRange CRange; + typedef ContainerRange CRange; typedef typename MultiRange::IndexType IndexType; OperationMaster(MutableMultiArrayBase& ma, const OpClass& second, @@ -129,7 +129,7 @@ namespace MultiArrayTools typedef T value_type; typedef OperationBase OB; typedef OperationTemplate > OT; - typedef ContainerRange CRange; + typedef ContainerRange CRange; typedef typename CRange::IndexType IndexType; static constexpr size_t SIZE = 1; @@ -165,7 +165,7 @@ namespace MultiArrayTools typedef T value_type; typedef OperationBase OB; typedef OperationTemplate > OT; - typedef ContainerRange CRange; + typedef ContainerRange CRange; typedef typename CRange::IndexType IndexType; static constexpr size_t SIZE = 1; diff --git a/src/include/ranges/container_range.h b/src/include/ranges/container_range.h index fa9240d..e0d9713 100644 --- a/src/include/ranges/container_range.h +++ b/src/include/ranges/container_range.h @@ -16,17 +16,18 @@ namespace MultiArrayTools { - template - class ContainerIndex : public IndexInterface, - std::tuple > + template + class ContainerIndex : public IndexInterface, + std::tuple >, + public std::iterator { public: - typedef IndexInterface, + typedef IndexInterface, std::tuple > IB; typedef std::tuple MetaType; typedef std::tuple...> IndexPack; - typedef ContainerRange RangeType; + typedef ContainerRange RangeType; static IndexType sType() { return IndexType::CONT; } static size_t sDim() { return sizeof...(Indices); } @@ -37,6 +38,7 @@ namespace MultiArrayTools bool mExternControl = false; IndexPack mIPack; std::array mBlockSizes; + const T* mData; public: @@ -96,19 +98,41 @@ namespace MultiArrayTools auto iforh(Exprs exs) const -> decltype(RPackNum::mkForh(mIPack, exs)); + // Iterator Stuff + + ContainerIndex& setData(const T* data); + + const T& operator*() const; + const T* operator->() const; + + ContainerIndex operator++(int); + ContainerIndex operator--(int); + ContainerIndex& operator+=(int diff); + ContainerIndex& operator-=(int diff); + ContainerIndex operator+(int num) const; + ContainerIndex operator-(int num) const; + + int operator-(const ContainerIndex& it) const; + const T& operator[](int num) const; + + bool operator<(const ContainerIndex& it) const; + bool operator>(const ContainerIndex& it) const; + bool operator<=(const ContainerIndex& it) const; + bool operator>=(const ContainerIndex& it) const; + }; - template + template class ContainerRangeFactory : public RangeFactoryBase { public: - typedef ContainerRange oType; + typedef ContainerRange oType; ContainerRangeFactory(); ContainerRangeFactory(const std::shared_ptr&... rs); - ContainerRangeFactory(const typename ContainerRange::SpaceType& space); + ContainerRangeFactory(const typename ContainerRange::SpaceType& space); virtual std::shared_ptr create() override; @@ -116,14 +140,14 @@ namespace MultiArrayTools }; - template - class ContainerRange : public RangeInterface > + template + class ContainerRange : public RangeInterface > { public: typedef RangeBase RB; typedef std::tuple...> SpaceType; - typedef ContainerIndex IndexType; + typedef ContainerIndex IndexType; //typedef typename RangeInterface >::IndexType IndexType; protected: @@ -153,7 +177,7 @@ namespace MultiArrayTools virtual IndexType begin() const override; virtual IndexType end() const override; - friend ContainerRangeFactory; + friend ContainerRangeFactory; static constexpr bool defaultable = false; static constexpr size_t ISSTATIC = SubProp::ISSTATIC; @@ -177,10 +201,10 @@ namespace MultiArrayTools * ContainerIndex * **********************/ - template + template template - ContainerIndex::ContainerIndex(const std::shared_ptr& range) : - IndexInterface,std::tuple >(range, 0) + ContainerIndex::ContainerIndex(const std::shared_ptr& range) : + IndexInterface,std::tuple >(range, 0) { RPackNum::construct(mIPack, *range); IB::mPos = RPackNum::makePos(mIPack); @@ -188,8 +212,8 @@ namespace MultiArrayTools RPackNum::initBlockSizes(mBlockSizes, mIPack); } - template - ContainerIndex& ContainerIndex::sync() + template + ContainerIndex& ContainerIndex::sync() { if(mExternControl){ IB::mPos = RPackNum::makePos(mIPack); @@ -200,39 +224,39 @@ namespace MultiArrayTools return *this; } - template + template template - auto ContainerIndex::get() const -> decltype( *std::get( mIPack ) )& + auto ContainerIndex::get() const -> decltype( *std::get( mIPack ) )& { return *std::get( mIPack ); } - template + template template - auto ContainerIndex::getPtr() const -> decltype( std::get( mIPack ) )& + auto ContainerIndex::getPtr() const -> decltype( std::get( mIPack ) )& { return std::get( mIPack ); } - template - ContainerIndex& ContainerIndex::operator()(const std::shared_ptr&... inds) + template + ContainerIndex& ContainerIndex::operator()(const std::shared_ptr&... inds) { RPackNum::swapIndices(mIPack, inds...); mExternControl = true; return sync(); } - template - ContainerIndex& ContainerIndex::operator()() + template + ContainerIndex& ContainerIndex::operator()() { return sync(); } - template - IndexType ContainerIndex::type() const { return IndexType::CONT; } + template + IndexType ContainerIndex::type() const { return IndexType::CONT; } - template - ContainerIndex& ContainerIndex::operator++() + template + ContainerIndex& ContainerIndex::operator++() { if(mExternControl){ IB::mPos = RPackNum::makePos(mIPack); @@ -242,8 +266,8 @@ namespace MultiArrayTools return *this; } - template - ContainerIndex& ContainerIndex::operator--() + template + ContainerIndex& ContainerIndex::operator--() { if(mExternControl){ IB::mPos = RPackNum::makePos(mIPack); @@ -254,80 +278,80 @@ namespace MultiArrayTools } - template - ContainerIndex& ContainerIndex::operator=(size_t pos) + template + ContainerIndex& ContainerIndex::operator=(size_t pos) { IB::mPos = pos; RPackNum::setIndexPack(mIPack, pos); return *this; } - template - int ContainerIndex::pp(std::intptr_t idxPtrNum) + template + int ContainerIndex::pp(std::intptr_t idxPtrNum) { int tmp = RPackNum::pp(mIPack, mBlockSizes, idxPtrNum); IB::mPos += tmp; return tmp; } - template - int ContainerIndex::mm(std::intptr_t idxPtrNum) + template + int ContainerIndex::mm(std::intptr_t idxPtrNum) { int tmp = RPackNum::mm(mIPack, mBlockSizes, idxPtrNum); IB::mPos -= tmp; return tmp; } - template - typename ContainerIndex::MetaType ContainerIndex::meta() + template + typename ContainerIndex::MetaType ContainerIndex::meta() { MetaType metaTuple; RPackNum::getMetaPos(metaTuple, mIPack); return metaTuple; } - template - ContainerIndex& ContainerIndex::at(const MetaType& metaPos) + template + ContainerIndex& ContainerIndex::at(const MetaType& metaPos) { RPackNum::setMeta(mIPack, metaPos); IB::mPos = RPackNum::makePos(mIPack); return *this; } - template - size_t ContainerIndex::dim() + template + size_t ContainerIndex::dim() { return sizeof...(Indices); } - template - bool ContainerIndex::first() + template + bool ContainerIndex::first() { return IB::pos() == 0; } - template - bool ContainerIndex::last() + template + bool ContainerIndex::last() { return IB::pos() == IB::mMax - 1; } - template - std::shared_ptr::RangeType> - ContainerIndex::range() + template + std::shared_ptr::RangeType> + ContainerIndex::range() { return std::dynamic_pointer_cast( IB::mRangePtr ); } - template + template template - auto ContainerIndex::getPtr() -> decltype( std::get( mIPack ) )& + auto ContainerIndex::getPtr() -> decltype( std::get( mIPack ) )& { return std::get( mIPack ); } - template - size_t ContainerIndex::getStepSize(size_t n) + template + size_t ContainerIndex::getStepSize(size_t n) { if(n >= sizeof...(Indices)){ assert(0); @@ -336,8 +360,8 @@ namespace MultiArrayTools return mBlockSizes[n+1]; } - template - std::vector ContainerIndex::infoVec() const + template + std::vector ContainerIndex::infoVec() const { std::vector out; out.reserve(sizeof...(Indices)); @@ -345,14 +369,14 @@ namespace MultiArrayTools return std::move( out ); } - template - std::string ContainerIndex::id() const + template + std::string ContainerIndex::id() const { return std::string("con") + std::to_string(IB::mId); } - template - void ContainerIndex::print(size_t offset) + template + void ContainerIndex::print(size_t offset) { if(offset == 0){ std::cout << " === " << std::endl; @@ -363,42 +387,156 @@ namespace MultiArrayTools RPackNum::printIndex(mIPack, offset+1); } - template + template template - auto ContainerIndex::ifor(Exprs exs) const + auto ContainerIndex::ifor(Exprs exs) const -> decltype(RPackNum::mkFor(mIPack, exs)) { return RPackNum::mkFor(mIPack, exs); } - template + template template - auto ContainerIndex::iforh(Exprs exs) const + auto ContainerIndex::iforh(Exprs exs) const -> decltype(RPackNum::mkForh(mIPack, exs)) { return RPackNum::mkForh(mIPack, exs); } - + template + ContainerIndex& ContainerIndex::setData(const T* data) + { + mData = data; + return *this; + } + + template + const T& ContainerIndex::operator*() const + { + return mData[IB::mPos]; + } + + template + const T* ContainerIndex::operator->() const + { + return &mData[IB::mPos]; + } + + template + ContainerIndex ContainerIndex::operator++(int) + { + auto tmp = *this; + ++(*this); + return tmp; + } + + template + ContainerIndex ContainerIndex::operator--(int) + { + auto tmp = *this; + --(*this); + return tmp; + } + + template + ContainerIndex& ContainerIndex::operator+=(int diff) + { + if(diff < 0){ + for(int i = 0; i != diff; ++i){ + (*this)--; + } + } + else { + for(int i = 0; i != diff; ++i){ + (*this)++; + } + } + } + + template + ContainerIndex& ContainerIndex::operator-=(int diff) + { + if(diff < 0){ + for(int i = 0; i != diff; ++i){ + (*this)++; + } + } + else { + for(int i = 0; i != diff; ++i){ + (*this)--; + } + } + } + + template + ContainerIndex ContainerIndex::operator+(int num) const + { + auto tmp = *this; + return tmp += num; + } + + template + ContainerIndex ContainerIndex::operator-(int num) const + { + auto tmp = *this; + return tmp -= num; + } + + template + int ContainerIndex::operator-(const ContainerIndex& it) const + { + return static_cast( IB::mPos ) - static_cast( it.pos() ); + } + + template + const T& ContainerIndex::operator[](int num) const + { + return mData[IB::mPos + num]; + } + + template + bool ContainerIndex::operator<(const ContainerIndex& it) const + { + return IB::mPos < it.pos(); + } + + template + bool ContainerIndex::operator>(const ContainerIndex& it) const + { + return IB::mPos > it.pos(); + } + + template + bool ContainerIndex::operator<=(const ContainerIndex& it) const + { + return IB::mPos <= it.pos(); + } + + template + bool ContainerIndex::operator>=(const ContainerIndex& it) const + { + return IB::mPos >= it.pos(); + } + /***************************** * ContainerRangeFactory * *****************************/ - template - ContainerRangeFactory::ContainerRangeFactory(const std::shared_ptr&... rs) + template + ContainerRangeFactory::ContainerRangeFactory(const std::shared_ptr&... rs) { - mProd = std::shared_ptr >( new ContainerRange( rs... ) ); + mProd = std::shared_ptr >( new ContainerRange( rs... ) ); } - template - ContainerRangeFactory:: - ContainerRangeFactory(const typename ContainerRange::SpaceType& space) + template + ContainerRangeFactory:: + ContainerRangeFactory(const typename ContainerRange::SpaceType& space) { - mProd = std::shared_ptr >( new ContainerRange( space ) ); + mProd = std::shared_ptr >( new ContainerRange( space ) ); } - template - std::shared_ptr ContainerRangeFactory::create() + template + std::shared_ptr ContainerRangeFactory::create() { setSelf(); return mProd; @@ -408,64 +546,65 @@ namespace MultiArrayTools * ContainerRange * **********************/ - template - ContainerRange::ContainerRange(const std::shared_ptr&... rs) : + template + ContainerRange::ContainerRange(const std::shared_ptr&... rs) : mSpace( std::make_tuple( rs... ) ) {} - template - ContainerRange::ContainerRange(const SpaceType& space) : mSpace( space ) {} + template + ContainerRange::ContainerRange(const SpaceType& space) : mSpace( space ) {} - template - size_t ContainerRange::dim() const + template + size_t ContainerRange::dim() const { return sizeof...(Ranges); } - template - size_t ContainerRange::size() const + template + size_t ContainerRange::size() const { return RPackNum::getSize(mSpace); } - template + template template - auto ContainerRange::get() const -> decltype( *std::get( mSpace ) )& + auto ContainerRange::get() const -> decltype( *std::get( mSpace ) )& { return *std::get( mSpace ); } - template + template template - auto ContainerRange::getPtr() const -> decltype( std::get( mSpace ) )& + auto ContainerRange::getPtr() const -> decltype( std::get( mSpace ) )& { return std::get( mSpace ); } - template - const typename ContainerRange::SpaceType& ContainerRange::space() const + template + const typename ContainerRange::SpaceType& ContainerRange::space() const { return mSpace; } - template - typename ContainerRange::IndexType ContainerRange::begin() const + template + typename ContainerRange::IndexType ContainerRange::begin() const { - ContainerIndex - i( std::dynamic_pointer_cast > + ContainerIndex + i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = 0; return i; } - template - typename ContainerRange::IndexType ContainerRange::end() const + template + typename ContainerRange::IndexType ContainerRange::end() const { - ContainerIndex - i( std::dynamic_pointer_cast > + ContainerIndex + i( std::dynamic_pointer_cast > ( std::shared_ptr( RB::mThis ) ) ); i = size(); return i; } + } // end namespace MultiArrayTools diff --git a/src/include/ranges/multi_range.h b/src/include/ranges/multi_range.h index c58839d..06b71e9 100644 --- a/src/include/ranges/multi_range.h +++ b/src/include/ranges/multi_range.h @@ -51,7 +51,8 @@ namespace MultiArrayTools // ( have to assign sub-indices (ptr!) correctly ) //MultiIndex(const MultiIndex& in); //MultiIndex& operator=(const MultiIndex& in); - MultiIndex& operator=(ContainerIndex& ci); + template + MultiIndex& operator=(ContainerIndex& ci); template MultiIndex(const std::shared_ptr& range); @@ -127,7 +128,9 @@ namespace MultiArrayTools MultiRangeFactory() = delete; MultiRangeFactory(const std::shared_ptr&... rs); MultiRangeFactory(const typename MultiRange::SpaceType& space); - MultiRangeFactory(const std::shared_ptr >& cr); + + template + MultiRangeFactory(const std::shared_ptr >& cr); virtual std::shared_ptr create() override; }; @@ -217,7 +220,8 @@ namespace MultiArrayTools } */ template - MultiIndex& MultiIndex::operator=(ContainerIndex& ci) + template + MultiIndex& MultiIndex::operator=(ContainerIndex& ci) { RPackNum::copyInst(mIPack, ci); IB::mPos = RPackNum::makePos(mIPack); @@ -441,7 +445,8 @@ namespace MultiArrayTools } template - MultiRangeFactory::MultiRangeFactory(const std::shared_ptr >& cr) + template + MultiRangeFactory::MultiRangeFactory(const std::shared_ptr >& cr) { mProd = std::shared_ptr< MultiRange >( new MultiRange( cr->space() ) ); } diff --git a/src/include/ranges/rbase_def.h b/src/include/ranges/rbase_def.h index 5d0cc67..e2c160a 100644 --- a/src/include/ranges/rbase_def.h +++ b/src/include/ranges/rbase_def.h @@ -51,15 +51,15 @@ namespace MultiArrayTools class MultiIndex; // container_range.h - template + template class ContainerRangeFactory; // container_range.h - template + template class ContainerRange; // container_range.h - template + template class ContainerIndex; // anonymous_range.h diff --git a/src/include/ranges/rpack_num.h b/src/include/ranges/rpack_num.h index ab76076..c547e2e 100644 --- a/src/include/ranges/rpack_num.h +++ b/src/include/ranges/rpack_num.h @@ -162,9 +162,9 @@ namespace MultiArrayHelper RPackNum::construct(ip, range); } - template class IndexType, class... Indices> + template static void copyInst(std::tuple...>& ip, - const IndexType& ind) + const IndexType& ind) { std::get(ip) = ind.template getPtr() ; RPackNum::copyInst(ip, ind); @@ -341,9 +341,9 @@ namespace MultiArrayHelper std::get<0>(ip) = std::shared_ptr( new SubIndexType( range.template getPtr<0>() ) ); } - template class IndexType, class... Indices> + template static void copyInst(std::tuple...>& ip, - const IndexType& ind) + const IndexType& ind) { std::get<0>(ip) = ind.template getPtr<0>(); } diff --git a/src/tests/ma_unit_test.cc b/src/tests/ma_unit_test.cc index 52cb8ae..5cf35ad 100644 --- a/src/tests/ma_unit_test.cc +++ b/src/tests/ma_unit_test.cc @@ -111,7 +111,7 @@ namespace { EXPECT_EQ( ma[ i.at('g') ], 0.577); } - + TEST_F(MATest_1Dim, ForLoop) { std::vector v2 = { 0.693 , 2.718, 3.141, 1.618, 9.98 }; @@ -136,7 +136,7 @@ namespace { } EXPECT_EQ(cnt, ma.size()); } - + TEST_F(MATest_1Dim, ReFormat) { swapFactory( rfbptr, { 'a', 'c', 'e', 'g', 'i' } ); diff --git a/src/tests/op_unit_test.cc b/src/tests/op_unit_test.cc index 83b6e04..7c9a99c 100644 --- a/src/tests/op_unit_test.cc +++ b/src/tests/op_unit_test.cc @@ -91,6 +91,9 @@ namespace { typedef MultiRangeFactory MRF; typedef MRF::oType MRange; + + typedef AnonymousRange ANO; + typedef MultiArray AMA; OpTest_MDim() { @@ -419,6 +422,38 @@ namespace { EXPECT_EQ( xround( res.at(mkt(mkt('3','b'),'A')) ), xround(2.911 + 0.373 + 1.470) ); EXPECT_EQ( xround( res.at(mkt(mkt('3','b'),'B')) ), xround(2.911 + 0.373 + 2.210) ); } + + TEST_F(OpTest_MDim, ExecAnonOp1) + { + MultiArray res(mr1ptr,sr4ptr); + MultiArray ma1(mr1ptr, v3); + MultiArray ma2(sr2ptr, v1); + MultiArray ma3(sr4ptr, v4); + + auto si1 = MAT::getIndex( sr2ptr ); + auto si2 = MAT::getIndex( sr3ptr ); + auto si3 = MAT::getIndex( sr4ptr ); + auto mi = MAT::getIndex( mr1ptr ); + mi->operator()(si1,si2); + + res(mi,si3) = ma1(mi) + ma2(si1) + ma3(si3); + + EXPECT_EQ( xround( res.at(mkt(mkt('1','a'),'A')) ), xround(0.353 + 2.917 + 1.470) ); + EXPECT_EQ( xround( res.at(mkt(mkt('1','a'),'B')) ), xround(0.353 + 2.917 + 2.210) ); + EXPECT_EQ( xround( res.at(mkt(mkt('1','b'),'A')) ), xround(4.005 + 2.917 + 1.470) ); + EXPECT_EQ( xround( res.at(mkt(mkt('1','b'),'B')) ), xround(4.005 + 2.917 + 2.210) ); + + EXPECT_EQ( xround( res.at(mkt(mkt('2','a'),'A')) ), xround(1.070 + 9.436 + 1.470) ); + EXPECT_EQ( xround( res.at(mkt(mkt('2','a'),'B')) ), xround(1.070 + 9.436 + 2.210) ); + EXPECT_EQ( xround( res.at(mkt(mkt('2','b'),'A')) ), xround(2.310 + 9.436 + 1.470) ); + EXPECT_EQ( xround( res.at(mkt(mkt('2','b'),'B')) ), xround(2.310 + 9.436 + 2.210) ); + + EXPECT_EQ( xround( res.at(mkt(mkt('3','a'),'A')) ), xround(9.243 + 0.373 + 1.470) ); + EXPECT_EQ( xround( res.at(mkt(mkt('3','a'),'B')) ), xround(9.243 + 0.373 + 2.210) ); + EXPECT_EQ( xround( res.at(mkt(mkt('3','b'),'A')) ), xround(2.911 + 0.373 + 1.470) ); + EXPECT_EQ( xround( res.at(mkt(mkt('3','b'),'B')) ), xround(2.911 + 0.373 + 2.210) ); + } + } // anonymous namspace diff --git a/src/tests/ranges/index_unit_test.cc b/src/tests/ranges/index_unit_test.cc index fa089ef..1f37092 100644 --- a/src/tests/ranges/index_unit_test.cc +++ b/src/tests/ranges/index_unit_test.cc @@ -48,7 +48,7 @@ namespace { typedef MultiRangeFactory MasterRF; typedef MasterRF::oType MasterRange; - typedef ContainerRangeFactory CRF; + typedef ContainerRangeFactory CRF; typedef CRF::oType CRange; IndexTest()