diff --git a/src/include/arith.h b/src/include/arith.h index 54ed0a1..a2dd583 100644 --- a/src/include/arith.h +++ b/src/include/arith.h @@ -2,9 +2,63 @@ #ifndef __arith_h__ #define __arith_h__ -namespace MultiArrayHelper +namespace MultiArrayTools { + + template + struct ArgPack + { + template + static inline auto mk(const Tuple& tp, As... as) + -> decltype(ArgPack::mk(tp, std::get(tp), as...)) + { + return ArgPack::mk(tp, std::get(tp), as...); + } + }; + + template <> + struct ArgPack<0> + { + template + static inline auto mk(const Tuple& tp, As... as) + -> decltype(F::apply(std::get<0>(tp), as...)) + { + return F::apply(std::get<0>(tp), as...); + } + }; + + template + struct StaticFunctionBase + { + static constexpr bool FISSTATIC = true; + typedef T value_type; + + template + static auto mk(const Ops&... ops) + -> Operation + { + return Operation(ops...); + } + + static inline T apply(const std::tuple& arg) + { + return ArgPack::template mk >(arg); + } + }; + // OPERATIONS (STATIC) + template + struct identity : public StaticFunctionBase, T> + { + //static constexpr bool FISSTATIC = true; + using StaticFunctionBase, T>::apply; + + static inline T apply(T a) + { + return a; + } + }; + template struct plus { diff --git a/src/include/mbase_def.h b/src/include/mbase_def.h index c0abd6b..ae6abc4 100644 --- a/src/include/mbase_def.h +++ b/src/include/mbase_def.h @@ -40,6 +40,10 @@ namespace MultiArrayTools template class OperationRoot; + // multi_array_operation.h + template + class OperationValue; + // multi_array_operation.h template class ConstOperationRoot; diff --git a/src/include/multi_array.h b/src/include/multi_array.h index b366cd6..a6aefcf 100644 --- a/src/include/multi_array.h +++ b/src/include/multi_array.h @@ -39,6 +39,7 @@ namespace MultiArrayTools MultiArray(const std::shared_ptr&... ranges, std::vector&& vec); MultiArray(const typename CRange::SpaceType& space); MultiArray(const typename CRange::SpaceType& space, std::vector&& vec); + MultiArray(MultiArray& ama, SIZET... sizes); // Only if ALL ranges have default extensions: //MultiArray(const std::vector& vec); @@ -86,7 +87,10 @@ namespace MultiArrayTools }; template - using Scalar = MultiArray; + using Scalar = MultiArray; + + template + Scalar scalar(const T& in); template struct ArrayCatter > @@ -119,6 +123,12 @@ namespace MultiArrayTools namespace MultiArrayTools { + template + Scalar scalar(const T& in) + { + NullRF nrf; + return Scalar( std::dynamic_pointer_cast( nrf.create() ), std::vector( { in } ) ); + } /******************* * MultiArray * @@ -175,6 +185,14 @@ namespace MultiArrayTools } } + template + MultiArray::MultiArray(MultiArray& ama, SIZET... sizes) : + MutableMultiArrayBase( ama.range()->template scast(sizes...)->space() ), + mCont( std::move( ama.mCont ) ) + { + MAB::mInit = true; + } + /* template template diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index c6497f5..8f7c57f 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -33,6 +33,11 @@ namespace MultiArrayTools OperationClass& THIS() { return static_cast(*this); } const OperationClass& THIS() const { return static_cast(*this); } + //inline auto operator+(const T& in) const + // -> Operation,OperationClass,OperationValue >; + + // !!! + template auto operator+(const Second& in) const -> Operation,OperationClass,Second>; @@ -196,6 +201,30 @@ namespace MultiArrayTools IndexType mIndex; }; + template + class OperationValue : public OperationTemplate > + { + typedef T value_type; + typedef OperationBase > OT; + typedef ContainerRange CRange; + typedef ContainerIndex IndexType; + + static constexpr size_t SIZE = 1; + + OperationValue(const T& val); + + template + inline T get(ET pos) const; + + 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() { @@ -323,6 +352,15 @@ namespace MultiArrayTools /*************************** * OperationTemplate * ***************************/ + + /* + template + auto OperationBase::operator+(const T& in) const + -> Operation,OperationClass,OperationValue > + { + return Operation,OperationClass,OperationValue >(THIS(), in); + } + */ template template @@ -524,6 +562,31 @@ namespace MultiArrayTools { return mDataPtr + mIndex.pos(); } + + + template + OperationValue::OperationValue(const T& val) : mVal(val) {} + + template + template + inline T OperationValue::get(ET pos) const + { + return mVal; + } + + template + MExt OperationValue::rootSteps(std::intptr_t iPtrNum) const + { + return MExt(0); + } + + template + template + Expr OperationValue::loop(Expr exp) const + { + return exp; + } + /******************* * Operation * diff --git a/src/include/ranges/range_types/header.h b/src/include/ranges/range_types/header.h index 9c1f66e..0d308f6 100644 --- a/src/include/ranges/range_types/header.h +++ b/src/include/ranges/range_types/header.h @@ -14,6 +14,7 @@ //#ifndef __ranges_header__ //#define __ranges_header__ +#include "null_range.h" #include "spin_range.h" #include "space_range.h" #include "classic_range.h" diff --git a/src/include/ranges/range_types/null_range.h b/src/include/ranges/range_types/null_range.h new file mode 100644 index 0000000..836e238 --- /dev/null +++ b/src/include/ranges/range_types/null_range.h @@ -0,0 +1,76 @@ + + +#ifdef include_range_type +include_range_type(NUL,-2) +#else + +#ifdef __ranges_header__ +// assert, that this is only used within range_types/header.h + +//#ifndef __spin_range_h__ +//#define __spin_range_h__ + +namespace MultiArrayTools +{ + typedef SingleIndex NullIndex; + + template <> + class SingleRangeFactory : public RangeFactoryBase + { + public: + + typedef SingleRange oType; + + SingleRangeFactory(); + std::shared_ptr create(); + + }; + + template <> + class SingleRange : public RangeInterface + { + public: + typedef RangeBase RB; + typedef typename RangeInterface >::IndexType IndexType; + typedef SingleRange RangeType; + typedef size_t MetaType; + + virtual size_t size() const override; + virtual size_t dim() const override; + + size_t get(size_t pos) const; + size_t getMeta(size_t metapos) const; + + virtual IndexType begin() const override; + virtual IndexType end() const override; + //virtual std::shared_ptr index() const override; + + friend SingleRangeFactory; + + static constexpr bool defaultable = true; + + static constexpr size_t ISSTATIC = 1; + static constexpr size_t SIZE = 1; + static constexpr bool HASMETACONT = false; + + static SingleRangeFactory factory() + { return SingleRangeFactory(); } + + protected: + + SingleRange() = default; + SingleRange(const SingleRange& in) = delete; + + //SingleRange(size_t spinNum); + }; + + typedef SingleRange NullRange; + typedef SingleRangeFactory NullRF; +} + + +//#endif // #ifndef __spin_range_h__ + +#endif // #ifdef __ranges_header__ + +#endif // #ifdef include_range_type diff --git a/src/lib/ranges/range_types/null_range.cc b/src/lib/ranges/range_types/null_range.cc new file mode 100644 index 0000000..9c7dc9c --- /dev/null +++ b/src/lib/ranges/range_types/null_range.cc @@ -0,0 +1,76 @@ + +#include "ranges/rheader.h" + +namespace MultiArrayTools +{ + /******************** + * SingleRange * + ********************/ + + SingleRangeFactory::SingleRangeFactory() + { + // Quasi Singleton + if(not mProd){ + mProd = std::shared_ptr( new SingleRange() ); + setSelf(); + } + } + + std::shared_ptr SingleRangeFactory::create() + { + return mProd; + } + + /******************** + * SingleRange * + ********************/ + + size_t SingleRange::get(size_t pos) const + { + return 0; + } + + size_t SingleRange::getMeta(size_t metapos) const + { + return 0; + } + + size_t SingleRange::size() const + { + return 1; + } + + size_t SingleRange::dim() const + { + return 1; + } + + typename SingleRange::IndexType SingleRange::begin() const + { + SingleIndex i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + i = 0; + return i; + } + + typename SingleRange::IndexType SingleRange::end() const + { + SingleIndex i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + i = size(); + return i; + } + + // put this in the interface class !!! + /* + std::shared_ptr SingleRange::index() const + { + typedef IndexWrapper IW; + return std::make_shared + ( std::make_shared + ( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ) ); + } + */ +} +