Vcsn  2.8
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  namespace detail
8  {
10  template <typename ValueSet, typename Value = typename ValueSet::value_t>
11  struct compare
12  {
13  using valueset_t = ValueSet;
14  using value_t = Value;
15 
16  int operator()(const value_t& lhs, const value_t& rhs) const
17  {
18  return valueset_t::compare(lhs, rhs);
19  }
20  };
21  }
22 
26  template <typename ValueSet>
27  class equal_to : public std::equal_to<typename ValueSet::value_t>
28  {
29  public:
30  using valueset_t = ValueSet;
31  using value_t = typename valueset_t::value_t;
32 
33  bool operator()(const value_t& v1, const value_t& v2) const
34  {
35  return valueset_t::equal(v1, v2);
36  }
37  };
38 
39  namespace detail
40  {
44  template <typename T>
45  auto hash_value(const T& v)
46  -> decltype(std::hash<T>{}(v))
47  {
48  auto hasher = std::hash<T>{};
49  return hasher(v);
50  }
51  }
52 
53  using detail::hash_value;
54 
55  // http://stackoverflow.com/questions/2590677
56  inline void hash_combine_hash(std::size_t& seed, size_t h)
57  {
58  seed ^= h + 0x9e3779b9 + (seed<<6) + (seed>>2);
59  }
60 
61  // http://stackoverflow.com/questions/2590677
62  template <typename T>
63  void hash_combine(std::size_t& seed, const T& v)
64  {
65  hash_combine_hash(seed, hash_value(v));
66  }
67 
71  template <typename ValueSet>
72  class hash
73  {
74  public:
75  using valueset_t = ValueSet;
76  using value_t = typename valueset_t::value_t;
77 
78  size_t operator()(const value_t& v) const
79  {
80  return valueset_t::hash(v);
81  }
82 
83  // Not used, but needed to satisfy the specification. See for example
84  // http://www.cplusplus.com/reference/functional/hash/ .
85  using result_type = size_t;
87  };
88 
90  template <typename ValueSet, typename Value = typename ValueSet::value_t>
91  struct less
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);
99  }
100  };
101 
103  template <typename ValueSet, typename Value = typename ValueSet::value_t>
104  struct less_equal
105  {
106  using valueset_t = ValueSet;
107  using value_t = Value;
108 
109  bool operator()(const value_t& lhs, const value_t& rhs) const
110  {
111  return valueset_t::less(lhs, rhs) || valueset_t::equal(lhs, rhs);
112  }
113  };
114 } // namespace vcsn
115 
116 namespace std
117 {
118  template <typename Value, size_t Size>
119  struct hash<std::array<Value, Size>>
120  {
121  public:
122  using value_t = std::array<Value, Size>;
123 
124  size_t operator()(const value_t& v) const
125  {
126  std::hash<Value> h;
127  size_t res = h(v[0]);
128  for (size_t i = 1; i < Size; i++)
129  vcsn::hash_combine(res, v[i]);
130  return res;
131  }
132  };
133 
134 #if defined __clang__ and __clang_major__ == 3 and __clang_minor__ < 7
135  // For clang 3.5, this is needed for ADL
137 #endif
138 };
typename valueset_t::value_t value_t
Definition: functional.hh:76
value_t argument_type
Definition: functional.hh:86
typename valueset_t::value_t value_t
Definition: functional.hh:31
ValueSet valueset_t
Definition: functional.hh:75
size_t operator()(const value_t &v) const
Definition: functional.hh:124
ValueSet valueset_t
Definition: functional.hh:106
bool operator()(const value_t &lhs, const value_t &rhs) const
Definition: functional.hh:109
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: functional.hh:72
Value value_t
Definition: functional.hh:94
bool operator()(const value_t &v1, const value_t &v2) const
Definition: functional.hh:33
void hash_combine_hash(std::size_t &seed, size_t h)
Definition: functional.hh:56
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:45
Functor to compare Values of ValueSets.
Definition: functional.hh:104
Definition: a-star.hh:8
bool operator()(const value_t &lhs, const value_t &rhs) const
Definition: functional.hh:96
ValueSet valueset_t
Definition: functional.hh:93
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: functional.hh:27
int operator()(const value_t &lhs, const value_t &rhs) const
Definition: functional.hh:16
ValueSet valueset_t
Definition: functional.hh:30
void hash_combine(std::size_t &seed, const T &v)
Definition: functional.hh:63
Functor to compare Values of ValueSets.
Definition: functional.hh:91
Functor to three-way comparison Values of ValueSets.
Definition: functional.hh:11
std::array< Value, Size > value_t
Definition: functional.hh:122
size_t operator()(const value_t &v) const
Definition: functional.hh:78
size_t result_type
Definition: functional.hh:85
STL namespace.
int compare(const Lhs &lhs, const Rhs &rhs)
Comparison between lhs and rhs.
return res
Definition: multiply.hh:399
return hasher(v)