EnTT  3.0.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 "../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 
58  using traits_type = internal::fnv1a_traits<ENTT_ID_TYPE>;
59 
60  struct const_wrapper {
61  // non-explicit constructor on purpose
62  constexpr const_wrapper(const char *curr) ENTT_NOEXCEPT: str{curr} {}
63  const char *str;
64  };
65 
66  // Fowler–Noll–Vo hash function v. 1a - the good
67  inline static constexpr ENTT_ID_TYPE helper(ENTT_ID_TYPE partial, const char *curr) ENTT_NOEXCEPT {
68  return curr[0] == 0 ? partial : helper((partial^curr[0])*traits_type::prime, curr+1);
69  }
70 
71 public:
73  using hash_type = ENTT_ID_TYPE;
74 
90  template<std::size_t N>
91  inline static constexpr hash_type to_value(const char (&str)[N]) ENTT_NOEXCEPT {
92  return helper(traits_type::offset, str);
93  }
94 
100  inline static hash_type to_value(const_wrapper wrapper) ENTT_NOEXCEPT {
101  return helper(traits_type::offset, wrapper.str);
102  }
103 
110  inline static hash_type to_value(const char *str, std::size_t size) ENTT_NOEXCEPT {
111  ENTT_ID_TYPE partial{traits_type::offset};
112  while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
113  return partial;
114  }
115 
117  constexpr hashed_string() ENTT_NOEXCEPT
118  : str{nullptr}, hash{}
119  {}
120 
135  template<std::size_t N>
136  constexpr hashed_string(const char (&curr)[N]) ENTT_NOEXCEPT
137  : str{curr}, hash{helper(traits_type::offset, curr)}
138  {}
139 
145  explicit constexpr hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
146  : str{wrapper.str}, hash{helper(traits_type::offset, wrapper.str)}
147  {}
148 
153  constexpr const char * data() const ENTT_NOEXCEPT {
154  return str;
155  }
156 
161  constexpr hash_type value() const ENTT_NOEXCEPT {
162  return hash;
163  }
164 
169  constexpr operator const char *() const ENTT_NOEXCEPT { return str; }
170 
172  constexpr operator hash_type() const ENTT_NOEXCEPT { return hash; }
173 
179  constexpr bool operator==(const hashed_string &other) const ENTT_NOEXCEPT {
180  return hash == other.hash;
181  }
182 
183 private:
184  const char *str;
185  hash_type hash;
186 };
187 
188 
195 constexpr bool operator!=(const hashed_string &lhs, const hashed_string &rhs) ENTT_NOEXCEPT {
196  return !(lhs == rhs);
197 }
198 
199 
200 }
201 
202 
208 constexpr entt::hashed_string operator"" ENTT_HS_SUFFIX(const char *str, std::size_t) ENTT_NOEXCEPT {
209  return entt::hashed_string{str};
210 }
211 
212 
213 #endif // ENTT_CORE_HASHED_STRING_HPP
static hash_type to_value(const char *str, std::size_t size) ENTT_NOEXCEPT
Returns directly the numeric representation of a string view.
EnTT default namespace.
Definition: algorithm.hpp:10
Zero overhead unique identifier.
constexpr hashed_string() ENTT_NOEXCEPT
Constructs an empty hashed string.
static constexpr hash_type to_value(const char(&str)[N]) ENTT_NOEXCEPT
Returns directly the numeric representation of a string.
constexpr hashed_string(const char(&curr)[N]) ENTT_NOEXCEPT
Constructs a hashed string from an array of const chars.
constexpr bool operator==(const hashed_string &other) const ENTT_NOEXCEPT
Compares two hashed strings.
static hash_type to_value(const_wrapper wrapper) ENTT_NOEXCEPT
Returns directly the numeric representation of a string.
constexpr hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
Explicit constructor on purpose to avoid constructing a hashed string directly from a const char *...
constexpr hash_type value() const ENTT_NOEXCEPT
Returns the numeric representation of a hashed string.
constexpr const char * data() const ENTT_NOEXCEPT
Returns the human-readable representation of a hashed string.
ENTT_ID_TYPE hash_type
Unsigned integer type.
constexpr bool operator!=(const hashed_string &lhs, const hashed_string &rhs) ENTT_NOEXCEPT
Compares two hashed strings.