EnTT  3.5.0
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 #include "fwd.hpp"
9 
10 
11 namespace entt {
12 
13 
20 namespace internal {
21 
22 
23 template<typename>
24 struct fnv1a_traits;
25 
26 
27 template<>
28 struct fnv1a_traits<std::uint32_t> {
29  using type = std::uint32_t;
30  static constexpr std::uint32_t offset = 2166136261;
31  static constexpr std::uint32_t prime = 16777619;
32 };
33 
34 
35 template<>
36 struct fnv1a_traits<std::uint64_t> {
37  using type = std::uint64_t;
38  static constexpr std::uint64_t offset = 14695981039346656037ull;
39  static constexpr std::uint64_t prime = 1099511628211ull;
40 };
41 
42 
43 }
44 
45 
63 template<typename Char>
65  using traits_type = internal::fnv1a_traits<id_type>;
66 
67  struct const_wrapper {
68  // non-explicit constructor on purpose
69  constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
70  const Char *str;
71  };
72 
73  // Fowler–Noll–Vo hash function v. 1a - the good
74  [[nodiscard]] static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT {
75  auto value = traits_type::offset;
76 
77  while(*curr != 0) {
78  value = (value ^ static_cast<traits_type::type>(*(curr++))) * traits_type::prime;
79  }
80 
81  return value;
82  }
83 
84 public:
86  using value_type = Char;
88  using hash_type = id_type;
89 
105  template<std::size_t N>
106  [[nodiscard]] static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
107  return helper(str);
108  }
109 
115  [[nodiscard]] static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
116  return helper(wrapper.str);
117  }
118 
125  [[nodiscard]] static hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
126  id_type partial{traits_type::offset};
127  while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
128  return partial;
129  }
130 
132  constexpr basic_hashed_string() ENTT_NOEXCEPT
133  : str{nullptr}, hash{}
134  {}
135 
150  template<std::size_t N>
151  constexpr basic_hashed_string(const value_type (&curr)[N]) ENTT_NOEXCEPT
152  : str{curr}, hash{helper(curr)}
153  {}
154 
160  explicit constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
161  : str{wrapper.str}, hash{helper(wrapper.str)}
162  {}
163 
168  [[nodiscard]] constexpr const value_type * data() const ENTT_NOEXCEPT {
169  return str;
170  }
171 
176  [[nodiscard]] constexpr hash_type value() const ENTT_NOEXCEPT {
177  return hash;
178  }
179 
181  [[nodiscard]] constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
182 
187  [[nodiscard]] constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
188 
194  [[nodiscard]] constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
195  return hash == other.hash;
196  }
197 
198 private:
199  const value_type *str;
200  hash_type hash;
201 };
202 
203 
214 template<typename Char, std::size_t N>
215 basic_hashed_string(const Char (&str)[N]) ENTT_NOEXCEPT
217 
218 
226 template<typename Char>
227 [[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
228  return !(lhs == rhs);
229 }
230 
231 
234 
235 
238 
239 
240 }
241 
242 
248 [[nodiscard]] constexpr entt::hashed_string operator"" ENTT_HS_SUFFIX(const char *str, std::size_t) ENTT_NOEXCEPT {
249  return entt::hashed_string{str};
250 }
251 
252 
258 [[nodiscard]] constexpr entt::hashed_wstring operator"" ENTT_HWS_SUFFIX(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
259  return entt::hashed_wstring{str};
260 }
261 
262 
263 #endif
entt::basic_hashed_string::value_type
Char value_type
Character type.
Definition: hashed_string.hpp:86
entt::basic_hashed_string::basic_hashed_string
constexpr basic_hashed_string() noexcept
Constructs an empty hashed string.
Definition: hashed_string.hpp:132
entt::basic_hashed_string::value
static hash_type value(const value_type *str, std::size_t size) noexcept
Returns directly the numeric representation of a string view.
Definition: hashed_string.hpp:125
entt::basic_hashed_string::operator==
constexpr bool operator==(const basic_hashed_string &other) const noexcept
Compares two hashed strings.
Definition: hashed_string.hpp:194
entt::basic_hashed_string::value
static hash_type value(const_wrapper wrapper) noexcept
Returns directly the numeric representation of a string.
Definition: hashed_string.hpp:115
entt::id_type
std::uint32_t id_type
Alias declaration for type identifiers.
Definition: fwd.hpp:12
entt::basic_hashed_string
Zero overhead unique identifier.
Definition: hashed_string.hpp:64
entt
EnTT default namespace.
Definition: algorithm.hpp:13
entt::basic_hashed_string::data
constexpr const value_type * data() const noexcept
Returns the human-readable representation of a hashed string.
Definition: hashed_string.hpp:168
entt::basic_hashed_string::value
constexpr hash_type value() const noexcept
Returns the numeric representation of a hashed string.
Definition: hashed_string.hpp:176
entt::basic_hashed_string::basic_hashed_string
constexpr basic_hashed_string(const value_type(&curr)[N]) noexcept
Constructs a hashed string from an array of const characters.
Definition: hashed_string.hpp:151
entt::operator!=
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition: hashed_string.hpp:227
entt::basic_hashed_string::basic_hashed_string
constexpr basic_hashed_string(const_wrapper wrapper) noexcept
Explicit constructor on purpose to avoid constructing a hashed string directly from a const value_typ...
Definition: hashed_string.hpp:160
entt::basic_hashed_string
basic_hashed_string(const Char(&str)[N]) noexcept -> basic_hashed_string< Char >
Deduction guide.
entt::basic_hashed_string::hash_type
id_type hash_type
Unsigned integer type.
Definition: hashed_string.hpp:88
entt::basic_hashed_string::value
static constexpr hash_type value(const value_type(&str)[N]) noexcept
Returns directly the numeric representation of a string.
Definition: hashed_string.hpp:106