forpy  2
hash.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef FORPY_UTIL_HASH_H_
3 #define FORPY_UTIL_HASH_H_
4 
5 #include <cstddef>
6 #include <vector>
7 
8 namespace forpy {
20  static size_t hash_fnv_1a(const unsigned char *key, const size_t &len) {
21  // hash = offset_basis
22  size_t h = 14695981039346656037ULL;
23  // for each octet_of_data to be hashed
24  for (size_t i = 0; i < len; ++i) {
25  // hash = hash xor octet_of_data
26  // hash = hash * FNV_prime
27  h = (h ^ key[ i ]) * 1099511628211;
28  }
29  // return hash
30  return h;
31  };
32 
34  struct vector_hasher {
36  size_t operator()(const std::vector<size_t> &t) const {
37  if (t.empty()) return 0;
38  return hash_fnv_1a(reinterpret_cast<const unsigned char *>(&t[0]),
39  t.size() * sizeof(size_t) / sizeof(unsigned char));
40  };
41  };
42 } // namespace forpy
43 #endif // FORPY_UTIL_HASH_H_
44 
static size_t hash_fnv_1a(const unsigned char *key, const size_t &len)
Quick and easy implementation of 64-bit FNV 1a hash.
Definition: hash.h:20
size_t operator()(const std::vector< size_t > &t) const
Definition: hash.h:36
A simple vector<size_t> hasher.
Definition: hash.h:34