EnTT  3.3.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 <cstdint>
7 #include "../config/config.h"
8 
9 
10 namespace entt {
11 
12 
19 namespace internal {
20 
21 
22 template<typename>
23 struct fnv1a_traits;
24 
25 
26 template<>
27 struct fnv1a_traits<std::uint32_t> {
28  using type = std::uint32_t;
29  static constexpr std::uint32_t offset = 2166136261;
30  static constexpr std::uint32_t prime = 16777619;
31 };
32 
33 
34 template<>
35 struct fnv1a_traits<std::uint64_t> {
36  using type = std::uint64_t;
37  static constexpr std::uint64_t offset = 14695981039346656037ull;
38  static constexpr std::uint64_t prime = 1099511628211ull;
39 };
40 
41 
42 }
43 
44 
62 template<typename Char>
64  using traits_type = internal::fnv1a_traits<ENTT_ID_TYPE>;
65 
66  struct const_wrapper {
67  // non-explicit constructor on purpose
68  constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
69  const Char *str;
70  };
71 
72  // Fowler–Noll–Vo hash function v. 1a - the good
73  static constexpr ENTT_ID_TYPE helper(const Char *curr) ENTT_NOEXCEPT {
74  auto value = traits_type::offset;
75 
76  while(*curr != 0) {
77  value = (value ^ static_cast<traits_type::type>(*(curr++))) * traits_type::prime;
78  }
79 
80  return value;
81  }
82 
83 public:
85  using value_type = Char;
87  using hash_type = ENTT_ID_TYPE;
88 
104  template<std::size_t N>
105  static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
106  return helper(str);
107  }
108 
114  static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
115  return helper(wrapper.str);
116  }
117 
124  static hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
125  ENTT_ID_TYPE partial{traits_type::offset};
126  while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
127  return partial;
128  }
129 
131  constexpr basic_hashed_string() ENTT_NOEXCEPT
132  : str{nullptr}, hash{}
133  {}
134 
149  template<std::size_t N>
150  constexpr basic_hashed_string(const value_type (&curr)[N]) ENTT_NOEXCEPT
151  : str{curr}, hash{helper(curr)}
152  {}
153 
159  explicit constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
160  : str{wrapper.str}, hash{helper(wrapper.str)}
161  {}
162 
167  constexpr const value_type * data() const ENTT_NOEXCEPT {
168  return str;
169  }
170 
175  constexpr hash_type value() const ENTT_NOEXCEPT {
176  return hash;
177  }
178 
180  constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
181 
186  constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
187 
193  constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
194  return hash == other.hash;
195  }
196 
197 private:
198  const value_type *str;
199  hash_type hash;
200 };
201 
202 
213 template<typename Char, std::size_t N>
214 basic_hashed_string(const Char (&str)[N]) ENTT_NOEXCEPT
215 -> basic_hashed_string<Char>;
216 
217 
225 template<typename Char>
226 constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
227  return !(lhs == rhs);
228 }
229 
230 
233 
234 
237 
238 
239 }
240 
241 
247 constexpr entt::hashed_string operator"" ENTT_HS_SUFFIX(const char *str, std::size_t) ENTT_NOEXCEPT {
248  return entt::hashed_string{str};
249 }
250 
251 
257 constexpr entt::hashed_wstring operator"" ENTT_HWS_SUFFIX(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
258  return entt::hashed_wstring{str};
259 }
260 
261 
262 #endif
entt::basic_hashed_string::value_type
Char value_type
Character type.
Definition: hashed_string.hpp:85
entt::basic_hashed_string::value
static constexpr hash_type value(const value_type(&str)[N]) ENTT_NOEXCEPT
Returns directly the numeric representation of a string.
Definition: hashed_string.hpp:105
entt::basic_hashed_string::operator==
constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT
Compares two hashed strings.
Definition: hashed_string.hpp:193
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:150
entt::basic_hashed_string::basic_hashed_string
constexpr basic_hashed_string() ENTT_NOEXCEPT
Constructs an empty hashed string.
Definition: hashed_string.hpp:131
entt::basic_hashed_string
Zero overhead unique identifier.
Definition: hashed_string.hpp:63
entt::basic_hashed_string::hash_type
ENTT_ID_TYPE hash_type
Unsigned integer type.
Definition: hashed_string.hpp:87
entt
EnTT default namespace.
Definition: algorithm.hpp:13
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:167
entt::basic_hashed_string::value
static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT
Returns directly the numeric representation of a string.
Definition: hashed_string.hpp:114
entt::basic_hashed_string::value
constexpr hash_type value() const ENTT_NOEXCEPT
Returns the numeric representation of a hashed string.
Definition: hashed_string.hpp:175
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:226
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:159
entt::basic_hashed_string
basic_hashed_string(const Char(&str)[N]) ENTT_NOEXCEPT -> basic_hashed_string< Char >
Deduction guide.
entt::basic_hashed_string::value
static hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT
Returns directly the numeric representation of a string view.
Definition: hashed_string.hpp:124