Skip to content

Commit

Permalink
Replace const char* with std::string for NH::HashString Value a…
Browse files Browse the repository at this point in the history
…s a temporary measure to keep its lifetime under control before larger refactoring is possible
  • Loading branch information
piotrmacha committed Jul 31, 2024
1 parent 83c1eb3 commit ef00e9f
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/NH/HashString.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,40 @@ namespace NH
{
return (str[0] == '\0') ? value : hash_64_fnv1a_const(&str[1], (value ^ uint64_t(str[0])) * prime_64_const);
}
}
}// namespace FNV1a

// HashString is a compile-time 64-bit FNV1a hash of a string literal.
// It can be used as a key in hash maps to avoid runtime string comparisons.
class HashString
{
uint64_t Id;
const char* Value;
std::string Value;

public:
constexpr HashString() noexcept : Id(0), Value(nullptr) {}
constexpr explicit(false) HashString(const char* str) noexcept: Id(FNV1a::hash_64_fnv1a_const(str)), Value(str) {} // NOLINT(google-explicit-constructor)
explicit(false) HashString(const String& str) noexcept: Id(FNV1a::hash_64_fnv1a_const(str)), Value(str.ToChar()) {} // NOLINT(google-explicit-constructor)
constexpr HashString() noexcept : Id(0) {}
constexpr explicit(false) HashString(const char* str) noexcept : Id(FNV1a::hash_64_fnv1a_const(str)), Value(str) {}// NOLINT(google-explicit-constructor)
explicit(false) HashString(const String& str) noexcept : Id(FNV1a::hash_64_fnv1a_const(str)), Value(str.ToChar()) {} // NOLINT(google-explicit-constructor)

[[nodiscard]] constexpr uint64_t GetHash() const { return Id; }

[[nodiscard]] constexpr const char* GetValue() const { return Value; }
[[nodiscard]] constexpr const char* GetValue() const { return Value.c_str(); }

constexpr explicit(false) operator uint64_t() const noexcept { return Id; } // NOLINT(google-explicit-constructor)
constexpr explicit(false) operator const char*() const noexcept { return Value; } // NOLINT(google-explicit-constructor)
explicit(false) operator String() const noexcept { return { Value }; } // NOLINT(google-explicit-constructor)
constexpr explicit(false) operator uint64_t() const noexcept { return Id; } // NOLINT(google-explicit-constructor)
constexpr explicit(false) operator const char*() const noexcept { return Value.c_str(); }// NOLINT(google-explicit-constructor)
explicit(false) operator String() const noexcept { return {Value.c_str()}; } // NOLINT(google-explicit-constructor)

constexpr bool operator==(const HashString& other) const noexcept { return Id == other.Id; }
};

constexpr HashString operator "" _hs(const char* str, size_t) noexcept
constexpr HashString operator"" _hs(const char* str, size_t) noexcept
{
return { str };
return {str};
}
}
}// namespace NH

template<>
struct std::hash<NH::HashString>
{
std::size_t operator()(const NH::HashString& k) const
struct std::hash<NH::HashString> {
std::size_t operator()(const NH::HashString& k) const noexcept
{
return std::hash<uint64_t>()(static_cast<uint64_t>(k));
}
Expand Down

0 comments on commit ef00e9f

Please sign in to comment.