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