cpp: Add std::hash specializations for basic types

This commit is contained in:
Paweł Bylica 2019-07-22 13:40:14 +02:00
parent 0bde57ec09
commit 635aebeb66
No known key found for this signature in database
GPG Key ID: 7A0C037434FE77EF
2 changed files with 75 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include <evmc/evmc.h> #include <evmc/evmc.h>
#include <evmc/helpers.h> #include <evmc/helpers.h>
#include <functional>
#include <initializer_list> #include <initializer_list>
#include <utility> #include <utility>
@ -52,6 +53,18 @@ constexpr inline uint32_t load32be(const uint8_t* bytes) noexcept
uint32_t{bytes[3]}; uint32_t{bytes[3]};
} }
namespace fnv
{
constexpr auto prime = 0x100000001b3; ///< The 64-bit FNV prime number.
constexpr auto offset_basis = 0xcbf29ce484222325; ///< The 64-bit FNV offset basis.
/// The hashing transformation for 64-bit inputs based on the FNV-1a formula.
constexpr inline uint64_t fnv1a_by64(uint64_t h, uint64_t x) noexcept
{
return (h ^ x) * prime;
}
} // namespace fnv
/// The "equal" comparison operator for the evmc::address type. /// The "equal" comparison operator for the evmc::address type.
constexpr bool operator==(const address& a, const address& b) noexcept constexpr bool operator==(const address& a, const address& b) noexcept
@ -479,3 +492,39 @@ constexpr evmc_host_interface interface{
inline Host::Host() noexcept : evmc_context{&internal::interface} {} inline Host::Host() noexcept : evmc_context{&internal::interface} {}
} // namespace evmc } // namespace evmc
namespace std
{
/// Hash operator template specialization for evmc::address. Needed for unordered containers.
template <>
struct hash<evmc::address>
{
/// Hash operator using FNV1a-based folding.
constexpr size_t operator()(const evmc::address& s) const noexcept
{
using namespace evmc;
using namespace fnv;
return static_cast<size_t>(fnv1a_by64(
fnv1a_by64(fnv1a_by64(fnv::offset_basis, load64be(&s.bytes[0])), load64be(&s.bytes[8])),
load32be(&s.bytes[16])));
}
};
/// Hash operator template specialization for evmc::bytes32. Needed for unordered containers.
template <>
struct hash<evmc::bytes32>
{
/// Hash operator using FNV1a-based folding.
constexpr size_t operator()(const evmc::bytes32& s) const noexcept
{
using namespace evmc;
using namespace fnv;
return static_cast<size_t>(
fnv1a_by64(fnv1a_by64(fnv1a_by64(fnv1a_by64(fnv::offset_basis, load64be(&s.bytes[0])),
load64be(&s.bytes[8])),
load64be(&s.bytes[16])),
load64be(&s.bytes[24])));
}
};
} // namespace std

View File

@ -16,6 +16,7 @@
#include <cstring> #include <cstring>
TEST(cpp, address) TEST(cpp, address)
{ {
evmc::address a; evmc::address a;
@ -46,6 +47,31 @@ TEST(cpp, bytes32)
EXPECT_TRUE(std::equal(std::begin(b.bytes), std::end(b.bytes), std::begin(other.bytes))); EXPECT_TRUE(std::equal(std::begin(b.bytes), std::end(b.bytes), std::begin(other.bytes)));
} }
TEST(cpp, std_hash)
{
#pragma warning(push)
#pragma warning(disable : 4307 /* integral constant overflow */)
#pragma warning(disable : 4309 /* 'static_cast': truncation of constant value */)
#if !defined(_MSC_VER) || (_MSC_VER >= 1910 /* Only for Visual Studio 2017+ */)
static_assert(std::hash<evmc::address>{}({}) == static_cast<size_t>(0xd94d12186c0f2fb7), "");
static_assert(std::hash<evmc::bytes32>{}({}) == static_cast<size_t>(0x4d25767f9dce13f5), "");
#endif
EXPECT_EQ(std::hash<evmc::address>{}({}), static_cast<size_t>(0xd94d12186c0f2fb7));
EXPECT_EQ(std::hash<evmc::bytes32>{}({}), static_cast<size_t>(0x4d25767f9dce13f5));
auto ea = evmc::address{};
std::fill_n(ea.bytes, sizeof(ea), uint8_t{0xee});
EXPECT_EQ(std::hash<evmc::address>{}(ea), static_cast<size_t>(0x41dc0178e01b7cd9));
auto eb = evmc::bytes32{};
std::fill_n(eb.bytes, sizeof(eb), uint8_t{0xee});
EXPECT_EQ(std::hash<evmc::bytes32>{}(eb), static_cast<size_t>(0xbb14e5c56b477375));
#pragma warning(pop)
}
TEST(cpp, address_comparison) TEST(cpp, address_comparison)
{ {
const auto zero = evmc::address{}; const auto zero = evmc::address{};