EnTT  3.2.2
hashed_string.hpp
1 #ifndef ENTT_CORE_HASHED_STRING_HPP
2 #define ENTT_CORE_HASHED_STRING_HPP
3 
4 
5 #include <cstddef>
6 #include "../config/config.h"
7 
8 
9 namespace entt {
10 
11 
18 namespace internal {
19 
20 
21 template<typename>
22 struct fnv1a_traits;
23 
24 
25 template<>
26 struct fnv1a_traits<std::uint32_t> {
27  static constexpr std::uint32_t offset = 2166136261;
28  static constexpr std::uint32_t prime = 16777619;
29 };
30 
31 
32 template<>
33 struct fnv1a_traits<std::uint64_t> {
34  static constexpr std::uint64_t offset = 14695981039346656037ull;
35  static constexpr std::uint64_t prime = 1099511628211ull;
36 };
37 
38 
39 }
40 
41 
59 template<typename Char>
61  using traits_type = internal::fnv1a_traits<ENTT_ID_TYPE>;
62 
63  struct const_wrapper {
64  // non-explicit constructor on purpose
65  constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
66  const Char *str;
67  };
68 
69  // Fowler–Noll–Vo hash function v. 1a - the good
70  static constexpr ENTT_ID_TYPE helper(ENTT_ID_TYPE partial, const Char *curr) ENTT_NOEXCEPT {
71  return curr[0] == 0 ? partial : helper((partial^curr[0])*traits_type::prime, curr+1);
72  }
73 
74 public:
76  using value_type = Char;
78  using hash_type = ENTT_ID_TYPE;
79 
95  template<std::size_t N>
96  static constexpr hash_type to_value(const value_type (&str)[N]) ENTT_NOEXCEPT {
97  return helper(traits_type::offset, str);
98  }
99 
105  static hash_type to_value(const_wrapper wrapper) ENTT_NOEXCEPT {
106  return helper(traits_type::offset, wrapper.str);
107  }
108 
115  static hash_type to_value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
116  ENTT_ID_TYPE partial{traits_type::offset};
117  while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
118  return partial;
119  }
120 
122  constexpr basic_hashed_string() ENTT_NOEXCEPT
123  : str{nullptr}, hash{}
124  {}
125 
140  template<std::size_t N>
141  constexpr basic_hashed_string(const value_type (&curr)[N]) ENTT_NOEXCEPT
142  : str{curr}, hash{helper(traits_type::offset, curr)}
143  {}
144 
150  explicit constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
151  : str{wrapper.str}, hash{helper(traits_type::offset, wrapper.str)}
152  {}
153 
158  constexpr const value_type * data() const ENTT_NOEXCEPT {
159  return str;
160  }
161 
166  constexpr hash_type value() const ENTT_NOEXCEPT {
167  return hash;
168  }
169 
174  constexpr operator const value_type *() const ENTT_NOEXCEPT { return str; }
175 
177  constexpr operator hash_type() const ENTT_NOEXCEPT { return hash; }
178 
184  constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
185  return hash == other.hash;
186  }
187 
188 private:
189  const value_type *str;
190  hash_type hash;
191 };
192 
193 
204 template<typename Char, std::size_t N>
205 basic_hashed_string(const Char (&str)[N]) ENTT_NOEXCEPT
206 -> basic_hashed_string<Char>;
207 
208 
216 template<typename Char>
217 constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
218  return !(lhs == rhs);
219 }
220 
221 
224 
225 
228 
229 
230 }
231 
232 
238 constexpr entt::hashed_string operator"" ENTT_HS_SUFFIX(const char *str, std::size_t) ENTT_NOEXCEPT {
239  return entt::hashed_string{str};
240 }
241 
242 
248 constexpr entt::hashed_wstring operator"" ENTT_HWS_SUFFIX(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
249  return entt::hashed_wstring{str};
250 }
251 
252 
253 #endif // ENTT_CORE_HASHED_STRING_HPP
entt::basic_hashed_string::value_type
Char value_type
Character type.
Definition: hashed_string.hpp:76
entt::basic_hashed_string::to_value
static constexpr hash_type to_value(const value_type(&str)[N]) ENTT_NOEXCEPT
Returns directly the numeric representation of a string.
Definition: hashed_string.hpp:96
entt::basic_hashed_string::operator==
constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT
Compares two hashed strings.
Definition: hashed_string.hpp:184
entt::basic_hashed_string::basic_hashed_string
constexpr basic_hashed_string(const value_type(&curr)[N]) ENTT_NOEXCEPT
Constructs a hashed string from an array of const characters.
Definition: hashed_string.hpp:141
entt::basic_hashed_string::to_value
static hash_type to_value(const_wrapper wrapper) ENTT_NOEXCEPT
Returns directly the numeric representation of a string.
Definition: hashed_string.hpp:105
entt::basic_hashed_string::basic_hashed_string
constexpr basic_hashed_string() ENTT_NOEXCEPT
Constructs an empty hashed string.
Definition: hashed_string.hpp:122
entt::basic_hashed_string
Zero overhead unique identifier.
Definition: hashed_string.hpp:60
entt::basic_hashed_string::hash_type
ENTT_ID_TYPE hash_type
Unsigned integer type.
Definition: hashed_string.hpp:78
entt
EnTT default namespace.
Definition: algorithm.hpp:12
entt::basic_hashed_string::to_value
static hash_type to_value(const value_type *str, std::size_t size) ENTT_NOEXCEPT
Returns directly the numeric representation of a string view.
Definition: hashed_string.hpp:115
entt::basic_hashed_string::data
constexpr const value_type * data() const ENTT_NOEXCEPT
Returns the human-readable representation of a hashed string.
Definition: hashed_string.hpp:158
entt::basic_hashed_string::value
constexpr hash_type value() const ENTT_NOEXCEPT
Returns the numeric representation of a hashed string.
Definition: hashed_string.hpp:166
entt::operator!=
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) ENTT_NOEXCEPT
Compares two hashed strings.
Definition: hashed_string.hpp:217
entt::basic_hashed_string::basic_hashed_string
constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
Explicit constructor on purpose to avoid constructing a hashed string directly from a const value_typ...
Definition: hashed_string.hpp:150
entt::basic_hashed_string
basic_hashed_string(const Char(&str)[N]) ENTT_NOEXCEPT -> basic_hashed_string< Char >
Deduction guide.