diff --git a/src/me.cc b/src/me.cc index 498db26..49c12d1 100644 --- a/src/me.cc +++ b/src/me.cc @@ -5,6 +5,52 @@ namespace ME { + /********************* + * MultiRangeType * + *********************/ + + MultiRangeType& MultiRangeType::operator=(RangeType& type) + { + setType(type); + return *this; + } + + MultiRangeType& MultiRangeType::operator=(const std::vector& multiType) + { + setMultiType(multiType); + return *this; + } + + MultiRangeType& MultiRangeType::operator[](size_t num) + { + return mMultiType->at(num); + } + + const MultiRangeType& MultiRangeType::operator[](size_t num) const + { + return mMultiType->at(num); + } + + bool MultiRangeType::multi() const + { + return mType != nullptr; + } + + void MultiRangeType::setType(RangeType type) + { + mType = type; + if(mMultiType != nullptr){ + delete mMultiType; + } + mMultiType = nullptr; + } + + void MultiRangeType::setMultiType(const std::vector& multiType) + { + mMultiType = new std::vector( multiType ); + mType = RangeType::NIL; + } + /****************** * RangeBase * ******************/ @@ -47,6 +93,41 @@ namespace ME } return cnt; } + + /************************ + * IndefinitIndexBase * + ************************/ + + bool IndefinitIndexBase::link(IndefinitIndexBase* toLink) + { + if(toLink->name() == name() and toLink->rangeType() == rangeType()){ + bool isAlready = false; + if(mLinked != nullptr){ + for(auto& x: *mLinked){ + if(x == toLink){ + isAlready = true; + break; + } + } + } + else { + mLinked = new std::vector(); + } + if(not isAlready){ + mLinked->push_back(toLink); + } + return true; + } + else { + return false; + } + } + + void IndefinitIndexBase::freeLinked() + { + delete mLinked; + mLinked = nullptr; + } /************** * IndexBase * @@ -116,7 +197,7 @@ namespace ME } template - size_t SingleIndexBase::evaluate(const Index& in) + size_t SingleIndexBase::evaluate(const Index& in) { return in.mPos; } @@ -142,12 +223,38 @@ namespace ME return subIndex.pos(); } } - + template size_t MultiIndex::evaluate(const MultiIndex& in) const { return evaluate_x(in); } + + template + bool MultiIndex::link(IndefinitIndexBase* toLink) + { + if(toLink->name() == name() and toLink->rangeType() == rangeType()){ + bool isAlready = false; + if(mLinked != nullptr){ + for(auto& x: *mLinked){ + if(x == toLink){ + isAlready = true; + break; + } + } + } + else { + mLinked = new std::vector(); + } + if(not isAlready){ + mLinked->push_back(toLink); + } + return true; + } + else { + return /*try each element in mIPack*/; + } + } /******************* * MultiArray * @@ -167,10 +274,18 @@ namespace ME /*************************** - * MultiArrayOperation * + * NamedMultiArray * ***************************/ - + IndefinitIndexBase& getIndex(const std::string& name) + { + return mIndexNameMap.at(name); + } + + const IndefinitIndexBase& getIndex(const std::string& name) const + { + return mIndexNameMap.at(name); + } } // end namespace ME diff --git a/src/me.h b/src/me.h index ca0466c..a5a95a9 100644 --- a/src/me.h +++ b/src/me.h @@ -59,13 +59,33 @@ namespace ME enum class RangeType { - SPACE, - MOMENTUM, - LORENTZ, - SPIN + NIL = 0, + SPACE = 1, + MOMENTUM = 2, + LORENTZ = 3, + SPIN = 4 }; + class MultiRangeType + { + public: + MultiRangeType& operator=(RangeType& type); + MultiRangeType& operator=(const std::vector& multiType); + + MultiRangeType& operator[](size_t num); + const MultiRangeType& operator[](size_t num) const; + + bool multi() const; + + private: + void setType(RangeType type); + void setMultiType(const std::vector& multiType); + + RangeType mType; + std::vector* mMultiType; + }; + template class RangeBase { @@ -131,34 +151,62 @@ namespace ME * Index * ******************/ - template - class IndexBase + class IndefinitIndexBase { public: - virtual Index& operator=(const Index& in); + virtual IndefinitIndexBase& operator=(const IndefinitIndexBase& in) = 0; - virtual Index& operator=(size_t pos); - virtual Index& operator++(); - virtual Index& operator--(); - virtual Index& operator+=(int n); - virtual Index& operator-=(int n); + virtual IndefinitIndexBase& operator=(size_t pos) = 0; + virtual IndefinitIndexBase& operator++() = 0; + virtual IndefinitIndexBase& operator--() = 0; + virtual IndefinitIndexBase& operator+=(int n) = 0; + virtual IndefinitIndexBase& operator-=(int n) = 0; - virtual bool operator==(const IndexBase& i); - virtual bool operator!=(const IndexBase& i); + virtual bool operator==(const IndefinitIndexBase& i) = 0; + virtual bool operator!=(const IndefinitIndexBase& i) = 0; virtual size_t dim() const = 0; - virtual size_t pos() const; // = mPos; implement !!! + virtual size_t pos() const = 0; // = mPos; implement !!! std::string& name(); const std::string& name() const; + + MultiRangeType rangeType() const = 0; + + virtual bool link(IndefinitIndexBase* toLink); + virtual void freeLinked(); + + protected: + + std::string mName; + size_t mPos; + + // => change: if two should be linked, link the second in *mLinked + // and so on for even more Indices + // IndefinitIndexBase* mLinked; + std::vector* mLinked; // if *this modified, modify mLinked the same way + }; + + template + class IndexBase : public IndefinitIndexBase + { + public: + virtual Index& operator=(const Index& in) override; + + virtual Index& operator=(size_t pos) override; + virtual Index& operator++() override; + virtual Index& operator--() override; + virtual Index& operator+=(int n) override; + virtual Index& operator-=(int n) override; + + virtual bool operator==(const IndexBase& i) override; + virtual bool operator!=(const IndexBase& i) override; + + virtual size_t pos() const override; // = mPos; implement !!! protected: // translate index into position virtual size_t evaluate(const Index& in) const = 0; - - std::string mName; - - size_t mPos; RangeBase* mRange; }; @@ -169,8 +217,8 @@ namespace ME virtual size_t size() const override; virtual SingleIndexBase& operator=(size_t pos) override; - virtual SingleIndexBase& operator=(const U& upos); - virtual const U& getMetaPos() const; + //virtual SingleIndexBase& operator=(const U& upos); + //virtual const U& getMetaPos() const; // = 1 virtual size_t dim() const override; // implement !!! @@ -178,7 +226,7 @@ namespace ME virtual size_t evaluate(const Index& in) const override; }; - template template + template class MultiIndex : public IndexBase > { public: @@ -190,6 +238,8 @@ namespace ME // dimension of MultiRange virtual size_t dim() const override; // implement !!! + + virtual bool link(IndefinitIndexBase* toLink) override; protected: virtual size_t evaluate(const MultiIndex& in) const override; @@ -231,31 +281,20 @@ namespace ME * MultiArrayOperation * ***************************/ - template - class MultiArrayOperation + class NamedMultiArray { public: - // only creates maximum output range - MultiArrayOperation operation(UnaryOperation uo) const; - - // only creates maximum output range - template - auto operation(const MultiArrayOperation& in, - BinaryOperation bo) const - -> MultiArrayOperation; - - // execute AnyOperation // exception if range types are inconsitent with names - template + template MultiArrayOperation& operator=(const MultiArrayOperation& in); - + + IndefinitIndexBase& getIndex(const std::string& name); + const IndefinitIndexBase& getIndex(const std::string& name) const; private: - MultiArray& mMaRef; - - std::map mIndexNameMap; + std::map mIndexNameMap; }; @@ -268,7 +307,7 @@ namespace ME ma3("mu","nu","lambda","x","y","z") = ma1("mu","nu","x","y") * ma2("nu","lambda","y","z"); // operator= operation() - ("mu","nu","lambda","x","y","z") <--- ("mu","nu","x","y","nu","lambda","y","z") + // ("mu","nu","lambda","x","y","z") <--- ("mu","nu","x","y","nu","lambda","y","z") } // end namespace ME