-
Notifications
You must be signed in to change notification settings - Fork 1
/
h.h
43 lines (38 loc) · 1.22 KB
/
h.h
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
#ifndef com_viraltaco_h_h
#define com_viraltaco_h_h "1.0.4"
/// Copyright 2023 viraltaco_ <https://viraltaco.com/h>
#include <numeric>
namespace h::inline v1 {
inline namespace details {
using std::size_t;
using hash_t = unsigned long long;
} // h::inline v1::inline details
constexpr auto hash(char const* str, size_t len) noexcept -> hash_t {
return std::accumulate(str, &str[len], hash_t{}, [](auto h, char8_t c) {
if (c == '-') { return h; }
h <<= 5;
switch (c) {
case '\0':return h | 0b11010; // h bit_or 26
case '=': return h | 0b11011; // h bit_or 27
case ',': return h | 0b11101; // h bit_or 29
case ' ': return h | 0b11110; // h bit_or 30
default:
if ('a' <= c and c <= 'z') {
return h | (c - 'a');
} else if ('A' <= c and c <= 'Z') {
return h | (c - 'A');
} else {
return h | 0b11111; // h bit_or 31;
}
}
});
}
template <size_t N>
consteval auto hash(char const(&str)[N]) { return h::hash(str, N - 1u); }
inline namespace literals {
consteval auto operator""_h(char const* str, size_t len) {
return h::hash(str, len);
}
} // h::inline v1::inline literals
} // h::inline v1
#endif // com_viraltaco_h_h