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_CORE_RAT_HASH_HH
18 # define AWALI_CORE_RAT_HASH_HH
19 
22 #include <awali/sttc/misc/cast.hh>
23 
24 namespace awali {
25  namespace sttc {
26  namespace rat {
27  template <typename RatExpSet>
28  class hash : public RatExpSet::const_visitor {
29  public:
30  using ratexpset_t = RatExpSet;
32  using weight_t = typename context_t::weightset_t::value_t;
33  using super_type = typename ratexpset_t::const_visitor;
34  using node_t = typename super_type::node_t;
35  using inner_t = typename super_type::inner_t;
36  template <type_t Type>
37  using variadic_t = typename super_type::template variadic_t<Type>;
38  template <type_t Type>
39  using unary_t = typename super_type::template unary_t<Type>;
40  template <type_t Type>
41  using weight_node_t = typename super_type::template weight_node_t<Type>;
42  using leaf_t = typename super_type::leaf_t;
43 
44 
46  size_t operator()(const node_t& v) {
47  res_ = 0;
48  v.accept(*this);
49  return res_;
50  }
51 
53  size_t operator()(const std::shared_ptr<const node_t>& v) {
54  return operator()(*v);
55  }
56 
57  private:
58 
59 # define DEFINE(Type) \
60  using Type ## _t = typename super_type::Type ## _t; \
61  virtual void visit(const Type ## _t& v) override
62 
63  DEFINE(atom);
64  DEFINE(complement) { visit_unary(v); }
65  DEFINE(conjunction) { visit_variadic(v); }
66  DEFINE(ldiv) { visit_variadic(v); }
67  DEFINE(lweight);
68  DEFINE(one) { visit_nullary(v); }
69  DEFINE(prod) { visit_variadic(v); }
70  DEFINE(rweight);
71  DEFINE(shuffle) { visit_variadic(v); }
72  DEFINE(star) { visit_unary(v); }
73  DEFINE(sum) { visit_variadic(v); }
74  DEFINE(transposition){ visit_unary(v); }
75  DEFINE(zero) { visit_nullary(v); }
76 
77 # undef DEFINE
78 
80  void combine_type(const node_t& node);
81 
83  void visit_nullary(const node_t& v);
84 
86  template <rat::exp::type_t Type>
87  void visit_unary(const unary_t<Type>& v);
88 
90  template <rat::exp::type_t Type>
91  void visit_variadic(const variadic_t<Type>& v);
92 
94  template <rat::exp::type_t Type>
95  void visit_weight_node(const weight_node_t<Type>& v);
96 
97  size_t res_;
98  };
99  } // namespace rat
100  }
101 }//end of ns awali::stc
102 
104 
105 #endif // !AWALI_CORE_RAT_HASH_HH
Definition: hash.hh:28
size_t operator()(const std::shared_ptr< const node_t > &v)
Entry point: return the hash of v.
Definition: hash.hh:53
typename super_type::template variadic_t< Type > variadic_t
Definition: hash.hh:37
typename super_type::template weight_node_t< Type > weight_node_t
Definition: hash.hh:41
typename super_type::node_t node_t
Definition: hash.hh:34
size_t operator()(const node_t &v)
Entry point: return the hash of v.
Definition: hash.hh:46
typename super_type::inner_t inner_t
Definition: hash.hh:35
typename super_type::template unary_t< Type > unary_t
Definition: hash.hh:39
context_t_of< ratexpset_t > context_t
Definition: hash.hh:31
typename context_t::weightset_t::value_t weight_t
Definition: hash.hh:32
typename super_type::leaf_t leaf_t
Definition: hash.hh:42
RatExpSet ratexpset_t
Definition: hash.hh:30
typename ratexpset_t::const_visitor super_type
Definition: hash.hh:33
variadic< type_t::sum, Label, Weight > sum
Definition: fwd.hh:154
weight_node< type_t::lweight, Label, Weight > lweight
Definition: fwd.hh:164
unary< type_t::star, Label, Weight > star
Definition: fwd.hh:129
constant< type_t::zero, Label, Weight > zero
Definition: fwd.hh:113
variadic< type_t::ldiv, Label, Weight > ldiv
Definition: fwd.hh:148
variadic< type_t::conjunction, Label, Weight > conjunction
Definition: fwd.hh:145
variadic< type_t::shuffle, Label, Weight > shuffle
Definition: fwd.hh:151
weight_node< type_t::rweight, Label, Weight > rweight
Definition: fwd.hh:167
unary< type_t::transposition, Label, Weight > transposition
Definition: fwd.hh:132
constant< type_t::one, Label, Weight > one
Definition: fwd.hh:116
unary< type_t::complement, Label, Weight > complement
Definition: fwd.hh:126
variadic< type_t::prod, Label, Weight > prod
Definition: fwd.hh:142
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
Main namespace of Awali.
Definition: ato.hh:22
#define DEFINE(Type)
Definition: hash.hh:59