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 
28 private:
29  std::hash<Key> _hash;
30 };
31 
44 template <class Key>
45 struct hash<Key*> {
46  hash_t operator()(const Key* key) const noexcept {
47  constexpr auto alignment = std::alignment_of<Key>();
48  constexpr auto shift = utils::find_last_bit_set(alignment) - 1;
49  auto hash = reinterpret_cast<hash_t>(key);
50  assert((hash >> shift) << shift == hash); // we must not loose a bit!
51  return hash >> shift;
52  }
53 };
54 } // namespace xenium
55 
56 #endif
xenium::hash
Slim wrapper around std::hash with specialization for pointer types.
Definition: hash.hpp:25