Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hash.hh
Go to the documentation of this file.
1 #ifndef VCSN_MISC_HASH_HH
2 # define VCSN_MISC_HASH_HH
3 
4 # include <functional> // std::equal_to
5 
6 namespace std
7 {
8 
9  // http://stackoverflow.com/questions/2590677
10  template <class T>
11  inline void hash_combine(std::size_t& seed, const T& v)
12  {
13  std::hash<T> hasher;
14  seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
15  }
16 
17 }
18 
19 namespace vcsn
20 {
21 
25  template <typename ValueSet>
26  class equal_to : public std::equal_to<typename ValueSet::value_t>
27  {
28  public:
29  using valueset_t = ValueSet;
30  using value_t = typename valueset_t::value_t;
31 
32  bool operator()(const value_t& v1, const value_t& v2) const
33  {
34  return valueset_t::equals(v1, v2);
35  }
36  };
37 
41  template <typename ValueSet>
42  class hash
43  {
44  public:
45  using valueset_t = ValueSet;
46  using value_t = typename valueset_t::value_t;
47 
48  size_t operator()(const value_t& v) const
49  {
50  return valueset_t::hash(v);
51  }
52 
53  // Not used, but needed to satisfy the specification. See for example
54  // http://www.cplusplus.com/reference/functional/hash/ .
55  using result_type = size_t;
57  };
58 
59  // Following the naming convention of Boost.
60  template <class T>
61  inline std::size_t hash_value(const T& v)
62  {
63  std::hash<T> hasher;
64  return hasher(v);
65  }
66 
67 } // namespace vcsn
68 
69 #endif // !VCSN_MISC_HASH_HH
ValueSet valueset_t
Definition: hash.hh:45
value_t argument_type
Definition: hash.hh:56
typename valueset_t::value_t value_t
Definition: hash.hh:30
std::size_t hash_value(const T &v)
Definition: hash.hh:61
bool operator()(const value_t &v1, const value_t &v2) const
Definition: hash.hh:32
ValueSet valueset_t
Definition: hash.hh:29
size_t result_type
Definition: hash.hh:55
typename valueset_t::value_t value_t
Definition: hash.hh:46
size_t operator()(const value_t &v) const
Definition: hash.hh:48
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: hash.hh:42
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: hash.hh:26