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