EnTT 4.0.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 "../stl/cstddef.hpp"
5#include "../stl/cstdint.hpp"
6#include "fwd.hpp"
7
8namespace entt {
9
11namespace internal {
12
13template<typename = id_type>
14struct fnv_1a_params;
15
16template<>
17struct fnv_1a_params<stl::uint32_t> {
18 static constexpr auto offset = 2166136261;
19 static constexpr auto prime = 16777619;
20};
21
22template<>
23struct fnv_1a_params<stl::uint64_t> {
24 static constexpr auto offset = 14695981039346656037ull;
25 static constexpr auto prime = 1099511628211ull;
26};
27
28template<typename Char>
30 using value_type = Char;
31 using size_type = stl::size_t;
32 using hash_type = id_type;
33
34 const value_type *repr{};
35 hash_type hash{fnv_1a_params<>::offset};
36 size_type length{};
37};
38
39} // namespace internal
41
57template<typename Char>
58class basic_hashed_string: internal::basic_hashed_string<Char> {
59 using base_type = internal::basic_hashed_string<Char>;
60 using params = internal::fnv_1a_params<>;
61
62 struct const_wrapper {
63 // non-explicit constructor on purpose
64 constexpr const_wrapper(const base_type::value_type *str) noexcept
65 : repr{str} {}
66
67 const base_type::value_type *repr;
68 };
69
70public:
72 using value_type = base_type::value_type;
74 using size_type = base_type::size_type;
76 using hash_type = base_type::hash_type;
77
84 [[nodiscard]] static constexpr hash_type value(const value_type *str, const size_type len) noexcept {
85 return basic_hashed_string{str, len};
86 }
87
94 template<stl::size_t N>
95 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays)
96 [[nodiscard]] static ENTT_CONSTEVAL hash_type value(const value_type (&str)[N]) noexcept {
97 return basic_hashed_string{str};
98 }
99
105 [[nodiscard]] static constexpr hash_type value(const_wrapper wrapper) noexcept {
106 return basic_hashed_string{wrapper};
107 }
108
110 constexpr basic_hashed_string() noexcept
111 : basic_hashed_string{nullptr, 0u} {}
112
118 constexpr basic_hashed_string(const value_type *str, const size_type len) noexcept
119 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
120 : base_type{str} {
121 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
122 for(; base_type::length < len; ++base_type::length) {
123 base_type::hash = (base_type::hash ^ static_cast<id_type>(str[base_type::length])) * params::prime;
124 }
125 // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
126 }
127
133 template<stl::size_t N>
134 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays)
135 ENTT_CONSTEVAL basic_hashed_string(const value_type (&str)[N]) noexcept
136 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
137 : base_type{str} {
138 for(; str[base_type::length]; ++base_type::length) {
139 base_type::hash = (base_type::hash ^ static_cast<id_type>(str[base_type::length])) * params::prime;
140 }
141 }
142
152 explicit constexpr basic_hashed_string(const_wrapper wrapper) noexcept
153 : base_type{wrapper.repr} {
154 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
155 for(; wrapper.repr[base_type::length]; ++base_type::length) {
156 base_type::hash = (base_type::hash ^ static_cast<id_type>(wrapper.repr[base_type::length])) * params::prime;
157 }
158 // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
159 }
160
165 [[nodiscard]] constexpr size_type size() const noexcept {
166 return base_type::length;
167 }
168
173 [[nodiscard]] constexpr const value_type *data() const noexcept {
174 return base_type::repr;
175 }
176
181 [[nodiscard]] constexpr hash_type value() const noexcept {
182 return base_type::hash;
183 }
184
186 [[nodiscard]] explicit constexpr operator const value_type *() const noexcept {
187 return data();
188 }
189
194 [[nodiscard]] constexpr operator hash_type() const noexcept {
195 return value();
196 }
197
203 [[nodiscard]] constexpr bool operator==(const basic_hashed_string &other) const noexcept {
204 return value() == other.value();
205 }
206
212 [[nodiscard]] constexpr auto operator<=>(const basic_hashed_string &other) const noexcept {
213 return value() <=> other.value();
214 }
215};
216
223template<typename Char>
224basic_hashed_string(const Char *str, stl::size_t len) -> basic_hashed_string<Char>;
225
232template<typename Char, stl::size_t N>
233// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays)
235
236inline namespace literals {
237
243[[nodiscard]] ENTT_CONSTEVAL hashed_string operator""_hs(const char *str, stl::size_t) noexcept {
244 return hashed_string{str};
245}
246
252[[nodiscard]] ENTT_CONSTEVAL hashed_wstring operator""_hws(const wchar_t *str, stl::size_t) noexcept {
253 return hashed_wstring{str};
254}
255
256} // namespace literals
257
258} // namespace entt
259
260#endif
Zero overhead unique identifier.
constexpr bool operator==(const basic_hashed_string &other) const noexcept
Compares two hashed strings.
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.
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.
constexpr auto operator<=>(const basic_hashed_string &other) const noexcept
Lexicographically compares two hashed strings.
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 of 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:25
basic_hashed_string< char > hashed_string
Aliases for common character types.
Definition fwd.hpp:41
basic_hashed_string(const Char *str, stl::size_t len) -> basic_hashed_string< Char >
Deduction guide.
basic_hashed_string< wchar_t > hashed_wstring
Aliases for common character types.
Definition fwd.hpp:44
stl::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:29