LeviLamina
Loading...
Searching...
No Matches
HashedString.h
1#pragma once
2
3#include "mc/_HeaderOutputPredefine.h"
4
5class HashedString {
6public:
7 [[nodiscard]] static constexpr uint64 computeHash(std::string_view str) {
8 if (str.empty()) return 0;
9 uint64 hash = 0xCBF29CE484222325ull;
10 for (char s : str) {
11 hash = s ^ (0x100000001B3ull * hash);
12 }
13 return hash;
14 }
15
16 // Constructors
17 [[nodiscard]] constexpr HashedString(std::nullptr_t = nullptr) noexcept : mStrHash(0), mLastMatch(nullptr) {}
18
19 [[nodiscard]] constexpr HashedString(uint64 h, char const* mStr) noexcept
20 : mStrHash(h),
21 mStr(mStr),
22 mLastMatch(nullptr) {}
23
24 [[nodiscard]] constexpr HashedString(std::string_view mStr) noexcept
25 : mStrHash(computeHash(mStr)),
26 mStr(mStr),
27 mLastMatch(nullptr) {}
28
29 [[nodiscard]] constexpr HashedString(std::string&& s) noexcept
30 : mStrHash(computeHash(s)),
31 mStr(std::move(s)),
32 mLastMatch(nullptr) {}
33
34 [[nodiscard]] constexpr HashedString(std::string const& s) noexcept
35 : mStrHash(computeHash(s)),
36 mStr(s),
37 mLastMatch(nullptr) {}
38
39 [[nodiscard]] constexpr HashedString(char const* mStr) noexcept : HashedString(std::string{mStr}) {}
40
41 [[nodiscard]] constexpr HashedString(HashedString const& other) noexcept
42 : mStrHash(other.mStrHash),
43 mStr(other.mStr),
44 mLastMatch(nullptr) {}
45
46 [[nodiscard]] constexpr HashedString(HashedString&& other) noexcept
47 : mStrHash(other.mStrHash),
48 mStr(std::move(other.mStr)),
49 mLastMatch(other.mLastMatch) {
50 other.mStrHash = 0;
51 other.mLastMatch = nullptr;
52 }
53
54 constexpr HashedString& operator=(HashedString const& other) noexcept {
55 if (this == &other) {
56 return *this;
57 }
58 mStrHash = other.mStrHash;
59 mStr = other.mStr;
60 mLastMatch = nullptr;
61 return *this;
62 }
63
64 constexpr HashedString& operator=(HashedString&& other) noexcept {
65 if (this == &other) {
66 return *this;
67 }
68 mStrHash = other.mStrHash;
69 mStr = std::move(other.mStr);
70 mLastMatch = other.mLastMatch;
71 other.mStrHash = 0;
72 other.mLastMatch = nullptr;
73 return *this;
74 }
75
76 // Accessors
77 [[nodiscard]] constexpr char const* c_str() const noexcept { return mStr.c_str(); }
78
79 [[nodiscard]] constexpr std::string const& getString() const noexcept { return mStr; }
80
81 [[nodiscard]] constexpr uint64 getHash() const noexcept { return mStrHash; }
82
83 [[nodiscard]] constexpr bool empty() const noexcept { return mStr.empty(); }
84
85 // Mutators
86 constexpr void clear() noexcept {
87 mStrHash = 0;
88 mStr.clear();
89 mLastMatch = nullptr;
90 }
91
92 // Operators
93 template <typename StringType>
94 [[nodiscard]] constexpr bool operator==(StringType const& rhs) const noexcept {
95 return mStr == rhs;
96 }
97
98 [[nodiscard]] constexpr bool operator==(HashedString const& other) const noexcept {
99 if (mStrHash == other.mStrHash) {
100 if (mLastMatch == std::addressof(other) && other.mLastMatch == this) {
101 return true;
102 }
103 if (mStr == other.mStr) {
104 mLastMatch = std::addressof(other);
105 other.mLastMatch = this;
106 return true;
107 }
108 }
109 mLastMatch = nullptr;
110 return false;
111 }
112
113 template <typename StringType>
114 [[nodiscard]] constexpr bool operator!=(StringType const& rhs) const noexcept {
115 return mStr != rhs;
116 }
117
118 [[nodiscard]] constexpr bool operator!=(HashedString const& other) const noexcept { return !(*this == other); }
119
120 [[nodiscard]] constexpr std::strong_ordering operator<=>(HashedString const& other) const noexcept {
121 if (mStrHash != other.mStrHash) {
122 return mStrHash <=> other.mStrHash;
123 }
124 return mStr <=> other.mStr;
125 }
126
127 // Convertors
128 [[nodiscard]] constexpr operator std::string const&() const { return mStr; }
129
130 [[nodiscard]] constexpr operator std::string_view() const { return std::string_view(mStr); }
131
132public:
133 // member variables
134 // NOLINTBEGIN
135 uint64 mStrHash;
136 ::std::string mStr;
137 mutable ::HashedString const* mLastMatch;
138 // NOLINTEND
139
140public:
141 // static variables
142 // NOLINTBEGIN
143 MCAPI static ::HashedString& defaultErrorValue();
144 // NOLINTEND
145
146public:
147 // constructor thunks
148 // NOLINTBEGIN
149#ifdef LL_PLAT_C
150 MCAPI void* $ctor(nullptr_t);
151#endif
152
153 MCAPI void* $ctor(::HashedString&& rhs);
154
155 MCAPI void* $ctor(::HashedString const& rhs);
156
157 MCAPI void* $ctor(::std::string const& str);
158
159 MCAPI void* $ctor(char const* str);
160
161 MCAPI void* $ctor(::std::string_view str);
162
163 MCAPI void* $ctor(uint64 hash, char const* str);
164 // NOLINTEND
165
166public:
167 // destructor thunk
168 // NOLINTBEGIN
169 MCFOLD void $dtor();
170 // NOLINTEND
171};
172
173namespace std {
174
175template <>
176struct hash<HashedString> {
177 size_t operator()(HashedString const& str) const noexcept { return str.getHash(); }
178};
179
180} // namespace std
Definition HashedString.h:5
STL namespace.