Vcsn  2.3a
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 <bool = context_t::is_lat,
82  typename Dummy = 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  template <typename Dummy>
93  struct visit_tuple<false, Dummy>
94  {
95  size_t operator()(const tuple_t&)
96  {
98  }
99  };
100 
101  void visit(const tuple_t& v, std::true_type) override
102  {
103  combine_type_(v);
105  }
106 
108  template <rat::exp::type_t Type>
109  void visit_(const constant_t<Type>& v)
110  {
111  combine_type_(v);
112  }
113 
115  template <rat::exp::type_t Type>
116  void visit_(const unary_t<Type>& v)
117  {
118  combine_type_(v);
119  v.sub()->accept(*this);
120  }
121 
123  template <rat::exp::type_t Type>
124  void visit_(const variadic_t<Type>& v)
125  {
126  combine_type_(v);
127  for (const auto& child : v)
128  child->accept(*this);
129  }
130 
132  template <rat::exp::type_t Type>
133  void visit_(const weight_node_t<Type>& v)
134  {
135  combine_type_(v);
136  combine_(ExpSet::weightset_t::hash(v.weight()));
137  v.sub()->accept(*this);
138  }
139 
142  size_t res_;
143  };
144  } // namespace rat
145 } // namespace vcsn
VCSN_RAT_VISIT(ldivide, v)
Definition: hash.hh:63
size_t operator()(const tuple_t &v)
Definition: hash.hh:86
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:133
VCSN_RAT_VISIT(zero, v)
Definition: hash.hh:71
return v
Definition: multiply.hh:361
typename super_t::node_t node_t
Actual node, without indirection.
Definition: hash.hh:19
Definition: a-star.hh:8
void hash_combine(std::size_t &seed, const T &v)
Definition: functional.hh:48
typename expressionset_t::template as_tupleset_t<> tupleset_t
Definition: hash.hh:85
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
An inner node implementing a weight.
Definition: expression.hh:264
void visit_(const variadic_t< Type > &v)
Traverse an n-ary node.
Definition: hash.hh:124
typename super_t::template unary_t< Type > unary_t
Definition: hash.hh:26
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:101
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:116
size_t operator()(const tuple_t &)
Definition: hash.hh:95
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
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
typename super_t::template constant_t< Type > constant_t
Definition: hash.hh:24
void visit_(const constant_t< Type > &v)
Traverse a nullary node.
Definition: hash.hh:109
VCSN_RAT_VISIT(infiltrate, v)
Definition: hash.hh:62
typename super_t::template variadic_t< Type > variadic_t
Definition: hash.hh:28
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
void combine_(size_t h)
Update res_ with the hash.
Definition: hash.hh:46
VCSN_RAT_VISIT(conjunction, v)
Definition: hash.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:142