Vcsn  2.4
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  one = 0;
54  rweight = 0;
55  shuffle = 0;
56  star = 0;
57  transposition = 0;
58  tuple = 0;
59  zero = 0;
60  }
61 
63  template <typename OtherExpSet>
65  {
66  add += other.add;
67  atom += other.atom;
68  complement += other.complement;
69  compose += other.compose;
70  conjunction += other.conjunction;
71  depth = std::max(depth, other.depth);
72  infiltrate += other.infiltrate;
73  ldivide += other.ldivide;
74  lweight += other.lweight;
75  mul += other.mul;
76  one += other.one;
77  rweight += other.rweight;
78  shuffle += other.shuffle;
79  star += other.star;
80  transposition += other.transposition;
81  tuple += other.tuple;
82  zero += other.zero;
83  return *this;
84  }
85 
87  constexpr static const char* me() { return "info"; }
88 
90  size_t depth = 0;
91 
92 #define DEFINE(Type) \
93  public: \
94  size_t Type = 0; \
95  private: \
96  VCSN_RAT_VISIT(Type, v)
97 
98  DEFINE(add) { ++add; visit_(v); }
99  DEFINE(atom) { ++atom; (void) v; depth = 0; }
105  DEFINE(lweight) { ++lweight; v.sub()->accept(*this); ++depth; }
106  DEFINE(mul) { ++mul; visit_(v);}
107  DEFINE(one) { ++one; (void) v; depth = 0; }
108  DEFINE(rweight) { ++rweight; v.sub()->accept(*this); ++depth; }
110  DEFINE(star) { ++star; visit_(v); }
112  DEFINE(zero) { ++zero; (void) v; depth = 0; }
113 #undef DEFINE
114 
115  private:
116  template <type_t Type>
118 
120  template <rat::exp::type_t Type>
121  void visit_(const unary_t<Type>& v)
122  {
123  v.sub()->accept(*this);
124  ++depth;
125  }
126 
127  template <type_t Type>
129 
131  template <exp::type_t Type>
132  void visit_(const variadic_t<Type>& v)
133  {
134  size_t d = 0;
135  for (const auto& c: v)
136  {
137  c->accept(*this);
138  d = std::max(d, depth);
139  }
140  depth = d + 1;
141  }
142 
143  /*---------.
144  | tuple. |
145  `---------*/
146 
147  public:
149  size_t tuple = 0;
150  using tuple_t = typename super_t::tuple_t;
151 
152  private:
153  template <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  void visit(const tuple_t& v, std::true_type) override
187  {
188  detail::static_if<context_t::is_lat>
189  ([this](auto&& v){ visit_tuple<decltype(v)>{*this}(v); })
190  (v);
191  }
192  };
193 
194  template <typename ExpSet>
195  info<ExpSet> make_info(const typename ExpSet::value_t& r)
196  {
197  auto s = info<ExpSet>{};
198  s(r);
199  return s;
200  }
201  } // namespace rat
202 } // namespace vcsn
size_t lweight
Definition: info.hh:105
Implementation of nodes of tuple of rational expressions.
Definition: expression.hh:173
size_t compose
Definition: info.hh:101
static constexpr const char * me()
Name of this algorithm, for error messages.
Definition: info.hh:87
An inner node implementing a weight.
Definition: expression.hh:255
info< ExpSet > make_info(const typename ExpSet::value_t &r)
Definition: info.hh:195
void visit_(const variadic_t< Type > &v)
Factor the visitation of variadic nodes.
Definition: info.hh:132
Gather information of the number of the different node types.
Definition: fwd.hh:20
context_t_of< expressionset_t > context_t
Definition: info.hh:26
#define DEFINE(Type)
Definition: info.hh:92
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
typename node_t::value_t expression_t
A shared_ptr to node_t.
Definition: info.hh:31
size_t transposition
Definition: info.hh:111
ExpSet expressionset_t
Definition: info.hh:22
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
info & operator+=(const info< OtherExpSet > &other)
Add the result from another info operator.
Definition: info.hh:64
size_t one
Definition: info.hh:107
void visit(const tuple_t &v, std::true_type) override
Definition: info.hh:186
void operator()(const tuple_t &v)
Entry point.
Definition: info.hh:177
Definition: a-star.hh:8
void operator()(const expression_t &v)
Entry point: compute info about v.
Definition: info.hh:34
typename super_t::tuple_t tuple_t
Definition: info.hh:150
size_t depth
Depth of the tree.
Definition: info.hh:90
size_t zero
Definition: info.hh:112
size_t ldivide
Definition: info.hh:104
void visit_(const unary_t< Type > &v)
Factor the visitation of unary nodes.
Definition: info.hh:121
size_t shuffle
Definition: info.hh:109
const value_t sub() const
Definition: expression.hxx:125
void clear()
Reset the visitor.
Definition: info.hh:41
size_t conjunction
Definition: info.hh:102
size_t star
Definition: info.hh:110
void info_(const tuple_t &v)
Info about tape I.
Definition: info.hh:158
size_t atom
Definition: info.hh:99
An inner node with multiple children.
Definition: expression.hh:118
return v
Definition: multiply.hh:361
size_t rweight
Definition: info.hh:108
size_t add
Definition: info.hh:98
std::ostream & info(const Aut &aut, std::ostream &out=std::cout, unsigned details=2)
Print info about an automaton.
Definition: info.hh:73
typename super_t::node_t node_t
Actual node, without indirection.
Definition: info.hh:29
size_t infiltrate
Definition: info.hh:103
size_t complement
Definition: info.hh:100
void info_(const tuple_t &v, detail::index_sequence< I... >)
Info all the tapes.
Definition: info.hh:166
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
size_t tuple
Number of tuple operators.
Definition: info.hh:149
typename expressionset_t::const_visitor super_t
Definition: info.hh:23
size_t mul
Definition: info.hh:106