Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
string_hash.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <functional>
5#include <string>
6#include <string_view>
7
8namespace helios::utils {
9
10/**
11 * @brief Transparent hash functor for string types.
12 * @details Enables heterogeneous lookup in unordered containers,
13 * allowing lookups with `std::string_view` without constructing temporary
14 * `std::string` objects. Supports `std::string`, `std::string_view`, and `const
15 * char*`.
16 */
17struct StringHash {
18 using is_transparent = void; ///< Enables heterogeneous lookup
19 using hash_type = std::hash<std::string_view>;
20
21 /**
22 * @brief Hashes a string-like object.
23 * @param str String to hash
24 * @return Hash value
25 */
26 [[nodiscard]] size_t operator()(std::string_view str) const noexcept {
27 return hash_type{}(str);
28 }
29
30 /**
31 * @brief Hashes a std::string.
32 * @param str String to hash
33 * @return Hash value
34 */
35 [[nodiscard]] size_t operator()(const std::string& str) const noexcept {
36 return hash_type{}(str);
37 }
38
39 /**
40 * @brief Hashes a C-style string.
41 * @param str String to hash
42 * @return Hash value
43 */
44 [[nodiscard]] size_t operator()(const char* str) const noexcept {
45 return hash_type{}(str);
46 }
47};
48
49/**
50 * @brief Transparent equality comparator for string types.
51 * @details Enables heterogeneous lookup in unordered containers,
52 * allowing comparisons between different string types without temporary
53 * allocations. Supports `std::string`, `std::string_view`, and `const char*`.
54 */
56 using is_transparent = void; ///< Enables heterogeneous lookup
57
58 /**
59 * @brief Compares two string-like objects for equality.
60 * @param lhs Left-hand side string
61 * @param rhs Right-hand side string
62 * @return True if strings are equal, false otherwise
63 */
64 [[nodiscard]] constexpr bool operator()(std::string_view lhs,
65 std::string_view rhs) const noexcept {
66 return lhs == rhs;
67 }
68
69 /**
70 * @brief Compares std::string with std::string_view.
71 * @param lhs Left-hand side string
72 * @param rhs Right-hand side string view
73 * @return True if strings are equal, false otherwise
74 */
75 [[nodiscard]] bool operator()(const std::string& lhs,
76 std::string_view rhs) const noexcept {
77 return lhs == rhs;
78 }
79
80 /**
81 * @brief Compares std::string_view with std::string.
82 * @param lhs Left-hand side string view
83 * @param rhs Right-hand side string
84 * @return True if strings are equal, false otherwise
85 */
86 [[nodiscard]] bool operator()(std::string_view lhs,
87 const std::string& rhs) const noexcept {
88 return lhs == rhs;
89 }
90
91 /**
92 * @brief Compares two std::string objects.
93 * @param lhs Left-hand side string
94 * @param rhs Right-hand side string
95 * @return True if strings are equal, false otherwise
96 */
97 [[nodiscard]] bool operator()(const std::string& lhs,
98 const std::string& rhs) const noexcept {
99 return lhs == rhs;
100 }
101
102 /**
103 * @brief Compares std::string with C-style string.
104 * @param lhs Left-hand side string
105 * @param rhs Right-hand side C-style string
106 * @return True if strings are equal, false otherwise
107 */
108 [[nodiscard]] bool operator()(const std::string& lhs,
109 const char* rhs) const noexcept {
110 return lhs == rhs;
111 }
112
113 /**
114 * @brief Compares C-style string with std::string_view.
115 * @param lhs Left-hand side C-style string
116 * @param rhs Right-hand side string view
117 * @return True if strings are equal, false otherwise
118 */
119 [[nodiscard]] constexpr bool operator()(const char* lhs,
120 std::string_view rhs) const noexcept {
121 return lhs == rhs;
122 }
123
124 /**
125 * @brief Compares C-style string with std::string.
126 * @param lhs Left-hand side C-style string
127 * @param rhs Right-hand side string
128 * @return True if strings are equal, false otherwise
129 */
130 [[nodiscard]] bool operator()(const char* lhs,
131 const std::string& rhs) const noexcept {
132 return lhs == rhs;
133 }
134
135 /**
136 * @brief Compares std::string_view with C-style string.
137 * @param lhs Left-hand side string view
138 * @param rhs Right-hand side C-style string
139 * @return True if strings are equal, false otherwise
140 */
141 [[nodiscard]] constexpr bool operator()(std::string_view lhs,
142 const char* rhs) const noexcept {
143 return lhs == rhs;
144 }
145};
146
147} // namespace helios::utils
Transparent equality comparator for string types.
void is_transparent
Enables heterogeneous lookup.
bool operator()(const std::string &lhs, const std::string &rhs) const noexcept
Compares two std::string objects.
bool operator()(const std::string &lhs, const char *rhs) const noexcept
Compares std::string with C-style string.
constexpr bool operator()(std::string_view lhs, std::string_view rhs) const noexcept
Compares two string-like objects for equality.
constexpr bool operator()(std::string_view lhs, const char *rhs) const noexcept
Compares std::string_view with C-style string.
bool operator()(const char *lhs, const std::string &rhs) const noexcept
Compares C-style string with std::string.
constexpr bool operator()(const char *lhs, std::string_view rhs) const noexcept
Compares C-style string with std::string_view.
bool operator()(const std::string &lhs, std::string_view rhs) const noexcept
Compares std::string with std::string_view.
bool operator()(std::string_view lhs, const std::string &rhs) const noexcept
Compares std::string_view with std::string.
Transparent hash functor for string types.
size_t operator()(std::string_view str) const noexcept
Hashes a string-like object.
std::hash< std::string_view > hash_type
size_t operator()(const char *str) const noexcept
Hashes a C-style string.
size_t operator()(const std::string &str) const noexcept
Hashes a std::string.
void is_transparent
Enables heterogeneous lookup.