My Project
fnv.h
1 #include <array>
2 #include <type_traits>
3 
4 template <typename T>
5 constexpr auto array_from_object(const T& obj) {
6  std::array<unsigned char, sizeof(T)> ret;
7  for (std::size_t i = 0; i < sizeof(T); ++i) {
8  ret[i] = reinterpret_cast<const unsigned char*>(&obj)[i];
9  }
10  return ret;
11 }
12 
13 // iterator-aware hasher
14 template <
15  typename it,
16  typename = std::enable_if_t<
17  std::is_pointer<decltype(&*std::declval<it>())>::value
18  // &&
19  // std::has_unique_object_representations<decltype(*std::declval<it>())>::value
20  >>
21 constexpr uint32_t FNV32a(it begin, it end) {
22  const uint32_t FNV_32_PRIME = 16777619;
23  uint32_t hval = 2166136261;
24  for (it p = begin; p != end; ++p) {
25  // get *p as a reference to array of unsigned char
26  auto repr = array_from_object(*p);
27  for (auto ch : repr) {
28  hval ^= std::uint32_t{ch};
29  hval *= FNV_32_PRIME;
30  }
31  }
32  return hval;
33 }