Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add abs_diff and deprecated safe_dist #92

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dxlibex/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_
65 changes: 65 additions & 0 deletions dxlibex/algorithm/abs_diff.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*=============================================================================
Copyright (C) 2015-2017 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 <typename T1, typename T2, enable_if_t<
std::is_same<decltype(std::declval<T1>() - std::declval<T2>()), decltype(std::declval<T2>() - std::declval<T2>())>::value
&& std::is_same<bool, decltype(std::declval<T1>() < std::declval<T2>())>::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<T1>() - std::declval<T2>())
&& DXLE_NOEXCEPT_EXPR(std::declval<T2>() - std::declval<T1>())
&& DXLE_NOEXCEPT_EXPR(std::declval<T1>() < std::declval<T2>())
)
-> decltype(std::declval<T1>() - std::declval<T2>())
{
return (a < b) ? b - a : a - b;
}
template <typename T1, typename T2, typename Compare, enable_if_t<
std::is_same<decltype(std::declval<T1>() - std::declval<T2>()), decltype(std::declval<T2>() - std::declval<T2>())>::value
&& ignore_type<decltype(std::declval<Compare>()(std::declval<T1>(), std::declval<T2>()))>::value
&& std::is_same<bool, result_of_t<Compare(T1, T2)>>::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<T1>() - std::declval<T2>())
&& DXLE_NOEXCEPT_EXPR(std::declval<T2>() - std::declval<T1>())
&& DXLE_NOEXCEPT_EXPR(std::declval<Compare>()(std::declval<T1>(), std::declval<T2>()))
)
-> decltype(std::declval<T1>() - std::declval<T2>())
{
return comp(a, b) ? b - a : a - b;
}
template <typename T1, typename T2, typename Compare, typename Difference, enable_if_t<
std::is_same<decltype(std::declval<T1>() - std::declval<T2>()), decltype(std::declval<T2>() - std::declval<T2>())>::value
&& ignore_type<decltype(std::declval<Difference>()(std::declval<T1>(), std::declval<T2>()))>::value
&& ignore_type<decltype(std::declval<Difference>()(std::declval<T2>(), std::declval<T1>()))>::value
&& std::is_same<result_of_t<Difference(T1, T2)>, result_of_t<Difference(T2, T1)>>::value
&& ignore_type<decltype(std::declval<Compare>()(std::declval<T1>(), std::declval<T2>()))>::value
&& std::is_same<bool, result_of_t<Compare(T1, T2)>>::value,
nullptr_t
> = nullptr>
static inline DXLE_CONSTEXPR result_of<Difference(T1, T2)> abs_diff(const T1& a, const T2& b, const Compare& comp, const Difference& diff)
DXLE_NOEXCEPT_IF(
DXLE_NOEXCEPT_EXPR(std::declval<Difference>()(std::declval<T1>(), std::declval<T2>()))
&& DXLE_NOEXCEPT_EXPR(std::declval<Difference>()(std::declval<T2>(), std::declval<T1>()))
&& DXLE_NOEXCEPT_EXPR(std::declval<Compare>()(std::declval<T1>(), std::declval<T2>()))
)
{
return comp(a, b) ? diff(b, a) : diff(a, b);
}
}
#endif //DXLE_INC_ALGORITHM_ABS_DIFF_HPP_
4 changes: 2 additions & 2 deletions dxlibex/algorithm/safe_dist.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*=============================================================================
/*=============================================================================
Copyright (C) 2015-2017 DxLibEx project
https://github.com/Nagarei/DxLibEx/

Expand All @@ -11,7 +11,7 @@
#include "dxlibex/type_traits/enable_if.hpp"
namespace dxle {
template<typename T1, typename T2>
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;
}
Expand Down
4 changes: 2 additions & 2 deletions dxlibex/basic_types/point2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,9 @@ namespace dxle {
*/
template<typename T1, typename T2>
distance_result_type_t<T1, T2> distance(const point_c<T1>& p1, const point_c<T2>& p2)
DXLE_NOEXCEPT_IF_EXPR(hypot(safe_dist(std::declval<T1>(), std::declval<T2>()), safe_dist(std::declval<T1>(), std::declval<T2>())))
DXLE_NOEXCEPT_IF_EXPR(hypot(abs_diff(std::declval<T1>(), std::declval<T2>()), abs_diff(std::declval<T1>(), std::declval<T2>())))
{
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<int> pointi;
typedef point_c<std::uint8_t> pointu8i;
Expand Down
8 changes: 4 additions & 4 deletions dxlibex/basic_types/point3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ namespace dxle {
//!1. operator bool
//!2. operator != (nullptr)
//!3. default constector + operator !=
DXLE_CONSTEXPR explicit operator bool()
DXLE_CONSTEXPR explicit operator bool()
const DXLE_NOEXCEPT_IF_EXPR((dxle::detail::operator_bool_helper(std::declval<value_type>(), std::declval<value_type>(), std::declval<value_type>())))
{
return dxle::detail::operator_bool_helper(this->x, this->y, this->z);
Expand Down Expand Up @@ -640,11 +640,11 @@ namespace dxle {
\~japanese @return 計算結果。
\~english @return Computed result.
*/
template<typename T1, typename T2>
template<typename T1, typename T2>
distance_result_type_t<T1, T2> distance(const point3d_c<T1>& p1, const point3d_c<T2>& p2)
DXLE_NOEXCEPT_IF_EXPR(hypot(safe_dist(std::declval<T1>(), std::declval<T2>()), safe_dist(std::declval<T1>(), std::declval<T2>())))
DXLE_NOEXCEPT_IF_EXPR(hypot(abs_diff(std::declval<T1>(), std::declval<T2>()), abs_diff(std::declval<T1>(), std::declval<T2>())))
{
return hypot(safe_dist(p1.x, p2.x), hypot(safe_dist(p1.y, p2.y), safe_dist(p1.z, p2.z)));
return hypot(abs_diff(p1.x, p2.x), hypot(abs_diff(p1.y, p2.y), abs_diff(p1.z, p2.z)));
}
typedef point3d_c<int> point3di;
typedef point3d_c<std::uint8_t> point3du8i;
Expand Down
2 changes: 2 additions & 0 deletions dxlibex/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_
21 changes: 21 additions & 0 deletions dxlibex/type_traits/result_of.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*=============================================================================
Copyright (C) 2015-2017 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 <type_traits>
#include "enable_if.hpp"
namespace dxle {
//!inline
namespace type_traits {
using std::result_of;
template <class T>
using result_of_t = typename result_of<T>::type;
}//namespace
using namespace type_traits;
}
#endif //DXLE_INC_TYPE_TRAITS_RESULT_OF_HPP_