|
|
|
@@ -8,6 +8,9 @@
|
|
|
|
|
|
|
|
|
|
#include "ContainerUtility.h"
|
|
|
|
|
|
|
|
|
|
namespace Leonetienne {
|
|
|
|
|
namespace GeneralUtility {
|
|
|
|
|
|
|
|
|
|
class BaseConversion {
|
|
|
|
|
public:
|
|
|
|
|
//! Will divide a number of arbitrary base in `dividend` by an integer divisor.
|
|
|
|
@@ -18,7 +21,8 @@ public:
|
|
|
|
|
//! \param set The set/base of `dividend`
|
|
|
|
|
//! \return A pair of the result. (result, rest)
|
|
|
|
|
template<class T_Container>
|
|
|
|
|
static std::pair<T_Container, int> DigitstringDivision(const T_Container& dividend, const unsigned int divisor, const T_Container& set);
|
|
|
|
|
static std::pair<T_Container, int>
|
|
|
|
|
DigitstringDivision(const T_Container ÷nd, const unsigned int divisor, const T_Container &set);
|
|
|
|
|
|
|
|
|
|
//! Will convert a number of arbitrary base to base 10
|
|
|
|
|
//! \tparam T_Container The type of container used for the digitstring
|
|
|
|
@@ -35,7 +39,8 @@ public:
|
|
|
|
|
//! \param set The desired set/base for the output to be in. Should be a listlike container (such as a string)
|
|
|
|
|
//! \return `num` in base `set`
|
|
|
|
|
template<class T_Container>
|
|
|
|
|
static T_Container Base10_2_X(const std::uint64_t& num, const T_Container& set, const std::uint32_t minOutLen = 1);
|
|
|
|
|
static T_Container
|
|
|
|
|
Base10_2_X(const std::uint64_t &num, const T_Container &set, const std::uint32_t minOutLen = 1);
|
|
|
|
|
|
|
|
|
|
//! Will convert a number from an arbitrary base to another arbitrary base.
|
|
|
|
|
//! \tparam T_ContainerIn The type of container used for the incoming digitstring
|
|
|
|
@@ -46,7 +51,9 @@ public:
|
|
|
|
|
//! \param minLen The minimum output length. Setting this will result in zero-padded output (Like, 00000001 instead of 1)
|
|
|
|
|
//! \return `num` in base `set_out`
|
|
|
|
|
template<class T_ContainerIn, class T_ContainerOut>
|
|
|
|
|
static T_ContainerOut BaseX_2_Y(const T_ContainerIn& num, const T_ContainerIn& set_in, const T_ContainerOut& set_out, const std::uint32_t minOutLen = 1);
|
|
|
|
|
static T_ContainerOut
|
|
|
|
|
BaseX_2_Y(const T_ContainerIn &num, const T_ContainerIn &set_in, const T_ContainerOut &set_out,
|
|
|
|
|
const std::uint32_t minOutLen = 1);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// No instantiation! >:(
|
|
|
|
@@ -87,7 +94,8 @@ std::uint64_t BaseConversion::BaseX_2_10(const T_Container& num, const T_Contain
|
|
|
|
|
// Based on: https://www.geeksforgeeks.org/divide-large-number-represented-string/
|
|
|
|
|
template<class T_Container>
|
|
|
|
|
std::pair<T_Container, int>
|
|
|
|
|
BaseConversion::DigitstringDivision(const T_Container& dividend, const unsigned int divisor, const T_Container& set) {
|
|
|
|
|
BaseConversion::DigitstringDivision(const T_Container ÷nd, const unsigned int divisor,
|
|
|
|
|
const T_Container &set) {
|
|
|
|
|
// Quick rejects:
|
|
|
|
|
|
|
|
|
|
// No set? Throw logic error
|
|
|
|
@@ -105,7 +113,8 @@ BaseConversion::DigitstringDivision(const T_Container& dividend, const unsigned
|
|
|
|
|
// Verify that all digits are represented in the set/base
|
|
|
|
|
for (const auto &c: dividend)
|
|
|
|
|
if (ContainerUtility::Ord(c, set) == -1)
|
|
|
|
|
throw std::logic_error("The supplied dividend contains a digit that is not represented in the set/base!");
|
|
|
|
|
throw std::logic_error(
|
|
|
|
|
"The supplied dividend contains a digit that is not represented in the set/base!");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Now for the actual algorithm:
|
|
|
|
@@ -150,7 +159,9 @@ BaseConversion::DigitstringDivision(const T_Container& dividend, const unsigned
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T_ContainerIn, class T_ContainerOut>
|
|
|
|
|
T_ContainerOut BaseConversion::BaseX_2_Y(const T_ContainerIn& num, const T_ContainerIn& set_in, const T_ContainerOut& set_out, const std::uint32_t minOutLen) {
|
|
|
|
|
T_ContainerOut
|
|
|
|
|
BaseConversion::BaseX_2_Y(const T_ContainerIn &num, const T_ContainerIn &set_in, const T_ContainerOut &set_out,
|
|
|
|
|
const std::uint32_t minOutLen) {
|
|
|
|
|
if ((set_in.size() == 0) || (set_out.size() == 0))
|
|
|
|
|
throw std::logic_error("Can't convert from or to base0! Please supply a nonempty set!");
|
|
|
|
|
|
|
|
|
@@ -179,17 +190,14 @@ T_ContainerOut BaseConversion::BaseX_2_Y(const T_ContainerIn& num, const T_Conta
|
|
|
|
|
for (std::size_t i = 0; i < buf.size(); i++)
|
|
|
|
|
result.insert(result.cend(), buf[buf.size() - i - 1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
} else {
|
|
|
|
|
// If num is 0, just pass a null-value. The algorithm would hang otherwise.
|
|
|
|
|
result.insert(result.cend(), set_out[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add as much null-values to the left as requested.
|
|
|
|
|
if (result.size() < minOutLen)
|
|
|
|
|
{
|
|
|
|
|
if (result.size() < minOutLen) {
|
|
|
|
|
const std::size_t cachedLen = result.size();
|
|
|
|
|
const T_ContainerOut cachedStr = result;
|
|
|
|
|
result.clear();
|
|
|
|
@@ -204,7 +212,8 @@ T_ContainerOut BaseConversion::BaseX_2_Y(const T_ContainerIn& num, const T_Conta
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T_Container>
|
|
|
|
|
T_Container BaseConversion::Base10_2_X(const std::uint64_t &num, const T_Container& set, const std::uint32_t minOutLen) {
|
|
|
|
|
T_Container
|
|
|
|
|
BaseConversion::Base10_2_X(const std::uint64_t &num, const T_Container &set, const std::uint32_t minOutLen) {
|
|
|
|
|
// Convert num to a string
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
ss << num;
|
|
|
|
@@ -217,4 +226,8 @@ T_Container BaseConversion::Base10_2_X(const std::uint64_t &num, const T_Contain
|
|
|
|
|
return convertedNum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif //GENERALUTILITY_BASECONVERSION_H
|
|
|
|
|