Vcsn  2.8
Be Rational
star-height.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/algos/project.hh>
5 
6 namespace vcsn
7 {
9  template <typename ExpSet>
10  unsigned
11  star_height(const ExpSet& es, const typename ExpSet::value_t& e);
12 
13  namespace detail
14  {
15  template <typename ExpSet>
17  : public ExpSet::const_visitor
18  {
19  public:
20  using expressionset_t = ExpSet;
21  using super_t = typename expressionset_t::const_visitor;
23  using expression_t = typename expressionset_t::value_t;
25  using node_t = typename super_t::node_t;
26 
28  constexpr static const char* me() { return "star_height"; }
29 
31  : rs_{rs}
32  {}
33 
35  unsigned operator()(const expression_t& v)
36  {
37  height_ = 0;
38  return recurse_(v);
39  }
40 
41  private:
43  unsigned recurse_(const expression_t& v)
44  {
45  unsigned res = 0;
46  std::swap(res, height_);
47  v->accept(*this);
48  std::swap(res, height_);
49  return res;
50  }
51 
52  VCSN_RAT_VISIT(add, v) { visit_(v); }
53  VCSN_RAT_VISIT(atom,) {}
54  VCSN_RAT_VISIT(complement, v) { visit_(v); }
55  VCSN_RAT_VISIT(compose, v) { visit_(v); }
57  VCSN_RAT_VISIT(infiltrate, v) { visit_(v); }
58  VCSN_RAT_VISIT(ldivide, v) { visit_(v); }
59  VCSN_RAT_VISIT(lweight, v) { v.sub()->accept(*this); }
60  VCSN_RAT_VISIT(mul, v) { visit_(v); }
61  VCSN_RAT_VISIT(name, e) { super_t::visit(e); }
62  VCSN_RAT_VISIT(one,) {}
63  VCSN_RAT_VISIT(rweight, v) { v.sub()->accept(*this); }
64  VCSN_RAT_VISIT(shuffle, v) { visit_(v); }
65  VCSN_RAT_VISIT(star, v) { visit_(v); ++height_; }
66  VCSN_RAT_VISIT(transposition, v){ visit_(v); }
67  VCSN_RAT_VISIT(zero,) {}
68 
69  template <rat::type_t Type>
70  using unary_t = typename super_t::template unary_t<Type>;
71 
73  template <rat::exp::type_t Type>
74  void visit_(const unary_t<Type>& v)
75  {
76  height_ = recurse_(v.sub());
77  }
78 
79  template <rat::type_t Type>
80  using variadic_t = typename super_t::template variadic_t<Type>;
81 
83  template <rat::type_t Type>
84  void visit_(const variadic_t<Type>& n)
85  {
86  height_ = 0;
87  // The height of an n-ary is the max of its heights.
88  for (const auto& c : n)
89  height_ = std::max(height_, recurse_(c));
90  }
91 
92  using tuple_t = typename super_t::tuple_t;
93 
94  private:
95  template <bool = context_t::is_lat,
96  typename Dummy = void>
97  struct visit_tuple
98  {
100  template <size_t I>
101  auto tape_(const tuple_t& v)
102  {
103  return star_height(project<I>(visitor_.rs_), std::get<I>(v.sub()));
104  }
105 
107  template <size_t... I>
109  {
110  return std::max({tape_<I>(v)...});
111  }
112 
114  auto operator()(const tuple_t& v)
115  {
117  }
119  };
120 
121  template <typename Dummy>
122  struct visit_tuple<false, Dummy>
123  {
124  unsigned operator()(const tuple_t&)
125  {
127  }
129  };
130 
131  void visit(const tuple_t& v, std::true_type) override
132  {
133  height_ = std::max(height_, visit_tuple<>{*this}(v));
134  }
135 
139  unsigned height_ = 0;
140  };
141  } // namespace detail
142 
143 
144  template <typename ExpSet>
145  unsigned
146  star_height(const ExpSet& es, const typename ExpSet::value_t& e)
147  {
149  return s(e);
150  }
151 
152  namespace dyn
153  {
154  namespace detail
155  {
157  template <typename ExpSet>
158  unsigned
160  {
161  const auto& e = exp->as<ExpSet>();
162  return ::vcsn::star_height(e.valueset(), e.value());
163  }
164  }
165  }
166 } // namespace vcsn
typename expressionset_t::value_t expression_t
Definition: star-height.hh:23
void visit_(const variadic_t< Type > &n)
Traverse variadic node.
Definition: star-height.hh:84
static constexpr const char * me()
Name of this algorithm, for error messages.
Definition: star-height.hh:28
typename super_t::node_t node_t
Definition: star-height.hh:25
unsigned height_
The current star height.
Definition: star-height.hh:139
auto operator()(const tuple_t &v)
Entry point.
Definition: star-height.hh:114
const expressionset_t & rs_
The expressionset.
Definition: star-height.hh:137
typename detail::context_t_of_impl< base_t< ValueSet > >::type context_t_of
Definition: traits.hh:61
void visit_(const unary_t< Type > &v)
Traverse unary node.
Definition: star-height.hh:74
typename super_t::tuple_t tuple_t
Definition: star-height.hh:92
unsigned star_height(const expression &exp)
Bridge.
Definition: star-height.hh:159
void visit(const tuple_t &v, std::true_type) override
Definition: star-height.hh:131
typename detail::labelset_t_of_impl< base_t< ValueSet > >::type labelset_t_of
Definition: traits.hh:63
typename super_t::template unary_t< Type > unary_t
Definition: star-height.hh:70
context_t_of< expressionset_t > context_t
Definition: star-height.hh:24
star_height_visitor(const expressionset_t &rs)
Definition: star-height.hh:30
Definition: a-star.hh:8
auto conjunction(const Aut &a, const Auts &... as)
Build the (accessible part of the) conjunction.
Definition: conjunction.hh:621
unsigned operator()(const expression_t &v)
The star height of v.
Definition: star-height.hh:35
auto tape_(const tuple_t &v)
The case of tape I.
Definition: star-height.hh:101
auto tapes_(const tuple_t &v, detail::index_sequence< I... >)
Info all the tapes.
Definition: star-height.hh:108
A static list of size_t.
Definition: tuple.hh:32
void swap(config::value &first, config::value &second)
unsigned recurse_(const expression_t &v)
Easy recursion: the star height of v, saving height_.
Definition: star-height.hh:43
typename expressionset_t::const_visitor super_t
Definition: star-height.hh:21
value_impl< detail::expression_tag > expression
Definition: fwd.hh:31
unsigned star_height(const ExpSet &es, const typename ExpSet::value_t &e)
Star height of an expression.
Definition: star-height.hh:146
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
VCSN_RAT_VISIT(transposition, v)
Definition: star-height.hh:66
return res
Definition: multiply.hh:399
typename super_t::template variadic_t< Type > variadic_t
Definition: star-height.hh:80