min_max.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2016 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MP_MIN_MAX_HPP
  6. #define BOOST_MP_MIN_MAX_HPP
  7. #include <boost/multiprecision/traits/is_backend.hpp>
  8. namespace boost { namespace multiprecision {
  9. //
  10. // Expression template overloads for (min) and (max):
  11. //
  12. // Introduced in response to https://svn.boost.org/trac/boost/ticket/11149
  13. // note that these can not legally be injected into namespace std, and that doing so
  14. // may break future enhancements to the standard. None the less adding
  15. // namespace std{ using boost::multiprecision::(min); using boost::multiprecision::(max); }
  16. // to your code may get some generic code working that wouldn't work otherwise.
  17. //
  18. // The use of enable_if on the return type is to avoid poisoning std::min/max,
  19. // otherwise attempting to make an explicit call to min<long>(a, b) when these and std
  20. // versions are in scope, will cause the compiler to try to instantiate the signatures
  21. // for our versions as well as the std ones, which in turn instantiates number<long>
  22. // which fails to compile as "long" is not a valid backend type.
  23. //
  24. template <class Backend>
  25. inline typename boost::enable_if_c<boost::multiprecision::detail::is_backend<Backend>::value, const number<Backend, et_on>&>::type(min)(const number<Backend, et_on>& a, const number<Backend, et_on>& b)
  26. {
  27. return a < b ? a : b;
  28. }
  29. template <class Backend, class tag, class A1, class A2, class A3, class A4>
  30. inline typename boost::enable_if_c<boost::multiprecision::detail::is_backend<Backend>::value, const number<Backend, et_on> >::type(min)(const number<Backend, et_on>& a, const detail::expression<tag, A1, A2, A3, A4>& b)
  31. {
  32. number<Backend, et_on> t(b);
  33. if (a < t)
  34. return a;
  35. return BOOST_MP_MOVE(t);
  36. }
  37. template <class tag, class A1, class A2, class A3, class A4, class Backend>
  38. inline typename boost::enable_if_c<boost::multiprecision::detail::is_backend<Backend>::value, const number<Backend, et_on> >::type(min)(const detail::expression<tag, A1, A2, A3, A4>& a, const number<Backend, et_on>& b)
  39. {
  40. number<Backend, et_on> t(a);
  41. if (t < b)
  42. return BOOST_MP_MOVE(t);
  43. return b;
  44. }
  45. template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
  46. inline typename detail::expression<tag, A1, A2, A3, A4>::result_type(min)(const detail::expression<tag, A1, A2, A3, A4>& a, const detail::expression<tagb, A1b, A2b, A3b, A4b>& b)
  47. {
  48. typename detail::expression<tag, A1, A2, A3, A4>::result_type t1(a), t2(b);
  49. if (t1 < t2)
  50. return BOOST_MP_MOVE(t1);
  51. return BOOST_MP_MOVE(t2);
  52. }
  53. template <class tag, class A1, class A2, class A3, class A4>
  54. inline typename detail::expression<tag, A1, A2, A3, A4>::result_type(min)(const detail::expression<tag, A1, A2, A3, A4>& a, const detail::expression<tag, A1, A2, A3, A4>& b)
  55. {
  56. typename detail::expression<tag, A1, A2, A3, A4>::result_type t1(a), t2(b);
  57. if (t1 < t2)
  58. return BOOST_MP_MOVE(t1);
  59. return BOOST_MP_MOVE(t2);
  60. }
  61. template <class Backend>
  62. inline typename boost::enable_if_c<boost::multiprecision::detail::is_backend<Backend>::value, const number<Backend, et_on>&>::type(max)(const number<Backend, et_on>& a, const number<Backend, et_on>& b)
  63. {
  64. return a > b ? a : b;
  65. }
  66. template <class Backend, class tag, class A1, class A2, class A3, class A4>
  67. inline typename boost::enable_if_c<boost::multiprecision::detail::is_backend<Backend>::value, const number<Backend, et_on> >::type(max)(const number<Backend, et_on>& a, const detail::expression<tag, A1, A2, A3, A4>& b)
  68. {
  69. number<Backend, et_on> t(b);
  70. if (a > t)
  71. return a;
  72. return BOOST_MP_MOVE(t);
  73. }
  74. template <class tag, class A1, class A2, class A3, class A4, class Backend>
  75. inline typename boost::enable_if_c<boost::multiprecision::detail::is_backend<Backend>::value, const number<Backend, et_on> >::type(max)(const detail::expression<tag, A1, A2, A3, A4>& a, const number<Backend, et_on>& b)
  76. {
  77. number<Backend, et_on> t(a);
  78. if (t > b)
  79. return BOOST_MP_MOVE(t);
  80. return b;
  81. }
  82. template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
  83. inline typename detail::expression<tag, A1, A2, A3, A4>::result_type(max)(const detail::expression<tag, A1, A2, A3, A4>& a, const detail::expression<tagb, A1b, A2b, A3b, A4b>& b)
  84. {
  85. typename detail::expression<tag, A1, A2, A3, A4>::result_type t1(a), t2(b);
  86. if (t1 > t2)
  87. return BOOST_MP_MOVE(t1);
  88. return BOOST_MP_MOVE(t2);
  89. }
  90. template <class tag, class A1, class A2, class A3, class A4>
  91. inline typename detail::expression<tag, A1, A2, A3, A4>::result_type(max)(const detail::expression<tag, A1, A2, A3, A4>& a, const detail::expression<tag, A1, A2, A3, A4>& b)
  92. {
  93. typename detail::expression<tag, A1, A2, A3, A4>::result_type t1(a), t2(b);
  94. if (t1 > t2)
  95. return BOOST_MP_MOVE(t1);
  96. return BOOST_MP_MOVE(t2);
  97. }
  98. }} // namespace boost::multiprecision
  99. #endif