Hash.hpp
Go to the documentation of this file.
1 //
3 // Aurora C++ Library
4 // Copyright (c) 2012-2016 Jan Haller
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would be
17 // appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not be
20 // misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
25 
28 
29 #ifndef AURORA_HASH_HPP
30 #define AURORA_HASH_HPP
31 
33 
34 #include <functional>
35 
36 
37 namespace aurora
38 {
39 
42 
45 template <typename T>
46 std::size_t hashValue(const T& object
47  AURORA_ENABLE_IF(!std::is_enum<T>::value))
48 {
49  std::hash<T> hasher;
50  return hasher(object);
51 }
52 
53 // Overload for enums
54 template <typename T>
55 std::size_t hashValue(const T& enumerator
56  AURORA_ENABLE_IF(std::is_enum<T>::value))
57 {
58  return hashValue(static_cast<typename std::underlying_type<T>::type>(enumerator));
59 }
60 
63 template <typename T>
64 void hashCombine(std::size_t& seed, const T& object)
65 {
66  // Implementation from Boost.Functional/Hash
67  seed ^= hashValue(object) + 0x9e3779b9u + (seed << 6) + (seed >> 2);
68 }
69 
72 template <typename Itr>
73 void hashRange(std::size_t& seed, Itr begin, Itr end)
74 {
75  for (; begin != end; ++begin)
76  hashCombine(seed, *begin);
77 }
78 
81 struct PairHasher
82 {
83  template <typename T, typename U>
84  std::size_t operator() (const std::pair<T, U>& pair) const
85  {
86  std::size_t hash = 0u;
87  hashCombine(hash, pair.first);
88  hashCombine(hash, pair.second);
89 
90  return hash;
91  }
92 };
93 
95 
96 } // namespace aurora
97 
98 #endif // AURORA_HASH_HPP
Utilities for template metaprogramming.
void hashCombine(std::size_t &seed, const T &object)
Combines a hash with the hash value of another object.
Definition: Hash.hpp:64
std::size_t hashValue(const T &object)
Computes the hash value of an object (short-hand notation).
Definition: Hash.hpp:46
void hashRange(std::size_t &seed, Itr begin, Itr end)
Combines a hash with the hash value of a range of objects.
Definition: Hash.hpp:73
#define AURORA_ENABLE_IF(...)
SFINAE Enable If Macro for parameter lists.
Definition: Templates.hpp:174
Hash object for std::pair.
Definition: Hash.hpp:81
Definition: DispatchTraits.hpp:39