From 341a70ef0665a4fa75c1f747b932c87fb72306cd Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Fri, 20 Jul 2018 19:13:42 +0200 Subject: [PATCH] various fixes --- src/include/arith.h | 4 +- src/include/functional_multi_array.h | 46 +++++++++++++++------ src/include/multi_array.h | 14 ++++++- src/include/ranges/range_types/null_range.h | 7 ++++ src/lib/ranges/range_types/null_range.cc | 25 +++++++++-- 5 files changed, 76 insertions(+), 20 deletions(-) diff --git a/src/include/arith.h b/src/include/arith.h index 336f71d..76cfb71 100644 --- a/src/include/arith.h +++ b/src/include/arith.h @@ -10,9 +10,9 @@ namespace MultiArrayTools { template static inline auto mk(const Tuple& tp, As... as) - -> decltype(ArgPack::mk(tp, std::get(tp), as...)) + -> decltype(ArgPack::template mk(tp)),As...>(tp, std::get(tp), as...)) { - return ArgPack::mk(tp, std::get(tp), as...); + return ArgPack::template mk(tp)),As...>(tp, std::get(tp), as...); } }; diff --git a/src/include/functional_multi_array.h b/src/include/functional_multi_array.h index 2f795b3..10f0a6a 100644 --- a/src/include/functional_multi_array.h +++ b/src/include/functional_multi_array.h @@ -8,9 +8,38 @@ namespace MultiArrayTools { + template + struct ToMAObject + { + template + static auto mk(const std::shared_ptr& i) + -> MultiArray + { + std::vector vv(i->range()->size()); + for(Index j = (*i); j.pos() != j.max(); ++j){ + vv[j.pos()] = j.meta(); + } + return MultiArray( i->range(), std::move( vv ) ); + } + }; + + template <> + struct ToMAObject + { + template + static auto mk(const std::shared_ptr& i) + -> ConstSlice + { + return ConstSlice( i->range(), i->metaPtr() ); + } + }; + template - auto indexToSlice(const std::shared_ptr& i) - -> ConstSlice; + auto mkMAObject(const std::shared_ptr& i) + -> decltype(ToMAObject::mk(i)) + { + return ToMAObject::mk(i); + } template @@ -49,7 +78,7 @@ namespace MultiArrayTools // EVALUTAION CLASS ??!!!! auto exec(std::shared_ptr&... inds) const - -> decltype( mkOperation( mFunc, ConstOperationRoot( indexToSlice( inds ), inds) ... ) ); + -> decltype( mkOperation( mFunc, ConstOperationRoot( mkMAObject( inds ), inds) ... ) ); virtual ConstOperationRoot operator()(std::shared_ptr&... inds) const override; @@ -65,13 +94,6 @@ namespace MultiArrayTools namespace MultiArrayTools { - - template - auto indexToSlice(const std::shared_ptr& i) - -> ConstSlice - { - return ConstSlice( i->range(), i->metaPtr() ); - } /**************************** @@ -166,9 +188,9 @@ namespace MultiArrayTools template auto FunctionalMultiArray:: exec(std::shared_ptr&... inds) const - -> decltype( mkOperation( mFunc, ConstOperationRoot( indexToSlice( inds ), inds) ... ) ) + -> decltype( mkOperation( mFunc, ConstOperationRoot( mkMAObject( inds ), inds) ... ) ) { - return mkOperation( mFunc, ConstOperationRoot( indexToSlice( inds ), inds ) ... ); + return mkOperation( mFunc, ConstOperationRoot( mkMAObject( inds ), inds ) ... ); } diff --git a/src/include/multi_array.h b/src/include/multi_array.h index 9273ba9..edae027 100644 --- a/src/include/multi_array.h +++ b/src/include/multi_array.h @@ -37,6 +37,7 @@ namespace MultiArrayTools DEFAULT_MEMBERS(MultiArray); MultiArray(const std::shared_ptr&... ranges); + MultiArray(const std::shared_ptr&... ranges, const T& val); MultiArray(const std::shared_ptr&... ranges, const std::vector& vec); MultiArray(const std::shared_ptr&... ranges, std::vector&& vec); MultiArray(const typename CRange::SpaceType& space); @@ -169,7 +170,15 @@ namespace MultiArrayTools { MAB::mInit = true; } - + + template + MultiArray::MultiArray(const std::shared_ptr&... ranges, const T& val) : + MutableMultiArrayBase(ranges...), + mCont(MAB::mRange->size(), val) + { + MAB::mInit = true; + } + template MultiArray::MultiArray(const std::shared_ptr&... ranges, const std::vector& vec) : MutableMultiArrayBase(ranges...), @@ -352,7 +361,8 @@ namespace MultiArrayTools template MultiArray::operator T() const { - static_assert( sizeof...(SRanges) == 0, "try to cast non-scalar type into scalar" ); + static_assert( sizeof...(SRanges) == 1, "try to cast non-scalar type into scalar" ); + // TODO: check that SIZE is statically = 1 !!! return mCont[0]; } diff --git a/src/include/ranges/range_types/null_range.h b/src/include/ranges/range_types/null_range.h index 836e238..0a5b075 100644 --- a/src/include/ranges/range_types/null_range.h +++ b/src/include/ranges/range_types/null_range.h @@ -14,6 +14,9 @@ namespace MultiArrayTools { typedef SingleIndex NullIndex; + std::shared_ptr > nullr(); + std::shared_ptr nulli(); + template <> class SingleRangeFactory : public RangeFactoryBase { @@ -23,7 +26,11 @@ namespace MultiArrayTools SingleRangeFactory(); std::shared_ptr create(); + + friend std::shared_ptr nullr(); + private: + static std::shared_ptr mRInstance; }; template <> diff --git a/src/lib/ranges/range_types/null_range.cc b/src/lib/ranges/range_types/null_range.cc index 9c7dc9c..056d5fd 100644 --- a/src/lib/ranges/range_types/null_range.cc +++ b/src/lib/ranges/range_types/null_range.cc @@ -6,13 +6,30 @@ namespace MultiArrayTools /******************** * SingleRange * ********************/ + + std::shared_ptr nullr() + { + return SingleRangeFactory::mRInstance; + } + + std::shared_ptr nulli() + { + return std::make_shared(nullr()); + } + std::shared_ptr> + SingleRangeFactory::mRInstance = nullptr; + SingleRangeFactory::SingleRangeFactory() { - // Quasi Singleton - if(not mProd){ - mProd = std::shared_ptr( new SingleRange() ); - setSelf(); + // Singleton + if( not mRInstance){ + if(not mProd){ + mProd = std::shared_ptr( new SingleRange() ); + setSelf(); + } + } else { + mProd = mRInstance; } }