// -*- 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" #include "xfor/xfor.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; template auto operator-(const OperationBase& in) const; template auto operator*(const OperationBase& in) const; template auto operator/(const OperationBase& in) const; 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 auto pto(const std::shared_ptr&... inds) const -> MultiArray; template auto paddto(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 AssignmentExpr2 : public ExpressionBase { private: AssignmentExpr2() = default; Target mTar; OpClass mSec; T* mDataPtr; public: static constexpr size_t LAYER = 0; static constexpr size_t SIZE = Target::SIZE + OpClass::SIZE; typedef decltype(mTar.rootSteps(0).extend( mSec.rootSteps(0) )) ExtType; AssignmentExpr2(T* dataPtr, const Target& tar, const OpClass& sec); AssignmentExpr2(const AssignmentExpr2& in) = default; AssignmentExpr2(AssignmentExpr2&& 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; inline void operator()(size_t mlast, DExt last) override final; inline DExt dRootSteps(std::intptr_t iPtrNum = 0) const override final; inline DExt dExtension() const override final; }; //template template class AddExpr { private: AddExpr() = default; Target mTar; OpClass mSec; T* mDataPtr; public: static constexpr size_t LAYER = 0; static constexpr size_t SIZE = Target::SIZE + OpClass::SIZE; typedef decltype(mTar.rootSteps(0).extend( mSec.rootSteps(0) )) ExtType; // static constexpr size_t LAYER = 0; //static constexpr size_t SIZE = OpClass::SIZE; //typedef decltype(mSec.rootSteps()) ExtType; //AddExpr(T* dataPtr, const OpClass& sec); AddExpr(T* dataPtr, const Target& tar, 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; }; 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: const T* mDataPtr; const T* mOrigDataPtr; IndexType mIndex; 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: 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; private: T* mDataPtr; T* mOrigDataPtr; IndexType mIndex; public: OperationRoot(MutableMultiArrayBase& ma, const std::shared_ptr&... indices); OperationRoot(T* data, const IndexType& ind); template auto assign(const OpClass& in) const -> decltype(mIndex.ifor(1,in.loop(AssignmentExpr2,OpClass> (mOrigDataPtr,*this,in)))); template auto assignExpr(const OpClass& in) const -> decltype(in.loop(AssignmentExpr2,OpClass>(mOrigDataPtr,*this,in))); template auto assign(const OpClass& in, const std::shared_ptr& i) const -> decltype(i->ifor(1,in.loop(AssignmentExpr2,OpClass> (mOrigDataPtr,*this,in)))); template auto plus(const OpClass& in) const -> decltype(mIndex.ifor(1,in.loop(AddExpr,OpClass> (mOrigDataPtr,*this,in)))); template auto plus(const OpClass& in, const std::shared_ptr& i) const -> decltype(i->ifor(1,in.loop(AddExpr,OpClass> (mOrigDataPtr,*this,in)))); template OperationRoot& operator=(const OpClass& in); template OperationRoot& operator+=(const OpClass& in); OperationRoot& operator=(const OperationRoot& in); ParallelOperationRoot 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; }; template class ParallelOperationRoot : 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; private: T* mDataPtr; T* mOrigDataPtr; IndexType mIndex; public: ParallelOperationRoot(MutableMultiArrayBase& ma, const std::shared_ptr&... indices); ParallelOperationRoot(T* data, const IndexType& ind); template auto assign(const OpClass& in) -> decltype(mIndex.pifor(1,in.loop(AssignmentExpr2,OpClass> (mOrigDataPtr,*this,in)))); template auto plus(const OpClass& in) -> decltype(mIndex.pifor(1,in.loop(AddExpr,OpClass> (mOrigDataPtr,*this,in)))); template ParallelOperationRoot& operator=(const OpClass& in); template ParallelOperationRoot& operator+=(const OpClass& in); ParallelOperationRoot& operator=(const ParallelOperationRoot& in); template inline T& get(ET pos) const; template inline ParallelOperationRoot& 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 class OperationValue : public OperationTemplate > { public: typedef T value_type; typedef OperationBase > OT; typedef ContainerRange CRange; typedef ContainerIndex IndexType; static constexpr size_t SIZE = 0; static constexpr bool CONT = true; OperationValue(const T& val); template inline const T& get(ET pos) const; template inline OperationValue& set(ET pos); None 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 auto 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