From ba8655896b31fa57d6ff84f7a3bb5cbe193f8f94 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Wed, 24 Jul 2019 18:49:53 +0200 Subject: [PATCH] generalize element return values in operation classes --- src/include/arith.h | 81 +++++++++++++++++++++++++- src/include/multi_array_operation.cc.h | 28 ++++----- src/include/multi_array_operation.h | 20 +++---- 3 files changed, 100 insertions(+), 29 deletions(-) diff --git a/src/include/arith.h b/src/include/arith.h index 713823f..fc8a712 100644 --- a/src/include/arith.h +++ b/src/include/arith.h @@ -75,7 +75,86 @@ namespace MultiArrayTools return a; } }; + + template + using plusv = decltype(std::declval()+std::declval()); + + template + using minusv = decltype(std::declval()-std::declval()); + + template + using multipliesv = decltype(std::declval()*std::declval()); + + template + using dividesv = decltype(std::declval()/std::declval()); + + template + struct plusx : public StaticFunctionBase, plusx, T, U> + { + static constexpr bool FISSTATIC = true; + using StaticFunctionBase, plusx, T, U>::apply; + + static inline plusv apply(T a1, U a2) + { + return a1 + a2; + } + + static inline T& selfApply(T& a1, const T& a2) + { + return a1 += a2; + } + }; + + template + struct minusx : public StaticFunctionBase, minusx, T, U> + { + static constexpr bool FISSTATIC = true; + using StaticFunctionBase, minusx, T, U>::apply; + + static inline plusv apply(T a1, U a2) + { + return a1 - a2; + } + }; + + template + struct multipliesx : public StaticFunctionBase, multipliesx, T, U> + { + static constexpr bool FISSTATIC = true; + using StaticFunctionBase, multipliesx, T, U>::apply; + + static inline multipliesv apply(T a1, U a2) + { + return a1 * a2; + } + }; + + template + struct dividesx : public StaticFunctionBase, dividesx, T, U> + { + static constexpr bool FISSTATIC = true; + using StaticFunctionBase, dividesx, T, U>::apply; + + static inline dividesv apply(T a1, U a2) + { + return a1 / a2; + } + + }; + + template + using plus = plusx; + + template + using minus = minusx; + template + using multiplies = multipliesx; + + template + using divides = dividesx; + + /* template struct plus : public StaticFunctionBase, T, T> { @@ -128,7 +207,7 @@ namespace MultiArrayTools return a1 / a2; } }; - + */ // OPERATIONS (STATIC) template class function diff --git a/src/include/multi_array_operation.cc.h b/src/include/multi_array_operation.cc.h index 15533a3..b9cf164 100644 --- a/src/include/multi_array_operation.cc.h +++ b/src/include/multi_array_operation.cc.h @@ -18,35 +18,31 @@ namespace MultiArrayTools template - template - auto OperationBase::operator+(const OperationBase& in) const - -> Operation,OperationClass,Second> + template + auto OperationBase::operator+(const OperationBase& in) const { - return Operation,OperationClass,Second>(THIS(), in.THIS()); + return Operation,plusx,OperationClass,Second>(THIS(), in.THIS()); } template - template - auto OperationBase::operator-(const OperationBase& in) const - -> Operation,OperationClass,Second> + template + auto OperationBase::operator-(const OperationBase& in) const { - return Operation,OperationClass,Second>(THIS(), in.THIS()); + return Operation,minusx,OperationClass,Second>(THIS(), in.THIS()); } template - template - auto OperationBase::operator*(const OperationBase& in) const - -> Operation,OperationClass,Second> + template + auto OperationBase::operator*(const OperationBase& in) const { - return Operation,OperationClass,Second>(THIS(), in.THIS()); + return Operation,multipliesx,OperationClass,Second>(THIS(), in.THIS()); } template - template - auto OperationBase::operator/(const OperationBase& in) const - -> Operation,OperationClass,Second> + template + auto OperationBase::operator/(const OperationBase& in) const { - return Operation,OperationClass,Second>(THIS(), in.THIS()); + return Operation,dividesx,OperationClass,Second>(THIS(), in.THIS()); } template diff --git a/src/include/multi_array_operation.h b/src/include/multi_array_operation.h index 55d1bbc..f688756 100644 --- a/src/include/multi_array_operation.h +++ b/src/include/multi_array_operation.h @@ -33,21 +33,17 @@ namespace MultiArrayTools 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; - template - auto operator-(const OperationBase& in) const - -> Operation,OperationClass,Second>; + template + auto operator-(const OperationBase& in) const; - template - auto operator*(const OperationBase& in) const - -> Operation,OperationClass,Second>; + template + auto operator*(const OperationBase& in) const; - template - auto operator/(const OperationBase& in) const - -> Operation,OperationClass,Second>; + template + auto operator/(const OperationBase& in) const; template auto c(const std::shared_ptr& ind) const