Vcsn  2.1
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  inline 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 
62  template <typename T>
63  inline auto hash_value(const T& v)
64  -> decltype(std::hash<T>{}(v))
65  {
66  std::hash<T> hasher;
67  return hasher(v);
68  }
69 
71  template <typename ValueSet, typename Value = typename ValueSet::value_t>
72  struct less
73  {
74  using valueset_t = ValueSet;
75  using value_t = Value;
76 
77  bool operator()(const value_t& lhs, const value_t& rhs) const
78  {
79  return valueset_t::less(lhs, rhs);
80  }
81  };
82 
84  template <typename ValueSet, typename Value = typename ValueSet::value_t>
85  struct less_equal
86  {
87  using valueset_t = ValueSet;
88  using value_t = Value;
89 
90  bool operator()(const value_t& lhs, const value_t& rhs) const
91  {
92  return valueset_t::less(lhs, rhs) || valueset_t::equal(lhs, rhs);
93  }
94  };
95 } // namespace vcsn
96 
97 namespace std
98 {
99  template <typename Value, size_t Size>
100  struct hash<std::array<Value, Size>>
101  {
102  public:
103  using value_t = std::array<Value, Size>;
104 
105  size_t operator()(const value_t& v) const
106  {
107  std::hash<Value> h;
108  size_t res = h(v[0]);
109  for (size_t i = 1; i < Size; i++)
110  vcsn::hash_combine(res, v[i]);
111  return res;
112  }
113  };
114 };
STL namespace.
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: functional.hh:12
Functor to compare Values of ValueSets.
Definition: functional.hh:72
typename valueset_t::value_t value_t
Definition: functional.hh:46
value_t argument_type
Definition: functional.hh:56
ValueSet valueset_t
Definition: functional.hh:74
bool operator()(const value_t &v1, const value_t &v2) const
Definition: functional.hh:18
bool operator()(const value_t &lhs, const value_t &rhs) const
Definition: functional.hh:90
std::array< Value, Size > value_t
Definition: functional.hh:103
typename valueset_t::value_t value_t
Definition: functional.hh:16
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:63
ValueSet valueset_t
Definition: functional.hh:15
size_t operator()(const value_t &v) const
Definition: functional.hh:48
return hasher(v)
bool operator()(const value_t &lhs, const value_t &rhs) const
Definition: functional.hh:77
void hash_combine(std::size_t &seed, const T &v)
Definition: functional.hh:32
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: functional.hh:42
size_t operator()(const value_t &v) const
Definition: functional.hh:105
void hash_combine_hash(std::size_t &seed, size_t h)
Definition: functional.hh:25
ValueSet valueset_t
Definition: functional.hh:45
Functor to compare Values of ValueSets.
Definition: functional.hh:85
Value value_t
Definition: functional.hh:75
size_t result_type
Definition: functional.hh:55
ValueSet valueset_t
Definition: functional.hh:87