From b827221f1c3cf5c1c2d35b8717aa3bd36dd4770c Mon Sep 17 00:00:00 2001 From: yumetodo Date: Mon, 26 Sep 2016 15:52:39 +0900 Subject: [PATCH 1/2] add abs_diff and deprecated safe_dist #91 --- dxlibex/algorithm.hpp | 3 +- dxlibex/algorithm/abs_diff.hpp | 65 +++++++++++++++++++++++++++++++ dxlibex/algorithm/safe_dist.hpp | 4 +- dxlibex/basic_types/point2d.hpp | 4 +- dxlibex/basic_types/point3d.hpp | 4 +- dxlibex/type_traits.hpp | 2 + dxlibex/type_traits/result_of.hpp | 21 ++++++++++ 7 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 dxlibex/algorithm/abs_diff.hpp create mode 100644 dxlibex/type_traits/result_of.hpp diff --git a/dxlibex/algorithm.hpp b/dxlibex/algorithm.hpp index 0ab2d13..b0a1d5f 100644 --- a/dxlibex/algorithm.hpp +++ b/dxlibex/algorithm.hpp @@ -8,6 +8,7 @@ #ifndef DXLE_INC_ALGORITHM_HPP_ #define DXLE_INC_ALGORITHM_HPP_ #include "dxlibex/algorithm/cast_if.hpp" -#include "dxlibex/algorithm/safe_dist.hpp" +#include "dxlibex/algorithm/abs_diff.hpp" +#include "dxlibex/algorithm/safe_dist.hpp"//deprecated #endif //DXLE_INC_ALGORITHM_HPP_ diff --git a/dxlibex/algorithm/abs_diff.hpp b/dxlibex/algorithm/abs_diff.hpp new file mode 100644 index 0000000..63e0262 --- /dev/null +++ b/dxlibex/algorithm/abs_diff.hpp @@ -0,0 +1,65 @@ +/*============================================================================= + Copyright (C) 2015-2016 DxLibEx project + https://github.com/Nagarei/DxLibEx/ + + Distributed under the Boost Software License, Version 1.0. + (See http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef DXLE_INC_ALGORITHM_ABS_DIFF_HPP_ +#define DXLE_INC_ALGORITHM_ABS_DIFF_HPP_ +#include "dxlibex/config/defines.h" +#include "dxlibex/type_traits/enable_if.hpp" +#include "dxlibex/type_traits/ignore.hpp" +#include "dxlibex/type_traits/result_of.hpp" +namespace dxle { + template () - std::declval()), decltype(std::declval() - std::declval())>::value + && std::is_same() < std::declval())>::value, + nullptr_t + > = nullptr> + static inline DXLE_CONSTEXPR auto abs_diff(const T1& a, const T2& b) + DXLE_NOEXCEPT_IF( + DXLE_NOEXCEPT_EXPR(std::declval() - std::declval()) + && DXLE_NOEXCEPT_EXPR(std::declval() - std::declval()) + && DXLE_NOEXCEPT_EXPR(std::declval() < std::declval()) + ) + -> decltype(std::declval() - std::declval()) + { + return (a < b) ? b - a : a - b; + } + template () - std::declval()), decltype(std::declval() - std::declval())>::value + && ignore_type()(std::declval(), std::declval()))>::value + && std::is_same>::value, + nullptr_t + > = nullptr> + static inline DXLE_CONSTEXPR auto abs_diff(const T1& a, const T2& b, const Compare& comp) + DXLE_NOEXCEPT_IF( + DXLE_NOEXCEPT_EXPR(std::declval() - std::declval()) + && DXLE_NOEXCEPT_EXPR(std::declval() - std::declval()) + && DXLE_NOEXCEPT_EXPR(std::declval()(std::declval(), std::declval())) + ) + -> decltype(std::declval() - std::declval()) + { + return comp(a, b) ? b - a : a - b; + } + template () - std::declval()), decltype(std::declval() - std::declval())>::value + && ignore_type()(std::declval(), std::declval()))>::value + && ignore_type()(std::declval(), std::declval()))>::value + && std::is_same, result_of_t>::value + && ignore_type()(std::declval(), std::declval()))>::value + && std::is_same>::value, + nullptr_t + > = nullptr> + static inline DXLE_CONSTEXPR result_of abs_diff(const T1& a, const T2& b, const Compare& comp, const Difference& diff) + DXLE_NOEXCEPT_IF( + DXLE_NOEXCEPT_EXPR(std::declval()(std::declval(), std::declval())) + && DXLE_NOEXCEPT_EXPR(std::declval()(std::declval(), std::declval())) + && DXLE_NOEXCEPT_EXPR(std::declval()(std::declval(), std::declval())) + ) + { + return comp(a, b) ? diff(b, a) : diff(a, b); + } +} +#endif //DXLE_INC_ALGORITHM_ABS_DIFF_HPP_ diff --git a/dxlibex/algorithm/safe_dist.hpp b/dxlibex/algorithm/safe_dist.hpp index e5fd650..fc3bbfa 100644 --- a/dxlibex/algorithm/safe_dist.hpp +++ b/dxlibex/algorithm/safe_dist.hpp @@ -1,4 +1,4 @@ -/*============================================================================= +/*============================================================================= Copyright (C) 2015-2016 DxLibEx project https://github.com/Nagarei/DxLibEx/ @@ -11,7 +11,7 @@ #include "dxlibex/type_traits/enable_if.hpp" namespace dxle { template - DXLE_CONSTEXPR auto safe_dist(const T1& n1, const T2& n2) DXLE_NOEXCEPT_IF_EXPR((n1 - n2)) -> decltype(n1 - n2) + DXLE_DEPRECATED_MESSAGE("use abs_diff() instead.") static inline DXLE_CONSTEXPR auto safe_dist(const T1& n1, const T2& n2) DXLE_NOEXCEPT_IF_EXPR((n1 - n2)) -> decltype(n1 - n2) { return (n1 < n2) ? n2 - n1 : n1 - n2; } diff --git a/dxlibex/basic_types/point2d.hpp b/dxlibex/basic_types/point2d.hpp index 0c5d38d..e970fa2 100644 --- a/dxlibex/basic_types/point2d.hpp +++ b/dxlibex/basic_types/point2d.hpp @@ -658,9 +658,9 @@ namespace dxle { */ template distance_result_type_t distance(const point_c& p1, const point_c& p2) - DXLE_NOEXCEPT_IF_EXPR(hypot(safe_dist(std::declval(), std::declval()), safe_dist(std::declval(), std::declval()))) + DXLE_NOEXCEPT_IF_EXPR(hypot(abs_diff(std::declval(), std::declval()), abs_diff(std::declval(), std::declval()))) { - return hypot(safe_dist(p1.x, p2.x), safe_dist(p1.y, p2.y)); + return hypot(abs_diff(p1.x, p2.x), abs_diff(p1.y, p2.y)); } typedef point_c pointi; typedef point_c pointu8i; diff --git a/dxlibex/basic_types/point3d.hpp b/dxlibex/basic_types/point3d.hpp index 9ee5721..9242780 100644 --- a/dxlibex/basic_types/point3d.hpp +++ b/dxlibex/basic_types/point3d.hpp @@ -642,9 +642,9 @@ namespace dxle { */ template distance_result_type_t distance(const point3d_c& p1, const point3d_c& p2) - DXLE_NOEXCEPT_IF_EXPR(hypot(safe_dist(std::declval(), std::declval()), safe_dist(std::declval(), std::declval()))) + DXLE_NOEXCEPT_IF_EXPR(hypot(abs_diff(std::declval(), std::declval()), abs_diff(std::declval(), std::declval()))) { - return hypot(safe_dist(p1.x, p2.x), (safe_dist(p1.y, p2.y), safe_dist(p1.z, p2.z))); + return hypot(abs_diff(p1.x, p2.x), (abs_diff(p1.y, p2.y), abs_diff(p1.z, p2.z))); } typedef point3d_c point3di; typedef point3d_c point3du8i; diff --git a/dxlibex/type_traits.hpp b/dxlibex/type_traits.hpp index ef250e7..0f8dbef 100644 --- a/dxlibex/type_traits.hpp +++ b/dxlibex/type_traits.hpp @@ -17,5 +17,7 @@ #include "dxlibex/type_traits/is_nothrow.hpp" #include "dxlibex/type_traits/is_representable.hpp" #include "dxlibex/type_traits/is_well_format.hpp" +#include "dxlibex/type_traits/result_of.hpp" + #endif//#ifndef DXLE_INC_TYPE_TRAITS_HPP_ diff --git a/dxlibex/type_traits/result_of.hpp b/dxlibex/type_traits/result_of.hpp new file mode 100644 index 0000000..c056e46 --- /dev/null +++ b/dxlibex/type_traits/result_of.hpp @@ -0,0 +1,21 @@ +/*============================================================================= + Copyright (C) 2015-2016 DxLibEx project + https://github.com/Nagarei/DxLibEx/ + + Distributed under the Boost Software License, Version 1.0. + (See http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef DXLE_INC_TYPE_TRAITS_RESULT_OF_HPP_ +#define DXLE_INC_TYPE_TRAITS_RESULT_OF_HPP_ +#include +#include "enable_if.hpp" +namespace dxle { +//!inline +namespace type_traits { + using std::result_of; + template + using result_of_t = typename result_of::type; +}//namespace +using namespace type_traits; +} +#endif //DXLE_INC_TYPE_TRAITS_RESULT_OF_HPP_ From e01e805f8e08e4d4cda668302bba50cbbb6672be Mon Sep 17 00:00:00 2001 From: yumetodo Date: Sun, 1 Jan 2017 10:47:12 +0900 Subject: [PATCH 2/2] A HAPPY NEW YEAR! s/2015-2016/2015-2017/ --- dxlibex/algorithm/abs_diff.hpp | 2 +- dxlibex/type_traits/result_of.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dxlibex/algorithm/abs_diff.hpp b/dxlibex/algorithm/abs_diff.hpp index 63e0262..2316834 100644 --- a/dxlibex/algorithm/abs_diff.hpp +++ b/dxlibex/algorithm/abs_diff.hpp @@ -1,5 +1,5 @@ /*============================================================================= - Copyright (C) 2015-2016 DxLibEx project + Copyright (C) 2015-2017 DxLibEx project https://github.com/Nagarei/DxLibEx/ Distributed under the Boost Software License, Version 1.0. diff --git a/dxlibex/type_traits/result_of.hpp b/dxlibex/type_traits/result_of.hpp index c056e46..0485563 100644 --- a/dxlibex/type_traits/result_of.hpp +++ b/dxlibex/type_traits/result_of.hpp @@ -1,5 +1,5 @@ /*============================================================================= - Copyright (C) 2015-2016 DxLibEx project + Copyright (C) 2015-2017 DxLibEx project https://github.com/Nagarei/DxLibEx/ Distributed under the Boost Software License, Version 1.0.