Chris@102
|
1 // Boost common_factor_ct.hpp header file ----------------------------------//
|
Chris@102
|
2
|
Chris@102
|
3 // (C) Copyright Daryle Walker and Stephen Cleary 2001-2002.
|
Chris@102
|
4 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@102
|
5 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@102
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
7
|
Chris@102
|
8 // See http://www.boost.org for updates, documentation, and revision history.
|
Chris@102
|
9
|
Chris@102
|
10 #ifndef BOOST_INTEGER_COMMON_FACTOR_CT_HPP
|
Chris@102
|
11 #define BOOST_INTEGER_COMMON_FACTOR_CT_HPP
|
Chris@102
|
12
|
Chris@102
|
13 #include <boost/integer_fwd.hpp> // self include
|
Chris@102
|
14 #include <boost/config.hpp> // for BOOST_STATIC_CONSTANT, etc.
|
Chris@102
|
15
|
Chris@102
|
16 namespace boost
|
Chris@102
|
17 {
|
Chris@102
|
18 namespace integer
|
Chris@102
|
19 {
|
Chris@102
|
20
|
Chris@102
|
21 // Implementation details --------------------------------------------------//
|
Chris@102
|
22
|
Chris@102
|
23 namespace detail
|
Chris@102
|
24 {
|
Chris@102
|
25 // Build GCD with Euclid's recursive algorithm
|
Chris@102
|
26 template < static_gcd_type Value1, static_gcd_type Value2 >
|
Chris@102
|
27 struct static_gcd_helper_t
|
Chris@102
|
28 {
|
Chris@102
|
29 private:
|
Chris@102
|
30 BOOST_STATIC_CONSTANT( static_gcd_type, new_value1 = Value2 );
|
Chris@102
|
31 BOOST_STATIC_CONSTANT( static_gcd_type, new_value2 = Value1 % Value2 );
|
Chris@102
|
32
|
Chris@102
|
33 #ifndef __BORLANDC__
|
Chris@102
|
34 #define BOOST_DETAIL_GCD_HELPER_VAL(Value) static_cast<static_gcd_type>(Value)
|
Chris@102
|
35 #else
|
Chris@102
|
36 typedef static_gcd_helper_t self_type;
|
Chris@102
|
37 #define BOOST_DETAIL_GCD_HELPER_VAL(Value) (self_type:: Value )
|
Chris@102
|
38 #endif
|
Chris@102
|
39
|
Chris@102
|
40 typedef static_gcd_helper_t< BOOST_DETAIL_GCD_HELPER_VAL(new_value1),
|
Chris@102
|
41 BOOST_DETAIL_GCD_HELPER_VAL(new_value2) > next_step_type;
|
Chris@102
|
42
|
Chris@102
|
43 #undef BOOST_DETAIL_GCD_HELPER_VAL
|
Chris@102
|
44
|
Chris@102
|
45 public:
|
Chris@102
|
46 BOOST_STATIC_CONSTANT( static_gcd_type, value = next_step_type::value );
|
Chris@102
|
47 };
|
Chris@102
|
48
|
Chris@102
|
49 // Non-recursive case
|
Chris@102
|
50 template < static_gcd_type Value1 >
|
Chris@102
|
51 struct static_gcd_helper_t< Value1, 0UL >
|
Chris@102
|
52 {
|
Chris@102
|
53 BOOST_STATIC_CONSTANT( static_gcd_type, value = Value1 );
|
Chris@102
|
54 };
|
Chris@102
|
55
|
Chris@102
|
56 // Build the LCM from the GCD
|
Chris@102
|
57 template < static_gcd_type Value1, static_gcd_type Value2 >
|
Chris@102
|
58 struct static_lcm_helper_t
|
Chris@102
|
59 {
|
Chris@102
|
60 typedef static_gcd_helper_t<Value1, Value2> gcd_type;
|
Chris@102
|
61
|
Chris@102
|
62 BOOST_STATIC_CONSTANT( static_gcd_type, value = Value1 / gcd_type::value
|
Chris@102
|
63 * Value2 );
|
Chris@102
|
64 };
|
Chris@102
|
65
|
Chris@102
|
66 // Special case for zero-GCD values
|
Chris@102
|
67 template < >
|
Chris@102
|
68 struct static_lcm_helper_t< 0UL, 0UL >
|
Chris@102
|
69 {
|
Chris@102
|
70 BOOST_STATIC_CONSTANT( static_gcd_type, value = 0UL );
|
Chris@102
|
71 };
|
Chris@102
|
72
|
Chris@102
|
73 } // namespace detail
|
Chris@102
|
74
|
Chris@102
|
75
|
Chris@102
|
76 // Compile-time greatest common divisor evaluator class declaration --------//
|
Chris@102
|
77
|
Chris@102
|
78 template < static_gcd_type Value1, static_gcd_type Value2 > struct static_gcd
|
Chris@102
|
79 {
|
Chris@102
|
80 BOOST_STATIC_CONSTANT( static_gcd_type, value = (detail::static_gcd_helper_t<Value1, Value2>::value) );
|
Chris@102
|
81 }; // boost::integer::static_gcd
|
Chris@102
|
82
|
Chris@102
|
83 #if !defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION)
|
Chris@102
|
84 template< static_gcd_type Value1, static_gcd_type Value2 > static_gcd_type const static_gcd< Value1, Value2 >::value;
|
Chris@102
|
85 #endif
|
Chris@102
|
86
|
Chris@102
|
87 // Compile-time least common multiple evaluator class declaration ----------//
|
Chris@102
|
88
|
Chris@102
|
89 template < static_gcd_type Value1, static_gcd_type Value2 > struct static_lcm
|
Chris@102
|
90 {
|
Chris@102
|
91 BOOST_STATIC_CONSTANT( static_gcd_type, value = (detail::static_lcm_helper_t<Value1, Value2>::value) );
|
Chris@102
|
92 }; // boost::integer::static_lcm
|
Chris@102
|
93
|
Chris@102
|
94 #if !defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION)
|
Chris@102
|
95 template< static_gcd_type Value1, static_gcd_type Value2 > static_gcd_type const static_lcm< Value1, Value2 >::value;
|
Chris@102
|
96 #endif
|
Chris@102
|
97
|
Chris@102
|
98 } // namespace integer
|
Chris@102
|
99 } // namespace boost
|
Chris@102
|
100
|
Chris@102
|
101
|
Chris@102
|
102 #endif // BOOST_INTEGER_COMMON_FACTOR_CT_HPP
|