Abstracted StringDivision to work with arbitrary containers and digit-types

This commit is contained in:
Leonetienne 2022-02-27 18:56:20 +01:00
parent 1903f19b96
commit 63400955af
5 changed files with 258 additions and 199 deletions

View File

@ -1,52 +1,5 @@
#include "GeneralUtility.h"
#include <string>
#include <sstream>
#include <stdexcept>
// Based on: https://www.geeksforgeeks.org/divide-large-number-represented-string/
std::pair<std::string, int>
GeneralUtility::StringDivision(const std::string& dividend, const unsigned int divisor, const std::string& set) {
// No set? Throw logic error
if (set.length() == 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!");
// As result can be very large store it in string
std::stringstream ss;
// Find prefix of number that is larger than divisor.
int idx = 0;
int temp = Ord(dividend[idx], set);
while (temp < divisor)
temp = temp * set.length() + Ord(dividend[++idx], set);
// 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
ss << (char)(set[(temp / divisor)]);
curRest = temp % divisor;
// Take next digit of number
temp = (temp % divisor) * set.length() + Ord(dividend[++idx], set);
}
// If divisor is greater than number
if (ss.str().length() == 0)
{
// Generate 0-value string
std::stringstream ss;
ss << set[0];
return std::make_pair(ss.str(), BaseX_2_10(dividend, set));
}
// else return the answer
return std::make_pair(ss.str(), curRest);
}
std::string GeneralUtility::BaseX_2_Y(const std::string &num, const std::string &set_in, const std::string &set_out, const std::uint32_t minOutLen) {
if ((set_in.length() == 0) || (set_out.length() == 0))
@ -64,7 +17,7 @@ std::string GeneralUtility::BaseX_2_Y(const std::string &num, const std::string
std::string buf = num;
while (buf != zeroInbase) {
const auto divRes = StringDivision(buf, set_out.length(), set_in);
const auto divRes = DigitstringDivision(buf, set_out.length(), set_in);
const std::uint64_t mod = divRes.second;
buf = divRes.first;
ss << set_out[mod];

View File

@ -3,6 +3,8 @@
#include <algorithm>
#include <utility>
#include <sstream>
#include <stdexcept>
class GeneralUtility {
public:
@ -21,7 +23,8 @@ public:
//! \param divisor The integer divisor
//! \param set The set/base of `dividend`
//! \return A pair of the result. (result, rest)
static std::pair<std::string, int> StringDivision(const std::string& dividend, const unsigned int divisor, const std::string& set = "0123456789");
template <class T_Container>
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
//! \param num A string representing the number
@ -95,4 +98,69 @@ int GeneralUtility::Ord(const T_Type& item, const T_Container& set) {
return result - set.begin();
}
// 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);
}
#endif //GENERALUTILITY_GENERALUTILITY_H

View File

@ -11,7 +11,7 @@ add_executable(Test
main.cpp
Ord.cpp
StringDivision.cpp
DigitstringDivision.cpp
BaseX_2_10.cpp
Base10_2_X.cpp
BaseX_2_Y.cpp

View File

@ -0,0 +1,187 @@
#include <GeneralUtility.h>
#include "Catch2.h"
#include <string>
#include <vector>
#include <time.h>
#include <sstream>
#include <stdexcept>
// Tests that basic division (base10) is working, with oracle values
TEST_CASE(__FILE__"/Base10", "[DigitstringDivision]")
{
const std::string set = "0123456789";
SECTION("No rest") {
const auto res = GeneralUtility::DigitstringDivision<std::string>("200", 10, set);
REQUIRE(res.first == "20");
REQUIRE(res.second == 0);
}
SECTION("With rest") {
const auto res = GeneralUtility::DigitstringDivision<std::string>("205", 10, set);
REQUIRE(res.first == "20");
REQUIRE(res.second == 5);
}
SECTION("With rest, and divisor > dividend") {
const auto res = GeneralUtility::DigitstringDivision<std::string>("205", 299, set);
REQUIRE(res.first == "0");
REQUIRE(res.second == 205);
}
}
// Tests that basic division (base10) is working, by doing a lot of random, precalculated divisions
TEST_CASE(__FILE__"/Base10_Randoms", "[DigitstringDivision]")
{
srand(time(0));
const std::string set = "0123456789";
// Test 1000 random numbers
for (std::size_t i = 0; i < 1000; i++) {
// Generate random input
const std::uint64_t i_dividend = rand();
const std::uint32_t i_divisor = rand() % 1000000;
// Precalculate expected output
const std::uint32_t expected_result = i_dividend / i_divisor;
const std::uint32_t expected_rest = i_dividend % i_divisor;
// Convert dividend to a string
std::stringstream ss;
ss << i_dividend;
const std::string dividend = ss.str();
// Compute results
const auto res = GeneralUtility::DigitstringDivision(dividend, i_divisor, set);
const std::uint32_t actual_result = std::stoi(res.first);
const std::uint32_t actual_rest = res.second;
// Assertions
REQUIRE(actual_result == expected_result);
REQUIRE(actual_rest == expected_rest);
}
}
// Tests that base16 division is working, with oracle values
TEST_CASE(__FILE__"/Base16", "[DigitstringDivision]")
{
const std::string set = "0123456789abcdef";
SECTION("No rest") {
const auto res = GeneralUtility::DigitstringDivision<std::string>("1f4", 10, set);
REQUIRE(res.first == "32");
REQUIRE(res.second == 0);
}
SECTION("With rest") {
const auto res = GeneralUtility::DigitstringDivision<std::string>("1fd", 10, set);
REQUIRE(res.first == "32");
REQUIRE(res.second == 9);
}
SECTION("With rest, and divisor > dividend") {
const auto res = GeneralUtility::DigitstringDivision<std::string>("1f4", 999, set);
REQUIRE(res.first == "0");
REQUIRE(res.second == 500);
}
}
// Tests that base2 division is working, with oracle values
TEST_CASE(__FILE__"/Base2", "[DigitstringDivision]")
{
const std::string set = "01";
SECTION("No rest") {
const auto res = GeneralUtility::DigitstringDivision<std::string>("111110100", 10, set);
REQUIRE(res.first == "110010");
REQUIRE(res.second == 0);
}
SECTION("With rest") {
const auto res = GeneralUtility::DigitstringDivision<std::string>("111111011", 10, set);
REQUIRE(res.first == "110010");
REQUIRE(res.second == 7);
}
SECTION("With rest, and divisor > dividend") {
const auto res = GeneralUtility::DigitstringDivision<std::string>("111110100", 599, set);
REQUIRE(res.first == "0");
REQUIRE(res.second == 500);
}
}
// Tests that fucking big bases are working, with oracle values
TEST_CASE(__FILE__"/BaseFuckingBig", "[DigitstringDivision]")
{
const std::string set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
SECTION("No rest") {
const auto res = GeneralUtility::DigitstringDivision<std::string>("9RieQV", 29915, set);
REQUIRE(res.first == "1DGL");
REQUIRE(res.second == 0);
}
SECTION("With rest") {
const auto res = GeneralUtility::DigitstringDivision<std::string>("9RieQZ", 29915, set);
REQUIRE(res.first == "1DGL");
REQUIRE(res.second == 4);
}
SECTION("With rest, and divisor > dividend") {
const auto res = GeneralUtility::DigitstringDivision<std::string>("8x", 600, set);
REQUIRE(res.first == "0");
REQUIRE(res.second == 555);
}
}
// Tests that having a dividend of size 0 returns 0
TEST_CASE(__FILE__"/DividendLengthZero", "[DigitstringDivision]")
{
const auto& res = GeneralUtility::DigitstringDivision<std::string>("", 19, "0123456789");
REQUIRE(res.first == "0");
REQUIRE(res.second == 0);
}
// Tests that a division by zero exception is thrown when appropriate
TEST_CASE(__FILE__"/DivisionByZero", "[DigitstringDivision]")
{
REQUIRE_THROWS_AS(
GeneralUtility::DigitstringDivision<std::string>("699", 0, "0123456789")
, std::overflow_error);
}
// Tests that a logic error is thrown when the supplied set is empty
TEST_CASE(__FILE__"/NoSetSupplied", "[DigitstringDivision]")
{
REQUIRE_THROWS_AS(
GeneralUtility::DigitstringDivision<std::string>("699", 5, "")
, std::logic_error);
}
// Tests that a logic error is thrown if the dividend contains a character not present in the set
TEST_CASE(__FILE__"/InvalidDigitsInDividend", "[DigitstringDivision]")
{
REQUIRE_THROWS_AS(
GeneralUtility::DigitstringDivision<std::string>("699z", 5, "0123465789")
, std::logic_error);
}
// Tests that weird, abstract bases are working, with oracle values
TEST_CASE(__FILE__"/BaseWeird", "[DigitstringDivision]")
{
const std::vector<std::string> set = { "apple", "apricot", "avocado", "banana", "bell pepper", "bilberry" };
SECTION("No rest") {
const auto res = GeneralUtility::DigitstringDivision<std::vector<std::string>>({"apricot", "bilberry", "bell pepper"}, 10, set);
REQUIRE(res.first == std::vector<std::string>({"apricot", "apricot"}));
REQUIRE(res.second == 0);
}
SECTION("With rest") {
const auto res = GeneralUtility::DigitstringDivision<std::vector<std::string>>({"avocado", "apple", "banana"}, 10, set);
REQUIRE(res.first == std::vector<std::string>({"apricot", "apricot"}));
REQUIRE(res.second == 5);
}
SECTION("With rest, and divisor > dividend") {
const auto res = GeneralUtility::DigitstringDivision<std::vector<std::string>>({"apricot", "bilberry", "bell pepper"}, 100, set);
REQUIRE(res.first == std::vector<std::string>({"apple"}));
REQUIRE(res.second == 70);
}
}

View File

@ -1,149 +0,0 @@
#include <GeneralUtility.h>
#include "Catch2.h"
#include <string>
#include <vector>
#include <time.h>
#include <sstream>
#include <stdexcept>
// Tests that basic division (base10) is working, with oracle values
TEST_CASE(__FILE__"/Base10", "[StringDivision]")
{
const std::string set = "0123456789";
SECTION("No rest") {
const auto res = GeneralUtility::StringDivision("200", 10, set);
REQUIRE(res.first == "20");
REQUIRE(res.second == 0);
}
SECTION("With rest") {
const auto res = GeneralUtility::StringDivision("205", 10, set);
REQUIRE(res.first == "20");
REQUIRE(res.second == 5);
}
SECTION("With rest, and divisor > dividend") {
const auto res = GeneralUtility::StringDivision("205", 299, set);
REQUIRE(res.first == "0");
REQUIRE(res.second == 205);
}
}
// Tests that basic division (base10) is working, by doing a lot of random, precalculated divisions
TEST_CASE(__FILE__"/Base10_Randoms", "[StringDivision]")
{
srand(time(0));
const std::string set = "0123456789";
// Test 1000 random numbers
for (std::size_t i = 0; i < 1000; i++) {
// Generate random input
const std::uint64_t i_dividend = rand();
const std::uint32_t i_divisor = rand() % 1000000;
// Precalculate expected output
const std::uint32_t expected_result = i_dividend / i_divisor;
const std::uint32_t expected_rest = i_dividend % i_divisor;
// Convert dividend to a string
std::stringstream ss;
ss << i_dividend;
const std::string dividend = ss.str();
// Compute results
const auto res = GeneralUtility::StringDivision(dividend, i_divisor, set);
const std::uint32_t actual_result = std::stoi(res.first);
const std::uint32_t actual_rest = res.second;
// Assertions
REQUIRE(actual_result == expected_result);
REQUIRE(actual_rest == expected_rest);
}
}
// Tests that base16 division is working, with oracle values
TEST_CASE(__FILE__"/Base16", "[StringDivision]")
{
const std::string set = "0123456789abcdef";
SECTION("No rest") {
const auto res = GeneralUtility::StringDivision("1f4", 10, set);
REQUIRE(res.first == "32");
REQUIRE(res.second == 0);
}
SECTION("With rest") {
const auto res = GeneralUtility::StringDivision("1fd", 10, set);
REQUIRE(res.first == "32");
REQUIRE(res.second == 9);
}
SECTION("With rest, and divisor > dividend") {
const auto res = GeneralUtility::StringDivision("1f4", 999, set);
REQUIRE(res.first == "0");
REQUIRE(res.second == 500);
}
}
// Tests that base2 division is working, with oracle values
TEST_CASE(__FILE__"/Base2", "[StringDivision]")
{
const std::string set = "01";
SECTION("No rest") {
const auto res = GeneralUtility::StringDivision("111110100", 10, set);
REQUIRE(res.first == "110010");
REQUIRE(res.second == 0);
}
SECTION("With rest") {
const auto res = GeneralUtility::StringDivision("111111011", 10, set);
REQUIRE(res.first == "110010");
REQUIRE(res.second == 7);
}
SECTION("With rest, and divisor > dividend") {
const auto res = GeneralUtility::StringDivision("111110100", 599, set);
REQUIRE(res.first == "0");
REQUIRE(res.second == 500);
}
}
// Tests that fucking big bases are working, with oracle values
TEST_CASE(__FILE__"/BaseFuckingBig", "[StringDivision]")
{
const std::string set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
SECTION("No rest") {
const auto res = GeneralUtility::StringDivision("9RieQV", 29915, set);
REQUIRE(res.first == "1DGL");
REQUIRE(res.second == 0);
}
SECTION("With rest") {
const auto res = GeneralUtility::StringDivision("9RieQZ", 29915, set);
REQUIRE(res.first == "1DGL");
REQUIRE(res.second == 4);
}
SECTION("With rest, and divisor > dividend") {
const auto res = GeneralUtility::StringDivision("8x", 600, set);
REQUIRE(res.first == "0");
REQUIRE(res.second == 555);
}
}
// Tests that a division by zero exception is thrown when appropriate
TEST_CASE(__FILE__"/DivisionByZero", "[StringDivision]")
{
REQUIRE_THROWS_AS(
GeneralUtility::StringDivision("699", 0)
, std::overflow_error);
}
// Tests that a logic error is thrown when the supplied set is empty
TEST_CASE(__FILE__"/NoSetSupplied", "[StringDivision]")
{
REQUIRE_THROWS_AS(
GeneralUtility::StringDivision("699", 5, "")
, std::logic_error);
}