gcd.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright Vicente J. Botet Escriba 2010
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/mpl for documentation.
  10. //
  11. ////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_MPL_GCD_HPP_INCLUDED
  13. #define BOOST_MPL_GCD_HPP_INCLUDED
  14. #include <boost/mpl/integral_c.hpp>
  15. #include <boost/ratio/detail/mpl/abs.hpp>
  16. #include <boost/mpl/aux_/largest_int.hpp>
  17. #include <boost/mpl/aux_/na_spec.hpp>
  18. #include <boost/mpl/aux_/lambda_support.hpp>
  19. #include <boost/mpl/aux_/config/integral.hpp>
  20. #include <boost/mpl/aux_/config/static_constant.hpp>
  21. #include <boost/mpl/aux_/config/dependent_nttp.hpp>
  22. #include <boost/cstdint.hpp>
  23. #if !defined(BOOST_MPL_CFG_NO_NESTED_VALUE_ARITHMETIC_2) \
  24. && !defined(BOOST_MPL_PREPROCESSING_MODE) \
  25. && !defined(__CUDACC__) \
  26. && ( defined(BOOST_MSVC) \
  27. || BOOST_WORKAROUND(__EDG_VERSION__, <= 238) \
  28. )
  29. # define BOOST_MPL_CFG_NO_NESTED_VALUE_ARITHMETIC_2
  30. #endif
  31. namespace boost { namespace mpl {
  32. template< typename Tag1, typename Tag2 > struct gcd_impl;
  33. template< typename T > struct gcd_tag
  34. {
  35. typedef typename T::tag type;
  36. };
  37. template<
  38. typename BOOST_MPL_AUX_NA_PARAM(N1)
  39. , typename BOOST_MPL_AUX_NA_PARAM(N2)
  40. >
  41. struct gcd
  42. : gcd_impl<
  43. typename gcd_tag<N1>::type
  44. , typename gcd_tag<N2>::type
  45. >::template apply<N1, N2>::type
  46. {
  47. BOOST_MPL_AUX_LAMBDA_SUPPORT(2, gcd, (N1, N2))
  48. };
  49. BOOST_MPL_AUX_NA_SPEC(2, gcd)
  50. template<
  51. typename T
  52. , T n1
  53. , T n2
  54. >
  55. struct gcd_c
  56. : gcd<integral_c<T,n1>,integral_c<T,n2> >
  57. {
  58. };
  59. namespace aux {
  60. // Workaround for error: the type of partial specialization template parameter constant "n2"
  61. // depends on another template parameter
  62. // Note: this solution could be wrong for n1 or n2 = [2**63 .. 2**64-1]
  63. #if defined(BOOST_MPL_CFG_NO_DEPENDENT_NONTYPE_PARAMETER_IN_PARTIAL_SPEC)
  64. template< typename T1, boost::intmax_t n1, bool n1_is_0
  65. , typename T2, boost::intmax_t n2, bool n2_is_0 >
  66. struct gcd_aux
  67. : gcd_aux<T2, n2, n2==0, T1, (n1 % n2), (n1 % n2)==0>
  68. {};
  69. template <typename T1, boost::intmax_t n1, typename T2, boost::intmax_t n2>
  70. struct gcd_aux<T1, n1, false, T2, n2, true> : integral_c<T1, n1>
  71. {};
  72. template <typename T1, boost::intmax_t n1, typename T2, boost::intmax_t n2, bool C>
  73. struct gcd_aux<T1, n1, true, T2, n2, C> : integral_c<T2, n2>
  74. {};
  75. #else // defined(BOOST_MPL_CFG_NO_DEPENDENT_NONTYPE_PARAMETER_IN_PARTIAL_SPEC)
  76. template< typename T1, T1 n1, bool n1_is_0, typename T2, T2 n2, bool n2_is_0 >
  77. struct gcd_aux
  78. : gcd_aux<T2, n2, n2==0,
  79. typename aux::largest_int<T1, T2>::type,
  80. //~ T1,
  81. (n1 % n2), (n1 % n2)==0>
  82. {};
  83. template <typename T1, T1 n1, typename T2, T2 n2>
  84. struct gcd_aux<T1, n1, false, T2, n2, true> : integral_c<T1, n1>
  85. {};
  86. template <typename T1, T1 n1, typename T2, T2 n2, bool C>
  87. struct gcd_aux<T1, n1, true, T2, n2, C> : integral_c<T2, n2>
  88. {};
  89. #endif // defined(BOOST_MPL_CFG_NO_DEPENDENT_NONTYPE_PARAMETER_IN_PARTIAL_SPEC)
  90. }
  91. template<>
  92. struct gcd_impl<integral_c_tag, integral_c_tag>
  93. {
  94. template< typename N1, typename N2 > struct apply
  95. : abs<aux::gcd_aux< typename N1::value_type, N1::value, N1::value==0,
  96. typename N2::value_type, N2::value, N2::value==0 > >
  97. {
  98. };
  99. };
  100. }}
  101. #endif // BOOST_MPL_GCD_HPP_INCLUDED