mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Use std::gcd to implement lowestTerms
This commit is contained in:
@@ -25,7 +25,9 @@
|
||||
#include <ripple/ledger/ReadView.h>
|
||||
#include <ripple/protocol/jss.h>
|
||||
#include <ripple/protocol/STAmount.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <numeric>
|
||||
#include <type_traits>
|
||||
|
||||
namespace ripple {
|
||||
@@ -81,47 +83,18 @@ LoadFeeTrack::lowerLocalFee ()
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// NIKB TODO: Once we get C++17, we can replace lowestTerms
|
||||
// with this:
|
||||
//
|
||||
// template <class T1, class T2,
|
||||
// class = std::enable_if_t<
|
||||
// std::is_integral_v<T1> &&
|
||||
// std::is_integral_v<T2>>
|
||||
// >
|
||||
// void lowestTerms(T1& a, T2& b)
|
||||
// {
|
||||
// if (auto const gcd = std::gcd(a, b))
|
||||
// {
|
||||
// a /= gcd;
|
||||
// b /= gcd;
|
||||
// }
|
||||
// }
|
||||
|
||||
template <class T1, class T2,
|
||||
class = std::enable_if_t <
|
||||
std::is_integral<T1>::value &&
|
||||
std::is_unsigned<T1>::value &&
|
||||
sizeof(T1) <= sizeof(std::uint64_t) >,
|
||||
class = std::enable_if_t <
|
||||
std::is_integral<T2>::value &&
|
||||
std::is_unsigned<T2>::value &&
|
||||
sizeof(T2) <= sizeof(std::uint64_t) >
|
||||
class = std::enable_if_t<
|
||||
std::is_integral_v<T1> &&
|
||||
std::is_integral_v<T2>>
|
||||
>
|
||||
void lowestTerms(T1& a, T2& b)
|
||||
{
|
||||
if (a == 0 && b == 0)
|
||||
return;
|
||||
|
||||
std::uint64_t x = a, y = b;
|
||||
while (y != 0)
|
||||
if (auto const gcd = std::gcd(a, b))
|
||||
{
|
||||
auto t = x % y;
|
||||
x = y;
|
||||
y = t;
|
||||
a /= gcd;
|
||||
b /= gcd;
|
||||
}
|
||||
a /= x;
|
||||
b /= x;
|
||||
}
|
||||
|
||||
// Scale using load as well as base rate
|
||||
|
||||
Reference in New Issue
Block a user