Vcsn  2.4
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 super_t = typename expressionset_t::const_visitor;
17 
19  using node_t = typename super_t::node_t;
21  using expression_t = typename node_t::value_t;
22 
23  template <type_t Type>
24  using constant_t = typename super_t::template constant_t<Type>;
25  template <type_t Type>
26  using unary_t = typename super_t::template unary_t<Type>;
27  template <type_t Type>
28  using variadic_t = typename super_t::template variadic_t<Type>;
29  template <type_t Type>
30  using weight_node_t = typename super_t::template weight_node_t<Type>;
31 
33  constexpr static const char* me() { return "hash"; }
34 
36  size_t operator()(const expression_t& v)
37  {
38  res_ = 0;
39  v->accept(*this);
40  return res_;
41  }
42 
43  private:
44 
46  void combine_(size_t h)
47  {
48  hash_combine(res_, h);
49  }
50 
53  void combine_type_(const node_t& node)
54  {
55  combine_(int(node.type()));
56  }
57 
72 
74  {
76  combine_(ExpSet::labelset_t::hash(v.value()));
77  }
78 
79  using tuple_t = typename super_t::tuple_t;
80 
81  template <typename = void>
82  struct visit_tuple
83  {
84  using tupleset_t = typename expressionset_t::template as_tupleset_t<>;
85  size_t operator()(const tuple_t& v)
86  {
87  return tupleset_t::hash(v.sub());
88  }
89  };
90 
91  void visit(const tuple_t& v, std::true_type) override
92  {
93  detail::static_if<context_t::is_lat>
94  ([this](auto&& v)
95  {
96  combine_type_(v);
97  combine_(visit_tuple<decltype(v)>{}(v));
98  })
99  (v);
100  }
101 
103  template <rat::exp::type_t Type>
104  void visit_(const constant_t<Type>& v)
105  {
106  combine_type_(v);
107  }
108 
110  template <rat::exp::type_t Type>
111  void visit_(const unary_t<Type>& v)
112  {
113  combine_type_(v);
114  v.sub()->accept(*this);
115  }
116 
118  template <rat::exp::type_t Type>
119  void visit_(const variadic_t<Type>& v)
120  {
121  combine_type_(v);
122  for (const auto& child : v)
123  child->accept(*this);
124  }
125 
127  template <rat::exp::type_t Type>
128  void visit_(const weight_node_t<Type>& v)
129  {
130  combine_type_(v);
131  combine_(ExpSet::weightset_t::hash(v.weight()));
132  v.sub()->accept(*this);
133  }
134 
137  size_t res_;
138  };
139  } // namespace rat
140 } // namespace vcsn
VCSN_RAT_VISIT(ldivide, v)
Definition: hash.hh:63
VCSN_RAT_VISIT(one, v)
Definition: hash.hh:66
void visit_(const weight_node_t< Type > &v)
Traverse a weight node (lweight, rweight).
Definition: hash.hh:128
VCSN_RAT_VISIT(zero, v)
Definition: hash.hh:71
typename super_t::node_t node_t
Actual node, without indirection.
Definition: hash.hh:19
An inner node implementing a weight.
Definition: expression.hh:255
context_t_of< expressionset_t > context_t
Definition: hash.hh:15
VCSN_RAT_VISIT(atom, v)
Definition: hash.hh:73
size_t operator()(const expression_t &v)
Entry point: return the hash of v.
Definition: hash.hh:36
typename super_t::tuple_t tuple_t
Definition: hash.hh:79
VCSN_RAT_VISIT(rweight, v)
Definition: hash.hh:67
static constexpr const char * me()
Name of this algorithm, for error messages.
Definition: hash.hh:33
Definition: a-star.hh:8
void visit_(const variadic_t< Type > &v)
Traverse an n-ary node.
Definition: hash.hh:119
typename super_t::template unary_t< Type > unary_t
Definition: hash.hh:26
typename expressionset_t::template as_tupleset_t<> tupleset_t
Definition: hash.hh:84
VCSN_RAT_VISIT(lweight, v)
Definition: hash.hh:64
void combine_type_(const node_t &node)
Update res_ by hashing the node type.
Definition: hash.hh:53
VCSN_RAT_VISIT(transposition, v)
Definition: hash.hh:70
typename expressionset_t::const_visitor super_t
Definition: hash.hh:16
void visit(const tuple_t &v, std::true_type) override
Definition: hash.hh:91
VCSN_RAT_VISIT(star, v)
Definition: hash.hh:69
VCSN_RAT_VISIT(add, v)
Definition: hash.hh:58
void visit_(const unary_t< Type > &v)
Traverse a unary node.
Definition: hash.hh:111
An inner node with multiple children.
Definition: expression.hh:118
typename super_t::template weight_node_t< Type > weight_node_t
Definition: hash.hh:30
return v
Definition: multiply.hh:361
typename super_t::template constant_t< Type > constant_t
Definition: hash.hh:24
size_t operator()(const tuple_t &v)
Definition: hash.hh:85
void visit_(const constant_t< Type > &v)
Traverse a nullary node.
Definition: hash.hh:104
VCSN_RAT_VISIT(infiltrate, v)
Definition: hash.hh:62
typename super_t::template variadic_t< Type > variadic_t
Definition: hash.hh:28
void combine_(size_t h)
Update res_ with the hash.
Definition: hash.hh:46
void hash_combine(std::size_t &seed, const T &v)
Definition: functional.hh:48
VCSN_RAT_VISIT(conjunction, v)
Definition: hash.hh:61
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
typename node_t::value_t expression_t
A shared_ptr to node_t.
Definition: hash.hh:21
ExpSet expressionset_t
Definition: hash.hh:14
VCSN_RAT_VISIT(mul, v)
Definition: hash.hh:65
VCSN_RAT_VISIT(compose, v)
Definition: hash.hh:60
VCSN_RAT_VISIT(complement, v)
Definition: hash.hh:59
The abstract parameterized, root for all rational expression types.
Definition: expression.hh:80
VCSN_RAT_VISIT(shuffle, v)
Definition: hash.hh:68
size_t res_
The result, which must be updated incrementally.
Definition: hash.hh:137