Namespacyfied

This commit is contained in:
Leonetienne 2022-02-27 19:59:09 +01:00
parent 425d53dc90
commit 9bece91a1d
7 changed files with 235 additions and 207 deletions

View File

@ -8,8 +8,11 @@
#include "ContainerUtility.h" #include "ContainerUtility.h"
class BaseConversion { namespace Leonetienne {
public: namespace GeneralUtility {
class BaseConversion {
public:
//! Will divide a number of arbitrary base in `dividend` by an integer divisor. //! Will divide a number of arbitrary base in `dividend` by an integer divisor.
//! This is a specific helper function for the base conversion functions. //! This is a specific helper function for the base conversion functions.
//! \tparam T_Container The type of container used for the digitstring //! \tparam T_Container The type of container used for the digitstring
@ -17,16 +20,17 @@ public:
//! \param divisor The integer divisor //! \param divisor The integer divisor
//! \param set The set/base of `dividend` //! \param set The set/base of `dividend`
//! \return A pair of the result. (result, rest) //! \return A pair of the result. (result, rest)
template <class T_Container> 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 &dividend, const unsigned int divisor, const T_Container &set);
//! Will convert a number of arbitrary base to base 10 //! Will convert a number of arbitrary base to base 10
//! \tparam T_Container The type of container used for the digitstring //! \tparam T_Container The type of container used for the digitstring
//! \param num A listlike container representing the number (such as a string) //! \param num A listlike container representing the number (such as a string)
//! \param set The set/base of the number //! \param set The set/base of the number
//! \return A 64-bit integer representing the number //! \return A 64-bit integer representing the number
template <class T_Container> template<class T_Container>
static std::uint64_t BaseX_2_10(const T_Container& num, const T_Container& set); static std::uint64_t BaseX_2_10(const T_Container &num, const T_Container &set);
//! Will convert a number to an arbitrary base. //! Will convert a number to an arbitrary base.
//! This just a wrapper for BaseX_2_Y. //! This just a wrapper for BaseX_2_Y.
@ -34,8 +38,9 @@ public:
//! \param num The number to be converted //! \param num The number to be converted
//! \param set The desired set/base for the output to be in. Should be a listlike container (such as a string) //! \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` //! \return `num` in base `set`
template <class T_Container> 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. //! Will convert a number from an arbitrary base to another arbitrary base.
//! \tparam T_ContainerIn The type of container used for the incoming digitstring //! \tparam T_ContainerIn The type of container used for the incoming digitstring
@ -45,15 +50,17 @@ public:
//! \param set_out The desired set/base to output //! \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) //! \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` //! \return `num` in base `set_out`
template <class T_ContainerIn, class T_ContainerOut> 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: private:
// No instantiation! >:( // No instantiation! >:(
BaseConversion(); BaseConversion();
}; };
namespace { namespace {
// Fast 64-bit int power function // Fast 64-bit int power function
inline std::uint64_t Powuli(const std::uint64_t &b, const std::uint64_t &e) { inline std::uint64_t Powuli(const std::uint64_t &b, const std::uint64_t &e) {
std::uint64_t buf = 1; std::uint64_t buf = 1;
@ -63,10 +70,10 @@ namespace {
return buf; return buf;
} }
} }
template <class T_Container> template<class T_Container>
std::uint64_t BaseConversion::BaseX_2_10(const T_Container& num, const T_Container& set) { std::uint64_t BaseConversion::BaseX_2_10(const T_Container &num, const T_Container &set) {
// If base is 0, throw logic error // If base is 0, throw logic error
if (set.size() == 0) if (set.size() == 0)
throw std::logic_error("Can't convert from base0! Please supply a nonempty set!"); throw std::logic_error("Can't convert from base0! Please supply a nonempty set!");
@ -75,19 +82,20 @@ std::uint64_t BaseConversion::BaseX_2_10(const T_Container& num, const T_Contain
for (std::size_t i = 0; i < num.size(); i++) { for (std::size_t i = 0; i < num.size(); i++) {
for (std::size_t j = 0; j < set.size(); j++) { for (std::size_t j = 0; j < set.size(); j++) {
if (num[i] == set[j]) { if (num[i] == set[j]) {
buf += Powuli((std::uint64_t)set.size(), (uint64_t)(num.size() - (i + 1))) * j; buf += Powuli((std::uint64_t) set.size(), (uint64_t) (num.size() - (i + 1))) * j;
break; break;
} }
} }
} }
return buf; return buf;
} }
// Based on: https://www.geeksforgeeks.org/divide-large-number-represented-string/ // Based on: https://www.geeksforgeeks.org/divide-large-number-represented-string/
template <class T_Container> template<class T_Container>
std::pair<T_Container, int> std::pair<T_Container, int>
BaseConversion::DigitstringDivision(const T_Container& dividend, const unsigned int divisor, const T_Container& set) { BaseConversion::DigitstringDivision(const T_Container &dividend, const unsigned int divisor,
const T_Container &set) {
// Quick rejects: // Quick rejects:
// No set? Throw logic error // No set? Throw logic error
@ -103,9 +111,10 @@ BaseConversion::DigitstringDivision(const T_Container& dividend, const unsigned
return std::make_pair(T_Container({set[0]}), 0); return std::make_pair(T_Container({set[0]}), 0);
// Verify that all digits are represented in the set/base // Verify that all digits are represented in the set/base
for (const auto& c : dividend) for (const auto &c: dividend)
if (ContainerUtility::Ord(c, set) == -1) 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: // Now for the actual algorithm:
@ -147,10 +156,12 @@ BaseConversion::DigitstringDivision(const T_Container& dividend, const unsigned
// else return the answer // else return the answer
return std::make_pair(result, curRest); return std::make_pair(result, curRest);
} }
template <class T_ContainerIn, class T_ContainerOut> 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)) 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!"); throw std::logic_error("Can't convert from or to base0! Please supply a nonempty set!");
@ -179,32 +190,30 @@ T_ContainerOut BaseConversion::BaseX_2_Y(const T_ContainerIn& num, const T_Conta
for (std::size_t i = 0; i < buf.size(); i++) for (std::size_t i = 0; i < buf.size(); i++)
result.insert(result.cend(), buf[buf.size() - i - 1]); 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. // If num is 0, just pass a null-value. The algorithm would hang otherwise.
result.insert(result.cend(), set_out[0]); result.insert(result.cend(), set_out[0]);
} }
// Add as much null-values to the left as requested. // 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 std::size_t cachedLen = result.size();
const T_ContainerOut cachedStr = result; const T_ContainerOut cachedStr = result;
result.clear(); result.clear();
for (std::size_t i = 0; i < minOutLen - cachedLen; i++) for (std::size_t i = 0; i < minOutLen - cachedLen; i++)
result.insert(result.cend(), set_out[0]); result.insert(result.cend(), set_out[0]);
for (const auto& it : cachedStr) for (const auto &it: cachedStr)
result.insert(result.cend(), it); result.insert(result.cend(), it);
} }
return result; return result;
} }
template <class T_Container> 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 // Convert num to a string
std::stringstream ss; std::stringstream ss;
ss << num; ss << num;
@ -215,6 +224,10 @@ T_Container BaseConversion::Base10_2_X(const std::uint64_t &num, const T_Contain
// return it // return it
return convertedNum; return convertedNum;
}
}
} }
#endif //GENERALUTILITY_BASECONVERSION_H #endif //GENERALUTILITY_BASECONVERSION_H

