Vcsn  2.2
Be Rational
functional.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional> // std::equal_to
4 
5 namespace vcsn
6 {
7 
11  template <typename ValueSet>
12  class equal_to : public std::equal_to<typename ValueSet::value_t>
13  {
14  public:
15  using valueset_t = ValueSet;
16  using value_t = typename valueset_t::value_t;
17 
18  bool operator()(const value_t& v1, const value_t& v2) const
19  {
20  return valueset_t::equal(v1, v2);
21  }
22  };
23 
24  // http://stackoverflow.com/questions/2590677
25  inline void hash_combine_hash(std::size_t& seed, size_t h)
26  {
27  seed ^= h + 0x9e3779b9 + (seed<<6) + (seed>>2);
28  }
29 
30  // http://stackoverflow.com/questions/2590677
31  template <typename T>
32  void hash_combine(std::size_t& seed, const T& v)
33  {
34  std::hash<T> hasher;
35  hash_combine_hash(seed, hasher(v));
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  namespace detail
60  {
61 
65  template <typename T>
66  inline auto hash_value(const T& v)
67  -> decltype(std::hash<T>{}(v))
68  {
69  std::hash<T> hasher;
70  return hasher(v);
71  }
72  }
73 
74  using detail::hash_value;
75 
77  template <typename ValueSet, typename Value = typename ValueSet::value_t>
78  struct less
79  {
80  using valueset_t = ValueSet;
81  using value_t = Value;
82 
83  bool operator()(const value_t& lhs, const value_t& rhs) const
84  {
85  return valueset_t::less(lhs, rhs);
86  }
87  };
88 
90  template <typename ValueSet, typename Value = typename ValueSet::value_t>
91  struct less_equal
92  {
93  using valueset_t = ValueSet;
94  using value_t = Value;
95 
96  bool operator()(const value_t& lhs, const value_t& rhs) const
97  {
98  return valueset_t::less(lhs, rhs) || valueset_t::equal(lhs, rhs);
99  }
100  };
101 } // namespace vcsn
102 
103 namespace std
104 {
105  template <typename Value, size_t Size>
106  struct hash<std::array<Value, Size>>
107  {
108  public:
109  using value_t = std::array<Value, Size>;
110 
111  size_t operator()(const value_t& v) const
112  {
113  std::hash<Value> h;
114  size_t res = h(v[0]);
115  for (size_t i = 1; i < Size; i++)
116  vcsn::hash_combine(res, v[i]);
117  return res;
118  }
119  };
120 
121 #if defined __clang__ and __clang_major__ == 3 and __clang_minor__ < 7
122  // For clang 3.5, this is needed for ADL
124 #endif
125 };
bool operator()(const value_t &lhs, const value_t &rhs) const
Definition: functional.hh:96
size_t result_type
Definition: functional.hh:55
ValueSet valueset_t
Definition: functional.hh:45
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:66
Definition: a-star.hh:8
bool operator()(const value_t &v1, const value_t &v2) const
Definition: functional.hh:18
STL namespace.
ValueSet valueset_t
Definition: functional.hh:80
ValueSet valueset_t
Definition: functional.hh:93
bool operator()(const value_t &lhs, const value_t &rhs) const
Definition: functional.hh:83
typename valueset_t::value_t value_t
Definition: functional.hh:46
size_t operator()(const value_t &v) const
Definition: functional.hh:48
void hash_combine(std::size_t &seed, const T &v)
Definition: functional.hh:32
value_t argument_type
Definition: functional.hh:56
Value value_t
Definition: functional.hh:81
Functor to compare Values of ValueSets.
Definition: functional.hh:91
size_t operator()(const value_t &v) const
Definition: functional.hh:111
std::array< Value, Size > value_t
Definition: functional.hh:109
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: functional.hh:42
ValueSet valueset_t
Definition: functional.hh:15
typename valueset_t::value_t value_t
Definition: functional.hh:16
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: functional.hh:12
void hash_combine_hash(std::size_t &seed, size_t h)
Definition: functional.hh:25
return hasher(v)
Functor to compare Values of ValueSets.
Definition: functional.hh:78