Vcsn  2.2
Be Rational
hash.hh
Go to the documentation of this file.
1 #pragma once
2 
4 
5 namespace vcsn
6 {
7  namespace rat
8  {
9  template <typename ExpSet>
10  class hash
11  : public ExpSet::const_visitor
12  {
13  public:
14  using expressionset_t = ExpSet;
16  using weight_t = typename context_t::weightset_t::value_t;
17  using super_t = typename expressionset_t::const_visitor;
18  using node_t = typename super_t::node_t;
19  using inner_t = typename super_t::inner_t;
20  template <type_t Type>
21  using variadic_t = typename super_t::template variadic_t<Type>;
22  template <type_t Type>
23  using unary_t = typename super_t::template unary_t<Type>;
24  template <type_t Type>
25  using weight_node_t = typename super_t::template weight_node_t<Type>;
26  using leaf_t = typename super_t::leaf_t;
27 
29  constexpr static const char* me() { return "hash"; }
30 
32  size_t
34  {
35  res_ = 0;
36  v.accept(*this);
37  return res_;
38  }
39 
41  size_t
42  operator()(const std::shared_ptr<const node_t>& v)
43  {
44  return operator()(*v);
45  }
46 
47  private:
48 
63 
64  using tuple_t = typename super_t::tuple_t;
65 
66  template <bool = context_t::is_lat,
67  typename Dummy = void>
68  struct visit_tuple
69  {
70  using tupleset_t = typename expressionset_t::template as_tupleset_t<>;
71  size_t operator()(const tuple_t& v)
72  {
73  return tupleset_t::hash(v.sub());
74  }
75  };
76 
77  template <typename Dummy>
78  struct visit_tuple<false, Dummy>
79  {
80  size_t operator()(const tuple_t&)
81  {
83  }
84  };
85 
86  void visit(const tuple_t& v, std::true_type) override
87  {
88  res_ = visit_tuple<>{}(v);
89  }
90 
92  void combine_type(const node_t& node);
93 
95  void visit_nullary(const node_t& v);
96 
98  template <rat::exp::type_t Type>
99  void visit_unary(const unary_t<Type>& v);
100 
102  template <rat::exp::type_t Type>
103  void visit_variadic(const variadic_t<Type>& v);
104 
106  template <rat::exp::type_t Type>
107  void visit_weight_node(const weight_node_t<Type>& v);
108 
109  size_t res_;
110  };
111  } // namespace rat
112 } // namespace vcsn
113 
114 #include <vcsn/core/rat/hash.hxx>
void combine_type(const node_t &node)
Update res_ by hashing the node type; this is needed for any node.
Definition: hash.hxx:38
void visit(const tuple_t &v, std::true_type) override
Definition: hash.hh:86
VCSN_RAT_VISIT(complement, v)
Definition: hash.hh:50
typename super_t::template unary_t< Type > unary_t
Definition: hash.hh:23
VCSN_RAT_VISIT(shuffle, v)
Definition: hash.hh:58
void visit_unary(const unary_t< Type > &v)
Traverse a unary node (*, {c}).
Definition: hash.hxx:55
Definition: a-star.hh:8
size_t operator()(const node_t &v)
Entry point: return the hash of v.
Definition: hash.hh:33
void visit_weight_node(const weight_node_t< Type > &v)
Traverse a weight node (lweight, rweight).
Definition: hash.hxx:65
typename super_t::template weight_node_t< Type > weight_node_t
Definition: hash.hh:25
typename super_t::template variadic_t< Type > variadic_t
Definition: hash.hh:21
typename super_t::inner_t inner_t
Definition: hash.hh:19
VCSN_RAT_VISIT(infiltration, v)
Definition: hash.hh:52
typename super_t::leaf_t leaf_t
Definition: hash.hh:26
typename expressionset_t::template as_tupleset_t<> tupleset_t
Definition: hash.hh:70
VCSN_RAT_VISIT(atom, v)
An inner node with multiple children.
Definition: expression.hh:118
void visit_variadic(const variadic_t< Type > &v)
Traverse an n-ary node (+, concatenation, &, :).
Definition: hash.hxx:76
VCSN_RAT_VISIT(sum, v)
Definition: hash.hh:60
size_t res_
Definition: hash.hh:109
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
typename super_t::node_t node_t
Definition: hash.hh:18
size_t operator()(const tuple_t &v)
Definition: hash.hh:71
VCSN_RAT_VISIT(transposition, v)
Definition: hash.hh:61
VCSN_RAT_VISIT(zero, v)
Definition: hash.hh:62
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:53
VCSN_RAT_VISIT(star, v)
Definition: hash.hh:59
An inner node implementing a weight.
Definition: expression.hh:264
context_t_of< expressionset_t > context_t
Definition: hash.hh:15
VCSN_RAT_VISIT(conjunction, v)
Definition: hash.hh:51
void visit_nullary(const node_t &v)
Traverse a nullary node (atom, \z, \e).
Definition: hash.hxx:46
size_t operator()(const std::shared_ptr< const node_t > &v)
Entry point: return the hash of v.
Definition: hash.hh:42
typename context_t::weightset_t::value_t weight_t
Definition: hash.hh:16
VCSN_RAT_VISIT(prod, v)
Definition: hash.hh:56
ExpSet expressionset_t
Definition: hash.hh:14
typename super_t::tuple_t tuple_t
Definition: hash.hh:64
VCSN_RAT_VISIT(ldiv, v)
Definition: hash.hh:53
size_t operator()(const tuple_t &)
Definition: hash.hh:80
static constexpr const char * me()
Name of this algorithm, for error messages.
Definition: hash.hh:29
The abstract parameterized, root for all rational expression types.
Definition: expression.hh:80
VCSN_RAT_VISIT(one, v)
Definition: hash.hh:55
typename expressionset_t::const_visitor super_t
Definition: hash.hh:17