Vcsn  2.3
Be Rational
info.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm> // std::max
4 #include <iostream>
5 
7 
8 namespace vcsn
9 {
10  namespace rat
11  {
12 
16  template <typename ExpSet>
17  class info
18  : public ExpSet::const_visitor
19  {
20  public:
21  using expressionset_t = ExpSet;
22  using super_t = typename expressionset_t::const_visitor;
23  using self_t = info;
24 
26 
28  using node_t = typename super_t::node_t;
30  using expression_t = typename node_t::value_t;
31 
33  void operator()(const expression_t& v)
34  {
35  clear();
36  v->accept(*this);
37  }
38 
40  void clear()
41  {
42  add = 0;
43  atom = 0;
44  complement = 0;
45  compose = 0;
46  conjunction = 0;
47  depth = 0;
48  infiltrate = 0;
49  ldivide = 0;
50  lweight = 0;
51  mul = 0;
52  one = 0;
53  rweight = 0;
54  shuffle = 0;
55  star = 0;
56  transposition = 0;
57  tuple = 0;
58  zero = 0;
59  }
60 
62  template <typename OtherExpSet>
64  {
65  add += other.add;
66  atom += other.atom;
67  complement += other.complement;
68  compose += other.compose;
69  conjunction += other.conjunction;
70  depth = std::max(depth, other.depth);
71  infiltrate += other.infiltrate;
72  ldivide += other.ldivide;
73  lweight += other.lweight;
74  mul += other.mul;
75  one += other.one;
76  rweight += other.rweight;
77  shuffle += other.shuffle;
78  star += other.star;
79  transposition += other.transposition;
80  tuple += other.tuple;
81  zero += other.zero;
82  return *this;
83  }
84 
86  constexpr static const char* me() { return "info"; }
87 
89  size_t depth = 0;
90 
91 #define DEFINE(Type) \
92  public: \
93  size_t Type = 0; \
94  private: \
95  VCSN_RAT_VISIT(Type, v)
96 
97  DEFINE(add) { ++add; visit_(v); }
98  DEFINE(atom) { ++atom; (void) v; depth = 0; }
104  DEFINE(lweight) { ++lweight; v.sub()->accept(*this); ++depth; }
105  DEFINE(mul) { ++mul; visit_(v);}
106  DEFINE(one) { ++one; (void) v; depth = 0; }
107  DEFINE(rweight) { ++rweight; v.sub()->accept(*this); ++depth; }
109  DEFINE(star) { ++star; visit_(v); }
111  DEFINE(zero) { ++zero; (void) v; depth = 0; }
112 #undef DEFINE
113 
114  private:
115  template <type_t Type>
117 
119  template <rat::exp::type_t Type>
120  void visit_(const unary_t<Type>& v)
121  {
122  v.sub()->accept(*this);
123  ++depth;
124  }
125 
126  template <type_t Type>
128 
130  template <exp::type_t Type>
131  void visit_(const variadic_t<Type>& v)
132  {
133  size_t d = 0;
134  for (const auto& c: v)
135  {
136  c->accept(*this);
137  d = std::max(d, depth);
138  }
139  depth = d + 1;
140  }
141 
142  /*---------.
143  | tuple. |
144  `---------*/
145 
146  public:
148  size_t tuple = 0;
149  using tuple_t = typename super_t::tuple_t;
150 
151  private:
152  template <bool = context_t::is_lat,
153  typename Dummy = void>
154  struct visit_tuple
155  {
157  template <size_t I>
158  void info_(const tuple_t& v)
159  {
160  using expset_t = typename expressionset_t::template project_t<I>;
161  visitor_ += make_info<expset_t>(std::get<I>(v.sub()));
162  }
163 
165  template <size_t... I>
167  {
168  using swallow = int[];
169  (void) swallow
170  {
171  (info_<I>(v),
172  0)...
173  };
174  }
175 
177  void operator()(const tuple_t& v)
178  {
180  ++visitor_.tuple;
181  ++visitor_.depth;
182  }
184  };
185 
186  template <typename Dummy>
187  struct visit_tuple<false, Dummy>
188  {
189  void operator()(const tuple_t&)
190  {
192  }
194  };
195 
196  void visit(const tuple_t& v, std::true_type) override
197  {
198  visit_tuple<>{*this}(v);
199  }
200  };
201 
202  template <typename ExpSet>
203  info<ExpSet> make_info(const typename ExpSet::value_t& r)
204  {
205  auto s = info<ExpSet>{};
206  s(r);
207  return s;
208  }
209  } // namespace rat
210 } // namespace vcsn
return v
Definition: multiply.hh:361
ExpSet expressionset_t
Definition: info.hh:21
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
size_t infiltrate
Definition: info.hh:102
size_t one
Definition: info.hh:106
context_t_of< expressionset_t > context_t
Definition: info.hh:25
size_t depth
Depth of the tree.
Definition: info.hh:89
static constexpr const char * me()
Name of this algorithm, for error messages.
Definition: info.hh:86
#define DEFINE(Type)
Definition: info.hh:91
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
An inner node implementing a weight.
Definition: expression.hh:264
info< ExpSet > make_info(const typename ExpSet::value_t &r)
Definition: info.hh:203
size_t star
Definition: info.hh:109
size_t conjunction
Definition: info.hh:101
Gather information of the number of the different node types.
Definition: fwd.hh:20
size_t zero
Definition: info.hh:111
size_t complement
Definition: info.hh:99
size_t shuffle
Definition: info.hh:108
void clear()
Reset the visitor.
Definition: info.hh:40
size_t atom
Definition: info.hh:98
info & operator+=(const info< OtherExpSet > &other)
Add the result from another info operator.
Definition: info.hh:63
size_t add
Definition: info.hh:97
typename super_t::tuple_t tuple_t
Definition: info.hh:149
Definition: a-star.hh:8
size_t transposition
Definition: info.hh:110
const value_t sub() const
Definition: expression.hxx:137
void operator()(const tuple_t &v)
Entry point.
Definition: info.hh:177
void info_(const tuple_t &v)
Info about tape I.
Definition: info.hh:158
void visit(const tuple_t &v, std::true_type) override
Definition: info.hh:196
void operator()(const expression_t &v)
Entry point: compute info about v.
Definition: info.hh:33
size_t tuple
Number of tuple operators.
Definition: info.hh:148
size_t mul
Definition: info.hh:105
size_t lweight
Definition: info.hh:104
Implementation of nodes of tuple of rational expressions.
Definition: expression.hh:182
size_t compose
Definition: info.hh:100
void visit_(const unary_t< Type > &v)
Factor the visitation of unary nodes.
Definition: info.hh:120
typename super_t::node_t node_t
Actual node, without indirection.
Definition: info.hh:28
size_t rweight
Definition: info.hh:107
An inner node with multiple children.
Definition: expression.hh:118
size_t ldivide
Definition: info.hh:103
std::ostream & info(const Aut &aut, std::ostream &out=std::cout, bool detailed=false)
Print info about an automaton.
Definition: info.hh:73
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
void info_(const tuple_t &v, detail::index_sequence< I... >)
Info all the tapes.
Definition: info.hh:166
typename expressionset_t::const_visitor super_t
Definition: info.hh:22
void visit_(const variadic_t< Type > &v)
Factor the visitation of variadic nodes.
Definition: info.hh:131
typename node_t::value_t expression_t
A shared_ptr to node_t.
Definition: info.hh:30