diff --git a/src/index_base.cc b/src/index_base.cc index 39c641e..457eee6 100644 --- a/src/index_base.cc +++ b/src/index_base.cc @@ -26,6 +26,11 @@ namespace MultiArrayTools return mPos; } + size_t IndexBase::max() const + { + return mRangePtr->size(); + } + operator IndexBase::size_t() const { return mPos; diff --git a/src/index_base.h b/src/index_base.h index eff765e..3f31cf9 100644 --- a/src/index_base.h +++ b/src/index_base.h @@ -18,15 +18,13 @@ namespace MultiArrayTools { public: DEFAULT_MEMBERS(IndexBase); - IndexBase(const RangeBase& range, size_t pos); + IndexBase(const std::shared_ptr& range, size_t pos); virtual ~IndexBase() = default; virtual IndexBase& operator=(size_t pos) = 0; virtual IndexBase& operator++() = 0; virtual IndexBase& operator--() = 0; - virtual IndexBase& operator+=(int n) = 0; - virtual IndexBase& operator-=(int n) = 0; - + bool operator==(const IndexBase& in) const; bool operator!=(const IndexBase& in) const; diff --git a/src/multi_range.cc b/src/multi_range.cc index 07caaab..59dce8b 100644 --- a/src/multi_range.cc +++ b/src/multi_range.cc @@ -398,6 +398,29 @@ namespace MultiArrayTools PackNum::setMeta(mIPack, metaPos); return *this; } + + /************************* + * MultiRangeFactory * + *************************/ + + template + MultiRangeFactory::MultiRangeFactory(const std::shared_ptr&... rs) + { + mProd = std::make_shared< MultiRange >( rs... ); + } + + template + MultiRangeFactory::MultiRangeFactory(const MultiRange::SpaceType& st) + { + mProd = std::make_shared< MultiRange >( st ); + } + + template + std::shared_ptr MultiRangeFactory::create() + { + setSelf(); + return mProd; + } /****************** * MultiRange * @@ -422,14 +445,6 @@ namespace MultiArrayTools { return PackNum::getSize(mSpace); } - - template - MultiRangeType MultiRange::type() const - { - std::vector rvec; - PackNum::buildRangeVec(rvec, mSpace); - return MultiRangeType(rvec); - } template const typename MultiRange::SpaceType& MultiRange::space() const @@ -440,26 +455,22 @@ namespace MultiArrayTools template MultiIndex MultiRange::begin() const { - std::tuple...> is; - PackNum::setBegin(is,mSpace); - return MultiIndex(this, is); + MultiIndex i( std::dynamic_pointer_cast >( mThis ) ); + return i = 0; } template MultiIndex MultiRange::end() const { - std::tuple...> is; - PackNum::setEnd(is,mSpace); - return ++MultiIndex(this, is); + MultiIndex i( std::dynamic_pointer_cast >( mThis ) ); + return i = size(); } template - std::shared_ptr MultiRange::indexInstance() const + std::shared_ptr MultiRange::index() const { - std::tuple...> is; - PackNum::setBegin(is,mSpace); - std::shared_ptr sptr(new MultiIndex(this, is)); - return sptr; + return std::make_shared > + ( std::dynamic_pointer_cast >( mThis ) ); } } diff --git a/src/multi_range.h b/src/multi_range.h index 747b3c6..7670ae7 100644 --- a/src/multi_range.h +++ b/src/multi_range.h @@ -60,11 +60,25 @@ namespace MultiArrayTools }; - /***************************** - * IndexGetter Functions * - ****************************/ + /************************* + * MultiRangeFactory * + *************************/ + + template + class MultiRangeFactory : public RangeFactory + { + public: + MultiRangeFactory() = delete; + MultiRangeFactory(const std::shared_ptr&... rs); + MultiRangeFactory(const MultiRange::SpaceType& st); + + virtual std::shared_ptr create() override; + }; - + /****************** + * MultiRange * + ******************/ + template class MultiRange : public RangeInterface > { @@ -89,6 +103,8 @@ namespace MultiArrayTools virtual typename IndexType end() const override; virtual std::shared_ptr index() const override; + + friend MultiRangeFactory; protected: diff --git a/src/range_base.cc b/src/range_base.cc index a8d3660..ce5c39b 100644 --- a/src/range_base.cc +++ b/src/range_base.cc @@ -7,5 +7,14 @@ namespace MultiArrayTools * RangeBase * ******************/ + bool RangeBase::operator==(const RangeBase& in) const + { + return this == ∈ + } + + bool RangeBase::operator!=(const RangeBase& in) const + { + return this != ∈ + } } diff --git a/src/range_base.h b/src/range_base.h index 1f2ea3e..b644f69 100644 --- a/src/range_base.h +++ b/src/range_base.h @@ -25,6 +25,23 @@ namespace MultiArrayTools DISTANCE = 8 }; + class RangeFactoryBase + { + public: + + RangeFactoryBase() = default; + virtual ~RangeFactoryBase() = default; + + // should return mProd !! + virtual std::shared_ptr create() = 0; + + protected: + + // call this function before returning product !! + void setSelf() { mProd->mThis = mProd; } + std::shared_ptr mProd; + }; + class RangeBase { public: @@ -35,9 +52,16 @@ namespace MultiArrayTools virtual size_t dim() const = 0; virtual std::shared_ptr index() const = 0; + + bool operator==(const RangeBase& in) const; + bool operator!=(const RangeBase& in) const; + + friend RangeFactoryBase; protected: - + + RangeBase() = default; + std::shared_ptr mThis; }; template @@ -49,6 +73,9 @@ namespace MultiArrayTools virtual Index begin() = 0; virtual Index end() = 0; + + protected: + RangeInterface() = default; }; } diff --git a/src/single_range.cc b/src/single_range.cc index 13235b8..02a1f79 100644 --- a/src/single_range.cc +++ b/src/single_range.cc @@ -7,136 +7,69 @@ namespace MultiArrayTools * SingleIndex * ******************/ - template - SingleIndex::SingleIndex(const U& upos, - std::shared_ptr > >& rangePtr) : - IndexBase >(rangePtr) - { - IIB::setPos( dynamic_pointer_cast >( IB::mRangePtr )->get(upos) ); - } + template + template + SingleIndex::SingleIndex(const std::shared_ptr >& range) : + IndexInterface(range, 0) {} - template - SingleIndex::SingleIndex(std::shared_ptr > >& rangePtr, - size_t pos) : IndexBase >(range) + template + SingleIndex& SingleIndex::operator=(size_t pos) { - IIB::setPos( pos ); - } - - template - SingleIndex& SingleIndex::operator=(size_t pos) - { - IIB::setPos( pos ); + mPos = pos; return *this; } - template - SingleIndex& SingleIndex::operator++() + template + SingleIndex& SingleIndex::operator++() { - IIB::setPos( IIB::pos() + 1 ); + ++mPos; return *this; } - template - SingleIndex& SingleIndex::operator--() + template + SingleIndex& SingleIndex::operator--() { - IIB::setPos( IIB::pos() - 1 ); + --mPos; return *this; } - template - SingleIndex& SingleIndex::operator+=(int n) - { - IIB::setPos( IIB::pos() + n ); - return *this; - } - - template - SingleIndex& SingleIndex::operator-=(int n) - { - IIB::setPos( IIB::pos() - n ); - return *this; - } - - template - bool SingleIndex::operator==(const SingleIndex& i) - { - return IB::mRange == i.mRange and IIB::pos() == static_cast( i.mPos ); - } - - template - bool SingleIndex::operator!=(const SingleIndex& i) - { - return IB::mRange != i.mRange or IIB::pos() != static_cast( i.mPos ); - } - - template - MultiRangeType SingleIndex::rangeType() const - { - return MultiRangeType(TYPE); - } - - template - U SingleIndex::getMetaPos() const + template + U SingleIndex::meta() const { return dynamic_cast const*>( IB::mRange )->get(IIB::pos()); } - template - SingleIndex& SingleIndex::atMeta(const U& metaPos) + template + SingleIndex& SingleIndex::at(const U& metaPos) { operator=( dynamic_cast const*>( IB::mRange )->getMeta(metaPos) ); return *this; } - template - size_t SingleIndex::dim() const + template + size_t SingleIndex::dim() const { return 1; } - - template - size_t SingleIndex::evaluate(const SingleIndex& in) const + + template + bool SingleIndex::last() const { - return in.pos(); + return mPos == mRangePtr->size() - 1; } - template - void SingleIndex::copyPos(const SingleIndex& in) + template + bool SingleIndex::first() const { - IIB::setPos(in.pos()); - } - - template - size_t SingleIndex::giveSubStepSize(IndefinitIndexBase* subIndex) - { - if(subIndex == this){ - return 1; - } - else { - return 0; - } + return mPos == 0; } - std::ostream& operator<<(std::ostream& os, VET vet) - { - os << ( (vet == VET::VALUE) ? std::string("VALUE") : std::string("ERROR") ); - return os; - } - - /* - template - void SingleIndex::eval() - { - IIB::setPos( evaluate( *this ) ); - } - */ - /******************** * SingleRange * ********************/ template - SingleRange::SingleRange(const std::vector& space) : RangeBase >(), + SingleRange::SingleRange(const std::vector& space) : RangeBase >(), mSpace(space) {} template @@ -165,24 +98,34 @@ namespace MultiArrayTools } template - MultiRangeType SingleRange::type() const + size_t SingleRange::dim() const { - return MultiRangeType(TYPE); + return 1; + } + + // put this in the interface class !!! + template + std::shared_ptr SingleRange::index() const + { + return std::make_shared > + ( std::dynamic_pointer_cast >( mThis ) ); } template - SingleIndex SingleRange::begin() const + SingleIndex SingleRange::begin() const { - return SingleIndex(this, 0); + SingleIndex i(mThis); + return i = 0; } template - SingleIndex SingleRange::end() const + SingleIndex SingleRange::end() const { - return SingleIndex(this, mSpace.size()); + SingleIndex i(mThis); + return i = size(); } - // specializations + // specializations (not updated!!!) SingleRange::SingleRange(size_t ext) : RangeBase >(), diff --git a/src/single_range.h b/src/single_range.h index cbc5c34..3e9ee85 100644 --- a/src/single_range.h +++ b/src/single_range.h @@ -14,69 +14,47 @@ namespace MultiArrayTools { - template - class SingleIndex : public IndexBase > + template + class SingleIndex : public IndexInterface { public: - - typedef IndexBase > IB; - typedef IndefinitIndexBase IIB; - DEFAULT_MEMBERS(SingleIndex); - SingleIndex(const U& upos, std::shared_ptr > >& rangePtr); - - SingleIndex(std::shared_ptr > >& rangePtr, - size_t pos = 0); - - //virtual SingleIndex& operator=(const U& upos); + template + SingleIndex(const std::shared_ptr >& range); virtual SingleIndex& operator=(size_t pos) override; virtual SingleIndex& operator++() override; virtual SingleIndex& operator--() override; - virtual SingleIndex& operator+=(int n) override; - virtual SingleIndex& operator-=(int n) override; - virtual bool operator==(const SingleIndex& i); - virtual bool operator!=(const SingleIndex& i); - - virtual MultiRangeType rangeType() const override; - - virtual U getMetaPos() const; - virtual SingleIndex& atMeta(const U& metaPos); + virtual U meta() const; + virtual SingleIndex& at(const U& metaPos); virtual size_t dim() const override; // = 1 - virtual void copyPos(const SingleIndex& in) override; - - virtual size_t giveSubStepSize(IndefinitIndexBase* subIndex) override; - - //virtual void eval() override; - //virtual bool virt() const override { return false; } - - protected: - virtual size_t evaluate(const SingleIndex& in) const override; + virtual bool last() const override; + virtual bool first() const override; }; template - class SingleRange : public RangeBase > + class SingleRange : public RangeInterface > { public: - typedef typename RangeBase >::IndexType IndexType; - - static SingleRange oType() { return SingleRange(); } + typedef typename RangeBase >::IndexType IndexType; virtual size_t size() const override; - //U get(size_t pos) const; const U& get(size_t pos) const; size_t getMeta(const U& metaPos) const; - - virtual MultiRangeType type() const override; - SingleIndex begin() const override; - SingleIndex end() const override; + virtual SingleIndex begin() const override; + virtual SingleIndex end() const override; + + friend SingleRangeFactory; protected: + + SingleRange() = delete; + SingleRange(const SingleRange& in) = delete; SingleRange(const std::vector& space);