entt  2.4.1
hashed_string.hpp
1 #ifndef ENTT_CORE_HASHED_STRING_HPP
2 #define ENTT_CORE_HASHED_STRING_HPP
3 
4 
5 #include <cstdint>
6 
7 
8 namespace entt {
9 
10 
20 class HashedString final {
21  struct ConstCharWrapper final {
22  // non-explicit constructor on purpose
23  constexpr ConstCharWrapper(const char *str) noexcept: str{str} {}
24  const char *str;
25  };
26 
27  static constexpr std::uint64_t offset = 14695981039346656037ull;
28  static constexpr std::uint64_t prime = 1099511628211ull;
29 
30  // Fowler–Noll–Vo hash function v. 1a - the good
31  static constexpr std::uint64_t helper(std::uint64_t partial, const char *str) noexcept {
32  return str[0] == 0 ? partial : helper((partial^str[0])*prime, str+1);
33  }
34 
35 public:
37  using hash_type = std::uint64_t;
38 
53  template <std::size_t N>
54  constexpr HashedString(const char (&str)[N]) noexcept
55  : hash{helper(offset, str)}, str{str}
56  {}
57 
64  explicit constexpr HashedString(ConstCharWrapper wrapper) noexcept
65  : hash{helper(offset, wrapper.str)}, str{wrapper.str}
66  {}
67 
72  constexpr operator const char *() const noexcept { return str; }
73 
78  constexpr operator hash_type() const noexcept { return hash; }
79 
85  constexpr bool operator==(const HashedString &other) const noexcept {
86  return hash == other.hash;
87  }
88 
89 private:
90  const hash_type hash;
91  const char *str;
92 };
93 
94 
101 constexpr bool operator!=(const HashedString &lhs, const HashedString &rhs) noexcept {
102  return !(lhs == rhs);
103 }
104 
105 
106 }
107 
108 
109 #endif // ENTT_CORE_HASHED_STRING_HPP
constexpr HashedString(ConstCharWrapper wrapper) noexcept
Explicit constructor on purpose to avoid constructing a hashed string directly from a const char *...
EnTT default namespace.
Definition: family.hpp:9
Zero overhead resource identifier.
constexpr HashedString(const char(&str)[N]) noexcept
Constructs a hashed string from an array of const chars.
std::uint64_t hash_type
Unsigned integer type.
constexpr bool operator!=(const HashedString &lhs, const HashedString &rhs) noexcept
Compares two hashed strings.
constexpr bool operator==(const HashedString &other) const noexcept
Compares two hashed strings.