From ca2e47b9867d2f40f64746755a46a290272dc160 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 29 Nov 2018 19:37:32 +0100 Subject: [PATCH] some utilities + corrections in 'call by lambda' operation --- src/include/helper_tools.h | 10 ++++++- src/include/multi_array_base.cc.h | 15 ++++++++++ src/include/multi_array_base.h | 39 ++++++++++++++++++++++++-- src/include/multi_array_operation.cc.h | 4 +-- src/include/multi_array_operation.h | 2 +- src/include/ranges/container_range.h | 9 ++++++ src/include/ranges/rpack_num.h | 13 +++++++++ src/include/slice.h | 5 +++- 8 files changed, 90 insertions(+), 7 deletions(-) diff --git a/src/include/helper_tools.h b/src/include/helper_tools.h index 39a5afb..c428adb 100644 --- a/src/include/helper_tools.h +++ b/src/include/helper_tools.h @@ -99,7 +99,15 @@ namespace MultiArrayTools typename Index::RangeType> fma(i->range()); return fma.exec(i); } - + + template + struct Func + { + static inline std::shared_ptr> mk(const std::function& ll) + { + return std::make_shared>(ll); + } + }; } diff --git a/src/include/multi_array_base.cc.h b/src/include/multi_array_base.cc.h index 9d9dbd9..d4423a4 100644 --- a/src/include/multi_array_base.cc.h +++ b/src/include/multi_array_base.cc.h @@ -33,6 +33,13 @@ namespace MultiArrayTools return (*this)[ii]; } + template + const T& MultiArrayBase::operator[](const std::tuple...>& is) const + { + IndexType ii(*mProtoI); + ii(is); + return (*this)[ii]; + } template size_t MultiArrayBase::size() const @@ -164,6 +171,14 @@ namespace MultiArrayTools return (*this)[ii]; } + template + T& MutableMultiArrayBase::operator[](const std::tuple...>& is) + { + IndexType ii(*MAB::mProtoI); + ii(is); + return (*this)[ii]; + } + template bool MutableMultiArrayBase::isConst() const diff --git a/src/include/multi_array_base.h b/src/include/multi_array_base.h index e771d6c..879c528 100644 --- a/src/include/multi_array_base.h +++ b/src/include/multi_array_base.h @@ -16,6 +16,39 @@ namespace MultiArrayTools { + template + using IPTR = std::shared_ptr; + + template + inline auto operator|(const IPTR& i1, const IPTR& i2) + -> decltype(std::make_tuple(i1->THIS(),i2->THIS())) + { + return std::make_tuple(i1->THIS(),i2->THIS()); + } + + template + inline auto operator|(const IPTR& i1, + const std::tuple...>& i2) + -> decltype(std::tuple_cat(std::make_tuple(i1),i2)) + { + return std::tuple_cat(std::make_tuple(i1),i2); + } + + template + inline auto operator|(const std::tuple...>& i1, + const IPTR& i2) + -> decltype(std::tuple_cat(i1,std::make_tuple(i2))) + { + return std::tuple_cat(i1,std::make_tuple(i2)); + } + + template + inline auto operator~(const IPTR& i) + -> decltype(std::make_tuple(i)) + { + return std::make_tuple(i); + } + // Explicitely specify subranges in template argument !!! template class MultiArrayBase @@ -41,7 +74,8 @@ namespace MultiArrayTools template const T& operator[](const ContainerIndex& i); - + const T& operator[](const std::tuple...>& is) const; + virtual const T& operator[](const IndexType& i) const = 0; virtual const T& at(const typename CRange::IndexType::MetaType& meta) const = 0; @@ -101,7 +135,8 @@ namespace MultiArrayTools template T& operator[](const ContainerIndex& i); - + T& operator[](const std::tuple...>& is); + virtual T& operator[](const IndexType& i) = 0; virtual T& at(const typename CRange::IndexType::MetaType& meta) = 0; diff --git a/src/include/multi_array_operation.cc.h b/src/include/multi_array_operation.cc.h index 109ba11..e3e012c 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/multi_array_operation.cc.h @@ -78,11 +78,11 @@ namespace MultiArrayTools template template - auto OperationBase::a(const function& ll, + auto OperationBase::a(const std::shared_ptr>& ll, const Args&... args) const -> Operation,OperationClass, Args...> { - return Operation,OperationClass, Args...>(THIS(), args...); + return Operation,OperationClass, Args...>(ll, THIS(), args...); } /***************************************** diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index ef4ecac..36dcbb9 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -62,7 +62,7 @@ namespace MultiArrayTools -> SliceContraction; template // Args = Operation Classes - auto a(const function& ll, const Args&... args) const + auto a(const std::shared_ptr>& ll, const Args&... args) const -> Operation,OperationClass, Args...>; private: diff --git a/src/include/ranges/container_range.h b/src/include/ranges/container_range.h index 090d873..6d8da63 100644 --- a/src/include/ranges/container_range.h +++ b/src/include/ranges/container_range.h @@ -82,6 +82,7 @@ namespace MultiArrayTools ContainerIndex& sync(); // recalculate 'IB::mPos' when externalControl == true ContainerIndex& operator()(const std::shared_ptr&... inds); // control via external indices + ContainerIndex& operator()(const std::tuple...>& inds); ContainerIndex& operator()(); // -> sync; just to shorten the code // ==== >>>>> STATIC POLYMORPHISM <<<<< ==== @@ -305,6 +306,14 @@ namespace MultiArrayTools return sync(); } + template + ContainerIndex& ContainerIndex::operator()(const std::tuple...>& inds) + { + RPackNum::swapIndices(mIPack, inds); + mExternControl = true; + return sync(); + } + template ContainerIndex& ContainerIndex::operator()() { diff --git a/src/include/ranges/rpack_num.h b/src/include/ranges/rpack_num.h index 470934d..fffd1ba 100644 --- a/src/include/ranges/rpack_num.h +++ b/src/include/ranges/rpack_num.h @@ -214,6 +214,13 @@ namespace MultiArrayHelper RPackNum::swapIndices(ipack, ninds...); } + template + static void swapIndices(Pack& ipack, const std::tuple...>& ninds) + { + std::get(ipack) = std::get(ninds); + RPackNum::swapIndices(ipack, ninds); + } + template static size_t blockSize(const std::tuple...>& pack) { @@ -455,6 +462,12 @@ namespace MultiArrayHelper std::get::value-1>(ipack) = nind; } + template + static void swapIndices(Pack& ipack, const std::tuple...>& ninds) + { + std::get<0>(ipack) = std::get<0>(ninds); + } + template static size_t blockSize(const std::tuple...>& pack) { diff --git a/src/include/slice.h b/src/include/slice.h index ff6eb70..e727a46 100644 --- a/src/include/slice.h +++ b/src/include/slice.h @@ -17,6 +17,7 @@ namespace MultiArrayTools typedef ContainerIndex IType; using MultiArrayBase::operator(); + using MultiArrayBase::operator[]; DEFAULT_MEMBERS(ConstSlice); @@ -60,7 +61,9 @@ namespace MultiArrayTools using MultiArrayBase::operator(); using MutableMultiArrayBase::operator(); - + using MultiArrayBase::operator[]; + using MutableMultiArrayBase::operator[]; + DEFAULT_MEMBERS(Slice); Slice(const std::shared_ptr&... ranges, T* data = nullptr);