-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.mpp
80 lines (64 loc) · 1.91 KB
/
Hash.mpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
export module CppUtils.String.Hash;
import std;
import CppUtils.String.Concept;
export namespace CppUtils::String
{
using Hash = std::uint64_t;
using Token = Hash;
[[nodiscard]] inline constexpr auto hash(GenericText auto&& sv) noexcept -> Hash
{
auto result = 0xcb'f2'9c'e4'84'22'23'25u;
for (const auto& c : sv)
result = (static_cast<Hash>(c) ^ result) * 0x1'00'00'00'01'b3u;
return result;
}
template<Char CharT = char, std::size_t N = 1>
struct Hasher final
{
inline constexpr Hasher(Hash hash):
hash{hash},
name{'\0'}
{}
inline constexpr Hasher(const CharT (&cString)[N]):
hash{CppUtils::String::hash(cString)}
{
std::copy(cString, cString + N, name.begin());
}
[[nodiscard]] inline constexpr operator Hash() const noexcept
{
return hash;
}
[[nodiscard]] inline constexpr operator std::basic_string_view<CharT>() const noexcept
{
return std::basic_string_view<CharT>{std::data(name), N - 1};
}
[[nodiscard]] inline constexpr operator std::basic_string<CharT>() const
{
return std::basic_string<CharT>{std::data(name), N - 1};
}
[[nodiscard]] inline constexpr auto operator==(Hash otherHash) const noexcept -> bool
{
return hash == otherHash;
}
Hash hash;
std::array<CharT, N> name;
};
namespace Literals
{
[[nodiscard]] inline constexpr auto operator""_hash(const char* cString, std::size_t size) noexcept -> Hash
{
return hash(std::string_view{cString, size});
}
[[nodiscard]] inline constexpr auto operator""_token(const char* cString, std::size_t size) noexcept -> Token
{
return hash(std::string_view{cString, size});
}
static_assert("Foo"_hash == "Foo"_hash);
static_assert("Foo"_hash != "Bar"_hash);
static_assert("Foo"_token == "Foo"_token);
static_assert("Foo"_token != "Bar"_token);
static_assert("Foo"_hash == "Foo"_token);
static_assert("Foo"_hash == "Foo"_token);
static_assert("Foo"_hash != "Bar"_token);
}
}