Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hash combine 64 #164

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/objects/include/currencycode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ class CurrencyCode {
std::ostream &operator<<(std::ostream &os, const CurrencyCode &c);
} // namespace cct

/// Specialize std::hash<CurrencyCode> for easy usage of CurrencyCode as unordered_map key
// Specialize std::hash<CurrencyCode> for easy usage of CurrencyCode as unordered_map key
namespace std {
template <>
struct hash<cct::CurrencyCode> {
std::size_t operator()(const cct::CurrencyCode &c) const { return cct::HashValue64(c.code()); }
size_t operator()(const cct::CurrencyCode &c) const { return cct::HashValue64(c.code()); }
};
} // namespace std
4 changes: 2 additions & 2 deletions src/objects/include/market.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class Market {
namespace std {
template <>
struct hash<cct::Market> {
std::size_t operator()(const cct::Market& m) const {
return cct::HashCombine(std::hash<cct::CurrencyCode>{}(m.base()), std::hash<cct::CurrencyCode>{}(m.quote()));
size_t operator()(const cct::Market& m) const {
return cct::HashCombine(hash<cct::CurrencyCode>()(m.base()), hash<cct::CurrencyCode>()(m.quote()));
}
};
} // namespace std
5 changes: 5 additions & 0 deletions src/tech/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ add_unit_test(
CCT_DISABLE_SPDLOG
)

add_unit_test(
cct_hash_test
test/cct_hash_test.cpp
)

add_unit_test(
commandlineoptionsparser_test
src/commandlineoption.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/tech/include/cachedresult.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CachedResult : public CachedResultBase {
private:
using TKey = std::tuple<std::remove_cvref_t<FuncTArgs>...>;
using TValue = std::pair<ResultType, TimePoint>;
using MapType = std::unordered_map<TKey, TValue>;
using MapType = std::unordered_map<TKey, TValue, HashTuple>;

public:
template <class... TArgs>
Expand Down
61 changes: 31 additions & 30 deletions src/tech/include/cct_hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,50 @@
#include <utility>

namespace cct {
inline std::size_t HashValue64(std::size_t x) {
inline uint64_t HashValue64(uint64_t x) {
// Murmur-inspired hashing.
const std::size_t kMul = 0x9ddfea08eb382d69ULL;
std::size_t b = x * kMul;
constexpr uint64_t kMul = 0x9ddfea08eb382d69ULL;
uint64_t b = x * kMul;
b ^= (b >> 44);
b *= kMul;
b ^= (b >> 41);
b *= kMul;
return b;
}

inline std::size_t HashCombine(std::size_t h1, std::size_t h2) {
inline size_t HashCombine(size_t h1, size_t h2) {
// Taken from boost::hash_combine
h1 ^= h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2);
static_assert(sizeof(size_t) == 4 || sizeof(size_t) == 8, "HashCombine not defined for this size_t");
if constexpr (sizeof(size_t) == 4) {
h1 ^= h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2);
} else if constexpr (sizeof(size_t) == 8) {
// see https://github.com/HowardHinnant/hash_append/issues/7
h1 ^= h2 + 0x9e3779b97f4a7c15ULL + (h1 << 12) + (h1 >> 4);
}
return h1;
}

// recursive variadic template hash
template <class T>
std::size_t HashVar(const T& v) {
return std::hash<T>()(v);
}
class HashTuple {
public:
template <class Tuple>
size_t operator()(const Tuple& tuple) const {
return std::hash<size_t>()(std::apply([](const auto&... xs) { return (Component{xs}, ..., 0); }, tuple));
}

template <class T, class... Args>
std::size_t HashVar(T first, Args... args) {
return HashCombine(std::hash<T>()(first), HashVar(args...));
}
private:
template <class T>
struct Component {
const T& value;

} // namespace cct
#if defined(__clang__)
// Explicit constructor for this aggregate as clang does not implement CTAD yet (as of December 2021)
// More information here:
// https://stackoverflow.com/questions/70260994/automatic-template-deduction-c20-with-aggregate-type
explicit Component(const T& v) : value(v) {}
#endif

namespace std {
template <class... TT>
struct hash<std::tuple<TT...>> {
size_t operator()(const std::tuple<TT...>& tup) const {
auto lazyHasher = [](size_t h, auto&&... values) {
auto lazyCombiner = [&h](auto&& val) {
h ^= std::hash<std::decay_t<decltype(val)>>{}(val) + 0Xeeffddcc + (h << 5) + (h >> 3);
};
(void)lazyCombiner;
(lazyCombiner(std::forward<decltype(values)>(values)), ...);
return h;
};
return std::apply(lazyHasher, std::tuple_cat(std::tuple(0), tup));
}
size_t operator,(size_t n) const { return HashCombine(std::hash<T>()(value), n); }
};
};
} // namespace std

} // namespace cct
2 changes: 1 addition & 1 deletion src/tech/src/commandlineoption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ CommandLineOption::Duration CommandLineOption::ParseDuration(std::string_view du
if (endAmountPos == std::string_view::npos || startTimeUnit == std::string_view::npos) {
throw InvalidArgumentException(kInvalidTimeDurationUnitMsg);
}
std::string_view timeAmountStr(durationStr.begin(), durationStr.begin() + endAmountPos);
std::string_view timeAmountStr(durationStr.data(), endAmountPos);
int64_t timeAmount = FromString<int64_t>(timeAmountStr);
std::string_view timeUnitStr(durationStr.begin() + startTimeUnit, durationStr.end());
if (timeUnitStr == "h") {
Expand Down
30 changes: 30 additions & 0 deletions src/tech/test/cct_hash_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "cct_hash.hpp"

#include <gtest/gtest.h>

namespace cct {

TEST(SSPHashTest, HashValue) {
for (int i = 0; i < 100; ++i) {
EXPECT_NE(HashValue64(i), HashValue64(i + 1));
}
}

TEST(SSPHashTest, HashCombine) {
for (int i = 0; i < 20; ++i) {
for (int j = 500; j < 520; ++j) {
EXPECT_NE(HashCombine(i, j), HashCombine(i, j + 1));
EXPECT_NE(HashCombine(i, j), HashCombine(i + 1, j));
}
}
}

TEST(SSPHashTest, Pair) {
using T = std::pair<int64_t, uint8_t>;

EXPECT_EQ(HashTuple()(T(37, 20)), HashTuple()(T(37, 20)));
EXPECT_NE(HashTuple()(T(37, 36)), HashTuple()(T(37, 200)));
EXPECT_NE(HashTuple()(T(37, 200)), HashTuple()(T(42, 200)));
}

} // namespace cct