Skip to content

Commit

Permalink
Optimised performance for some components.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZCG-coder committed Apr 8, 2024
1 parent 6f6c9e1 commit d0dc3fd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
6 changes: 6 additions & 0 deletions include/fn/basicArithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ namespace steppable::__internals::arithmetic
template<typename Pred>
void loop(const std::string_view& times, Pred predicate)
{
// We're done already!
if (times == "0")
return;
if (times == "1")
return predicate("1");

std::string current = "0";
auto result = compare(current, times, 0);

Expand Down
31 changes: 28 additions & 3 deletions src/power/power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,41 @@ namespace steppable::__internals::arithmetic
{
std::string power(const std::string_view _number, const std::string_view& _raiseTo, const int steps)
{
std::string raiseTo = static_cast<std::string>(_raiseTo);
std::string raiseTo = static_cast<std::string>(_raiseTo), number = static_cast<std::string>(_number);

// Here, we attempt to give a quick answer, instead of doing pointless iterations.
if (number == "1")
{
if (steps == 2)
return "Since the number is 1, the result is 1.";
else if (steps == 1)
return "1"s + symbols::makeSuperscript(static_cast<std::string>(raiseTo)) + " = 1";
else
return "1";
}
else if (number == "0")
{
if (steps == 2)
return "Since the number is 0, the result is 0.";
else if (steps == 1)
return "0"s + symbols::makeSuperscript(static_cast<std::string>(raiseTo)) + " = 0";
else
return "0";
}

auto numberNoTrailingZeros = removeTrailingZeros(number);
size_t numberTrailingZeros = number.length() - numberNoTrailingZeros.length();
// Remove the zeros to reduce the workload.
number = numberNoTrailingZeros;

bool negative = false;
if (compare(raiseTo, "0", 0) == "0")
{
// raiseTo is negative
raiseTo = raiseTo.substr(1);
negative = true;
}
std::string numberOrig = static_cast<std::string>(_number), number = "1";
return reportPower(_number, raiseTo, negative, steps);
return reportPower(number, raiseTo, numberTrailingZeros, negative, steps);
}
} // namespace steppable::__internals::arithmetic

Expand Down
18 changes: 18 additions & 0 deletions src/power/powerReport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,28 @@ using namespace steppable::__internals::arithmetic;

std::string reportPower(const std::string_view _number,
const std::string_view& raiseTo,
const size_t numberTrailingZeros,
const bool negative,
const int steps)
{
std::stringstream ss;
const auto numberOrig = static_cast<std::string>(_number);

auto number = "1"s;

// Here, we attempt to give a quick answer, instead of doing pointless iterations.
if (numberOrig == "1")
goto finish;
else if (numberOrig == "0")
{
if (steps == 2)
return "Since the number is 0, the result is 0.";
else if (steps == 1)
return "0"s + makeSuperscript(static_cast<std::string>(raiseTo)) + " = 0";
else
return "0";
}

loop(raiseTo, [&](const auto& i) {
if (not negative)
number = multiply(number, numberOrig, 0);
Expand All @@ -69,6 +84,7 @@ std::string reportPower(const std::string_view _number,
}
});

finish:
if (negative)
{
if (steps == 2)
Expand All @@ -88,5 +104,7 @@ std::string reportPower(const std::string_view _number,
else if (steps == 0)
ss << number;

loop(multiply(raiseTo, std::to_string(numberTrailingZeros), 0), [&](const auto& _) { ss << "0"; });

return ss.str();
}
6 changes: 5 additions & 1 deletion src/power/powerReport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@
*
* @return The formatted power report.
*/
std::string reportPower(std::string_view _number, const std::string_view& raiseTo, bool negative, int steps);
std::string reportPower(const std::string_view _number,
const std::string_view& raiseTo,
const size_t numberTrailingZeros,
const bool negative,
const int steps);

0 comments on commit d0dc3fd

Please sign in to comment.