Loading TemplateTest/main.cpp +10 −4 Original line number Original line Diff line number Diff line Loading @@ -450,10 +450,13 @@ public: MAKE_ATTRIBUTE_TRAIT(ns::TraitedClass, d1, d2, ts); MAKE_ATTRIBUTE_TRAIT(ns::TraitedClass, d1, d2, ts); template <typename T> template <typename T> auto sum(const T& val){ auto _max(const T& val){ DBGVAR(val); return TraitsAggregationProcesor<Max>::evaluate(val); return TraitsAggregationProcesor<BinaryPlus>::evaluate(val); } } void testTraitsAlgorithms() { void testTraitsAlgorithms() { Loading @@ -463,7 +466,10 @@ void testTraitsAlgorithms() { DBGVAR(std::make_pair(e1, DefaultArithmeticTraits<ExportTest>::getTraits())); DBGVAR(std::make_pair(e1, DefaultArithmeticTraits<ExportTest>::getTraits())); DBGVAR(std::make_pair(e1.attrTempData, DefaultArithmeticTraits<tempData>::getTraits())); DBGVAR(std::make_pair(e1.attrTempData, DefaultArithmeticTraits<tempData>::getTraits())); DBGVAR(sum(e1)); DBGVAR(sum(e1), _max(e1)); e1.attrInt = 100; e1.attrTempData.velocity[0]=1e5; DBGVAR(max(e1), _max(e1)); DBGVAR(2.45*e1,e1 + e2, e1, e2,HasDefaultArithmeticTraits<int>::value, max(e2), min(e2), max(vec)); DBGVAR(2.45*e1,e1 + e2, e1, e2,HasDefaultArithmeticTraits<int>::value, max(e2), min(e2), max(vec)); Loading src/GTMesh/Traits/TraitsAlgorithm/TraitsAlgorithm.h +17 −128 Original line number Original line Diff line number Diff line Loading @@ -916,145 +916,35 @@ struct TraitsAggregationProcesor { }; }; template <typename T1, typename T2> struct Max{ template <typename T> static auto evaluate(const T1& _1, const T2& _2){ typename std::enable_if< return std::max<std::common_type_t<T1, T2>>(_1, _2); !std::is_class<T>::value, T >::type max(const T& arg) noexcept { return arg; } template <typename T> typename std::enable_if< IsIndexable<T>::value, double >::type max(const T& array) noexcept { double res = std::numeric_limits<double>::min(); for (decltype (array.size()) index = 0; index < array.size(); index++){ double m = max(array[index]); if (res < m){ res = m; } } return res; } } }; template <typename T> template <typename T> typename std::enable_if< auto max(const T& val){ IsTNLIndexable<T>::value, return TraitsAggregationProcesor<Max>::evaluate(val); double >::type max(const T& array) noexcept { double res = std::numeric_limits<double>::min(); for (decltype (array.getSize()) index = 0; index < array.getSize(); index++){ double m = max(array[index]); if (res < m){ res = m; } } return res; } } namespace ImplMax { template<typename TraitT, unsigned int Index = DefaultArithmeticTraits<TraitT>::size() - 1> typename std::enable_if< Index == 0 , double //typename TraitCommonType<TraitT>::type >::type max(const TraitT& op1){ using ::max; return max(DefaultArithmeticTraits<TraitT>::getTraits().template getValue<Index>(op1)); } template<typename TraitT, unsigned int Index = DefaultArithmeticTraits<TraitT>::size() - 1> typename std::enable_if< (Index > 0) && (Index <= DefaultArithmeticTraits<TraitT>::size() - 1) , double >::type max(const TraitT& op1){ using ::max; using common_type = typename std::common_type< decltype (max(DefaultArithmeticTraits<TraitT>::getTraits().template getValue<Index - 1>(op1))), decltype (max(DefaultArithmeticTraits<TraitT>::getTraits().template getValue<Index>(op1))) >::type; return std::max<common_type>( (max(DefaultArithmeticTraits<TraitT>::getTraits().template getValue<Index - 1>(op1))), (max(DefaultArithmeticTraits<TraitT>::getTraits().template getValue<Index>(op1))) ); } template <typename T1, typename T2> } struct Min{ static auto evaluate(const T1& _1, const T2& _2){ return std::min<std::common_type_t<T1, T2>>(_1, _2); template <typename TraitT> typename std::enable_if< HasDefaultArithmeticTraits<TraitT>::value, double //typename TraitCommonType<TraitT>::type >::type max(const TraitT& op1) noexcept { return ImplMax::max(op1); } template <typename T> typename std::enable_if< !std::is_class<T>::value, T >::type min(const T& arg) noexcept { return arg; } } }; template <typename T> template <typename T> typename std::enable_if< auto min(const T& val){ IsIndexable<T>::value, return TraitsAggregationProcesor<Max>::evaluate(val); double >::type min(const T& array) noexcept { double res = std::numeric_limits<double>::max(); for (decltype (array.size()) index = 0; index < array.size(); index++){ double m = min(array[index]); if (res > m){ res = m; } } } return res; } template <typename TraitT> template <typename T> typename std::enable_if< auto sum(const T& val){ HasDefaultArithmeticTraits<TraitT>::value, return TraitsAggregationProcesor<BinaryPlus>::evaluate(val); double//typename TraitCommonType<TraitT>::type >::type min(const TraitT& op1) noexcept { double res = std::numeric_limits<double>::max(); //typename TraitCommonType<TraitT>::type res = std::numeric_limits<typename TraitCommonType<TraitT>::type>::max(); DefaultArithmeticTraits<TraitT>::getTraits().apply( [&res, &op1](unsigned int, const auto& ref, const std::string&) noexcept { auto m = min(ref.getValue(op1)); if (res > m) { res = m; } } ); return res; } } Loading @@ -1062,7 +952,6 @@ min(const TraitT& op1) noexcept { #endif // TRAITSALGORITHM_H #endif // TRAITSALGORITHM_H Loading src/UnitTests/Traits/ArithmeticTraitsTest.cpp +1 −0 Original line number Original line Diff line number Diff line Loading @@ -41,6 +41,7 @@ TEST( ArithmeticTraitsTest, basicTest ) EXPECT_EQ(abs(-ns), NumStruct(21,15)); EXPECT_EQ(abs(-ns), NumStruct(21,15)); EXPECT_EQ(min(-ns), -21); EXPECT_EQ(min(-ns), -21); EXPECT_EQ(max(-ns), -15); EXPECT_EQ(max(-ns), -15); EXPECT_EQ(sum(ns), 15 + 21); EXPECT_EQ(max(abs(-ns)), 21); EXPECT_EQ(max(abs(-ns)), 21); Loading Loading
TemplateTest/main.cpp +10 −4 Original line number Original line Diff line number Diff line Loading @@ -450,10 +450,13 @@ public: MAKE_ATTRIBUTE_TRAIT(ns::TraitedClass, d1, d2, ts); MAKE_ATTRIBUTE_TRAIT(ns::TraitedClass, d1, d2, ts); template <typename T> template <typename T> auto sum(const T& val){ auto _max(const T& val){ DBGVAR(val); return TraitsAggregationProcesor<Max>::evaluate(val); return TraitsAggregationProcesor<BinaryPlus>::evaluate(val); } } void testTraitsAlgorithms() { void testTraitsAlgorithms() { Loading @@ -463,7 +466,10 @@ void testTraitsAlgorithms() { DBGVAR(std::make_pair(e1, DefaultArithmeticTraits<ExportTest>::getTraits())); DBGVAR(std::make_pair(e1, DefaultArithmeticTraits<ExportTest>::getTraits())); DBGVAR(std::make_pair(e1.attrTempData, DefaultArithmeticTraits<tempData>::getTraits())); DBGVAR(std::make_pair(e1.attrTempData, DefaultArithmeticTraits<tempData>::getTraits())); DBGVAR(sum(e1)); DBGVAR(sum(e1), _max(e1)); e1.attrInt = 100; e1.attrTempData.velocity[0]=1e5; DBGVAR(max(e1), _max(e1)); DBGVAR(2.45*e1,e1 + e2, e1, e2,HasDefaultArithmeticTraits<int>::value, max(e2), min(e2), max(vec)); DBGVAR(2.45*e1,e1 + e2, e1, e2,HasDefaultArithmeticTraits<int>::value, max(e2), min(e2), max(vec)); Loading
src/GTMesh/Traits/TraitsAlgorithm/TraitsAlgorithm.h +17 −128 Original line number Original line Diff line number Diff line Loading @@ -916,145 +916,35 @@ struct TraitsAggregationProcesor { }; }; template <typename T1, typename T2> struct Max{ template <typename T> static auto evaluate(const T1& _1, const T2& _2){ typename std::enable_if< return std::max<std::common_type_t<T1, T2>>(_1, _2); !std::is_class<T>::value, T >::type max(const T& arg) noexcept { return arg; } template <typename T> typename std::enable_if< IsIndexable<T>::value, double >::type max(const T& array) noexcept { double res = std::numeric_limits<double>::min(); for (decltype (array.size()) index = 0; index < array.size(); index++){ double m = max(array[index]); if (res < m){ res = m; } } return res; } } }; template <typename T> template <typename T> typename std::enable_if< auto max(const T& val){ IsTNLIndexable<T>::value, return TraitsAggregationProcesor<Max>::evaluate(val); double >::type max(const T& array) noexcept { double res = std::numeric_limits<double>::min(); for (decltype (array.getSize()) index = 0; index < array.getSize(); index++){ double m = max(array[index]); if (res < m){ res = m; } } return res; } } namespace ImplMax { template<typename TraitT, unsigned int Index = DefaultArithmeticTraits<TraitT>::size() - 1> typename std::enable_if< Index == 0 , double //typename TraitCommonType<TraitT>::type >::type max(const TraitT& op1){ using ::max; return max(DefaultArithmeticTraits<TraitT>::getTraits().template getValue<Index>(op1)); } template<typename TraitT, unsigned int Index = DefaultArithmeticTraits<TraitT>::size() - 1> typename std::enable_if< (Index > 0) && (Index <= DefaultArithmeticTraits<TraitT>::size() - 1) , double >::type max(const TraitT& op1){ using ::max; using common_type = typename std::common_type< decltype (max(DefaultArithmeticTraits<TraitT>::getTraits().template getValue<Index - 1>(op1))), decltype (max(DefaultArithmeticTraits<TraitT>::getTraits().template getValue<Index>(op1))) >::type; return std::max<common_type>( (max(DefaultArithmeticTraits<TraitT>::getTraits().template getValue<Index - 1>(op1))), (max(DefaultArithmeticTraits<TraitT>::getTraits().template getValue<Index>(op1))) ); } template <typename T1, typename T2> } struct Min{ static auto evaluate(const T1& _1, const T2& _2){ return std::min<std::common_type_t<T1, T2>>(_1, _2); template <typename TraitT> typename std::enable_if< HasDefaultArithmeticTraits<TraitT>::value, double //typename TraitCommonType<TraitT>::type >::type max(const TraitT& op1) noexcept { return ImplMax::max(op1); } template <typename T> typename std::enable_if< !std::is_class<T>::value, T >::type min(const T& arg) noexcept { return arg; } } }; template <typename T> template <typename T> typename std::enable_if< auto min(const T& val){ IsIndexable<T>::value, return TraitsAggregationProcesor<Max>::evaluate(val); double >::type min(const T& array) noexcept { double res = std::numeric_limits<double>::max(); for (decltype (array.size()) index = 0; index < array.size(); index++){ double m = min(array[index]); if (res > m){ res = m; } } } return res; } template <typename TraitT> template <typename T> typename std::enable_if< auto sum(const T& val){ HasDefaultArithmeticTraits<TraitT>::value, return TraitsAggregationProcesor<BinaryPlus>::evaluate(val); double//typename TraitCommonType<TraitT>::type >::type min(const TraitT& op1) noexcept { double res = std::numeric_limits<double>::max(); //typename TraitCommonType<TraitT>::type res = std::numeric_limits<typename TraitCommonType<TraitT>::type>::max(); DefaultArithmeticTraits<TraitT>::getTraits().apply( [&res, &op1](unsigned int, const auto& ref, const std::string&) noexcept { auto m = min(ref.getValue(op1)); if (res > m) { res = m; } } ); return res; } } Loading @@ -1062,7 +952,6 @@ min(const TraitT& op1) noexcept { #endif // TRAITSALGORITHM_H #endif // TRAITSALGORITHM_H Loading
src/UnitTests/Traits/ArithmeticTraitsTest.cpp +1 −0 Original line number Original line Diff line number Diff line Loading @@ -41,6 +41,7 @@ TEST( ArithmeticTraitsTest, basicTest ) EXPECT_EQ(abs(-ns), NumStruct(21,15)); EXPECT_EQ(abs(-ns), NumStruct(21,15)); EXPECT_EQ(min(-ns), -21); EXPECT_EQ(min(-ns), -21); EXPECT_EQ(max(-ns), -15); EXPECT_EQ(max(-ns), -15); EXPECT_EQ(sum(ns), 15 + 21); EXPECT_EQ(max(abs(-ns)), 21); EXPECT_EQ(max(abs(-ns)), 21); Loading