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