VCCC  2024.05
VisualCamp Common C++ library
hash.hpp
Go to the documentation of this file.
1 //
2 // Created by YongGyu Lee on 02/01/24.
3 //
4 
5 #ifndef VCCC_VARIANT_HASH_HPP
6 #define VCCC_VARIANT_HASH_HPP
7 
8 #include <functional>
9 
16 
19 
20 namespace vccc {
21 namespace detail {
22 
23 struct variant_hash_visitor {
24  template<typename T, std::size_t I>
25  constexpr std::size_t operator()(const T& x, in_place_index_t<I>) const {
26  return vccc::FNV_1a(std::hash<T>{}(x), I);
27  }
28 
29  template<typename T>
30  constexpr std::size_t operator()(const T& x, in_place_index_t<variant_npos>) const {
31  return 0;
32  }
33 };
34 
35 template<typename Variant, bool Hashable /* false */>
36 struct variant_hash {};
37 
38 template<typename Variant>
39 struct variant_hash<Variant, true> {
40  std::size_t operator()(const Variant& var) const {
41  return variant_raw_visit(var.index(), var._base().union_, variant_hash_visitor{});
42  }
43 };
44 
45 } // namespace detail
46 } // namespace vccc
47 
48 template<typename... Ts>
49 struct std::hash<vccc::variant<Ts...>>
50  : vccc::detail::variant_hash<
51  vccc::variant<Ts...>,
52  vccc::conjunction<vccc::is_invocable<std::hash<Ts>, const vccc::remove_cvref_t<Ts>&>...>::value
53  > {};
54 
56 
57 #endif // VCCC_VARIANT_HASH_HPP
std::size_t FNV_1a(std::size_t value, const T &byte)
Definition: hash_array.hpp:72
Definition: directory.h:12