diff --git a/src/base_def.h b/src/base_def.h new file mode 100644 index 0000000..9be6977 --- /dev/null +++ b/src/base_def.h @@ -0,0 +1,68 @@ +// -*- C++ -*- + +#ifndef __base_def_h__ +#define __base_def_h__ + +#define DEFAULT_MEMBERS(__class_name__) __class_name__() = default; \ + __class_name__(const __class_name__& in) = default; \ + __class_name__& operator=(const __class_name__& in) = default; \ + __class_name__(__class_name__&& in) = default; \ + __class_name__& operator=(__class_name__&& in) = default + +namespace MultiArrayTools +{ + /*********************** + * Provided Types * + ***********************/ + + // range_base.h + enum class RangeType; + + // range_base.h + class MultiRangeType; + + // range_base.h + template + class RangeBase; + + // range_base.h + template + class SubRangeBase; + + // index_base.h + class IndefinitIndexBase; + + // index_base.h + template + class IndexBase; + + // single_range.h + template + class SingleIndex; + + // single_range.h + template + class SingleRange; + + // multi_range.h + template + class MultiIndex; + + // multi_range.h + template + class MultiRange; + + // multi_array.h + template + class MultiArray; + + // multi_array_operation.h + template + class MultiArrayOperationBase; + + // multi_array_operation.h + template + class MultiArrayOperation; +} + +#endif diff --git a/src/index_base.h b/src/index_base.h new file mode 100644 index 0000000..383fdd2 --- /dev/null +++ b/src/index_base.h @@ -0,0 +1,87 @@ +// -*- C++ -*- + +#ifndef __index_base_h__ +#define __index_base_h__ + +#include +#include + +#include "base_def.h" +#include "range_base.h" + +namespace MultiArrayTools +{ + + class IndefinitIndexBase + { + public: + virtual IndefinitIndexBase& operator=(const IndefinitIndexBase& in) = 0; + + 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 IndefinitIndexBase& i) = 0; + virtual bool operator!=(const IndefinitIndexBase& i) = 0; + + virtual size_t dim() const = 0; + 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(); + virtual bool linked() const; + virtual void linkTo(IndefinitIndexBase* target) = 0; + + virtual void setPos(size_t pos); + + virtual size_t max() const; + virtual size_t outOfRange() const; + + protected: + + DEFAULT_MEMBERS(IndefinitIndexBase); + + std::string mName; + size_t mPos; + + IndefinitIndexBase* mLinked; + }; + + 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: + + DEFAULT_MEMBERS(IndexBase); + + // translate index into position + virtual size_t evaluate(const Index& in) const = 0; + RangeBase* mRange; + }; + +} + +#include "index_base.cc" + +#endif diff --git a/src/me.h b/src/me.h deleted file mode 100644 index 28591c4..0000000 --- a/src/me.h +++ /dev/null @@ -1,410 +0,0 @@ -// -*- C++ -*- - -#ifndef __me_h__ -#define __me_h__ - -#include -#include -#include -#include - -#define DEFAULT_MEMBERS(__class_name__) __class_name__() = default; \ - __class_name__(const __class_name__& in) = default; \ - __class_name__& operator=(const __class_name__& in) = default; \ - __class_name__(__class_name__&& in) = default; \ - __class_name__& operator=(__class_name__&& in) = default - -namespace ME -{ - - template - auto make_left_x(const Tuple& tp) -> decltype(std::tuple_cat(make_left(tp), - std::make_tuple(get(tp)))) - { - return std::tuple_cat(make_left(tp), std::make_tuple(get(tp))); - } - - template - auto make_left_x(const Tuple& tp) -> decltype(std::make_tuple(get(tp))) - { - return std::make_tuple(get(tp)); - } - - template - auto make_right_x(const Tuple& tp) -> decltype(std::tuple_cat(std::make_tuple(get(tp)), - make_left(tp))) - { - const size_t M = std::tuple_size(tp) - N; - return std::tuple_cat(std::make_tuple(get(tp)), make_left(tp)); - } - - template - auto make_right_x(const Tuple& tp) -> decltype(std::make_tuple(get(tp))) - { - const size_t M = std::tuple_size(tp); - return std::make_tuple(get(tp)); - } - - template - auto make_left(const Tuple& tp) -> decltype(make_left_x(tp)) - { - return make_left_x(tp); - } - - template - auto make_right(const Tuple& tp) -> decltype(make_right_x(tp)) - { - return make_right_x(tp); - } - - /***********************************************************/ - - /****************** - * Range * - ******************/ - - enum class RangeType - { - NIL = 0, - SPACE = 1, - MOMENTUM = 2, - LORENTZ = 3, - SPIN = 4 - }; - - class MultiRangeType - { - public: - - DEFAULT_MEMBERS(MultiRangeType); - - 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; - - bool operator==(const MultiRangeType& in) const; - bool operator!=(const MultiRangeType& in) const; - - private: - void setType(RangeType type); - void setMultiType(const std::vector& multiType); - - RangeType mType; - std::vector* mMultiType; - }; - - template - class RangeBase - { - public: - 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: - DEFAULT_MEMBERS(RangeBase); - - }; - - template - auto cross(const Range& r1, const Range& r2) -> /**/; - - template - auto cross(const Range1& r1, const Range2& r2) -> /**/; - - template - class SubRangeBase : public RangeBase - { - public: - virtual bool isSubRange() const override; - protected: - DEFAULT_MEMBERS(SubRangeBase); - RangeBase* mBase; - std::vector mOccupation; - }; - - template ; - class SingleIndex; - - template - class MultiIndex; - - template - class SingleRange : public RangeBase > - { - public: - DEFAULT_MEMBERS(SingleRange); - - const U& get(size_t pos) const; - size_t get(const U& metaPos) const; - - protected: - std::vector mSpace; - }; - - template - class MultiRange : public RangeBase > - { - public: - - DEFAULT_MEMBERS(MultiRange); - static size_t dim = sizeof...(Ranges); - - template - auto get() -> decltype( std::get(mSpace) ); - - protected: - std::tuple mSpace; - }; - - /****************** - * Index * - ******************/ - - class IndefinitIndexBase - { - public: - virtual IndefinitIndexBase& operator=(const IndefinitIndexBase& in) = 0; - - 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 IndefinitIndexBase& i) = 0; - virtual bool operator!=(const IndefinitIndexBase& i) = 0; - - virtual size_t dim() const = 0; - 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(); - virtual bool linked() const; - virtual void linkTo(IndefinitIndexBase* target) = 0; - - virtual void setPos(size_t pos); - - virtual size_t max() const; - virtual size_t outOfRange() const; - - protected: - - DEFAULT_MEMBERS(IndefinitIndexBase); - - std::string mName; - size_t mPos; - - IndefinitIndexBase* mLinked; - }; - - 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: - - DEFAULT_MEMBERS(IndexBase); - - // translate index into position - virtual size_t evaluate(const Index& in) const = 0; - RangeBase* mRange; - }; - - template - class SingleIndex : public IndexBase > - { - public: - - DEFAULT_MEMBERS(SingleIndex); - - 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 size_t dim() const override; // = 1 - - virtual void linkTo(IndefinitIndexBase* target) override; - - protected: - virtual size_t evaluate(const Index& in) const override; - }; - - template - class MultiIndex : public IndexBase > - { - public: - - DEFAULT_MEMBERS(MultiIndex); - - typedef std::tuple IndexPack; - static size_t sMult = sizeof...(Indices); - - virtual MultiIndex& operator++() override; - virtual MultiIndex& operator--() override; - virtual MultiIndex& operator+=(int n) override; - virtual MultiIndex& operator-=(int n) override; - - template - auto& getIndex() -> decltype(std::get(mIPack)); - - template - const auto& getIndex() const -> decltype(std::get(mIPack)); - - IndefinitIndexBase& getIndex(size_t n); - const IndefinitIndexBase& getIndex(size_t n) const; - - // dimension of MultiRange - virtual size_t dim() const override; // implement !!! - - virtual bool link(IndefinitIndexBase* toLink) override; - virtual void linkTo(IndefinitIndexBase* target) override; - - protected: - - virtual bool linkLower(IndefinitIndexBase* toLink); - virtual size_t evaluate(const MultiIndex& in) const override; - - IndexPack mIPack; - }; - - - /****************** - * MultiArray * - ******************/ - - template - class MultiArray - { - public: - - DEFAULT_MEMBERS(MultiArray); - MultiArray(const Range& range); // !!!! - - template - MultiArrayOperation& operator()(const NameTypes&... str) const; - - T& operator()(const typename Range::indexType& i); - const T& operator()(const typename Range::indexType& i) const; - - private: - bool mInit = false; - std::vector mCont; - std::shared_ptr mRange; - }; - - - - - /*************************** - * MultiArrayOperation * - ***************************/ - - template - class MultiArrayOperationBase - { - public: - - MultiArrayOperation(MultiArray& ma, const IndefinitIndexBase& iib); - - // execute AnyOperation - // exception if range types are inconsitent with names - template - MultiArrayOperationBase& operator=(const MultiArrayOperationBase& in); - - - template - MultiArrayOperation operator()(Operation& op, MultiArrayOperationBase&... secs); - - template - MultiArrayOperation,Range2> operator+(MultiArrayOperationBase& sec); - - template - MultiArrayOperation,Range2> operator-(MultiArrayOperationBase& sec); - - template - MultiArrayOperation,Range2> operator*(MultiArrayOperationBase& sec); - - template - MultiArrayOperation,Range2> operator/(MultiArrayOperationBase& sec); - - virtual size_t argNum() const; - - IndefinitIndexBase* index(); - - virtual void linkIndicesTo(IndefinitIndexBase* target); - - virtual T& get(); - virtual const T& get() const; - - protected: - - MultiArray& mArrayRef; - IndefinitIndexBase* mIibPtr; - - }; - - template - class MultiArrayOperation : public MultiArrayOperationBase - { - public: - - MultiArrayOperation(Operation& op, MultiArrayOperationBase&... secs); - virtual size_t argNum() const override; - - virtual void linkIndicesTo(IndefinitIndexBase* target) override; - - virtual T& get() override; - virtual const T& get() const override; - - protected: - - T mVal; - Operation mOp; - std::tuple... > mSecs; - }; - - // ========= - // Code that should finally work: - - MultiArray ma1; - MultiArray ma2; - MultiArray ma3; - - 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") - - -} // end namespace ME - -#include "me.cc" - -#endif diff --git a/src/multi_array.h b/src/multi_array.h new file mode 100644 index 0000000..5398fed --- /dev/null +++ b/src/multi_array.h @@ -0,0 +1,38 @@ +// -*- C++ -*- + +#ifndef __multi_array_h__ +#define __multi_array_h__ + +#include + +#include "base_def.h" +#include "multi_array_operation.h" + +namespace MultiArrayTools +{ + + template + class MultiArray + { + public: + + DEFAULT_MEMBERS(MultiArray); + MultiArray(const Range& range); // !!!! + + template + MultiArrayOperation& operator()(const NameTypes&... str) const; + + T& operator()(const typename Range::indexType& i); + const T& operator()(const typename Range::indexType& i) const; + + private: + bool mInit = false; + std::vector mCont; + std::shared_ptr mRange; + }; + +} + +#include "multi_array.cc" + +#endif diff --git a/src/multi_array_operation.h b/src/multi_array_operation.h new file mode 100644 index 0000000..d188c9a --- /dev/null +++ b/src/multi_array_operation.h @@ -0,0 +1,85 @@ +// -*- C++ -*- + +#ifndef __multi_array_operation_h__ +#define __multi_array_operation_h__ + +#include +#include + +#include "base_def.h" +#include "index_base.h" + +namespace MultiArrayTools +{ + + template + class MultiArrayOperationBase + { + public: + + MultiArrayOperation(MultiArray& ma, const IndefinitIndexBase& iib); + + // execute AnyOperation + // exception if range types are inconsitent with names + template + MultiArrayOperationBase& operator=(const MultiArrayOperationBase& in); + + + template + MultiArrayOperation operator()(Operation& op, MultiArrayOperationBase&... secs); + + template + MultiArrayOperation,Range2> operator+(MultiArrayOperationBase& sec); + + template + MultiArrayOperation,Range2> operator-(MultiArrayOperationBase& sec); + + template + MultiArrayOperation,Range2> operator*(MultiArrayOperationBase& sec); + + template + MultiArrayOperation,Range2> operator/(MultiArrayOperationBase& sec); + + virtual size_t argNum() const; + + IndefinitIndexBase* index(); + + virtual void linkIndicesTo(IndefinitIndexBase* target); + + virtual T& get(); + virtual const T& get() const; + + protected: + + MultiArray& mArrayRef; + IndefinitIndexBase* mIibPtr; + + }; + + template + class MultiArrayOperation : public MultiArrayOperationBase + { + public: + + MultiArrayOperation(Operation& op, MultiArrayOperationBase&... secs); + virtual size_t argNum() const override; + + virtual void linkIndicesTo(IndefinitIndexBase* target) override; + + virtual T& get() override; + virtual const T& get() const override; + + protected: + + T mVal; + Operation mOp; + std::tuple... > mSecs; + }; + + + +} + +#include "multi_array_operation.h" + +#endif diff --git a/src/multi_range.h b/src/multi_range.h new file mode 100644 index 0000000..5871a0a --- /dev/null +++ b/src/multi_range.h @@ -0,0 +1,73 @@ +// -*- C++ -*- + +#ifndef __multi_range_h__ +#define __multi_range_h__ + +#include +#include + +#include "base_def.h" +#include "range_base.h" +#include "index_base.h" + +namespace MultiArrayTools +{ + + template + class MultiIndex : public IndexBase > + { + public: + + DEFAULT_MEMBERS(MultiIndex); + + typedef std::tuple IndexPack; + static size_t sMult = sizeof...(Indices); + + virtual MultiIndex& operator++() override; + virtual MultiIndex& operator--() override; + virtual MultiIndex& operator+=(int n) override; + virtual MultiIndex& operator-=(int n) override; + + template + auto& getIndex() -> decltype(std::get(mIPack)); + + template + const auto& getIndex() const -> decltype(std::get(mIPack)); + + IndefinitIndexBase& getIndex(size_t n); + const IndefinitIndexBase& getIndex(size_t n) const; + + // dimension of MultiRange + virtual size_t dim() const override; // implement !!! + + virtual bool link(IndefinitIndexBase* toLink) override; + virtual void linkTo(IndefinitIndexBase* target) override; + + protected: + + virtual bool linkLower(IndefinitIndexBase* toLink); + virtual size_t evaluate(const MultiIndex& in) const override; + + IndexPack mIPack; + }; + + template + class MultiRange : public RangeBase > + { + public: + + DEFAULT_MEMBERS(MultiRange); + static size_t dim = sizeof...(Ranges); + + template + auto get() -> decltype( std::get(mSpace) ); + + protected: + std::tuple mSpace; + }; + +} + +#include "multi_range.cc" + +#endif diff --git a/src/range_base.h b/src/range_base.h new file mode 100644 index 0000000..b92bdff --- /dev/null +++ b/src/range_base.h @@ -0,0 +1,85 @@ +// -*- C++ -*- + +#ifndef __range_base_h__ +#define __range_base_h__ + +#include +#include +#include "base_def.h" + +namespace MultiArrayTools +{ + + enum class RangeType + { + NIL = 0, + SPACE = 1, + MOMENTUM = 2, + LORENTZ = 3, + SPIN = 4 + }; + + class MultiRangeType + { + public: + + DEFAULT_MEMBERS(MultiRangeType); + + 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; + + bool operator==(const MultiRangeType& in) const; + bool operator!=(const MultiRangeType& in) const; + + private: + void setType(RangeType type); + void setMultiType(const std::vector& multiType); + + RangeType mType; + std::vector* mMultiType; + }; + + template + class RangeBase + { + public: + 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: + DEFAULT_MEMBERS(RangeBase); + + }; + + template + auto cross(const Range& r1, const Range& r2) -> /**/; + + template + auto cross(const Range1& r1, const Range2& r2) -> /**/; + + template + class SubRangeBase : public RangeBase + { + public: + virtual bool isSubRange() const override; + protected: + DEFAULT_MEMBERS(SubRangeBase); + RangeBase* mBase; + std::vector mOccupation; + }; + +} + +#include "range_base.cc" + +#endif diff --git a/src/single_range.h b/src/single_range.h new file mode 100644 index 0000000..abee6d5 --- /dev/null +++ b/src/single_range.h @@ -0,0 +1,53 @@ +// -*- C++ -*- + +#ifndef __single_range_h__ +#define __single_range_h__ + +#include +#include + +#include "base_def.h" +#include "index_base.h" +#include "range_base.h" + +namespace MultiArrayTools +{ + + template + class SingleIndex : public IndexBase > + { + public: + + DEFAULT_MEMBERS(SingleIndex); + + 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 size_t dim() const override; // = 1 + + virtual void linkTo(IndefinitIndexBase* target) override; + + protected: + virtual size_t evaluate(const Index& in) const override; + }; + + template + class SingleRange : public RangeBase > + { + public: + DEFAULT_MEMBERS(SingleRange); + + const U& get(size_t pos) const; + size_t get(const U& metaPos) const; + + protected: + std::vector mSpace; + }; + +} + +#include "single_range.cc" + +#endif diff --git a/src/tuple_helper.h b/src/tuple_helper.h new file mode 100644 index 0000000..d3f99b0 --- /dev/null +++ b/src/tuple_helper.h @@ -0,0 +1,60 @@ +// -*- C++ -*- + +#ifndef __tuple_helper_h__ +#define __tuple_helper_h__ + +#include +#include + +#include "base_def.h" + + +namespace MultiArrayTools +{ + + template + auto make_left_x(const Tuple& tp) -> decltype(std::tuple_cat(make_left(tp), + std::make_tuple(get(tp)))) + { + return std::tuple_cat(make_left(tp), std::make_tuple(get(tp))); + } + + template + auto make_left_x(const Tuple& tp) -> decltype(std::make_tuple(get(tp))) + { + return std::make_tuple(get(tp)); + } + + template + auto make_right_x(const Tuple& tp) -> decltype(std::tuple_cat(std::make_tuple(get(tp)), + make_left(tp))) + { + const size_t M = std::tuple_size(tp) - N; + return std::tuple_cat(std::make_tuple(get(tp)), make_left(tp)); + } + + template + auto make_right_x(const Tuple& tp) -> decltype(std::make_tuple(get(tp))) + { + const size_t M = std::tuple_size(tp); + return std::make_tuple(get(tp)); + } + + template + auto make_left(const Tuple& tp) -> decltype(make_left_x(tp)) + { + return make_left_x(tp); + } + + template + auto make_right(const Tuple& tp) -> decltype(make_right_x(tp)) + { + return make_right_x(tp); + } + + +} + +#include "tuple_helper.cc" + +#endif