Awali
Another Weighted Automata library
hash.hxx
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2021 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_CORE_RAT_HASH_HXX
18 # define AWALI_CORE_RAT_HASH_HXX
19 
20 #include <awali/utils/hash.hh>
21 
22 namespace awali {
23  namespace sttc {
24  namespace rat {
25 
26 # define DEFINE \
27  template <typename RatExpSet> \
28  inline \
29  auto \
30  hash<RatExpSet>
31 
32 # define VISIT(Type) \
33  DEFINE::visit(const Type ## _t& v) \
34  -> void
35 
36 
37  VISIT(atom) {
38  visit_nullary(v);
39  std::hash_combine(res_, RatExpSet::labelset_t::hash(v.value()));
40  }
41 
42  VISIT(lweight)
43  {
44  visit_weight_node(v);
45  }
46 
47  VISIT(rweight)
48  {
49  visit_weight_node(v);
50  }
51 
52  template <typename RatExpSet>
53  inline
54  void
55  hash<RatExpSet>::combine_type(const node_t& node)
56  {
57  std::hash_combine(res_, int(node.type()));
58  }
59 
60  template <typename RatExpSet>
61  inline
62  void
63  hash<RatExpSet>::visit_nullary(const node_t &n)
64  {
65  combine_type(n);
66  }
67 
68  template <typename RatExpSet>
69  template <type_t Type>
70  inline
71  void
72  hash<RatExpSet>::visit_unary(const unary_t<Type>& n)
73  {
74  combine_type(n);
75  n.sub()->accept(*this);
76  }
77 
78  template <typename RatExpSet>
79  template <type_t Type>
80  inline
81  void
82  hash<RatExpSet>::visit_weight_node(const weight_node_t<Type>& n)
83  {
84  combine_type(n);
85  std::hash_combine(res_, RatExpSet::weightset_t::hash(n.weight()));
86  n.sub()->accept(*this);
87  }
88 
89  template <typename RatExpSet>
90  template <type_t Type>
91  inline
92  void
93  hash<RatExpSet>::visit_variadic(const variadic_t<Type>& n)
94  {
95  combine_type(n);
96  for (auto child : n)
97  child->accept(*this);
98  }
99 # undef VISIT
100 # undef DEFINE
101 
102  } // namespace rat
103  }
104 }//end of ns awali::stc
105 
106 #endif // !AWALI_CORE_RAT_HASH_HXX
#define VISIT(Type)
Definition: hash.hxx:32
weight_node< type_t::lweight, Label, Weight > lweight
Definition: fwd.hh:164
weight_node< type_t::rweight, Label, Weight > rweight
Definition: fwd.hh:167
Main namespace of Awali.
Definition: ato.hh:22
void hash_combine(std::size_t &seed, const T &v)
Definition: hash.hh:27