From 2d5d7770b0faa243ac4dd9f731cdad9753de93e1 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Fri, 3 Feb 2017 14:01:15 +0100 Subject: [PATCH] intermediate commit --- src/me.cc | 196 +++++++++++++++++++++++++++++------------------------- src/me.h | 125 +++++++++++++++++++--------------- 2 files changed, 180 insertions(+), 141 deletions(-) diff --git a/src/me.cc b/src/me.cc index 0c0803d..890ee39 100644 --- a/src/me.cc +++ b/src/me.cc @@ -5,52 +5,120 @@ namespace ME { + /****************** + * RangeBase * + ******************/ + + template + bool RangeBase::isSubRange() const + { + return false; + } + + /********************* + * SubRangeBase * + *********************/ + + template + bool SubRangeBase::isSubRange() const + { + return true; + } + + /******************** + * SingleRange * + ********************/ + + template + const U& SingleRange::get(size_t pos) const + { + return mSpace[pos]; + } + + template + size_t SingleRange::get(const U& metaPos) const + { + size_t cnt = 0; + for(auto& x: mSpace){ + if(x == metaPos){ + return cnt; + } + ++cnt; + } + return cnt; + } + /************** * IndexBase * **************/ - - size_t IndexBase::size() const + + template + Index& IndexBase::operator=(const Index& in) { - return mPoss->size(); + mPos = evaluate(in); } - IndexBase& IndexBase::operator=(size_t pos) + template + Index& IndexBase::operator=(size_t pos) { mPos = pos; + return *this; } + template + Index& IndexBase::operator++() + { + ++mPos; + return *this; + } + + template + Index& IndexBase::operator--() + { + --mPos; + return *this; + } + + template + Index& IndexBase::operator+=(int n) + { + mPos += n; + return *this; + } + + template + Index& IndexBase::operator-=(int n) + { + mPos -= n; + return *this; + } + + template + bool IndexBase::operator==(const Index& i) + { + return mRange == i.mRange and mPos == i.mPos; + } + + template + bool IndexBase::operator!=(const Index& i) + { + return mRange != i.mRange or mPos != i.mPos; + } /******************** * SingleIndexBase * ********************/ - template - SingleIndexBase& SingleIndexBase::operator=(size_t pos) + const U& SingleIndexBase::getMetaPos() const { - mPos = pos; - mActPos = (*mPoss)[pos]; + return dynamic_cast( mRange )->get(mPos); } template - SingleIndexBase& SingleIndexBase::operator=(const U& upos) + size_t SingleIndexBase::evaluate(const Index& in) { - size_t cnt = 0; - for(auto& x: *mPoss){ - if(x == upos){ - mPos = cnt; - return *this; - } - ++cnt; - } - // THROW !! - return *this; - } - - template - const U& SingleIndexBase::getActPos() const - { - return mActPos; + return in.mPos; } @@ -60,77 +128,27 @@ namespace ME namespace { - - template - size_t size(const Tuple& tp, size_t curSize = 1) + template + size_t evaluate_x(const MultiIndex& index) { - return size( tp, std::get(tp).size() * curSize ); - } - - template - size_t size(const Tuple& tp, size_t curSize = 1) - { - return std::get<0>(tp).size() * curSize; + const auto& subIndex = index.getIndex(); + return evaluate_x(index) * subIndex.size() + subIndex.pos(); } - template - size_t pos(const Tuple& tp) + template + size_t evaluate_x<0>(const MultiIndex& index) { - const size_t M = std::tuple_size(tp) - N; - return pos(tp) * std::get(tp).size() + std::get(tp).pos() - } - - template - size_t pos(const Tuple& tp) - { - const size_t M = std::tuple_size(tp); - return std::get(tp).pos(); - } - - size_t varpos(size_t curPos, const I& i) - { - return curPos * i.size() + i.pos(); - } - - template - size_t varpos(size_t curPos, const I& i, const Is&... is) - { - return varpos(curPos * i.size() + i.pos(), is...); + const auto& subIndex = index.getIndex<0>(); + return subIndex.pos(); } } + + template + size_t MultiIndex::evaluate(const MultiIndex& in) const + { + return evaluate_x(in); + } - template - size_t MultiIndexBase::size() const - { - return size(mCont); - } - - template - MultiIndexBase& MultiIndexBase::operator=(const ContType& upos) - { - mCont = upos; - mPos = pos(mCont); - } - - const ContType& MultiIndexBase::getActPos() const - { - return mCont; - } - - template - auto together() -> decltype(std::tuple_cat(make_left(mCont), - make_right(mCont) )) - { - return std::tuple_cat(make_left(mCont), make_right(mCont)); - } - - template - auto add(const I& in) -> decltype(std::tuple_cat(make_left(mCont), in, - make_right(mCont))) - { - return std::tuple_cat(make_left(mCont), in, make_right(mCont)); - } - /******************* * MultiArray * *******************/ diff --git a/src/me.h b/src/me.h index 1745be4..b16e9fa 100644 --- a/src/me.h +++ b/src/me.h @@ -70,13 +70,15 @@ namespace ME class RangeBase { public: - static Index indexType; + typedef Index IndexType; virtual size_t size() const = 0; virtual Index begin() = 0; virtual Index end() = 0; virtual RangeBase* base() = 0; virtual bool isSubRange() const; + protected: + mutable std::string mName; }; template @@ -98,23 +100,21 @@ namespace ME template class SingleRange : public RangeBase > { + public: + const U& get(size_t pos) const; + size_t get(const U& metaPos) const; + protected: std::vector mSpace; }; template - class MultiRange : public RangeBase > + class MultiRange : public RangeBase > { public: static size_t dim = sizeof...(Ranges); - - template - auto combine(const MultiRange& in) -> MultiRange; - - template - auto merge() -> /**/; - + protected: std::tuple mSpace; }; @@ -122,64 +122,66 @@ namespace ME /****************** * Index * ******************/ - + + template class IndexBase { public: - virtual size_t pos() const; - virtual IndexBase& operator=(size_t pos); - virtual IndexBase& operator++(); - //virtual IndexBase operator++(int); - virtual IndexBase& operator--(); - //virtual IndexBase operator--(int); - virtual IndexBase& operator+=(int n); - //virtual IndexBase operator+(int n); - virtual IndexBase& operator-=(int n); - //virtual IndexBase operator-(int n); + virtual Index& operator=(const Index& in); + + virtual Index& operator=(size_t pos); + virtual Index& operator++(); + virtual Index& operator--(); + virtual Index& operator+=(int n); + virtual Index& operator-=(int n); + virtual bool operator==(const IndexBase& i); virtual bool operator!=(const IndexBase& i); + + virtual size_t dim() const = 0; + virtual size_t pos() const; // = mPos; implement !!! + protected: + // translate index into position + virtual size_t evaluate(const Index& in) const = 0; + size_t mPos; - RangeBase* mRange; + RangeBase* mRange; }; template - class SingleIndex : public IndexBase + class SingleIndex : public IndexBase > { public: virtual size_t size() const override; virtual SingleIndexBase& operator=(size_t pos) override; virtual SingleIndexBase& operator=(const U& upos); - virtual const U& getActPos() const; + virtual const U& getMetaPos() const; + + // = 1 + virtual size_t dim() const override; // implement !!! protected: + virtual size_t evaluate(const Index& in) const override; }; - template - class MultiIndex : public IndexBase + template template + class MultiIndex : public IndexBase > { public: - typedef std::tuple ContType; + typedef std::tuple IndexPack; - virtual size_t size() const override; - - virtual MultiIndex& operator=(const U& upos); - - virtual const ContType& getActPos() const; - - // treat index N and N+1 as the same index template - auto together() -> decltype(std::tuple_cat(make_left(mCont), - make_right(mCont))); - - // Inserts an index in 'mCont' at position 'N' - template - auto add(const I& in) -> decltype(std::tuple_cat(make_left(mCont), in, - make_right(mCont))); + auto getIndex() -> decltype(std::get(mIPack)); + + // dimension of MultiRange + virtual size_t dim() const override; // implement !!! protected: - ContType mCont; + virtual size_t evaluate(const MultiIndex& in) const override; + + IndexPack mIPack; }; /****************** @@ -199,26 +201,45 @@ namespace ME MultiArray(const Range& range); // !!!! - template - MultiArray operation(std::array& d1, - std::array& d2, - const MultiArray& in) const; + template + MultiArrayOperation& operator()(const Strings&... str) const; - T& operator()(const typename Range::indexType& i); - const T& operator()(const typename Range::indexType& i) const; + T& operator()(const typename Range::indexType& i); // implement + const T& operator()(const typename Range::indexType& i) const; // implement private: bool mInit = false; std::vector mCont; + std::shared_ptr mRange; }; - - enum class NameTag + template + class MultiArrayOperation { - // to enumerate the ranges/indices when merging two MultiArrays, etc... - }; + public: + + template + MultiArrayOperation operation(/*some operation*/) const; - template + template + MultiArrayOperation operation(const MultiArrayOperation& in, /*some operation*/) const; + + // execute operation + template + MultiArray operator()(const Strings&... str); + }; + + + // ========= + // Code that should finally work: + + MultiArray ma1; + MultiArray ma2; + MultiArray ma3; + + ma3 = ( ma1("mu","nu","x","y") * ma2("nu","lambda","y","z") )("mu","nu","lambda","x","y","z"); + + } // end namespace ME