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_LCM_HPP_INCLUDED 13 #define BOOST_MPL_LCM_HPP_INCLUDED 14 15 #include <boost/mpl/integral_c.hpp> 16 #include <boost/ratio/detail/mpl/abs.hpp> 17 #include <boost/mpl/aux_/largest_int.hpp> 18 #include <boost/mpl/aux_/na_spec.hpp> 19 #include <boost/mpl/aux_/lambda_support.hpp> 20 #include <boost/mpl/aux_/config/integral.hpp> 21 #include <boost/mpl/aux_/config/static_constant.hpp> 22 #include <boost/mpl/aux_/config/dependent_nttp.hpp> 23 #include <boost/cstdint.hpp> 24 25 #if !defined(BOOST_MPL_CFG_NO_NESTED_VALUE_ARITHMETIC_2) \ 26 && !defined(BOOST_MPL_PREPROCESSING_MODE) \ 27 && !defined(__CUDACC__) \ 28 && ( defined(BOOST_MSVC) \ 29 || BOOST_WORKAROUND(__EDG_VERSION__, <= 238) \ 30 ) 31 32 # define BOOST_MPL_CFG_NO_NESTED_VALUE_ARITHMETIC_2 33 34 #endif 35 36 namespace boost { namespace mpl { 37 38 template< typename Tag1, typename Tag2 > struct lcm_impl; 39 40 template< typename T > struct lcm_tag 41 { 42 typedef typename T::tag type; 43 }; 44 45 template< 46 typename BOOST_MPL_AUX_NA_PARAM(N1) 47 , typename BOOST_MPL_AUX_NA_PARAM(N2) 48 > 49 struct lcm 50 : lcm_impl< 51 typename lcm_tag<N1>::type 52 , typename lcm_tag<N2>::type 53 >::template apply<N1, N2>::type 54 { 55 BOOST_MPL_AUX_LAMBDA_SUPPORT(2, lcm, (N1, N2)) 56 }; 57 58 BOOST_MPL_AUX_NA_SPEC(2, lcm) 59 60 template< 61 typename T 62 , T n1 63 , T n2 64 > 65 struct lcm_c 66 : lcm<integral_c<T,n1>,integral_c<T,n2> > 67 { 68 }; 69 70 71 namespace aux { 72 // Workaround for error: the type of partial specialization template parameter constant "n2" 73 // depends on another template parameter 74 // Note: this solution could be wrong for n1 or n2 = [2**63 .. 2**64-1] 75 #if defined(BOOST_MPL_CFG_NO_DEPENDENT_NONTYPE_PARAMETER_IN_PARTIAL_SPEC) 76 template< typename T1, boost::intmax_t n1, bool n1_is_0 77 , typename T2, boost::intmax_t n2, bool n2_is_0 > 78 struct lcm_aux 79 : abs<integral_c< typename aux::largest_int<T1, T2>::type, 80 ( n1 / gcd<integral_c<T1,n1>, integral_c<T2,n2> >::value * n2 ) 81 > > 82 {}; 83 84 template <typename T1, boost::intmax_t n1, typename T2, boost::intmax_t n2> 85 struct lcm_aux<T1, n1, false, T2, n2, true> : integral_c<T2, 0> 86 {}; 87 88 template <typename T1, boost::intmax_t n1, typename T2, boost::intmax_t n2, bool C> 89 struct lcm_aux<T1, n1, true, T2, n2, C> : integral_c<T1, 0> 90 {}; 91 92 93 #else // defined(BOOST_MPL_CFG_NO_DEPENDENT_NONTYPE_PARAMETER_IN_PARTIAL_SPEC) 94 95 96 template< typename T1, T1 n1, bool n1_is_0, typename T2, T2 n2, bool n2_is_0 > 97 struct lcm_aux 98 99 : abs<integral_c< typename aux::largest_int<T1, T2>::type, 100 ( n1 / gcd<integral_c<T1,n1>, integral_c<T2,n2> >::value * n2 ) 101 > > 102 {}; 103 104 template <typename T1, T1 n1, typename T2, T2 n2> 105 struct lcm_aux<T1, n1, false, T2, n2, true> : integral_c<T2, 0> 106 {}; 107 108 template <typename T1, T1 n1, typename T2, T2 n2, bool C> 109 struct lcm_aux<T1, n1, true, T2, n2, C> : integral_c<T1, 0> 110 {}; 111 #endif // defined(BOOST_MPL_CFG_NO_DEPENDENT_NONTYPE_PARAMETER_IN_PARTIAL_SPEC) 112 } 113 114 template<> 115 struct lcm_impl<integral_c_tag, integral_c_tag> 116 { 117 template< typename N1, typename N2 > struct apply 118 : abs<aux::lcm_aux< typename N1::value_type, N1::value, N1::value==0, 119 typename N2::value_type, N2::value, N2::value==0 > > 120 { 121 }; 122 }; 123 124 }} 125 126 #endif // BOOST_MPL_LCM_HPP_INCLUDED 127