Added missing includes, remove C++11 requirement, other code cleanup.

Also, use Boost.Swap instead of the direct unqualified call to std::swap
and boost::enable_if_c instead of std::enable_if.
This commit is contained in:
Andrey Semashev
2018-11-03 23:10:44 +03:00
parent 2b08ca9368
commit 2f634ca78b
4 changed files with 39 additions and 29 deletions

View File

@ -38,8 +38,6 @@ The extended Euclidean algorithm solves the integer relation /mx + ny/ = gcd(/m/
int y = res.y; int y = res.y;
// mx + ny = gcd(m,n) should now hold // mx + ny = gcd(m,n) should now hold
Unlike most of the library, the extended Euclidean algorithm requires C++11 features.
[endsect] [endsect]
[section References] [section References]

View File

@ -9,6 +9,8 @@
#include <limits> #include <limits>
#include <stdexcept> #include <stdexcept>
#include <boost/throw_exception.hpp> #include <boost/throw_exception.hpp>
#include <boost/core/swap.hpp>
#include <boost/core/enable_if.hpp>
namespace boost { namespace integer { namespace boost { namespace integer {
@ -16,27 +18,27 @@ namespace boost { namespace integer {
// Solves mx + ny = gcd(m,n). Returns tuple with (gcd(m,n), x, y). // Solves mx + ny = gcd(m,n). Returns tuple with (gcd(m,n), x, y).
template<class Z> template<class Z>
struct euclidean_result_t { struct euclidean_result_t
Z gcd; {
Z x; Z gcd;
Z y; Z x;
Z y;
}; };
template<class Z> template<class Z>
euclidean_result_t<typename std::enable_if<std::numeric_limits< Z >::is_signed, Z>::type> typename boost::enable_if_c< std::numeric_limits< Z >::is_signed, euclidean_result_t< Z > >::type
extended_euclidean(Z m, Z n) extended_euclidean(Z m, Z n)
{ {
if (m < 1 || n < 1) if (m < 1 || n < 1)
{ {
BOOST_THROW_EXCEPTION(std::domain_error("Arguments must be strictly positive.")); BOOST_THROW_EXCEPTION(std::domain_error("extended_euclidean: arguments must be strictly positive"));
} }
bool swapped = false; bool swapped = false;
if (m < n) if (m < n)
{ {
swapped = true; swapped = true;
using std::swap; boost::swap(m, n);
swap(m, n);
} }
Z u0 = m; Z u0 = m;
Z u1 = 1; Z u1 = 1;
@ -49,23 +51,32 @@ extended_euclidean(Z m, Z n)
Z w2; Z w2;
while(v0 > 0) while(v0 > 0)
{ {
Z q = u0/v0; Z q = u0/v0;
w0 = u0 - q*v0; w0 = u0 - q*v0;
w1 = u1 - q*v1; w1 = u1 - q*v1;
w2 = u2 - q*v2; w2 = u2 - q*v2;
u0 = v0; u0 = v0;
u1 = v1; u1 = v1;
u2 = v2; u2 = v2;
v0 = w0; v0 = w0;
v1 = w1; v1 = w1;
v2 = w2; v2 = w2;
} }
if (swapped) euclidean_result_t< Z > result;
result.gcd = u0;
if (!swapped)
{ {
return {u0, u2, u1}; result.x = u1;
result.y = u2;
} }
return {u0, u1, u2}; else
{
result.x = u2;
result.y = u1;
}
return result;
} }
}} }}

View File

@ -8,7 +8,8 @@
#define BOOST_INTEGER_MOD_INVERSE_HPP #define BOOST_INTEGER_MOD_INVERSE_HPP
#include <stdexcept> #include <stdexcept>
#include <boost/throw_exception.hpp> #include <boost/throw_exception.hpp>
#include <boost/optional.hpp> #include <boost/none.hpp>
#include <boost/optional/optional.hpp>
#include <boost/integer/extended_euclidean.hpp> #include <boost/integer/extended_euclidean.hpp>
namespace boost { namespace integer { namespace boost { namespace integer {
@ -25,7 +26,7 @@ boost::optional<Z> mod_inverse(Z a, Z modulus)
{ {
if (modulus < 2) if (modulus < 2)
{ {
BOOST_THROW_EXCEPTION(std::domain_error("Modulus must be > 1.")); BOOST_THROW_EXCEPTION(std::domain_error("mod_inverse: modulus must be > 1"));
} }
// make sure a < modulus: // make sure a < modulus:
a = a % modulus; a = a % modulus;
@ -34,7 +35,7 @@ boost::optional<Z> mod_inverse(Z a, Z modulus)
// a doesn't have a modular multiplicative inverse: // a doesn't have a modular multiplicative inverse:
return boost::none; return boost::none;
} }
euclidean_result_t<Z> u = extended_euclidean(a, modulus); boost::integer::euclidean_result_t<Z> u = boost::integer::extended_euclidean(a, modulus);
if (u.gcd > 1) if (u.gcd > 1)
{ {
return boost::none; return boost::none;

View File

@ -17,8 +17,8 @@ test-suite integer
[ run integer_mask_test.cpp ] [ run integer_mask_test.cpp ]
[ run static_log2_test.cpp ] [ run static_log2_test.cpp ]
[ run static_min_max_test.cpp ] [ run static_min_max_test.cpp ]
[ run extended_euclidean_test.cpp : : : [ requires cxx11_unified_initialization_syntax sfinae_expr ] ] [ run extended_euclidean_test.cpp ]
[ run mod_inverse_test.cpp : : : [ requires cxx11_unified_initialization_syntax sfinae_expr ] ] [ run mod_inverse_test.cpp ]
[ compile integer_traits_include_test.cpp ] [ compile integer_traits_include_test.cpp ]
[ compile integer_include_test.cpp ] [ compile integer_include_test.cpp ]
[ compile integer_mask_include_test.cpp ] [ compile integer_mask_include_test.cpp ]