View File

@ -3,26 +3,29 @@
#include <algorithm> #include <algorithm>
class ContainerUtility { namespace Leonetienne {
public: namespace GeneralUtility {
class ContainerUtility {
public:
//! Will return the index of `item` in `set`. //! Will return the index of `item` in `set`.
//! \tparam T_Type The type of `item` //! \tparam T_Type The type of `item`
//! \tparam T_Container The type of container //! \tparam T_Container The type of container
//! \param item The item to find the index for //! \param item The item to find the index for
//! \param set The container to be looking in //! \param set The container to be looking in
//! \return The index of `item` in `set`. -1 if not found. //! \return The index of `item` in `set`. -1 if not found.
template <typename T_Type, class T_Container> template<typename T_Type, class T_Container>
static int Ord(const T_Type& item, const T_Container& set); static int Ord(const T_Type &item, const T_Container &set);
private: private:
// No instantiation! >:( // No instantiation! >:(
ContainerUtility(); ContainerUtility();
}; };
template<typename T_Type, class T_Container> template<typename T_Type, class T_Container>
int ContainerUtility::Ord(const T_Type& item, const T_Container& set) { int ContainerUtility::Ord(const T_Type &item, const T_Container &set) {
const auto result = const auto result =
std::find_if(set.begin(), set.end(), [item](const T_Type& c) -> bool { std::find_if(set.begin(), set.end(), [item](const T_Type &c) -> bool {
return c == item; return c == item;
}); });
@ -31,6 +34,8 @@ int ContainerUtility::Ord(const T_Type& item, const T_Container& set) {
return -1; return -1;
else else
return result - set.begin(); return result - set.begin();
}
}
} }
#endif //GENERALUTILITY_CONTAINERUTILITY_H #endif //GENERALUTILITY_CONTAINERUTILITY_H

View File

@ -1,6 +1,8 @@
#include <BaseConversion.h> #include <BaseConversion.h>
#include "Catch2.h" #include "Catch2.h"
using namespace Leonetienne::GeneralUtility;
// Tests base 10 to 10 // Tests base 10 to 10
TEST_CASE(__FILE__"/Base10_to_10", "[BaseConversion][Base10_2_X]") TEST_CASE(__FILE__"/Base10_to_10", "[BaseConversion][Base10_2_X]")
{ {

View File

@ -1,6 +1,8 @@
#include <BaseConversion.h> #include <BaseConversion.h>
#include "Catch2.h" #include "Catch2.h"
using namespace Leonetienne::GeneralUtility;
// Tests base 10 to 10 // Tests base 10 to 10
TEST_CASE(__FILE__"/Base10_to_10", "[BaseConversion][BaseX_2_10]") TEST_CASE(__FILE__"/Base10_to_10", "[BaseConversion][BaseX_2_10]")
{ {

View File

@ -1,6 +1,8 @@
#include <BaseConversion.h> #include <BaseConversion.h>
#include "Catch2.h" #include "Catch2.h"
using namespace Leonetienne::GeneralUtility;
// Tests base 10 to 10 // Tests base 10 to 10
TEST_CASE(__FILE__"/Base10_to_10", "[BaseConversion][BaseX_2_Y]") TEST_CASE(__FILE__"/Base10_to_10", "[BaseConversion][BaseX_2_Y]")
{ {

View File

@ -5,6 +5,8 @@
#include <time.h> #include <time.h>
#include <stdexcept> #include <stdexcept>
using namespace Leonetienne::GeneralUtility;
// Tests that basic division (base10) is working, with oracle values // Tests that basic division (base10) is working, with oracle values
TEST_CASE(__FILE__"/Base10", "[BaseConversion][DigitstringDivision]") TEST_CASE(__FILE__"/Base10", "[BaseConversion][DigitstringDivision]")
{ {

View File

@ -3,6 +3,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
using namespace Leonetienne::GeneralUtility;
// Tests that the Ord method works with characters in a string // Tests that the Ord method works with characters in a string
TEST_CASE(__FILE__"/WorksWithCharsInString", "[ContainerUtility][Ord]") TEST_CASE(__FILE__"/WorksWithCharsInString", "[ContainerUtility][Ord]")
{ {