EnTT: src/entt/core/hashed_string.hpp Source File

EnTT 3.15.0

Loading...
Searching...
No Matches
hashed_string.hpp
1#ifndef ENTT_CORE_HASHED_STRING_HPP
2#define ENTT_CORE_HASHED_STRING_HPP
3
4#include <cstddef>
5#include <cstdint>
6#include <string_view>
7#include "fwd.hpp"
8
9namespace entt {
10
12namespace internal {
13
14template<typename = id_type>
15struct fnv_1a_params;
16
17template<>
18struct fnv_1a_params<std::uint32_t> {
19 static constexpr auto offset = 2166136261;
20 static constexpr auto prime = 16777619;
21};
22
23template<>
24struct fnv_1a_params<std::uint64_t> {
25 static constexpr auto offset = 14695981039346656037ull;
26 static constexpr auto prime = 1099511628211ull;
27};
28
29template<typename Char>
30struct basic_hashed_string {
31 using value_type = Char;
32 using size_type = std::size_t;
33 using hash_type = id_type;
34
35 const value_type *repr;
36 size_type length;
37 hash_type hash;
38};
39
40} // namespace internal
42
58template<typename Char>
59 class basic_hashed_string: internal::basic_hashed_string<Char> {
60 using base_type = internal::basic_hashed_string<Char>;
61 using params = internal::fnv_1a_params<>;
62
63 struct const_wrapper {
64 // non-explicit constructor on purpose
65 constexpr const_wrapper(const Char *str) noexcept
66 : repr{str} {}
67
68 const Char *repr;
69 };
70
71 // Fowler–Noll–Vo hash function v. 1a - the good
72 [[nodiscard]] static constexpr auto helper(const std::basic_string_view<Char> view) noexcept {
73 base_type base{view.data(), view.size(), params::offset};
74
75 for(auto &&curr: view) {
76 base.hash = (base.hash ^ static_cast<id_type>(curr)) * params::prime;
77 }
78
79 return base;
80 }
81
82public:
84 using value_type = typename base_type::value_type;
86 using size_type = typename base_type::size_type;
88 using hash_type = typename base_type::hash_type;
89
96 [[nodiscard]] static constexpr hash_type value(const value_type *str, const size_type len) noexcept {
97 return basic_hashed_string{str, len};
98 }
99
106 template<std::size_t N>
107 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays)
108 [[nodiscard]] static ENTT_CONSTEVAL hash_type value(const value_type (&str)[N]) noexcept {
109 return basic_hashed_string{str};
110 }
111
117 [[nodiscard]] static constexpr hash_type value(const_wrapper wrapper) noexcept {
118 return basic_hashed_string{wrapper};
119 }
120
122 constexpr basic_hashed_string() noexcept
123 : basic_hashed_string{nullptr, 0u} {}
124
130 constexpr basic_hashed_string(const value_type *str, const size_type len) noexcept
131 : base_type{helper({str, len})} {}
132
138 template<std::size_t N>
139 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays)
140 ENTT_CONSTEVAL basic_hashed_string(const value_type (&str)[N]) noexcept
141 : base_type{helper({static_cast<const value_type *>(str)})} {}
142
152 explicit constexpr basic_hashed_string(const_wrapper wrapper) noexcept
153 : base_type{helper({wrapper.repr})} {}
154
159 [[nodiscard]] constexpr size_type size() const noexcept {
160 return base_type::length;
161 }
162
167 [[nodiscard]] constexpr const value_type *data() const noexcept {
168 return base_type::repr;
169 }
170
175 [[nodiscard]] constexpr hash_type value() const noexcept {
176 return base_type::hash;
177 }
178
180 [[nodiscard]] constexpr operator const value_type *() const noexcept {
181 return data();
182 }
183
188 [[nodiscard]] constexpr operator hash_type() const noexcept {
189 return value();
190 }
191};
192
199template<typename Char>
200basic_hashed_string(const Char *str, std::size_t len) -> basic_hashed_string<Char>;
201
208template<typename Char, std::size_t N>
209// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays)
210 basic_hashed_string(const Char (&str)[N]) -> basic_hashed_string<Char>;
211
219template<typename Char>
220[[nodiscard]] constexpr bool operator==(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) noexcept {
221 return lhs.value() == rhs.value();
222}
223
231template<typename Char>
232[[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) noexcept {
233 return !(lhs == rhs);
234}
235
243template<typename Char>
244[[nodiscard]] constexpr bool operator<(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) noexcept {
245 return lhs.value() < rhs.value();
246}
247
256template<typename Char>
257[[nodiscard]] constexpr bool operator<=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) noexcept {
258 return !(rhs < lhs);
259}
260
269template<typename Char>
270[[nodiscard]] constexpr bool operator>(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) noexcept {
271 return rhs < lhs;
272}
273
282template<typename Char>
283[[nodiscard]] constexpr bool operator>=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) noexcept {
284 return !(lhs < rhs);
285}
286
287inline namespace literals {
288
294[[nodiscard]] ENTT_CONSTEVAL hashed_string operator""_hs(const char *str, std::size_t) noexcept {
295 return hashed_string{str};
296}
297
303[[nodiscard]] ENTT_CONSTEVAL hashed_wstring operator""_hws(const wchar_t *str, std::size_t) noexcept {
304 return hashed_wstring{str};
305}
306
307} // namespace literals
308
309} // namespace entt
310
311#endif
entt::basic_hashed_string
Zero overhead unique identifier.
Definition hashed_string.hpp:59
entt::basic_hashed_string< char >::value_type
typename base_type::value_type value_type
Definition hashed_string.hpp:84
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:167
entt::basic_hashed_string::value
constexpr hash_type value() const noexcept
Returns the numeric representation of a hashed string.
Definition hashed_string.hpp:175
entt::basic_hashed_string< char >::size_type
typename base_type::size_type size_type
Definition hashed_string.hpp:86
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:108
entt::basic_hashed_string::value
static constexpr hash_type value(const_wrapper wrapper) noexcept
Returns directly the numeric representation of a string.
Definition hashed_string.hpp:117
entt::basic_hashed_string< char >::hash_type
typename base_type::hash_type hash_type
Definition hashed_string.hpp:88
entt::basic_hashed_string::basic_hashed_string
constexpr basic_hashed_string(const value_type *str, const size_type len) noexcept
Constructs a hashed string from a string view.
Definition hashed_string.hpp:130
entt::basic_hashed_string::basic_hashed_string
constexpr basic_hashed_string(const value_type(&str)[N]) noexcept
Constructs a hashed string from an array of const characters.
Definition hashed_string.hpp:140
entt::basic_hashed_string::size
constexpr size_type size() const noexcept
Returns the size a hashed string.
Definition hashed_string.hpp:159
entt::basic_hashed_string::basic_hashed_string
constexpr basic_hashed_string() noexcept
Constructs an empty hashed string.
Definition hashed_string.hpp:122
entt::basic_hashed_string::value
static constexpr hash_type value(const value_type *str, const size_type len) noexcept
Returns directly the numeric representation of a string view.
Definition hashed_string.hpp:96
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:152
entt
EnTT default namespace.
Definition dense_map.hpp:22
entt::basic_hashed_string
basic_hashed_string(const Char *str, std::size_t len) -> basic_hashed_string< Char >
Deduction guide.
entt::hashed_string
basic_hashed_string< char > hashed_string
Aliases for common character types.
Definition fwd.hpp:41
entt::view
basic_view< type_list_transform_t< Get, storage_for >, type_list_transform_t< Exclude, storage_for > > view
Alias declaration for the most common use case.
Definition fwd.hpp:277
entt::id_type
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:29
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:257
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:244
entt::hashed_wstring
basic_hashed_string< wchar_t > hashed_wstring
Aliases for common character types.
Definition fwd.hpp:44
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:232
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:283
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:270
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:220