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