Vcsn  2.4
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  namespace detail
25  {
29  template <typename T>
30  auto hash_value(const T& v)
31  -> decltype(std::hash<T>{}(v))
32  {
33  auto hasher = std::hash<T>{};
34  return hasher(v);
35  }
36  }
37 
38  using detail::hash_value;
39 
40  // http://stackoverflow.com/questions/2590677
41  inline void hash_combine_hash(std::size_t& seed, size_t h)
42  {
43  seed ^= h + 0x9e3779b9 + (seed<<6) + (seed>>2);
44  }
45 
46  // http://stackoverflow.com/questions/2590677
47  template <typename T>
48  void hash_combine(std::size_t& seed, const T& v)
49  {
50  hash_combine_hash(seed, hash_value(v));
51  }
52 
56  template <typename ValueSet>
57  class hash
58  {
59  public:
60  using valueset_t = ValueSet;
61  using value_t = typename valueset_t::value_t;
62 
63  size_t operator()(const value_t& v) const
64  {
65  return valueset_t::hash(v);
66  }
67 
68  // Not used, but needed to satisfy the specification. See for example
69  // http://www.cplusplus.com/reference/functional/hash/ .
70  using result_type = size_t;
72  };
73 
75  template <typename ValueSet, typename Value = typename ValueSet::value_t>
76  struct less
77  {
78  using valueset_t = ValueSet;
79  using value_t = Value;
80 
81  bool operator()(const value_t& lhs, const value_t& rhs) const
82  {
83  return valueset_t::less(lhs, rhs);
84  }
85  };
86 
88  template <typename ValueSet, typename Value = typename ValueSet::value_t>
89  struct less_equal
90  {
91  using valueset_t = ValueSet;
92  using value_t = Value;
93 
94  bool operator()(const value_t& lhs, const value_t& rhs) const
95  {
96  return valueset_t::less(lhs, rhs) || valueset_t::equal(lhs, rhs);
97  }
98  };
99 } // namespace vcsn
100 
101 namespace std
102 {
103  template <typename Value, size_t Size>
104  struct hash<std::array<Value, Size>>
105  {
106  public:
107  using value_t = std::array<Value, Size>;
108 
109  size_t operator()(const value_t& v) const
110  {
111  std::hash<Value> h;
112  size_t res = h(v[0]);
113  for (size_t i = 1; i < Size; i++)
114  vcsn::hash_combine(res, v[i]);
115  return res;
116  }
117  };
118 
119 #if defined __clang__ and __clang_major__ == 3 and __clang_minor__ < 7
120  // For clang 3.5, this is needed for ADL
122 #endif
123 };
std::array< Value, Size > value_t
Definition: functional.hh:107
STL namespace.
size_t operator()(const value_t &v) const
Definition: functional.hh:63
return res
Definition: multiply.hh:398
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:76
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: functional.hh:57
size_t result_type
Definition: functional.hh:70
ValueSet valueset_t
Definition: functional.hh:60
size_t operator()(const value_t &v) const
Definition: functional.hh:109
void hash_combine_hash(std::size_t &seed, size_t h)
Definition: functional.hh:41
bool operator()(const value_t &lhs, const value_t &rhs) const
Definition: functional.hh:81
ValueSet valueset_t
Definition: functional.hh:78
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:30
Definition: a-star.hh:8
Functor to compare Values of ValueSets.
Definition: functional.hh:89
ValueSet valueset_t
Definition: functional.hh:91
ValueSet valueset_t
Definition: functional.hh:15
value_t argument_type
Definition: functional.hh:71
return hasher(v)
typename valueset_t::value_t value_t
Definition: functional.hh:61
Value value_t
Definition: functional.hh:79
return v
Definition: multiply.hh:361
bool operator()(const value_t &lhs, const value_t &rhs) const
Definition: functional.hh:94
typename valueset_t::value_t value_t
Definition: functional.hh:16
void hash_combine(std::size_t &seed, const T &v)
Definition: functional.hh:48
bool operator()(const value_t &v1, const value_t &v2) const
Definition: functional.hh:18