-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cppm
More file actions
81 lines (61 loc) · 2.27 KB
/
hash.cppm
File metadata and controls
81 lines (61 loc) · 2.27 KB
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
81
module;
#include <concepts>
#include <cstddef>
#include <functional>
#include <type_traits>
export module mcpplibs.primitives.algorithms.hash;
import mcpplibs.primitives.underlying;
namespace mcpplibs::primitives::algorithms::details {
template <typename T>
concept std_hashable = requires(std::remove_cv_t<T> const &value) {
{
std::hash<std::remove_cv_t<T>>{}(value)
} -> std::convertible_to<std::size_t>;
};
} // namespace mcpplibs::primitives::algorithms::details
export namespace mcpplibs::primitives::algorithms {
template <typename T> struct hash {
using value_type = std::remove_cv_t<T>;
using result_type = std::size_t;
static constexpr bool enabled = false;
auto operator()(value_type const &) const noexcept -> result_type {
return result_type{};
}
};
template <typename T>
using hash_result_t = hash<std::remove_cvref_t<T>>::result_type;
template <typename T>
concept hashable = hash<std::remove_cvref_t<T>>::enabled;
template <typename T>
requires details::std_hashable<T>
struct hash<T> {
using value_type = std::remove_cv_t<T>;
using result_type = std::size_t;
static constexpr bool enabled = true;
auto operator()(value_type const &value) const noexcept(
noexcept(std::hash<value_type>{}(value))) -> result_type {
return std::hash<value_type>{}(value);
}
};
template <underlying_type T>
requires (!details::std_hashable<T> &&
!std::same_as<std::remove_cv_t<T>,
typename underlying::traits<std::remove_cv_t<T>>::rep_type> &&
hash<typename underlying::traits<std::remove_cv_t<T>>::rep_type>::enabled)
struct hash<T> {
using value_type = std::remove_cv_t<T>;
using rep_type = underlying::traits<value_type>::rep_type;
using result_type = hash_result_t<rep_type>;
static constexpr bool enabled = true;
auto operator()(value_type const &value) const noexcept(
noexcept(hash<rep_type>{}(underlying::traits<value_type>::to_rep(value))))
-> result_type {
return hash<rep_type>{}(underlying::traits<value_type>::to_rep(value));
}
};
template <hashable T>
auto hash_value(T const &value) noexcept(
noexcept(hash<std::remove_cvref_t<T>>{}(value))) -> hash_result_t<T> {
return hash<std::remove_cvref_t<T>>{}(value);
}
} // namespace mcpplibs::primitives::algorithms