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,6 +8,9 @@
#include "ContainerUtility.h" #include "ContainerUtility.h"
namespace Leonetienne {
namespace GeneralUtility {
class BaseConversion { class BaseConversion {
public: 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.
@ -18,7 +21,8 @@ public:
//! \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
@ -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) //! \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
@ -46,7 +51,9 @@ public:
//! \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! >:(
@ -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/ // 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
@ -105,7 +113,8 @@ BaseConversion::DigitstringDivision(const T_Container& dividend, const unsigned
// 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:
@ -150,7 +159,9 @@ BaseConversion::DigitstringDivision(const T_Container& dividend, const unsigned
} }
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,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++) 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();
@ -204,7 +212,8 @@ T_ContainerOut BaseConversion::BaseX_2_Y(const T_ContainerIn& num, const T_Conta
} }
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;
@ -217,4 +226,8 @@ T_Container BaseConversion::Base10_2_X(const std::uint64_t &num, const T_Contain
return convertedNum; return convertedNum;
} }
}
}
#endif //GENERALUTILITY_BASECONVERSION_H #endif //GENERALUTILITY_BASECONVERSION_H

View File

@ -3,6 +3,9 @@
#include <algorithm> #include <algorithm>
namespace Leonetienne {
namespace GeneralUtility {
class ContainerUtility { class ContainerUtility {
public: public:
//! Will return the index of `item` in `set`. //! Will return the index of `item` in `set`.
@ -32,5 +35,7 @@ int ContainerUtility::Ord(const T_Type& item, const T_Container& set) {
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]")
{ {