1#ifndef ENTT_CORE_HASHED_STRING_HPP
2#define ENTT_CORE_HASHED_STRING_HPP
4#include "../stl/cstddef.hpp"
5#include "../stl/cstdint.hpp"
13template<
typename =
id_type>
17struct fnv_1a_params<stl::uint32_t> {
18 static constexpr auto offset = 2166136261;
19 static constexpr auto prime = 16777619;
23struct fnv_1a_params<stl::uint64_t> {
24 static constexpr auto offset = 14695981039346656037ull;
25 static constexpr auto prime = 1099511628211ull;
28template<
typename Char>
30 using value_type = Char;
31 using size_type = stl::size_t;
34 const value_type *repr{};
35 hash_type hash{fnv_1a_params<>::offset};
57template<
typename Char>
59 using base_type = internal::basic_hashed_string<Char>;
60 using params = internal::fnv_1a_params<>;
62 struct const_wrapper {
64 constexpr const_wrapper(
const base_type::value_type *str) noexcept
67 const base_type::value_type *repr;
94 template<stl::
size_t N>
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;
133 template<stl::
size_t N>
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;
153 : base_type{wrapper.repr} {
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;
166 return base_type::length;
174 return base_type::repr;
182 return base_type::hash;
186 [[nodiscard]]
explicit constexpr operator const value_type *()
const noexcept {
194 [[nodiscard]]
constexpr operator hash_type() const noexcept {
204 return value() == other.value();
213 return value() <=> other.value();
223template<
typename Char>
232template<
typename Char, stl::
size_t N>
236inline namespace literals {
243[[nodiscard]] ENTT_CONSTEVAL
hashed_string operator""_hs(
const char *str, stl::size_t)
noexcept {
252[[nodiscard]] ENTT_CONSTEVAL
hashed_wstring operator""_hws(
const wchar_t *str, stl::size_t)
noexcept {
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.
base_type::size_type size_type
base_type::hash_type hash_type
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.
base_type::value_type value_type
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...
basic_hashed_string< char > hashed_string
Aliases for common character types.
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.
stl::uint32_t id_type
Alias declaration for type identifiers.