williamr@2
|
1 |
// Boost common_factor_ct.hpp header file ----------------------------------//
|
williamr@2
|
2 |
|
williamr@2
|
3 |
// (C) Copyright Daryle Walker and Stephen Cleary 2001-2002.
|
williamr@2
|
4 |
// Distributed under the Boost Software License, Version 1.0. (See
|
williamr@2
|
5 |
// accompanying file LICENSE_1_0.txt or copy at
|
williamr@2
|
6 |
// http://www.boost.org/LICENSE_1_0.txt)
|
williamr@2
|
7 |
|
williamr@2
|
8 |
// See http://www.boost.org for updates, documentation, and revision history.
|
williamr@2
|
9 |
|
williamr@2
|
10 |
#ifndef BOOST_MATH_COMMON_FACTOR_CT_HPP
|
williamr@2
|
11 |
#define BOOST_MATH_COMMON_FACTOR_CT_HPP
|
williamr@2
|
12 |
|
williamr@2
|
13 |
#include <boost/math_fwd.hpp> // self include
|
williamr@2
|
14 |
|
williamr@2
|
15 |
#include <boost/config.hpp> // for BOOST_STATIC_CONSTANT, etc.
|
williamr@2
|
16 |
|
williamr@2
|
17 |
|
williamr@2
|
18 |
namespace boost
|
williamr@2
|
19 |
{
|
williamr@2
|
20 |
namespace math
|
williamr@2
|
21 |
{
|
williamr@2
|
22 |
|
williamr@2
|
23 |
|
williamr@2
|
24 |
// Implementation details --------------------------------------------------//
|
williamr@2
|
25 |
|
williamr@2
|
26 |
namespace detail
|
williamr@2
|
27 |
{
|
williamr@2
|
28 |
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
williamr@2
|
29 |
// Build GCD with Euclid's recursive algorithm
|
williamr@2
|
30 |
template < unsigned long Value1, unsigned long Value2 >
|
williamr@2
|
31 |
struct static_gcd_helper_t
|
williamr@2
|
32 |
{
|
williamr@2
|
33 |
private:
|
williamr@2
|
34 |
BOOST_STATIC_CONSTANT( unsigned long, new_value1 = Value2 );
|
williamr@2
|
35 |
BOOST_STATIC_CONSTANT( unsigned long, new_value2 = Value1 % Value2 );
|
williamr@2
|
36 |
|
williamr@2
|
37 |
#ifndef __BORLANDC__
|
williamr@2
|
38 |
#define BOOST_DETAIL_GCD_HELPER_VAL(Value) static_cast<unsigned long>(Value)
|
williamr@2
|
39 |
#else
|
williamr@2
|
40 |
typedef static_gcd_helper_t self_type;
|
williamr@2
|
41 |
#define BOOST_DETAIL_GCD_HELPER_VAL(Value) (self_type:: Value )
|
williamr@2
|
42 |
#endif
|
williamr@2
|
43 |
|
williamr@2
|
44 |
typedef static_gcd_helper_t< BOOST_DETAIL_GCD_HELPER_VAL(new_value1),
|
williamr@2
|
45 |
BOOST_DETAIL_GCD_HELPER_VAL(new_value2) > next_step_type;
|
williamr@2
|
46 |
|
williamr@2
|
47 |
#undef BOOST_DETAIL_GCD_HELPER_VAL
|
williamr@2
|
48 |
|
williamr@2
|
49 |
public:
|
williamr@2
|
50 |
BOOST_STATIC_CONSTANT( unsigned long, value = next_step_type::value );
|
williamr@2
|
51 |
};
|
williamr@2
|
52 |
|
williamr@2
|
53 |
// Non-recursive case
|
williamr@2
|
54 |
template < unsigned long Value1 >
|
williamr@2
|
55 |
struct static_gcd_helper_t< Value1, 0UL >
|
williamr@2
|
56 |
{
|
williamr@2
|
57 |
BOOST_STATIC_CONSTANT( unsigned long, value = Value1 );
|
williamr@2
|
58 |
};
|
williamr@2
|
59 |
#else
|
williamr@2
|
60 |
// Use inner class template workaround from Peter Dimov
|
williamr@2
|
61 |
template < unsigned long Value1 >
|
williamr@2
|
62 |
struct static_gcd_helper2_t
|
williamr@2
|
63 |
{
|
williamr@2
|
64 |
template < unsigned long Value2 >
|
williamr@2
|
65 |
struct helper
|
williamr@2
|
66 |
{
|
williamr@2
|
67 |
BOOST_STATIC_CONSTANT( unsigned long, value
|
williamr@2
|
68 |
= static_gcd_helper2_t<Value2>::BOOST_NESTED_TEMPLATE
|
williamr@2
|
69 |
helper<Value1 % Value2>::value );
|
williamr@2
|
70 |
};
|
williamr@2
|
71 |
|
williamr@2
|
72 |
template < >
|
williamr@2
|
73 |
struct helper< 0UL >
|
williamr@2
|
74 |
{
|
williamr@2
|
75 |
BOOST_STATIC_CONSTANT( unsigned long, value = Value1 );
|
williamr@2
|
76 |
};
|
williamr@2
|
77 |
};
|
williamr@2
|
78 |
|
williamr@2
|
79 |
// Special case
|
williamr@2
|
80 |
template < >
|
williamr@2
|
81 |
struct static_gcd_helper2_t< 0UL >
|
williamr@2
|
82 |
{
|
williamr@2
|
83 |
template < unsigned long Value2 >
|
williamr@2
|
84 |
struct helper
|
williamr@2
|
85 |
{
|
williamr@2
|
86 |
BOOST_STATIC_CONSTANT( unsigned long, value = Value2 );
|
williamr@2
|
87 |
};
|
williamr@2
|
88 |
};
|
williamr@2
|
89 |
|
williamr@2
|
90 |
// Build the GCD from the above template(s)
|
williamr@2
|
91 |
template < unsigned long Value1, unsigned long Value2 >
|
williamr@2
|
92 |
struct static_gcd_helper_t
|
williamr@2
|
93 |
{
|
williamr@2
|
94 |
BOOST_STATIC_CONSTANT( unsigned long, value
|
williamr@2
|
95 |
= static_gcd_helper2_t<Value1>::BOOST_NESTED_TEMPLATE
|
williamr@2
|
96 |
helper<Value2>::value );
|
williamr@2
|
97 |
};
|
williamr@2
|
98 |
#endif
|
williamr@2
|
99 |
|
williamr@2
|
100 |
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
williamr@2
|
101 |
// Build the LCM from the GCD
|
williamr@2
|
102 |
template < unsigned long Value1, unsigned long Value2 >
|
williamr@2
|
103 |
struct static_lcm_helper_t
|
williamr@2
|
104 |
{
|
williamr@2
|
105 |
typedef static_gcd_helper_t<Value1, Value2> gcd_type;
|
williamr@2
|
106 |
|
williamr@2
|
107 |
BOOST_STATIC_CONSTANT( unsigned long, value = Value1 / gcd_type::value
|
williamr@2
|
108 |
* Value2 );
|
williamr@2
|
109 |
};
|
williamr@2
|
110 |
|
williamr@2
|
111 |
// Special case for zero-GCD values
|
williamr@2
|
112 |
template < >
|
williamr@2
|
113 |
struct static_lcm_helper_t< 0UL, 0UL >
|
williamr@2
|
114 |
{
|
williamr@2
|
115 |
BOOST_STATIC_CONSTANT( unsigned long, value = 0UL );
|
williamr@2
|
116 |
};
|
williamr@2
|
117 |
#else
|
williamr@2
|
118 |
// Adapt GCD's inner class template workaround for LCM
|
williamr@2
|
119 |
template < unsigned long Value1 >
|
williamr@2
|
120 |
struct static_lcm_helper2_t
|
williamr@2
|
121 |
{
|
williamr@2
|
122 |
template < unsigned long Value2 >
|
williamr@2
|
123 |
struct helper
|
williamr@2
|
124 |
{
|
williamr@2
|
125 |
typedef static_gcd_helper_t<Value1, Value2> gcd_type;
|
williamr@2
|
126 |
|
williamr@2
|
127 |
BOOST_STATIC_CONSTANT( unsigned long, value = Value1
|
williamr@2
|
128 |
/ gcd_type::value * Value2 );
|
williamr@2
|
129 |
};
|
williamr@2
|
130 |
|
williamr@2
|
131 |
template < >
|
williamr@2
|
132 |
struct helper< 0UL >
|
williamr@2
|
133 |
{
|
williamr@2
|
134 |
BOOST_STATIC_CONSTANT( unsigned long, value = 0UL );
|
williamr@2
|
135 |
};
|
williamr@2
|
136 |
};
|
williamr@2
|
137 |
|
williamr@2
|
138 |
// Special case
|
williamr@2
|
139 |
template < >
|
williamr@2
|
140 |
struct static_lcm_helper2_t< 0UL >
|
williamr@2
|
141 |
{
|
williamr@2
|
142 |
template < unsigned long Value2 >
|
williamr@2
|
143 |
struct helper
|
williamr@2
|
144 |
{
|
williamr@2
|
145 |
BOOST_STATIC_CONSTANT( unsigned long, value = 0UL );
|
williamr@2
|
146 |
};
|
williamr@2
|
147 |
};
|
williamr@2
|
148 |
|
williamr@2
|
149 |
// Build the LCM from the above template(s)
|
williamr@2
|
150 |
template < unsigned long Value1, unsigned long Value2 >
|
williamr@2
|
151 |
struct static_lcm_helper_t
|
williamr@2
|
152 |
{
|
williamr@2
|
153 |
BOOST_STATIC_CONSTANT( unsigned long, value
|
williamr@2
|
154 |
= static_lcm_helper2_t<Value1>::BOOST_NESTED_TEMPLATE
|
williamr@2
|
155 |
helper<Value2>::value );
|
williamr@2
|
156 |
};
|
williamr@2
|
157 |
#endif
|
williamr@2
|
158 |
|
williamr@2
|
159 |
} // namespace detail
|
williamr@2
|
160 |
|
williamr@2
|
161 |
|
williamr@2
|
162 |
// Compile-time greatest common divisor evaluator class declaration --------//
|
williamr@2
|
163 |
|
williamr@2
|
164 |
template < unsigned long Value1, unsigned long Value2 >
|
williamr@2
|
165 |
struct static_gcd
|
williamr@2
|
166 |
{
|
williamr@2
|
167 |
BOOST_STATIC_CONSTANT( unsigned long, value
|
williamr@2
|
168 |
= (detail::static_gcd_helper_t<Value1, Value2>::value) );
|
williamr@2
|
169 |
|
williamr@2
|
170 |
}; // boost::math::static_gcd
|
williamr@2
|
171 |
|
williamr@2
|
172 |
|
williamr@2
|
173 |
// Compile-time least common multiple evaluator class declaration ----------//
|
williamr@2
|
174 |
|
williamr@2
|
175 |
template < unsigned long Value1, unsigned long Value2 >
|
williamr@2
|
176 |
struct static_lcm
|
williamr@2
|
177 |
{
|
williamr@2
|
178 |
BOOST_STATIC_CONSTANT( unsigned long, value
|
williamr@2
|
179 |
= (detail::static_lcm_helper_t<Value1, Value2>::value) );
|
williamr@2
|
180 |
|
williamr@2
|
181 |
}; // boost::math::static_lcm
|
williamr@2
|
182 |
|
williamr@2
|
183 |
|
williamr@2
|
184 |
} // namespace math
|
williamr@2
|
185 |
} // namespace boost
|
williamr@2
|
186 |
|
williamr@2
|
187 |
|
williamr@2
|
188 |
#endif // BOOST_MATH_COMMON_FACTOR_CT_HPP
|