VCCC  2024.05
VisualCamp Common C++ library
hash_array.hpp
Go to the documentation of this file.
1 //
2 // Created by YongGyu Lee on 12/1/23.
3 //
4 
5 #ifndef VCCC_FUNCTIONAL_HASH_ARRAY_HPP_
6 #define VCCC_FUNCTIONAL_HASH_ARRAY_HPP_
7 
8 #include <climits>
9 #include <cstddef>
10 #include <type_traits>
11 
12 namespace vccc {
13 
16 
19 template<typename T, std::size_t Bytes = sizeof(T) * CHAR_BIT>
21 template<typename T>
22 struct basic_FNV_prime<T, 32> : std::integral_constant<T, 0x01000193> {};
23 template<typename T>
24 struct basic_FNV_prime<T, 64> : std::integral_constant<T, 0x00000100000001B3> {};
25 
28 
29 
32 template<typename T, std::size_t Bytes = sizeof(T) * CHAR_BIT>
34 template<typename T>
35 struct basic_FNV_offset_basis<T, 32> : std::integral_constant<T, 0x811c9dc5> {};
36 template<typename T>
37 struct basic_FNV_offset_basis<T, 64> : std::integral_constant<T, 0xcbf29ce484222325> {};
38 
41 
42 // template<typename T> struct basic_FNV_prime<T, 128> : std::integral_constant<T, 0x0000000001000000000000000000013B> {};
43 // template<typename T> struct basic_FNV_offset_basis<T, 128> : std::integral_constant<T, 0x6c62272e07bb014262b821756295c58d> {};
44 
45 // template<typename T> struct basic_FNV_prime<T, 256>
46 // : std::integral_constant<T, 0x0000000000000000000001000000000000000000000000000000000000000163> {};
47 // template<typename T> struct basic_FNV_offset_basis<T, 256>
48 // : std::integral_constant<T, 0xdd268dbcaac550362d98c384c4e576ccc8b1536847b6bbb31023b4c8caee0535> {};
49 
50 
56 template<typename T>
57 std::size_t FNV_1(std::size_t value, const T& byte) {
58  static_assert(std::is_integral<T>::value, "T must integral type");
60  value = value ^ static_cast<std::size_t>(byte);
61  return value;
62 }
63 
64 inline std::size_t FNV_1(std::size_t value, const unsigned char* const bytes, std::size_t size) {
65  for (std::size_t i = 0; i < size; ++i) {
66  value = FNV_1(value, bytes[i]);
67  }
68  return value;
69 }
70 
71 template<typename T>
72 std::size_t FNV_1a(std::size_t value, const T& byte) {
73  static_assert(std::is_integral<T>::value, "T must integral type");
74  value = value ^ static_cast<std::size_t>(byte);
76  return value;
77 }
78 
79 inline std::size_t FNV_1a(std::size_t value, const unsigned char* const bytes, std::size_t size) {
80  for (std::size_t i = 0; i < size; ++i) {
81  value = FNV_1a(value, bytes[i]);
82  }
83  return value;
84 }
86 
90 template<typename T>
91 std::size_t hash_array(const T* bytes, std::size_t size) {
92  return FNV_1a(FNV_offset_basis::value, reinterpret_cast<const unsigned char* const>(bytes), sizeof(T) * size);
93 }
94 
95 
96 template<typename T, std::size_t N>
97 std::size_t hash_array(const T(&bytes)[N]) {
98  return hash_array(bytes, N);
99 }
101 
103 
104 } // namespace vccc
105 
106 #endif // VCCC_FUNCTIONAL_HASH_ARRAY_HPP_
Definition: byte.hpp:27
std::size_t FNV_1a(std::size_t value, const T &byte)
Definition: hash_array.hpp:72
std::size_t FNV_1(std::size_t value, const T &byte)
Definition: hash_array.hpp:57
std::size_t hash_array(const T *bytes, std::size_t size)
Definition: hash_array.hpp:91
Definition: directory.h:12
constexpr auto size(const C &c) -> decltype(c.size())
Definition: size.hpp:16
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35
Definition: hash_array.hpp:33
Definition: hash_array.hpp:20