EnTT 3.16.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 "fwd.hpp"
7
8namespace entt {
9
11namespace internal {
12
13template<typename = id_type>
14struct fnv_1a_params;
15
16template<>
17struct fnv_1a_params<std::uint32_t> {
18 static constexpr auto offset = 2166136261;
19 static constexpr auto prime = 16777619;
20};
21
22template<>
23struct fnv_1a_params<std::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 = std::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 typename base_type::value_type *str) noexcept
65 : repr{str} {}
66
67 const typename base_type::value_type *repr;
68 };
69
70public:
72 using value_type = typename base_type::value_type;
74 using size_type = typename base_type::size_type;
76 using hash_type = typename 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<std::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<std::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};
198
205template<typename Char>
206basic_hashed_string(const Char *str, std::size_t len) -> basic_hashed_string<Char>;
207
214template<typename Char, std::size_t N>
215// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays)
217
225template<typename Char>
226[[nodiscard]] constexpr bool operator==(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) noexcept {
227 return lhs.value() == rhs.value();
228}
229
237template<typename Char>
238[[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) noexcept {
239 return !(lhs == rhs);
240}
241
249template<typename Char>
250[[nodiscard]] constexpr bool operator<(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) noexcept {
251 return lhs.value() < rhs.value();
252}
253
262template<typename Char>
263[[nodiscard]] constexpr bool operator<=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) noexcept {
264 return !(rhs < lhs);
265}
266
275template<typename Char>
276[[nodiscard]] constexpr bool operator>(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) noexcept {
277 return rhs < lhs;
278}
279
288template<typename Char>
289[[nodiscard]] constexpr bool operator>=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) noexcept {
290 return !(lhs < rhs);
291}
292
293inline namespace literals {
294
300[[nodiscard]] ENTT_CONSTEVAL hashed_string operator""_hs(const char *str, std::size_t) noexcept {
301 return hashed_string{str};
302}
303
309[[nodiscard]] ENTT_CONSTEVAL hashed_wstring operator""_hws(const wchar_t *str, std::size_t) noexcept {
310 return hashed_wstring{str};
311}
312
313} // namespace literals
314
315} // namespace entt
316
317#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 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: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
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.