Vcsn  2.8
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 
81  {
83  combine_(hash_value(v.name_get()));
84  }
85 
86  using tuple_t = typename super_t::tuple_t;
87 
88  template <typename = void>
89  struct visit_tuple
90  {
91  using tupleset_t = typename expressionset_t::template as_tupleset_t<>;
92  size_t operator()(const tuple_t& v)
93  {
94  return tupleset_t::hash(v.sub());
95  }
96  };
97 
98  void visit(const tuple_t& v, std::true_type) override
99  {
100  detail::static_if<context_t::is_lat>
101  ([this](auto&& v)
102  {
103  combine_type_(v);
104  combine_(visit_tuple<decltype(v)>{}(v));
105  })
106  (v);
107  }
108 
110  template <rat::exp::type_t Type>
111  void visit_(const constant_t<Type>& v)
112  {
113  combine_type_(v);
114  }
115 
117  template <rat::exp::type_t Type>
118  void visit_(const unary_t<Type>& v)
119  {
120  combine_type_(v);
121  v.sub()->accept(*this);
122  }
123 
125  template <rat::exp::type_t Type>
126  void visit_(const variadic_t<Type>& v)
127  {
128  combine_type_(v);
129  for (const auto& child : v)
130  child->accept(*this);
131  }
132 
134  template <rat::exp::type_t Type>
135  void visit_(const weight_node_t<Type>& v)
136  {
137  combine_type_(v);
138  combine_(ExpSet::weightset_t::hash(v.weight()));
139  v.sub()->accept(*this);
140  }
141 
144  size_t res_;
145  };
146  } // namespace rat
147 } // namespace vcsn
size_t res_
The result, which must be updated incrementally.
Definition: hash.hh:144
VCSN_RAT_VISIT(one, v)
Definition: hash.hh:67
void visit_(const variadic_t< Type > &v)
Traverse an n-ary node.
Definition: hash.hh:126
typename super_t::node_t node_t
Actual node, without indirection.
Definition: hash.hh:20
typename super_t::tuple_t tuple_t
Definition: hash.hh:86
context_t_of< expressionset_t > context_t
Definition: hash.hh:16
An inner node with multiple children.
Definition: expression.hh:119
void combine_type_(const node_t &node)
Update res_ by hashing the node type.
Definition: hash.hh:54
typename expressionset_t::template as_tupleset_t<> tupleset_t
Definition: hash.hh:91
VCSN_RAT_VISIT(rweight, v)
Definition: hash.hh:68
VCSN_RAT_VISIT(infiltrate, v)
Definition: hash.hh:63
typename super_t::template unary_t< Type > unary_t
Definition: hash.hh:27
void visit(const tuple_t &v, std::true_type) override
Definition: hash.hh:98
VCSN_RAT_VISIT(lweight, v)
Definition: hash.hh:65
VCSN_RAT_VISIT(compose, v)
Definition: hash.hh:61
void visit_(const constant_t< Type > &v)
Traverse a nullary node.
Definition: hash.hh:111
VCSN_RAT_VISIT(transposition, v)
Definition: hash.hh:71
VCSN_RAT_VISIT(complement, v)
Definition: hash.hh:60
VCSN_RAT_VISIT(shuffle, v)
Definition: hash.hh:69
typename super_t::template weight_node_t< Type > weight_node_t
Definition: hash.hh:31
An inner node to name the subexpression.
Definition: expression.hh:290
typename detail::context_t_of_impl< base_t< ValueSet > >::type context_t_of
Definition: traits.hh:61
VCSN_RAT_VISIT(add, v)
Definition: hash.hh:59
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:45
VCSN_RAT_VISIT(zero, v)
Definition: hash.hh:72
Definition: a-star.hh:8
void visit_(const weight_node_t< Type > &v)
Traverse a weight node (lweight, rweight).
Definition: hash.hh:135
void combine_(size_t h)
Update res_ with the hash.
Definition: hash.hh:47
ExpSet expressionset_t
Definition: hash.hh:14
The abstract parameterized, root for all rational expression types.
Definition: expression.hh:81
void visit_(const unary_t< Type > &v)
Traverse a unary node.
Definition: hash.hh:118
typename node_t::value_t expression_t
A shared_ptr to node_t.
Definition: hash.hh:22
VCSN_RAT_VISIT(star, v)
Definition: hash.hh:70
typename super_t::template constant_t< Type > constant_t
Definition: hash.hh:25
An inner node implementing a weight.
Definition: expression.hh:256
typename expressionset_t::const_visitor super_t
Definition: hash.hh:17
size_t operator()(const tuple_t &v)
Definition: hash.hh:92
static constexpr const char * me()
Name of this algorithm, for error messages.
Definition: hash.hh:34
return v
Definition: multiply.hh:362
VCSN_RAT_VISIT(atom, v)
Definition: hash.hh:74
void hash_combine(std::size_t &seed, const T &v)
Definition: functional.hh:63
VCSN_RAT_VISIT(conjunction, v)
Definition: hash.hh:62
typename super_t::template variadic_t< Type > variadic_t
Definition: hash.hh:29
VCSN_RAT_VISIT(ldivide, v)
Definition: hash.hh:64
size_t operator()(const expression_t &v)
Entry point: return the hash of v.
Definition: hash.hh:37
VCSN_RAT_VISIT(mul, v)
Definition: hash.hh:66
VCSN_RAT_VISIT(name, v)
Definition: hash.hh:80