Vcsn  2.4
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(one,) {}
62  VCSN_RAT_VISIT(rweight, v) { v.sub()->accept(*this); }
63  VCSN_RAT_VISIT(shuffle, v) { visit_(v); }
64  VCSN_RAT_VISIT(star, v) { visit_(v); ++height_; }
65  VCSN_RAT_VISIT(transposition, v){ visit_(v); }
66  VCSN_RAT_VISIT(zero,) {}
67 
68  template <rat::type_t Type>
69  using unary_t = typename super_t::template unary_t<Type>;
70 
72  template <rat::exp::type_t Type>
73  void visit_(const unary_t<Type>& v)
74  {
75  height_ = recurse_(v.sub());
76  }
77 
78  template <rat::type_t Type>
79  using variadic_t = typename super_t::template variadic_t<Type>;
80 
82  template <rat::type_t Type>
83  void visit_(const variadic_t<Type>& n)
84  {
85  height_ = 0;
86  // The height of an n-ary is the max of its heights.
87  for (const auto& c : n)
88  height_ = std::max(height_, recurse_(c));
89  }
90 
91  using tuple_t = typename super_t::tuple_t;
92 
93  private:
94  template <bool = context_t::is_lat,
95  typename Dummy = void>
96  struct visit_tuple
97  {
99  template <size_t I>
100  auto tape_(const tuple_t& v)
101  {
102  return star_height(project<I>(visitor_.rs_), std::get<I>(v.sub()));
103  }
104 
106  template <size_t... I>
108  {
109  return std::max({tape_<I>(v)...});
110  }
111 
113  auto operator()(const tuple_t& v)
114  {
116  }
118  };
119 
120  template <typename Dummy>
121  struct visit_tuple<false, Dummy>
122  {
123  unsigned operator()(const tuple_t&)
124  {
126  }
128  };
129 
130  void visit(const tuple_t& v, std::true_type) override
131  {
132  height_ = std::max(height_, visit_tuple<>{*this}(v));
133  }
134 
138  unsigned height_;
139  };
140  } // namespace detail
141 
142 
143  template <typename ExpSet>
144  unsigned
145  star_height(const ExpSet& es, const typename ExpSet::value_t& e)
146  {
148  return s(e);
149  }
150 
151  namespace dyn
152  {
153  namespace detail
154  {
156  template <typename ExpSet>
157  unsigned
159  {
160  const auto& e = exp->as<ExpSet>();
161  return ::vcsn::star_height(e.valueset(), e.value());
162  }
163  }
164  }
165 } // namespace vcsn
auto tape_(const tuple_t &v)
The case of tape I.
Definition: star-height.hh:100
typename expressionset_t::value_t expression_t
Definition: star-height.hh:23
unsigned height_
The current star height.
Definition: star-height.hh:138
static constexpr const char * me()
Name of this algorithm, for error messages.
Definition: star-height.hh:28
auto rs
Definition: lift.hh:152
return res
Definition: multiply.hh:398
void visit_(const variadic_t< Type > &n)
Traverse variadic node.
Definition: star-height.hh:83
unsigned star_height(const ExpSet &es, const typename ExpSet::value_t &e)
Star height of an expression.
Definition: star-height.hh:145
typename super_t::template unary_t< Type > unary_t
Definition: star-height.hh:69
unsigned operator()(const expression_t &v)
The star height of v.
Definition: star-height.hh:35
auto operator()(const tuple_t &v)
Entry point.
Definition: star-height.hh:113
void visit_(const unary_t< Type > &v)
Traverse unary node.
Definition: star-height.hh:73
unsigned recurse_(const expression_t &v)
Easy recursion: the star height of v, saving height_.
Definition: star-height.hh:43
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
auto tapes_(const tuple_t &v, detail::index_sequence< I... >)
Info all the tapes.
Definition: star-height.hh:107
Definition: a-star.hh:8
unsigned star_height(const expression &exp)
Bridge.
Definition: star-height.hh:158
star_height_visitor(const expressionset_t &rs)
Definition: star-height.hh:30
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
typename expressionset_t::const_visitor super_t
Definition: star-height.hh:21
context_t_of< expressionset_t > context_t
Definition: star-height.hh:24
void visit(const tuple_t &v, std::true_type) override
Definition: star-height.hh:130
value_impl< detail::expression_tag > expression
Definition: fwd.hh:25
typename super_t::template variadic_t< Type > variadic_t
Definition: star-height.hh:79
VCSN_RAT_VISIT(transposition, v)
Definition: star-height.hh:65
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
typename super_t::tuple_t tuple_t
Definition: star-height.hh:91
typename super_t::node_t node_t
Definition: star-height.hh:25
auto conjunction(const Aut &a, const Auts &...as)
Build the (accessible part of the) conjunction.
Definition: conjunction.hh:622
const expressionset_t & rs_
The expressionset.
Definition: star-height.hh:136