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>
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>
59class 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)
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
Zero overhead unique identifier.
typename base_type::value_type value_type
constexpr const value_type * data() const noexcept
Returns the human-readable representation of a hashed string.
constexpr hash_type value() const noexcept
Returns the numeric representation of a hashed string.
typename base_type::size_type size_type
static constexpr hash_type value(const value_type(&str)[N]) noexcept
Returns directly the numeric representation of a string.
static constexpr hash_type value(const_wrapper wrapper) noexcept
Returns directly the numeric representation of a string.
typename base_type::hash_type hash_type
constexpr basic_hashed_string(const value_type *str, const size_type len) noexcept
Constructs a hashed string from a string view.
constexpr basic_hashed_string(const value_type(&str)[N]) noexcept
Constructs a hashed string from an array of const characters.
constexpr size_type size() const noexcept
Returns the size a hashed string.
constexpr basic_hashed_string() noexcept
Constructs an empty hashed string.
static constexpr hash_type value(const value_type *str, const size_type len) noexcept
Returns directly the numeric representation of a string view.
constexpr basic_hashed_string(const_wrapper wrapper) noexcept
Explicit constructor on purpose to avoid constructing a hashed string directly from a const value_typ...
EnTT default namespace.
Definition dense_map.hpp:22
basic_hashed_string(const Char *str, std::size_t len) -> basic_hashed_string< Char >
Deduction guide.
basic_hashed_string< char > hashed_string
Aliases for common character types.
Definition fwd.hpp:41
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
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:29
constexpr bool operator<=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator<(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
basic_hashed_string< wchar_t > hashed_wstring
Aliases for common character types.
Definition fwd.hpp:44
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator>=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator>(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.