Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
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 std::string objects.
14 * Supports std::string, std::string_view, and const char*.
15 */
16struct StringHash {
17 using is_transparent = void; ///< Enables heterogeneous lookup
18 using hash_type = std::hash<std::string_view>;
19
20 /**
21 * @brief Hashes a string-like object.
22 * @param str String to hash
23 * @return Hash value
24 */
25 [[nodiscard]] size_t operator()(std::string_view str) const noexcept { return hash_type{}(str); }
26
27 /**
28 * @brief Hashes a std::string.
29 * @param str String to hash
30 * @return Hash value
31 */
32 [[nodiscard]] size_t operator()(const std::string& str) const noexcept { return hash_type{}(str); }
33
34 /**
35 * @brief Hashes a C-style string.
36 * @param str String to hash
37 * @return Hash value
38 */
39 [[nodiscard]] size_t operator()(const char* str) const noexcept { return hash_type{}(str); }
40};
41
42/**
43 * @brief Transparent equality comparator for string types.
44 * @details Enables heterogeneous lookup in unordered containers,
45 * allowing comparisons between different string types without temporary allocations.
46 * Supports std::string, std::string_view, and const char*.
47 */
49 using is_transparent = void; ///< Enables heterogeneous lookup
50
51 /**
52 * @brief Compares two string-like objects for equality.
53 * @param lhs Left-hand side string
54 * @param rhs Right-hand side string
55 * @return True if strings are equal, false otherwise
56 */
57 [[nodiscard]] constexpr bool operator()(std::string_view lhs, std::string_view rhs) const noexcept {
58 return lhs == rhs;
59 }
60
61 /**
62 * @brief Compares std::string with std::string_view.
63 * @param lhs Left-hand side string
64 * @param rhs Right-hand side string view
65 * @return True if strings are equal, false otherwise
66 */
67 [[nodiscard]] bool operator()(const std::string& lhs, std::string_view rhs) const noexcept { return lhs == rhs; }
68
69 /**
70 * @brief Compares std::string_view with std::string.
71 * @param lhs Left-hand side string view
72 * @param rhs Right-hand side string
73 * @return True if strings are equal, false otherwise
74 */
75 [[nodiscard]] bool operator()(std::string_view lhs, const std::string& rhs) const noexcept { return lhs == rhs; }
76
77 /**
78 * @brief Compares two std::string objects.
79 * @param lhs Left-hand side string
80 * @param rhs Right-hand side string
81 * @return True if strings are equal, false otherwise
82 */
83 [[nodiscard]] bool operator()(const std::string& lhs, const std::string& rhs) const noexcept { return lhs == rhs; }
84
85 /**
86 * @brief Compares std::string with C-style string.
87 * @param lhs Left-hand side string
88 * @param rhs Right-hand side C-style string
89 * @return True if strings are equal, false otherwise
90 */
91 [[nodiscard]] bool operator()(const std::string& lhs, const char* rhs) const noexcept { return lhs == rhs; }
92
93 /**
94 * @brief Compares C-style string with std::string_view.
95 * @param lhs Left-hand side C-style string
96 * @param rhs Right-hand side string view
97 * @return True if strings are equal, false otherwise
98 */
99 [[nodiscard]] constexpr bool operator()(const char* lhs, std::string_view rhs) const noexcept { return lhs == rhs; }
100
101 /**
102 * @brief Compares C-style string with std::string.
103 * @param lhs Left-hand side C-style string
104 * @param rhs Right-hand side string
105 * @return True if strings are equal, false otherwise
106 */
107 [[nodiscard]] bool operator()(const char* lhs, const std::string& rhs) const noexcept { return lhs == rhs; }
108
109 /**
110 * @brief Compares std::string_view with C-style string.
111 * @param lhs Left-hand side string view
112 * @param rhs Right-hand side C-style string
113 * @return True if strings are equal, false otherwise
114 */
115 [[nodiscard]] constexpr bool operator()(std::string_view lhs, const char* rhs) const noexcept { return lhs == rhs; }
116};
117
118} // 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.