1 #ifndef ENTT_CORE_HASHED_STRING_HPP
2 #define ENTT_CORE_HASHED_STRING_HPP
6 #include "../config/config.h"
22 struct fnv1a_traits<std::uint32_t> {
23 using type = std::uint32_t;
24 static constexpr std::uint32_t offset = 2166136261;
25 static constexpr std::uint32_t prime = 16777619;
29 struct fnv1a_traits<std::uint64_t> {
30 using type = std::uint64_t;
31 static constexpr std::uint64_t offset = 14695981039346656037ull;
32 static constexpr std::uint64_t prime = 1099511628211ull;
35 template<
typename Char>
37 using value_type = Char;
38 using size_type = std::size_t;
41 const value_type *repr;
68 template<
typename Char>
70 using base_type = internal::basic_hashed_string<Char>;
71 using hs_traits = internal::fnv1a_traits<id_type>;
73 struct const_wrapper {
75 constexpr const_wrapper(
const Char *str) ENTT_NOEXCEPT: repr{str} {}
80 [[nodiscard]]
static constexpr
auto helper(
const Char *str) ENTT_NOEXCEPT {
81 base_type base{str, 0u, hs_traits::offset};
83 for(; str[base.length]; ++base.length) {
84 base.hash = (base.hash ^
static_cast<hs_traits::type
>(str[base.length])) * hs_traits::prime;
91 [[nodiscard]]
static constexpr
auto helper(
const Char *str,
const std::size_t len) ENTT_NOEXCEPT {
92 base_type base{str, len, hs_traits::offset};
95 base.hash = (base.hash ^
static_cast<hs_traits::type
>(str[pos])) * hs_traits::prime;
125 template<std::
size_t N>
135 [[nodiscard]]
static constexpr
hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
149 : base_type{helper(str, len)} {}
156 template<std::
size_t N>
158 : base_type{helper(str)} {}
170 : base_type{helper(wrapper.repr)} {}
177 return base_type::length;
185 return base_type::repr;
193 return base_type::hash;
197 [[nodiscard]] constexpr
operator const value_type *()
const ENTT_NOEXCEPT {
205 [[nodiscard]] constexpr
operator hash_type() const ENTT_NOEXCEPT {
216 template<
typename Char>
225 template<
typename Char, std::
size_t N>
235 template<
typename Char>
237 return lhs.value() == rhs.value();
247 template<
typename Char>
249 return !(lhs == rhs);
259 template<
typename Char>
261 return lhs.value() < rhs.value();
272 template<
typename Char>
285 template<
typename Char>
298 template<
typename Char>
309 inline namespace literals {
316 [[nodiscard]] constexpr
hashed_string operator"" _hs(
const char *str, std::size_t) ENTT_NOEXCEPT {
325 [[nodiscard]] constexpr
hashed_wstring operator"" _hws(
const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
Zero overhead unique identifier.
typename base_type::value_type value_type
Character type.
constexpr const value_type * data() const
Returns the human-readable representation of a hashed string.
static constexpr hash_type value(const_wrapper wrapper)
Returns directly the numeric representation of a string.
constexpr hash_type value() const
Returns the numeric representation of a hashed string.
typename base_type::size_type size_type
Unsigned integer type.
static constexpr hash_type value(const value_type *str, const size_type len)
Returns directly the numeric representation of a string view.
constexpr basic_hashed_string(const_wrapper wrapper)
Explicit constructor on purpose to avoid constructing a hashed string directly from a const value_typ...
static constexpr hash_type value(const value_type(&str)[N])
Returns directly the numeric representation of a string.
constexpr size_type size() const
Returns the size a hashed string.
typename base_type::hash_type hash_type
Unsigned integer type.
constexpr basic_hashed_string(const value_type *str, const size_type len)
Constructs a hashed string from a string view.
constexpr basic_hashed_string()
Constructs an empty hashed string.
constexpr basic_hashed_string(const value_type(&str)[N])
Constructs a hashed string from an array of const characters.
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs)
Compares two hashed strings.
std::uint32_t id_type
Alias declaration for type identifiers.
basic_hashed_string(const Char *str, const std::size_t len) -> basic_hashed_string< Char >
Deduction guide.
constexpr bool operator<=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs)
Compares two hashed strings.
constexpr bool operator>=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs)
Compares two hashed strings.
constexpr bool operator<(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs)
Compares two hashed strings.
bool operator!=(const basic_any< Len, Align > &lhs, const basic_any< Len, Align > &rhs)
Checks if two wrappers differ in their content.
constexpr bool operator>(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs)
Compares two hashed strings.