xenium
hash.hpp
1 //
2 // Copyright (c) 2018-2020 Manuel Pöter.
3 // Licensed under the MIT License. See LICENSE file in the project root for full license information.
4 //
5 
6 #ifndef XENIUM_HASH_HPP
7 #define XENIUM_HASH_HPP
8 
9 #include <xenium/utils.hpp>
10 
11 #include <cstdint>
12 #include <functional>
13 
14 namespace xenium {
15 
16  using hash_t = std::size_t;
17 
24  template <class Key>
25  struct hash {
26  hash_t operator()(const Key& key) const noexcept { return _hash(key); }
27  private:
28  std::hash<Key> _hash;
29  };
30 
43  template <class Key>
44  struct hash<Key*> {
45  hash_t operator()(const Key* key) const noexcept {
46  constexpr auto alignment = std::alignment_of<Key>();
47  constexpr auto shift = utils::find_last_bit_set(alignment) - 1;
48  auto hash = reinterpret_cast<hash_t>(key);
49  assert((hash >> shift) << shift == hash); // we must not loose a bit!
50  return hash >> shift;
51  }
52  };
53 }
54 
55 #endif
xenium::hash
Slim wrapper around std::hash with specialization for pointer types.
Definition: hash.hpp:25