further development

This commit is contained in:
Christian Zimmermann 2017-02-14 18:03:42 +01:00
parent 5a369fb08a
commit 3363671d35
2 changed files with 198 additions and 44 deletions

123
src/me.cc
View file

@ -5,6 +5,52 @@
namespace ME namespace ME
{ {
/*********************
* MultiRangeType *
*********************/
MultiRangeType& MultiRangeType::operator=(RangeType& type)
{
setType(type);
return *this;
}
MultiRangeType& MultiRangeType::operator=(const std::vector<MultiRangeType>& 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<MultiRangeType>& multiType)
{
mMultiType = new std::vector<MultiRangeType>( multiType );
mType = RangeType::NIL;
}
/****************** /******************
* RangeBase * * RangeBase *
******************/ ******************/
@ -47,6 +93,41 @@ namespace ME
} }
return cnt; 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<IndefinitIndexBase*>();
}
if(not isAlready){
mLinked->push_back(toLink);
}
return true;
}
else {
return false;
}
}
void IndefinitIndexBase::freeLinked()
{
delete mLinked;
mLinked = nullptr;
}
/************** /**************
* IndexBase * * IndexBase *
@ -116,7 +197,7 @@ namespace ME
} }
template <typename U, IndexType TYPE> template <typename U, IndexType TYPE>
size_t SingleIndexBase<U,TYPE>::evaluate(const Index& in) size_t SingleIndexBase<TYPE>::evaluate(const Index& in)
{ {
return in.mPos; return in.mPos;
} }
@ -142,12 +223,38 @@ namespace ME
return subIndex.pos(); return subIndex.pos();
} }
} }
template <class... Indices> template <class... Indices>
size_t MultiIndex<Indices...>::evaluate(const MultiIndex<Indices...>& in) const size_t MultiIndex<Indices...>::evaluate(const MultiIndex<Indices...>& in) const
{ {
return evaluate_x<sizeof...(Indices)-1>(in); return evaluate_x<sizeof...(Indices)-1>(in);
} }
template <class... Indices>
bool MultiIndex<Indices...>::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<IndefinitIndexBase*>();
}
if(not isAlready){
mLinked->push_back(toLink);
}
return true;
}
else {
return /*try each element in mIPack*/;
}
}
/******************* /*******************
* MultiArray * * 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 } // end namespace ME

119
src/me.h
View file

@ -59,13 +59,33 @@ namespace ME
enum class RangeType enum class RangeType
{ {
SPACE, NIL = 0,
MOMENTUM, SPACE = 1,
LORENTZ, MOMENTUM = 2,
SPIN LORENTZ = 3,
SPIN = 4
}; };
class MultiRangeType
{
public:
MultiRangeType& operator=(RangeType& type);
MultiRangeType& operator=(const std::vector<MultiRangeType>& 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<MultiRangeType>& multiType);
RangeType mType;
std::vector<MultiRangeType>* mMultiType;
};
template <class Index> template <class Index>
class RangeBase class RangeBase
{ {
@ -131,34 +151,62 @@ namespace ME
* Index * * Index *
******************/ ******************/
template <Index> class IndefinitIndexBase
class IndexBase
{ {
public: public:
virtual Index& operator=(const Index& in); virtual IndefinitIndexBase& operator=(const IndefinitIndexBase& in) = 0;
virtual Index& operator=(size_t pos); virtual IndefinitIndexBase& operator=(size_t pos) = 0;
virtual Index& operator++(); virtual IndefinitIndexBase& operator++() = 0;
virtual Index& operator--(); virtual IndefinitIndexBase& operator--() = 0;
virtual Index& operator+=(int n); virtual IndefinitIndexBase& operator+=(int n) = 0;
virtual Index& operator-=(int n); virtual IndefinitIndexBase& operator-=(int n) = 0;
virtual bool operator==(const IndexBase& i); virtual bool operator==(const IndefinitIndexBase& i) = 0;
virtual bool operator!=(const IndexBase& i); virtual bool operator!=(const IndefinitIndexBase& i) = 0;
virtual size_t dim() const = 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(); std::string& name();
const std::string& name() const; 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<IndefinitIndexBase*>* mLinked; // if *this modified, modify mLinked the same way
};
template <class Index>
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: protected:
// translate index into position // translate index into position
virtual size_t evaluate(const Index& in) const = 0; virtual size_t evaluate(const Index& in) const = 0;
std::string mName;
size_t mPos;
RangeBase<Index>* mRange; RangeBase<Index>* mRange;
}; };
@ -169,8 +217,8 @@ namespace ME
virtual size_t size() const override; virtual size_t size() const override;
virtual SingleIndexBase& operator=(size_t pos) override; virtual SingleIndexBase& operator=(size_t pos) override;
virtual SingleIndexBase& operator=(const U& upos); //virtual SingleIndexBase& operator=(const U& upos);
virtual const U& getMetaPos() const; //virtual const U& getMetaPos() const;
// = 1 // = 1
virtual size_t dim() const override; // implement !!! virtual size_t dim() const override; // implement !!!
@ -178,7 +226,7 @@ namespace ME
virtual size_t evaluate(const Index& in) const override; virtual size_t evaluate(const Index& in) const override;
}; };
template <class... Indices>template <class... Indices> template <class... Indices>
class MultiIndex : public IndexBase<MultiIndex<Indices...> > class MultiIndex : public IndexBase<MultiIndex<Indices...> >
{ {
public: public:
@ -190,6 +238,8 @@ namespace ME
// dimension of MultiRange // dimension of MultiRange
virtual size_t dim() const override; // implement !!! virtual size_t dim() const override; // implement !!!
virtual bool link(IndefinitIndexBase* toLink) override;
protected: protected:
virtual size_t evaluate(const MultiIndex& in) const override; virtual size_t evaluate(const MultiIndex& in) const override;
@ -231,31 +281,20 @@ namespace ME
* MultiArrayOperation * * MultiArrayOperation *
***************************/ ***************************/
template <typename T, class Range, class Operation> class NamedMultiArray
class MultiArrayOperation
{ {
public: public:
// only creates maximum output range
MultiArrayOperation<T,Range,UnaryOperation> operation(UnaryOperation uo) const;
// only creates maximum output range
template <class Range2, class BinaryOperation, class AnyOperation>
auto operation(const MultiArrayOperation<T, Range2, AnyOperation>& in,
BinaryOperation bo) const
-> MultiArrayOperation<T, decltype( cross( Range, Range2 ) ), BinaryOperation>;
// execute AnyOperation // execute AnyOperation
// exception if range types are inconsitent with names // exception if range types are inconsitent with names
template<class Range2, class AnyOperation> template <class Range2, class AnyOperation>
MultiArrayOperation& operator=(const MultiArrayOperation<T, Range2, AnyOperation>& in); MultiArrayOperation& operator=(const MultiArrayOperation<T, Range2, AnyOperation>& in);
IndefinitIndexBase& getIndex(const std::string& name);
const IndefinitIndexBase& getIndex(const std::string& name) const;
private: private:
MultiArray<T,Range>& mMaRef; std::map<std::string, IndefinitIndexBase> mIndexNameMap;
std::map<size_t,std::string> mIndexNameMap;
}; };
@ -268,7 +307,7 @@ namespace ME
ma3("mu","nu","lambda","x","y","z") = ma1("mu","nu","x","y") * ma2("nu","lambda","y","z"); ma3("mu","nu","lambda","x","y","z") = ma1("mu","nu","x","y") * ma2("nu","lambda","y","z");
// operator= operation() // 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 } // end namespace ME