// -*- C++ -*- #ifndef __multi_array_operation_h__ #define __multi_array_operation_h__ #include #include #include #include #include #include "base_def.h" #include "mbase_def.h" #include "ranges/rheader.h" #include "pack_num.h" #include "arith.h" namespace MultiArrayTools { namespace { using namespace MultiArrayHelper; } template class OperationBase { public: OperationClass& THIS() { return static_cast(*this); } const OperationClass& THIS() const { return static_cast(*this); } template auto operator+(const OperationBase& in) const -> Operation,OperationClass,Second>; template auto operator-(const OperationBase& in) const -> Operation,OperationClass,Second>; template auto operator*(const OperationBase& in) const -> Operation,OperationClass,Second>; template auto operator/(const OperationBase& in) const -> Operation,OperationClass,Second>; template auto c(const std::shared_ptr& ind) const -> Contraction; template auto sl(const std::shared_ptr&... inds) const -> ConstSlice; template auto slc(const std::shared_ptr&... inds) const -> SliceContraction; template auto p(const std::shared_ptr&... inds) const -> ConstOperationRoot; template auto to(const std::shared_ptr&... inds) const -> MultiArray; template auto addto(const std::shared_ptr&... inds) const -> MultiArray; template // Args = Operation Classes auto a(const std::shared_ptr>& ll, const Args&... args) const -> Operation,OperationClass, Args...>; private: friend OperationClass; friend OperationTemplate; OperationBase() = default; }; template class OperationTemplate : public OperationBase { /* empty per default; specialize if needed */ private: OperationTemplate() = default; friend OperationClass; }; template struct SelfIdentity { static inline T& sapply(T& a, T b) { return a = b; } }; template class OperationMaster { public: class AssignmentExpr { private: AssignmentExpr() = default; //OperationMaster mM; OpClass mSec; T* mDataPtr; public: static constexpr size_t LAYER = 0; static constexpr size_t SIZE = OpClass::SIZE; typedef decltype(mSec.rootSteps()) ExtType; //AssignmentExpr(OperationMaster& m, const OpClass& sec); AssignmentExpr(T* dataPtr, const OpClass& sec); AssignmentExpr(const AssignmentExpr& in) = default; AssignmentExpr(AssignmentExpr&& in) = default; inline void operator()(size_t start = 0); inline void operator()(size_t start, ExtType last); auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; }; class AddExpr { private: AddExpr() = default; //OperationMaster mM; OpClass mSec; T* mDataPtr; public: static constexpr size_t LAYER = 0; static constexpr size_t SIZE = OpClass::SIZE; typedef decltype(mSec.rootSteps()) ExtType; //AssignmentExpr(OperationMaster& m, const OpClass& sec); AddExpr(T* dataPtr, const OpClass& sec); AddExpr(const AddExpr& in) = default; AddExpr(AddExpr&& in) = default; inline void operator()(size_t start = 0); inline void operator()(size_t start, ExtType last); auto rootSteps(std::intptr_t iPtrNum = 0) const -> ExtType; }; typedef T value_type; //typedef OperationBase OB; typedef ContainerRange CRange; typedef ContainerIndex IndexType; //typedef typename MultiRange::IndexType IndexType; OperationMaster(MutableMultiArrayBase& ma, const OpClass& second, IndexType& index, bool doParallel = false); OperationMaster(T* data, const OpClass& second, IndexType& index, bool doParallel = false); inline void set(size_t pos, T val) { AOp::sapply(mDataPtr[pos],val); } //inline void add(size_t pos, T val) { mDataPtr[pos] += val; } inline T get(size_t pos) const; private: void performAssignment(std::intptr_t blockIndexNum); OpClass const& mSecond; //MutableMultiArrayBase& mArrayRef; T* mDataPtr; IndexType mIndex; bool mDoParallel; }; template class ConstOperationRoot : public OperationTemplate > { public: typedef T value_type; typedef OperationBase > OT; typedef ContainerRange CRange; typedef ContainerIndex IndexType; static constexpr size_t SIZE = 1; static constexpr bool CONT = true; ConstOperationRoot(const MultiArrayBase& ma, const std::shared_ptr&... indices); ConstOperationRoot(std::shared_ptr > maptr, const std::shared_ptr&... indices); ConstOperationRoot(const T* data, const IndexType& ind); template inline const T& get(ET pos) const; template inline ConstOperationRoot& set(ET pos); MExt rootSteps(std::intptr_t iPtrNum = 0) const; // nullptr for simple usage with decltype template Expr loop(Expr exp) const; const T* data() const; private: //MultiArrayBase const& mArrayRef; const T* mDataPtr; const T* mOrigDataPtr; IndexType mIndex; //size_t mOff = 0; std::shared_ptr > mMaPtr; // never remove this ptr, otherwise we lose temporary container instances! }; template class StaticCast : public OperationTemplate > { private: Op mOp; public: typedef T value_type; typedef OperationBase > OT; typedef typename Op::CRange CRange; typedef typename Op::IndexType IndexType; static constexpr size_t SIZE = Op::SIZE; static constexpr bool CONT = false; StaticCast(const Op& op); template inline T get(ET pos) const; template inline StaticCast& set(ET pos); auto rootSteps(std::intptr_t iPtrNum = 0) const -> decltype(mOp.rootSteps(iPtrNum)); template Expr loop(Expr exp) const; }; template StaticCast staticcast(const Op& op) { return StaticCast(op); } template class MetaOperationRoot : public OperationTemplate > { public: typedef typename Range::IndexType IndexType; typedef typename IndexType::MetaType value_type; typedef OperationBase > OT; static constexpr size_t SIZE = 1; static constexpr bool CONT = false; MetaOperationRoot(const std::shared_ptr& ind); template inline value_type get(ET pos) const; template inline MetaOperationRoot& set(ET pos); MExt rootSteps(std::intptr_t iPtrNum = 0) const; // nullptr for simple usage with decltype template Expr loop(Expr exp) const; private: //MultiArrayBase const& mArrayRef; //const T* mDataPtr; mutable IndexType mWorkIndex; std::shared_ptr mIndex; }; template class OperationRoot : public OperationTemplate > { public: typedef T value_type; typedef OperationBase > OT; typedef ContainerRange CRange; typedef ContainerIndex IndexType; static constexpr size_t SIZE = 1; static constexpr bool CONT = true; OperationRoot(MutableMultiArrayBase& ma, const std::shared_ptr&... indices); OperationRoot(T* data, const IndexType& ind); /* template OperationMaster,OpClass,Ranges...> operator=(const OpClass& in); template OperationMaster,OpClass,Ranges...> operator+=(const OpClass& in); OperationMaster,OperationRoot,Ranges...> operator=(const OperationRoot& in); */ template OperationRoot& operator=(const OpClass& in); template OperationRoot& operator+=(const OpClass& in); OperationRoot& operator=(const OperationRoot& in); OperationRoot& par(); template inline T& get(ET pos) const; template inline OperationRoot& set(ET pos); MExt rootSteps(std::intptr_t iPtrNum = 0) const; // nullptr for simple usage with decltype template Expr loop(Expr exp) const; T* data() const; template auto sl(const std::shared_ptr&... inds) -> Slice; private: //MutableMultiArrayBase& mArrayRef; T* mDataPtr; T* mOrigDataPtr; IndexType mIndex; //size_t mOff = 0; bool mDoParallel = false; }; template class OperationValue : public OperationTemplate > { public: typedef T value_type; typedef OperationBase > OT; typedef ContainerRange CRange; typedef ContainerIndex IndexType; static constexpr size_t SIZE = 1; static constexpr bool CONT = true; OperationValue(const T& val); template inline const T& get(ET pos) const; template inline OperationValue& set(ET pos); MExt rootSteps(std::intptr_t iPtrNum = 0) const; // nullptr for simple usage with decltype template Expr loop(Expr exp) const; private: T mVal; }; template size_t sumRootNum() { return typename Op::rootNum(); } template size_t sumRootNum() { return typename Op1::rootNum() + sumRootNum(); } template struct RootSumN { template struct rs { static constexpr size_t SIZE = Op1::SIZE + RootSumN::template rs::SIZE; }; }; template <> struct RootSumN<0> { template struct rs { static constexpr size_t SIZE = Op1::SIZE; }; }; template struct RootSum { static constexpr size_t SIZE = RootSumN::template rs::SIZE; }; template class Operation : public OperationTemplate > { public: typedef T value_type; typedef OperationBase > OT; typedef OpFunction F; static constexpr size_t SIZE = RootSum::SIZE; static constexpr bool FISSTATIC = OpFunction::FISSTATIC; static constexpr bool CONT = false; private: std::tuple mOps; std::shared_ptr mF; // only if non-static public: typedef decltype(PackNum::template mkSteps(0, mOps)) ETuple; Operation(const Ops&... ops); Operation(std::shared_ptr ff, const Ops&... ops); template inline T get(ET pos) const; template inline Operation& set(ET pos); auto rootSteps(std::intptr_t iPtrNum = 0) const // nullptr for simple usage with decltype -> decltype(PackNum::mkSteps(iPtrNum, mOps)); template auto loop(Expr exp) const -> decltype(PackNum::mkLoop( mOps, exp)); }; namespace { template struct OpMaker { template static inline auto mkOperation(const std::shared_ptr& f, const Ops&... ops) -> Operation { return Operation(f,ops...); } }; template <> struct OpMaker { template static inline auto mkOperation(const std::shared_ptr& f, const Ops&... ops) -> Operation { return Operation(ops...); } }; } template auto mkOperation(const std::shared_ptr& f, const Ops&... ops) -> Operation { return OpMaker::mkOperation(f, ops...); } template class Contraction : public OperationTemplate > { public: typedef T value_type; typedef OperationBase > OT; static constexpr size_t SIZE = Op::SIZE; static constexpr bool CONT = Op::CONT; private: Op mOp; std::shared_ptr mInd; public: typedef decltype(mOp.rootSteps(0)) ETuple; Contraction(const Op& op, std::shared_ptr ind); template inline auto get(ET pos) const -> decltype(mOp.template get(pos)); template inline Contraction& set(ET pos); auto rootSteps(std::intptr_t iPtrNum = 0) const // nullptr for simple usage with decltype -> decltype(mOp.rootSteps(iPtrNum)); template auto loop(Expr exp) const -> decltype(mInd->iforh(1,mOp.loop(exp))); }; template // class SliceContraction : public OperationTemplate //, //SliceContraction,Op,Indices...> > class SliceContraction : public OperationTemplate > { public: typedef MultiArray value_type; typedef OperationTemplate > OT; static constexpr size_t SIZE = Op::SIZE; static constexpr bool CONT = false; private: mutable Op mOp; mutable std::shared_ptr > mCont; mutable OperationRoot mTarOp; public: typedef decltype(mOp.rootSteps(0)) ETuple; SliceContraction(const Op& op, std::shared_ptr... ind); template inline const value_type& get(ET pos) const; template inline SliceContraction& set(ET pos); auto rootSteps(std::intptr_t iPtrNum = 0) const // nullptr for simple usage with decltype -> decltype(mOp.rootSteps(iPtrNum)); template auto loop(Expr exp) const -> decltype(mOp.loop(exp)); // no loop }; } #include "type_operations.h" #endif