2022-02-27 13:48:21 +01:00
|
|
|
#ifndef GENERALUTILITY_GENERALUTILITY_H
|
|
|
|
#define GENERALUTILITY_GENERALUTILITY_H
|
|
|
|
|
2022-02-27 14:05:51 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
2022-02-27 18:56:20 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
2022-02-27 14:05:51 +01:00
|
|
|
|
2022-02-27 13:48:21 +01:00
|
|
|
class GeneralUtility {
|
|
|
|
public:
|
2022-02-27 14:33:54 +01:00
|
|
|
//! Will return the index of `item` in `set`.
|
|
|
|
//! \tparam T_Type The type of `item`
|
|
|
|
//! \tparam T_Container The type of container
|
|
|
|
//! \param item The item to find the index for
|
|
|
|
//! \param set The container to be looking in
|
|
|
|
//! \return The index of `item` in `set`. -1 if not found.
|
2022-02-27 14:05:51 +01:00
|
|
|
template <typename T_Type, class T_Container>
|
|
|
|
static int Ord(const T_Type& item, const T_Container& set);
|
2022-02-27 15:49:09 +01:00
|
|
|
|
|
|
|
//! Will divide a number of arbitrary base in `dividend` by an integer divisor.
|
|
|
|
//! This is a specific helper function for the base conversion functions.
|
|
|
|
//! \param dividend The number to be divided in string form
|
|
|
|
//! \param divisor The integer divisor
|
|
|
|
//! \param set The set/base of `dividend`
|
|
|
|
//! \return A pair of the result. (result, rest)
|
2022-02-27 18:56:20 +01:00
|
|
|
template <class T_Container>
|
|
|
|
static std::pair<T_Container, int> DigitstringDivision(const T_Container& dividend, const unsigned int divisor, const T_Container& set);
|
2022-02-27 15:49:09 +01:00
|
|
|
|
|
|
|
//! Will convert a number of arbitrary base to base 10
|
|
|
|
//! \param num A string representing the number
|
|
|
|
//! \param set The set/base of the number
|
|
|
|
//! \return A 64-bit integer representing the number
|
2022-02-27 17:52:14 +01:00
|
|
|
template <class T_Container>
|
|
|
|
static std::uint64_t BaseX_2_10(const T_Container& num, const T_Container& set);
|
2022-02-27 15:49:09 +01:00
|
|
|
|
2022-02-27 17:08:57 +01:00
|
|
|
//! Will convert a number to an arbitrary base.
|
|
|
|
//! This just a wrapper for BaseX_2_Y.
|
|
|
|
//! \param num The number to be converted
|
|
|
|
//! \param set The desired set/base for the output to be in
|
|
|
|
//! \return `num` in base `set`
|
|
|
|
static std::string Base10_2_X(const std::uint64_t& num, const std::string& set, const std::uint32_t minOutLen = 1);
|
|
|
|
|
2022-02-27 16:55:18 +01:00
|
|
|
//! Will convert a number from an arbitrary base to another arbitrary base.
|
|
|
|
//! \param num A string representation of a number
|
|
|
|
//! \param set_in The set/base of the input
|
|
|
|
//! \param set_out The desired set/base to output
|
|
|
|
//! \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`
|
2022-02-27 17:08:57 +01:00
|
|
|
static std::string BaseX_2_Y(const std::string& num, const std::string& set_in, const std::string& set_out, const std::uint32_t minOutLen = 1);
|
2022-02-27 16:55:18 +01:00
|
|
|
|
2022-02-27 13:48:21 +01:00
|
|
|
private:
|
2022-02-27 14:32:09 +01:00
|
|
|
// No instantiation! >:(
|
2022-02-27 13:48:21 +01:00
|
|
|
GeneralUtility();
|
|
|
|
};
|
|
|
|
|
2022-02-27 17:52:14 +01:00
|
|
|
namespace {
|
|
|
|
// Fast 64-bit int power function
|
|
|
|
inline std::uint64_t Powuli(const std::uint64_t &b, const std::uint64_t &e) {
|
|
|
|
std::uint64_t buf = 1;
|
|
|
|
|
|
|
|
for (std::uint64_t i = 0; i < e; i++)
|
|
|
|
buf *= b;
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T_Container>
|
|
|
|
std::uint64_t GeneralUtility::BaseX_2_10(const T_Container& num, const T_Container& set) {
|
|
|
|
// If base is 0, throw logic error
|
|
|
|
if (set.size() == 0)
|
|
|
|
throw std::logic_error("Can't convert from base0! Please supply a nonempty set!");
|
|
|
|
|
|
|
|
unsigned long long int buf = 0;
|
|
|
|
for (std::size_t i = 0; i < num.size(); i++) {
|
|
|
|
for (std::size_t j = 0; j < set.size(); j++) {
|
|
|
|
if (num[i] == set[j]) {
|
|
|
|
buf += Powuli((std::uint64_t)set.size(), (uint64_t)(num.size() - (i + 1))) * j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2022-02-27 14:05:51 +01:00
|
|
|
template<typename T_Type, class T_Container>
|
2022-02-27 14:32:09 +01:00
|
|
|
int GeneralUtility::Ord(const T_Type& item, const T_Container& set) {
|
2022-02-27 14:05:51 +01:00
|
|
|
const auto result =
|
2022-02-27 14:32:09 +01:00
|
|
|
std::find_if(set.begin(), set.end(), [item](const T_Type& c) -> bool {
|
2022-02-27 14:05:51 +01:00
|
|
|
return c == item;
|
|
|
|
});
|
|
|
|
|
|
|
|
// No item found
|
|
|
|
if (result == set.end())
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return result - set.begin();
|
|
|
|
}
|
|
|
|
|
2022-02-27 18:56:20 +01:00
|
|
|
// Based on: https://www.geeksforgeeks.org/divide-large-number-represented-string/
|
|
|
|
template <class T_Container>
|
|
|
|
std::pair<T_Container, int>
|
|
|
|
GeneralUtility::DigitstringDivision(const T_Container& dividend, const unsigned int divisor, const T_Container& set) {
|
|
|
|
// Quick rejects:
|
|
|
|
|
|
|
|
// No set? Throw logic error
|
|
|
|
if (set.size() == 0)
|
|
|
|
throw std::logic_error("Can't divide a number of base0! Please supply a nonempty set!");
|
|
|
|
|
|
|
|
// No division by 0
|
|
|
|
if (divisor == 0)
|
|
|
|
throw std::overflow_error("Division by zero!");
|
|
|
|
|
|
|
|
// Dividend size 0? Return 0.
|
|
|
|
if (dividend.size() == 0)
|
|
|
|
return std::make_pair(T_Container({set[0]}), 0);
|
|
|
|
|
|
|
|
// Verify that all digits are represented in the set/base
|
|
|
|
for (const auto& c : dividend)
|
|
|
|
if (Ord(c, set) == -1)
|
|
|
|
throw std::logic_error("The supplied dividend contains a digit that is not represented in the set/base!");
|
|
|
|
|
|
|
|
|
|
|
|
// Now for the actual algorithm:
|
|
|
|
T_Container result;
|
|
|
|
|
|
|
|
// Find prefix of number that is larger than divisor.
|
|
|
|
int idx = 0;
|
|
|
|
int temp = Ord(dividend[idx], set);
|
|
|
|
while (temp < divisor) {
|
|
|
|
idx++;
|
|
|
|
if (idx < dividend.size())
|
|
|
|
temp = temp * set.size() + Ord(dividend[idx], set);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repeatedly divide divisor with temp. After
|
|
|
|
// every division, update temp to include one
|
|
|
|
// more digit.
|
|
|
|
int curRest = temp % divisor;
|
|
|
|
while (dividend.size() > idx) {
|
|
|
|
// Store result in answer i.e. temp / divisor
|
|
|
|
result.insert(result.cend(), set[temp / divisor]);
|
|
|
|
curRest = temp % divisor;
|
|
|
|
|
|
|
|
// Take next digit of number
|
|
|
|
idx++;
|
|
|
|
if (idx < dividend.size())
|
|
|
|
temp = (temp % divisor) * set.size() + Ord(dividend[idx], set);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If divisor is greater than number
|
|
|
|
if (result.size() == 0) {
|
|
|
|
// Generate 0-value digitstring
|
|
|
|
result.clear();
|
|
|
|
result.insert(result.cend(), set[0]);
|
|
|
|
return std::make_pair(result, BaseX_2_10(dividend, set));
|
|
|
|
}
|
|
|
|
|
|
|
|
// else return the answer
|
|
|
|
return std::make_pair(result, curRest);
|
|
|
|
}
|
|
|
|
|
2022-02-27 13:48:21 +01:00
|
|
|
#endif //GENERALUTILITY_GENERALUTILITY_H
|