Skip to content

Commit

Permalink
Split mathhelpers.hpp into dedicated include files for each functiona…
Browse files Browse the repository at this point in the history
…lity
  • Loading branch information
sjanel committed May 9, 2024
1 parent f926fb4 commit e54726d
Show file tree
Hide file tree
Showing 21 changed files with 281 additions and 210 deletions.
3 changes: 2 additions & 1 deletion src/api/exchanges/src/bithumbprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
#include "exchangepublicapitypes.hpp"
#include "file.hpp"
#include "httprequesttype.hpp"
#include "ipow.hpp"
#include "market.hpp"
#include "mathhelpers.hpp"
#include "monetaryamount.hpp"
#include "ndigits.hpp"
#include "opened-order.hpp"
#include "orderid.hpp"
#include "ordersconstraints.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/http-request/src/besturlpicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <string_view>

#include "cct_log.hpp"
#include "mathhelpers.hpp"
#include "ipow.hpp"

namespace cct {
BestURLPicker::BestURLPicker(std::span<const std::string_view> baseUrls)
Expand Down
3 changes: 2 additions & 1 deletion src/objects/include/monetaryamount.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#include "cct_log.hpp"
#include "cct_string.hpp"
#include "currencycode.hpp"
#include "mathhelpers.hpp"
#include "ipow.hpp"
#include "ndigits.hpp"

namespace cct {

Expand Down
3 changes: 2 additions & 1 deletion src/objects/src/monetaryamount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
#include "cct_exception.hpp"
#include "cct_invalid_argument_exception.hpp"
#include "currencycode.hpp"
#include "mathhelpers.hpp"
#include "ipow.hpp"
#include "ndigits.hpp"
#include "stringhelpers.hpp"

namespace cct {
Expand Down
2 changes: 1 addition & 1 deletion src/objects/test/monetaryamount_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "cct_invalid_argument_exception.hpp"
#include "cct_string.hpp"
#include "currencycode.hpp"
#include "mathhelpers.hpp"
#include "ipow.hpp"

namespace cct {

Expand Down
9 changes: 7 additions & 2 deletions src/tech/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,13 @@ else()
endif()

add_unit_test(
mathhelpers_test
test/mathhelpers_test.cpp
ndigits_test
test/ndigits_test.cpp
)

add_unit_test(
ipow_test
test/ipow_test.cpp
)

add_unit_test(
Expand Down
104 changes: 104 additions & 0 deletions src/tech/include/ipow.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#pragma once

#include <array>
#include <cstdint>
#include <limits>

namespace cct {
/// constexpr and integral version of math.power.
/// Taken from https://gist.github.com/orlp/3551590
constexpr int64_t ipow(int64_t base, uint8_t exp) noexcept {
constexpr uint8_t highest_bit_set[] = {0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 255, // anything past 63 is a guaranteed overflow with base > 1
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};

int64_t result = 1;

switch (highest_bit_set[exp]) {
case 255: // we use 255 as an overflow marker and return 0 on overflow/underflow
return base == 1 ? 1 : (base == -1 ? (1 - 2 * (exp & 1)) : 0);
case 6:
if ((exp & 1U) != 0) {
result *= base;
}
exp >>= 1;
base *= base;
[[fallthrough]];
case 5:
if ((exp & 1U) != 0) {
result *= base;
}
exp >>= 1;
base *= base;
[[fallthrough]];
case 4:
if ((exp & 1U) != 0) {
result *= base;
}
exp >>= 1;
base *= base;
[[fallthrough]];
case 3:
if ((exp & 1U) != 0) {
result *= base;
}
exp >>= 1;
base *= base;
[[fallthrough]];
case 2:
if ((exp & 1U) != 0) {
result *= base;
}
exp >>= 1;
base *= base;
[[fallthrough]];
case 1:
if ((exp & 1U) != 0) {
result *= base;
}
[[fallthrough]];
default:
return result;
}
}

/// Optimization of ipow(10, uint8_t exp)
constexpr int64_t ipow10(uint8_t exp) noexcept {
constexpr const int64_t kPow10Table[] = {1LL,
10LL,
100LL,
1000LL,
10000LL,
100000LL,
1000000LL,
10000000LL,
100000000LL,
1000000000LL,
10000000000LL,
100000000000LL,
1000000000000LL,
10000000000000LL,
100000000000000LL,
1000000000000000LL,
10000000000000000LL,
100000000000000000LL,
1000000000000000000LL};
return exp < sizeof(kPow10Table) / sizeof(kPow10Table[0]) ? kPow10Table[exp] : std::numeric_limits<int64_t>::max();
}

} // namespace cct
4 changes: 2 additions & 2 deletions src/tech/include/levenshteindistancecalculator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
#include "cct_vector.hpp"

namespace cct {

class LevenshteinDistanceCalculator {
public:
LevenshteinDistanceCalculator() noexcept = default;

/// Computes the levenshtein distance between both input words.
/// Complexity is in 'word1.length() * word2.length()' in time,
/// min(word1.length(), word2.length()) in space.
Expand All @@ -18,4 +17,5 @@ class LevenshteinDistanceCalculator {
// This is only for caching purposes, so that repeated calls to distance calculation do not allocate memory each time
vector<int> _minDistance;
};

} // namespace cct
175 changes: 0 additions & 175 deletions src/tech/include/mathhelpers.hpp

This file was deleted.

15 changes: 15 additions & 0 deletions src/tech/include/nchars.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <concepts>

#include "ndigits.hpp"

namespace cct {

/// Count the number of digits including the possible minus sign for negative integrals.
constexpr int nchars(std::signed_integral auto n) noexcept { return ndigits(n) + static_cast<int>(n < 0); }

/// Synonym of ndigits for unsigned types.
constexpr int nchars(std::unsigned_integral auto n) noexcept { return ndigits(n); }

} // namespace cct
Loading

0 comments on commit e54726d

Please sign in to comment.