Awali
Another Weighted Automata library
hash.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_MISC_HASH_HH
18 # define AWALI_MISC_HASH_HH
19 # include <utility>
20 # include <functional> // std::equal_to
21 
22 namespace std
23 {
24 
25  // http://stackoverflow.com/questions/2590677
26  template <class T>
27  inline void hash_combine(std::size_t& seed, const T& v)
28  {
29  std::hash<T> hasher;
30  seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
31  }
32 
33 }
34 
35 namespace awali { namespace utils {
36 
40  template <typename ValueSet>
41  class equal_to : public std::equal_to<typename ValueSet::value_t>
42  {
43  public:
44  using valueset_t = ValueSet;
45  using value_t = typename valueset_t::value_t;
46 
47  bool operator()(const value_t& v1, const value_t& v2) const
48  {
49  return valueset_t::equals(v1, v2);
50  }
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  std::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 = std::size_t;
72  };
73 
74  // Following the naming convention of Boost.
75  template <class T>
76  inline std::size_t hash_value(const T& v)
77  {
78  std::hash<T> hasher;
79  return hasher(v);
80  }
81 
82  // Following the naming convention of Boost.
83  template<typename F, typename S>
84  inline std::size_t hash_value(const std::pair<F,S>& p)
85  {
86  std::size_t res = 0;
87  std::hash_combine(res, hash_value(p.first));
88  std::hash_combine(res, hash_value(p.second));
89  return res;
90  }
91 }}//end of ns awali::utils
92 
93 #endif // !AWALI_MISC_HASH_HH
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: hash.hh:42
typename valueset_t::value_t value_t
Definition: hash.hh:45
bool operator()(const value_t &v1, const value_t &v2) const
Definition: hash.hh:47
ValueSet valueset_t
Definition: hash.hh:44
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: hash.hh:58
ValueSet valueset_t
Definition: hash.hh:60
std::size_t operator()(const value_t &v) const
Definition: hash.hh:63
std::size_t result_type
Definition: hash.hh:70
typename valueset_t::value_t value_t
Definition: hash.hh:61
value_t argument_type
Definition: hash.hh:71
RatExpSet::ratexp_t equals(const RatExpSet &rs, const typename RatExpSet::ratexp_t &v)
Definition: equal_visit.hh:153
std::size_t hash_value(const T &v)
Definition: hash.hh:76
Main namespace of Awali.
Definition: ato.hh:22
void hash_combine(std::size_t &seed, const T &v)
Definition: hash.hh:27